Fix warning with -Wsign-compare -Wsystem-headers
[official-gcc.git] / gcc / gimplify.c
blob4a109aee27a681c3d1a4887f90a7c427517105e9
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 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2177 /* Quiet. */;
2178 else if (gimple_code (prev) == GIMPLE_LABEL
2179 && (label = gimple_label_label (as_a <glabel *> (prev)))
2180 && (l = find_label_entry (&labels, label)))
2181 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2182 "this statement may fall through");
2183 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2184 /* Try to be clever and don't warn when the statement
2185 can't actually fall through. */
2186 && gimple_stmt_may_fallthru (prev)
2187 && gimple_has_location (prev))
2188 warned_p = warning_at (gimple_location (prev),
2189 OPT_Wimplicit_fallthrough_,
2190 "this statement may fall through");
2191 if (warned_p)
2192 inform (gimple_location (next), "here");
2194 /* Mark this label as processed so as to prevent multiple
2195 warnings in nested switches. */
2196 FALLTHROUGH_LABEL_P (label) = true;
2198 /* So that next warn_implicit_fallthrough_r will start looking for
2199 a new sequence starting with this label. */
2200 gsi_prev (gsi_p);
2203 break;
2204 default:
2205 break;
2207 return NULL_TREE;
2210 /* Warn when a switch case falls through. */
2212 static void
2213 maybe_warn_implicit_fallthrough (gimple_seq seq)
2215 if (!warn_implicit_fallthrough)
2216 return;
2218 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2219 if (!(lang_GNU_C ()
2220 || lang_GNU_CXX ()
2221 || lang_GNU_OBJC ()))
2222 return;
2224 struct walk_stmt_info wi;
2225 memset (&wi, 0, sizeof (wi));
2226 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2229 /* Callback for walk_gimple_seq. */
2231 static tree
2232 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2233 struct walk_stmt_info *)
2235 gimple *stmt = gsi_stmt (*gsi_p);
2237 *handled_ops_p = true;
2238 switch (gimple_code (stmt))
2240 case GIMPLE_TRY:
2241 case GIMPLE_BIND:
2242 case GIMPLE_CATCH:
2243 case GIMPLE_EH_FILTER:
2244 case GIMPLE_TRANSACTION:
2245 /* Walk the sub-statements. */
2246 *handled_ops_p = false;
2247 break;
2248 case GIMPLE_CALL:
2249 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2251 gsi_remove (gsi_p, true);
2252 if (gsi_end_p (*gsi_p))
2253 return integer_zero_node;
2255 bool found = false;
2256 location_t loc = gimple_location (stmt);
2258 gimple_stmt_iterator gsi2 = *gsi_p;
2259 stmt = gsi_stmt (gsi2);
2260 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2262 /* Go on until the artificial label. */
2263 tree goto_dest = gimple_goto_dest (stmt);
2264 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2266 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2267 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2268 == goto_dest)
2269 break;
2272 /* Not found? Stop. */
2273 if (gsi_end_p (gsi2))
2274 break;
2276 /* Look one past it. */
2277 gsi_next (&gsi2);
2280 /* We're looking for a case label or default label here. */
2281 while (!gsi_end_p (gsi2))
2283 stmt = gsi_stmt (gsi2);
2284 if (gimple_code (stmt) == GIMPLE_LABEL)
2286 tree label = gimple_label_label (as_a <glabel *> (stmt));
2287 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2289 found = true;
2290 break;
2293 else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2295 else if (!is_gimple_debug (stmt))
2296 /* Anything else is not expected. */
2297 break;
2298 gsi_next (&gsi2);
2300 if (!found)
2301 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2302 "a case label or default label");
2304 break;
2305 default:
2306 break;
2308 return NULL_TREE;
2311 /* Expand all FALLTHROUGH () calls in SEQ. */
2313 static void
2314 expand_FALLTHROUGH (gimple_seq *seq_p)
2316 struct walk_stmt_info wi;
2317 memset (&wi, 0, sizeof (wi));
2318 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2322 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2323 branch to. */
2325 static enum gimplify_status
2326 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2328 tree switch_expr = *expr_p;
2329 gimple_seq switch_body_seq = NULL;
2330 enum gimplify_status ret;
2331 tree index_type = TREE_TYPE (switch_expr);
2332 if (index_type == NULL_TREE)
2333 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2335 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2336 fb_rvalue);
2337 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2338 return ret;
2340 if (SWITCH_BODY (switch_expr))
2342 vec<tree> labels;
2343 vec<tree> saved_labels;
2344 hash_set<tree> *saved_live_switch_vars = NULL;
2345 tree default_case = NULL_TREE;
2346 gswitch *switch_stmt;
2348 /* Save old labels, get new ones from body, then restore the old
2349 labels. Save all the things from the switch body to append after. */
2350 saved_labels = gimplify_ctxp->case_labels;
2351 gimplify_ctxp->case_labels.create (8);
2353 /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2354 saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2355 tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2356 if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2357 gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2358 else
2359 gimplify_ctxp->live_switch_vars = NULL;
2361 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2362 gimplify_ctxp->in_switch_expr = true;
2364 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2366 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2367 maybe_warn_switch_unreachable (switch_body_seq);
2368 maybe_warn_implicit_fallthrough (switch_body_seq);
2369 /* Only do this for the outermost GIMPLE_SWITCH. */
2370 if (!gimplify_ctxp->in_switch_expr)
2371 expand_FALLTHROUGH (&switch_body_seq);
2373 labels = gimplify_ctxp->case_labels;
2374 gimplify_ctxp->case_labels = saved_labels;
2376 if (gimplify_ctxp->live_switch_vars)
2378 gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0);
2379 delete gimplify_ctxp->live_switch_vars;
2381 gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2383 preprocess_case_label_vec_for_gimple (labels, index_type,
2384 &default_case);
2386 bool add_bind = false;
2387 if (!default_case)
2389 glabel *new_default;
2391 default_case
2392 = build_case_label (NULL_TREE, NULL_TREE,
2393 create_artificial_label (UNKNOWN_LOCATION));
2394 if (old_in_switch_expr)
2396 SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
2397 add_bind = true;
2399 new_default = gimple_build_label (CASE_LABEL (default_case));
2400 gimplify_seq_add_stmt (&switch_body_seq, new_default);
2402 else if (old_in_switch_expr)
2404 gimple *last = gimple_seq_last_stmt (switch_body_seq);
2405 if (last && gimple_code (last) == GIMPLE_LABEL)
2407 tree label = gimple_label_label (as_a <glabel *> (last));
2408 if (SWITCH_BREAK_LABEL_P (label))
2409 add_bind = true;
2413 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2414 default_case, labels);
2415 /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
2416 ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
2417 wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
2418 so that we can easily find the start and end of the switch
2419 statement. */
2420 if (add_bind)
2422 gimple_seq bind_body = NULL;
2423 gimplify_seq_add_stmt (&bind_body, switch_stmt);
2424 gimple_seq_add_seq (&bind_body, switch_body_seq);
2425 gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
2426 gimple_set_location (bind, EXPR_LOCATION (switch_expr));
2427 gimplify_seq_add_stmt (pre_p, bind);
2429 else
2431 gimplify_seq_add_stmt (pre_p, switch_stmt);
2432 gimplify_seq_add_seq (pre_p, switch_body_seq);
2434 labels.release ();
2436 else
2437 gcc_unreachable ();
2439 return GS_ALL_DONE;
2442 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2444 static enum gimplify_status
2445 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2447 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2448 == current_function_decl);
2450 tree label = LABEL_EXPR_LABEL (*expr_p);
2451 glabel *label_stmt = gimple_build_label (label);
2452 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2453 gimplify_seq_add_stmt (pre_p, label_stmt);
2455 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2456 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2457 NOT_TAKEN));
2458 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2459 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2460 TAKEN));
2462 return GS_ALL_DONE;
2465 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2467 static enum gimplify_status
2468 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2470 struct gimplify_ctx *ctxp;
2471 glabel *label_stmt;
2473 /* Invalid programs can play Duff's Device type games with, for example,
2474 #pragma omp parallel. At least in the C front end, we don't
2475 detect such invalid branches until after gimplification, in the
2476 diagnose_omp_blocks pass. */
2477 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2478 if (ctxp->case_labels.exists ())
2479 break;
2481 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
2482 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2483 ctxp->case_labels.safe_push (*expr_p);
2484 gimplify_seq_add_stmt (pre_p, label_stmt);
2486 return GS_ALL_DONE;
2489 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2490 if necessary. */
2492 tree
2493 build_and_jump (tree *label_p)
2495 if (label_p == NULL)
2496 /* If there's nowhere to jump, just fall through. */
2497 return NULL_TREE;
2499 if (*label_p == NULL_TREE)
2501 tree label = create_artificial_label (UNKNOWN_LOCATION);
2502 *label_p = label;
2505 return build1 (GOTO_EXPR, void_type_node, *label_p);
2508 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2509 This also involves building a label to jump to and communicating it to
2510 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2512 static enum gimplify_status
2513 gimplify_exit_expr (tree *expr_p)
2515 tree cond = TREE_OPERAND (*expr_p, 0);
2516 tree expr;
2518 expr = build_and_jump (&gimplify_ctxp->exit_label);
2519 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2520 *expr_p = expr;
2522 return GS_OK;
2525 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2526 different from its canonical type, wrap the whole thing inside a
2527 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2528 type.
2530 The canonical type of a COMPONENT_REF is the type of the field being
2531 referenced--unless the field is a bit-field which can be read directly
2532 in a smaller mode, in which case the canonical type is the
2533 sign-appropriate type corresponding to that mode. */
2535 static void
2536 canonicalize_component_ref (tree *expr_p)
2538 tree expr = *expr_p;
2539 tree type;
2541 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2543 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2544 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2545 else
2546 type = TREE_TYPE (TREE_OPERAND (expr, 1));
2548 /* One could argue that all the stuff below is not necessary for
2549 the non-bitfield case and declare it a FE error if type
2550 adjustment would be needed. */
2551 if (TREE_TYPE (expr) != type)
2553 #ifdef ENABLE_TYPES_CHECKING
2554 tree old_type = TREE_TYPE (expr);
2555 #endif
2556 int type_quals;
2558 /* We need to preserve qualifiers and propagate them from
2559 operand 0. */
2560 type_quals = TYPE_QUALS (type)
2561 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2562 if (TYPE_QUALS (type) != type_quals)
2563 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2565 /* Set the type of the COMPONENT_REF to the underlying type. */
2566 TREE_TYPE (expr) = type;
2568 #ifdef ENABLE_TYPES_CHECKING
2569 /* It is now a FE error, if the conversion from the canonical
2570 type to the original expression type is not useless. */
2571 gcc_assert (useless_type_conversion_p (old_type, type));
2572 #endif
2576 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2577 to foo, embed that change in the ADDR_EXPR by converting
2578 T array[U];
2579 (T *)&array
2581 &array[L]
2582 where L is the lower bound. For simplicity, only do this for constant
2583 lower bound.
2584 The constraint is that the type of &array[L] is trivially convertible
2585 to T *. */
2587 static void
2588 canonicalize_addr_expr (tree *expr_p)
2590 tree expr = *expr_p;
2591 tree addr_expr = TREE_OPERAND (expr, 0);
2592 tree datype, ddatype, pddatype;
2594 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2595 if (!POINTER_TYPE_P (TREE_TYPE (expr))
2596 || TREE_CODE (addr_expr) != ADDR_EXPR)
2597 return;
2599 /* The addr_expr type should be a pointer to an array. */
2600 datype = TREE_TYPE (TREE_TYPE (addr_expr));
2601 if (TREE_CODE (datype) != ARRAY_TYPE)
2602 return;
2604 /* The pointer to element type shall be trivially convertible to
2605 the expression pointer type. */
2606 ddatype = TREE_TYPE (datype);
2607 pddatype = build_pointer_type (ddatype);
2608 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2609 pddatype))
2610 return;
2612 /* The lower bound and element sizes must be constant. */
2613 if (!TYPE_SIZE_UNIT (ddatype)
2614 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2615 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2616 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2617 return;
2619 /* All checks succeeded. Build a new node to merge the cast. */
2620 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2621 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2622 NULL_TREE, NULL_TREE);
2623 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2625 /* We can have stripped a required restrict qualifier above. */
2626 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2627 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2630 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2631 underneath as appropriate. */
2633 static enum gimplify_status
2634 gimplify_conversion (tree *expr_p)
2636 location_t loc = EXPR_LOCATION (*expr_p);
2637 gcc_assert (CONVERT_EXPR_P (*expr_p));
2639 /* Then strip away all but the outermost conversion. */
2640 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2642 /* And remove the outermost conversion if it's useless. */
2643 if (tree_ssa_useless_type_conversion (*expr_p))
2644 *expr_p = TREE_OPERAND (*expr_p, 0);
2646 /* If we still have a conversion at the toplevel,
2647 then canonicalize some constructs. */
2648 if (CONVERT_EXPR_P (*expr_p))
2650 tree sub = TREE_OPERAND (*expr_p, 0);
2652 /* If a NOP conversion is changing the type of a COMPONENT_REF
2653 expression, then canonicalize its type now in order to expose more
2654 redundant conversions. */
2655 if (TREE_CODE (sub) == COMPONENT_REF)
2656 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2658 /* If a NOP conversion is changing a pointer to array of foo
2659 to a pointer to foo, embed that change in the ADDR_EXPR. */
2660 else if (TREE_CODE (sub) == ADDR_EXPR)
2661 canonicalize_addr_expr (expr_p);
2664 /* If we have a conversion to a non-register type force the
2665 use of a VIEW_CONVERT_EXPR instead. */
2666 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2667 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2668 TREE_OPERAND (*expr_p, 0));
2670 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2671 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2672 TREE_SET_CODE (*expr_p, NOP_EXPR);
2674 return GS_OK;
2677 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2678 DECL_VALUE_EXPR, and it's worth re-examining things. */
2680 static enum gimplify_status
2681 gimplify_var_or_parm_decl (tree *expr_p)
2683 tree decl = *expr_p;
2685 /* ??? If this is a local variable, and it has not been seen in any
2686 outer BIND_EXPR, then it's probably the result of a duplicate
2687 declaration, for which we've already issued an error. It would
2688 be really nice if the front end wouldn't leak these at all.
2689 Currently the only known culprit is C++ destructors, as seen
2690 in g++.old-deja/g++.jason/binding.C. */
2691 if (VAR_P (decl)
2692 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2693 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2694 && decl_function_context (decl) == current_function_decl)
2696 gcc_assert (seen_error ());
2697 return GS_ERROR;
2700 /* When within an OMP context, notice uses of variables. */
2701 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2702 return GS_ALL_DONE;
2704 /* If the decl is an alias for another expression, substitute it now. */
2705 if (DECL_HAS_VALUE_EXPR_P (decl))
2707 *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
2708 return GS_OK;
2711 return GS_ALL_DONE;
2714 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2716 static void
2717 recalculate_side_effects (tree t)
2719 enum tree_code code = TREE_CODE (t);
2720 int len = TREE_OPERAND_LENGTH (t);
2721 int i;
2723 switch (TREE_CODE_CLASS (code))
2725 case tcc_expression:
2726 switch (code)
2728 case INIT_EXPR:
2729 case MODIFY_EXPR:
2730 case VA_ARG_EXPR:
2731 case PREDECREMENT_EXPR:
2732 case PREINCREMENT_EXPR:
2733 case POSTDECREMENT_EXPR:
2734 case POSTINCREMENT_EXPR:
2735 /* All of these have side-effects, no matter what their
2736 operands are. */
2737 return;
2739 default:
2740 break;
2742 /* Fall through. */
2744 case tcc_comparison: /* a comparison expression */
2745 case tcc_unary: /* a unary arithmetic expression */
2746 case tcc_binary: /* a binary arithmetic expression */
2747 case tcc_reference: /* a reference */
2748 case tcc_vl_exp: /* a function call */
2749 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2750 for (i = 0; i < len; ++i)
2752 tree op = TREE_OPERAND (t, i);
2753 if (op && TREE_SIDE_EFFECTS (op))
2754 TREE_SIDE_EFFECTS (t) = 1;
2756 break;
2758 case tcc_constant:
2759 /* No side-effects. */
2760 return;
2762 default:
2763 gcc_unreachable ();
2767 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2768 node *EXPR_P.
2770 compound_lval
2771 : min_lval '[' val ']'
2772 | min_lval '.' ID
2773 | compound_lval '[' val ']'
2774 | compound_lval '.' ID
2776 This is not part of the original SIMPLE definition, which separates
2777 array and member references, but it seems reasonable to handle them
2778 together. Also, this way we don't run into problems with union
2779 aliasing; gcc requires that for accesses through a union to alias, the
2780 union reference must be explicit, which was not always the case when we
2781 were splitting up array and member refs.
2783 PRE_P points to the sequence where side effects that must happen before
2784 *EXPR_P should be stored.
2786 POST_P points to the sequence where side effects that must happen after
2787 *EXPR_P should be stored. */
2789 static enum gimplify_status
2790 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2791 fallback_t fallback)
2793 tree *p;
2794 enum gimplify_status ret = GS_ALL_DONE, tret;
2795 int i;
2796 location_t loc = EXPR_LOCATION (*expr_p);
2797 tree expr = *expr_p;
2799 /* Create a stack of the subexpressions so later we can walk them in
2800 order from inner to outer. */
2801 auto_vec<tree, 10> expr_stack;
2803 /* We can handle anything that get_inner_reference can deal with. */
2804 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2806 restart:
2807 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2808 if (TREE_CODE (*p) == INDIRECT_REF)
2809 *p = fold_indirect_ref_loc (loc, *p);
2811 if (handled_component_p (*p))
2813 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2814 additional COMPONENT_REFs. */
2815 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2816 && gimplify_var_or_parm_decl (p) == GS_OK)
2817 goto restart;
2818 else
2819 break;
2821 expr_stack.safe_push (*p);
2824 gcc_assert (expr_stack.length ());
2826 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2827 walked through and P points to the innermost expression.
2829 Java requires that we elaborated nodes in source order. That
2830 means we must gimplify the inner expression followed by each of
2831 the indices, in order. But we can't gimplify the inner
2832 expression until we deal with any variable bounds, sizes, or
2833 positions in order to deal with PLACEHOLDER_EXPRs.
2835 So we do this in three steps. First we deal with the annotations
2836 for any variables in the components, then we gimplify the base,
2837 then we gimplify any indices, from left to right. */
2838 for (i = expr_stack.length () - 1; i >= 0; i--)
2840 tree t = expr_stack[i];
2842 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2844 /* Gimplify the low bound and element type size and put them into
2845 the ARRAY_REF. If these values are set, they have already been
2846 gimplified. */
2847 if (TREE_OPERAND (t, 2) == NULL_TREE)
2849 tree low = unshare_expr (array_ref_low_bound (t));
2850 if (!is_gimple_min_invariant (low))
2852 TREE_OPERAND (t, 2) = low;
2853 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2854 post_p, is_gimple_reg,
2855 fb_rvalue);
2856 ret = MIN (ret, tret);
2859 else
2861 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2862 is_gimple_reg, fb_rvalue);
2863 ret = MIN (ret, tret);
2866 if (TREE_OPERAND (t, 3) == NULL_TREE)
2868 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2869 tree elmt_size = unshare_expr (array_ref_element_size (t));
2870 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2872 /* Divide the element size by the alignment of the element
2873 type (above). */
2874 elmt_size
2875 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2877 if (!is_gimple_min_invariant (elmt_size))
2879 TREE_OPERAND (t, 3) = elmt_size;
2880 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2881 post_p, is_gimple_reg,
2882 fb_rvalue);
2883 ret = MIN (ret, tret);
2886 else
2888 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2889 is_gimple_reg, fb_rvalue);
2890 ret = MIN (ret, tret);
2893 else if (TREE_CODE (t) == COMPONENT_REF)
2895 /* Set the field offset into T and gimplify it. */
2896 if (TREE_OPERAND (t, 2) == NULL_TREE)
2898 tree offset = unshare_expr (component_ref_field_offset (t));
2899 tree field = TREE_OPERAND (t, 1);
2900 tree factor
2901 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2903 /* Divide the offset by its alignment. */
2904 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2906 if (!is_gimple_min_invariant (offset))
2908 TREE_OPERAND (t, 2) = offset;
2909 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2910 post_p, is_gimple_reg,
2911 fb_rvalue);
2912 ret = MIN (ret, tret);
2915 else
2917 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2918 is_gimple_reg, fb_rvalue);
2919 ret = MIN (ret, tret);
2924 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2925 so as to match the min_lval predicate. Failure to do so may result
2926 in the creation of large aggregate temporaries. */
2927 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2928 fallback | fb_lvalue);
2929 ret = MIN (ret, tret);
2931 /* And finally, the indices and operands of ARRAY_REF. During this
2932 loop we also remove any useless conversions. */
2933 for (; expr_stack.length () > 0; )
2935 tree t = expr_stack.pop ();
2937 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2939 /* Gimplify the dimension. */
2940 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2942 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2943 is_gimple_val, fb_rvalue);
2944 ret = MIN (ret, tret);
2948 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2950 /* The innermost expression P may have originally had
2951 TREE_SIDE_EFFECTS set which would have caused all the outer
2952 expressions in *EXPR_P leading to P to also have had
2953 TREE_SIDE_EFFECTS set. */
2954 recalculate_side_effects (t);
2957 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2958 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2960 canonicalize_component_ref (expr_p);
2963 expr_stack.release ();
2965 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2967 return ret;
2970 /* Gimplify the self modifying expression pointed to by EXPR_P
2971 (++, --, +=, -=).
2973 PRE_P points to the list where side effects that must happen before
2974 *EXPR_P should be stored.
2976 POST_P points to the list where side effects that must happen after
2977 *EXPR_P should be stored.
2979 WANT_VALUE is nonzero iff we want to use the value of this expression
2980 in another expression.
2982 ARITH_TYPE is the type the computation should be performed in. */
2984 enum gimplify_status
2985 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2986 bool want_value, tree arith_type)
2988 enum tree_code code;
2989 tree lhs, lvalue, rhs, t1;
2990 gimple_seq post = NULL, *orig_post_p = post_p;
2991 bool postfix;
2992 enum tree_code arith_code;
2993 enum gimplify_status ret;
2994 location_t loc = EXPR_LOCATION (*expr_p);
2996 code = TREE_CODE (*expr_p);
2998 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2999 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3001 /* Prefix or postfix? */
3002 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3003 /* Faster to treat as prefix if result is not used. */
3004 postfix = want_value;
3005 else
3006 postfix = false;
3008 /* For postfix, make sure the inner expression's post side effects
3009 are executed after side effects from this expression. */
3010 if (postfix)
3011 post_p = &post;
3013 /* Add or subtract? */
3014 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3015 arith_code = PLUS_EXPR;
3016 else
3017 arith_code = MINUS_EXPR;
3019 /* Gimplify the LHS into a GIMPLE lvalue. */
3020 lvalue = TREE_OPERAND (*expr_p, 0);
3021 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3022 if (ret == GS_ERROR)
3023 return ret;
3025 /* Extract the operands to the arithmetic operation. */
3026 lhs = lvalue;
3027 rhs = TREE_OPERAND (*expr_p, 1);
3029 /* For postfix operator, we evaluate the LHS to an rvalue and then use
3030 that as the result value and in the postqueue operation. */
3031 if (postfix)
3033 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3034 if (ret == GS_ERROR)
3035 return ret;
3037 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
3040 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3041 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3043 rhs = convert_to_ptrofftype_loc (loc, rhs);
3044 if (arith_code == MINUS_EXPR)
3045 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3046 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3048 else
3049 t1 = fold_convert (TREE_TYPE (*expr_p),
3050 fold_build2 (arith_code, arith_type,
3051 fold_convert (arith_type, lhs),
3052 fold_convert (arith_type, rhs)));
3054 if (postfix)
3056 gimplify_assign (lvalue, t1, pre_p);
3057 gimplify_seq_add_seq (orig_post_p, post);
3058 *expr_p = lhs;
3059 return GS_ALL_DONE;
3061 else
3063 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3064 return GS_OK;
3068 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3070 static void
3071 maybe_with_size_expr (tree *expr_p)
3073 tree expr = *expr_p;
3074 tree type = TREE_TYPE (expr);
3075 tree size;
3077 /* If we've already wrapped this or the type is error_mark_node, we can't do
3078 anything. */
3079 if (TREE_CODE (expr) == WITH_SIZE_EXPR
3080 || type == error_mark_node)
3081 return;
3083 /* If the size isn't known or is a constant, we have nothing to do. */
3084 size = TYPE_SIZE_UNIT (type);
3085 if (!size || poly_int_tree_p (size))
3086 return;
3088 /* Otherwise, make a WITH_SIZE_EXPR. */
3089 size = unshare_expr (size);
3090 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3091 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3094 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3095 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3096 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3097 gimplified to an SSA name. */
3099 enum gimplify_status
3100 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3101 bool allow_ssa)
3103 bool (*test) (tree);
3104 fallback_t fb;
3106 /* In general, we allow lvalues for function arguments to avoid
3107 extra overhead of copying large aggregates out of even larger
3108 aggregates into temporaries only to copy the temporaries to
3109 the argument list. Make optimizers happy by pulling out to
3110 temporaries those types that fit in registers. */
3111 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3112 test = is_gimple_val, fb = fb_rvalue;
3113 else
3115 test = is_gimple_lvalue, fb = fb_either;
3116 /* Also strip a TARGET_EXPR that would force an extra copy. */
3117 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3119 tree init = TARGET_EXPR_INITIAL (*arg_p);
3120 if (init
3121 && !VOID_TYPE_P (TREE_TYPE (init)))
3122 *arg_p = init;
3126 /* If this is a variable sized type, we must remember the size. */
3127 maybe_with_size_expr (arg_p);
3129 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3130 /* Make sure arguments have the same location as the function call
3131 itself. */
3132 protected_set_expr_location (*arg_p, call_location);
3134 /* There is a sequence point before a function call. Side effects in
3135 the argument list must occur before the actual call. So, when
3136 gimplifying arguments, force gimplify_expr to use an internal
3137 post queue which is then appended to the end of PRE_P. */
3138 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3141 /* Don't fold inside offloading or taskreg regions: it can break code by
3142 adding decl references that weren't in the source. We'll do it during
3143 omplower pass instead. */
3145 static bool
3146 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3148 struct gimplify_omp_ctx *ctx;
3149 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3150 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3151 return false;
3152 return fold_stmt (gsi);
3155 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3156 WANT_VALUE is true if the result of the call is desired. */
3158 static enum gimplify_status
3159 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3161 tree fndecl, parms, p, fnptrtype;
3162 enum gimplify_status ret;
3163 int i, nargs;
3164 gcall *call;
3165 bool builtin_va_start_p = false;
3166 location_t loc = EXPR_LOCATION (*expr_p);
3168 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3170 /* For reliable diagnostics during inlining, it is necessary that
3171 every call_expr be annotated with file and line. */
3172 if (! EXPR_HAS_LOCATION (*expr_p))
3173 SET_EXPR_LOCATION (*expr_p, input_location);
3175 /* Gimplify internal functions created in the FEs. */
3176 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3178 if (want_value)
3179 return GS_ALL_DONE;
3181 nargs = call_expr_nargs (*expr_p);
3182 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3183 auto_vec<tree> vargs (nargs);
3185 for (i = 0; i < nargs; i++)
3187 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3188 EXPR_LOCATION (*expr_p));
3189 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3192 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3193 gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
3194 gimplify_seq_add_stmt (pre_p, call);
3195 return GS_ALL_DONE;
3198 /* This may be a call to a builtin function.
3200 Builtin function calls may be transformed into different
3201 (and more efficient) builtin function calls under certain
3202 circumstances. Unfortunately, gimplification can muck things
3203 up enough that the builtin expanders are not aware that certain
3204 transformations are still valid.
3206 So we attempt transformation/gimplification of the call before
3207 we gimplify the CALL_EXPR. At this time we do not manage to
3208 transform all calls in the same manner as the expanders do, but
3209 we do transform most of them. */
3210 fndecl = get_callee_fndecl (*expr_p);
3211 if (fndecl
3212 && DECL_BUILT_IN_CLASS (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 && DECL_BUILT_IN (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 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
3300 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
3301 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
3303 tree call = *expr_p;
3305 --nargs;
3306 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3307 CALL_EXPR_FN (call),
3308 nargs, CALL_EXPR_ARGP (call));
3310 /* Copy all CALL_EXPR flags, location and block, except
3311 CALL_EXPR_VA_ARG_PACK flag. */
3312 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3313 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3314 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3315 = CALL_EXPR_RETURN_SLOT_OPT (call);
3316 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3317 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3319 /* Set CALL_EXPR_VA_ARG_PACK. */
3320 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3324 /* If the call returns twice then after building the CFG the call
3325 argument computations will no longer dominate the call because
3326 we add an abnormal incoming edge to the call. So do not use SSA
3327 vars there. */
3328 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3330 /* Gimplify the function arguments. */
3331 if (nargs > 0)
3333 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3334 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3335 PUSH_ARGS_REVERSED ? i-- : i++)
3337 enum gimplify_status t;
3339 /* Avoid gimplifying the second argument to va_start, which needs to
3340 be the plain PARM_DECL. */
3341 if ((i != 1) || !builtin_va_start_p)
3343 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3344 EXPR_LOCATION (*expr_p), ! returns_twice);
3346 if (t == GS_ERROR)
3347 ret = GS_ERROR;
3352 /* Gimplify the static chain. */
3353 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3355 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3356 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3357 else
3359 enum gimplify_status t;
3360 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3361 EXPR_LOCATION (*expr_p), ! returns_twice);
3362 if (t == GS_ERROR)
3363 ret = GS_ERROR;
3367 /* Verify the function result. */
3368 if (want_value && fndecl
3369 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3371 error_at (loc, "using result of function returning %<void%>");
3372 ret = GS_ERROR;
3375 /* Try this again in case gimplification exposed something. */
3376 if (ret != GS_ERROR)
3378 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3380 if (new_tree && new_tree != *expr_p)
3382 /* There was a transformation of this call which computes the
3383 same value, but in a more efficient way. Return and try
3384 again. */
3385 *expr_p = new_tree;
3386 return GS_OK;
3389 else
3391 *expr_p = error_mark_node;
3392 return GS_ERROR;
3395 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3396 decl. This allows us to eliminate redundant or useless
3397 calls to "const" functions. */
3398 if (TREE_CODE (*expr_p) == CALL_EXPR)
3400 int flags = call_expr_flags (*expr_p);
3401 if (flags & (ECF_CONST | ECF_PURE)
3402 /* An infinite loop is considered a side effect. */
3403 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3404 TREE_SIDE_EFFECTS (*expr_p) = 0;
3407 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3408 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3409 form and delegate the creation of a GIMPLE_CALL to
3410 gimplify_modify_expr. This is always possible because when
3411 WANT_VALUE is true, the caller wants the result of this call into
3412 a temporary, which means that we will emit an INIT_EXPR in
3413 internal_get_tmp_var which will then be handled by
3414 gimplify_modify_expr. */
3415 if (!want_value)
3417 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3418 have to do is replicate it as a GIMPLE_CALL tuple. */
3419 gimple_stmt_iterator gsi;
3420 call = gimple_build_call_from_tree (*expr_p, fnptrtype);
3421 notice_special_calls (call);
3422 gimplify_seq_add_stmt (pre_p, call);
3423 gsi = gsi_last (*pre_p);
3424 maybe_fold_stmt (&gsi);
3425 *expr_p = NULL_TREE;
3427 else
3428 /* Remember the original function type. */
3429 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3430 CALL_EXPR_FN (*expr_p));
3432 return ret;
3435 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3436 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3438 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3439 condition is true or false, respectively. If null, we should generate
3440 our own to skip over the evaluation of this specific expression.
3442 LOCUS is the source location of the COND_EXPR.
3444 This function is the tree equivalent of do_jump.
3446 shortcut_cond_r should only be called by shortcut_cond_expr. */
3448 static tree
3449 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3450 location_t locus)
3452 tree local_label = NULL_TREE;
3453 tree t, expr = NULL;
3455 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3456 retain the shortcut semantics. Just insert the gotos here;
3457 shortcut_cond_expr will append the real blocks later. */
3458 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3460 location_t new_locus;
3462 /* Turn if (a && b) into
3464 if (a); else goto no;
3465 if (b) goto yes; else goto no;
3466 (no:) */
3468 if (false_label_p == NULL)
3469 false_label_p = &local_label;
3471 /* Keep the original source location on the first 'if'. */
3472 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3473 append_to_statement_list (t, &expr);
3475 /* Set the source location of the && on the second 'if'. */
3476 new_locus = rexpr_location (pred, locus);
3477 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3478 new_locus);
3479 append_to_statement_list (t, &expr);
3481 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3483 location_t new_locus;
3485 /* Turn if (a || b) into
3487 if (a) goto yes;
3488 if (b) goto yes; else goto no;
3489 (yes:) */
3491 if (true_label_p == NULL)
3492 true_label_p = &local_label;
3494 /* Keep the original source location on the first 'if'. */
3495 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3496 append_to_statement_list (t, &expr);
3498 /* Set the source location of the || on the second 'if'. */
3499 new_locus = rexpr_location (pred, locus);
3500 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3501 new_locus);
3502 append_to_statement_list (t, &expr);
3504 else if (TREE_CODE (pred) == COND_EXPR
3505 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3506 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3508 location_t new_locus;
3510 /* As long as we're messing with gotos, turn if (a ? b : c) into
3511 if (a)
3512 if (b) goto yes; else goto no;
3513 else
3514 if (c) goto yes; else goto no;
3516 Don't do this if one of the arms has void type, which can happen
3517 in C++ when the arm is throw. */
3519 /* Keep the original source location on the first 'if'. Set the source
3520 location of the ? on the second 'if'. */
3521 new_locus = rexpr_location (pred, locus);
3522 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3523 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3524 false_label_p, locus),
3525 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3526 false_label_p, new_locus));
3528 else
3530 expr = build3 (COND_EXPR, void_type_node, pred,
3531 build_and_jump (true_label_p),
3532 build_and_jump (false_label_p));
3533 SET_EXPR_LOCATION (expr, locus);
3536 if (local_label)
3538 t = build1 (LABEL_EXPR, void_type_node, local_label);
3539 append_to_statement_list (t, &expr);
3542 return expr;
3545 /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
3546 any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
3547 statement, if it is the last one. Otherwise, return NULL. */
3549 static tree
3550 find_goto (tree expr)
3552 if (!expr)
3553 return NULL_TREE;
3555 if (TREE_CODE (expr) == GOTO_EXPR)
3556 return expr;
3558 if (TREE_CODE (expr) != STATEMENT_LIST)
3559 return NULL_TREE;
3561 tree_stmt_iterator i = tsi_start (expr);
3563 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
3564 tsi_next (&i);
3566 if (!tsi_one_before_end_p (i))
3567 return NULL_TREE;
3569 return find_goto (tsi_stmt (i));
3572 /* Same as find_goto, except that it returns NULL if the destination
3573 is not a LABEL_DECL. */
3575 static inline tree
3576 find_goto_label (tree expr)
3578 tree dest = find_goto (expr);
3579 if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
3580 return dest;
3581 return NULL_TREE;
3584 /* Given a conditional expression EXPR with short-circuit boolean
3585 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3586 predicate apart into the equivalent sequence of conditionals. */
3588 static tree
3589 shortcut_cond_expr (tree expr)
3591 tree pred = TREE_OPERAND (expr, 0);
3592 tree then_ = TREE_OPERAND (expr, 1);
3593 tree else_ = TREE_OPERAND (expr, 2);
3594 tree true_label, false_label, end_label, t;
3595 tree *true_label_p;
3596 tree *false_label_p;
3597 bool emit_end, emit_false, jump_over_else;
3598 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3599 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3601 /* First do simple transformations. */
3602 if (!else_se)
3604 /* If there is no 'else', turn
3605 if (a && b) then c
3606 into
3607 if (a) if (b) then c. */
3608 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3610 /* Keep the original source location on the first 'if'. */
3611 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3612 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3613 /* Set the source location of the && on the second 'if'. */
3614 if (rexpr_has_location (pred))
3615 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3616 then_ = shortcut_cond_expr (expr);
3617 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3618 pred = TREE_OPERAND (pred, 0);
3619 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3620 SET_EXPR_LOCATION (expr, locus);
3624 if (!then_se)
3626 /* If there is no 'then', turn
3627 if (a || b); else d
3628 into
3629 if (a); else if (b); else d. */
3630 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3632 /* Keep the original source location on the first 'if'. */
3633 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3634 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3635 /* Set the source location of the || on the second 'if'. */
3636 if (rexpr_has_location (pred))
3637 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3638 else_ = shortcut_cond_expr (expr);
3639 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3640 pred = TREE_OPERAND (pred, 0);
3641 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3642 SET_EXPR_LOCATION (expr, locus);
3646 /* If we're done, great. */
3647 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3648 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3649 return expr;
3651 /* Otherwise we need to mess with gotos. Change
3652 if (a) c; else d;
3654 if (a); else goto no;
3655 c; goto end;
3656 no: d; end:
3657 and recursively gimplify the condition. */
3659 true_label = false_label = end_label = NULL_TREE;
3661 /* If our arms just jump somewhere, hijack those labels so we don't
3662 generate jumps to jumps. */
3664 if (tree then_goto = find_goto_label (then_))
3666 true_label = GOTO_DESTINATION (then_goto);
3667 then_ = NULL;
3668 then_se = false;
3671 if (tree else_goto = find_goto_label (else_))
3673 false_label = GOTO_DESTINATION (else_goto);
3674 else_ = NULL;
3675 else_se = false;
3678 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3679 if (true_label)
3680 true_label_p = &true_label;
3681 else
3682 true_label_p = NULL;
3684 /* The 'else' branch also needs a label if it contains interesting code. */
3685 if (false_label || else_se)
3686 false_label_p = &false_label;
3687 else
3688 false_label_p = NULL;
3690 /* If there was nothing else in our arms, just forward the label(s). */
3691 if (!then_se && !else_se)
3692 return shortcut_cond_r (pred, true_label_p, false_label_p,
3693 EXPR_LOC_OR_LOC (expr, input_location));
3695 /* If our last subexpression already has a terminal label, reuse it. */
3696 if (else_se)
3697 t = expr_last (else_);
3698 else if (then_se)
3699 t = expr_last (then_);
3700 else
3701 t = NULL;
3702 if (t && TREE_CODE (t) == LABEL_EXPR)
3703 end_label = LABEL_EXPR_LABEL (t);
3705 /* If we don't care about jumping to the 'else' branch, jump to the end
3706 if the condition is false. */
3707 if (!false_label_p)
3708 false_label_p = &end_label;
3710 /* We only want to emit these labels if we aren't hijacking them. */
3711 emit_end = (end_label == NULL_TREE);
3712 emit_false = (false_label == NULL_TREE);
3714 /* We only emit the jump over the else clause if we have to--if the
3715 then clause may fall through. Otherwise we can wind up with a
3716 useless jump and a useless label at the end of gimplified code,
3717 which will cause us to think that this conditional as a whole
3718 falls through even if it doesn't. If we then inline a function
3719 which ends with such a condition, that can cause us to issue an
3720 inappropriate warning about control reaching the end of a
3721 non-void function. */
3722 jump_over_else = block_may_fallthru (then_);
3724 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3725 EXPR_LOC_OR_LOC (expr, input_location));
3727 expr = NULL;
3728 append_to_statement_list (pred, &expr);
3730 append_to_statement_list (then_, &expr);
3731 if (else_se)
3733 if (jump_over_else)
3735 tree last = expr_last (expr);
3736 t = build_and_jump (&end_label);
3737 if (rexpr_has_location (last))
3738 SET_EXPR_LOCATION (t, rexpr_location (last));
3739 append_to_statement_list (t, &expr);
3741 if (emit_false)
3743 t = build1 (LABEL_EXPR, void_type_node, false_label);
3744 append_to_statement_list (t, &expr);
3746 append_to_statement_list (else_, &expr);
3748 if (emit_end && end_label)
3750 t = build1 (LABEL_EXPR, void_type_node, end_label);
3751 append_to_statement_list (t, &expr);
3754 return expr;
3757 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3759 tree
3760 gimple_boolify (tree expr)
3762 tree type = TREE_TYPE (expr);
3763 location_t loc = EXPR_LOCATION (expr);
3765 if (TREE_CODE (expr) == NE_EXPR
3766 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3767 && integer_zerop (TREE_OPERAND (expr, 1)))
3769 tree call = TREE_OPERAND (expr, 0);
3770 tree fn = get_callee_fndecl (call);
3772 /* For __builtin_expect ((long) (x), y) recurse into x as well
3773 if x is truth_value_p. */
3774 if (fn
3775 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3776 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3777 && call_expr_nargs (call) == 2)
3779 tree arg = CALL_EXPR_ARG (call, 0);
3780 if (arg)
3782 if (TREE_CODE (arg) == NOP_EXPR
3783 && TREE_TYPE (arg) == TREE_TYPE (call))
3784 arg = TREE_OPERAND (arg, 0);
3785 if (truth_value_p (TREE_CODE (arg)))
3787 arg = gimple_boolify (arg);
3788 CALL_EXPR_ARG (call, 0)
3789 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3795 switch (TREE_CODE (expr))
3797 case TRUTH_AND_EXPR:
3798 case TRUTH_OR_EXPR:
3799 case TRUTH_XOR_EXPR:
3800 case TRUTH_ANDIF_EXPR:
3801 case TRUTH_ORIF_EXPR:
3802 /* Also boolify the arguments of truth exprs. */
3803 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3804 /* FALLTHRU */
3806 case TRUTH_NOT_EXPR:
3807 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3809 /* These expressions always produce boolean results. */
3810 if (TREE_CODE (type) != BOOLEAN_TYPE)
3811 TREE_TYPE (expr) = boolean_type_node;
3812 return expr;
3814 case ANNOTATE_EXPR:
3815 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3817 case annot_expr_ivdep_kind:
3818 case annot_expr_unroll_kind:
3819 case annot_expr_no_vector_kind:
3820 case annot_expr_vector_kind:
3821 case annot_expr_parallel_kind:
3822 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3823 if (TREE_CODE (type) != BOOLEAN_TYPE)
3824 TREE_TYPE (expr) = boolean_type_node;
3825 return expr;
3826 default:
3827 gcc_unreachable ();
3830 default:
3831 if (COMPARISON_CLASS_P (expr))
3833 /* There expressions always prduce boolean results. */
3834 if (TREE_CODE (type) != BOOLEAN_TYPE)
3835 TREE_TYPE (expr) = boolean_type_node;
3836 return expr;
3838 /* Other expressions that get here must have boolean values, but
3839 might need to be converted to the appropriate mode. */
3840 if (TREE_CODE (type) == BOOLEAN_TYPE)
3841 return expr;
3842 return fold_convert_loc (loc, boolean_type_node, expr);
3846 /* Given a conditional expression *EXPR_P without side effects, gimplify
3847 its operands. New statements are inserted to PRE_P. */
3849 static enum gimplify_status
3850 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3852 tree expr = *expr_p, cond;
3853 enum gimplify_status ret, tret;
3854 enum tree_code code;
3856 cond = gimple_boolify (COND_EXPR_COND (expr));
3858 /* We need to handle && and || specially, as their gimplification
3859 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3860 code = TREE_CODE (cond);
3861 if (code == TRUTH_ANDIF_EXPR)
3862 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3863 else if (code == TRUTH_ORIF_EXPR)
3864 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3865 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3866 COND_EXPR_COND (*expr_p) = cond;
3868 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3869 is_gimple_val, fb_rvalue);
3870 ret = MIN (ret, tret);
3871 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3872 is_gimple_val, fb_rvalue);
3874 return MIN (ret, tret);
3877 /* Return true if evaluating EXPR could trap.
3878 EXPR is GENERIC, while tree_could_trap_p can be called
3879 only on GIMPLE. */
3881 bool
3882 generic_expr_could_trap_p (tree expr)
3884 unsigned i, n;
3886 if (!expr || is_gimple_val (expr))
3887 return false;
3889 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3890 return true;
3892 n = TREE_OPERAND_LENGTH (expr);
3893 for (i = 0; i < n; i++)
3894 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3895 return true;
3897 return false;
3900 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3901 into
3903 if (p) if (p)
3904 t1 = a; a;
3905 else or else
3906 t1 = b; b;
3909 The second form is used when *EXPR_P is of type void.
3911 PRE_P points to the list where side effects that must happen before
3912 *EXPR_P should be stored. */
3914 static enum gimplify_status
3915 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3917 tree expr = *expr_p;
3918 tree type = TREE_TYPE (expr);
3919 location_t loc = EXPR_LOCATION (expr);
3920 tree tmp, arm1, arm2;
3921 enum gimplify_status ret;
3922 tree label_true, label_false, label_cont;
3923 bool have_then_clause_p, have_else_clause_p;
3924 gcond *cond_stmt;
3925 enum tree_code pred_code;
3926 gimple_seq seq = NULL;
3928 /* If this COND_EXPR has a value, copy the values into a temporary within
3929 the arms. */
3930 if (!VOID_TYPE_P (type))
3932 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3933 tree result;
3935 /* If either an rvalue is ok or we do not require an lvalue, create the
3936 temporary. But we cannot do that if the type is addressable. */
3937 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3938 && !TREE_ADDRESSABLE (type))
3940 if (gimplify_ctxp->allow_rhs_cond_expr
3941 /* If either branch has side effects or could trap, it can't be
3942 evaluated unconditionally. */
3943 && !TREE_SIDE_EFFECTS (then_)
3944 && !generic_expr_could_trap_p (then_)
3945 && !TREE_SIDE_EFFECTS (else_)
3946 && !generic_expr_could_trap_p (else_))
3947 return gimplify_pure_cond_expr (expr_p, pre_p);
3949 tmp = create_tmp_var (type, "iftmp");
3950 result = tmp;
3953 /* Otherwise, only create and copy references to the values. */
3954 else
3956 type = build_pointer_type (type);
3958 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3959 then_ = build_fold_addr_expr_loc (loc, then_);
3961 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3962 else_ = build_fold_addr_expr_loc (loc, else_);
3964 expr
3965 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3967 tmp = create_tmp_var (type, "iftmp");
3968 result = build_simple_mem_ref_loc (loc, tmp);
3971 /* Build the new then clause, `tmp = then_;'. But don't build the
3972 assignment if the value is void; in C++ it can be if it's a throw. */
3973 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3974 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3976 /* Similarly, build the new else clause, `tmp = else_;'. */
3977 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3978 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3980 TREE_TYPE (expr) = void_type_node;
3981 recalculate_side_effects (expr);
3983 /* Move the COND_EXPR to the prequeue. */
3984 gimplify_stmt (&expr, pre_p);
3986 *expr_p = result;
3987 return GS_ALL_DONE;
3990 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3991 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3992 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3993 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3995 /* Make sure the condition has BOOLEAN_TYPE. */
3996 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3998 /* Break apart && and || conditions. */
3999 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
4000 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
4002 expr = shortcut_cond_expr (expr);
4004 if (expr != *expr_p)
4006 *expr_p = expr;
4008 /* We can't rely on gimplify_expr to re-gimplify the expanded
4009 form properly, as cleanups might cause the target labels to be
4010 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
4011 set up a conditional context. */
4012 gimple_push_condition ();
4013 gimplify_stmt (expr_p, &seq);
4014 gimple_pop_condition (pre_p);
4015 gimple_seq_add_seq (pre_p, seq);
4017 return GS_ALL_DONE;
4021 /* Now do the normal gimplification. */
4023 /* Gimplify condition. */
4024 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
4025 fb_rvalue);
4026 if (ret == GS_ERROR)
4027 return GS_ERROR;
4028 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
4030 gimple_push_condition ();
4032 have_then_clause_p = have_else_clause_p = false;
4033 label_true = find_goto_label (TREE_OPERAND (expr, 1));
4034 if (label_true
4035 && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
4036 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4037 have different locations, otherwise we end up with incorrect
4038 location information on the branches. */
4039 && (optimize
4040 || !EXPR_HAS_LOCATION (expr)
4041 || !rexpr_has_location (label_true)
4042 || EXPR_LOCATION (expr) == rexpr_location (label_true)))
4044 have_then_clause_p = true;
4045 label_true = GOTO_DESTINATION (label_true);
4047 else
4048 label_true = create_artificial_label (UNKNOWN_LOCATION);
4049 label_false = find_goto_label (TREE_OPERAND (expr, 2));
4050 if (label_false
4051 && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
4052 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4053 have different locations, otherwise we end up with incorrect
4054 location information on the branches. */
4055 && (optimize
4056 || !EXPR_HAS_LOCATION (expr)
4057 || !rexpr_has_location (label_false)
4058 || EXPR_LOCATION (expr) == rexpr_location (label_false)))
4060 have_else_clause_p = true;
4061 label_false = GOTO_DESTINATION (label_false);
4063 else
4064 label_false = create_artificial_label (UNKNOWN_LOCATION);
4066 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
4067 &arm2);
4068 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
4069 label_false);
4070 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
4071 gimplify_seq_add_stmt (&seq, cond_stmt);
4072 gimple_stmt_iterator gsi = gsi_last (seq);
4073 maybe_fold_stmt (&gsi);
4075 label_cont = NULL_TREE;
4076 if (!have_then_clause_p)
4078 /* For if (...) {} else { code; } put label_true after
4079 the else block. */
4080 if (TREE_OPERAND (expr, 1) == NULL_TREE
4081 && !have_else_clause_p
4082 && TREE_OPERAND (expr, 2) != NULL_TREE)
4083 label_cont = label_true;
4084 else
4086 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
4087 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
4088 /* For if (...) { code; } else {} or
4089 if (...) { code; } else goto label; or
4090 if (...) { code; return; } else { ... }
4091 label_cont isn't needed. */
4092 if (!have_else_clause_p
4093 && TREE_OPERAND (expr, 2) != NULL_TREE
4094 && gimple_seq_may_fallthru (seq))
4096 gimple *g;
4097 label_cont = create_artificial_label (UNKNOWN_LOCATION);
4099 g = gimple_build_goto (label_cont);
4101 /* GIMPLE_COND's are very low level; they have embedded
4102 gotos. This particular embedded goto should not be marked
4103 with the location of the original COND_EXPR, as it would
4104 correspond to the COND_EXPR's condition, not the ELSE or the
4105 THEN arms. To avoid marking it with the wrong location, flag
4106 it as "no location". */
4107 gimple_set_do_not_emit_location (g);
4109 gimplify_seq_add_stmt (&seq, g);
4113 if (!have_else_clause_p)
4115 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
4116 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
4118 if (label_cont)
4119 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
4121 gimple_pop_condition (pre_p);
4122 gimple_seq_add_seq (pre_p, seq);
4124 if (ret == GS_ERROR)
4125 ; /* Do nothing. */
4126 else if (have_then_clause_p || have_else_clause_p)
4127 ret = GS_ALL_DONE;
4128 else
4130 /* Both arms are empty; replace the COND_EXPR with its predicate. */
4131 expr = TREE_OPERAND (expr, 0);
4132 gimplify_stmt (&expr, pre_p);
4135 *expr_p = NULL;
4136 return ret;
4139 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4140 to be marked addressable.
4142 We cannot rely on such an expression being directly markable if a temporary
4143 has been created by the gimplification. In this case, we create another
4144 temporary and initialize it with a copy, which will become a store after we
4145 mark it addressable. This can happen if the front-end passed us something
4146 that it could not mark addressable yet, like a Fortran pass-by-reference
4147 parameter (int) floatvar. */
4149 static void
4150 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4152 while (handled_component_p (*expr_p))
4153 expr_p = &TREE_OPERAND (*expr_p, 0);
4154 if (is_gimple_reg (*expr_p))
4156 /* Do not allow an SSA name as the temporary. */
4157 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4158 DECL_GIMPLE_REG_P (var) = 0;
4159 *expr_p = var;
4163 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4164 a call to __builtin_memcpy. */
4166 static enum gimplify_status
4167 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4168 gimple_seq *seq_p)
4170 tree t, to, to_ptr, from, from_ptr;
4171 gcall *gs;
4172 location_t loc = EXPR_LOCATION (*expr_p);
4174 to = TREE_OPERAND (*expr_p, 0);
4175 from = TREE_OPERAND (*expr_p, 1);
4177 /* Mark the RHS addressable. Beware that it may not be possible to do so
4178 directly if a temporary has been created by the gimplification. */
4179 prepare_gimple_addressable (&from, seq_p);
4181 mark_addressable (from);
4182 from_ptr = build_fold_addr_expr_loc (loc, from);
4183 gimplify_arg (&from_ptr, seq_p, loc);
4185 mark_addressable (to);
4186 to_ptr = build_fold_addr_expr_loc (loc, to);
4187 gimplify_arg (&to_ptr, seq_p, loc);
4189 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4191 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4193 if (want_value)
4195 /* tmp = memcpy() */
4196 t = create_tmp_var (TREE_TYPE (to_ptr));
4197 gimple_call_set_lhs (gs, t);
4198 gimplify_seq_add_stmt (seq_p, gs);
4200 *expr_p = build_simple_mem_ref (t);
4201 return GS_ALL_DONE;
4204 gimplify_seq_add_stmt (seq_p, gs);
4205 *expr_p = NULL;
4206 return GS_ALL_DONE;
4209 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4210 a call to __builtin_memset. In this case we know that the RHS is
4211 a CONSTRUCTOR with an empty element list. */
4213 static enum gimplify_status
4214 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4215 gimple_seq *seq_p)
4217 tree t, from, to, to_ptr;
4218 gcall *gs;
4219 location_t loc = EXPR_LOCATION (*expr_p);
4221 /* Assert our assumptions, to abort instead of producing wrong code
4222 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4223 not be immediately exposed. */
4224 from = TREE_OPERAND (*expr_p, 1);
4225 if (TREE_CODE (from) == WITH_SIZE_EXPR)
4226 from = TREE_OPERAND (from, 0);
4228 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4229 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4231 /* Now proceed. */
4232 to = TREE_OPERAND (*expr_p, 0);
4234 to_ptr = build_fold_addr_expr_loc (loc, to);
4235 gimplify_arg (&to_ptr, seq_p, loc);
4236 t = builtin_decl_implicit (BUILT_IN_MEMSET);
4238 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4240 if (want_value)
4242 /* tmp = memset() */
4243 t = create_tmp_var (TREE_TYPE (to_ptr));
4244 gimple_call_set_lhs (gs, t);
4245 gimplify_seq_add_stmt (seq_p, gs);
4247 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4248 return GS_ALL_DONE;
4251 gimplify_seq_add_stmt (seq_p, gs);
4252 *expr_p = NULL;
4253 return GS_ALL_DONE;
4256 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4257 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4258 assignment. Return non-null if we detect a potential overlap. */
4260 struct gimplify_init_ctor_preeval_data
4262 /* The base decl of the lhs object. May be NULL, in which case we
4263 have to assume the lhs is indirect. */
4264 tree lhs_base_decl;
4266 /* The alias set of the lhs object. */
4267 alias_set_type lhs_alias_set;
4270 static tree
4271 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4273 struct gimplify_init_ctor_preeval_data *data
4274 = (struct gimplify_init_ctor_preeval_data *) xdata;
4275 tree t = *tp;
4277 /* If we find the base object, obviously we have overlap. */
4278 if (data->lhs_base_decl == t)
4279 return t;
4281 /* If the constructor component is indirect, determine if we have a
4282 potential overlap with the lhs. The only bits of information we
4283 have to go on at this point are addressability and alias sets. */
4284 if ((INDIRECT_REF_P (t)
4285 || TREE_CODE (t) == MEM_REF)
4286 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4287 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4288 return t;
4290 /* If the constructor component is a call, determine if it can hide a
4291 potential overlap with the lhs through an INDIRECT_REF like above.
4292 ??? Ugh - this is completely broken. In fact this whole analysis
4293 doesn't look conservative. */
4294 if (TREE_CODE (t) == CALL_EXPR)
4296 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4298 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4299 if (POINTER_TYPE_P (TREE_VALUE (type))
4300 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4301 && alias_sets_conflict_p (data->lhs_alias_set,
4302 get_alias_set
4303 (TREE_TYPE (TREE_VALUE (type)))))
4304 return t;
4307 if (IS_TYPE_OR_DECL_P (t))
4308 *walk_subtrees = 0;
4309 return NULL;
4312 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4313 force values that overlap with the lhs (as described by *DATA)
4314 into temporaries. */
4316 static void
4317 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4318 struct gimplify_init_ctor_preeval_data *data)
4320 enum gimplify_status one;
4322 /* If the value is constant, then there's nothing to pre-evaluate. */
4323 if (TREE_CONSTANT (*expr_p))
4325 /* Ensure it does not have side effects, it might contain a reference to
4326 the object we're initializing. */
4327 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4328 return;
4331 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4332 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4333 return;
4335 /* Recurse for nested constructors. */
4336 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4338 unsigned HOST_WIDE_INT ix;
4339 constructor_elt *ce;
4340 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4342 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4343 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4345 return;
4348 /* If this is a variable sized type, we must remember the size. */
4349 maybe_with_size_expr (expr_p);
4351 /* Gimplify the constructor element to something appropriate for the rhs
4352 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4353 the gimplifier will consider this a store to memory. Doing this
4354 gimplification now means that we won't have to deal with complicated
4355 language-specific trees, nor trees like SAVE_EXPR that can induce
4356 exponential search behavior. */
4357 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4358 if (one == GS_ERROR)
4360 *expr_p = NULL;
4361 return;
4364 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4365 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4366 always be true for all scalars, since is_gimple_mem_rhs insists on a
4367 temporary variable for them. */
4368 if (DECL_P (*expr_p))
4369 return;
4371 /* If this is of variable size, we have no choice but to assume it doesn't
4372 overlap since we can't make a temporary for it. */
4373 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4374 return;
4376 /* Otherwise, we must search for overlap ... */
4377 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4378 return;
4380 /* ... and if found, force the value into a temporary. */
4381 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4384 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4385 a RANGE_EXPR in a CONSTRUCTOR for an array.
4387 var = lower;
4388 loop_entry:
4389 object[var] = value;
4390 if (var == upper)
4391 goto loop_exit;
4392 var = var + 1;
4393 goto loop_entry;
4394 loop_exit:
4396 We increment var _after_ the loop exit check because we might otherwise
4397 fail if upper == TYPE_MAX_VALUE (type for upper).
4399 Note that we never have to deal with SAVE_EXPRs here, because this has
4400 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4402 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4403 gimple_seq *, bool);
4405 static void
4406 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4407 tree value, tree array_elt_type,
4408 gimple_seq *pre_p, bool cleared)
4410 tree loop_entry_label, loop_exit_label, fall_thru_label;
4411 tree var, var_type, cref, tmp;
4413 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4414 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4415 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4417 /* Create and initialize the index variable. */
4418 var_type = TREE_TYPE (upper);
4419 var = create_tmp_var (var_type);
4420 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4422 /* Add the loop entry label. */
4423 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4425 /* Build the reference. */
4426 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4427 var, NULL_TREE, NULL_TREE);
4429 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4430 the store. Otherwise just assign value to the reference. */
4432 if (TREE_CODE (value) == CONSTRUCTOR)
4433 /* NB we might have to call ourself recursively through
4434 gimplify_init_ctor_eval if the value is a constructor. */
4435 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4436 pre_p, cleared);
4437 else
4438 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4440 /* We exit the loop when the index var is equal to the upper bound. */
4441 gimplify_seq_add_stmt (pre_p,
4442 gimple_build_cond (EQ_EXPR, var, upper,
4443 loop_exit_label, fall_thru_label));
4445 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4447 /* Otherwise, increment the index var... */
4448 tmp = build2 (PLUS_EXPR, var_type, var,
4449 fold_convert (var_type, integer_one_node));
4450 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4452 /* ...and jump back to the loop entry. */
4453 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4455 /* Add the loop exit label. */
4456 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4459 /* Return true if FDECL is accessing a field that is zero sized. */
4461 static bool
4462 zero_sized_field_decl (const_tree fdecl)
4464 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4465 && integer_zerop (DECL_SIZE (fdecl)))
4466 return true;
4467 return false;
4470 /* Return true if TYPE is zero sized. */
4472 static bool
4473 zero_sized_type (const_tree type)
4475 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4476 && integer_zerop (TYPE_SIZE (type)))
4477 return true;
4478 return false;
4481 /* A subroutine of gimplify_init_constructor. Generate individual
4482 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4483 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4484 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4485 zeroed first. */
4487 static void
4488 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4489 gimple_seq *pre_p, bool cleared)
4491 tree array_elt_type = NULL;
4492 unsigned HOST_WIDE_INT ix;
4493 tree purpose, value;
4495 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4496 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4498 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4500 tree cref;
4502 /* NULL values are created above for gimplification errors. */
4503 if (value == NULL)
4504 continue;
4506 if (cleared && initializer_zerop (value))
4507 continue;
4509 /* ??? Here's to hoping the front end fills in all of the indices,
4510 so we don't have to figure out what's missing ourselves. */
4511 gcc_assert (purpose);
4513 /* Skip zero-sized fields, unless value has side-effects. This can
4514 happen with calls to functions returning a zero-sized type, which
4515 we shouldn't discard. As a number of downstream passes don't
4516 expect sets of zero-sized fields, we rely on the gimplification of
4517 the MODIFY_EXPR we make below to drop the assignment statement. */
4518 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4519 continue;
4521 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4522 whole range. */
4523 if (TREE_CODE (purpose) == RANGE_EXPR)
4525 tree lower = TREE_OPERAND (purpose, 0);
4526 tree upper = TREE_OPERAND (purpose, 1);
4528 /* If the lower bound is equal to upper, just treat it as if
4529 upper was the index. */
4530 if (simple_cst_equal (lower, upper))
4531 purpose = upper;
4532 else
4534 gimplify_init_ctor_eval_range (object, lower, upper, value,
4535 array_elt_type, pre_p, cleared);
4536 continue;
4540 if (array_elt_type)
4542 /* Do not use bitsizetype for ARRAY_REF indices. */
4543 if (TYPE_DOMAIN (TREE_TYPE (object)))
4544 purpose
4545 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4546 purpose);
4547 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4548 purpose, NULL_TREE, NULL_TREE);
4550 else
4552 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4553 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4554 unshare_expr (object), purpose, NULL_TREE);
4557 if (TREE_CODE (value) == CONSTRUCTOR
4558 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4559 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4560 pre_p, cleared);
4561 else
4563 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4564 gimplify_and_add (init, pre_p);
4565 ggc_free (init);
4570 /* Return the appropriate RHS predicate for this LHS. */
4572 gimple_predicate
4573 rhs_predicate_for (tree lhs)
4575 if (is_gimple_reg (lhs))
4576 return is_gimple_reg_rhs_or_call;
4577 else
4578 return is_gimple_mem_rhs_or_call;
4581 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4582 before the LHS has been gimplified. */
4584 static gimple_predicate
4585 initial_rhs_predicate_for (tree lhs)
4587 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4588 return is_gimple_reg_rhs_or_call;
4589 else
4590 return is_gimple_mem_rhs_or_call;
4593 /* Gimplify a C99 compound literal expression. This just means adding
4594 the DECL_EXPR before the current statement and using its anonymous
4595 decl instead. */
4597 static enum gimplify_status
4598 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4599 bool (*gimple_test_f) (tree),
4600 fallback_t fallback)
4602 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4603 tree decl = DECL_EXPR_DECL (decl_s);
4604 tree init = DECL_INITIAL (decl);
4605 /* Mark the decl as addressable if the compound literal
4606 expression is addressable now, otherwise it is marked too late
4607 after we gimplify the initialization expression. */
4608 if (TREE_ADDRESSABLE (*expr_p))
4609 TREE_ADDRESSABLE (decl) = 1;
4610 /* Otherwise, if we don't need an lvalue and have a literal directly
4611 substitute it. Check if it matches the gimple predicate, as
4612 otherwise we'd generate a new temporary, and we can as well just
4613 use the decl we already have. */
4614 else if (!TREE_ADDRESSABLE (decl)
4615 && init
4616 && (fallback & fb_lvalue) == 0
4617 && gimple_test_f (init))
4619 *expr_p = init;
4620 return GS_OK;
4623 /* Preliminarily mark non-addressed complex variables as eligible
4624 for promotion to gimple registers. We'll transform their uses
4625 as we find them. */
4626 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4627 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4628 && !TREE_THIS_VOLATILE (decl)
4629 && !needs_to_live_in_memory (decl))
4630 DECL_GIMPLE_REG_P (decl) = 1;
4632 /* If the decl is not addressable, then it is being used in some
4633 expression or on the right hand side of a statement, and it can
4634 be put into a readonly data section. */
4635 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4636 TREE_READONLY (decl) = 1;
4638 /* This decl isn't mentioned in the enclosing block, so add it to the
4639 list of temps. FIXME it seems a bit of a kludge to say that
4640 anonymous artificial vars aren't pushed, but everything else is. */
4641 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4642 gimple_add_tmp_var (decl);
4644 gimplify_and_add (decl_s, pre_p);
4645 *expr_p = decl;
4646 return GS_OK;
4649 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4650 return a new CONSTRUCTOR if something changed. */
4652 static tree
4653 optimize_compound_literals_in_ctor (tree orig_ctor)
4655 tree ctor = orig_ctor;
4656 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4657 unsigned int idx, num = vec_safe_length (elts);
4659 for (idx = 0; idx < num; idx++)
4661 tree value = (*elts)[idx].value;
4662 tree newval = value;
4663 if (TREE_CODE (value) == CONSTRUCTOR)
4664 newval = optimize_compound_literals_in_ctor (value);
4665 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4667 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4668 tree decl = DECL_EXPR_DECL (decl_s);
4669 tree init = DECL_INITIAL (decl);
4671 if (!TREE_ADDRESSABLE (value)
4672 && !TREE_ADDRESSABLE (decl)
4673 && init
4674 && TREE_CODE (init) == CONSTRUCTOR)
4675 newval = optimize_compound_literals_in_ctor (init);
4677 if (newval == value)
4678 continue;
4680 if (ctor == orig_ctor)
4682 ctor = copy_node (orig_ctor);
4683 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4684 elts = CONSTRUCTOR_ELTS (ctor);
4686 (*elts)[idx].value = newval;
4688 return ctor;
4691 /* A subroutine of gimplify_modify_expr. Break out elements of a
4692 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4694 Note that we still need to clear any elements that don't have explicit
4695 initializers, so if not all elements are initialized we keep the
4696 original MODIFY_EXPR, we just remove all of the constructor elements.
4698 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4699 GS_ERROR if we would have to create a temporary when gimplifying
4700 this constructor. Otherwise, return GS_OK.
4702 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4704 static enum gimplify_status
4705 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4706 bool want_value, bool notify_temp_creation)
4708 tree object, ctor, type;
4709 enum gimplify_status ret;
4710 vec<constructor_elt, va_gc> *elts;
4712 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4714 if (!notify_temp_creation)
4716 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4717 is_gimple_lvalue, fb_lvalue);
4718 if (ret == GS_ERROR)
4719 return ret;
4722 object = TREE_OPERAND (*expr_p, 0);
4723 ctor = TREE_OPERAND (*expr_p, 1)
4724 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4725 type = TREE_TYPE (ctor);
4726 elts = CONSTRUCTOR_ELTS (ctor);
4727 ret = GS_ALL_DONE;
4729 switch (TREE_CODE (type))
4731 case RECORD_TYPE:
4732 case UNION_TYPE:
4733 case QUAL_UNION_TYPE:
4734 case ARRAY_TYPE:
4736 struct gimplify_init_ctor_preeval_data preeval_data;
4737 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4738 bool cleared, complete_p, valid_const_initializer;
4740 /* Aggregate types must lower constructors to initialization of
4741 individual elements. The exception is that a CONSTRUCTOR node
4742 with no elements indicates zero-initialization of the whole. */
4743 if (vec_safe_is_empty (elts))
4745 if (notify_temp_creation)
4746 return GS_OK;
4747 break;
4750 /* Fetch information about the constructor to direct later processing.
4751 We might want to make static versions of it in various cases, and
4752 can only do so if it known to be a valid constant initializer. */
4753 valid_const_initializer
4754 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4755 &num_ctor_elements, &complete_p);
4757 /* If a const aggregate variable is being initialized, then it
4758 should never be a lose to promote the variable to be static. */
4759 if (valid_const_initializer
4760 && num_nonzero_elements > 1
4761 && TREE_READONLY (object)
4762 && VAR_P (object)
4763 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4765 if (notify_temp_creation)
4766 return GS_ERROR;
4767 DECL_INITIAL (object) = ctor;
4768 TREE_STATIC (object) = 1;
4769 if (!DECL_NAME (object))
4770 DECL_NAME (object) = create_tmp_var_name ("C");
4771 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4773 /* ??? C++ doesn't automatically append a .<number> to the
4774 assembler name, and even when it does, it looks at FE private
4775 data structures to figure out what that number should be,
4776 which are not set for this variable. I suppose this is
4777 important for local statics for inline functions, which aren't
4778 "local" in the object file sense. So in order to get a unique
4779 TU-local symbol, we must invoke the lhd version now. */
4780 lhd_set_decl_assembler_name (object);
4782 *expr_p = NULL_TREE;
4783 break;
4786 /* If there are "lots" of initialized elements, even discounting
4787 those that are not address constants (and thus *must* be
4788 computed at runtime), then partition the constructor into
4789 constant and non-constant parts. Block copy the constant
4790 parts in, then generate code for the non-constant parts. */
4791 /* TODO. There's code in cp/typeck.c to do this. */
4793 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4794 /* store_constructor will ignore the clearing of variable-sized
4795 objects. Initializers for such objects must explicitly set
4796 every field that needs to be set. */
4797 cleared = false;
4798 else if (!complete_p)
4799 /* If the constructor isn't complete, clear the whole object
4800 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4802 ??? This ought not to be needed. For any element not present
4803 in the initializer, we should simply set them to zero. Except
4804 we'd need to *find* the elements that are not present, and that
4805 requires trickery to avoid quadratic compile-time behavior in
4806 large cases or excessive memory use in small cases. */
4807 cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
4808 else if (num_ctor_elements - num_nonzero_elements
4809 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4810 && num_nonzero_elements < num_ctor_elements / 4)
4811 /* If there are "lots" of zeros, it's more efficient to clear
4812 the memory and then set the nonzero elements. */
4813 cleared = true;
4814 else
4815 cleared = false;
4817 /* If there are "lots" of initialized elements, and all of them
4818 are valid address constants, then the entire initializer can
4819 be dropped to memory, and then memcpy'd out. Don't do this
4820 for sparse arrays, though, as it's more efficient to follow
4821 the standard CONSTRUCTOR behavior of memset followed by
4822 individual element initialization. Also don't do this for small
4823 all-zero initializers (which aren't big enough to merit
4824 clearing), and don't try to make bitwise copies of
4825 TREE_ADDRESSABLE types. */
4827 if (valid_const_initializer
4828 && !(cleared || num_nonzero_elements == 0)
4829 && !TREE_ADDRESSABLE (type))
4831 HOST_WIDE_INT size = int_size_in_bytes (type);
4832 unsigned int align;
4834 /* ??? We can still get unbounded array types, at least
4835 from the C++ front end. This seems wrong, but attempt
4836 to work around it for now. */
4837 if (size < 0)
4839 size = int_size_in_bytes (TREE_TYPE (object));
4840 if (size >= 0)
4841 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4844 /* Find the maximum alignment we can assume for the object. */
4845 /* ??? Make use of DECL_OFFSET_ALIGN. */
4846 if (DECL_P (object))
4847 align = DECL_ALIGN (object);
4848 else
4849 align = TYPE_ALIGN (type);
4851 /* Do a block move either if the size is so small as to make
4852 each individual move a sub-unit move on average, or if it
4853 is so large as to make individual moves inefficient. */
4854 if (size > 0
4855 && num_nonzero_elements > 1
4856 && (size < num_nonzero_elements
4857 || !can_move_by_pieces (size, align)))
4859 if (notify_temp_creation)
4860 return GS_ERROR;
4862 walk_tree (&ctor, force_labels_r, NULL, NULL);
4863 ctor = tree_output_constant_def (ctor);
4864 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4865 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4866 TREE_OPERAND (*expr_p, 1) = ctor;
4868 /* This is no longer an assignment of a CONSTRUCTOR, but
4869 we still may have processing to do on the LHS. So
4870 pretend we didn't do anything here to let that happen. */
4871 return GS_UNHANDLED;
4875 /* If the target is volatile, we have non-zero elements and more than
4876 one field to assign, initialize the target from a temporary. */
4877 if (TREE_THIS_VOLATILE (object)
4878 && !TREE_ADDRESSABLE (type)
4879 && num_nonzero_elements > 0
4880 && vec_safe_length (elts) > 1)
4882 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4883 TREE_OPERAND (*expr_p, 0) = temp;
4884 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4885 *expr_p,
4886 build2 (MODIFY_EXPR, void_type_node,
4887 object, temp));
4888 return GS_OK;
4891 if (notify_temp_creation)
4892 return GS_OK;
4894 /* If there are nonzero elements and if needed, pre-evaluate to capture
4895 elements overlapping with the lhs into temporaries. We must do this
4896 before clearing to fetch the values before they are zeroed-out. */
4897 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4899 preeval_data.lhs_base_decl = get_base_address (object);
4900 if (!DECL_P (preeval_data.lhs_base_decl))
4901 preeval_data.lhs_base_decl = NULL;
4902 preeval_data.lhs_alias_set = get_alias_set (object);
4904 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4905 pre_p, post_p, &preeval_data);
4908 bool ctor_has_side_effects_p
4909 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4911 if (cleared)
4913 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4914 Note that we still have to gimplify, in order to handle the
4915 case of variable sized types. Avoid shared tree structures. */
4916 CONSTRUCTOR_ELTS (ctor) = NULL;
4917 TREE_SIDE_EFFECTS (ctor) = 0;
4918 object = unshare_expr (object);
4919 gimplify_stmt (expr_p, pre_p);
4922 /* If we have not block cleared the object, or if there are nonzero
4923 elements in the constructor, or if the constructor has side effects,
4924 add assignments to the individual scalar fields of the object. */
4925 if (!cleared
4926 || num_nonzero_elements > 0
4927 || ctor_has_side_effects_p)
4928 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4930 *expr_p = NULL_TREE;
4932 break;
4934 case COMPLEX_TYPE:
4936 tree r, i;
4938 if (notify_temp_creation)
4939 return GS_OK;
4941 /* Extract the real and imaginary parts out of the ctor. */
4942 gcc_assert (elts->length () == 2);
4943 r = (*elts)[0].value;
4944 i = (*elts)[1].value;
4945 if (r == NULL || i == NULL)
4947 tree zero = build_zero_cst (TREE_TYPE (type));
4948 if (r == NULL)
4949 r = zero;
4950 if (i == NULL)
4951 i = zero;
4954 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4955 represent creation of a complex value. */
4956 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4958 ctor = build_complex (type, r, i);
4959 TREE_OPERAND (*expr_p, 1) = ctor;
4961 else
4963 ctor = build2 (COMPLEX_EXPR, type, r, i);
4964 TREE_OPERAND (*expr_p, 1) = ctor;
4965 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4966 pre_p,
4967 post_p,
4968 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4969 fb_rvalue);
4972 break;
4974 case VECTOR_TYPE:
4976 unsigned HOST_WIDE_INT ix;
4977 constructor_elt *ce;
4979 if (notify_temp_creation)
4980 return GS_OK;
4982 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4983 if (TREE_CONSTANT (ctor))
4985 bool constant_p = true;
4986 tree value;
4988 /* Even when ctor is constant, it might contain non-*_CST
4989 elements, such as addresses or trapping values like
4990 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4991 in VECTOR_CST nodes. */
4992 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4993 if (!CONSTANT_CLASS_P (value))
4995 constant_p = false;
4996 break;
4999 if (constant_p)
5001 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
5002 break;
5005 TREE_CONSTANT (ctor) = 0;
5008 /* Vector types use CONSTRUCTOR all the way through gimple
5009 compilation as a general initializer. */
5010 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5012 enum gimplify_status tret;
5013 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5014 fb_rvalue);
5015 if (tret == GS_ERROR)
5016 ret = GS_ERROR;
5017 else if (TREE_STATIC (ctor)
5018 && !initializer_constant_valid_p (ce->value,
5019 TREE_TYPE (ce->value)))
5020 TREE_STATIC (ctor) = 0;
5022 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5023 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5025 break;
5027 default:
5028 /* So how did we get a CONSTRUCTOR for a scalar type? */
5029 gcc_unreachable ();
5032 if (ret == GS_ERROR)
5033 return GS_ERROR;
5034 /* If we have gimplified both sides of the initializer but have
5035 not emitted an assignment, do so now. */
5036 if (*expr_p)
5038 tree lhs = TREE_OPERAND (*expr_p, 0);
5039 tree rhs = TREE_OPERAND (*expr_p, 1);
5040 if (want_value && object == lhs)
5041 lhs = unshare_expr (lhs);
5042 gassign *init = gimple_build_assign (lhs, rhs);
5043 gimplify_seq_add_stmt (pre_p, init);
5045 if (want_value)
5047 *expr_p = object;
5048 return GS_OK;
5050 else
5052 *expr_p = NULL;
5053 return GS_ALL_DONE;
5057 /* Given a pointer value OP0, return a simplified version of an
5058 indirection through OP0, or NULL_TREE if no simplification is
5059 possible. This may only be applied to a rhs of an expression.
5060 Note that the resulting type may be different from the type pointed
5061 to in the sense that it is still compatible from the langhooks
5062 point of view. */
5064 static tree
5065 gimple_fold_indirect_ref_rhs (tree t)
5067 return gimple_fold_indirect_ref (t);
5070 /* Subroutine of gimplify_modify_expr to do simplifications of
5071 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5072 something changes. */
5074 static enum gimplify_status
5075 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5076 gimple_seq *pre_p, gimple_seq *post_p,
5077 bool want_value)
5079 enum gimplify_status ret = GS_UNHANDLED;
5080 bool changed;
5084 changed = false;
5085 switch (TREE_CODE (*from_p))
5087 case VAR_DECL:
5088 /* If we're assigning from a read-only variable initialized with
5089 a constructor, do the direct assignment from the constructor,
5090 but only if neither source nor target are volatile since this
5091 latter assignment might end up being done on a per-field basis. */
5092 if (DECL_INITIAL (*from_p)
5093 && TREE_READONLY (*from_p)
5094 && !TREE_THIS_VOLATILE (*from_p)
5095 && !TREE_THIS_VOLATILE (*to_p)
5096 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5098 tree old_from = *from_p;
5099 enum gimplify_status subret;
5101 /* Move the constructor into the RHS. */
5102 *from_p = unshare_expr (DECL_INITIAL (*from_p));
5104 /* Let's see if gimplify_init_constructor will need to put
5105 it in memory. */
5106 subret = gimplify_init_constructor (expr_p, NULL, NULL,
5107 false, true);
5108 if (subret == GS_ERROR)
5110 /* If so, revert the change. */
5111 *from_p = old_from;
5113 else
5115 ret = GS_OK;
5116 changed = true;
5119 break;
5120 case INDIRECT_REF:
5122 /* If we have code like
5124 *(const A*)(A*)&x
5126 where the type of "x" is a (possibly cv-qualified variant
5127 of "A"), treat the entire expression as identical to "x".
5128 This kind of code arises in C++ when an object is bound
5129 to a const reference, and if "x" is a TARGET_EXPR we want
5130 to take advantage of the optimization below. */
5131 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5132 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5133 if (t)
5135 if (TREE_THIS_VOLATILE (t) != volatile_p)
5137 if (DECL_P (t))
5138 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5139 build_fold_addr_expr (t));
5140 if (REFERENCE_CLASS_P (t))
5141 TREE_THIS_VOLATILE (t) = volatile_p;
5143 *from_p = t;
5144 ret = GS_OK;
5145 changed = true;
5147 break;
5150 case TARGET_EXPR:
5152 /* If we are initializing something from a TARGET_EXPR, strip the
5153 TARGET_EXPR and initialize it directly, if possible. This can't
5154 be done if the initializer is void, since that implies that the
5155 temporary is set in some non-trivial way.
5157 ??? What about code that pulls out the temp and uses it
5158 elsewhere? I think that such code never uses the TARGET_EXPR as
5159 an initializer. If I'm wrong, we'll die because the temp won't
5160 have any RTL. In that case, I guess we'll need to replace
5161 references somehow. */
5162 tree init = TARGET_EXPR_INITIAL (*from_p);
5164 if (init
5165 && (TREE_CODE (*expr_p) != MODIFY_EXPR
5166 || !TARGET_EXPR_NO_ELIDE (*from_p))
5167 && !VOID_TYPE_P (TREE_TYPE (init)))
5169 *from_p = init;
5170 ret = GS_OK;
5171 changed = true;
5174 break;
5176 case COMPOUND_EXPR:
5177 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5178 caught. */
5179 gimplify_compound_expr (from_p, pre_p, true);
5180 ret = GS_OK;
5181 changed = true;
5182 break;
5184 case CONSTRUCTOR:
5185 /* If we already made some changes, let the front end have a
5186 crack at this before we break it down. */
5187 if (ret != GS_UNHANDLED)
5188 break;
5189 /* If we're initializing from a CONSTRUCTOR, break this into
5190 individual MODIFY_EXPRs. */
5191 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5192 false);
5194 case COND_EXPR:
5195 /* If we're assigning to a non-register type, push the assignment
5196 down into the branches. This is mandatory for ADDRESSABLE types,
5197 since we cannot generate temporaries for such, but it saves a
5198 copy in other cases as well. */
5199 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5201 /* This code should mirror the code in gimplify_cond_expr. */
5202 enum tree_code code = TREE_CODE (*expr_p);
5203 tree cond = *from_p;
5204 tree result = *to_p;
5206 ret = gimplify_expr (&result, pre_p, post_p,
5207 is_gimple_lvalue, fb_lvalue);
5208 if (ret != GS_ERROR)
5209 ret = GS_OK;
5211 /* If we are going to write RESULT more than once, clear
5212 TREE_READONLY flag, otherwise we might incorrectly promote
5213 the variable to static const and initialize it at compile
5214 time in one of the branches. */
5215 if (VAR_P (result)
5216 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5217 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5218 TREE_READONLY (result) = 0;
5219 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5220 TREE_OPERAND (cond, 1)
5221 = build2 (code, void_type_node, result,
5222 TREE_OPERAND (cond, 1));
5223 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5224 TREE_OPERAND (cond, 2)
5225 = build2 (code, void_type_node, unshare_expr (result),
5226 TREE_OPERAND (cond, 2));
5228 TREE_TYPE (cond) = void_type_node;
5229 recalculate_side_effects (cond);
5231 if (want_value)
5233 gimplify_and_add (cond, pre_p);
5234 *expr_p = unshare_expr (result);
5236 else
5237 *expr_p = cond;
5238 return ret;
5240 break;
5242 case CALL_EXPR:
5243 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5244 return slot so that we don't generate a temporary. */
5245 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5246 && aggregate_value_p (*from_p, *from_p))
5248 bool use_target;
5250 if (!(rhs_predicate_for (*to_p))(*from_p))
5251 /* If we need a temporary, *to_p isn't accurate. */
5252 use_target = false;
5253 /* It's OK to use the return slot directly unless it's an NRV. */
5254 else if (TREE_CODE (*to_p) == RESULT_DECL
5255 && DECL_NAME (*to_p) == NULL_TREE
5256 && needs_to_live_in_memory (*to_p))
5257 use_target = true;
5258 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5259 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5260 /* Don't force regs into memory. */
5261 use_target = false;
5262 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5263 /* It's OK to use the target directly if it's being
5264 initialized. */
5265 use_target = true;
5266 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5267 != INTEGER_CST)
5268 /* Always use the target and thus RSO for variable-sized types.
5269 GIMPLE cannot deal with a variable-sized assignment
5270 embedded in a call statement. */
5271 use_target = true;
5272 else if (TREE_CODE (*to_p) != SSA_NAME
5273 && (!is_gimple_variable (*to_p)
5274 || needs_to_live_in_memory (*to_p)))
5275 /* Don't use the original target if it's already addressable;
5276 if its address escapes, and the called function uses the
5277 NRV optimization, a conforming program could see *to_p
5278 change before the called function returns; see c++/19317.
5279 When optimizing, the return_slot pass marks more functions
5280 as safe after we have escape info. */
5281 use_target = false;
5282 else
5283 use_target = true;
5285 if (use_target)
5287 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5288 mark_addressable (*to_p);
5291 break;
5293 case WITH_SIZE_EXPR:
5294 /* Likewise for calls that return an aggregate of non-constant size,
5295 since we would not be able to generate a temporary at all. */
5296 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5298 *from_p = TREE_OPERAND (*from_p, 0);
5299 /* We don't change ret in this case because the
5300 WITH_SIZE_EXPR might have been added in
5301 gimplify_modify_expr, so returning GS_OK would lead to an
5302 infinite loop. */
5303 changed = true;
5305 break;
5307 /* If we're initializing from a container, push the initialization
5308 inside it. */
5309 case CLEANUP_POINT_EXPR:
5310 case BIND_EXPR:
5311 case STATEMENT_LIST:
5313 tree wrap = *from_p;
5314 tree t;
5316 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5317 fb_lvalue);
5318 if (ret != GS_ERROR)
5319 ret = GS_OK;
5321 t = voidify_wrapper_expr (wrap, *expr_p);
5322 gcc_assert (t == *expr_p);
5324 if (want_value)
5326 gimplify_and_add (wrap, pre_p);
5327 *expr_p = unshare_expr (*to_p);
5329 else
5330 *expr_p = wrap;
5331 return GS_OK;
5334 case COMPOUND_LITERAL_EXPR:
5336 tree complit = TREE_OPERAND (*expr_p, 1);
5337 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5338 tree decl = DECL_EXPR_DECL (decl_s);
5339 tree init = DECL_INITIAL (decl);
5341 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5342 into struct T x = { 0, 1, 2 } if the address of the
5343 compound literal has never been taken. */
5344 if (!TREE_ADDRESSABLE (complit)
5345 && !TREE_ADDRESSABLE (decl)
5346 && init)
5348 *expr_p = copy_node (*expr_p);
5349 TREE_OPERAND (*expr_p, 1) = init;
5350 return GS_OK;
5354 default:
5355 break;
5358 while (changed);
5360 return ret;
5364 /* Return true if T looks like a valid GIMPLE statement. */
5366 static bool
5367 is_gimple_stmt (tree t)
5369 const enum tree_code code = TREE_CODE (t);
5371 switch (code)
5373 case NOP_EXPR:
5374 /* The only valid NOP_EXPR is the empty statement. */
5375 return IS_EMPTY_STMT (t);
5377 case BIND_EXPR:
5378 case COND_EXPR:
5379 /* These are only valid if they're void. */
5380 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5382 case SWITCH_EXPR:
5383 case GOTO_EXPR:
5384 case RETURN_EXPR:
5385 case LABEL_EXPR:
5386 case CASE_LABEL_EXPR:
5387 case TRY_CATCH_EXPR:
5388 case TRY_FINALLY_EXPR:
5389 case EH_FILTER_EXPR:
5390 case CATCH_EXPR:
5391 case ASM_EXPR:
5392 case STATEMENT_LIST:
5393 case OACC_PARALLEL:
5394 case OACC_KERNELS:
5395 case OACC_DATA:
5396 case OACC_HOST_DATA:
5397 case OACC_DECLARE:
5398 case OACC_UPDATE:
5399 case OACC_ENTER_DATA:
5400 case OACC_EXIT_DATA:
5401 case OACC_CACHE:
5402 case OMP_PARALLEL:
5403 case OMP_FOR:
5404 case OMP_SIMD:
5405 case OMP_DISTRIBUTE:
5406 case OACC_LOOP:
5407 case OMP_SECTIONS:
5408 case OMP_SECTION:
5409 case OMP_SINGLE:
5410 case OMP_MASTER:
5411 case OMP_TASKGROUP:
5412 case OMP_ORDERED:
5413 case OMP_CRITICAL:
5414 case OMP_TASK:
5415 case OMP_TARGET:
5416 case OMP_TARGET_DATA:
5417 case OMP_TARGET_UPDATE:
5418 case OMP_TARGET_ENTER_DATA:
5419 case OMP_TARGET_EXIT_DATA:
5420 case OMP_TASKLOOP:
5421 case OMP_TEAMS:
5422 /* These are always void. */
5423 return true;
5425 case CALL_EXPR:
5426 case MODIFY_EXPR:
5427 case PREDICT_EXPR:
5428 /* These are valid regardless of their type. */
5429 return true;
5431 default:
5432 return false;
5437 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5438 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5439 DECL_GIMPLE_REG_P set.
5441 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5442 other, unmodified part of the complex object just before the total store.
5443 As a consequence, if the object is still uninitialized, an undefined value
5444 will be loaded into a register, which may result in a spurious exception
5445 if the register is floating-point and the value happens to be a signaling
5446 NaN for example. Then the fully-fledged complex operations lowering pass
5447 followed by a DCE pass are necessary in order to fix things up. */
5449 static enum gimplify_status
5450 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5451 bool want_value)
5453 enum tree_code code, ocode;
5454 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5456 lhs = TREE_OPERAND (*expr_p, 0);
5457 rhs = TREE_OPERAND (*expr_p, 1);
5458 code = TREE_CODE (lhs);
5459 lhs = TREE_OPERAND (lhs, 0);
5461 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5462 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5463 TREE_NO_WARNING (other) = 1;
5464 other = get_formal_tmp_var (other, pre_p);
5466 realpart = code == REALPART_EXPR ? rhs : other;
5467 imagpart = code == REALPART_EXPR ? other : rhs;
5469 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5470 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5471 else
5472 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5474 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5475 *expr_p = (want_value) ? rhs : NULL_TREE;
5477 return GS_ALL_DONE;
5480 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5482 modify_expr
5483 : varname '=' rhs
5484 | '*' ID '=' rhs
5486 PRE_P points to the list where side effects that must happen before
5487 *EXPR_P should be stored.
5489 POST_P points to the list where side effects that must happen after
5490 *EXPR_P should be stored.
5492 WANT_VALUE is nonzero iff we want to use the value of this expression
5493 in another expression. */
5495 static enum gimplify_status
5496 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5497 bool want_value)
5499 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5500 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5501 enum gimplify_status ret = GS_UNHANDLED;
5502 gimple *assign;
5503 location_t loc = EXPR_LOCATION (*expr_p);
5504 gimple_stmt_iterator gsi;
5506 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5507 || TREE_CODE (*expr_p) == INIT_EXPR);
5509 /* Trying to simplify a clobber using normal logic doesn't work,
5510 so handle it here. */
5511 if (TREE_CLOBBER_P (*from_p))
5513 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5514 if (ret == GS_ERROR)
5515 return ret;
5516 gcc_assert (!want_value);
5517 if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
5519 tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
5520 pre_p, post_p);
5521 *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
5523 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5524 *expr_p = NULL;
5525 return GS_ALL_DONE;
5528 /* Insert pointer conversions required by the middle-end that are not
5529 required by the frontend. This fixes middle-end type checking for
5530 for example gcc.dg/redecl-6.c. */
5531 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5533 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5534 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5535 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5538 /* See if any simplifications can be done based on what the RHS is. */
5539 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5540 want_value);
5541 if (ret != GS_UNHANDLED)
5542 return ret;
5544 /* For zero sized types only gimplify the left hand side and right hand
5545 side as statements and throw away the assignment. Do this after
5546 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5547 types properly. */
5548 if (zero_sized_type (TREE_TYPE (*from_p))
5549 && !want_value
5550 /* Don't do this for calls that return addressable types, expand_call
5551 relies on those having a lhs. */
5552 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5553 && TREE_CODE (*from_p) == CALL_EXPR))
5555 gimplify_stmt (from_p, pre_p);
5556 gimplify_stmt (to_p, pre_p);
5557 *expr_p = NULL_TREE;
5558 return GS_ALL_DONE;
5561 /* If the value being copied is of variable width, compute the length
5562 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5563 before gimplifying any of the operands so that we can resolve any
5564 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5565 the size of the expression to be copied, not of the destination, so
5566 that is what we must do here. */
5567 maybe_with_size_expr (from_p);
5569 /* As a special case, we have to temporarily allow for assignments
5570 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5571 a toplevel statement, when gimplifying the GENERIC expression
5572 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5573 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5575 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5576 prevent gimplify_expr from trying to create a new temporary for
5577 foo's LHS, we tell it that it should only gimplify until it
5578 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5579 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5580 and all we need to do here is set 'a' to be its LHS. */
5582 /* Gimplify the RHS first for C++17 and bug 71104. */
5583 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5584 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5585 if (ret == GS_ERROR)
5586 return ret;
5588 /* Then gimplify the LHS. */
5589 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5590 twice we have to make sure to gimplify into non-SSA as otherwise
5591 the abnormal edge added later will make those defs not dominate
5592 their uses.
5593 ??? Technically this applies only to the registers used in the
5594 resulting non-register *TO_P. */
5595 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5596 if (saved_into_ssa
5597 && TREE_CODE (*from_p) == CALL_EXPR
5598 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5599 gimplify_ctxp->into_ssa = false;
5600 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5601 gimplify_ctxp->into_ssa = saved_into_ssa;
5602 if (ret == GS_ERROR)
5603 return ret;
5605 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5606 guess for the predicate was wrong. */
5607 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5608 if (final_pred != initial_pred)
5610 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5611 if (ret == GS_ERROR)
5612 return ret;
5615 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5616 size as argument to the call. */
5617 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5619 tree call = TREE_OPERAND (*from_p, 0);
5620 tree vlasize = TREE_OPERAND (*from_p, 1);
5622 if (TREE_CODE (call) == CALL_EXPR
5623 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5625 int nargs = call_expr_nargs (call);
5626 tree type = TREE_TYPE (call);
5627 tree ap = CALL_EXPR_ARG (call, 0);
5628 tree tag = CALL_EXPR_ARG (call, 1);
5629 tree aptag = CALL_EXPR_ARG (call, 2);
5630 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5631 IFN_VA_ARG, type,
5632 nargs + 1, ap, tag,
5633 aptag, vlasize);
5634 TREE_OPERAND (*from_p, 0) = newcall;
5638 /* Now see if the above changed *from_p to something we handle specially. */
5639 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5640 want_value);
5641 if (ret != GS_UNHANDLED)
5642 return ret;
5644 /* If we've got a variable sized assignment between two lvalues (i.e. does
5645 not involve a call), then we can make things a bit more straightforward
5646 by converting the assignment to memcpy or memset. */
5647 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5649 tree from = TREE_OPERAND (*from_p, 0);
5650 tree size = TREE_OPERAND (*from_p, 1);
5652 if (TREE_CODE (from) == CONSTRUCTOR)
5653 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5655 if (is_gimple_addressable (from))
5657 *from_p = from;
5658 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5659 pre_p);
5663 /* Transform partial stores to non-addressable complex variables into
5664 total stores. This allows us to use real instead of virtual operands
5665 for these variables, which improves optimization. */
5666 if ((TREE_CODE (*to_p) == REALPART_EXPR
5667 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5668 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5669 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5671 /* Try to alleviate the effects of the gimplification creating artificial
5672 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5673 make sure not to create DECL_DEBUG_EXPR links across functions. */
5674 if (!gimplify_ctxp->into_ssa
5675 && VAR_P (*from_p)
5676 && DECL_IGNORED_P (*from_p)
5677 && DECL_P (*to_p)
5678 && !DECL_IGNORED_P (*to_p)
5679 && decl_function_context (*to_p) == current_function_decl
5680 && decl_function_context (*from_p) == current_function_decl)
5682 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5683 DECL_NAME (*from_p)
5684 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5685 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5686 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5689 if (want_value && TREE_THIS_VOLATILE (*to_p))
5690 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5692 if (TREE_CODE (*from_p) == CALL_EXPR)
5694 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5695 instead of a GIMPLE_ASSIGN. */
5696 gcall *call_stmt;
5697 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5699 /* Gimplify internal functions created in the FEs. */
5700 int nargs = call_expr_nargs (*from_p), i;
5701 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5702 auto_vec<tree> vargs (nargs);
5704 for (i = 0; i < nargs; i++)
5706 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5707 EXPR_LOCATION (*from_p));
5708 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5710 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5711 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5712 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5714 else
5716 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5717 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5718 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5719 tree fndecl = get_callee_fndecl (*from_p);
5720 if (fndecl
5721 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5722 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
5723 && call_expr_nargs (*from_p) == 3)
5724 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5725 CALL_EXPR_ARG (*from_p, 0),
5726 CALL_EXPR_ARG (*from_p, 1),
5727 CALL_EXPR_ARG (*from_p, 2));
5728 else
5730 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5733 notice_special_calls (call_stmt);
5734 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5735 gimple_call_set_lhs (call_stmt, *to_p);
5736 else if (TREE_CODE (*to_p) == SSA_NAME)
5737 /* The above is somewhat premature, avoid ICEing later for a
5738 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5739 ??? This doesn't make it a default-def. */
5740 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5742 assign = call_stmt;
5744 else
5746 assign = gimple_build_assign (*to_p, *from_p);
5747 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5748 if (COMPARISON_CLASS_P (*from_p))
5749 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5752 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5754 /* We should have got an SSA name from the start. */
5755 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5756 || ! gimple_in_ssa_p (cfun));
5759 gimplify_seq_add_stmt (pre_p, assign);
5760 gsi = gsi_last (*pre_p);
5761 maybe_fold_stmt (&gsi);
5763 if (want_value)
5765 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5766 return GS_OK;
5768 else
5769 *expr_p = NULL;
5771 return GS_ALL_DONE;
5774 /* Gimplify a comparison between two variable-sized objects. Do this
5775 with a call to BUILT_IN_MEMCMP. */
5777 static enum gimplify_status
5778 gimplify_variable_sized_compare (tree *expr_p)
5780 location_t loc = EXPR_LOCATION (*expr_p);
5781 tree op0 = TREE_OPERAND (*expr_p, 0);
5782 tree op1 = TREE_OPERAND (*expr_p, 1);
5783 tree t, arg, dest, src, expr;
5785 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5786 arg = unshare_expr (arg);
5787 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5788 src = build_fold_addr_expr_loc (loc, op1);
5789 dest = build_fold_addr_expr_loc (loc, op0);
5790 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5791 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5793 expr
5794 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5795 SET_EXPR_LOCATION (expr, loc);
5796 *expr_p = expr;
5798 return GS_OK;
5801 /* Gimplify a comparison between two aggregate objects of integral scalar
5802 mode as a comparison between the bitwise equivalent scalar values. */
5804 static enum gimplify_status
5805 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5807 location_t loc = EXPR_LOCATION (*expr_p);
5808 tree op0 = TREE_OPERAND (*expr_p, 0);
5809 tree op1 = TREE_OPERAND (*expr_p, 1);
5811 tree type = TREE_TYPE (op0);
5812 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5814 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5815 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5817 *expr_p
5818 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5820 return GS_OK;
5823 /* Gimplify an expression sequence. This function gimplifies each
5824 expression and rewrites the original expression with the last
5825 expression of the sequence in GIMPLE form.
5827 PRE_P points to the list where the side effects for all the
5828 expressions in the sequence will be emitted.
5830 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5832 static enum gimplify_status
5833 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5835 tree t = *expr_p;
5839 tree *sub_p = &TREE_OPERAND (t, 0);
5841 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5842 gimplify_compound_expr (sub_p, pre_p, false);
5843 else
5844 gimplify_stmt (sub_p, pre_p);
5846 t = TREE_OPERAND (t, 1);
5848 while (TREE_CODE (t) == COMPOUND_EXPR);
5850 *expr_p = t;
5851 if (want_value)
5852 return GS_OK;
5853 else
5855 gimplify_stmt (expr_p, pre_p);
5856 return GS_ALL_DONE;
5860 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5861 gimplify. After gimplification, EXPR_P will point to a new temporary
5862 that holds the original value of the SAVE_EXPR node.
5864 PRE_P points to the list where side effects that must happen before
5865 *EXPR_P should be stored. */
5867 static enum gimplify_status
5868 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5870 enum gimplify_status ret = GS_ALL_DONE;
5871 tree val;
5873 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5874 val = TREE_OPERAND (*expr_p, 0);
5876 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5877 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5879 /* The operand may be a void-valued expression. It is
5880 being executed only for its side-effects. */
5881 if (TREE_TYPE (val) == void_type_node)
5883 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5884 is_gimple_stmt, fb_none);
5885 val = NULL;
5887 else
5888 /* The temporary may not be an SSA name as later abnormal and EH
5889 control flow may invalidate use/def domination. When in SSA
5890 form then assume there are no such issues and SAVE_EXPRs only
5891 appear via GENERIC foldings. */
5892 val = get_initialized_tmp_var (val, pre_p, post_p,
5893 gimple_in_ssa_p (cfun));
5895 TREE_OPERAND (*expr_p, 0) = val;
5896 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5899 *expr_p = val;
5901 return ret;
5904 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5906 unary_expr
5907 : ...
5908 | '&' varname
5911 PRE_P points to the list where side effects that must happen before
5912 *EXPR_P should be stored.
5914 POST_P points to the list where side effects that must happen after
5915 *EXPR_P should be stored. */
5917 static enum gimplify_status
5918 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5920 tree expr = *expr_p;
5921 tree op0 = TREE_OPERAND (expr, 0);
5922 enum gimplify_status ret;
5923 location_t loc = EXPR_LOCATION (*expr_p);
5925 switch (TREE_CODE (op0))
5927 case INDIRECT_REF:
5928 do_indirect_ref:
5929 /* Check if we are dealing with an expression of the form '&*ptr'.
5930 While the front end folds away '&*ptr' into 'ptr', these
5931 expressions may be generated internally by the compiler (e.g.,
5932 builtins like __builtin_va_end). */
5933 /* Caution: the silent array decomposition semantics we allow for
5934 ADDR_EXPR means we can't always discard the pair. */
5935 /* Gimplification of the ADDR_EXPR operand may drop
5936 cv-qualification conversions, so make sure we add them if
5937 needed. */
5939 tree op00 = TREE_OPERAND (op0, 0);
5940 tree t_expr = TREE_TYPE (expr);
5941 tree t_op00 = TREE_TYPE (op00);
5943 if (!useless_type_conversion_p (t_expr, t_op00))
5944 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5945 *expr_p = op00;
5946 ret = GS_OK;
5948 break;
5950 case VIEW_CONVERT_EXPR:
5951 /* Take the address of our operand and then convert it to the type of
5952 this ADDR_EXPR.
5954 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5955 all clear. The impact of this transformation is even less clear. */
5957 /* If the operand is a useless conversion, look through it. Doing so
5958 guarantees that the ADDR_EXPR and its operand will remain of the
5959 same type. */
5960 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5961 op0 = TREE_OPERAND (op0, 0);
5963 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5964 build_fold_addr_expr_loc (loc,
5965 TREE_OPERAND (op0, 0)));
5966 ret = GS_OK;
5967 break;
5969 case MEM_REF:
5970 if (integer_zerop (TREE_OPERAND (op0, 1)))
5971 goto do_indirect_ref;
5973 /* fall through */
5975 default:
5976 /* If we see a call to a declared builtin or see its address
5977 being taken (we can unify those cases here) then we can mark
5978 the builtin for implicit generation by GCC. */
5979 if (TREE_CODE (op0) == FUNCTION_DECL
5980 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
5981 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
5982 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
5984 /* We use fb_either here because the C frontend sometimes takes
5985 the address of a call that returns a struct; see
5986 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5987 the implied temporary explicit. */
5989 /* Make the operand addressable. */
5990 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5991 is_gimple_addressable, fb_either);
5992 if (ret == GS_ERROR)
5993 break;
5995 /* Then mark it. Beware that it may not be possible to do so directly
5996 if a temporary has been created by the gimplification. */
5997 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5999 op0 = TREE_OPERAND (expr, 0);
6001 /* For various reasons, the gimplification of the expression
6002 may have made a new INDIRECT_REF. */
6003 if (TREE_CODE (op0) == INDIRECT_REF)
6004 goto do_indirect_ref;
6006 mark_addressable (TREE_OPERAND (expr, 0));
6008 /* The FEs may end up building ADDR_EXPRs early on a decl with
6009 an incomplete type. Re-build ADDR_EXPRs in canonical form
6010 here. */
6011 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
6012 *expr_p = build_fold_addr_expr (op0);
6014 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
6015 recompute_tree_invariant_for_addr_expr (*expr_p);
6017 /* If we re-built the ADDR_EXPR add a conversion to the original type
6018 if required. */
6019 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
6020 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
6022 break;
6025 return ret;
6028 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
6029 value; output operands should be a gimple lvalue. */
6031 static enum gimplify_status
6032 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6034 tree expr;
6035 int noutputs;
6036 const char **oconstraints;
6037 int i;
6038 tree link;
6039 const char *constraint;
6040 bool allows_mem, allows_reg, is_inout;
6041 enum gimplify_status ret, tret;
6042 gasm *stmt;
6043 vec<tree, va_gc> *inputs;
6044 vec<tree, va_gc> *outputs;
6045 vec<tree, va_gc> *clobbers;
6046 vec<tree, va_gc> *labels;
6047 tree link_next;
6049 expr = *expr_p;
6050 noutputs = list_length (ASM_OUTPUTS (expr));
6051 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
6053 inputs = NULL;
6054 outputs = NULL;
6055 clobbers = NULL;
6056 labels = NULL;
6058 ret = GS_ALL_DONE;
6059 link_next = NULL_TREE;
6060 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
6062 bool ok;
6063 size_t constraint_len;
6065 link_next = TREE_CHAIN (link);
6067 oconstraints[i]
6068 = constraint
6069 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6070 constraint_len = strlen (constraint);
6071 if (constraint_len == 0)
6072 continue;
6074 ok = parse_output_constraint (&constraint, i, 0, 0,
6075 &allows_mem, &allows_reg, &is_inout);
6076 if (!ok)
6078 ret = GS_ERROR;
6079 is_inout = false;
6082 if (!allows_reg && allows_mem)
6083 mark_addressable (TREE_VALUE (link));
6085 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6086 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
6087 fb_lvalue | fb_mayfail);
6088 if (tret == GS_ERROR)
6090 error ("invalid lvalue in asm output %d", i);
6091 ret = tret;
6094 /* If the constraint does not allow memory make sure we gimplify
6095 it to a register if it is not already but its base is. This
6096 happens for complex and vector components. */
6097 if (!allows_mem)
6099 tree op = TREE_VALUE (link);
6100 if (! is_gimple_val (op)
6101 && is_gimple_reg_type (TREE_TYPE (op))
6102 && is_gimple_reg (get_base_address (op)))
6104 tree tem = create_tmp_reg (TREE_TYPE (op));
6105 tree ass;
6106 if (is_inout)
6108 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
6109 tem, unshare_expr (op));
6110 gimplify_and_add (ass, pre_p);
6112 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
6113 gimplify_and_add (ass, post_p);
6115 TREE_VALUE (link) = tem;
6116 tret = GS_OK;
6120 vec_safe_push (outputs, link);
6121 TREE_CHAIN (link) = NULL_TREE;
6123 if (is_inout)
6125 /* An input/output operand. To give the optimizers more
6126 flexibility, split it into separate input and output
6127 operands. */
6128 tree input;
6129 /* Buffer big enough to format a 32-bit UINT_MAX into. */
6130 char buf[11];
6132 /* Turn the in/out constraint into an output constraint. */
6133 char *p = xstrdup (constraint);
6134 p[0] = '=';
6135 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
6137 /* And add a matching input constraint. */
6138 if (allows_reg)
6140 sprintf (buf, "%u", i);
6142 /* If there are multiple alternatives in the constraint,
6143 handle each of them individually. Those that allow register
6144 will be replaced with operand number, the others will stay
6145 unchanged. */
6146 if (strchr (p, ',') != NULL)
6148 size_t len = 0, buflen = strlen (buf);
6149 char *beg, *end, *str, *dst;
6151 for (beg = p + 1;;)
6153 end = strchr (beg, ',');
6154 if (end == NULL)
6155 end = strchr (beg, '\0');
6156 if ((size_t) (end - beg) < buflen)
6157 len += buflen + 1;
6158 else
6159 len += end - beg + 1;
6160 if (*end)
6161 beg = end + 1;
6162 else
6163 break;
6166 str = (char *) alloca (len);
6167 for (beg = p + 1, dst = str;;)
6169 const char *tem;
6170 bool mem_p, reg_p, inout_p;
6172 end = strchr (beg, ',');
6173 if (end)
6174 *end = '\0';
6175 beg[-1] = '=';
6176 tem = beg - 1;
6177 parse_output_constraint (&tem, i, 0, 0,
6178 &mem_p, &reg_p, &inout_p);
6179 if (dst != str)
6180 *dst++ = ',';
6181 if (reg_p)
6183 memcpy (dst, buf, buflen);
6184 dst += buflen;
6186 else
6188 if (end)
6189 len = end - beg;
6190 else
6191 len = strlen (beg);
6192 memcpy (dst, beg, len);
6193 dst += len;
6195 if (end)
6196 beg = end + 1;
6197 else
6198 break;
6200 *dst = '\0';
6201 input = build_string (dst - str, str);
6203 else
6204 input = build_string (strlen (buf), buf);
6206 else
6207 input = build_string (constraint_len - 1, constraint + 1);
6209 free (p);
6211 input = build_tree_list (build_tree_list (NULL_TREE, input),
6212 unshare_expr (TREE_VALUE (link)));
6213 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6217 link_next = NULL_TREE;
6218 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6220 link_next = TREE_CHAIN (link);
6221 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6222 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6223 oconstraints, &allows_mem, &allows_reg);
6225 /* If we can't make copies, we can only accept memory. */
6226 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6228 if (allows_mem)
6229 allows_reg = 0;
6230 else
6232 error ("impossible constraint in %<asm%>");
6233 error ("non-memory input %d must stay in memory", i);
6234 return GS_ERROR;
6238 /* If the operand is a memory input, it should be an lvalue. */
6239 if (!allows_reg && allows_mem)
6241 tree inputv = TREE_VALUE (link);
6242 STRIP_NOPS (inputv);
6243 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6244 || TREE_CODE (inputv) == PREINCREMENT_EXPR
6245 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6246 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6247 || TREE_CODE (inputv) == MODIFY_EXPR)
6248 TREE_VALUE (link) = error_mark_node;
6249 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6250 is_gimple_lvalue, fb_lvalue | fb_mayfail);
6251 if (tret != GS_ERROR)
6253 /* Unlike output operands, memory inputs are not guaranteed
6254 to be lvalues by the FE, and while the expressions are
6255 marked addressable there, if it is e.g. a statement
6256 expression, temporaries in it might not end up being
6257 addressable. They might be already used in the IL and thus
6258 it is too late to make them addressable now though. */
6259 tree x = TREE_VALUE (link);
6260 while (handled_component_p (x))
6261 x = TREE_OPERAND (x, 0);
6262 if (TREE_CODE (x) == MEM_REF
6263 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6264 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6265 if ((VAR_P (x)
6266 || TREE_CODE (x) == PARM_DECL
6267 || TREE_CODE (x) == RESULT_DECL)
6268 && !TREE_ADDRESSABLE (x)
6269 && is_gimple_reg (x))
6271 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6272 input_location), 0,
6273 "memory input %d is not directly addressable",
6275 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6278 mark_addressable (TREE_VALUE (link));
6279 if (tret == GS_ERROR)
6281 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6282 "memory input %d is not directly addressable", i);
6283 ret = tret;
6286 else
6288 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6289 is_gimple_asm_val, fb_rvalue);
6290 if (tret == GS_ERROR)
6291 ret = tret;
6294 TREE_CHAIN (link) = NULL_TREE;
6295 vec_safe_push (inputs, link);
6298 link_next = NULL_TREE;
6299 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6301 link_next = TREE_CHAIN (link);
6302 TREE_CHAIN (link) = NULL_TREE;
6303 vec_safe_push (clobbers, link);
6306 link_next = NULL_TREE;
6307 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6309 link_next = TREE_CHAIN (link);
6310 TREE_CHAIN (link) = NULL_TREE;
6311 vec_safe_push (labels, link);
6314 /* Do not add ASMs with errors to the gimple IL stream. */
6315 if (ret != GS_ERROR)
6317 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6318 inputs, outputs, clobbers, labels);
6320 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6321 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6323 gimplify_seq_add_stmt (pre_p, stmt);
6326 return ret;
6329 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6330 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6331 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6332 return to this function.
6334 FIXME should we complexify the prequeue handling instead? Or use flags
6335 for all the cleanups and let the optimizer tighten them up? The current
6336 code seems pretty fragile; it will break on a cleanup within any
6337 non-conditional nesting. But any such nesting would be broken, anyway;
6338 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6339 and continues out of it. We can do that at the RTL level, though, so
6340 having an optimizer to tighten up try/finally regions would be a Good
6341 Thing. */
6343 static enum gimplify_status
6344 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6346 gimple_stmt_iterator iter;
6347 gimple_seq body_sequence = NULL;
6349 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6351 /* We only care about the number of conditions between the innermost
6352 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6353 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6354 int old_conds = gimplify_ctxp->conditions;
6355 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6356 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6357 gimplify_ctxp->conditions = 0;
6358 gimplify_ctxp->conditional_cleanups = NULL;
6359 gimplify_ctxp->in_cleanup_point_expr = true;
6361 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6363 gimplify_ctxp->conditions = old_conds;
6364 gimplify_ctxp->conditional_cleanups = old_cleanups;
6365 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6367 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6369 gimple *wce = gsi_stmt (iter);
6371 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6373 if (gsi_one_before_end_p (iter))
6375 /* Note that gsi_insert_seq_before and gsi_remove do not
6376 scan operands, unlike some other sequence mutators. */
6377 if (!gimple_wce_cleanup_eh_only (wce))
6378 gsi_insert_seq_before_without_update (&iter,
6379 gimple_wce_cleanup (wce),
6380 GSI_SAME_STMT);
6381 gsi_remove (&iter, true);
6382 break;
6384 else
6386 gtry *gtry;
6387 gimple_seq seq;
6388 enum gimple_try_flags kind;
6390 if (gimple_wce_cleanup_eh_only (wce))
6391 kind = GIMPLE_TRY_CATCH;
6392 else
6393 kind = GIMPLE_TRY_FINALLY;
6394 seq = gsi_split_seq_after (iter);
6396 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6397 /* Do not use gsi_replace here, as it may scan operands.
6398 We want to do a simple structural modification only. */
6399 gsi_set_stmt (&iter, gtry);
6400 iter = gsi_start (gtry->eval);
6403 else
6404 gsi_next (&iter);
6407 gimplify_seq_add_seq (pre_p, body_sequence);
6408 if (temp)
6410 *expr_p = temp;
6411 return GS_OK;
6413 else
6415 *expr_p = NULL;
6416 return GS_ALL_DONE;
6420 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6421 is the cleanup action required. EH_ONLY is true if the cleanup should
6422 only be executed if an exception is thrown, not on normal exit.
6423 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
6424 only valid for clobbers. */
6426 static void
6427 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
6428 bool force_uncond = false)
6430 gimple *wce;
6431 gimple_seq cleanup_stmts = NULL;
6433 /* Errors can result in improperly nested cleanups. Which results in
6434 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6435 if (seen_error ())
6436 return;
6438 if (gimple_conditional_context ())
6440 /* If we're in a conditional context, this is more complex. We only
6441 want to run the cleanup if we actually ran the initialization that
6442 necessitates it, but we want to run it after the end of the
6443 conditional context. So we wrap the try/finally around the
6444 condition and use a flag to determine whether or not to actually
6445 run the destructor. Thus
6447 test ? f(A()) : 0
6449 becomes (approximately)
6451 flag = 0;
6452 try {
6453 if (test) { A::A(temp); flag = 1; val = f(temp); }
6454 else { val = 0; }
6455 } finally {
6456 if (flag) A::~A(temp);
6460 if (force_uncond)
6462 gimplify_stmt (&cleanup, &cleanup_stmts);
6463 wce = gimple_build_wce (cleanup_stmts);
6464 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6466 else
6468 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6469 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6470 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6472 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6473 gimplify_stmt (&cleanup, &cleanup_stmts);
6474 wce = gimple_build_wce (cleanup_stmts);
6476 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6477 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6478 gimplify_seq_add_stmt (pre_p, ftrue);
6480 /* Because of this manipulation, and the EH edges that jump
6481 threading cannot redirect, the temporary (VAR) will appear
6482 to be used uninitialized. Don't warn. */
6483 TREE_NO_WARNING (var) = 1;
6486 else
6488 gimplify_stmt (&cleanup, &cleanup_stmts);
6489 wce = gimple_build_wce (cleanup_stmts);
6490 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6491 gimplify_seq_add_stmt (pre_p, wce);
6495 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6497 static enum gimplify_status
6498 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6500 tree targ = *expr_p;
6501 tree temp = TARGET_EXPR_SLOT (targ);
6502 tree init = TARGET_EXPR_INITIAL (targ);
6503 enum gimplify_status ret;
6505 bool unpoison_empty_seq = false;
6506 gimple_stmt_iterator unpoison_it;
6508 if (init)
6510 tree cleanup = NULL_TREE;
6512 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6513 to the temps list. Handle also variable length TARGET_EXPRs. */
6514 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6516 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6517 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6518 gimplify_vla_decl (temp, pre_p);
6520 else
6522 /* Save location where we need to place unpoisoning. It's possible
6523 that a variable will be converted to needs_to_live_in_memory. */
6524 unpoison_it = gsi_last (*pre_p);
6525 unpoison_empty_seq = gsi_end_p (unpoison_it);
6527 gimple_add_tmp_var (temp);
6530 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6531 expression is supposed to initialize the slot. */
6532 if (VOID_TYPE_P (TREE_TYPE (init)))
6533 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6534 else
6536 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6537 init = init_expr;
6538 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6539 init = NULL;
6540 ggc_free (init_expr);
6542 if (ret == GS_ERROR)
6544 /* PR c++/28266 Make sure this is expanded only once. */
6545 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6546 return GS_ERROR;
6548 if (init)
6549 gimplify_and_add (init, pre_p);
6551 /* If needed, push the cleanup for the temp. */
6552 if (TARGET_EXPR_CLEANUP (targ))
6554 if (CLEANUP_EH_ONLY (targ))
6555 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6556 CLEANUP_EH_ONLY (targ), pre_p);
6557 else
6558 cleanup = TARGET_EXPR_CLEANUP (targ);
6561 /* Add a clobber for the temporary going out of scope, like
6562 gimplify_bind_expr. */
6563 if (gimplify_ctxp->in_cleanup_point_expr
6564 && needs_to_live_in_memory (temp))
6566 if (flag_stack_reuse == SR_ALL)
6568 tree clobber = build_clobber (TREE_TYPE (temp));
6569 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6570 gimple_push_cleanup (temp, clobber, false, pre_p, true);
6572 if (asan_poisoned_variables
6573 && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
6574 && dbg_cnt (asan_use_after_scope)
6575 && !gimplify_omp_ctxp)
6577 tree asan_cleanup = build_asan_poison_call_expr (temp);
6578 if (asan_cleanup)
6580 if (unpoison_empty_seq)
6581 unpoison_it = gsi_start (*pre_p);
6583 asan_poison_variable (temp, false, &unpoison_it,
6584 unpoison_empty_seq);
6585 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6589 if (cleanup)
6590 gimple_push_cleanup (temp, cleanup, false, pre_p);
6592 /* Only expand this once. */
6593 TREE_OPERAND (targ, 3) = init;
6594 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6596 else
6597 /* We should have expanded this before. */
6598 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6600 *expr_p = temp;
6601 return GS_OK;
6604 /* Gimplification of expression trees. */
6606 /* Gimplify an expression which appears at statement context. The
6607 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6608 NULL, a new sequence is allocated.
6610 Return true if we actually added a statement to the queue. */
6612 bool
6613 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6615 gimple_seq_node last;
6617 last = gimple_seq_last (*seq_p);
6618 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6619 return last != gimple_seq_last (*seq_p);
6622 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6623 to CTX. If entries already exist, force them to be some flavor of private.
6624 If there is no enclosing parallel, do nothing. */
6626 void
6627 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6629 splay_tree_node n;
6631 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6632 return;
6636 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6637 if (n != NULL)
6639 if (n->value & GOVD_SHARED)
6640 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6641 else if (n->value & GOVD_MAP)
6642 n->value |= GOVD_MAP_TO_ONLY;
6643 else
6644 return;
6646 else if ((ctx->region_type & ORT_TARGET) != 0)
6648 if (ctx->target_map_scalars_firstprivate)
6649 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6650 else
6651 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6653 else if (ctx->region_type != ORT_WORKSHARE
6654 && ctx->region_type != ORT_SIMD
6655 && ctx->region_type != ORT_ACC
6656 && !(ctx->region_type & ORT_TARGET_DATA))
6657 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6659 ctx = ctx->outer_context;
6661 while (ctx);
6664 /* Similarly for each of the type sizes of TYPE. */
6666 static void
6667 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6669 if (type == NULL || type == error_mark_node)
6670 return;
6671 type = TYPE_MAIN_VARIANT (type);
6673 if (ctx->privatized_types->add (type))
6674 return;
6676 switch (TREE_CODE (type))
6678 case INTEGER_TYPE:
6679 case ENUMERAL_TYPE:
6680 case BOOLEAN_TYPE:
6681 case REAL_TYPE:
6682 case FIXED_POINT_TYPE:
6683 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6684 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6685 break;
6687 case ARRAY_TYPE:
6688 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6689 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6690 break;
6692 case RECORD_TYPE:
6693 case UNION_TYPE:
6694 case QUAL_UNION_TYPE:
6696 tree field;
6697 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6698 if (TREE_CODE (field) == FIELD_DECL)
6700 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6701 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6704 break;
6706 case POINTER_TYPE:
6707 case REFERENCE_TYPE:
6708 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6709 break;
6711 default:
6712 break;
6715 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6716 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6717 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6720 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6722 static void
6723 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6725 splay_tree_node n;
6726 unsigned int nflags;
6727 tree t;
6729 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6730 return;
6732 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6733 there are constructors involved somewhere. Exception is a shared clause,
6734 there is nothing privatized in that case. */
6735 if ((flags & GOVD_SHARED) == 0
6736 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6737 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6738 flags |= GOVD_SEEN;
6740 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6741 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6743 /* We shouldn't be re-adding the decl with the same data
6744 sharing class. */
6745 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6746 nflags = n->value | flags;
6747 /* The only combination of data sharing classes we should see is
6748 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6749 reduction variables to be used in data sharing clauses. */
6750 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6751 || ((nflags & GOVD_DATA_SHARE_CLASS)
6752 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6753 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6754 n->value = nflags;
6755 return;
6758 /* When adding a variable-sized variable, we have to handle all sorts
6759 of additional bits of data: the pointer replacement variable, and
6760 the parameters of the type. */
6761 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6763 /* Add the pointer replacement variable as PRIVATE if the variable
6764 replacement is private, else FIRSTPRIVATE since we'll need the
6765 address of the original variable either for SHARED, or for the
6766 copy into or out of the context. */
6767 if (!(flags & GOVD_LOCAL))
6769 if (flags & GOVD_MAP)
6770 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6771 else if (flags & GOVD_PRIVATE)
6772 nflags = GOVD_PRIVATE;
6773 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6774 && (flags & GOVD_FIRSTPRIVATE))
6775 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6776 else
6777 nflags = GOVD_FIRSTPRIVATE;
6778 nflags |= flags & GOVD_SEEN;
6779 t = DECL_VALUE_EXPR (decl);
6780 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6781 t = TREE_OPERAND (t, 0);
6782 gcc_assert (DECL_P (t));
6783 omp_add_variable (ctx, t, nflags);
6786 /* Add all of the variable and type parameters (which should have
6787 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6788 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6789 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6790 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6792 /* The variable-sized variable itself is never SHARED, only some form
6793 of PRIVATE. The sharing would take place via the pointer variable
6794 which we remapped above. */
6795 if (flags & GOVD_SHARED)
6796 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6797 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6799 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6800 alloca statement we generate for the variable, so make sure it
6801 is available. This isn't automatically needed for the SHARED
6802 case, since we won't be allocating local storage then.
6803 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6804 in this case omp_notice_variable will be called later
6805 on when it is gimplified. */
6806 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6807 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6808 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6810 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6811 && lang_hooks.decls.omp_privatize_by_reference (decl))
6813 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6815 /* Similar to the direct variable sized case above, we'll need the
6816 size of references being privatized. */
6817 if ((flags & GOVD_SHARED) == 0)
6819 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6820 if (DECL_P (t))
6821 omp_notice_variable (ctx, t, true);
6825 if (n != NULL)
6826 n->value |= flags;
6827 else
6828 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6830 /* For reductions clauses in OpenACC loop directives, by default create a
6831 copy clause on the enclosing parallel construct for carrying back the
6832 results. */
6833 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6835 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6836 while (outer_ctx)
6838 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6839 if (n != NULL)
6841 /* Ignore local variables and explicitly declared clauses. */
6842 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6843 break;
6844 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6846 /* According to the OpenACC spec, such a reduction variable
6847 should already have a copy map on a kernels construct,
6848 verify that here. */
6849 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6850 && (n->value & GOVD_MAP));
6852 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6854 /* Remove firstprivate and make it a copy map. */
6855 n->value &= ~GOVD_FIRSTPRIVATE;
6856 n->value |= GOVD_MAP;
6859 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6861 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6862 GOVD_MAP | GOVD_SEEN);
6863 break;
6865 outer_ctx = outer_ctx->outer_context;
6870 /* Notice a threadprivate variable DECL used in OMP context CTX.
6871 This just prints out diagnostics about threadprivate variable uses
6872 in untied tasks. If DECL2 is non-NULL, prevent this warning
6873 on that variable. */
6875 static bool
6876 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6877 tree decl2)
6879 splay_tree_node n;
6880 struct gimplify_omp_ctx *octx;
6882 for (octx = ctx; octx; octx = octx->outer_context)
6883 if ((octx->region_type & ORT_TARGET) != 0)
6885 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6886 if (n == NULL)
6888 error ("threadprivate variable %qE used in target region",
6889 DECL_NAME (decl));
6890 error_at (octx->location, "enclosing target region");
6891 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6893 if (decl2)
6894 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6897 if (ctx->region_type != ORT_UNTIED_TASK)
6898 return false;
6899 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6900 if (n == NULL)
6902 error ("threadprivate variable %qE used in untied task",
6903 DECL_NAME (decl));
6904 error_at (ctx->location, "enclosing task");
6905 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6907 if (decl2)
6908 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6909 return false;
6912 /* Return true if global var DECL is device resident. */
6914 static bool
6915 device_resident_p (tree decl)
6917 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6919 if (!attr)
6920 return false;
6922 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6924 tree c = TREE_VALUE (t);
6925 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6926 return true;
6929 return false;
6932 /* Return true if DECL has an ACC DECLARE attribute. */
6934 static bool
6935 is_oacc_declared (tree decl)
6937 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
6938 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
6939 return declared != NULL_TREE;
6942 /* Determine outer default flags for DECL mentioned in an OMP region
6943 but not declared in an enclosing clause.
6945 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6946 remapped firstprivate instead of shared. To some extent this is
6947 addressed in omp_firstprivatize_type_sizes, but not
6948 effectively. */
6950 static unsigned
6951 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
6952 bool in_code, unsigned flags)
6954 enum omp_clause_default_kind default_kind = ctx->default_kind;
6955 enum omp_clause_default_kind kind;
6957 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
6958 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6959 default_kind = kind;
6961 switch (default_kind)
6963 case OMP_CLAUSE_DEFAULT_NONE:
6965 const char *rtype;
6967 if (ctx->region_type & ORT_PARALLEL)
6968 rtype = "parallel";
6969 else if (ctx->region_type & ORT_TASK)
6970 rtype = "task";
6971 else if (ctx->region_type & ORT_TEAMS)
6972 rtype = "teams";
6973 else
6974 gcc_unreachable ();
6976 error ("%qE not specified in enclosing %qs",
6977 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
6978 error_at (ctx->location, "enclosing %qs", rtype);
6980 /* FALLTHRU */
6981 case OMP_CLAUSE_DEFAULT_SHARED:
6982 flags |= GOVD_SHARED;
6983 break;
6984 case OMP_CLAUSE_DEFAULT_PRIVATE:
6985 flags |= GOVD_PRIVATE;
6986 break;
6987 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
6988 flags |= GOVD_FIRSTPRIVATE;
6989 break;
6990 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6991 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
6992 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
6993 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
6995 omp_notice_variable (octx, decl, in_code);
6996 for (; octx; octx = octx->outer_context)
6998 splay_tree_node n2;
7000 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
7001 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
7002 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
7003 continue;
7004 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7006 flags |= GOVD_FIRSTPRIVATE;
7007 goto found_outer;
7009 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7011 flags |= GOVD_SHARED;
7012 goto found_outer;
7017 if (TREE_CODE (decl) == PARM_DECL
7018 || (!is_global_var (decl)
7019 && DECL_CONTEXT (decl) == current_function_decl))
7020 flags |= GOVD_FIRSTPRIVATE;
7021 else
7022 flags |= GOVD_SHARED;
7023 found_outer:
7024 break;
7026 default:
7027 gcc_unreachable ();
7030 return flags;
7034 /* Determine outer default flags for DECL mentioned in an OACC region
7035 but not declared in an enclosing clause. */
7037 static unsigned
7038 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7040 const char *rkind;
7041 bool on_device = false;
7042 bool declared = is_oacc_declared (decl);
7043 tree type = TREE_TYPE (decl);
7045 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7046 type = TREE_TYPE (type);
7048 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7049 && is_global_var (decl)
7050 && device_resident_p (decl))
7052 on_device = true;
7053 flags |= GOVD_MAP_TO_ONLY;
7056 switch (ctx->region_type)
7058 case ORT_ACC_KERNELS:
7059 rkind = "kernels";
7061 if (AGGREGATE_TYPE_P (type))
7063 /* Aggregates default to 'present_or_copy', or 'present'. */
7064 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7065 flags |= GOVD_MAP;
7066 else
7067 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7069 else
7070 /* Scalars default to 'copy'. */
7071 flags |= GOVD_MAP | GOVD_MAP_FORCE;
7073 break;
7075 case ORT_ACC_PARALLEL:
7076 rkind = "parallel";
7078 if (on_device || declared)
7079 flags |= GOVD_MAP;
7080 else if (AGGREGATE_TYPE_P (type))
7082 /* Aggregates default to 'present_or_copy', or 'present'. */
7083 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7084 flags |= GOVD_MAP;
7085 else
7086 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7088 else
7089 /* Scalars default to 'firstprivate'. */
7090 flags |= GOVD_FIRSTPRIVATE;
7092 break;
7094 default:
7095 gcc_unreachable ();
7098 if (DECL_ARTIFICIAL (decl))
7099 ; /* We can get compiler-generated decls, and should not complain
7100 about them. */
7101 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7103 error ("%qE not specified in enclosing OpenACC %qs construct",
7104 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7105 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7107 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7108 ; /* Handled above. */
7109 else
7110 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7112 return flags;
7115 /* Record the fact that DECL was used within the OMP context CTX.
7116 IN_CODE is true when real code uses DECL, and false when we should
7117 merely emit default(none) errors. Return true if DECL is going to
7118 be remapped and thus DECL shouldn't be gimplified into its
7119 DECL_VALUE_EXPR (if any). */
7121 static bool
7122 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7124 splay_tree_node n;
7125 unsigned flags = in_code ? GOVD_SEEN : 0;
7126 bool ret = false, shared;
7128 if (error_operand_p (decl))
7129 return false;
7131 if (ctx->region_type == ORT_NONE)
7132 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7134 if (is_global_var (decl))
7136 /* Threadprivate variables are predetermined. */
7137 if (DECL_THREAD_LOCAL_P (decl))
7138 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7140 if (DECL_HAS_VALUE_EXPR_P (decl))
7142 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7144 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7145 return omp_notice_threadprivate_variable (ctx, decl, value);
7148 if (gimplify_omp_ctxp->outer_context == NULL
7149 && VAR_P (decl)
7150 && oacc_get_fn_attrib (current_function_decl))
7152 location_t loc = DECL_SOURCE_LOCATION (decl);
7154 if (lookup_attribute ("omp declare target link",
7155 DECL_ATTRIBUTES (decl)))
7157 error_at (loc,
7158 "%qE with %<link%> clause used in %<routine%> function",
7159 DECL_NAME (decl));
7160 return false;
7162 else if (!lookup_attribute ("omp declare target",
7163 DECL_ATTRIBUTES (decl)))
7165 error_at (loc,
7166 "%qE requires a %<declare%> directive for use "
7167 "in a %<routine%> function", DECL_NAME (decl));
7168 return false;
7173 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7174 if ((ctx->region_type & ORT_TARGET) != 0)
7176 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7177 if (n == NULL)
7179 unsigned nflags = flags;
7180 if (ctx->target_map_pointers_as_0len_arrays
7181 || ctx->target_map_scalars_firstprivate)
7183 bool is_declare_target = false;
7184 bool is_scalar = false;
7185 if (is_global_var (decl)
7186 && varpool_node::get_create (decl)->offloadable)
7188 struct gimplify_omp_ctx *octx;
7189 for (octx = ctx->outer_context;
7190 octx; octx = octx->outer_context)
7192 n = splay_tree_lookup (octx->variables,
7193 (splay_tree_key)decl);
7194 if (n
7195 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7196 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7197 break;
7199 is_declare_target = octx == NULL;
7201 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
7202 is_scalar = lang_hooks.decls.omp_scalar_p (decl);
7203 if (is_declare_target)
7205 else if (ctx->target_map_pointers_as_0len_arrays
7206 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7207 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7208 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7209 == POINTER_TYPE)))
7210 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
7211 else if (is_scalar)
7212 nflags |= GOVD_FIRSTPRIVATE;
7215 struct gimplify_omp_ctx *octx = ctx->outer_context;
7216 if ((ctx->region_type & ORT_ACC) && octx)
7218 /* Look in outer OpenACC contexts, to see if there's a
7219 data attribute for this variable. */
7220 omp_notice_variable (octx, decl, in_code);
7222 for (; octx; octx = octx->outer_context)
7224 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7225 break;
7226 splay_tree_node n2
7227 = splay_tree_lookup (octx->variables,
7228 (splay_tree_key) decl);
7229 if (n2)
7231 if (octx->region_type == ORT_ACC_HOST_DATA)
7232 error ("variable %qE declared in enclosing "
7233 "%<host_data%> region", DECL_NAME (decl));
7234 nflags |= GOVD_MAP;
7235 if (octx->region_type == ORT_ACC_DATA
7236 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7237 nflags |= GOVD_MAP_0LEN_ARRAY;
7238 goto found_outer;
7244 tree type = TREE_TYPE (decl);
7246 if (nflags == flags
7247 && gimplify_omp_ctxp->target_firstprivatize_array_bases
7248 && lang_hooks.decls.omp_privatize_by_reference (decl))
7249 type = TREE_TYPE (type);
7250 if (nflags == flags
7251 && !lang_hooks.types.omp_mappable_type (type))
7253 error ("%qD referenced in target region does not have "
7254 "a mappable type", decl);
7255 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7257 else if (nflags == flags)
7259 if ((ctx->region_type & ORT_ACC) != 0)
7260 nflags = oacc_default_clause (ctx, decl, flags);
7261 else
7262 nflags |= GOVD_MAP;
7265 found_outer:
7266 omp_add_variable (ctx, decl, nflags);
7268 else
7270 /* If nothing changed, there's nothing left to do. */
7271 if ((n->value & flags) == flags)
7272 return ret;
7273 flags |= n->value;
7274 n->value = flags;
7276 goto do_outer;
7279 if (n == NULL)
7281 if (ctx->region_type == ORT_WORKSHARE
7282 || ctx->region_type == ORT_SIMD
7283 || ctx->region_type == ORT_ACC
7284 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7285 goto do_outer;
7287 flags = omp_default_clause (ctx, decl, in_code, flags);
7289 if ((flags & GOVD_PRIVATE)
7290 && lang_hooks.decls.omp_private_outer_ref (decl))
7291 flags |= GOVD_PRIVATE_OUTER_REF;
7293 omp_add_variable (ctx, decl, flags);
7295 shared = (flags & GOVD_SHARED) != 0;
7296 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7297 goto do_outer;
7300 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7301 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7302 && DECL_SIZE (decl))
7304 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7306 splay_tree_node n2;
7307 tree t = DECL_VALUE_EXPR (decl);
7308 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7309 t = TREE_OPERAND (t, 0);
7310 gcc_assert (DECL_P (t));
7311 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7312 n2->value |= GOVD_SEEN;
7314 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7315 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7316 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7317 != INTEGER_CST))
7319 splay_tree_node n2;
7320 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7321 gcc_assert (DECL_P (t));
7322 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7323 if (n2)
7324 omp_notice_variable (ctx, t, true);
7328 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7329 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7331 /* If nothing changed, there's nothing left to do. */
7332 if ((n->value & flags) == flags)
7333 return ret;
7334 flags |= n->value;
7335 n->value = flags;
7337 do_outer:
7338 /* If the variable is private in the current context, then we don't
7339 need to propagate anything to an outer context. */
7340 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7341 return ret;
7342 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7343 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7344 return ret;
7345 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7346 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7347 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7348 return ret;
7349 if (ctx->outer_context
7350 && omp_notice_variable (ctx->outer_context, decl, in_code))
7351 return true;
7352 return ret;
7355 /* Verify that DECL is private within CTX. If there's specific information
7356 to the contrary in the innermost scope, generate an error. */
7358 static bool
7359 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7361 splay_tree_node n;
7363 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7364 if (n != NULL)
7366 if (n->value & GOVD_SHARED)
7368 if (ctx == gimplify_omp_ctxp)
7370 if (simd)
7371 error ("iteration variable %qE is predetermined linear",
7372 DECL_NAME (decl));
7373 else
7374 error ("iteration variable %qE should be private",
7375 DECL_NAME (decl));
7376 n->value = GOVD_PRIVATE;
7377 return true;
7379 else
7380 return false;
7382 else if ((n->value & GOVD_EXPLICIT) != 0
7383 && (ctx == gimplify_omp_ctxp
7384 || (ctx->region_type == ORT_COMBINED_PARALLEL
7385 && gimplify_omp_ctxp->outer_context == ctx)))
7387 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7388 error ("iteration variable %qE should not be firstprivate",
7389 DECL_NAME (decl));
7390 else if ((n->value & GOVD_REDUCTION) != 0)
7391 error ("iteration variable %qE should not be reduction",
7392 DECL_NAME (decl));
7393 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
7394 error ("iteration variable %qE should not be linear",
7395 DECL_NAME (decl));
7396 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
7397 error ("iteration variable %qE should not be lastprivate",
7398 DECL_NAME (decl));
7399 else if (simd && (n->value & GOVD_PRIVATE) != 0)
7400 error ("iteration variable %qE should not be private",
7401 DECL_NAME (decl));
7402 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
7403 error ("iteration variable %qE is predetermined linear",
7404 DECL_NAME (decl));
7406 return (ctx == gimplify_omp_ctxp
7407 || (ctx->region_type == ORT_COMBINED_PARALLEL
7408 && gimplify_omp_ctxp->outer_context == ctx));
7411 if (ctx->region_type != ORT_WORKSHARE
7412 && ctx->region_type != ORT_SIMD
7413 && ctx->region_type != ORT_ACC)
7414 return false;
7415 else if (ctx->outer_context)
7416 return omp_is_private (ctx->outer_context, decl, simd);
7417 return false;
7420 /* Return true if DECL is private within a parallel region
7421 that binds to the current construct's context or in parallel
7422 region's REDUCTION clause. */
7424 static bool
7425 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7427 splay_tree_node n;
7431 ctx = ctx->outer_context;
7432 if (ctx == NULL)
7434 if (is_global_var (decl))
7435 return false;
7437 /* References might be private, but might be shared too,
7438 when checking for copyprivate, assume they might be
7439 private, otherwise assume they might be shared. */
7440 if (copyprivate)
7441 return true;
7443 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7444 return false;
7446 /* Treat C++ privatized non-static data members outside
7447 of the privatization the same. */
7448 if (omp_member_access_dummy_var (decl))
7449 return false;
7451 return true;
7454 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7456 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7457 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7458 continue;
7460 if (n != NULL)
7462 if ((n->value & GOVD_LOCAL) != 0
7463 && omp_member_access_dummy_var (decl))
7464 return false;
7465 return (n->value & GOVD_SHARED) == 0;
7468 while (ctx->region_type == ORT_WORKSHARE
7469 || ctx->region_type == ORT_SIMD
7470 || ctx->region_type == ORT_ACC);
7471 return false;
7474 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7476 static tree
7477 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7479 tree t = *tp;
7481 /* If this node has been visited, unmark it and keep looking. */
7482 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7483 return t;
7485 if (IS_TYPE_OR_DECL_P (t))
7486 *walk_subtrees = 0;
7487 return NULL_TREE;
7490 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7491 and previous omp contexts. */
7493 static void
7494 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7495 enum omp_region_type region_type,
7496 enum tree_code code)
7498 struct gimplify_omp_ctx *ctx, *outer_ctx;
7499 tree c;
7500 hash_map<tree, tree> *struct_map_to_clause = NULL;
7501 tree *prev_list_p = NULL;
7503 ctx = new_omp_context (region_type);
7504 outer_ctx = ctx->outer_context;
7505 if (code == OMP_TARGET)
7507 if (!lang_GNU_Fortran ())
7508 ctx->target_map_pointers_as_0len_arrays = true;
7509 ctx->target_map_scalars_firstprivate = true;
7511 if (!lang_GNU_Fortran ())
7512 switch (code)
7514 case OMP_TARGET:
7515 case OMP_TARGET_DATA:
7516 case OMP_TARGET_ENTER_DATA:
7517 case OMP_TARGET_EXIT_DATA:
7518 case OACC_DECLARE:
7519 case OACC_HOST_DATA:
7520 ctx->target_firstprivatize_array_bases = true;
7521 default:
7522 break;
7525 while ((c = *list_p) != NULL)
7527 bool remove = false;
7528 bool notice_outer = true;
7529 const char *check_non_private = NULL;
7530 unsigned int flags;
7531 tree decl;
7533 switch (OMP_CLAUSE_CODE (c))
7535 case OMP_CLAUSE_PRIVATE:
7536 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
7537 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
7539 flags |= GOVD_PRIVATE_OUTER_REF;
7540 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
7542 else
7543 notice_outer = false;
7544 goto do_add;
7545 case OMP_CLAUSE_SHARED:
7546 flags = GOVD_SHARED | GOVD_EXPLICIT;
7547 goto do_add;
7548 case OMP_CLAUSE_FIRSTPRIVATE:
7549 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7550 check_non_private = "firstprivate";
7551 goto do_add;
7552 case OMP_CLAUSE_LASTPRIVATE:
7553 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
7554 check_non_private = "lastprivate";
7555 decl = OMP_CLAUSE_DECL (c);
7556 if (error_operand_p (decl))
7557 goto do_add;
7558 else if (outer_ctx
7559 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
7560 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
7561 && splay_tree_lookup (outer_ctx->variables,
7562 (splay_tree_key) decl) == NULL)
7564 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
7565 if (outer_ctx->outer_context)
7566 omp_notice_variable (outer_ctx->outer_context, decl, true);
7568 else if (outer_ctx
7569 && (outer_ctx->region_type & ORT_TASK) != 0
7570 && outer_ctx->combined_loop
7571 && splay_tree_lookup (outer_ctx->variables,
7572 (splay_tree_key) decl) == NULL)
7574 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7575 if (outer_ctx->outer_context)
7576 omp_notice_variable (outer_ctx->outer_context, decl, true);
7578 else if (outer_ctx
7579 && (outer_ctx->region_type == ORT_WORKSHARE
7580 || outer_ctx->region_type == ORT_ACC)
7581 && outer_ctx->combined_loop
7582 && splay_tree_lookup (outer_ctx->variables,
7583 (splay_tree_key) decl) == NULL
7584 && !omp_check_private (outer_ctx, decl, false))
7586 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7587 if (outer_ctx->outer_context
7588 && (outer_ctx->outer_context->region_type
7589 == ORT_COMBINED_PARALLEL)
7590 && splay_tree_lookup (outer_ctx->outer_context->variables,
7591 (splay_tree_key) decl) == NULL)
7593 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
7594 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
7595 if (octx->outer_context)
7597 octx = octx->outer_context;
7598 if (octx->region_type == ORT_WORKSHARE
7599 && octx->combined_loop
7600 && splay_tree_lookup (octx->variables,
7601 (splay_tree_key) decl) == NULL
7602 && !omp_check_private (octx, decl, false))
7604 omp_add_variable (octx, decl,
7605 GOVD_LASTPRIVATE | GOVD_SEEN);
7606 octx = octx->outer_context;
7607 if (octx
7608 && octx->region_type == ORT_COMBINED_TEAMS
7609 && (splay_tree_lookup (octx->variables,
7610 (splay_tree_key) decl)
7611 == NULL))
7613 omp_add_variable (octx, decl,
7614 GOVD_SHARED | GOVD_SEEN);
7615 octx = octx->outer_context;
7618 if (octx)
7619 omp_notice_variable (octx, decl, true);
7622 else if (outer_ctx->outer_context)
7623 omp_notice_variable (outer_ctx->outer_context, decl, true);
7625 goto do_add;
7626 case OMP_CLAUSE_REDUCTION:
7627 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
7628 /* OpenACC permits reductions on private variables. */
7629 if (!(region_type & ORT_ACC))
7630 check_non_private = "reduction";
7631 decl = OMP_CLAUSE_DECL (c);
7632 if (TREE_CODE (decl) == MEM_REF)
7634 tree type = TREE_TYPE (decl);
7635 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
7636 NULL, is_gimple_val, fb_rvalue, false)
7637 == GS_ERROR)
7639 remove = true;
7640 break;
7642 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7643 if (DECL_P (v))
7645 omp_firstprivatize_variable (ctx, v);
7646 omp_notice_variable (ctx, v, true);
7648 decl = TREE_OPERAND (decl, 0);
7649 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
7651 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
7652 NULL, is_gimple_val, fb_rvalue, false)
7653 == GS_ERROR)
7655 remove = true;
7656 break;
7658 v = TREE_OPERAND (decl, 1);
7659 if (DECL_P (v))
7661 omp_firstprivatize_variable (ctx, v);
7662 omp_notice_variable (ctx, v, true);
7664 decl = TREE_OPERAND (decl, 0);
7666 if (TREE_CODE (decl) == ADDR_EXPR
7667 || TREE_CODE (decl) == INDIRECT_REF)
7668 decl = TREE_OPERAND (decl, 0);
7670 goto do_add_decl;
7671 case OMP_CLAUSE_LINEAR:
7672 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
7673 is_gimple_val, fb_rvalue) == GS_ERROR)
7675 remove = true;
7676 break;
7678 else
7680 if (code == OMP_SIMD
7681 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7683 struct gimplify_omp_ctx *octx = outer_ctx;
7684 if (octx
7685 && octx->region_type == ORT_WORKSHARE
7686 && octx->combined_loop
7687 && !octx->distribute)
7689 if (octx->outer_context
7690 && (octx->outer_context->region_type
7691 == ORT_COMBINED_PARALLEL))
7692 octx = octx->outer_context->outer_context;
7693 else
7694 octx = octx->outer_context;
7696 if (octx
7697 && octx->region_type == ORT_WORKSHARE
7698 && octx->combined_loop
7699 && octx->distribute)
7701 error_at (OMP_CLAUSE_LOCATION (c),
7702 "%<linear%> clause for variable other than "
7703 "loop iterator specified on construct "
7704 "combined with %<distribute%>");
7705 remove = true;
7706 break;
7709 /* For combined #pragma omp parallel for simd, need to put
7710 lastprivate and perhaps firstprivate too on the
7711 parallel. Similarly for #pragma omp for simd. */
7712 struct gimplify_omp_ctx *octx = outer_ctx;
7713 decl = NULL_TREE;
7716 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7717 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7718 break;
7719 decl = OMP_CLAUSE_DECL (c);
7720 if (error_operand_p (decl))
7722 decl = NULL_TREE;
7723 break;
7725 flags = GOVD_SEEN;
7726 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7727 flags |= GOVD_FIRSTPRIVATE;
7728 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7729 flags |= GOVD_LASTPRIVATE;
7730 if (octx
7731 && octx->region_type == ORT_WORKSHARE
7732 && octx->combined_loop)
7734 if (octx->outer_context
7735 && (octx->outer_context->region_type
7736 == ORT_COMBINED_PARALLEL))
7737 octx = octx->outer_context;
7738 else if (omp_check_private (octx, decl, false))
7739 break;
7741 else if (octx
7742 && (octx->region_type & ORT_TASK) != 0
7743 && octx->combined_loop)
7745 else if (octx
7746 && octx->region_type == ORT_COMBINED_PARALLEL
7747 && ctx->region_type == ORT_WORKSHARE
7748 && octx == outer_ctx)
7749 flags = GOVD_SEEN | GOVD_SHARED;
7750 else if (octx
7751 && octx->region_type == ORT_COMBINED_TEAMS)
7752 flags = GOVD_SEEN | GOVD_SHARED;
7753 else if (octx
7754 && octx->region_type == ORT_COMBINED_TARGET)
7756 flags &= ~GOVD_LASTPRIVATE;
7757 if (flags == GOVD_SEEN)
7758 break;
7760 else
7761 break;
7762 splay_tree_node on
7763 = splay_tree_lookup (octx->variables,
7764 (splay_tree_key) decl);
7765 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
7767 octx = NULL;
7768 break;
7770 omp_add_variable (octx, decl, flags);
7771 if (octx->outer_context == NULL)
7772 break;
7773 octx = octx->outer_context;
7775 while (1);
7776 if (octx
7777 && decl
7778 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7779 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7780 omp_notice_variable (octx, decl, true);
7782 flags = GOVD_LINEAR | GOVD_EXPLICIT;
7783 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7784 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7786 notice_outer = false;
7787 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7789 goto do_add;
7791 case OMP_CLAUSE_MAP:
7792 decl = OMP_CLAUSE_DECL (c);
7793 if (error_operand_p (decl))
7794 remove = true;
7795 switch (code)
7797 case OMP_TARGET:
7798 break;
7799 case OACC_DATA:
7800 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
7801 break;
7802 /* FALLTHRU */
7803 case OMP_TARGET_DATA:
7804 case OMP_TARGET_ENTER_DATA:
7805 case OMP_TARGET_EXIT_DATA:
7806 case OACC_ENTER_DATA:
7807 case OACC_EXIT_DATA:
7808 case OACC_HOST_DATA:
7809 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7810 || (OMP_CLAUSE_MAP_KIND (c)
7811 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7812 /* For target {,enter ,exit }data only the array slice is
7813 mapped, but not the pointer to it. */
7814 remove = true;
7815 break;
7816 default:
7817 break;
7819 if (remove)
7820 break;
7821 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
7823 struct gimplify_omp_ctx *octx;
7824 for (octx = outer_ctx; octx; octx = octx->outer_context)
7826 if (octx->region_type != ORT_ACC_HOST_DATA)
7827 break;
7828 splay_tree_node n2
7829 = splay_tree_lookup (octx->variables,
7830 (splay_tree_key) decl);
7831 if (n2)
7832 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
7833 "declared in enclosing %<host_data%> region",
7834 DECL_NAME (decl));
7837 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7838 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7839 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7840 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7841 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7843 remove = true;
7844 break;
7846 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7847 || (OMP_CLAUSE_MAP_KIND (c)
7848 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7849 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
7851 OMP_CLAUSE_SIZE (c)
7852 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
7853 false);
7854 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
7855 GOVD_FIRSTPRIVATE | GOVD_SEEN);
7857 if (!DECL_P (decl))
7859 tree d = decl, *pd;
7860 if (TREE_CODE (d) == ARRAY_REF)
7862 while (TREE_CODE (d) == ARRAY_REF)
7863 d = TREE_OPERAND (d, 0);
7864 if (TREE_CODE (d) == COMPONENT_REF
7865 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
7866 decl = d;
7868 pd = &OMP_CLAUSE_DECL (c);
7869 if (d == decl
7870 && TREE_CODE (decl) == INDIRECT_REF
7871 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7872 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7873 == REFERENCE_TYPE))
7875 pd = &TREE_OPERAND (decl, 0);
7876 decl = TREE_OPERAND (decl, 0);
7878 if (TREE_CODE (decl) == COMPONENT_REF)
7880 while (TREE_CODE (decl) == COMPONENT_REF)
7881 decl = TREE_OPERAND (decl, 0);
7882 if (TREE_CODE (decl) == INDIRECT_REF
7883 && DECL_P (TREE_OPERAND (decl, 0))
7884 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7885 == REFERENCE_TYPE))
7886 decl = TREE_OPERAND (decl, 0);
7888 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
7889 == GS_ERROR)
7891 remove = true;
7892 break;
7894 if (DECL_P (decl))
7896 if (error_operand_p (decl))
7898 remove = true;
7899 break;
7902 tree stype = TREE_TYPE (decl);
7903 if (TREE_CODE (stype) == REFERENCE_TYPE)
7904 stype = TREE_TYPE (stype);
7905 if (TYPE_SIZE_UNIT (stype) == NULL
7906 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
7908 error_at (OMP_CLAUSE_LOCATION (c),
7909 "mapping field %qE of variable length "
7910 "structure", OMP_CLAUSE_DECL (c));
7911 remove = true;
7912 break;
7915 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7917 /* Error recovery. */
7918 if (prev_list_p == NULL)
7920 remove = true;
7921 break;
7923 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7925 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
7926 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
7928 remove = true;
7929 break;
7934 tree offset;
7935 poly_int64 bitsize, bitpos;
7936 machine_mode mode;
7937 int unsignedp, reversep, volatilep = 0;
7938 tree base = OMP_CLAUSE_DECL (c);
7939 while (TREE_CODE (base) == ARRAY_REF)
7940 base = TREE_OPERAND (base, 0);
7941 if (TREE_CODE (base) == INDIRECT_REF)
7942 base = TREE_OPERAND (base, 0);
7943 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7944 &mode, &unsignedp, &reversep,
7945 &volatilep);
7946 tree orig_base = base;
7947 if ((TREE_CODE (base) == INDIRECT_REF
7948 || (TREE_CODE (base) == MEM_REF
7949 && integer_zerop (TREE_OPERAND (base, 1))))
7950 && DECL_P (TREE_OPERAND (base, 0))
7951 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
7952 == REFERENCE_TYPE))
7953 base = TREE_OPERAND (base, 0);
7954 gcc_assert (base == decl
7955 && (offset == NULL_TREE
7956 || poly_int_tree_p (offset)));
7958 splay_tree_node n
7959 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7960 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
7961 == GOMP_MAP_ALWAYS_POINTER);
7962 if (n == NULL || (n->value & GOVD_MAP) == 0)
7964 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7965 OMP_CLAUSE_MAP);
7966 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
7967 if (orig_base != base)
7968 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
7969 else
7970 OMP_CLAUSE_DECL (l) = decl;
7971 OMP_CLAUSE_SIZE (l) = size_int (1);
7972 if (struct_map_to_clause == NULL)
7973 struct_map_to_clause = new hash_map<tree, tree>;
7974 struct_map_to_clause->put (decl, l);
7975 if (ptr)
7977 enum gomp_map_kind mkind
7978 = code == OMP_TARGET_EXIT_DATA
7979 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
7980 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7981 OMP_CLAUSE_MAP);
7982 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
7983 OMP_CLAUSE_DECL (c2)
7984 = unshare_expr (OMP_CLAUSE_DECL (c));
7985 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
7986 OMP_CLAUSE_SIZE (c2)
7987 = TYPE_SIZE_UNIT (ptr_type_node);
7988 OMP_CLAUSE_CHAIN (l) = c2;
7989 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7991 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
7992 tree c3
7993 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7994 OMP_CLAUSE_MAP);
7995 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
7996 OMP_CLAUSE_DECL (c3)
7997 = unshare_expr (OMP_CLAUSE_DECL (c4));
7998 OMP_CLAUSE_SIZE (c3)
7999 = TYPE_SIZE_UNIT (ptr_type_node);
8000 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8001 OMP_CLAUSE_CHAIN (c2) = c3;
8003 *prev_list_p = l;
8004 prev_list_p = NULL;
8006 else
8008 OMP_CLAUSE_CHAIN (l) = c;
8009 *list_p = l;
8010 list_p = &OMP_CLAUSE_CHAIN (l);
8012 if (orig_base != base && code == OMP_TARGET)
8014 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8015 OMP_CLAUSE_MAP);
8016 enum gomp_map_kind mkind
8017 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8018 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8019 OMP_CLAUSE_DECL (c2) = decl;
8020 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8021 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8022 OMP_CLAUSE_CHAIN (l) = c2;
8024 flags = GOVD_MAP | GOVD_EXPLICIT;
8025 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8026 flags |= GOVD_SEEN;
8027 goto do_add_decl;
8029 else
8031 tree *osc = struct_map_to_clause->get (decl);
8032 tree *sc = NULL, *scp = NULL;
8033 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8034 n->value |= GOVD_SEEN;
8035 poly_offset_int o1, o2;
8036 if (offset)
8037 o1 = wi::to_poly_offset (offset);
8038 else
8039 o1 = 0;
8040 if (maybe_ne (bitpos, 0))
8041 o1 += bits_to_bytes_round_down (bitpos);
8042 sc = &OMP_CLAUSE_CHAIN (*osc);
8043 if (*sc != c
8044 && (OMP_CLAUSE_MAP_KIND (*sc)
8045 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8046 sc = &OMP_CLAUSE_CHAIN (*sc);
8047 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8048 if (ptr && sc == prev_list_p)
8049 break;
8050 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8051 != COMPONENT_REF
8052 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8053 != INDIRECT_REF)
8054 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8055 != ARRAY_REF))
8056 break;
8057 else
8059 tree offset2;
8060 poly_int64 bitsize2, bitpos2;
8061 base = OMP_CLAUSE_DECL (*sc);
8062 if (TREE_CODE (base) == ARRAY_REF)
8064 while (TREE_CODE (base) == ARRAY_REF)
8065 base = TREE_OPERAND (base, 0);
8066 if (TREE_CODE (base) != COMPONENT_REF
8067 || (TREE_CODE (TREE_TYPE (base))
8068 != ARRAY_TYPE))
8069 break;
8071 else if (TREE_CODE (base) == INDIRECT_REF
8072 && (TREE_CODE (TREE_OPERAND (base, 0))
8073 == COMPONENT_REF)
8074 && (TREE_CODE (TREE_TYPE
8075 (TREE_OPERAND (base, 0)))
8076 == REFERENCE_TYPE))
8077 base = TREE_OPERAND (base, 0);
8078 base = get_inner_reference (base, &bitsize2,
8079 &bitpos2, &offset2,
8080 &mode, &unsignedp,
8081 &reversep, &volatilep);
8082 if ((TREE_CODE (base) == INDIRECT_REF
8083 || (TREE_CODE (base) == MEM_REF
8084 && integer_zerop (TREE_OPERAND (base,
8085 1))))
8086 && DECL_P (TREE_OPERAND (base, 0))
8087 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8088 0)))
8089 == REFERENCE_TYPE))
8090 base = TREE_OPERAND (base, 0);
8091 if (base != decl)
8092 break;
8093 if (scp)
8094 continue;
8095 gcc_assert (offset == NULL_TREE
8096 || poly_int_tree_p (offset));
8097 tree d1 = OMP_CLAUSE_DECL (*sc);
8098 tree d2 = OMP_CLAUSE_DECL (c);
8099 while (TREE_CODE (d1) == ARRAY_REF)
8100 d1 = TREE_OPERAND (d1, 0);
8101 while (TREE_CODE (d2) == ARRAY_REF)
8102 d2 = TREE_OPERAND (d2, 0);
8103 if (TREE_CODE (d1) == INDIRECT_REF)
8104 d1 = TREE_OPERAND (d1, 0);
8105 if (TREE_CODE (d2) == INDIRECT_REF)
8106 d2 = TREE_OPERAND (d2, 0);
8107 while (TREE_CODE (d1) == COMPONENT_REF)
8108 if (TREE_CODE (d2) == COMPONENT_REF
8109 && TREE_OPERAND (d1, 1)
8110 == TREE_OPERAND (d2, 1))
8112 d1 = TREE_OPERAND (d1, 0);
8113 d2 = TREE_OPERAND (d2, 0);
8115 else
8116 break;
8117 if (d1 == d2)
8119 error_at (OMP_CLAUSE_LOCATION (c),
8120 "%qE appears more than once in map "
8121 "clauses", OMP_CLAUSE_DECL (c));
8122 remove = true;
8123 break;
8125 if (offset2)
8126 o2 = wi::to_poly_offset (offset2);
8127 else
8128 o2 = 0;
8129 o2 += bits_to_bytes_round_down (bitpos2);
8130 if (maybe_lt (o1, o2)
8131 || (known_eq (o1, 2)
8132 && maybe_lt (bitpos, bitpos2)))
8134 if (ptr)
8135 scp = sc;
8136 else
8137 break;
8140 if (remove)
8141 break;
8142 OMP_CLAUSE_SIZE (*osc)
8143 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8144 size_one_node);
8145 if (ptr)
8147 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8148 OMP_CLAUSE_MAP);
8149 tree cl = NULL_TREE;
8150 enum gomp_map_kind mkind
8151 = code == OMP_TARGET_EXIT_DATA
8152 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8153 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8154 OMP_CLAUSE_DECL (c2)
8155 = unshare_expr (OMP_CLAUSE_DECL (c));
8156 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8157 OMP_CLAUSE_SIZE (c2)
8158 = TYPE_SIZE_UNIT (ptr_type_node);
8159 cl = scp ? *prev_list_p : c2;
8160 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8162 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8163 tree c3
8164 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8165 OMP_CLAUSE_MAP);
8166 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8167 OMP_CLAUSE_DECL (c3)
8168 = unshare_expr (OMP_CLAUSE_DECL (c4));
8169 OMP_CLAUSE_SIZE (c3)
8170 = TYPE_SIZE_UNIT (ptr_type_node);
8171 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8172 if (!scp)
8173 OMP_CLAUSE_CHAIN (c2) = c3;
8174 else
8175 cl = c3;
8177 if (scp)
8178 *scp = c2;
8179 if (sc == prev_list_p)
8181 *sc = cl;
8182 prev_list_p = NULL;
8184 else
8186 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8187 list_p = prev_list_p;
8188 prev_list_p = NULL;
8189 OMP_CLAUSE_CHAIN (c) = *sc;
8190 *sc = cl;
8191 continue;
8194 else if (*sc != c)
8196 *list_p = OMP_CLAUSE_CHAIN (c);
8197 OMP_CLAUSE_CHAIN (c) = *sc;
8198 *sc = c;
8199 continue;
8203 if (!remove
8204 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8205 && OMP_CLAUSE_CHAIN (c)
8206 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8207 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8208 == GOMP_MAP_ALWAYS_POINTER))
8209 prev_list_p = list_p;
8210 break;
8212 flags = GOVD_MAP | GOVD_EXPLICIT;
8213 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8214 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8215 flags |= GOVD_MAP_ALWAYS_TO;
8216 goto do_add;
8218 case OMP_CLAUSE_DEPEND:
8219 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8221 tree deps = OMP_CLAUSE_DECL (c);
8222 while (deps && TREE_CODE (deps) == TREE_LIST)
8224 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8225 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8226 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8227 pre_p, NULL, is_gimple_val, fb_rvalue);
8228 deps = TREE_CHAIN (deps);
8230 break;
8232 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8233 break;
8234 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8236 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8237 NULL, is_gimple_val, fb_rvalue);
8238 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8240 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8242 remove = true;
8243 break;
8245 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8246 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8247 is_gimple_val, fb_rvalue) == GS_ERROR)
8249 remove = true;
8250 break;
8252 break;
8254 case OMP_CLAUSE_TO:
8255 case OMP_CLAUSE_FROM:
8256 case OMP_CLAUSE__CACHE_:
8257 decl = OMP_CLAUSE_DECL (c);
8258 if (error_operand_p (decl))
8260 remove = true;
8261 break;
8263 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8264 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8265 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8266 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8267 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8269 remove = true;
8270 break;
8272 if (!DECL_P (decl))
8274 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8275 NULL, is_gimple_lvalue, fb_lvalue)
8276 == GS_ERROR)
8278 remove = true;
8279 break;
8281 break;
8283 goto do_notice;
8285 case OMP_CLAUSE_USE_DEVICE_PTR:
8286 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8287 goto do_add;
8288 case OMP_CLAUSE_IS_DEVICE_PTR:
8289 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8290 goto do_add;
8292 do_add:
8293 decl = OMP_CLAUSE_DECL (c);
8294 do_add_decl:
8295 if (error_operand_p (decl))
8297 remove = true;
8298 break;
8300 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8302 tree t = omp_member_access_dummy_var (decl);
8303 if (t)
8305 tree v = DECL_VALUE_EXPR (decl);
8306 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8307 if (outer_ctx)
8308 omp_notice_variable (outer_ctx, t, true);
8311 if (code == OACC_DATA
8312 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8313 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8314 flags |= GOVD_MAP_0LEN_ARRAY;
8315 omp_add_variable (ctx, decl, flags);
8316 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8317 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8319 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8320 GOVD_LOCAL | GOVD_SEEN);
8321 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8322 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8323 find_decl_expr,
8324 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8325 NULL) == NULL_TREE)
8326 omp_add_variable (ctx,
8327 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8328 GOVD_LOCAL | GOVD_SEEN);
8329 gimplify_omp_ctxp = ctx;
8330 push_gimplify_context ();
8332 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8333 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8335 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8336 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8337 pop_gimplify_context
8338 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8339 push_gimplify_context ();
8340 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8341 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8342 pop_gimplify_context
8343 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8344 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8345 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8347 gimplify_omp_ctxp = outer_ctx;
8349 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8350 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8352 gimplify_omp_ctxp = ctx;
8353 push_gimplify_context ();
8354 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8356 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8357 NULL, NULL);
8358 TREE_SIDE_EFFECTS (bind) = 1;
8359 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8360 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8362 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8363 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8364 pop_gimplify_context
8365 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8366 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8368 gimplify_omp_ctxp = outer_ctx;
8370 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8371 && OMP_CLAUSE_LINEAR_STMT (c))
8373 gimplify_omp_ctxp = ctx;
8374 push_gimplify_context ();
8375 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8377 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8378 NULL, NULL);
8379 TREE_SIDE_EFFECTS (bind) = 1;
8380 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8381 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8383 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8384 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8385 pop_gimplify_context
8386 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8387 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8389 gimplify_omp_ctxp = outer_ctx;
8391 if (notice_outer)
8392 goto do_notice;
8393 break;
8395 case OMP_CLAUSE_COPYIN:
8396 case OMP_CLAUSE_COPYPRIVATE:
8397 decl = OMP_CLAUSE_DECL (c);
8398 if (error_operand_p (decl))
8400 remove = true;
8401 break;
8403 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8404 && !remove
8405 && !omp_check_private (ctx, decl, true))
8407 remove = true;
8408 if (is_global_var (decl))
8410 if (DECL_THREAD_LOCAL_P (decl))
8411 remove = false;
8412 else if (DECL_HAS_VALUE_EXPR_P (decl))
8414 tree value = get_base_address (DECL_VALUE_EXPR (decl));
8416 if (value
8417 && DECL_P (value)
8418 && DECL_THREAD_LOCAL_P (value))
8419 remove = false;
8422 if (remove)
8423 error_at (OMP_CLAUSE_LOCATION (c),
8424 "copyprivate variable %qE is not threadprivate"
8425 " or private in outer context", DECL_NAME (decl));
8427 do_notice:
8428 if (outer_ctx)
8429 omp_notice_variable (outer_ctx, decl, true);
8430 if (check_non_private
8431 && region_type == ORT_WORKSHARE
8432 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8433 || decl == OMP_CLAUSE_DECL (c)
8434 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
8435 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8436 == ADDR_EXPR
8437 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8438 == POINTER_PLUS_EXPR
8439 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8440 (OMP_CLAUSE_DECL (c), 0), 0))
8441 == ADDR_EXPR)))))
8442 && omp_check_private (ctx, decl, false))
8444 error ("%s variable %qE is private in outer context",
8445 check_non_private, DECL_NAME (decl));
8446 remove = true;
8448 break;
8450 case OMP_CLAUSE_IF:
8451 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
8452 && OMP_CLAUSE_IF_MODIFIER (c) != code)
8454 const char *p[2];
8455 for (int i = 0; i < 2; i++)
8456 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
8458 case OMP_PARALLEL: p[i] = "parallel"; break;
8459 case OMP_TASK: p[i] = "task"; break;
8460 case OMP_TASKLOOP: p[i] = "taskloop"; break;
8461 case OMP_TARGET_DATA: p[i] = "target data"; break;
8462 case OMP_TARGET: p[i] = "target"; break;
8463 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
8464 case OMP_TARGET_ENTER_DATA:
8465 p[i] = "target enter data"; break;
8466 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
8467 default: gcc_unreachable ();
8469 error_at (OMP_CLAUSE_LOCATION (c),
8470 "expected %qs %<if%> clause modifier rather than %qs",
8471 p[0], p[1]);
8472 remove = true;
8474 /* Fall through. */
8476 case OMP_CLAUSE_FINAL:
8477 OMP_CLAUSE_OPERAND (c, 0)
8478 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
8479 /* Fall through. */
8481 case OMP_CLAUSE_SCHEDULE:
8482 case OMP_CLAUSE_NUM_THREADS:
8483 case OMP_CLAUSE_NUM_TEAMS:
8484 case OMP_CLAUSE_THREAD_LIMIT:
8485 case OMP_CLAUSE_DIST_SCHEDULE:
8486 case OMP_CLAUSE_DEVICE:
8487 case OMP_CLAUSE_PRIORITY:
8488 case OMP_CLAUSE_GRAINSIZE:
8489 case OMP_CLAUSE_NUM_TASKS:
8490 case OMP_CLAUSE_HINT:
8491 case OMP_CLAUSE_ASYNC:
8492 case OMP_CLAUSE_WAIT:
8493 case OMP_CLAUSE_NUM_GANGS:
8494 case OMP_CLAUSE_NUM_WORKERS:
8495 case OMP_CLAUSE_VECTOR_LENGTH:
8496 case OMP_CLAUSE_WORKER:
8497 case OMP_CLAUSE_VECTOR:
8498 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8499 is_gimple_val, fb_rvalue) == GS_ERROR)
8500 remove = true;
8501 break;
8503 case OMP_CLAUSE_GANG:
8504 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8505 is_gimple_val, fb_rvalue) == GS_ERROR)
8506 remove = true;
8507 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
8508 is_gimple_val, fb_rvalue) == GS_ERROR)
8509 remove = true;
8510 break;
8512 case OMP_CLAUSE_NOWAIT:
8513 case OMP_CLAUSE_ORDERED:
8514 case OMP_CLAUSE_UNTIED:
8515 case OMP_CLAUSE_COLLAPSE:
8516 case OMP_CLAUSE_TILE:
8517 case OMP_CLAUSE_AUTO:
8518 case OMP_CLAUSE_SEQ:
8519 case OMP_CLAUSE_INDEPENDENT:
8520 case OMP_CLAUSE_MERGEABLE:
8521 case OMP_CLAUSE_PROC_BIND:
8522 case OMP_CLAUSE_SAFELEN:
8523 case OMP_CLAUSE_SIMDLEN:
8524 case OMP_CLAUSE_NOGROUP:
8525 case OMP_CLAUSE_THREADS:
8526 case OMP_CLAUSE_SIMD:
8527 case OMP_CLAUSE_IF_PRESENT:
8528 case OMP_CLAUSE_FINALIZE:
8529 break;
8531 case OMP_CLAUSE_DEFAULTMAP:
8532 ctx->target_map_scalars_firstprivate = false;
8533 break;
8535 case OMP_CLAUSE_ALIGNED:
8536 decl = OMP_CLAUSE_DECL (c);
8537 if (error_operand_p (decl))
8539 remove = true;
8540 break;
8542 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
8543 is_gimple_val, fb_rvalue) == GS_ERROR)
8545 remove = true;
8546 break;
8548 if (!is_global_var (decl)
8549 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8550 omp_add_variable (ctx, decl, GOVD_ALIGNED);
8551 break;
8553 case OMP_CLAUSE_DEFAULT:
8554 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
8555 break;
8557 default:
8558 gcc_unreachable ();
8561 if (code == OACC_DATA
8562 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8563 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8564 remove = true;
8565 if (remove)
8566 *list_p = OMP_CLAUSE_CHAIN (c);
8567 else
8568 list_p = &OMP_CLAUSE_CHAIN (c);
8571 gimplify_omp_ctxp = ctx;
8572 if (struct_map_to_clause)
8573 delete struct_map_to_clause;
8576 /* Return true if DECL is a candidate for shared to firstprivate
8577 optimization. We only consider non-addressable scalars, not
8578 too big, and not references. */
8580 static bool
8581 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
8583 if (TREE_ADDRESSABLE (decl))
8584 return false;
8585 tree type = TREE_TYPE (decl);
8586 if (!is_gimple_reg_type (type)
8587 || TREE_CODE (type) == REFERENCE_TYPE
8588 || TREE_ADDRESSABLE (type))
8589 return false;
8590 /* Don't optimize too large decls, as each thread/task will have
8591 its own. */
8592 HOST_WIDE_INT len = int_size_in_bytes (type);
8593 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
8594 return false;
8595 if (lang_hooks.decls.omp_privatize_by_reference (decl))
8596 return false;
8597 return true;
8600 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8601 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8602 GOVD_WRITTEN in outer contexts. */
8604 static void
8605 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
8607 for (; ctx; ctx = ctx->outer_context)
8609 splay_tree_node n = splay_tree_lookup (ctx->variables,
8610 (splay_tree_key) decl);
8611 if (n == NULL)
8612 continue;
8613 else if (n->value & GOVD_SHARED)
8615 n->value |= GOVD_WRITTEN;
8616 return;
8618 else if (n->value & GOVD_DATA_SHARE_CLASS)
8619 return;
8623 /* Helper callback for walk_gimple_seq to discover possible stores
8624 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8625 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8626 for those. */
8628 static tree
8629 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
8631 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
8633 *walk_subtrees = 0;
8634 if (!wi->is_lhs)
8635 return NULL_TREE;
8637 tree op = *tp;
8640 if (handled_component_p (op))
8641 op = TREE_OPERAND (op, 0);
8642 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
8643 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
8644 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8645 else
8646 break;
8648 while (1);
8649 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
8650 return NULL_TREE;
8652 omp_mark_stores (gimplify_omp_ctxp, op);
8653 return NULL_TREE;
8656 /* Helper callback for walk_gimple_seq to discover possible stores
8657 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8658 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8659 for those. */
8661 static tree
8662 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
8663 bool *handled_ops_p,
8664 struct walk_stmt_info *wi)
8666 gimple *stmt = gsi_stmt (*gsi_p);
8667 switch (gimple_code (stmt))
8669 /* Don't recurse on OpenMP constructs for which
8670 gimplify_adjust_omp_clauses already handled the bodies,
8671 except handle gimple_omp_for_pre_body. */
8672 case GIMPLE_OMP_FOR:
8673 *handled_ops_p = true;
8674 if (gimple_omp_for_pre_body (stmt))
8675 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
8676 omp_find_stores_stmt, omp_find_stores_op, wi);
8677 break;
8678 case GIMPLE_OMP_PARALLEL:
8679 case GIMPLE_OMP_TASK:
8680 case GIMPLE_OMP_SECTIONS:
8681 case GIMPLE_OMP_SINGLE:
8682 case GIMPLE_OMP_TARGET:
8683 case GIMPLE_OMP_TEAMS:
8684 case GIMPLE_OMP_CRITICAL:
8685 *handled_ops_p = true;
8686 break;
8687 default:
8688 break;
8690 return NULL_TREE;
8693 struct gimplify_adjust_omp_clauses_data
8695 tree *list_p;
8696 gimple_seq *pre_p;
8699 /* For all variables that were not actually used within the context,
8700 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
8702 static int
8703 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
8705 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
8706 gimple_seq *pre_p
8707 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
8708 tree decl = (tree) n->key;
8709 unsigned flags = n->value;
8710 enum omp_clause_code code;
8711 tree clause;
8712 bool private_debug;
8714 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
8715 return 0;
8716 if ((flags & GOVD_SEEN) == 0)
8717 return 0;
8718 if (flags & GOVD_DEBUG_PRIVATE)
8720 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
8721 private_debug = true;
8723 else if (flags & GOVD_MAP)
8724 private_debug = false;
8725 else
8726 private_debug
8727 = lang_hooks.decls.omp_private_debug_clause (decl,
8728 !!(flags & GOVD_SHARED));
8729 if (private_debug)
8730 code = OMP_CLAUSE_PRIVATE;
8731 else if (flags & GOVD_MAP)
8733 code = OMP_CLAUSE_MAP;
8734 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8735 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8737 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
8738 return 0;
8741 else if (flags & GOVD_SHARED)
8743 if (is_global_var (decl))
8745 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8746 while (ctx != NULL)
8748 splay_tree_node on
8749 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8750 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
8751 | GOVD_PRIVATE | GOVD_REDUCTION
8752 | GOVD_LINEAR | GOVD_MAP)) != 0)
8753 break;
8754 ctx = ctx->outer_context;
8756 if (ctx == NULL)
8757 return 0;
8759 code = OMP_CLAUSE_SHARED;
8761 else if (flags & GOVD_PRIVATE)
8762 code = OMP_CLAUSE_PRIVATE;
8763 else if (flags & GOVD_FIRSTPRIVATE)
8765 code = OMP_CLAUSE_FIRSTPRIVATE;
8766 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
8767 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8768 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8770 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8771 "%<target%> construct", decl);
8772 return 0;
8775 else if (flags & GOVD_LASTPRIVATE)
8776 code = OMP_CLAUSE_LASTPRIVATE;
8777 else if (flags & GOVD_ALIGNED)
8778 return 0;
8779 else
8780 gcc_unreachable ();
8782 if (((flags & GOVD_LASTPRIVATE)
8783 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
8784 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8785 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8787 tree chain = *list_p;
8788 clause = build_omp_clause (input_location, code);
8789 OMP_CLAUSE_DECL (clause) = decl;
8790 OMP_CLAUSE_CHAIN (clause) = chain;
8791 if (private_debug)
8792 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
8793 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
8794 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
8795 else if (code == OMP_CLAUSE_SHARED
8796 && (flags & GOVD_WRITTEN) == 0
8797 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8798 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
8799 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
8800 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
8801 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
8803 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
8804 OMP_CLAUSE_DECL (nc) = decl;
8805 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
8806 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
8807 OMP_CLAUSE_DECL (clause)
8808 = build_simple_mem_ref_loc (input_location, decl);
8809 OMP_CLAUSE_DECL (clause)
8810 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
8811 build_int_cst (build_pointer_type (char_type_node), 0));
8812 OMP_CLAUSE_SIZE (clause) = size_zero_node;
8813 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8814 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
8815 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
8816 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8817 OMP_CLAUSE_CHAIN (nc) = chain;
8818 OMP_CLAUSE_CHAIN (clause) = nc;
8819 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8820 gimplify_omp_ctxp = ctx->outer_context;
8821 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
8822 pre_p, NULL, is_gimple_val, fb_rvalue);
8823 gimplify_omp_ctxp = ctx;
8825 else if (code == OMP_CLAUSE_MAP)
8827 int kind;
8828 /* Not all combinations of these GOVD_MAP flags are actually valid. */
8829 switch (flags & (GOVD_MAP_TO_ONLY
8830 | GOVD_MAP_FORCE
8831 | GOVD_MAP_FORCE_PRESENT))
8833 case 0:
8834 kind = GOMP_MAP_TOFROM;
8835 break;
8836 case GOVD_MAP_FORCE:
8837 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
8838 break;
8839 case GOVD_MAP_TO_ONLY:
8840 kind = GOMP_MAP_TO;
8841 break;
8842 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
8843 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
8844 break;
8845 case GOVD_MAP_FORCE_PRESENT:
8846 kind = GOMP_MAP_FORCE_PRESENT;
8847 break;
8848 default:
8849 gcc_unreachable ();
8851 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
8852 if (DECL_SIZE (decl)
8853 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8855 tree decl2 = DECL_VALUE_EXPR (decl);
8856 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8857 decl2 = TREE_OPERAND (decl2, 0);
8858 gcc_assert (DECL_P (decl2));
8859 tree mem = build_simple_mem_ref (decl2);
8860 OMP_CLAUSE_DECL (clause) = mem;
8861 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8862 if (gimplify_omp_ctxp->outer_context)
8864 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8865 omp_notice_variable (ctx, decl2, true);
8866 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
8868 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8869 OMP_CLAUSE_MAP);
8870 OMP_CLAUSE_DECL (nc) = decl;
8871 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8872 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
8873 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8874 else
8875 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8876 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8877 OMP_CLAUSE_CHAIN (clause) = nc;
8879 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
8880 && lang_hooks.decls.omp_privatize_by_reference (decl))
8882 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
8883 OMP_CLAUSE_SIZE (clause)
8884 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
8885 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8886 gimplify_omp_ctxp = ctx->outer_context;
8887 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
8888 pre_p, NULL, is_gimple_val, fb_rvalue);
8889 gimplify_omp_ctxp = ctx;
8890 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8891 OMP_CLAUSE_MAP);
8892 OMP_CLAUSE_DECL (nc) = decl;
8893 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8894 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8895 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8896 OMP_CLAUSE_CHAIN (clause) = nc;
8898 else
8899 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
8901 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
8903 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
8904 OMP_CLAUSE_DECL (nc) = decl;
8905 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
8906 OMP_CLAUSE_CHAIN (nc) = chain;
8907 OMP_CLAUSE_CHAIN (clause) = nc;
8908 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8909 gimplify_omp_ctxp = ctx->outer_context;
8910 lang_hooks.decls.omp_finish_clause (nc, pre_p);
8911 gimplify_omp_ctxp = ctx;
8913 *list_p = clause;
8914 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8915 gimplify_omp_ctxp = ctx->outer_context;
8916 lang_hooks.decls.omp_finish_clause (clause, pre_p);
8917 if (gimplify_omp_ctxp)
8918 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
8919 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
8920 && DECL_P (OMP_CLAUSE_SIZE (clause)))
8921 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
8922 true);
8923 gimplify_omp_ctxp = ctx;
8924 return 0;
8927 static void
8928 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
8929 enum tree_code code)
8931 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8932 tree c, decl;
8934 if (body)
8936 struct gimplify_omp_ctx *octx;
8937 for (octx = ctx; octx; octx = octx->outer_context)
8938 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
8939 break;
8940 if (octx)
8942 struct walk_stmt_info wi;
8943 memset (&wi, 0, sizeof (wi));
8944 walk_gimple_seq (body, omp_find_stores_stmt,
8945 omp_find_stores_op, &wi);
8948 while ((c = *list_p) != NULL)
8950 splay_tree_node n;
8951 bool remove = false;
8953 switch (OMP_CLAUSE_CODE (c))
8955 case OMP_CLAUSE_FIRSTPRIVATE:
8956 if ((ctx->region_type & ORT_TARGET)
8957 && (ctx->region_type & ORT_ACC) == 0
8958 && TYPE_ATOMIC (strip_array_types
8959 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
8961 error_at (OMP_CLAUSE_LOCATION (c),
8962 "%<_Atomic%> %qD in %<firstprivate%> clause on "
8963 "%<target%> construct", OMP_CLAUSE_DECL (c));
8964 remove = true;
8965 break;
8967 /* FALLTHRU */
8968 case OMP_CLAUSE_PRIVATE:
8969 case OMP_CLAUSE_SHARED:
8970 case OMP_CLAUSE_LINEAR:
8971 decl = OMP_CLAUSE_DECL (c);
8972 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8973 remove = !(n->value & GOVD_SEEN);
8974 if (! remove)
8976 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
8977 if ((n->value & GOVD_DEBUG_PRIVATE)
8978 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
8980 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
8981 || ((n->value & GOVD_DATA_SHARE_CLASS)
8982 == GOVD_SHARED));
8983 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
8984 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
8986 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8987 && (n->value & GOVD_WRITTEN) == 0
8988 && DECL_P (decl)
8989 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8990 OMP_CLAUSE_SHARED_READONLY (c) = 1;
8991 else if (DECL_P (decl)
8992 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8993 && (n->value & GOVD_WRITTEN) != 0)
8994 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8995 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
8996 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8997 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8999 break;
9001 case OMP_CLAUSE_LASTPRIVATE:
9002 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9003 accurately reflect the presence of a FIRSTPRIVATE clause. */
9004 decl = OMP_CLAUSE_DECL (c);
9005 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9006 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9007 = (n->value & GOVD_FIRSTPRIVATE) != 0;
9008 if (code == OMP_DISTRIBUTE
9009 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9011 remove = true;
9012 error_at (OMP_CLAUSE_LOCATION (c),
9013 "same variable used in %<firstprivate%> and "
9014 "%<lastprivate%> clauses on %<distribute%> "
9015 "construct");
9017 if (!remove
9018 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9019 && DECL_P (decl)
9020 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9021 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9022 break;
9024 case OMP_CLAUSE_ALIGNED:
9025 decl = OMP_CLAUSE_DECL (c);
9026 if (!is_global_var (decl))
9028 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9029 remove = n == NULL || !(n->value & GOVD_SEEN);
9030 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9032 struct gimplify_omp_ctx *octx;
9033 if (n != NULL
9034 && (n->value & (GOVD_DATA_SHARE_CLASS
9035 & ~GOVD_FIRSTPRIVATE)))
9036 remove = true;
9037 else
9038 for (octx = ctx->outer_context; octx;
9039 octx = octx->outer_context)
9041 n = splay_tree_lookup (octx->variables,
9042 (splay_tree_key) decl);
9043 if (n == NULL)
9044 continue;
9045 if (n->value & GOVD_LOCAL)
9046 break;
9047 /* We have to avoid assigning a shared variable
9048 to itself when trying to add
9049 __builtin_assume_aligned. */
9050 if (n->value & GOVD_SHARED)
9052 remove = true;
9053 break;
9058 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9060 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9061 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9062 remove = true;
9064 break;
9066 case OMP_CLAUSE_MAP:
9067 if (code == OMP_TARGET_EXIT_DATA
9068 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9070 remove = true;
9071 break;
9073 decl = OMP_CLAUSE_DECL (c);
9074 /* Data clauses associated with acc parallel reductions must be
9075 compatible with present_or_copy. Warn and adjust the clause
9076 if that is not the case. */
9077 if (ctx->region_type == ORT_ACC_PARALLEL)
9079 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9080 n = NULL;
9082 if (DECL_P (t))
9083 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9085 if (n && (n->value & GOVD_REDUCTION))
9087 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9089 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9090 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9091 && kind != GOMP_MAP_FORCE_PRESENT
9092 && kind != GOMP_MAP_POINTER)
9094 warning_at (OMP_CLAUSE_LOCATION (c), 0,
9095 "incompatible data clause with reduction "
9096 "on %qE; promoting to present_or_copy",
9097 DECL_NAME (t));
9098 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9102 if (!DECL_P (decl))
9104 if ((ctx->region_type & ORT_TARGET) != 0
9105 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9107 if (TREE_CODE (decl) == INDIRECT_REF
9108 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
9109 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
9110 == REFERENCE_TYPE))
9111 decl = TREE_OPERAND (decl, 0);
9112 if (TREE_CODE (decl) == COMPONENT_REF)
9114 while (TREE_CODE (decl) == COMPONENT_REF)
9115 decl = TREE_OPERAND (decl, 0);
9116 if (DECL_P (decl))
9118 n = splay_tree_lookup (ctx->variables,
9119 (splay_tree_key) decl);
9120 if (!(n->value & GOVD_SEEN))
9121 remove = true;
9125 break;
9127 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9128 if ((ctx->region_type & ORT_TARGET) != 0
9129 && !(n->value & GOVD_SEEN)
9130 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
9131 && (!is_global_var (decl)
9132 || !lookup_attribute ("omp declare target link",
9133 DECL_ATTRIBUTES (decl))))
9135 remove = true;
9136 /* For struct element mapping, if struct is never referenced
9137 in target block and none of the mapping has always modifier,
9138 remove all the struct element mappings, which immediately
9139 follow the GOMP_MAP_STRUCT map clause. */
9140 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
9142 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
9143 while (cnt--)
9144 OMP_CLAUSE_CHAIN (c)
9145 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
9148 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
9149 && code == OMP_TARGET_EXIT_DATA)
9150 remove = true;
9151 else if (DECL_SIZE (decl)
9152 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
9153 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
9154 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9155 && (OMP_CLAUSE_MAP_KIND (c)
9156 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9158 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9159 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9160 INTEGER_CST. */
9161 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
9163 tree decl2 = DECL_VALUE_EXPR (decl);
9164 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9165 decl2 = TREE_OPERAND (decl2, 0);
9166 gcc_assert (DECL_P (decl2));
9167 tree mem = build_simple_mem_ref (decl2);
9168 OMP_CLAUSE_DECL (c) = mem;
9169 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9170 if (ctx->outer_context)
9172 omp_notice_variable (ctx->outer_context, decl2, true);
9173 omp_notice_variable (ctx->outer_context,
9174 OMP_CLAUSE_SIZE (c), true);
9176 if (((ctx->region_type & ORT_TARGET) != 0
9177 || !ctx->target_firstprivatize_array_bases)
9178 && ((n->value & GOVD_SEEN) == 0
9179 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
9181 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9182 OMP_CLAUSE_MAP);
9183 OMP_CLAUSE_DECL (nc) = decl;
9184 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9185 if (ctx->target_firstprivatize_array_bases)
9186 OMP_CLAUSE_SET_MAP_KIND (nc,
9187 GOMP_MAP_FIRSTPRIVATE_POINTER);
9188 else
9189 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9190 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9191 OMP_CLAUSE_CHAIN (c) = nc;
9192 c = nc;
9195 else
9197 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9198 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9199 gcc_assert ((n->value & GOVD_SEEN) == 0
9200 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9201 == 0));
9203 break;
9205 case OMP_CLAUSE_TO:
9206 case OMP_CLAUSE_FROM:
9207 case OMP_CLAUSE__CACHE_:
9208 decl = OMP_CLAUSE_DECL (c);
9209 if (!DECL_P (decl))
9210 break;
9211 if (DECL_SIZE (decl)
9212 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9214 tree decl2 = DECL_VALUE_EXPR (decl);
9215 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9216 decl2 = TREE_OPERAND (decl2, 0);
9217 gcc_assert (DECL_P (decl2));
9218 tree mem = build_simple_mem_ref (decl2);
9219 OMP_CLAUSE_DECL (c) = mem;
9220 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9221 if (ctx->outer_context)
9223 omp_notice_variable (ctx->outer_context, decl2, true);
9224 omp_notice_variable (ctx->outer_context,
9225 OMP_CLAUSE_SIZE (c), true);
9228 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9229 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9230 break;
9232 case OMP_CLAUSE_REDUCTION:
9233 decl = OMP_CLAUSE_DECL (c);
9234 /* OpenACC reductions need a present_or_copy data clause.
9235 Add one if necessary. Emit error when the reduction is private. */
9236 if (ctx->region_type == ORT_ACC_PARALLEL)
9238 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9239 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9241 remove = true;
9242 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9243 "reduction on %qE", DECL_NAME (decl));
9245 else if ((n->value & GOVD_MAP) == 0)
9247 tree next = OMP_CLAUSE_CHAIN (c);
9248 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9249 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9250 OMP_CLAUSE_DECL (nc) = decl;
9251 OMP_CLAUSE_CHAIN (c) = nc;
9252 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9253 while (1)
9255 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9256 if (OMP_CLAUSE_CHAIN (nc) == NULL)
9257 break;
9258 nc = OMP_CLAUSE_CHAIN (nc);
9260 OMP_CLAUSE_CHAIN (nc) = next;
9261 n->value |= GOVD_MAP;
9264 if (DECL_P (decl)
9265 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9266 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9267 break;
9268 case OMP_CLAUSE_COPYIN:
9269 case OMP_CLAUSE_COPYPRIVATE:
9270 case OMP_CLAUSE_IF:
9271 case OMP_CLAUSE_NUM_THREADS:
9272 case OMP_CLAUSE_NUM_TEAMS:
9273 case OMP_CLAUSE_THREAD_LIMIT:
9274 case OMP_CLAUSE_DIST_SCHEDULE:
9275 case OMP_CLAUSE_DEVICE:
9276 case OMP_CLAUSE_SCHEDULE:
9277 case OMP_CLAUSE_NOWAIT:
9278 case OMP_CLAUSE_ORDERED:
9279 case OMP_CLAUSE_DEFAULT:
9280 case OMP_CLAUSE_UNTIED:
9281 case OMP_CLAUSE_COLLAPSE:
9282 case OMP_CLAUSE_FINAL:
9283 case OMP_CLAUSE_MERGEABLE:
9284 case OMP_CLAUSE_PROC_BIND:
9285 case OMP_CLAUSE_SAFELEN:
9286 case OMP_CLAUSE_SIMDLEN:
9287 case OMP_CLAUSE_DEPEND:
9288 case OMP_CLAUSE_PRIORITY:
9289 case OMP_CLAUSE_GRAINSIZE:
9290 case OMP_CLAUSE_NUM_TASKS:
9291 case OMP_CLAUSE_NOGROUP:
9292 case OMP_CLAUSE_THREADS:
9293 case OMP_CLAUSE_SIMD:
9294 case OMP_CLAUSE_HINT:
9295 case OMP_CLAUSE_DEFAULTMAP:
9296 case OMP_CLAUSE_USE_DEVICE_PTR:
9297 case OMP_CLAUSE_IS_DEVICE_PTR:
9298 case OMP_CLAUSE_ASYNC:
9299 case OMP_CLAUSE_WAIT:
9300 case OMP_CLAUSE_INDEPENDENT:
9301 case OMP_CLAUSE_NUM_GANGS:
9302 case OMP_CLAUSE_NUM_WORKERS:
9303 case OMP_CLAUSE_VECTOR_LENGTH:
9304 case OMP_CLAUSE_GANG:
9305 case OMP_CLAUSE_WORKER:
9306 case OMP_CLAUSE_VECTOR:
9307 case OMP_CLAUSE_AUTO:
9308 case OMP_CLAUSE_SEQ:
9309 case OMP_CLAUSE_TILE:
9310 case OMP_CLAUSE_IF_PRESENT:
9311 case OMP_CLAUSE_FINALIZE:
9312 break;
9314 default:
9315 gcc_unreachable ();
9318 if (remove)
9319 *list_p = OMP_CLAUSE_CHAIN (c);
9320 else
9321 list_p = &OMP_CLAUSE_CHAIN (c);
9324 /* Add in any implicit data sharing. */
9325 struct gimplify_adjust_omp_clauses_data data;
9326 data.list_p = list_p;
9327 data.pre_p = pre_p;
9328 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
9330 gimplify_omp_ctxp = ctx->outer_context;
9331 delete_omp_context (ctx);
9334 /* Gimplify OACC_CACHE. */
9336 static void
9337 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
9339 tree expr = *expr_p;
9341 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
9342 OACC_CACHE);
9343 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
9344 OACC_CACHE);
9346 /* TODO: Do something sensible with this information. */
9348 *expr_p = NULL_TREE;
9351 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
9352 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
9353 kind. The entry kind will replace the one in CLAUSE, while the exit
9354 kind will be used in a new omp_clause and returned to the caller. */
9356 static tree
9357 gimplify_oacc_declare_1 (tree clause)
9359 HOST_WIDE_INT kind, new_op;
9360 bool ret = false;
9361 tree c = NULL;
9363 kind = OMP_CLAUSE_MAP_KIND (clause);
9365 switch (kind)
9367 case GOMP_MAP_ALLOC:
9368 new_op = GOMP_MAP_RELEASE;
9369 ret = true;
9370 break;
9372 case GOMP_MAP_FROM:
9373 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9374 new_op = GOMP_MAP_FROM;
9375 ret = true;
9376 break;
9378 case GOMP_MAP_TOFROM:
9379 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
9380 new_op = GOMP_MAP_FROM;
9381 ret = true;
9382 break;
9384 case GOMP_MAP_DEVICE_RESIDENT:
9385 case GOMP_MAP_FORCE_DEVICEPTR:
9386 case GOMP_MAP_FORCE_PRESENT:
9387 case GOMP_MAP_LINK:
9388 case GOMP_MAP_POINTER:
9389 case GOMP_MAP_TO:
9390 break;
9392 default:
9393 gcc_unreachable ();
9394 break;
9397 if (ret)
9399 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
9400 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
9401 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
9404 return c;
9407 /* Gimplify OACC_DECLARE. */
9409 static void
9410 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
9412 tree expr = *expr_p;
9413 gomp_target *stmt;
9414 tree clauses, t, decl;
9416 clauses = OACC_DECLARE_CLAUSES (expr);
9418 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
9419 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
9421 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
9423 decl = OMP_CLAUSE_DECL (t);
9425 if (TREE_CODE (decl) == MEM_REF)
9426 decl = TREE_OPERAND (decl, 0);
9428 if (VAR_P (decl) && !is_oacc_declared (decl))
9430 tree attr = get_identifier ("oacc declare target");
9431 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
9432 DECL_ATTRIBUTES (decl));
9435 if (VAR_P (decl)
9436 && !is_global_var (decl)
9437 && DECL_CONTEXT (decl) == current_function_decl)
9439 tree c = gimplify_oacc_declare_1 (t);
9440 if (c)
9442 if (oacc_declare_returns == NULL)
9443 oacc_declare_returns = new hash_map<tree, tree>;
9445 oacc_declare_returns->put (decl, c);
9449 if (gimplify_omp_ctxp)
9450 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
9453 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
9454 clauses);
9456 gimplify_seq_add_stmt (pre_p, stmt);
9458 *expr_p = NULL_TREE;
9461 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
9462 gimplification of the body, as well as scanning the body for used
9463 variables. We need to do this scan now, because variable-sized
9464 decls will be decomposed during gimplification. */
9466 static void
9467 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
9469 tree expr = *expr_p;
9470 gimple *g;
9471 gimple_seq body = NULL;
9473 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
9474 OMP_PARALLEL_COMBINED (expr)
9475 ? ORT_COMBINED_PARALLEL
9476 : ORT_PARALLEL, OMP_PARALLEL);
9478 push_gimplify_context ();
9480 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
9481 if (gimple_code (g) == GIMPLE_BIND)
9482 pop_gimplify_context (g);
9483 else
9484 pop_gimplify_context (NULL);
9486 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
9487 OMP_PARALLEL);
9489 g = gimple_build_omp_parallel (body,
9490 OMP_PARALLEL_CLAUSES (expr),
9491 NULL_TREE, NULL_TREE);
9492 if (OMP_PARALLEL_COMBINED (expr))
9493 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
9494 gimplify_seq_add_stmt (pre_p, g);
9495 *expr_p = NULL_TREE;
9498 /* Gimplify the contents of an OMP_TASK statement. This involves
9499 gimplification of the body, as well as scanning the body for used
9500 variables. We need to do this scan now, because variable-sized
9501 decls will be decomposed during gimplification. */
9503 static void
9504 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
9506 tree expr = *expr_p;
9507 gimple *g;
9508 gimple_seq body = NULL;
9510 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
9511 omp_find_clause (OMP_TASK_CLAUSES (expr),
9512 OMP_CLAUSE_UNTIED)
9513 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
9515 push_gimplify_context ();
9517 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
9518 if (gimple_code (g) == GIMPLE_BIND)
9519 pop_gimplify_context (g);
9520 else
9521 pop_gimplify_context (NULL);
9523 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
9524 OMP_TASK);
9526 g = gimple_build_omp_task (body,
9527 OMP_TASK_CLAUSES (expr),
9528 NULL_TREE, NULL_TREE,
9529 NULL_TREE, NULL_TREE, NULL_TREE);
9530 gimplify_seq_add_stmt (pre_p, g);
9531 *expr_p = NULL_TREE;
9534 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9535 with non-NULL OMP_FOR_INIT. Also, fill in pdata array,
9536 pdata[0] non-NULL if there is anything non-trivial in between, pdata[1]
9537 is address of OMP_PARALLEL in between if any, pdata[2] is address of
9538 OMP_FOR in between if any and pdata[3] is address of the inner
9539 OMP_FOR/OMP_SIMD. */
9541 static tree
9542 find_combined_omp_for (tree *tp, int *walk_subtrees, void *data)
9544 tree **pdata = (tree **) data;
9545 *walk_subtrees = 0;
9546 switch (TREE_CODE (*tp))
9548 case OMP_FOR:
9549 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9551 pdata[3] = tp;
9552 return *tp;
9554 pdata[2] = tp;
9555 *walk_subtrees = 1;
9556 break;
9557 case OMP_SIMD:
9558 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9560 pdata[3] = tp;
9561 return *tp;
9563 break;
9564 case BIND_EXPR:
9565 if (BIND_EXPR_VARS (*tp)
9566 || (BIND_EXPR_BLOCK (*tp)
9567 && BLOCK_VARS (BIND_EXPR_BLOCK (*tp))))
9568 pdata[0] = tp;
9569 *walk_subtrees = 1;
9570 break;
9571 case STATEMENT_LIST:
9572 if (!tsi_one_before_end_p (tsi_start (*tp)))
9573 pdata[0] = tp;
9574 *walk_subtrees = 1;
9575 break;
9576 case TRY_FINALLY_EXPR:
9577 pdata[0] = tp;
9578 *walk_subtrees = 1;
9579 break;
9580 case OMP_PARALLEL:
9581 pdata[1] = tp;
9582 *walk_subtrees = 1;
9583 break;
9584 default:
9585 break;
9587 return NULL_TREE;
9590 /* Gimplify the gross structure of an OMP_FOR statement. */
9592 static enum gimplify_status
9593 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
9595 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
9596 enum gimplify_status ret = GS_ALL_DONE;
9597 enum gimplify_status tret;
9598 gomp_for *gfor;
9599 gimple_seq for_body, for_pre_body;
9600 int i;
9601 bitmap has_decl_expr = NULL;
9602 enum omp_region_type ort = ORT_WORKSHARE;
9604 orig_for_stmt = for_stmt = *expr_p;
9606 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9608 tree *data[4] = { NULL, NULL, NULL, NULL };
9609 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
9610 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
9611 find_combined_omp_for, data, NULL);
9612 if (inner_for_stmt == NULL_TREE)
9614 gcc_assert (seen_error ());
9615 *expr_p = NULL_TREE;
9616 return GS_ERROR;
9618 if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
9620 append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
9621 &OMP_FOR_PRE_BODY (for_stmt));
9622 OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
9624 if (OMP_FOR_PRE_BODY (inner_for_stmt))
9626 append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
9627 &OMP_FOR_PRE_BODY (for_stmt));
9628 OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
9631 if (data[0])
9633 /* We have some statements or variable declarations in between
9634 the composite construct directives. Move them around the
9635 inner_for_stmt. */
9636 data[0] = expr_p;
9637 for (i = 0; i < 3; i++)
9638 if (data[i])
9640 tree t = *data[i];
9641 if (i < 2 && data[i + 1] == &OMP_BODY (t))
9642 data[i + 1] = data[i];
9643 *data[i] = OMP_BODY (t);
9644 tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9645 NULL_TREE, make_node (BLOCK));
9646 OMP_BODY (t) = body;
9647 append_to_statement_list_force (inner_for_stmt,
9648 &BIND_EXPR_BODY (body));
9649 *data[3] = t;
9650 data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
9651 gcc_assert (*data[3] == inner_for_stmt);
9653 return GS_OK;
9656 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
9657 if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
9658 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
9659 i)) == TREE_LIST)
9661 tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
9662 /* Class iterators aren't allowed on OMP_SIMD, so the only
9663 case we need to solve is distribute parallel for. */
9664 gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
9665 && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
9666 && data[1]);
9667 tree orig_decl = TREE_PURPOSE (orig);
9668 tree last = TREE_VALUE (orig);
9669 tree *pc;
9670 for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
9671 *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
9672 if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
9673 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
9674 && OMP_CLAUSE_DECL (*pc) == orig_decl)
9675 break;
9676 if (*pc == NULL_TREE)
9678 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
9680 /* private clause will appear only on inner_for_stmt.
9681 Change it into firstprivate, and add private clause
9682 on for_stmt. */
9683 tree c = copy_node (*pc);
9684 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9685 OMP_FOR_CLAUSES (for_stmt) = c;
9686 OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
9687 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
9689 else
9691 /* lastprivate clause will appear on both inner_for_stmt
9692 and for_stmt. Add firstprivate clause to
9693 inner_for_stmt. */
9694 tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
9695 OMP_CLAUSE_FIRSTPRIVATE);
9696 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
9697 OMP_CLAUSE_CHAIN (c) = *pc;
9698 *pc = c;
9699 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
9701 tree c = build_omp_clause (UNKNOWN_LOCATION,
9702 OMP_CLAUSE_FIRSTPRIVATE);
9703 OMP_CLAUSE_DECL (c) = last;
9704 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
9705 OMP_PARALLEL_CLAUSES (*data[1]) = c;
9706 c = build_omp_clause (UNKNOWN_LOCATION,
9707 *pc ? OMP_CLAUSE_SHARED
9708 : OMP_CLAUSE_FIRSTPRIVATE);
9709 OMP_CLAUSE_DECL (c) = orig_decl;
9710 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
9711 OMP_PARALLEL_CLAUSES (*data[1]) = c;
9715 switch (TREE_CODE (for_stmt))
9717 case OMP_FOR:
9718 case OMP_DISTRIBUTE:
9719 break;
9720 case OACC_LOOP:
9721 ort = ORT_ACC;
9722 break;
9723 case OMP_TASKLOOP:
9724 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
9725 ort = ORT_UNTIED_TASK;
9726 else
9727 ort = ORT_TASK;
9728 break;
9729 case OMP_SIMD:
9730 ort = ORT_SIMD;
9731 break;
9732 default:
9733 gcc_unreachable ();
9736 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9737 clause for the IV. */
9738 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9740 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
9741 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9742 decl = TREE_OPERAND (t, 0);
9743 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
9744 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9745 && OMP_CLAUSE_DECL (c) == decl)
9747 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9748 break;
9752 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
9753 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
9754 TREE_CODE (for_stmt));
9756 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
9757 gimplify_omp_ctxp->distribute = true;
9759 /* Handle OMP_FOR_INIT. */
9760 for_pre_body = NULL;
9761 if ((ort == ORT_SIMD
9762 || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
9763 && OMP_FOR_PRE_BODY (for_stmt))
9765 has_decl_expr = BITMAP_ALLOC (NULL);
9766 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
9767 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
9768 == VAR_DECL)
9770 t = OMP_FOR_PRE_BODY (for_stmt);
9771 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9773 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
9775 tree_stmt_iterator si;
9776 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
9777 tsi_next (&si))
9779 t = tsi_stmt (si);
9780 if (TREE_CODE (t) == DECL_EXPR
9781 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
9782 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9786 if (OMP_FOR_PRE_BODY (for_stmt))
9788 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
9789 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9790 else
9792 struct gimplify_omp_ctx ctx;
9793 memset (&ctx, 0, sizeof (ctx));
9794 ctx.region_type = ORT_NONE;
9795 gimplify_omp_ctxp = &ctx;
9796 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9797 gimplify_omp_ctxp = NULL;
9800 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
9802 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9803 for_stmt = inner_for_stmt;
9805 /* For taskloop, need to gimplify the start, end and step before the
9806 taskloop, outside of the taskloop omp context. */
9807 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9809 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9811 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9812 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9814 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9815 TREE_OPERAND (t, 1)
9816 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9817 gimple_seq_empty_p (for_pre_body)
9818 ? pre_p : &for_pre_body, NULL,
9819 false);
9820 /* Reference to pointer conversion is considered useless,
9821 but is significant for firstprivate clause. Force it
9822 here. */
9823 if (TREE_CODE (type) == POINTER_TYPE
9824 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
9825 == REFERENCE_TYPE))
9827 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
9828 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
9829 TREE_OPERAND (t, 1));
9830 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
9831 ? pre_p : &for_pre_body);
9832 TREE_OPERAND (t, 1) = v;
9834 tree c = build_omp_clause (input_location,
9835 OMP_CLAUSE_FIRSTPRIVATE);
9836 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9837 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9838 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9841 /* Handle OMP_FOR_COND. */
9842 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9843 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9845 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9846 TREE_OPERAND (t, 1)
9847 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9848 gimple_seq_empty_p (for_pre_body)
9849 ? pre_p : &for_pre_body, NULL,
9850 false);
9851 /* Reference to pointer conversion is considered useless,
9852 but is significant for firstprivate clause. Force it
9853 here. */
9854 if (TREE_CODE (type) == POINTER_TYPE
9855 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
9856 == REFERENCE_TYPE))
9858 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
9859 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
9860 TREE_OPERAND (t, 1));
9861 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
9862 ? pre_p : &for_pre_body);
9863 TREE_OPERAND (t, 1) = v;
9865 tree c = build_omp_clause (input_location,
9866 OMP_CLAUSE_FIRSTPRIVATE);
9867 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9868 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9869 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9872 /* Handle OMP_FOR_INCR. */
9873 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9874 if (TREE_CODE (t) == MODIFY_EXPR)
9876 decl = TREE_OPERAND (t, 0);
9877 t = TREE_OPERAND (t, 1);
9878 tree *tp = &TREE_OPERAND (t, 1);
9879 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
9880 tp = &TREE_OPERAND (t, 0);
9882 if (!is_gimple_constant (*tp))
9884 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
9885 ? pre_p : &for_pre_body;
9886 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
9887 tree c = build_omp_clause (input_location,
9888 OMP_CLAUSE_FIRSTPRIVATE);
9889 OMP_CLAUSE_DECL (c) = *tp;
9890 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9891 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9896 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
9897 OMP_TASKLOOP);
9900 if (orig_for_stmt != for_stmt)
9901 gimplify_omp_ctxp->combined_loop = true;
9903 for_body = NULL;
9904 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9905 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
9906 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9907 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
9909 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
9910 bool is_doacross = false;
9911 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
9913 is_doacross = true;
9914 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
9915 (OMP_FOR_INIT (for_stmt))
9916 * 2);
9918 int collapse = 1, tile = 0;
9919 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
9920 if (c)
9921 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9922 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
9923 if (c)
9924 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
9925 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9927 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9928 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9929 decl = TREE_OPERAND (t, 0);
9930 gcc_assert (DECL_P (decl));
9931 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
9932 || POINTER_TYPE_P (TREE_TYPE (decl)));
9933 if (is_doacross)
9935 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
9937 tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
9938 if (TREE_CODE (orig_decl) == TREE_LIST)
9939 orig_decl = TREE_PURPOSE (orig_decl);
9940 gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
9942 else
9943 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9944 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9947 /* Make sure the iteration variable is private. */
9948 tree c = NULL_TREE;
9949 tree c2 = NULL_TREE;
9950 if (orig_for_stmt != for_stmt)
9952 /* Preserve this information until we gimplify the inner simd. */
9953 if (has_decl_expr
9954 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9955 TREE_PRIVATE (t) = 1;
9957 else if (ort == ORT_SIMD)
9959 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9960 (splay_tree_key) decl);
9961 omp_is_private (gimplify_omp_ctxp, decl,
9962 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9963 != 1));
9964 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9965 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9966 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9968 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9969 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9970 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
9971 if ((has_decl_expr
9972 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9973 || TREE_PRIVATE (t))
9975 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9976 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9978 struct gimplify_omp_ctx *outer
9979 = gimplify_omp_ctxp->outer_context;
9980 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9982 if (outer->region_type == ORT_WORKSHARE
9983 && outer->combined_loop)
9985 n = splay_tree_lookup (outer->variables,
9986 (splay_tree_key)decl);
9987 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9989 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9990 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9992 else
9994 struct gimplify_omp_ctx *octx = outer->outer_context;
9995 if (octx
9996 && octx->region_type == ORT_COMBINED_PARALLEL
9997 && octx->outer_context
9998 && (octx->outer_context->region_type
9999 == ORT_WORKSHARE)
10000 && octx->outer_context->combined_loop)
10002 octx = octx->outer_context;
10003 n = splay_tree_lookup (octx->variables,
10004 (splay_tree_key)decl);
10005 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10007 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10008 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10015 OMP_CLAUSE_DECL (c) = decl;
10016 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10017 OMP_FOR_CLAUSES (for_stmt) = c;
10018 omp_add_variable (gimplify_omp_ctxp, decl, flags);
10019 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
10021 if (outer->region_type == ORT_WORKSHARE
10022 && outer->combined_loop)
10024 if (outer->outer_context
10025 && (outer->outer_context->region_type
10026 == ORT_COMBINED_PARALLEL))
10027 outer = outer->outer_context;
10028 else if (omp_check_private (outer, decl, false))
10029 outer = NULL;
10031 else if (((outer->region_type & ORT_TASK) != 0)
10032 && outer->combined_loop
10033 && !omp_check_private (gimplify_omp_ctxp,
10034 decl, false))
10036 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10038 omp_notice_variable (outer, decl, true);
10039 outer = NULL;
10041 if (outer)
10043 n = splay_tree_lookup (outer->variables,
10044 (splay_tree_key)decl);
10045 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10047 omp_add_variable (outer, decl,
10048 GOVD_LASTPRIVATE | GOVD_SEEN);
10049 if (outer->region_type == ORT_COMBINED_PARALLEL
10050 && outer->outer_context
10051 && (outer->outer_context->region_type
10052 == ORT_WORKSHARE)
10053 && outer->outer_context->combined_loop)
10055 outer = outer->outer_context;
10056 n = splay_tree_lookup (outer->variables,
10057 (splay_tree_key)decl);
10058 if (omp_check_private (outer, decl, false))
10059 outer = NULL;
10060 else if (n == NULL
10061 || ((n->value & GOVD_DATA_SHARE_CLASS)
10062 == 0))
10063 omp_add_variable (outer, decl,
10064 GOVD_LASTPRIVATE
10065 | GOVD_SEEN);
10066 else
10067 outer = NULL;
10069 if (outer && outer->outer_context
10070 && (outer->outer_context->region_type
10071 == ORT_COMBINED_TEAMS))
10073 outer = outer->outer_context;
10074 n = splay_tree_lookup (outer->variables,
10075 (splay_tree_key)decl);
10076 if (n == NULL
10077 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10078 omp_add_variable (outer, decl,
10079 GOVD_SHARED | GOVD_SEEN);
10080 else
10081 outer = NULL;
10083 if (outer && outer->outer_context)
10084 omp_notice_variable (outer->outer_context, decl,
10085 true);
10090 else
10092 bool lastprivate
10093 = (!has_decl_expr
10094 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
10095 if (TREE_PRIVATE (t))
10096 lastprivate = false;
10097 struct gimplify_omp_ctx *outer
10098 = gimplify_omp_ctxp->outer_context;
10099 if (outer && lastprivate)
10101 if (outer->region_type == ORT_WORKSHARE
10102 && outer->combined_loop)
10104 n = splay_tree_lookup (outer->variables,
10105 (splay_tree_key)decl);
10106 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10108 lastprivate = false;
10109 outer = NULL;
10111 else if (outer->outer_context
10112 && (outer->outer_context->region_type
10113 == ORT_COMBINED_PARALLEL))
10114 outer = outer->outer_context;
10115 else if (omp_check_private (outer, decl, false))
10116 outer = NULL;
10118 else if (((outer->region_type & ORT_TASK) != 0)
10119 && outer->combined_loop
10120 && !omp_check_private (gimplify_omp_ctxp,
10121 decl, false))
10123 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10125 omp_notice_variable (outer, decl, true);
10126 outer = NULL;
10128 if (outer)
10130 n = splay_tree_lookup (outer->variables,
10131 (splay_tree_key)decl);
10132 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10134 omp_add_variable (outer, decl,
10135 GOVD_LASTPRIVATE | GOVD_SEEN);
10136 if (outer->region_type == ORT_COMBINED_PARALLEL
10137 && outer->outer_context
10138 && (outer->outer_context->region_type
10139 == ORT_WORKSHARE)
10140 && outer->outer_context->combined_loop)
10142 outer = outer->outer_context;
10143 n = splay_tree_lookup (outer->variables,
10144 (splay_tree_key)decl);
10145 if (omp_check_private (outer, decl, false))
10146 outer = NULL;
10147 else if (n == NULL
10148 || ((n->value & GOVD_DATA_SHARE_CLASS)
10149 == 0))
10150 omp_add_variable (outer, decl,
10151 GOVD_LASTPRIVATE
10152 | GOVD_SEEN);
10153 else
10154 outer = NULL;
10156 if (outer && outer->outer_context
10157 && (outer->outer_context->region_type
10158 == ORT_COMBINED_TEAMS))
10160 outer = outer->outer_context;
10161 n = splay_tree_lookup (outer->variables,
10162 (splay_tree_key)decl);
10163 if (n == NULL
10164 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10165 omp_add_variable (outer, decl,
10166 GOVD_SHARED | GOVD_SEEN);
10167 else
10168 outer = NULL;
10170 if (outer && outer->outer_context)
10171 omp_notice_variable (outer->outer_context, decl,
10172 true);
10177 c = build_omp_clause (input_location,
10178 lastprivate ? OMP_CLAUSE_LASTPRIVATE
10179 : OMP_CLAUSE_PRIVATE);
10180 OMP_CLAUSE_DECL (c) = decl;
10181 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10182 OMP_FOR_CLAUSES (for_stmt) = c;
10183 omp_add_variable (gimplify_omp_ctxp, decl,
10184 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
10185 | GOVD_EXPLICIT | GOVD_SEEN);
10186 c = NULL_TREE;
10189 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
10190 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10191 else
10192 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
10194 /* If DECL is not a gimple register, create a temporary variable to act
10195 as an iteration counter. This is valid, since DECL cannot be
10196 modified in the body of the loop. Similarly for any iteration vars
10197 in simd with collapse > 1 where the iterator vars must be
10198 lastprivate. */
10199 if (orig_for_stmt != for_stmt)
10200 var = decl;
10201 else if (!is_gimple_reg (decl)
10202 || (ort == ORT_SIMD
10203 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
10205 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10206 /* Make sure omp_add_variable is not called on it prematurely.
10207 We call it ourselves a few lines later. */
10208 gimplify_omp_ctxp = NULL;
10209 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10210 gimplify_omp_ctxp = ctx;
10211 TREE_OPERAND (t, 0) = var;
10213 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
10215 if (ort == ORT_SIMD
10216 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10218 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10219 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
10220 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
10221 OMP_CLAUSE_DECL (c2) = var;
10222 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
10223 OMP_FOR_CLAUSES (for_stmt) = c2;
10224 omp_add_variable (gimplify_omp_ctxp, var,
10225 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
10226 if (c == NULL_TREE)
10228 c = c2;
10229 c2 = NULL_TREE;
10232 else
10233 omp_add_variable (gimplify_omp_ctxp, var,
10234 GOVD_PRIVATE | GOVD_SEEN);
10236 else
10237 var = decl;
10239 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10240 is_gimple_val, fb_rvalue, false);
10241 ret = MIN (ret, tret);
10242 if (ret == GS_ERROR)
10243 return ret;
10245 /* Handle OMP_FOR_COND. */
10246 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10247 gcc_assert (COMPARISON_CLASS_P (t));
10248 gcc_assert (TREE_OPERAND (t, 0) == decl);
10250 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10251 is_gimple_val, fb_rvalue, false);
10252 ret = MIN (ret, tret);
10254 /* Handle OMP_FOR_INCR. */
10255 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10256 switch (TREE_CODE (t))
10258 case PREINCREMENT_EXPR:
10259 case POSTINCREMENT_EXPR:
10261 tree decl = TREE_OPERAND (t, 0);
10262 /* c_omp_for_incr_canonicalize_ptr() should have been
10263 called to massage things appropriately. */
10264 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10266 if (orig_for_stmt != for_stmt)
10267 break;
10268 t = build_int_cst (TREE_TYPE (decl), 1);
10269 if (c)
10270 OMP_CLAUSE_LINEAR_STEP (c) = t;
10271 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10272 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10273 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10274 break;
10277 case PREDECREMENT_EXPR:
10278 case POSTDECREMENT_EXPR:
10279 /* c_omp_for_incr_canonicalize_ptr() should have been
10280 called to massage things appropriately. */
10281 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
10282 if (orig_for_stmt != for_stmt)
10283 break;
10284 t = build_int_cst (TREE_TYPE (decl), -1);
10285 if (c)
10286 OMP_CLAUSE_LINEAR_STEP (c) = t;
10287 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
10288 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
10289 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
10290 break;
10292 case MODIFY_EXPR:
10293 gcc_assert (TREE_OPERAND (t, 0) == decl);
10294 TREE_OPERAND (t, 0) = var;
10296 t = TREE_OPERAND (t, 1);
10297 switch (TREE_CODE (t))
10299 case PLUS_EXPR:
10300 if (TREE_OPERAND (t, 1) == decl)
10302 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
10303 TREE_OPERAND (t, 0) = var;
10304 break;
10307 /* Fallthru. */
10308 case MINUS_EXPR:
10309 case POINTER_PLUS_EXPR:
10310 gcc_assert (TREE_OPERAND (t, 0) == decl);
10311 TREE_OPERAND (t, 0) = var;
10312 break;
10313 default:
10314 gcc_unreachable ();
10317 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
10318 is_gimple_val, fb_rvalue, false);
10319 ret = MIN (ret, tret);
10320 if (c)
10322 tree step = TREE_OPERAND (t, 1);
10323 tree stept = TREE_TYPE (decl);
10324 if (POINTER_TYPE_P (stept))
10325 stept = sizetype;
10326 step = fold_convert (stept, step);
10327 if (TREE_CODE (t) == MINUS_EXPR)
10328 step = fold_build1 (NEGATE_EXPR, stept, step);
10329 OMP_CLAUSE_LINEAR_STEP (c) = step;
10330 if (step != TREE_OPERAND (t, 1))
10332 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
10333 &for_pre_body, NULL,
10334 is_gimple_val, fb_rvalue, false);
10335 ret = MIN (ret, tret);
10338 break;
10340 default:
10341 gcc_unreachable ();
10344 if (c2)
10346 gcc_assert (c);
10347 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
10350 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
10352 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
10353 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10354 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
10355 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10356 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
10357 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
10358 && OMP_CLAUSE_DECL (c) == decl)
10360 if (is_doacross && (collapse == 1 || i >= collapse))
10361 t = var;
10362 else
10364 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10365 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10366 gcc_assert (TREE_OPERAND (t, 0) == var);
10367 t = TREE_OPERAND (t, 1);
10368 gcc_assert (TREE_CODE (t) == PLUS_EXPR
10369 || TREE_CODE (t) == MINUS_EXPR
10370 || TREE_CODE (t) == POINTER_PLUS_EXPR);
10371 gcc_assert (TREE_OPERAND (t, 0) == var);
10372 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
10373 is_doacross ? var : decl,
10374 TREE_OPERAND (t, 1));
10376 gimple_seq *seq;
10377 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10378 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
10379 else
10380 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
10381 gimplify_assign (decl, t, seq);
10386 BITMAP_FREE (has_decl_expr);
10388 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10390 push_gimplify_context ();
10391 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
10393 OMP_FOR_BODY (orig_for_stmt)
10394 = build3 (BIND_EXPR, void_type_node, NULL,
10395 OMP_FOR_BODY (orig_for_stmt), NULL);
10396 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
10400 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
10401 &for_body);
10403 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10405 if (gimple_code (g) == GIMPLE_BIND)
10406 pop_gimplify_context (g);
10407 else
10408 pop_gimplify_context (NULL);
10411 if (orig_for_stmt != for_stmt)
10412 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10414 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10415 decl = TREE_OPERAND (t, 0);
10416 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10417 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10418 gimplify_omp_ctxp = ctx->outer_context;
10419 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10420 gimplify_omp_ctxp = ctx;
10421 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
10422 TREE_OPERAND (t, 0) = var;
10423 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10424 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
10425 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
10428 gimplify_adjust_omp_clauses (pre_p, for_body,
10429 &OMP_FOR_CLAUSES (orig_for_stmt),
10430 TREE_CODE (orig_for_stmt));
10432 int kind;
10433 switch (TREE_CODE (orig_for_stmt))
10435 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
10436 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
10437 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
10438 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
10439 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
10440 default:
10441 gcc_unreachable ();
10443 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
10444 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
10445 for_pre_body);
10446 if (orig_for_stmt != for_stmt)
10447 gimple_omp_for_set_combined_p (gfor, true);
10448 if (gimplify_omp_ctxp
10449 && (gimplify_omp_ctxp->combined_loop
10450 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
10451 && gimplify_omp_ctxp->outer_context
10452 && gimplify_omp_ctxp->outer_context->combined_loop)))
10454 gimple_omp_for_set_combined_into_p (gfor, true);
10455 if (gimplify_omp_ctxp->combined_loop)
10456 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
10457 else
10458 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
10461 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10463 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10464 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
10465 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
10466 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10467 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
10468 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
10469 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10470 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
10473 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
10474 constructs with GIMPLE_OMP_TASK sandwiched in between them.
10475 The outer taskloop stands for computing the number of iterations,
10476 counts for collapsed loops and holding taskloop specific clauses.
10477 The task construct stands for the effect of data sharing on the
10478 explicit task it creates and the inner taskloop stands for expansion
10479 of the static loop inside of the explicit task construct. */
10480 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10482 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
10483 tree task_clauses = NULL_TREE;
10484 tree c = *gfor_clauses_ptr;
10485 tree *gtask_clauses_ptr = &task_clauses;
10486 tree outer_for_clauses = NULL_TREE;
10487 tree *gforo_clauses_ptr = &outer_for_clauses;
10488 for (; c; c = OMP_CLAUSE_CHAIN (c))
10489 switch (OMP_CLAUSE_CODE (c))
10491 /* These clauses are allowed on task, move them there. */
10492 case OMP_CLAUSE_SHARED:
10493 case OMP_CLAUSE_FIRSTPRIVATE:
10494 case OMP_CLAUSE_DEFAULT:
10495 case OMP_CLAUSE_IF:
10496 case OMP_CLAUSE_UNTIED:
10497 case OMP_CLAUSE_FINAL:
10498 case OMP_CLAUSE_MERGEABLE:
10499 case OMP_CLAUSE_PRIORITY:
10500 *gtask_clauses_ptr = c;
10501 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10502 break;
10503 case OMP_CLAUSE_PRIVATE:
10504 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
10506 /* We want private on outer for and firstprivate
10507 on task. */
10508 *gtask_clauses_ptr
10509 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10510 OMP_CLAUSE_FIRSTPRIVATE);
10511 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10512 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10513 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10514 *gforo_clauses_ptr = c;
10515 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10517 else
10519 *gtask_clauses_ptr = c;
10520 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10522 break;
10523 /* These clauses go into outer taskloop clauses. */
10524 case OMP_CLAUSE_GRAINSIZE:
10525 case OMP_CLAUSE_NUM_TASKS:
10526 case OMP_CLAUSE_NOGROUP:
10527 *gforo_clauses_ptr = c;
10528 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10529 break;
10530 /* Taskloop clause we duplicate on both taskloops. */
10531 case OMP_CLAUSE_COLLAPSE:
10532 *gfor_clauses_ptr = c;
10533 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10534 *gforo_clauses_ptr = copy_node (c);
10535 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10536 break;
10537 /* For lastprivate, keep the clause on inner taskloop, and add
10538 a shared clause on task. If the same decl is also firstprivate,
10539 add also firstprivate clause on the inner taskloop. */
10540 case OMP_CLAUSE_LASTPRIVATE:
10541 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
10543 /* For taskloop C++ lastprivate IVs, we want:
10544 1) private on outer taskloop
10545 2) firstprivate and shared on task
10546 3) lastprivate on inner taskloop */
10547 *gtask_clauses_ptr
10548 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10549 OMP_CLAUSE_FIRSTPRIVATE);
10550 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10551 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10552 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10553 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
10554 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10555 OMP_CLAUSE_PRIVATE);
10556 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
10557 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
10558 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
10559 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10561 *gfor_clauses_ptr = c;
10562 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10563 *gtask_clauses_ptr
10564 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
10565 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10566 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
10567 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
10568 gtask_clauses_ptr
10569 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10570 break;
10571 default:
10572 gcc_unreachable ();
10574 *gfor_clauses_ptr = NULL_TREE;
10575 *gtask_clauses_ptr = NULL_TREE;
10576 *gforo_clauses_ptr = NULL_TREE;
10577 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
10578 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
10579 NULL_TREE, NULL_TREE, NULL_TREE);
10580 gimple_omp_task_set_taskloop_p (g, true);
10581 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
10582 gomp_for *gforo
10583 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
10584 gimple_omp_for_collapse (gfor),
10585 gimple_omp_for_pre_body (gfor));
10586 gimple_omp_for_set_pre_body (gfor, NULL);
10587 gimple_omp_for_set_combined_p (gforo, true);
10588 gimple_omp_for_set_combined_into_p (gfor, true);
10589 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
10591 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
10592 tree v = create_tmp_var (type);
10593 gimple_omp_for_set_index (gforo, i, v);
10594 t = unshare_expr (gimple_omp_for_initial (gfor, i));
10595 gimple_omp_for_set_initial (gforo, i, t);
10596 gimple_omp_for_set_cond (gforo, i,
10597 gimple_omp_for_cond (gfor, i));
10598 t = unshare_expr (gimple_omp_for_final (gfor, i));
10599 gimple_omp_for_set_final (gforo, i, t);
10600 t = unshare_expr (gimple_omp_for_incr (gfor, i));
10601 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
10602 TREE_OPERAND (t, 0) = v;
10603 gimple_omp_for_set_incr (gforo, i, t);
10604 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
10605 OMP_CLAUSE_DECL (t) = v;
10606 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
10607 gimple_omp_for_set_clauses (gforo, t);
10609 gimplify_seq_add_stmt (pre_p, gforo);
10611 else
10612 gimplify_seq_add_stmt (pre_p, gfor);
10613 if (ret != GS_ALL_DONE)
10614 return GS_ERROR;
10615 *expr_p = NULL_TREE;
10616 return GS_ALL_DONE;
10619 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10620 of OMP_TARGET's body. */
10622 static tree
10623 find_omp_teams (tree *tp, int *walk_subtrees, void *)
10625 *walk_subtrees = 0;
10626 switch (TREE_CODE (*tp))
10628 case OMP_TEAMS:
10629 return *tp;
10630 case BIND_EXPR:
10631 case STATEMENT_LIST:
10632 *walk_subtrees = 1;
10633 break;
10634 default:
10635 break;
10637 return NULL_TREE;
10640 /* Helper function of optimize_target_teams, determine if the expression
10641 can be computed safely before the target construct on the host. */
10643 static tree
10644 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
10646 splay_tree_node n;
10648 if (TYPE_P (*tp))
10650 *walk_subtrees = 0;
10651 return NULL_TREE;
10653 switch (TREE_CODE (*tp))
10655 case VAR_DECL:
10656 case PARM_DECL:
10657 case RESULT_DECL:
10658 *walk_subtrees = 0;
10659 if (error_operand_p (*tp)
10660 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
10661 || DECL_HAS_VALUE_EXPR_P (*tp)
10662 || DECL_THREAD_LOCAL_P (*tp)
10663 || TREE_SIDE_EFFECTS (*tp)
10664 || TREE_THIS_VOLATILE (*tp))
10665 return *tp;
10666 if (is_global_var (*tp)
10667 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
10668 || lookup_attribute ("omp declare target link",
10669 DECL_ATTRIBUTES (*tp))))
10670 return *tp;
10671 if (VAR_P (*tp)
10672 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
10673 && !is_global_var (*tp)
10674 && decl_function_context (*tp) == current_function_decl)
10675 return *tp;
10676 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10677 (splay_tree_key) *tp);
10678 if (n == NULL)
10680 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
10681 return NULL_TREE;
10682 return *tp;
10684 else if (n->value & GOVD_LOCAL)
10685 return *tp;
10686 else if (n->value & GOVD_FIRSTPRIVATE)
10687 return NULL_TREE;
10688 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10689 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10690 return NULL_TREE;
10691 return *tp;
10692 case INTEGER_CST:
10693 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10694 return *tp;
10695 return NULL_TREE;
10696 case TARGET_EXPR:
10697 if (TARGET_EXPR_INITIAL (*tp)
10698 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
10699 return *tp;
10700 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
10701 walk_subtrees, NULL);
10702 /* Allow some reasonable subset of integral arithmetics. */
10703 case PLUS_EXPR:
10704 case MINUS_EXPR:
10705 case MULT_EXPR:
10706 case TRUNC_DIV_EXPR:
10707 case CEIL_DIV_EXPR:
10708 case FLOOR_DIV_EXPR:
10709 case ROUND_DIV_EXPR:
10710 case TRUNC_MOD_EXPR:
10711 case CEIL_MOD_EXPR:
10712 case FLOOR_MOD_EXPR:
10713 case ROUND_MOD_EXPR:
10714 case RDIV_EXPR:
10715 case EXACT_DIV_EXPR:
10716 case MIN_EXPR:
10717 case MAX_EXPR:
10718 case LSHIFT_EXPR:
10719 case RSHIFT_EXPR:
10720 case BIT_IOR_EXPR:
10721 case BIT_XOR_EXPR:
10722 case BIT_AND_EXPR:
10723 case NEGATE_EXPR:
10724 case ABS_EXPR:
10725 case BIT_NOT_EXPR:
10726 case NON_LVALUE_EXPR:
10727 CASE_CONVERT:
10728 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10729 return *tp;
10730 return NULL_TREE;
10731 /* And disallow anything else, except for comparisons. */
10732 default:
10733 if (COMPARISON_CLASS_P (*tp))
10734 return NULL_TREE;
10735 return *tp;
10739 /* Try to determine if the num_teams and/or thread_limit expressions
10740 can have their values determined already before entering the
10741 target construct.
10742 INTEGER_CSTs trivially are,
10743 integral decls that are firstprivate (explicitly or implicitly)
10744 or explicitly map(always, to:) or map(always, tofrom:) on the target
10745 region too, and expressions involving simple arithmetics on those
10746 too, function calls are not ok, dereferencing something neither etc.
10747 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10748 EXPR based on what we find:
10749 0 stands for clause not specified at all, use implementation default
10750 -1 stands for value that can't be determined easily before entering
10751 the target construct.
10752 If teams construct is not present at all, use 1 for num_teams
10753 and 0 for thread_limit (only one team is involved, and the thread
10754 limit is implementation defined. */
10756 static void
10757 optimize_target_teams (tree target, gimple_seq *pre_p)
10759 tree body = OMP_BODY (target);
10760 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
10761 tree num_teams = integer_zero_node;
10762 tree thread_limit = integer_zero_node;
10763 location_t num_teams_loc = EXPR_LOCATION (target);
10764 location_t thread_limit_loc = EXPR_LOCATION (target);
10765 tree c, *p, expr;
10766 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
10768 if (teams == NULL_TREE)
10769 num_teams = integer_one_node;
10770 else
10771 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
10773 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
10775 p = &num_teams;
10776 num_teams_loc = OMP_CLAUSE_LOCATION (c);
10778 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
10780 p = &thread_limit;
10781 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
10783 else
10784 continue;
10785 expr = OMP_CLAUSE_OPERAND (c, 0);
10786 if (TREE_CODE (expr) == INTEGER_CST)
10788 *p = expr;
10789 continue;
10791 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
10793 *p = integer_minus_one_node;
10794 continue;
10796 *p = expr;
10797 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
10798 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
10799 == GS_ERROR)
10801 gimplify_omp_ctxp = target_ctx;
10802 *p = integer_minus_one_node;
10803 continue;
10805 gimplify_omp_ctxp = target_ctx;
10806 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
10807 OMP_CLAUSE_OPERAND (c, 0) = *p;
10809 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10810 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
10811 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10812 OMP_TARGET_CLAUSES (target) = c;
10813 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10814 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
10815 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10816 OMP_TARGET_CLAUSES (target) = c;
10819 /* Gimplify the gross structure of several OMP constructs. */
10821 static void
10822 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
10824 tree expr = *expr_p;
10825 gimple *stmt;
10826 gimple_seq body = NULL;
10827 enum omp_region_type ort;
10829 switch (TREE_CODE (expr))
10831 case OMP_SECTIONS:
10832 case OMP_SINGLE:
10833 ort = ORT_WORKSHARE;
10834 break;
10835 case OMP_TARGET:
10836 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
10837 break;
10838 case OACC_KERNELS:
10839 ort = ORT_ACC_KERNELS;
10840 break;
10841 case OACC_PARALLEL:
10842 ort = ORT_ACC_PARALLEL;
10843 break;
10844 case OACC_DATA:
10845 ort = ORT_ACC_DATA;
10846 break;
10847 case OMP_TARGET_DATA:
10848 ort = ORT_TARGET_DATA;
10849 break;
10850 case OMP_TEAMS:
10851 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
10852 break;
10853 case OACC_HOST_DATA:
10854 ort = ORT_ACC_HOST_DATA;
10855 break;
10856 default:
10857 gcc_unreachable ();
10859 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
10860 TREE_CODE (expr));
10861 if (TREE_CODE (expr) == OMP_TARGET)
10862 optimize_target_teams (expr, pre_p);
10863 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
10865 push_gimplify_context ();
10866 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
10867 if (gimple_code (g) == GIMPLE_BIND)
10868 pop_gimplify_context (g);
10869 else
10870 pop_gimplify_context (NULL);
10871 if ((ort & ORT_TARGET_DATA) != 0)
10873 enum built_in_function end_ix;
10874 switch (TREE_CODE (expr))
10876 case OACC_DATA:
10877 case OACC_HOST_DATA:
10878 end_ix = BUILT_IN_GOACC_DATA_END;
10879 break;
10880 case OMP_TARGET_DATA:
10881 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
10882 break;
10883 default:
10884 gcc_unreachable ();
10886 tree fn = builtin_decl_explicit (end_ix);
10887 g = gimple_build_call (fn, 0);
10888 gimple_seq cleanup = NULL;
10889 gimple_seq_add_stmt (&cleanup, g);
10890 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10891 body = NULL;
10892 gimple_seq_add_stmt (&body, g);
10895 else
10896 gimplify_and_add (OMP_BODY (expr), &body);
10897 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
10898 TREE_CODE (expr));
10900 switch (TREE_CODE (expr))
10902 case OACC_DATA:
10903 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
10904 OMP_CLAUSES (expr));
10905 break;
10906 case OACC_KERNELS:
10907 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
10908 OMP_CLAUSES (expr));
10909 break;
10910 case OACC_HOST_DATA:
10911 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
10912 OMP_CLAUSES (expr));
10913 break;
10914 case OACC_PARALLEL:
10915 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
10916 OMP_CLAUSES (expr));
10917 break;
10918 case OMP_SECTIONS:
10919 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
10920 break;
10921 case OMP_SINGLE:
10922 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
10923 break;
10924 case OMP_TARGET:
10925 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
10926 OMP_CLAUSES (expr));
10927 break;
10928 case OMP_TARGET_DATA:
10929 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
10930 OMP_CLAUSES (expr));
10931 break;
10932 case OMP_TEAMS:
10933 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
10934 break;
10935 default:
10936 gcc_unreachable ();
10939 gimplify_seq_add_stmt (pre_p, stmt);
10940 *expr_p = NULL_TREE;
10943 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10944 target update constructs. */
10946 static void
10947 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
10949 tree expr = *expr_p;
10950 int kind;
10951 gomp_target *stmt;
10952 enum omp_region_type ort = ORT_WORKSHARE;
10954 switch (TREE_CODE (expr))
10956 case OACC_ENTER_DATA:
10957 case OACC_EXIT_DATA:
10958 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
10959 ort = ORT_ACC;
10960 break;
10961 case OACC_UPDATE:
10962 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
10963 ort = ORT_ACC;
10964 break;
10965 case OMP_TARGET_UPDATE:
10966 kind = GF_OMP_TARGET_KIND_UPDATE;
10967 break;
10968 case OMP_TARGET_ENTER_DATA:
10969 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
10970 break;
10971 case OMP_TARGET_EXIT_DATA:
10972 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
10973 break;
10974 default:
10975 gcc_unreachable ();
10977 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
10978 ort, TREE_CODE (expr));
10979 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
10980 TREE_CODE (expr));
10981 if (TREE_CODE (expr) == OACC_UPDATE
10982 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
10983 OMP_CLAUSE_IF_PRESENT))
10985 /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
10986 clause. */
10987 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
10988 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
10989 switch (OMP_CLAUSE_MAP_KIND (c))
10991 case GOMP_MAP_FORCE_TO:
10992 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
10993 break;
10994 case GOMP_MAP_FORCE_FROM:
10995 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
10996 break;
10997 default:
10998 break;
11001 else if (TREE_CODE (expr) == OACC_EXIT_DATA
11002 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
11003 OMP_CLAUSE_FINALIZE))
11005 /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote that "finalize"
11006 semantics apply to all mappings of this OpenACC directive. */
11007 bool finalize_marked = false;
11008 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11009 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
11010 switch (OMP_CLAUSE_MAP_KIND (c))
11012 case GOMP_MAP_FROM:
11013 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
11014 finalize_marked = true;
11015 break;
11016 case GOMP_MAP_RELEASE:
11017 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
11018 finalize_marked = true;
11019 break;
11020 default:
11021 /* Check consistency: libgomp relies on the very first data
11022 mapping clause being marked, so make sure we did that before
11023 any other mapping clauses. */
11024 gcc_assert (finalize_marked);
11025 break;
11028 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
11030 gimplify_seq_add_stmt (pre_p, stmt);
11031 *expr_p = NULL_TREE;
11034 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
11035 stabilized the lhs of the atomic operation as *ADDR. Return true if
11036 EXPR is this stabilized form. */
11038 static bool
11039 goa_lhs_expr_p (tree expr, tree addr)
11041 /* Also include casts to other type variants. The C front end is fond
11042 of adding these for e.g. volatile variables. This is like
11043 STRIP_TYPE_NOPS but includes the main variant lookup. */
11044 STRIP_USELESS_TYPE_CONVERSION (expr);
11046 if (TREE_CODE (expr) == INDIRECT_REF)
11048 expr = TREE_OPERAND (expr, 0);
11049 while (expr != addr
11050 && (CONVERT_EXPR_P (expr)
11051 || TREE_CODE (expr) == NON_LVALUE_EXPR)
11052 && TREE_CODE (expr) == TREE_CODE (addr)
11053 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
11055 expr = TREE_OPERAND (expr, 0);
11056 addr = TREE_OPERAND (addr, 0);
11058 if (expr == addr)
11059 return true;
11060 return (TREE_CODE (addr) == ADDR_EXPR
11061 && TREE_CODE (expr) == ADDR_EXPR
11062 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
11064 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
11065 return true;
11066 return false;
11069 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
11070 expression does not involve the lhs, evaluate it into a temporary.
11071 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
11072 or -1 if an error was encountered. */
11074 static int
11075 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
11076 tree lhs_var)
11078 tree expr = *expr_p;
11079 int saw_lhs;
11081 if (goa_lhs_expr_p (expr, lhs_addr))
11083 *expr_p = lhs_var;
11084 return 1;
11086 if (is_gimple_val (expr))
11087 return 0;
11089 saw_lhs = 0;
11090 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
11092 case tcc_binary:
11093 case tcc_comparison:
11094 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
11095 lhs_var);
11096 /* FALLTHRU */
11097 case tcc_unary:
11098 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
11099 lhs_var);
11100 break;
11101 case tcc_expression:
11102 switch (TREE_CODE (expr))
11104 case TRUTH_ANDIF_EXPR:
11105 case TRUTH_ORIF_EXPR:
11106 case TRUTH_AND_EXPR:
11107 case TRUTH_OR_EXPR:
11108 case TRUTH_XOR_EXPR:
11109 case BIT_INSERT_EXPR:
11110 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
11111 lhs_addr, lhs_var);
11112 /* FALLTHRU */
11113 case TRUTH_NOT_EXPR:
11114 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11115 lhs_addr, lhs_var);
11116 break;
11117 case COMPOUND_EXPR:
11118 /* Break out any preevaluations from cp_build_modify_expr. */
11119 for (; TREE_CODE (expr) == COMPOUND_EXPR;
11120 expr = TREE_OPERAND (expr, 1))
11121 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
11122 *expr_p = expr;
11123 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
11124 default:
11125 break;
11127 break;
11128 case tcc_reference:
11129 if (TREE_CODE (expr) == BIT_FIELD_REF)
11130 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11131 lhs_addr, lhs_var);
11132 break;
11133 default:
11134 break;
11137 if (saw_lhs == 0)
11139 enum gimplify_status gs;
11140 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
11141 if (gs != GS_ALL_DONE)
11142 saw_lhs = -1;
11145 return saw_lhs;
11148 /* Gimplify an OMP_ATOMIC statement. */
11150 static enum gimplify_status
11151 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
11153 tree addr = TREE_OPERAND (*expr_p, 0);
11154 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
11155 ? NULL : TREE_OPERAND (*expr_p, 1);
11156 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
11157 tree tmp_load;
11158 gomp_atomic_load *loadstmt;
11159 gomp_atomic_store *storestmt;
11161 tmp_load = create_tmp_reg (type);
11162 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
11163 return GS_ERROR;
11165 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
11166 != GS_ALL_DONE)
11167 return GS_ERROR;
11169 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
11170 gimplify_seq_add_stmt (pre_p, loadstmt);
11171 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
11172 != GS_ALL_DONE)
11173 return GS_ERROR;
11175 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
11176 rhs = tmp_load;
11177 storestmt = gimple_build_omp_atomic_store (rhs);
11178 gimplify_seq_add_stmt (pre_p, storestmt);
11179 if (OMP_ATOMIC_SEQ_CST (*expr_p))
11181 gimple_omp_atomic_set_seq_cst (loadstmt);
11182 gimple_omp_atomic_set_seq_cst (storestmt);
11184 switch (TREE_CODE (*expr_p))
11186 case OMP_ATOMIC_READ:
11187 case OMP_ATOMIC_CAPTURE_OLD:
11188 *expr_p = tmp_load;
11189 gimple_omp_atomic_set_need_value (loadstmt);
11190 break;
11191 case OMP_ATOMIC_CAPTURE_NEW:
11192 *expr_p = rhs;
11193 gimple_omp_atomic_set_need_value (storestmt);
11194 break;
11195 default:
11196 *expr_p = NULL;
11197 break;
11200 return GS_ALL_DONE;
11203 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
11204 body, and adding some EH bits. */
11206 static enum gimplify_status
11207 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
11209 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
11210 gimple *body_stmt;
11211 gtransaction *trans_stmt;
11212 gimple_seq body = NULL;
11213 int subcode = 0;
11215 /* Wrap the transaction body in a BIND_EXPR so we have a context
11216 where to put decls for OMP. */
11217 if (TREE_CODE (tbody) != BIND_EXPR)
11219 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
11220 TREE_SIDE_EFFECTS (bind) = 1;
11221 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
11222 TRANSACTION_EXPR_BODY (expr) = bind;
11225 push_gimplify_context ();
11226 temp = voidify_wrapper_expr (*expr_p, NULL);
11228 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
11229 pop_gimplify_context (body_stmt);
11231 trans_stmt = gimple_build_transaction (body);
11232 if (TRANSACTION_EXPR_OUTER (expr))
11233 subcode = GTMA_IS_OUTER;
11234 else if (TRANSACTION_EXPR_RELAXED (expr))
11235 subcode = GTMA_IS_RELAXED;
11236 gimple_transaction_set_subcode (trans_stmt, subcode);
11238 gimplify_seq_add_stmt (pre_p, trans_stmt);
11240 if (temp)
11242 *expr_p = temp;
11243 return GS_OK;
11246 *expr_p = NULL_TREE;
11247 return GS_ALL_DONE;
11250 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
11251 is the OMP_BODY of the original EXPR (which has already been
11252 gimplified so it's not present in the EXPR).
11254 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
11256 static gimple *
11257 gimplify_omp_ordered (tree expr, gimple_seq body)
11259 tree c, decls;
11260 int failures = 0;
11261 unsigned int i;
11262 tree source_c = NULL_TREE;
11263 tree sink_c = NULL_TREE;
11265 if (gimplify_omp_ctxp)
11267 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11268 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11269 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
11270 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
11271 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
11273 error_at (OMP_CLAUSE_LOCATION (c),
11274 "%<ordered%> construct with %<depend%> clause must be "
11275 "closely nested inside a loop with %<ordered%> clause "
11276 "with a parameter");
11277 failures++;
11279 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11280 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
11282 bool fail = false;
11283 for (decls = OMP_CLAUSE_DECL (c), i = 0;
11284 decls && TREE_CODE (decls) == TREE_LIST;
11285 decls = TREE_CHAIN (decls), ++i)
11286 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
11287 continue;
11288 else if (TREE_VALUE (decls)
11289 != gimplify_omp_ctxp->loop_iter_var[2 * i])
11291 error_at (OMP_CLAUSE_LOCATION (c),
11292 "variable %qE is not an iteration "
11293 "of outermost loop %d, expected %qE",
11294 TREE_VALUE (decls), i + 1,
11295 gimplify_omp_ctxp->loop_iter_var[2 * i]);
11296 fail = true;
11297 failures++;
11299 else
11300 TREE_VALUE (decls)
11301 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
11302 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
11304 error_at (OMP_CLAUSE_LOCATION (c),
11305 "number of variables in %<depend(sink)%> "
11306 "clause does not match number of "
11307 "iteration variables");
11308 failures++;
11310 sink_c = c;
11312 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11313 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
11315 if (source_c)
11317 error_at (OMP_CLAUSE_LOCATION (c),
11318 "more than one %<depend(source)%> clause on an "
11319 "%<ordered%> construct");
11320 failures++;
11322 else
11323 source_c = c;
11326 if (source_c && sink_c)
11328 error_at (OMP_CLAUSE_LOCATION (source_c),
11329 "%<depend(source)%> clause specified together with "
11330 "%<depend(sink:)%> clauses on the same construct");
11331 failures++;
11334 if (failures)
11335 return gimple_build_nop ();
11336 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
11339 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
11340 expression produces a value to be used as an operand inside a GIMPLE
11341 statement, the value will be stored back in *EXPR_P. This value will
11342 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
11343 an SSA_NAME. The corresponding sequence of GIMPLE statements is
11344 emitted in PRE_P and POST_P.
11346 Additionally, this process may overwrite parts of the input
11347 expression during gimplification. Ideally, it should be
11348 possible to do non-destructive gimplification.
11350 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
11351 the expression needs to evaluate to a value to be used as
11352 an operand in a GIMPLE statement, this value will be stored in
11353 *EXPR_P on exit. This happens when the caller specifies one
11354 of fb_lvalue or fb_rvalue fallback flags.
11356 PRE_P will contain the sequence of GIMPLE statements corresponding
11357 to the evaluation of EXPR and all the side-effects that must
11358 be executed before the main expression. On exit, the last
11359 statement of PRE_P is the core statement being gimplified. For
11360 instance, when gimplifying 'if (++a)' the last statement in
11361 PRE_P will be 'if (t.1)' where t.1 is the result of
11362 pre-incrementing 'a'.
11364 POST_P will contain the sequence of GIMPLE statements corresponding
11365 to the evaluation of all the side-effects that must be executed
11366 after the main expression. If this is NULL, the post
11367 side-effects are stored at the end of PRE_P.
11369 The reason why the output is split in two is to handle post
11370 side-effects explicitly. In some cases, an expression may have
11371 inner and outer post side-effects which need to be emitted in
11372 an order different from the one given by the recursive
11373 traversal. For instance, for the expression (*p--)++ the post
11374 side-effects of '--' must actually occur *after* the post
11375 side-effects of '++'. However, gimplification will first visit
11376 the inner expression, so if a separate POST sequence was not
11377 used, the resulting sequence would be:
11379 1 t.1 = *p
11380 2 p = p - 1
11381 3 t.2 = t.1 + 1
11382 4 *p = t.2
11384 However, the post-decrement operation in line #2 must not be
11385 evaluated until after the store to *p at line #4, so the
11386 correct sequence should be:
11388 1 t.1 = *p
11389 2 t.2 = t.1 + 1
11390 3 *p = t.2
11391 4 p = p - 1
11393 So, by specifying a separate post queue, it is possible
11394 to emit the post side-effects in the correct order.
11395 If POST_P is NULL, an internal queue will be used. Before
11396 returning to the caller, the sequence POST_P is appended to
11397 the main output sequence PRE_P.
11399 GIMPLE_TEST_F points to a function that takes a tree T and
11400 returns nonzero if T is in the GIMPLE form requested by the
11401 caller. The GIMPLE predicates are in gimple.c.
11403 FALLBACK tells the function what sort of a temporary we want if
11404 gimplification cannot produce an expression that complies with
11405 GIMPLE_TEST_F.
11407 fb_none means that no temporary should be generated
11408 fb_rvalue means that an rvalue is OK to generate
11409 fb_lvalue means that an lvalue is OK to generate
11410 fb_either means that either is OK, but an lvalue is preferable.
11411 fb_mayfail means that gimplification may fail (in which case
11412 GS_ERROR will be returned)
11414 The return value is either GS_ERROR or GS_ALL_DONE, since this
11415 function iterates until EXPR is completely gimplified or an error
11416 occurs. */
11418 enum gimplify_status
11419 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
11420 bool (*gimple_test_f) (tree), fallback_t fallback)
11422 tree tmp;
11423 gimple_seq internal_pre = NULL;
11424 gimple_seq internal_post = NULL;
11425 tree save_expr;
11426 bool is_statement;
11427 location_t saved_location;
11428 enum gimplify_status ret;
11429 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
11430 tree label;
11432 save_expr = *expr_p;
11433 if (save_expr == NULL_TREE)
11434 return GS_ALL_DONE;
11436 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
11437 is_statement = gimple_test_f == is_gimple_stmt;
11438 if (is_statement)
11439 gcc_assert (pre_p);
11441 /* Consistency checks. */
11442 if (gimple_test_f == is_gimple_reg)
11443 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
11444 else if (gimple_test_f == is_gimple_val
11445 || gimple_test_f == is_gimple_call_addr
11446 || gimple_test_f == is_gimple_condexpr
11447 || gimple_test_f == is_gimple_mem_rhs
11448 || gimple_test_f == is_gimple_mem_rhs_or_call
11449 || gimple_test_f == is_gimple_reg_rhs
11450 || gimple_test_f == is_gimple_reg_rhs_or_call
11451 || gimple_test_f == is_gimple_asm_val
11452 || gimple_test_f == is_gimple_mem_ref_addr)
11453 gcc_assert (fallback & fb_rvalue);
11454 else if (gimple_test_f == is_gimple_min_lval
11455 || gimple_test_f == is_gimple_lvalue)
11456 gcc_assert (fallback & fb_lvalue);
11457 else if (gimple_test_f == is_gimple_addressable)
11458 gcc_assert (fallback & fb_either);
11459 else if (gimple_test_f == is_gimple_stmt)
11460 gcc_assert (fallback == fb_none);
11461 else
11463 /* We should have recognized the GIMPLE_TEST_F predicate to
11464 know what kind of fallback to use in case a temporary is
11465 needed to hold the value or address of *EXPR_P. */
11466 gcc_unreachable ();
11469 /* We used to check the predicate here and return immediately if it
11470 succeeds. This is wrong; the design is for gimplification to be
11471 idempotent, and for the predicates to only test for valid forms, not
11472 whether they are fully simplified. */
11473 if (pre_p == NULL)
11474 pre_p = &internal_pre;
11476 if (post_p == NULL)
11477 post_p = &internal_post;
11479 /* Remember the last statements added to PRE_P and POST_P. Every
11480 new statement added by the gimplification helpers needs to be
11481 annotated with location information. To centralize the
11482 responsibility, we remember the last statement that had been
11483 added to both queues before gimplifying *EXPR_P. If
11484 gimplification produces new statements in PRE_P and POST_P, those
11485 statements will be annotated with the same location information
11486 as *EXPR_P. */
11487 pre_last_gsi = gsi_last (*pre_p);
11488 post_last_gsi = gsi_last (*post_p);
11490 saved_location = input_location;
11491 if (save_expr != error_mark_node
11492 && EXPR_HAS_LOCATION (*expr_p))
11493 input_location = EXPR_LOCATION (*expr_p);
11495 /* Loop over the specific gimplifiers until the toplevel node
11496 remains the same. */
11499 /* Strip away as many useless type conversions as possible
11500 at the toplevel. */
11501 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
11503 /* Remember the expr. */
11504 save_expr = *expr_p;
11506 /* Die, die, die, my darling. */
11507 if (error_operand_p (save_expr))
11509 ret = GS_ERROR;
11510 break;
11513 /* Do any language-specific gimplification. */
11514 ret = ((enum gimplify_status)
11515 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
11516 if (ret == GS_OK)
11518 if (*expr_p == NULL_TREE)
11519 break;
11520 if (*expr_p != save_expr)
11521 continue;
11523 else if (ret != GS_UNHANDLED)
11524 break;
11526 /* Make sure that all the cases set 'ret' appropriately. */
11527 ret = GS_UNHANDLED;
11528 switch (TREE_CODE (*expr_p))
11530 /* First deal with the special cases. */
11532 case POSTINCREMENT_EXPR:
11533 case POSTDECREMENT_EXPR:
11534 case PREINCREMENT_EXPR:
11535 case PREDECREMENT_EXPR:
11536 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
11537 fallback != fb_none,
11538 TREE_TYPE (*expr_p));
11539 break;
11541 case VIEW_CONVERT_EXPR:
11542 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
11543 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
11545 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11546 post_p, is_gimple_val, fb_rvalue);
11547 recalculate_side_effects (*expr_p);
11548 break;
11550 /* Fallthru. */
11552 case ARRAY_REF:
11553 case ARRAY_RANGE_REF:
11554 case REALPART_EXPR:
11555 case IMAGPART_EXPR:
11556 case COMPONENT_REF:
11557 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
11558 fallback ? fallback : fb_rvalue);
11559 break;
11561 case COND_EXPR:
11562 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
11564 /* C99 code may assign to an array in a structure value of a
11565 conditional expression, and this has undefined behavior
11566 only on execution, so create a temporary if an lvalue is
11567 required. */
11568 if (fallback == fb_lvalue)
11570 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11571 mark_addressable (*expr_p);
11572 ret = GS_OK;
11574 break;
11576 case CALL_EXPR:
11577 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
11579 /* C99 code may assign to an array in a structure returned
11580 from a function, and this has undefined behavior only on
11581 execution, so create a temporary if an lvalue is
11582 required. */
11583 if (fallback == fb_lvalue)
11585 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11586 mark_addressable (*expr_p);
11587 ret = GS_OK;
11589 break;
11591 case TREE_LIST:
11592 gcc_unreachable ();
11594 case COMPOUND_EXPR:
11595 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
11596 break;
11598 case COMPOUND_LITERAL_EXPR:
11599 ret = gimplify_compound_literal_expr (expr_p, pre_p,
11600 gimple_test_f, fallback);
11601 break;
11603 case MODIFY_EXPR:
11604 case INIT_EXPR:
11605 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
11606 fallback != fb_none);
11607 break;
11609 case TRUTH_ANDIF_EXPR:
11610 case TRUTH_ORIF_EXPR:
11612 /* Preserve the original type of the expression and the
11613 source location of the outer expression. */
11614 tree org_type = TREE_TYPE (*expr_p);
11615 *expr_p = gimple_boolify (*expr_p);
11616 *expr_p = build3_loc (input_location, COND_EXPR,
11617 org_type, *expr_p,
11618 fold_convert_loc
11619 (input_location,
11620 org_type, boolean_true_node),
11621 fold_convert_loc
11622 (input_location,
11623 org_type, boolean_false_node));
11624 ret = GS_OK;
11625 break;
11628 case TRUTH_NOT_EXPR:
11630 tree type = TREE_TYPE (*expr_p);
11631 /* The parsers are careful to generate TRUTH_NOT_EXPR
11632 only with operands that are always zero or one.
11633 We do not fold here but handle the only interesting case
11634 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
11635 *expr_p = gimple_boolify (*expr_p);
11636 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
11637 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
11638 TREE_TYPE (*expr_p),
11639 TREE_OPERAND (*expr_p, 0));
11640 else
11641 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
11642 TREE_TYPE (*expr_p),
11643 TREE_OPERAND (*expr_p, 0),
11644 build_int_cst (TREE_TYPE (*expr_p), 1));
11645 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
11646 *expr_p = fold_convert_loc (input_location, type, *expr_p);
11647 ret = GS_OK;
11648 break;
11651 case ADDR_EXPR:
11652 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
11653 break;
11655 case ANNOTATE_EXPR:
11657 tree cond = TREE_OPERAND (*expr_p, 0);
11658 tree kind = TREE_OPERAND (*expr_p, 1);
11659 tree data = TREE_OPERAND (*expr_p, 2);
11660 tree type = TREE_TYPE (cond);
11661 if (!INTEGRAL_TYPE_P (type))
11663 *expr_p = cond;
11664 ret = GS_OK;
11665 break;
11667 tree tmp = create_tmp_var (type);
11668 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
11669 gcall *call
11670 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
11671 gimple_call_set_lhs (call, tmp);
11672 gimplify_seq_add_stmt (pre_p, call);
11673 *expr_p = tmp;
11674 ret = GS_ALL_DONE;
11675 break;
11678 case VA_ARG_EXPR:
11679 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
11680 break;
11682 CASE_CONVERT:
11683 if (IS_EMPTY_STMT (*expr_p))
11685 ret = GS_ALL_DONE;
11686 break;
11689 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
11690 || fallback == fb_none)
11692 /* Just strip a conversion to void (or in void context) and
11693 try again. */
11694 *expr_p = TREE_OPERAND (*expr_p, 0);
11695 ret = GS_OK;
11696 break;
11699 ret = gimplify_conversion (expr_p);
11700 if (ret == GS_ERROR)
11701 break;
11702 if (*expr_p != save_expr)
11703 break;
11704 /* FALLTHRU */
11706 case FIX_TRUNC_EXPR:
11707 /* unary_expr: ... | '(' cast ')' val | ... */
11708 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11709 is_gimple_val, fb_rvalue);
11710 recalculate_side_effects (*expr_p);
11711 break;
11713 case INDIRECT_REF:
11715 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
11716 bool notrap = TREE_THIS_NOTRAP (*expr_p);
11717 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
11719 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
11720 if (*expr_p != save_expr)
11722 ret = GS_OK;
11723 break;
11726 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11727 is_gimple_reg, fb_rvalue);
11728 if (ret == GS_ERROR)
11729 break;
11731 recalculate_side_effects (*expr_p);
11732 *expr_p = fold_build2_loc (input_location, MEM_REF,
11733 TREE_TYPE (*expr_p),
11734 TREE_OPERAND (*expr_p, 0),
11735 build_int_cst (saved_ptr_type, 0));
11736 TREE_THIS_VOLATILE (*expr_p) = volatilep;
11737 TREE_THIS_NOTRAP (*expr_p) = notrap;
11738 ret = GS_OK;
11739 break;
11742 /* We arrive here through the various re-gimplifcation paths. */
11743 case MEM_REF:
11744 /* First try re-folding the whole thing. */
11745 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
11746 TREE_OPERAND (*expr_p, 0),
11747 TREE_OPERAND (*expr_p, 1));
11748 if (tmp)
11750 REF_REVERSE_STORAGE_ORDER (tmp)
11751 = REF_REVERSE_STORAGE_ORDER (*expr_p);
11752 *expr_p = tmp;
11753 recalculate_side_effects (*expr_p);
11754 ret = GS_OK;
11755 break;
11757 /* Avoid re-gimplifying the address operand if it is already
11758 in suitable form. Re-gimplifying would mark the address
11759 operand addressable. Always gimplify when not in SSA form
11760 as we still may have to gimplify decls with value-exprs. */
11761 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
11762 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
11764 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11765 is_gimple_mem_ref_addr, fb_rvalue);
11766 if (ret == GS_ERROR)
11767 break;
11769 recalculate_side_effects (*expr_p);
11770 ret = GS_ALL_DONE;
11771 break;
11773 /* Constants need not be gimplified. */
11774 case INTEGER_CST:
11775 case REAL_CST:
11776 case FIXED_CST:
11777 case STRING_CST:
11778 case COMPLEX_CST:
11779 case VECTOR_CST:
11780 /* Drop the overflow flag on constants, we do not want
11781 that in the GIMPLE IL. */
11782 if (TREE_OVERFLOW_P (*expr_p))
11783 *expr_p = drop_tree_overflow (*expr_p);
11784 ret = GS_ALL_DONE;
11785 break;
11787 case CONST_DECL:
11788 /* If we require an lvalue, such as for ADDR_EXPR, retain the
11789 CONST_DECL node. Otherwise the decl is replaceable by its
11790 value. */
11791 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
11792 if (fallback & fb_lvalue)
11793 ret = GS_ALL_DONE;
11794 else
11796 *expr_p = DECL_INITIAL (*expr_p);
11797 ret = GS_OK;
11799 break;
11801 case DECL_EXPR:
11802 ret = gimplify_decl_expr (expr_p, pre_p);
11803 break;
11805 case BIND_EXPR:
11806 ret = gimplify_bind_expr (expr_p, pre_p);
11807 break;
11809 case LOOP_EXPR:
11810 ret = gimplify_loop_expr (expr_p, pre_p);
11811 break;
11813 case SWITCH_EXPR:
11814 ret = gimplify_switch_expr (expr_p, pre_p);
11815 break;
11817 case EXIT_EXPR:
11818 ret = gimplify_exit_expr (expr_p);
11819 break;
11821 case GOTO_EXPR:
11822 /* If the target is not LABEL, then it is a computed jump
11823 and the target needs to be gimplified. */
11824 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
11826 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
11827 NULL, is_gimple_val, fb_rvalue);
11828 if (ret == GS_ERROR)
11829 break;
11831 gimplify_seq_add_stmt (pre_p,
11832 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
11833 ret = GS_ALL_DONE;
11834 break;
11836 case PREDICT_EXPR:
11837 gimplify_seq_add_stmt (pre_p,
11838 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
11839 PREDICT_EXPR_OUTCOME (*expr_p)));
11840 ret = GS_ALL_DONE;
11841 break;
11843 case LABEL_EXPR:
11844 ret = gimplify_label_expr (expr_p, pre_p);
11845 label = LABEL_EXPR_LABEL (*expr_p);
11846 gcc_assert (decl_function_context (label) == current_function_decl);
11848 /* If the label is used in a goto statement, or address of the label
11849 is taken, we need to unpoison all variables that were seen so far.
11850 Doing so would prevent us from reporting a false positives. */
11851 if (asan_poisoned_variables
11852 && asan_used_labels != NULL
11853 && asan_used_labels->contains (label))
11854 asan_poison_variables (asan_poisoned_variables, false, pre_p);
11855 break;
11857 case CASE_LABEL_EXPR:
11858 ret = gimplify_case_label_expr (expr_p, pre_p);
11860 if (gimplify_ctxp->live_switch_vars)
11861 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
11862 pre_p);
11863 break;
11865 case RETURN_EXPR:
11866 ret = gimplify_return_expr (*expr_p, pre_p);
11867 break;
11869 case CONSTRUCTOR:
11870 /* Don't reduce this in place; let gimplify_init_constructor work its
11871 magic. Buf if we're just elaborating this for side effects, just
11872 gimplify any element that has side-effects. */
11873 if (fallback == fb_none)
11875 unsigned HOST_WIDE_INT ix;
11876 tree val;
11877 tree temp = NULL_TREE;
11878 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
11879 if (TREE_SIDE_EFFECTS (val))
11880 append_to_statement_list (val, &temp);
11882 *expr_p = temp;
11883 ret = temp ? GS_OK : GS_ALL_DONE;
11885 /* C99 code may assign to an array in a constructed
11886 structure or union, and this has undefined behavior only
11887 on execution, so create a temporary if an lvalue is
11888 required. */
11889 else if (fallback == fb_lvalue)
11891 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11892 mark_addressable (*expr_p);
11893 ret = GS_OK;
11895 else
11896 ret = GS_ALL_DONE;
11897 break;
11899 /* The following are special cases that are not handled by the
11900 original GIMPLE grammar. */
11902 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11903 eliminated. */
11904 case SAVE_EXPR:
11905 ret = gimplify_save_expr (expr_p, pre_p, post_p);
11906 break;
11908 case BIT_FIELD_REF:
11909 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11910 post_p, is_gimple_lvalue, fb_either);
11911 recalculate_side_effects (*expr_p);
11912 break;
11914 case TARGET_MEM_REF:
11916 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
11918 if (TMR_BASE (*expr_p))
11919 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
11920 post_p, is_gimple_mem_ref_addr, fb_either);
11921 if (TMR_INDEX (*expr_p))
11922 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
11923 post_p, is_gimple_val, fb_rvalue);
11924 if (TMR_INDEX2 (*expr_p))
11925 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
11926 post_p, is_gimple_val, fb_rvalue);
11927 /* TMR_STEP and TMR_OFFSET are always integer constants. */
11928 ret = MIN (r0, r1);
11930 break;
11932 case NON_LVALUE_EXPR:
11933 /* This should have been stripped above. */
11934 gcc_unreachable ();
11936 case ASM_EXPR:
11937 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
11938 break;
11940 case TRY_FINALLY_EXPR:
11941 case TRY_CATCH_EXPR:
11943 gimple_seq eval, cleanup;
11944 gtry *try_;
11946 /* Calls to destructors are generated automatically in FINALLY/CATCH
11947 block. They should have location as UNKNOWN_LOCATION. However,
11948 gimplify_call_expr will reset these call stmts to input_location
11949 if it finds stmt's location is unknown. To prevent resetting for
11950 destructors, we set the input_location to unknown.
11951 Note that this only affects the destructor calls in FINALLY/CATCH
11952 block, and will automatically reset to its original value by the
11953 end of gimplify_expr. */
11954 input_location = UNKNOWN_LOCATION;
11955 eval = cleanup = NULL;
11956 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
11957 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
11958 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
11959 if (gimple_seq_empty_p (cleanup))
11961 gimple_seq_add_seq (pre_p, eval);
11962 ret = GS_ALL_DONE;
11963 break;
11965 try_ = gimple_build_try (eval, cleanup,
11966 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
11967 ? GIMPLE_TRY_FINALLY
11968 : GIMPLE_TRY_CATCH);
11969 if (EXPR_HAS_LOCATION (save_expr))
11970 gimple_set_location (try_, EXPR_LOCATION (save_expr));
11971 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
11972 gimple_set_location (try_, saved_location);
11973 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
11974 gimple_try_set_catch_is_cleanup (try_,
11975 TRY_CATCH_IS_CLEANUP (*expr_p));
11976 gimplify_seq_add_stmt (pre_p, try_);
11977 ret = GS_ALL_DONE;
11978 break;
11981 case CLEANUP_POINT_EXPR:
11982 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
11983 break;
11985 case TARGET_EXPR:
11986 ret = gimplify_target_expr (expr_p, pre_p, post_p);
11987 break;
11989 case CATCH_EXPR:
11991 gimple *c;
11992 gimple_seq handler = NULL;
11993 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
11994 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
11995 gimplify_seq_add_stmt (pre_p, c);
11996 ret = GS_ALL_DONE;
11997 break;
12000 case EH_FILTER_EXPR:
12002 gimple *ehf;
12003 gimple_seq failure = NULL;
12005 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
12006 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
12007 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
12008 gimplify_seq_add_stmt (pre_p, ehf);
12009 ret = GS_ALL_DONE;
12010 break;
12013 case OBJ_TYPE_REF:
12015 enum gimplify_status r0, r1;
12016 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
12017 post_p, is_gimple_val, fb_rvalue);
12018 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
12019 post_p, is_gimple_val, fb_rvalue);
12020 TREE_SIDE_EFFECTS (*expr_p) = 0;
12021 ret = MIN (r0, r1);
12023 break;
12025 case LABEL_DECL:
12026 /* We get here when taking the address of a label. We mark
12027 the label as "forced"; meaning it can never be removed and
12028 it is a potential target for any computed goto. */
12029 FORCED_LABEL (*expr_p) = 1;
12030 ret = GS_ALL_DONE;
12031 break;
12033 case STATEMENT_LIST:
12034 ret = gimplify_statement_list (expr_p, pre_p);
12035 break;
12037 case WITH_SIZE_EXPR:
12039 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12040 post_p == &internal_post ? NULL : post_p,
12041 gimple_test_f, fallback);
12042 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12043 is_gimple_val, fb_rvalue);
12044 ret = GS_ALL_DONE;
12046 break;
12048 case VAR_DECL:
12049 case PARM_DECL:
12050 ret = gimplify_var_or_parm_decl (expr_p);
12051 break;
12053 case RESULT_DECL:
12054 /* When within an OMP context, notice uses of variables. */
12055 if (gimplify_omp_ctxp)
12056 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
12057 ret = GS_ALL_DONE;
12058 break;
12060 case DEBUG_EXPR_DECL:
12061 gcc_unreachable ();
12063 case DEBUG_BEGIN_STMT:
12064 gimplify_seq_add_stmt (pre_p,
12065 gimple_build_debug_begin_stmt
12066 (TREE_BLOCK (*expr_p),
12067 EXPR_LOCATION (*expr_p)));
12068 ret = GS_ALL_DONE;
12069 *expr_p = NULL;
12070 break;
12072 case SSA_NAME:
12073 /* Allow callbacks into the gimplifier during optimization. */
12074 ret = GS_ALL_DONE;
12075 break;
12077 case OMP_PARALLEL:
12078 gimplify_omp_parallel (expr_p, pre_p);
12079 ret = GS_ALL_DONE;
12080 break;
12082 case OMP_TASK:
12083 gimplify_omp_task (expr_p, pre_p);
12084 ret = GS_ALL_DONE;
12085 break;
12087 case OMP_FOR:
12088 case OMP_SIMD:
12089 case OMP_DISTRIBUTE:
12090 case OMP_TASKLOOP:
12091 case OACC_LOOP:
12092 ret = gimplify_omp_for (expr_p, pre_p);
12093 break;
12095 case OACC_CACHE:
12096 gimplify_oacc_cache (expr_p, pre_p);
12097 ret = GS_ALL_DONE;
12098 break;
12100 case OACC_DECLARE:
12101 gimplify_oacc_declare (expr_p, pre_p);
12102 ret = GS_ALL_DONE;
12103 break;
12105 case OACC_HOST_DATA:
12106 case OACC_DATA:
12107 case OACC_KERNELS:
12108 case OACC_PARALLEL:
12109 case OMP_SECTIONS:
12110 case OMP_SINGLE:
12111 case OMP_TARGET:
12112 case OMP_TARGET_DATA:
12113 case OMP_TEAMS:
12114 gimplify_omp_workshare (expr_p, pre_p);
12115 ret = GS_ALL_DONE;
12116 break;
12118 case OACC_ENTER_DATA:
12119 case OACC_EXIT_DATA:
12120 case OACC_UPDATE:
12121 case OMP_TARGET_UPDATE:
12122 case OMP_TARGET_ENTER_DATA:
12123 case OMP_TARGET_EXIT_DATA:
12124 gimplify_omp_target_update (expr_p, pre_p);
12125 ret = GS_ALL_DONE;
12126 break;
12128 case OMP_SECTION:
12129 case OMP_MASTER:
12130 case OMP_TASKGROUP:
12131 case OMP_ORDERED:
12132 case OMP_CRITICAL:
12134 gimple_seq body = NULL;
12135 gimple *g;
12137 gimplify_and_add (OMP_BODY (*expr_p), &body);
12138 switch (TREE_CODE (*expr_p))
12140 case OMP_SECTION:
12141 g = gimple_build_omp_section (body);
12142 break;
12143 case OMP_MASTER:
12144 g = gimple_build_omp_master (body);
12145 break;
12146 case OMP_TASKGROUP:
12148 gimple_seq cleanup = NULL;
12149 tree fn
12150 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
12151 g = gimple_build_call (fn, 0);
12152 gimple_seq_add_stmt (&cleanup, g);
12153 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
12154 body = NULL;
12155 gimple_seq_add_stmt (&body, g);
12156 g = gimple_build_omp_taskgroup (body);
12158 break;
12159 case OMP_ORDERED:
12160 g = gimplify_omp_ordered (*expr_p, body);
12161 break;
12162 case OMP_CRITICAL:
12163 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
12164 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
12165 gimplify_adjust_omp_clauses (pre_p, body,
12166 &OMP_CRITICAL_CLAUSES (*expr_p),
12167 OMP_CRITICAL);
12168 g = gimple_build_omp_critical (body,
12169 OMP_CRITICAL_NAME (*expr_p),
12170 OMP_CRITICAL_CLAUSES (*expr_p));
12171 break;
12172 default:
12173 gcc_unreachable ();
12175 gimplify_seq_add_stmt (pre_p, g);
12176 ret = GS_ALL_DONE;
12177 break;
12180 case OMP_ATOMIC:
12181 case OMP_ATOMIC_READ:
12182 case OMP_ATOMIC_CAPTURE_OLD:
12183 case OMP_ATOMIC_CAPTURE_NEW:
12184 ret = gimplify_omp_atomic (expr_p, pre_p);
12185 break;
12187 case TRANSACTION_EXPR:
12188 ret = gimplify_transaction (expr_p, pre_p);
12189 break;
12191 case TRUTH_AND_EXPR:
12192 case TRUTH_OR_EXPR:
12193 case TRUTH_XOR_EXPR:
12195 tree orig_type = TREE_TYPE (*expr_p);
12196 tree new_type, xop0, xop1;
12197 *expr_p = gimple_boolify (*expr_p);
12198 new_type = TREE_TYPE (*expr_p);
12199 if (!useless_type_conversion_p (orig_type, new_type))
12201 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
12202 ret = GS_OK;
12203 break;
12206 /* Boolified binary truth expressions are semantically equivalent
12207 to bitwise binary expressions. Canonicalize them to the
12208 bitwise variant. */
12209 switch (TREE_CODE (*expr_p))
12211 case TRUTH_AND_EXPR:
12212 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
12213 break;
12214 case TRUTH_OR_EXPR:
12215 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
12216 break;
12217 case TRUTH_XOR_EXPR:
12218 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
12219 break;
12220 default:
12221 break;
12223 /* Now make sure that operands have compatible type to
12224 expression's new_type. */
12225 xop0 = TREE_OPERAND (*expr_p, 0);
12226 xop1 = TREE_OPERAND (*expr_p, 1);
12227 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
12228 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
12229 new_type,
12230 xop0);
12231 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
12232 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
12233 new_type,
12234 xop1);
12235 /* Continue classified as tcc_binary. */
12236 goto expr_2;
12239 case VEC_COND_EXPR:
12241 enum gimplify_status r0, r1, r2;
12243 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12244 post_p, is_gimple_condexpr, fb_rvalue);
12245 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12246 post_p, is_gimple_val, fb_rvalue);
12247 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12248 post_p, is_gimple_val, fb_rvalue);
12250 ret = MIN (MIN (r0, r1), r2);
12251 recalculate_side_effects (*expr_p);
12253 break;
12255 case VEC_PERM_EXPR:
12256 /* Classified as tcc_expression. */
12257 goto expr_3;
12259 case BIT_INSERT_EXPR:
12260 /* Argument 3 is a constant. */
12261 goto expr_2;
12263 case POINTER_PLUS_EXPR:
12265 enum gimplify_status r0, r1;
12266 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12267 post_p, is_gimple_val, fb_rvalue);
12268 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12269 post_p, is_gimple_val, fb_rvalue);
12270 recalculate_side_effects (*expr_p);
12271 ret = MIN (r0, r1);
12272 break;
12275 default:
12276 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
12278 case tcc_comparison:
12279 /* Handle comparison of objects of non scalar mode aggregates
12280 with a call to memcmp. It would be nice to only have to do
12281 this for variable-sized objects, but then we'd have to allow
12282 the same nest of reference nodes we allow for MODIFY_EXPR and
12283 that's too complex.
12285 Compare scalar mode aggregates as scalar mode values. Using
12286 memcmp for them would be very inefficient at best, and is
12287 plain wrong if bitfields are involved. */
12289 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
12291 /* Vector comparisons need no boolification. */
12292 if (TREE_CODE (type) == VECTOR_TYPE)
12293 goto expr_2;
12294 else if (!AGGREGATE_TYPE_P (type))
12296 tree org_type = TREE_TYPE (*expr_p);
12297 *expr_p = gimple_boolify (*expr_p);
12298 if (!useless_type_conversion_p (org_type,
12299 TREE_TYPE (*expr_p)))
12301 *expr_p = fold_convert_loc (input_location,
12302 org_type, *expr_p);
12303 ret = GS_OK;
12305 else
12306 goto expr_2;
12308 else if (TYPE_MODE (type) != BLKmode)
12309 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
12310 else
12311 ret = gimplify_variable_sized_compare (expr_p);
12313 break;
12316 /* If *EXPR_P does not need to be special-cased, handle it
12317 according to its class. */
12318 case tcc_unary:
12319 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12320 post_p, is_gimple_val, fb_rvalue);
12321 break;
12323 case tcc_binary:
12324 expr_2:
12326 enum gimplify_status r0, r1;
12328 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12329 post_p, is_gimple_val, fb_rvalue);
12330 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12331 post_p, is_gimple_val, fb_rvalue);
12333 ret = MIN (r0, r1);
12334 break;
12337 expr_3:
12339 enum gimplify_status r0, r1, r2;
12341 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12342 post_p, is_gimple_val, fb_rvalue);
12343 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
12344 post_p, is_gimple_val, fb_rvalue);
12345 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
12346 post_p, is_gimple_val, fb_rvalue);
12348 ret = MIN (MIN (r0, r1), r2);
12349 break;
12352 case tcc_declaration:
12353 case tcc_constant:
12354 ret = GS_ALL_DONE;
12355 goto dont_recalculate;
12357 default:
12358 gcc_unreachable ();
12361 recalculate_side_effects (*expr_p);
12363 dont_recalculate:
12364 break;
12367 gcc_assert (*expr_p || ret != GS_OK);
12369 while (ret == GS_OK);
12371 /* If we encountered an error_mark somewhere nested inside, either
12372 stub out the statement or propagate the error back out. */
12373 if (ret == GS_ERROR)
12375 if (is_statement)
12376 *expr_p = NULL;
12377 goto out;
12380 /* This was only valid as a return value from the langhook, which
12381 we handled. Make sure it doesn't escape from any other context. */
12382 gcc_assert (ret != GS_UNHANDLED);
12384 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
12386 /* We aren't looking for a value, and we don't have a valid
12387 statement. If it doesn't have side-effects, throw it away.
12388 We can also get here with code such as "*&&L;", where L is
12389 a LABEL_DECL that is marked as FORCED_LABEL. */
12390 if (TREE_CODE (*expr_p) == LABEL_DECL
12391 || !TREE_SIDE_EFFECTS (*expr_p))
12392 *expr_p = NULL;
12393 else if (!TREE_THIS_VOLATILE (*expr_p))
12395 /* This is probably a _REF that contains something nested that
12396 has side effects. Recurse through the operands to find it. */
12397 enum tree_code code = TREE_CODE (*expr_p);
12399 switch (code)
12401 case COMPONENT_REF:
12402 case REALPART_EXPR:
12403 case IMAGPART_EXPR:
12404 case VIEW_CONVERT_EXPR:
12405 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12406 gimple_test_f, fallback);
12407 break;
12409 case ARRAY_REF:
12410 case ARRAY_RANGE_REF:
12411 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12412 gimple_test_f, fallback);
12413 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12414 gimple_test_f, fallback);
12415 break;
12417 default:
12418 /* Anything else with side-effects must be converted to
12419 a valid statement before we get here. */
12420 gcc_unreachable ();
12423 *expr_p = NULL;
12425 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
12426 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
12428 /* Historically, the compiler has treated a bare reference
12429 to a non-BLKmode volatile lvalue as forcing a load. */
12430 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
12432 /* Normally, we do not want to create a temporary for a
12433 TREE_ADDRESSABLE type because such a type should not be
12434 copied by bitwise-assignment. However, we make an
12435 exception here, as all we are doing here is ensuring that
12436 we read the bytes that make up the type. We use
12437 create_tmp_var_raw because create_tmp_var will abort when
12438 given a TREE_ADDRESSABLE type. */
12439 tree tmp = create_tmp_var_raw (type, "vol");
12440 gimple_add_tmp_var (tmp);
12441 gimplify_assign (tmp, *expr_p, pre_p);
12442 *expr_p = NULL;
12444 else
12445 /* We can't do anything useful with a volatile reference to
12446 an incomplete type, so just throw it away. Likewise for
12447 a BLKmode type, since any implicit inner load should
12448 already have been turned into an explicit one by the
12449 gimplification process. */
12450 *expr_p = NULL;
12453 /* If we are gimplifying at the statement level, we're done. Tack
12454 everything together and return. */
12455 if (fallback == fb_none || is_statement)
12457 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
12458 it out for GC to reclaim it. */
12459 *expr_p = NULL_TREE;
12461 if (!gimple_seq_empty_p (internal_pre)
12462 || !gimple_seq_empty_p (internal_post))
12464 gimplify_seq_add_seq (&internal_pre, internal_post);
12465 gimplify_seq_add_seq (pre_p, internal_pre);
12468 /* The result of gimplifying *EXPR_P is going to be the last few
12469 statements in *PRE_P and *POST_P. Add location information
12470 to all the statements that were added by the gimplification
12471 helpers. */
12472 if (!gimple_seq_empty_p (*pre_p))
12473 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
12475 if (!gimple_seq_empty_p (*post_p))
12476 annotate_all_with_location_after (*post_p, post_last_gsi,
12477 input_location);
12479 goto out;
12482 #ifdef ENABLE_GIMPLE_CHECKING
12483 if (*expr_p)
12485 enum tree_code code = TREE_CODE (*expr_p);
12486 /* These expressions should already be in gimple IR form. */
12487 gcc_assert (code != MODIFY_EXPR
12488 && code != ASM_EXPR
12489 && code != BIND_EXPR
12490 && code != CATCH_EXPR
12491 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
12492 && code != EH_FILTER_EXPR
12493 && code != GOTO_EXPR
12494 && code != LABEL_EXPR
12495 && code != LOOP_EXPR
12496 && code != SWITCH_EXPR
12497 && code != TRY_FINALLY_EXPR
12498 && code != OACC_PARALLEL
12499 && code != OACC_KERNELS
12500 && code != OACC_DATA
12501 && code != OACC_HOST_DATA
12502 && code != OACC_DECLARE
12503 && code != OACC_UPDATE
12504 && code != OACC_ENTER_DATA
12505 && code != OACC_EXIT_DATA
12506 && code != OACC_CACHE
12507 && code != OMP_CRITICAL
12508 && code != OMP_FOR
12509 && code != OACC_LOOP
12510 && code != OMP_MASTER
12511 && code != OMP_TASKGROUP
12512 && code != OMP_ORDERED
12513 && code != OMP_PARALLEL
12514 && code != OMP_SECTIONS
12515 && code != OMP_SECTION
12516 && code != OMP_SINGLE);
12518 #endif
12520 /* Otherwise we're gimplifying a subexpression, so the resulting
12521 value is interesting. If it's a valid operand that matches
12522 GIMPLE_TEST_F, we're done. Unless we are handling some
12523 post-effects internally; if that's the case, we need to copy into
12524 a temporary before adding the post-effects to POST_P. */
12525 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
12526 goto out;
12528 /* Otherwise, we need to create a new temporary for the gimplified
12529 expression. */
12531 /* We can't return an lvalue if we have an internal postqueue. The
12532 object the lvalue refers to would (probably) be modified by the
12533 postqueue; we need to copy the value out first, which means an
12534 rvalue. */
12535 if ((fallback & fb_lvalue)
12536 && gimple_seq_empty_p (internal_post)
12537 && is_gimple_addressable (*expr_p))
12539 /* An lvalue will do. Take the address of the expression, store it
12540 in a temporary, and replace the expression with an INDIRECT_REF of
12541 that temporary. */
12542 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
12543 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
12544 *expr_p = build_simple_mem_ref (tmp);
12546 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
12548 /* An rvalue will do. Assign the gimplified expression into a
12549 new temporary TMP and replace the original expression with
12550 TMP. First, make sure that the expression has a type so that
12551 it can be assigned into a temporary. */
12552 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
12553 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
12555 else
12557 #ifdef ENABLE_GIMPLE_CHECKING
12558 if (!(fallback & fb_mayfail))
12560 fprintf (stderr, "gimplification failed:\n");
12561 print_generic_expr (stderr, *expr_p);
12562 debug_tree (*expr_p);
12563 internal_error ("gimplification failed");
12565 #endif
12566 gcc_assert (fallback & fb_mayfail);
12568 /* If this is an asm statement, and the user asked for the
12569 impossible, don't die. Fail and let gimplify_asm_expr
12570 issue an error. */
12571 ret = GS_ERROR;
12572 goto out;
12575 /* Make sure the temporary matches our predicate. */
12576 gcc_assert ((*gimple_test_f) (*expr_p));
12578 if (!gimple_seq_empty_p (internal_post))
12580 annotate_all_with_location (internal_post, input_location);
12581 gimplify_seq_add_seq (pre_p, internal_post);
12584 out:
12585 input_location = saved_location;
12586 return ret;
12589 /* Like gimplify_expr but make sure the gimplified result is not itself
12590 a SSA name (but a decl if it were). Temporaries required by
12591 evaluating *EXPR_P may be still SSA names. */
12593 static enum gimplify_status
12594 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12595 bool (*gimple_test_f) (tree), fallback_t fallback,
12596 bool allow_ssa)
12598 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
12599 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
12600 gimple_test_f, fallback);
12601 if (! allow_ssa
12602 && TREE_CODE (*expr_p) == SSA_NAME)
12604 tree name = *expr_p;
12605 if (was_ssa_name_p)
12606 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
12607 else
12609 /* Avoid the extra copy if possible. */
12610 *expr_p = create_tmp_reg (TREE_TYPE (name));
12611 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
12612 release_ssa_name (name);
12615 return ret;
12618 /* Look through TYPE for variable-sized objects and gimplify each such
12619 size that we find. Add to LIST_P any statements generated. */
12621 void
12622 gimplify_type_sizes (tree type, gimple_seq *list_p)
12624 tree field, t;
12626 if (type == NULL || type == error_mark_node)
12627 return;
12629 /* We first do the main variant, then copy into any other variants. */
12630 type = TYPE_MAIN_VARIANT (type);
12632 /* Avoid infinite recursion. */
12633 if (TYPE_SIZES_GIMPLIFIED (type))
12634 return;
12636 TYPE_SIZES_GIMPLIFIED (type) = 1;
12638 switch (TREE_CODE (type))
12640 case INTEGER_TYPE:
12641 case ENUMERAL_TYPE:
12642 case BOOLEAN_TYPE:
12643 case REAL_TYPE:
12644 case FIXED_POINT_TYPE:
12645 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
12646 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
12648 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12650 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
12651 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
12653 break;
12655 case ARRAY_TYPE:
12656 /* These types may not have declarations, so handle them here. */
12657 gimplify_type_sizes (TREE_TYPE (type), list_p);
12658 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
12659 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12660 with assigned stack slots, for -O1+ -g they should be tracked
12661 by VTA. */
12662 if (!(TYPE_NAME (type)
12663 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12664 && DECL_IGNORED_P (TYPE_NAME (type)))
12665 && TYPE_DOMAIN (type)
12666 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
12668 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
12669 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12670 DECL_IGNORED_P (t) = 0;
12671 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12672 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12673 DECL_IGNORED_P (t) = 0;
12675 break;
12677 case RECORD_TYPE:
12678 case UNION_TYPE:
12679 case QUAL_UNION_TYPE:
12680 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12681 if (TREE_CODE (field) == FIELD_DECL)
12683 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
12684 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
12685 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
12686 gimplify_type_sizes (TREE_TYPE (field), list_p);
12688 break;
12690 case POINTER_TYPE:
12691 case REFERENCE_TYPE:
12692 /* We used to recurse on the pointed-to type here, which turned out to
12693 be incorrect because its definition might refer to variables not
12694 yet initialized at this point if a forward declaration is involved.
12696 It was actually useful for anonymous pointed-to types to ensure
12697 that the sizes evaluation dominates every possible later use of the
12698 values. Restricting to such types here would be safe since there
12699 is no possible forward declaration around, but would introduce an
12700 undesirable middle-end semantic to anonymity. We then defer to
12701 front-ends the responsibility of ensuring that the sizes are
12702 evaluated both early and late enough, e.g. by attaching artificial
12703 type declarations to the tree. */
12704 break;
12706 default:
12707 break;
12710 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
12711 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
12713 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12715 TYPE_SIZE (t) = TYPE_SIZE (type);
12716 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
12717 TYPE_SIZES_GIMPLIFIED (t) = 1;
12721 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12722 a size or position, has had all of its SAVE_EXPRs evaluated.
12723 We add any required statements to *STMT_P. */
12725 void
12726 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
12728 tree expr = *expr_p;
12730 /* We don't do anything if the value isn't there, is constant, or contains
12731 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
12732 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
12733 will want to replace it with a new variable, but that will cause problems
12734 if this type is from outside the function. It's OK to have that here. */
12735 if (expr == NULL_TREE
12736 || is_gimple_constant (expr)
12737 || TREE_CODE (expr) == VAR_DECL
12738 || CONTAINS_PLACEHOLDER_P (expr))
12739 return;
12741 *expr_p = unshare_expr (expr);
12743 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12744 if the def vanishes. */
12745 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
12747 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
12748 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
12749 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
12750 if (is_gimple_constant (*expr_p))
12751 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
12754 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12755 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
12756 is true, also gimplify the parameters. */
12758 gbind *
12759 gimplify_body (tree fndecl, bool do_parms)
12761 location_t saved_location = input_location;
12762 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
12763 gimple *outer_stmt;
12764 gbind *outer_bind;
12766 timevar_push (TV_TREE_GIMPLIFY);
12768 init_tree_ssa (cfun);
12770 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12771 gimplification. */
12772 default_rtl_profile ();
12774 gcc_assert (gimplify_ctxp == NULL);
12775 push_gimplify_context (true);
12777 if (flag_openacc || flag_openmp)
12779 gcc_assert (gimplify_omp_ctxp == NULL);
12780 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
12781 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
12784 /* Unshare most shared trees in the body and in that of any nested functions.
12785 It would seem we don't have to do this for nested functions because
12786 they are supposed to be output and then the outer function gimplified
12787 first, but the g++ front end doesn't always do it that way. */
12788 unshare_body (fndecl);
12789 unvisit_body (fndecl);
12791 /* Make sure input_location isn't set to something weird. */
12792 input_location = DECL_SOURCE_LOCATION (fndecl);
12794 /* Resolve callee-copies. This has to be done before processing
12795 the body so that DECL_VALUE_EXPR gets processed correctly. */
12796 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
12798 /* Gimplify the function's body. */
12799 seq = NULL;
12800 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
12801 outer_stmt = gimple_seq_first_stmt (seq);
12802 if (!outer_stmt)
12804 outer_stmt = gimple_build_nop ();
12805 gimplify_seq_add_stmt (&seq, outer_stmt);
12808 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
12809 not the case, wrap everything in a GIMPLE_BIND to make it so. */
12810 if (gimple_code (outer_stmt) == GIMPLE_BIND
12811 && gimple_seq_first (seq) == gimple_seq_last (seq))
12812 outer_bind = as_a <gbind *> (outer_stmt);
12813 else
12814 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
12816 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12818 /* If we had callee-copies statements, insert them at the beginning
12819 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
12820 if (!gimple_seq_empty_p (parm_stmts))
12822 tree parm;
12824 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
12825 if (parm_cleanup)
12827 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
12828 GIMPLE_TRY_FINALLY);
12829 parm_stmts = NULL;
12830 gimple_seq_add_stmt (&parm_stmts, g);
12832 gimple_bind_set_body (outer_bind, parm_stmts);
12834 for (parm = DECL_ARGUMENTS (current_function_decl);
12835 parm; parm = DECL_CHAIN (parm))
12836 if (DECL_HAS_VALUE_EXPR_P (parm))
12838 DECL_HAS_VALUE_EXPR_P (parm) = 0;
12839 DECL_IGNORED_P (parm) = 0;
12843 if ((flag_openacc || flag_openmp || flag_openmp_simd)
12844 && gimplify_omp_ctxp)
12846 delete_omp_context (gimplify_omp_ctxp);
12847 gimplify_omp_ctxp = NULL;
12850 pop_gimplify_context (outer_bind);
12851 gcc_assert (gimplify_ctxp == NULL);
12853 if (flag_checking && !seen_error ())
12854 verify_gimple_in_seq (gimple_bind_body (outer_bind));
12856 timevar_pop (TV_TREE_GIMPLIFY);
12857 input_location = saved_location;
12859 return outer_bind;
12862 typedef char *char_p; /* For DEF_VEC_P. */
12864 /* Return whether we should exclude FNDECL from instrumentation. */
12866 static bool
12867 flag_instrument_functions_exclude_p (tree fndecl)
12869 vec<char_p> *v;
12871 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
12872 if (v && v->length () > 0)
12874 const char *name;
12875 int i;
12876 char *s;
12878 name = lang_hooks.decl_printable_name (fndecl, 0);
12879 FOR_EACH_VEC_ELT (*v, i, s)
12880 if (strstr (name, s) != NULL)
12881 return true;
12884 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
12885 if (v && v->length () > 0)
12887 const char *name;
12888 int i;
12889 char *s;
12891 name = DECL_SOURCE_FILE (fndecl);
12892 FOR_EACH_VEC_ELT (*v, i, s)
12893 if (strstr (name, s) != NULL)
12894 return true;
12897 return false;
12900 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
12901 node for the function we want to gimplify.
12903 Return the sequence of GIMPLE statements corresponding to the body
12904 of FNDECL. */
12906 void
12907 gimplify_function_tree (tree fndecl)
12909 tree parm, ret;
12910 gimple_seq seq;
12911 gbind *bind;
12913 gcc_assert (!gimple_body (fndecl));
12915 if (DECL_STRUCT_FUNCTION (fndecl))
12916 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
12917 else
12918 push_struct_function (fndecl);
12920 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12921 if necessary. */
12922 cfun->curr_properties |= PROP_gimple_lva;
12924 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
12926 /* Preliminarily mark non-addressed complex variables as eligible
12927 for promotion to gimple registers. We'll transform their uses
12928 as we find them. */
12929 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
12930 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
12931 && !TREE_THIS_VOLATILE (parm)
12932 && !needs_to_live_in_memory (parm))
12933 DECL_GIMPLE_REG_P (parm) = 1;
12936 ret = DECL_RESULT (fndecl);
12937 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
12938 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
12939 && !needs_to_live_in_memory (ret))
12940 DECL_GIMPLE_REG_P (ret) = 1;
12942 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
12943 asan_poisoned_variables = new hash_set<tree> ();
12944 bind = gimplify_body (fndecl, true);
12945 if (asan_poisoned_variables)
12947 delete asan_poisoned_variables;
12948 asan_poisoned_variables = NULL;
12951 /* The tree body of the function is no longer needed, replace it
12952 with the new GIMPLE body. */
12953 seq = NULL;
12954 gimple_seq_add_stmt (&seq, bind);
12955 gimple_set_body (fndecl, seq);
12957 /* If we're instrumenting function entry/exit, then prepend the call to
12958 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12959 catch the exit hook. */
12960 /* ??? Add some way to ignore exceptions for this TFE. */
12961 if (flag_instrument_function_entry_exit
12962 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
12963 /* Do not instrument extern inline functions. */
12964 && !(DECL_DECLARED_INLINE_P (fndecl)
12965 && DECL_EXTERNAL (fndecl)
12966 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
12967 && !flag_instrument_functions_exclude_p (fndecl))
12969 tree x;
12970 gbind *new_bind;
12971 gimple *tf;
12972 gimple_seq cleanup = NULL, body = NULL;
12973 tree tmp_var, this_fn_addr;
12974 gcall *call;
12976 /* The instrumentation hooks aren't going to call the instrumented
12977 function and the address they receive is expected to be matchable
12978 against symbol addresses. Make sure we don't create a trampoline,
12979 in case the current function is nested. */
12980 this_fn_addr = build_fold_addr_expr (current_function_decl);
12981 TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
12983 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12984 call = gimple_build_call (x, 1, integer_zero_node);
12985 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12986 gimple_call_set_lhs (call, tmp_var);
12987 gimplify_seq_add_stmt (&cleanup, call);
12988 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
12989 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
12990 gimplify_seq_add_stmt (&cleanup, call);
12991 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
12993 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12994 call = gimple_build_call (x, 1, integer_zero_node);
12995 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12996 gimple_call_set_lhs (call, tmp_var);
12997 gimplify_seq_add_stmt (&body, call);
12998 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
12999 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
13000 gimplify_seq_add_stmt (&body, call);
13001 gimplify_seq_add_stmt (&body, tf);
13002 new_bind = gimple_build_bind (NULL, body, NULL);
13004 /* Replace the current function body with the body
13005 wrapped in the try/finally TF. */
13006 seq = NULL;
13007 gimple_seq_add_stmt (&seq, new_bind);
13008 gimple_set_body (fndecl, seq);
13009 bind = new_bind;
13012 if (sanitize_flags_p (SANITIZE_THREAD))
13014 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
13015 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
13016 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
13017 /* Replace the current function body with the body
13018 wrapped in the try/finally TF. */
13019 seq = NULL;
13020 gimple_seq_add_stmt (&seq, new_bind);
13021 gimple_set_body (fndecl, seq);
13024 DECL_SAVED_TREE (fndecl) = NULL_TREE;
13025 cfun->curr_properties |= PROP_gimple_any;
13027 pop_cfun ();
13029 dump_function (TDI_gimple, fndecl);
13032 /* Return a dummy expression of type TYPE in order to keep going after an
13033 error. */
13035 static tree
13036 dummy_object (tree type)
13038 tree t = build_int_cst (build_pointer_type (type), 0);
13039 return build2 (MEM_REF, type, t, t);
13042 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
13043 builtin function, but a very special sort of operator. */
13045 enum gimplify_status
13046 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
13047 gimple_seq *post_p ATTRIBUTE_UNUSED)
13049 tree promoted_type, have_va_type;
13050 tree valist = TREE_OPERAND (*expr_p, 0);
13051 tree type = TREE_TYPE (*expr_p);
13052 tree t, tag, aptag;
13053 location_t loc = EXPR_LOCATION (*expr_p);
13055 /* Verify that valist is of the proper type. */
13056 have_va_type = TREE_TYPE (valist);
13057 if (have_va_type == error_mark_node)
13058 return GS_ERROR;
13059 have_va_type = targetm.canonical_va_list_type (have_va_type);
13060 if (have_va_type == NULL_TREE
13061 && POINTER_TYPE_P (TREE_TYPE (valist)))
13062 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
13063 have_va_type
13064 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
13065 gcc_assert (have_va_type != NULL_TREE);
13067 /* Generate a diagnostic for requesting data of a type that cannot
13068 be passed through `...' due to type promotion at the call site. */
13069 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
13070 != type)
13072 static bool gave_help;
13073 bool warned;
13074 /* Use the expansion point to handle cases such as passing bool (defined
13075 in a system header) through `...'. */
13076 source_location xloc
13077 = expansion_point_location_if_in_system_header (loc);
13079 /* Unfortunately, this is merely undefined, rather than a constraint
13080 violation, so we cannot make this an error. If this call is never
13081 executed, the program is still strictly conforming. */
13082 warned = warning_at (xloc, 0,
13083 "%qT is promoted to %qT when passed through %<...%>",
13084 type, promoted_type);
13085 if (!gave_help && warned)
13087 gave_help = true;
13088 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
13089 promoted_type, type);
13092 /* We can, however, treat "undefined" any way we please.
13093 Call abort to encourage the user to fix the program. */
13094 if (warned)
13095 inform (xloc, "if this code is reached, the program will abort");
13096 /* Before the abort, allow the evaluation of the va_list
13097 expression to exit or longjmp. */
13098 gimplify_and_add (valist, pre_p);
13099 t = build_call_expr_loc (loc,
13100 builtin_decl_implicit (BUILT_IN_TRAP), 0);
13101 gimplify_and_add (t, pre_p);
13103 /* This is dead code, but go ahead and finish so that the
13104 mode of the result comes out right. */
13105 *expr_p = dummy_object (type);
13106 return GS_ALL_DONE;
13109 tag = build_int_cst (build_pointer_type (type), 0);
13110 aptag = build_int_cst (TREE_TYPE (valist), 0);
13112 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
13113 valist, tag, aptag);
13115 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
13116 needs to be expanded. */
13117 cfun->curr_properties &= ~PROP_gimple_lva;
13119 return GS_OK;
13122 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
13124 DST/SRC are the destination and source respectively. You can pass
13125 ungimplified trees in DST or SRC, in which case they will be
13126 converted to a gimple operand if necessary.
13128 This function returns the newly created GIMPLE_ASSIGN tuple. */
13130 gimple *
13131 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
13133 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13134 gimplify_and_add (t, seq_p);
13135 ggc_free (t);
13136 return gimple_seq_last_stmt (*seq_p);
13139 inline hashval_t
13140 gimplify_hasher::hash (const elt_t *p)
13142 tree t = p->val;
13143 return iterative_hash_expr (t, 0);
13146 inline bool
13147 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
13149 tree t1 = p1->val;
13150 tree t2 = p2->val;
13151 enum tree_code code = TREE_CODE (t1);
13153 if (TREE_CODE (t2) != code
13154 || TREE_TYPE (t1) != TREE_TYPE (t2))
13155 return false;
13157 if (!operand_equal_p (t1, t2, 0))
13158 return false;
13160 /* Only allow them to compare equal if they also hash equal; otherwise
13161 results are nondeterminate, and we fail bootstrap comparison. */
13162 gcc_checking_assert (hash (p1) == hash (p2));
13164 return true;