re PR fortran/88376 (ICE in is_illegal_recursion, at fortran/resolve.c:1689)
[official-gcc.git] / gcc / gimplify.c
blob5677ddd9e84c8847874233f44e8f0867e3acfc2f
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2019 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 /* Flag for GOVD_MAP: only allocate. */
109 GOVD_MAP_ALLOC_ONLY = 1048576,
111 /* Flag for GOVD_MAP: only copy back. */
112 GOVD_MAP_FROM_ONLY = 2097152,
114 GOVD_NONTEMPORAL = 4194304,
116 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
117 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
118 | GOVD_LOCAL)
122 enum omp_region_type
124 ORT_WORKSHARE = 0x00,
125 ORT_TASKGROUP = 0x01,
126 ORT_SIMD = 0x04,
128 ORT_PARALLEL = 0x08,
129 ORT_COMBINED_PARALLEL = ORT_PARALLEL | 1,
131 ORT_TASK = 0x10,
132 ORT_UNTIED_TASK = ORT_TASK | 1,
133 ORT_TASKLOOP = ORT_TASK | 2,
134 ORT_UNTIED_TASKLOOP = ORT_UNTIED_TASK | 2,
136 ORT_TEAMS = 0x20,
137 ORT_COMBINED_TEAMS = ORT_TEAMS | 1,
138 ORT_HOST_TEAMS = ORT_TEAMS | 2,
139 ORT_COMBINED_HOST_TEAMS = ORT_COMBINED_TEAMS | 2,
141 /* Data region. */
142 ORT_TARGET_DATA = 0x40,
144 /* Data region with offloading. */
145 ORT_TARGET = 0x80,
146 ORT_COMBINED_TARGET = ORT_TARGET | 1,
148 /* OpenACC variants. */
149 ORT_ACC = 0x100, /* A generic OpenACC region. */
150 ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
151 ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
152 ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 2, /* Kernels construct. */
153 ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 2, /* Host data. */
155 /* Dummy OpenMP region, used to disable expansion of
156 DECL_VALUE_EXPRs in taskloop pre body. */
157 ORT_NONE = 0x200
160 /* Gimplify hashtable helper. */
162 struct gimplify_hasher : free_ptr_hash <elt_t>
164 static inline hashval_t hash (const elt_t *);
165 static inline bool equal (const elt_t *, const elt_t *);
168 struct gimplify_ctx
170 struct gimplify_ctx *prev_context;
172 vec<gbind *> bind_expr_stack;
173 tree temps;
174 gimple_seq conditional_cleanups;
175 tree exit_label;
176 tree return_temp;
178 vec<tree> case_labels;
179 hash_set<tree> *live_switch_vars;
180 /* The formal temporary table. Should this be persistent? */
181 hash_table<gimplify_hasher> *temp_htab;
183 int conditions;
184 unsigned into_ssa : 1;
185 unsigned allow_rhs_cond_expr : 1;
186 unsigned in_cleanup_point_expr : 1;
187 unsigned keep_stack : 1;
188 unsigned save_stack : 1;
189 unsigned in_switch_expr : 1;
192 enum gimplify_defaultmap_kind
194 GDMK_SCALAR,
195 GDMK_AGGREGATE,
196 GDMK_ALLOCATABLE,
197 GDMK_POINTER
200 struct gimplify_omp_ctx
202 struct gimplify_omp_ctx *outer_context;
203 splay_tree variables;
204 hash_set<tree> *privatized_types;
205 /* Iteration variables in an OMP_FOR. */
206 vec<tree> loop_iter_var;
207 location_t location;
208 enum omp_clause_default_kind default_kind;
209 enum omp_region_type region_type;
210 bool combined_loop;
211 bool distribute;
212 bool target_firstprivatize_array_bases;
213 int defaultmap[4];
216 static struct gimplify_ctx *gimplify_ctxp;
217 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
219 /* Forward declaration. */
220 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
221 static hash_map<tree, tree> *oacc_declare_returns;
222 static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
223 bool (*) (tree), fallback_t, bool);
225 /* Shorter alias name for the above function for use in gimplify.c
226 only. */
228 static inline void
229 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
231 gimple_seq_add_stmt_without_update (seq_p, gs);
234 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
235 NULL, a new sequence is allocated. This function is
236 similar to gimple_seq_add_seq, but does not scan the operands.
237 During gimplification, we need to manipulate statement sequences
238 before the def/use vectors have been constructed. */
240 static void
241 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
243 gimple_stmt_iterator si;
245 if (src == NULL)
246 return;
248 si = gsi_last (*dst_p);
249 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
253 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
254 and popping gimplify contexts. */
256 static struct gimplify_ctx *ctx_pool = NULL;
258 /* Return a gimplify context struct from the pool. */
260 static inline struct gimplify_ctx *
261 ctx_alloc (void)
263 struct gimplify_ctx * c = ctx_pool;
265 if (c)
266 ctx_pool = c->prev_context;
267 else
268 c = XNEW (struct gimplify_ctx);
270 memset (c, '\0', sizeof (*c));
271 return c;
274 /* Put gimplify context C back into the pool. */
276 static inline void
277 ctx_free (struct gimplify_ctx *c)
279 c->prev_context = ctx_pool;
280 ctx_pool = c;
283 /* Free allocated ctx stack memory. */
285 void
286 free_gimplify_stack (void)
288 struct gimplify_ctx *c;
290 while ((c = ctx_pool))
292 ctx_pool = c->prev_context;
293 free (c);
298 /* Set up a context for the gimplifier. */
300 void
301 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
303 struct gimplify_ctx *c = ctx_alloc ();
305 c->prev_context = gimplify_ctxp;
306 gimplify_ctxp = c;
307 gimplify_ctxp->into_ssa = in_ssa;
308 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
311 /* Tear down a context for the gimplifier. If BODY is non-null, then
312 put the temporaries into the outer BIND_EXPR. Otherwise, put them
313 in the local_decls.
315 BODY is not a sequence, but the first tuple in a sequence. */
317 void
318 pop_gimplify_context (gimple *body)
320 struct gimplify_ctx *c = gimplify_ctxp;
322 gcc_assert (c
323 && (!c->bind_expr_stack.exists ()
324 || c->bind_expr_stack.is_empty ()));
325 c->bind_expr_stack.release ();
326 gimplify_ctxp = c->prev_context;
328 if (body)
329 declare_vars (c->temps, body, false);
330 else
331 record_vars (c->temps);
333 delete c->temp_htab;
334 c->temp_htab = NULL;
335 ctx_free (c);
338 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
340 static void
341 gimple_push_bind_expr (gbind *bind_stmt)
343 gimplify_ctxp->bind_expr_stack.reserve (8);
344 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
347 /* Pop the first element off the stack of bindings. */
349 static void
350 gimple_pop_bind_expr (void)
352 gimplify_ctxp->bind_expr_stack.pop ();
355 /* Return the first element of the stack of bindings. */
357 gbind *
358 gimple_current_bind_expr (void)
360 return gimplify_ctxp->bind_expr_stack.last ();
363 /* Return the stack of bindings created during gimplification. */
365 vec<gbind *>
366 gimple_bind_expr_stack (void)
368 return gimplify_ctxp->bind_expr_stack;
371 /* Return true iff there is a COND_EXPR between us and the innermost
372 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
374 static bool
375 gimple_conditional_context (void)
377 return gimplify_ctxp->conditions > 0;
380 /* Note that we've entered a COND_EXPR. */
382 static void
383 gimple_push_condition (void)
385 #ifdef ENABLE_GIMPLE_CHECKING
386 if (gimplify_ctxp->conditions == 0)
387 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
388 #endif
389 ++(gimplify_ctxp->conditions);
392 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
393 now, add any conditional cleanups we've seen to the prequeue. */
395 static void
396 gimple_pop_condition (gimple_seq *pre_p)
398 int conds = --(gimplify_ctxp->conditions);
400 gcc_assert (conds >= 0);
401 if (conds == 0)
403 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
404 gimplify_ctxp->conditional_cleanups = NULL;
408 /* A stable comparison routine for use with splay trees and DECLs. */
410 static int
411 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
413 tree a = (tree) xa;
414 tree b = (tree) xb;
416 return DECL_UID (a) - DECL_UID (b);
419 /* Create a new omp construct that deals with variable remapping. */
421 static struct gimplify_omp_ctx *
422 new_omp_context (enum omp_region_type region_type)
424 struct gimplify_omp_ctx *c;
426 c = XCNEW (struct gimplify_omp_ctx);
427 c->outer_context = gimplify_omp_ctxp;
428 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
429 c->privatized_types = new hash_set<tree>;
430 c->location = input_location;
431 c->region_type = region_type;
432 if ((region_type & ORT_TASK) == 0)
433 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
434 else
435 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
436 c->defaultmap[GDMK_SCALAR] = GOVD_MAP;
437 c->defaultmap[GDMK_AGGREGATE] = GOVD_MAP;
438 c->defaultmap[GDMK_ALLOCATABLE] = GOVD_MAP;
439 c->defaultmap[GDMK_POINTER] = GOVD_MAP;
441 return c;
444 /* Destroy an omp construct that deals with variable remapping. */
446 static void
447 delete_omp_context (struct gimplify_omp_ctx *c)
449 splay_tree_delete (c->variables);
450 delete c->privatized_types;
451 c->loop_iter_var.release ();
452 XDELETE (c);
455 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
456 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
458 /* Both gimplify the statement T and append it to *SEQ_P. This function
459 behaves exactly as gimplify_stmt, but you don't have to pass T as a
460 reference. */
462 void
463 gimplify_and_add (tree t, gimple_seq *seq_p)
465 gimplify_stmt (&t, seq_p);
468 /* Gimplify statement T into sequence *SEQ_P, and return the first
469 tuple in the sequence of generated tuples for this statement.
470 Return NULL if gimplifying T produced no tuples. */
472 static gimple *
473 gimplify_and_return_first (tree t, gimple_seq *seq_p)
475 gimple_stmt_iterator last = gsi_last (*seq_p);
477 gimplify_and_add (t, seq_p);
479 if (!gsi_end_p (last))
481 gsi_next (&last);
482 return gsi_stmt (last);
484 else
485 return gimple_seq_first_stmt (*seq_p);
488 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
489 LHS, or for a call argument. */
491 static bool
492 is_gimple_mem_rhs (tree t)
494 /* If we're dealing with a renamable type, either source or dest must be
495 a renamed variable. */
496 if (is_gimple_reg_type (TREE_TYPE (t)))
497 return is_gimple_val (t);
498 else
499 return is_gimple_val (t) || is_gimple_lvalue (t);
502 /* Return true if T is a CALL_EXPR or an expression that can be
503 assigned to a temporary. Note that this predicate should only be
504 used during gimplification. See the rationale for this in
505 gimplify_modify_expr. */
507 static bool
508 is_gimple_reg_rhs_or_call (tree t)
510 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
511 || TREE_CODE (t) == CALL_EXPR);
514 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
515 this predicate should only be used during gimplification. See the
516 rationale for this in gimplify_modify_expr. */
518 static bool
519 is_gimple_mem_rhs_or_call (tree t)
521 /* If we're dealing with a renamable type, either source or dest must be
522 a renamed variable. */
523 if (is_gimple_reg_type (TREE_TYPE (t)))
524 return is_gimple_val (t);
525 else
526 return (is_gimple_val (t)
527 || is_gimple_lvalue (t)
528 || TREE_CLOBBER_P (t)
529 || TREE_CODE (t) == CALL_EXPR);
532 /* Create a temporary with a name derived from VAL. Subroutine of
533 lookup_tmp_var; nobody else should call this function. */
535 static inline tree
536 create_tmp_from_val (tree val)
538 /* Drop all qualifiers and address-space information from the value type. */
539 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
540 tree var = create_tmp_var (type, get_name (val));
541 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
542 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
543 DECL_GIMPLE_REG_P (var) = 1;
544 return var;
547 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
548 an existing expression temporary. */
550 static tree
551 lookup_tmp_var (tree val, bool is_formal)
553 tree ret;
555 /* If not optimizing, never really reuse a temporary. local-alloc
556 won't allocate any variable that is used in more than one basic
557 block, which means it will go into memory, causing much extra
558 work in reload and final and poorer code generation, outweighing
559 the extra memory allocation here. */
560 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
561 ret = create_tmp_from_val (val);
562 else
564 elt_t elt, *elt_p;
565 elt_t **slot;
567 elt.val = val;
568 if (!gimplify_ctxp->temp_htab)
569 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
570 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
571 if (*slot == NULL)
573 elt_p = XNEW (elt_t);
574 elt_p->val = val;
575 elt_p->temp = ret = create_tmp_from_val (val);
576 *slot = elt_p;
578 else
580 elt_p = *slot;
581 ret = elt_p->temp;
585 return ret;
588 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
590 static tree
591 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
592 bool is_formal, bool allow_ssa)
594 tree t, mod;
596 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
597 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
598 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
599 fb_rvalue);
601 if (allow_ssa
602 && gimplify_ctxp->into_ssa
603 && is_gimple_reg_type (TREE_TYPE (val)))
605 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
606 if (! gimple_in_ssa_p (cfun))
608 const char *name = get_name (val);
609 if (name)
610 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
613 else
614 t = lookup_tmp_var (val, is_formal);
616 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
618 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
620 /* gimplify_modify_expr might want to reduce this further. */
621 gimplify_and_add (mod, pre_p);
622 ggc_free (mod);
624 return t;
627 /* Return a formal temporary variable initialized with VAL. PRE_P is as
628 in gimplify_expr. Only use this function if:
630 1) The value of the unfactored expression represented by VAL will not
631 change between the initialization and use of the temporary, and
632 2) The temporary will not be otherwise modified.
634 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
635 and #2 means it is inappropriate for && temps.
637 For other cases, use get_initialized_tmp_var instead. */
639 tree
640 get_formal_tmp_var (tree val, gimple_seq *pre_p)
642 return internal_get_tmp_var (val, pre_p, NULL, true, true);
645 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
646 are as in gimplify_expr. */
648 tree
649 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
650 bool allow_ssa)
652 return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa);
655 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
656 generate debug info for them; otherwise don't. */
658 void
659 declare_vars (tree vars, gimple *gs, bool debug_info)
661 tree last = vars;
662 if (last)
664 tree temps, block;
666 gbind *scope = as_a <gbind *> (gs);
668 temps = nreverse (last);
670 block = gimple_bind_block (scope);
671 gcc_assert (!block || TREE_CODE (block) == BLOCK);
672 if (!block || !debug_info)
674 DECL_CHAIN (last) = gimple_bind_vars (scope);
675 gimple_bind_set_vars (scope, temps);
677 else
679 /* We need to attach the nodes both to the BIND_EXPR and to its
680 associated BLOCK for debugging purposes. The key point here
681 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
682 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
683 if (BLOCK_VARS (block))
684 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
685 else
687 gimple_bind_set_vars (scope,
688 chainon (gimple_bind_vars (scope), temps));
689 BLOCK_VARS (block) = temps;
695 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
696 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
697 no such upper bound can be obtained. */
699 static void
700 force_constant_size (tree var)
702 /* The only attempt we make is by querying the maximum size of objects
703 of the variable's type. */
705 HOST_WIDE_INT max_size;
707 gcc_assert (VAR_P (var));
709 max_size = max_int_size_in_bytes (TREE_TYPE (var));
711 gcc_assert (max_size >= 0);
713 DECL_SIZE_UNIT (var)
714 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
715 DECL_SIZE (var)
716 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
719 /* Push the temporary variable TMP into the current binding. */
721 void
722 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
724 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
726 /* Later processing assumes that the object size is constant, which might
727 not be true at this point. Force the use of a constant upper bound in
728 this case. */
729 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
730 force_constant_size (tmp);
732 DECL_CONTEXT (tmp) = fn->decl;
733 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
735 record_vars_into (tmp, fn->decl);
738 /* Push the temporary variable TMP into the current binding. */
740 void
741 gimple_add_tmp_var (tree tmp)
743 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
745 /* Later processing assumes that the object size is constant, which might
746 not be true at this point. Force the use of a constant upper bound in
747 this case. */
748 if (!tree_fits_poly_uint64_p (DECL_SIZE_UNIT (tmp)))
749 force_constant_size (tmp);
751 DECL_CONTEXT (tmp) = current_function_decl;
752 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
754 if (gimplify_ctxp)
756 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
757 gimplify_ctxp->temps = tmp;
759 /* Mark temporaries local within the nearest enclosing parallel. */
760 if (gimplify_omp_ctxp)
762 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
763 while (ctx
764 && (ctx->region_type == ORT_WORKSHARE
765 || ctx->region_type == ORT_TASKGROUP
766 || ctx->region_type == ORT_SIMD
767 || ctx->region_type == ORT_ACC))
768 ctx = ctx->outer_context;
769 if (ctx)
770 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
773 else if (cfun)
774 record_vars (tmp);
775 else
777 gimple_seq body_seq;
779 /* This case is for nested functions. We need to expose the locals
780 they create. */
781 body_seq = gimple_body (current_function_decl);
782 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
788 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
789 nodes that are referenced more than once in GENERIC functions. This is
790 necessary because gimplification (translation into GIMPLE) is performed
791 by modifying tree nodes in-place, so gimplication of a shared node in a
792 first context could generate an invalid GIMPLE form in a second context.
794 This is achieved with a simple mark/copy/unmark algorithm that walks the
795 GENERIC representation top-down, marks nodes with TREE_VISITED the first
796 time it encounters them, duplicates them if they already have TREE_VISITED
797 set, and finally removes the TREE_VISITED marks it has set.
799 The algorithm works only at the function level, i.e. it generates a GENERIC
800 representation of a function with no nodes shared within the function when
801 passed a GENERIC function (except for nodes that are allowed to be shared).
803 At the global level, it is also necessary to unshare tree nodes that are
804 referenced in more than one function, for the same aforementioned reason.
805 This requires some cooperation from the front-end. There are 2 strategies:
807 1. Manual unsharing. The front-end needs to call unshare_expr on every
808 expression that might end up being shared across functions.
810 2. Deep unsharing. This is an extension of regular unsharing. Instead
811 of calling unshare_expr on expressions that might be shared across
812 functions, the front-end pre-marks them with TREE_VISITED. This will
813 ensure that they are unshared on the first reference within functions
814 when the regular unsharing algorithm runs. The counterpart is that
815 this algorithm must look deeper than for manual unsharing, which is
816 specified by LANG_HOOKS_DEEP_UNSHARING.
818 If there are only few specific cases of node sharing across functions, it is
819 probably easier for a front-end to unshare the expressions manually. On the
820 contrary, if the expressions generated at the global level are as widespread
821 as expressions generated within functions, deep unsharing is very likely the
822 way to go. */
824 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
825 These nodes model computations that must be done once. If we were to
826 unshare something like SAVE_EXPR(i++), the gimplification process would
827 create wrong code. However, if DATA is non-null, it must hold a pointer
828 set that is used to unshare the subtrees of these nodes. */
830 static tree
831 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
833 tree t = *tp;
834 enum tree_code code = TREE_CODE (t);
836 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
837 copy their subtrees if we can make sure to do it only once. */
838 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
840 if (data && !((hash_set<tree> *)data)->add (t))
842 else
843 *walk_subtrees = 0;
846 /* Stop at types, decls, constants like copy_tree_r. */
847 else if (TREE_CODE_CLASS (code) == tcc_type
848 || TREE_CODE_CLASS (code) == tcc_declaration
849 || TREE_CODE_CLASS (code) == tcc_constant)
850 *walk_subtrees = 0;
852 /* Cope with the statement expression extension. */
853 else if (code == STATEMENT_LIST)
856 /* Leave the bulk of the work to copy_tree_r itself. */
857 else
858 copy_tree_r (tp, walk_subtrees, NULL);
860 return NULL_TREE;
863 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
864 If *TP has been visited already, then *TP is deeply copied by calling
865 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
867 static tree
868 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
870 tree t = *tp;
871 enum tree_code code = TREE_CODE (t);
873 /* Skip types, decls, and constants. But we do want to look at their
874 types and the bounds of types. Mark them as visited so we properly
875 unmark their subtrees on the unmark pass. If we've already seen them,
876 don't look down further. */
877 if (TREE_CODE_CLASS (code) == tcc_type
878 || TREE_CODE_CLASS (code) == tcc_declaration
879 || TREE_CODE_CLASS (code) == tcc_constant)
881 if (TREE_VISITED (t))
882 *walk_subtrees = 0;
883 else
884 TREE_VISITED (t) = 1;
887 /* If this node has been visited already, unshare it and don't look
888 any deeper. */
889 else if (TREE_VISITED (t))
891 walk_tree (tp, mostly_copy_tree_r, data, NULL);
892 *walk_subtrees = 0;
895 /* Otherwise, mark the node as visited and keep looking. */
896 else
897 TREE_VISITED (t) = 1;
899 return NULL_TREE;
902 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
903 copy_if_shared_r callback unmodified. */
905 static inline void
906 copy_if_shared (tree *tp, void *data)
908 walk_tree (tp, copy_if_shared_r, data, NULL);
911 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
912 any nested functions. */
914 static void
915 unshare_body (tree fndecl)
917 struct cgraph_node *cgn = cgraph_node::get (fndecl);
918 /* If the language requires deep unsharing, we need a pointer set to make
919 sure we don't repeatedly unshare subtrees of unshareable nodes. */
920 hash_set<tree> *visited
921 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
923 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
924 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
925 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
927 delete visited;
929 if (cgn)
930 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
931 unshare_body (cgn->decl);
934 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
935 Subtrees are walked until the first unvisited node is encountered. */
937 static tree
938 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
940 tree t = *tp;
942 /* If this node has been visited, unmark it and keep looking. */
943 if (TREE_VISITED (t))
944 TREE_VISITED (t) = 0;
946 /* Otherwise, don't look any deeper. */
947 else
948 *walk_subtrees = 0;
950 return NULL_TREE;
953 /* Unmark the visited trees rooted at *TP. */
955 static inline void
956 unmark_visited (tree *tp)
958 walk_tree (tp, unmark_visited_r, NULL, NULL);
961 /* Likewise, but mark all trees as not visited. */
963 static void
964 unvisit_body (tree fndecl)
966 struct cgraph_node *cgn = cgraph_node::get (fndecl);
968 unmark_visited (&DECL_SAVED_TREE (fndecl));
969 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
970 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
972 if (cgn)
973 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
974 unvisit_body (cgn->decl);
977 /* Unconditionally make an unshared copy of EXPR. This is used when using
978 stored expressions which span multiple functions, such as BINFO_VTABLE,
979 as the normal unsharing process can't tell that they're shared. */
981 tree
982 unshare_expr (tree expr)
984 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
985 return expr;
988 /* Worker for unshare_expr_without_location. */
990 static tree
991 prune_expr_location (tree *tp, int *walk_subtrees, void *)
993 if (EXPR_P (*tp))
994 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
995 else
996 *walk_subtrees = 0;
997 return NULL_TREE;
1000 /* Similar to unshare_expr but also prune all expression locations
1001 from EXPR. */
1003 tree
1004 unshare_expr_without_location (tree expr)
1006 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1007 if (EXPR_P (expr))
1008 walk_tree (&expr, prune_expr_location, NULL, NULL);
1009 return expr;
1012 /* Return the EXPR_LOCATION of EXPR, if it (maybe recursively) has
1013 one, OR_ELSE otherwise. The location of a STATEMENT_LISTs
1014 comprising at least one DEBUG_BEGIN_STMT followed by exactly one
1015 EXPR is the location of the EXPR. */
1017 static location_t
1018 rexpr_location (tree expr, location_t or_else = UNKNOWN_LOCATION)
1020 if (!expr)
1021 return or_else;
1023 if (EXPR_HAS_LOCATION (expr))
1024 return EXPR_LOCATION (expr);
1026 if (TREE_CODE (expr) != STATEMENT_LIST)
1027 return or_else;
1029 tree_stmt_iterator i = tsi_start (expr);
1031 bool found = false;
1032 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
1034 found = true;
1035 tsi_next (&i);
1038 if (!found || !tsi_one_before_end_p (i))
1039 return or_else;
1041 return rexpr_location (tsi_stmt (i), or_else);
1044 /* Return TRUE iff EXPR (maybe recursively) has a location; see
1045 rexpr_location for the potential recursion. */
1047 static inline bool
1048 rexpr_has_location (tree expr)
1050 return rexpr_location (expr) != UNKNOWN_LOCATION;
1054 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1055 contain statements and have a value. Assign its value to a temporary
1056 and give it void_type_node. Return the temporary, or NULL_TREE if
1057 WRAPPER was already void. */
1059 tree
1060 voidify_wrapper_expr (tree wrapper, tree temp)
1062 tree type = TREE_TYPE (wrapper);
1063 if (type && !VOID_TYPE_P (type))
1065 tree *p;
1067 /* Set p to point to the body of the wrapper. Loop until we find
1068 something that isn't a wrapper. */
1069 for (p = &wrapper; p && *p; )
1071 switch (TREE_CODE (*p))
1073 case BIND_EXPR:
1074 TREE_SIDE_EFFECTS (*p) = 1;
1075 TREE_TYPE (*p) = void_type_node;
1076 /* For a BIND_EXPR, the body is operand 1. */
1077 p = &BIND_EXPR_BODY (*p);
1078 break;
1080 case CLEANUP_POINT_EXPR:
1081 case TRY_FINALLY_EXPR:
1082 case TRY_CATCH_EXPR:
1083 TREE_SIDE_EFFECTS (*p) = 1;
1084 TREE_TYPE (*p) = void_type_node;
1085 p = &TREE_OPERAND (*p, 0);
1086 break;
1088 case STATEMENT_LIST:
1090 tree_stmt_iterator i = tsi_last (*p);
1091 TREE_SIDE_EFFECTS (*p) = 1;
1092 TREE_TYPE (*p) = void_type_node;
1093 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1095 break;
1097 case COMPOUND_EXPR:
1098 /* Advance to the last statement. Set all container types to
1099 void. */
1100 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1102 TREE_SIDE_EFFECTS (*p) = 1;
1103 TREE_TYPE (*p) = void_type_node;
1105 break;
1107 case TRANSACTION_EXPR:
1108 TREE_SIDE_EFFECTS (*p) = 1;
1109 TREE_TYPE (*p) = void_type_node;
1110 p = &TRANSACTION_EXPR_BODY (*p);
1111 break;
1113 default:
1114 /* Assume that any tree upon which voidify_wrapper_expr is
1115 directly called is a wrapper, and that its body is op0. */
1116 if (p == &wrapper)
1118 TREE_SIDE_EFFECTS (*p) = 1;
1119 TREE_TYPE (*p) = void_type_node;
1120 p = &TREE_OPERAND (*p, 0);
1121 break;
1123 goto out;
1127 out:
1128 if (p == NULL || IS_EMPTY_STMT (*p))
1129 temp = NULL_TREE;
1130 else if (temp)
1132 /* The wrapper is on the RHS of an assignment that we're pushing
1133 down. */
1134 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1135 || TREE_CODE (temp) == MODIFY_EXPR);
1136 TREE_OPERAND (temp, 1) = *p;
1137 *p = temp;
1139 else
1141 temp = create_tmp_var (type, "retval");
1142 *p = build2 (INIT_EXPR, type, temp, *p);
1145 return temp;
1148 return NULL_TREE;
1151 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1152 a temporary through which they communicate. */
1154 static void
1155 build_stack_save_restore (gcall **save, gcall **restore)
1157 tree tmp_var;
1159 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1160 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1161 gimple_call_set_lhs (*save, tmp_var);
1163 *restore
1164 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1165 1, tmp_var);
1168 /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1170 static tree
1171 build_asan_poison_call_expr (tree decl)
1173 /* Do not poison variables that have size equal to zero. */
1174 tree unit_size = DECL_SIZE_UNIT (decl);
1175 if (zerop (unit_size))
1176 return NULL_TREE;
1178 tree base = build_fold_addr_expr (decl);
1180 return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1181 void_type_node, 3,
1182 build_int_cst (integer_type_node,
1183 ASAN_MARK_POISON),
1184 base, unit_size);
1187 /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1188 on POISON flag, shadow memory of a DECL variable. The call will be
1189 put on location identified by IT iterator, where BEFORE flag drives
1190 position where the stmt will be put. */
1192 static void
1193 asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1194 bool before)
1196 tree unit_size = DECL_SIZE_UNIT (decl);
1197 tree base = build_fold_addr_expr (decl);
1199 /* Do not poison variables that have size equal to zero. */
1200 if (zerop (unit_size))
1201 return;
1203 /* It's necessary to have all stack variables aligned to ASAN granularity
1204 bytes. */
1205 if (DECL_ALIGN_UNIT (decl) <= ASAN_SHADOW_GRANULARITY)
1206 SET_DECL_ALIGN (decl, BITS_PER_UNIT * ASAN_SHADOW_GRANULARITY);
1208 HOST_WIDE_INT flags = poison ? ASAN_MARK_POISON : ASAN_MARK_UNPOISON;
1210 gimple *g
1211 = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1212 build_int_cst (integer_type_node, flags),
1213 base, unit_size);
1215 if (before)
1216 gsi_insert_before (it, g, GSI_NEW_STMT);
1217 else
1218 gsi_insert_after (it, g, GSI_NEW_STMT);
1221 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1222 either poisons or unpoisons a DECL. Created statement is appended
1223 to SEQ_P gimple sequence. */
1225 static void
1226 asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1228 gimple_stmt_iterator it = gsi_last (*seq_p);
1229 bool before = false;
1231 if (gsi_end_p (it))
1232 before = true;
1234 asan_poison_variable (decl, poison, &it, before);
1237 /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1239 static int
1240 sort_by_decl_uid (const void *a, const void *b)
1242 const tree *t1 = (const tree *)a;
1243 const tree *t2 = (const tree *)b;
1245 int uid1 = DECL_UID (*t1);
1246 int uid2 = DECL_UID (*t2);
1248 if (uid1 < uid2)
1249 return -1;
1250 else if (uid1 > uid2)
1251 return 1;
1252 else
1253 return 0;
1256 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1257 depending on POISON flag. Created statement is appended
1258 to SEQ_P gimple sequence. */
1260 static void
1261 asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1263 unsigned c = variables->elements ();
1264 if (c == 0)
1265 return;
1267 auto_vec<tree> sorted_variables (c);
1269 for (hash_set<tree>::iterator it = variables->begin ();
1270 it != variables->end (); ++it)
1271 sorted_variables.safe_push (*it);
1273 sorted_variables.qsort (sort_by_decl_uid);
1275 unsigned i;
1276 tree var;
1277 FOR_EACH_VEC_ELT (sorted_variables, i, var)
1279 asan_poison_variable (var, poison, seq_p);
1281 /* Add use_after_scope_memory attribute for the variable in order
1282 to prevent re-written into SSA. */
1283 if (!lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1284 DECL_ATTRIBUTES (var)))
1285 DECL_ATTRIBUTES (var)
1286 = tree_cons (get_identifier (ASAN_USE_AFTER_SCOPE_ATTRIBUTE),
1287 integer_one_node,
1288 DECL_ATTRIBUTES (var));
1292 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1294 static enum gimplify_status
1295 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1297 tree bind_expr = *expr_p;
1298 bool old_keep_stack = gimplify_ctxp->keep_stack;
1299 bool old_save_stack = gimplify_ctxp->save_stack;
1300 tree t;
1301 gbind *bind_stmt;
1302 gimple_seq body, cleanup;
1303 gcall *stack_save;
1304 location_t start_locus = 0, end_locus = 0;
1305 tree ret_clauses = NULL;
1307 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1309 /* Mark variables seen in this bind expr. */
1310 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1312 if (VAR_P (t))
1314 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1316 /* Mark variable as local. */
1317 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1318 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1319 || splay_tree_lookup (ctx->variables,
1320 (splay_tree_key) t) == NULL))
1322 if (ctx->region_type == ORT_SIMD
1323 && TREE_ADDRESSABLE (t)
1324 && !TREE_STATIC (t))
1325 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1326 else
1327 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1330 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1332 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1333 cfun->has_local_explicit_reg_vars = true;
1336 /* Preliminarily mark non-addressed complex variables as eligible
1337 for promotion to gimple registers. We'll transform their uses
1338 as we find them. */
1339 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1340 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1341 && !TREE_THIS_VOLATILE (t)
1342 && (VAR_P (t) && !DECL_HARD_REGISTER (t))
1343 && !needs_to_live_in_memory (t))
1344 DECL_GIMPLE_REG_P (t) = 1;
1347 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1348 BIND_EXPR_BLOCK (bind_expr));
1349 gimple_push_bind_expr (bind_stmt);
1351 gimplify_ctxp->keep_stack = false;
1352 gimplify_ctxp->save_stack = false;
1354 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1355 body = NULL;
1356 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1357 gimple_bind_set_body (bind_stmt, body);
1359 /* Source location wise, the cleanup code (stack_restore and clobbers)
1360 belongs to the end of the block, so propagate what we have. The
1361 stack_save operation belongs to the beginning of block, which we can
1362 infer from the bind_expr directly if the block has no explicit
1363 assignment. */
1364 if (BIND_EXPR_BLOCK (bind_expr))
1366 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1367 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1369 if (start_locus == 0)
1370 start_locus = EXPR_LOCATION (bind_expr);
1372 cleanup = NULL;
1373 stack_save = NULL;
1375 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1376 the stack space allocated to the VLAs. */
1377 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1379 gcall *stack_restore;
1381 /* Save stack on entry and restore it on exit. Add a try_finally
1382 block to achieve this. */
1383 build_stack_save_restore (&stack_save, &stack_restore);
1385 gimple_set_location (stack_save, start_locus);
1386 gimple_set_location (stack_restore, end_locus);
1388 gimplify_seq_add_stmt (&cleanup, stack_restore);
1391 /* Add clobbers for all variables that go out of scope. */
1392 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1394 if (VAR_P (t)
1395 && !is_global_var (t)
1396 && DECL_CONTEXT (t) == current_function_decl)
1398 if (!DECL_HARD_REGISTER (t)
1399 && !TREE_THIS_VOLATILE (t)
1400 && !DECL_HAS_VALUE_EXPR_P (t)
1401 /* Only care for variables that have to be in memory. Others
1402 will be rewritten into SSA names, hence moved to the
1403 top-level. */
1404 && !is_gimple_reg (t)
1405 && flag_stack_reuse != SR_NONE)
1407 tree clobber = build_clobber (TREE_TYPE (t));
1408 gimple *clobber_stmt;
1409 clobber_stmt = gimple_build_assign (t, clobber);
1410 gimple_set_location (clobber_stmt, end_locus);
1411 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1414 if (flag_openacc && oacc_declare_returns != NULL)
1416 tree *c = oacc_declare_returns->get (t);
1417 if (c != NULL)
1419 if (ret_clauses)
1420 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1422 ret_clauses = *c;
1424 oacc_declare_returns->remove (t);
1426 if (oacc_declare_returns->elements () == 0)
1428 delete oacc_declare_returns;
1429 oacc_declare_returns = NULL;
1435 if (asan_poisoned_variables != NULL
1436 && asan_poisoned_variables->contains (t))
1438 asan_poisoned_variables->remove (t);
1439 asan_poison_variable (t, true, &cleanup);
1442 if (gimplify_ctxp->live_switch_vars != NULL
1443 && gimplify_ctxp->live_switch_vars->contains (t))
1444 gimplify_ctxp->live_switch_vars->remove (t);
1447 if (ret_clauses)
1449 gomp_target *stmt;
1450 gimple_stmt_iterator si = gsi_start (cleanup);
1452 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1453 ret_clauses);
1454 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1457 if (cleanup)
1459 gtry *gs;
1460 gimple_seq new_body;
1462 new_body = NULL;
1463 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1464 GIMPLE_TRY_FINALLY);
1466 if (stack_save)
1467 gimplify_seq_add_stmt (&new_body, stack_save);
1468 gimplify_seq_add_stmt (&new_body, gs);
1469 gimple_bind_set_body (bind_stmt, new_body);
1472 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1473 if (!gimplify_ctxp->keep_stack)
1474 gimplify_ctxp->keep_stack = old_keep_stack;
1475 gimplify_ctxp->save_stack = old_save_stack;
1477 gimple_pop_bind_expr ();
1479 gimplify_seq_add_stmt (pre_p, bind_stmt);
1481 if (temp)
1483 *expr_p = temp;
1484 return GS_OK;
1487 *expr_p = NULL_TREE;
1488 return GS_ALL_DONE;
1491 /* Maybe add early return predict statement to PRE_P sequence. */
1493 static void
1494 maybe_add_early_return_predict_stmt (gimple_seq *pre_p)
1496 /* If we are not in a conditional context, add PREDICT statement. */
1497 if (gimple_conditional_context ())
1499 gimple *predict = gimple_build_predict (PRED_TREE_EARLY_RETURN,
1500 NOT_TAKEN);
1501 gimplify_seq_add_stmt (pre_p, predict);
1505 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1506 GIMPLE value, it is assigned to a new temporary and the statement is
1507 re-written to return the temporary.
1509 PRE_P points to the sequence where side effects that must happen before
1510 STMT should be stored. */
1512 static enum gimplify_status
1513 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1515 greturn *ret;
1516 tree ret_expr = TREE_OPERAND (stmt, 0);
1517 tree result_decl, result;
1519 if (ret_expr == error_mark_node)
1520 return GS_ERROR;
1522 if (!ret_expr
1523 || TREE_CODE (ret_expr) == RESULT_DECL)
1525 maybe_add_early_return_predict_stmt (pre_p);
1526 greturn *ret = gimple_build_return (ret_expr);
1527 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1528 gimplify_seq_add_stmt (pre_p, ret);
1529 return GS_ALL_DONE;
1532 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1533 result_decl = NULL_TREE;
1534 else
1536 result_decl = TREE_OPERAND (ret_expr, 0);
1538 /* See through a return by reference. */
1539 if (TREE_CODE (result_decl) == INDIRECT_REF)
1540 result_decl = TREE_OPERAND (result_decl, 0);
1542 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1543 || TREE_CODE (ret_expr) == INIT_EXPR)
1544 && TREE_CODE (result_decl) == RESULT_DECL);
1547 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1548 Recall that aggregate_value_p is FALSE for any aggregate type that is
1549 returned in registers. If we're returning values in registers, then
1550 we don't want to extend the lifetime of the RESULT_DECL, particularly
1551 across another call. In addition, for those aggregates for which
1552 hard_function_value generates a PARALLEL, we'll die during normal
1553 expansion of structure assignments; there's special code in expand_return
1554 to handle this case that does not exist in expand_expr. */
1555 if (!result_decl)
1556 result = NULL_TREE;
1557 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1559 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1561 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1562 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1563 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1564 should be effectively allocated by the caller, i.e. all calls to
1565 this function must be subject to the Return Slot Optimization. */
1566 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1567 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1569 result = result_decl;
1571 else if (gimplify_ctxp->return_temp)
1572 result = gimplify_ctxp->return_temp;
1573 else
1575 result = create_tmp_reg (TREE_TYPE (result_decl));
1577 /* ??? With complex control flow (usually involving abnormal edges),
1578 we can wind up warning about an uninitialized value for this. Due
1579 to how this variable is constructed and initialized, this is never
1580 true. Give up and never warn. */
1581 TREE_NO_WARNING (result) = 1;
1583 gimplify_ctxp->return_temp = result;
1586 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1587 Then gimplify the whole thing. */
1588 if (result != result_decl)
1589 TREE_OPERAND (ret_expr, 0) = result;
1591 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1593 maybe_add_early_return_predict_stmt (pre_p);
1594 ret = gimple_build_return (result);
1595 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1596 gimplify_seq_add_stmt (pre_p, ret);
1598 return GS_ALL_DONE;
1601 /* Gimplify a variable-length array DECL. */
1603 static void
1604 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1606 /* This is a variable-sized decl. Simplify its size and mark it
1607 for deferred expansion. */
1608 tree t, addr, ptr_type;
1610 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1611 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1613 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1614 if (DECL_HAS_VALUE_EXPR_P (decl))
1615 return;
1617 /* All occurrences of this decl in final gimplified code will be
1618 replaced by indirection. Setting DECL_VALUE_EXPR does two
1619 things: First, it lets the rest of the gimplifier know what
1620 replacement to use. Second, it lets the debug info know
1621 where to find the value. */
1622 ptr_type = build_pointer_type (TREE_TYPE (decl));
1623 addr = create_tmp_var (ptr_type, get_name (decl));
1624 DECL_IGNORED_P (addr) = 0;
1625 t = build_fold_indirect_ref (addr);
1626 TREE_THIS_NOTRAP (t) = 1;
1627 SET_DECL_VALUE_EXPR (decl, t);
1628 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1630 t = build_alloca_call_expr (DECL_SIZE_UNIT (decl), DECL_ALIGN (decl),
1631 max_int_size_in_bytes (TREE_TYPE (decl)));
1632 /* The call has been built for a variable-sized object. */
1633 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1634 t = fold_convert (ptr_type, t);
1635 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1637 gimplify_and_add (t, seq_p);
1640 /* A helper function to be called via walk_tree. Mark all labels under *TP
1641 as being forced. To be called for DECL_INITIAL of static variables. */
1643 static tree
1644 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1646 if (TYPE_P (*tp))
1647 *walk_subtrees = 0;
1648 if (TREE_CODE (*tp) == LABEL_DECL)
1650 FORCED_LABEL (*tp) = 1;
1651 cfun->has_forced_label_in_static = 1;
1654 return NULL_TREE;
1657 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1658 and initialization explicit. */
1660 static enum gimplify_status
1661 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1663 tree stmt = *stmt_p;
1664 tree decl = DECL_EXPR_DECL (stmt);
1666 *stmt_p = NULL_TREE;
1668 if (TREE_TYPE (decl) == error_mark_node)
1669 return GS_ERROR;
1671 if ((TREE_CODE (decl) == TYPE_DECL
1672 || VAR_P (decl))
1673 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1675 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1676 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1677 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1680 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1681 in case its size expressions contain problematic nodes like CALL_EXPR. */
1682 if (TREE_CODE (decl) == TYPE_DECL
1683 && DECL_ORIGINAL_TYPE (decl)
1684 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1686 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1687 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1688 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1691 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1693 tree init = DECL_INITIAL (decl);
1694 bool is_vla = false;
1696 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1697 || (!TREE_STATIC (decl)
1698 && flag_stack_check == GENERIC_STACK_CHECK
1699 && compare_tree_int (DECL_SIZE_UNIT (decl),
1700 STACK_CHECK_MAX_VAR_SIZE) > 0))
1702 gimplify_vla_decl (decl, seq_p);
1703 is_vla = true;
1706 if (asan_poisoned_variables
1707 && !is_vla
1708 && TREE_ADDRESSABLE (decl)
1709 && !TREE_STATIC (decl)
1710 && !DECL_HAS_VALUE_EXPR_P (decl)
1711 && DECL_ALIGN (decl) <= MAX_SUPPORTED_STACK_ALIGNMENT
1712 && dbg_cnt (asan_use_after_scope)
1713 && !gimplify_omp_ctxp)
1715 asan_poisoned_variables->add (decl);
1716 asan_poison_variable (decl, false, seq_p);
1717 if (!DECL_ARTIFICIAL (decl) && gimplify_ctxp->live_switch_vars)
1718 gimplify_ctxp->live_switch_vars->add (decl);
1721 /* Some front ends do not explicitly declare all anonymous
1722 artificial variables. We compensate here by declaring the
1723 variables, though it would be better if the front ends would
1724 explicitly declare them. */
1725 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1726 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1727 gimple_add_tmp_var (decl);
1729 if (init && init != error_mark_node)
1731 if (!TREE_STATIC (decl))
1733 DECL_INITIAL (decl) = NULL_TREE;
1734 init = build2 (INIT_EXPR, void_type_node, decl, init);
1735 gimplify_and_add (init, seq_p);
1736 ggc_free (init);
1738 else
1739 /* We must still examine initializers for static variables
1740 as they may contain a label address. */
1741 walk_tree (&init, force_labels_r, NULL, NULL);
1745 return GS_ALL_DONE;
1748 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1749 and replacing the LOOP_EXPR with goto, but if the loop contains an
1750 EXIT_EXPR, we need to append a label for it to jump to. */
1752 static enum gimplify_status
1753 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1755 tree saved_label = gimplify_ctxp->exit_label;
1756 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1758 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1760 gimplify_ctxp->exit_label = NULL_TREE;
1762 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1764 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1766 if (gimplify_ctxp->exit_label)
1767 gimplify_seq_add_stmt (pre_p,
1768 gimple_build_label (gimplify_ctxp->exit_label));
1770 gimplify_ctxp->exit_label = saved_label;
1772 *expr_p = NULL;
1773 return GS_ALL_DONE;
1776 /* Gimplify a statement list onto a sequence. These may be created either
1777 by an enlightened front-end, or by shortcut_cond_expr. */
1779 static enum gimplify_status
1780 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1782 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1784 tree_stmt_iterator i = tsi_start (*expr_p);
1786 while (!tsi_end_p (i))
1788 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1789 tsi_delink (&i);
1792 if (temp)
1794 *expr_p = temp;
1795 return GS_OK;
1798 return GS_ALL_DONE;
1801 /* Callback for walk_gimple_seq. */
1803 static tree
1804 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1805 struct walk_stmt_info *wi)
1807 gimple *stmt = gsi_stmt (*gsi_p);
1809 *handled_ops_p = true;
1810 switch (gimple_code (stmt))
1812 case GIMPLE_TRY:
1813 /* A compiler-generated cleanup or a user-written try block.
1814 If it's empty, don't dive into it--that would result in
1815 worse location info. */
1816 if (gimple_try_eval (stmt) == NULL)
1818 wi->info = stmt;
1819 return integer_zero_node;
1821 /* Fall through. */
1822 case GIMPLE_BIND:
1823 case GIMPLE_CATCH:
1824 case GIMPLE_EH_FILTER:
1825 case GIMPLE_TRANSACTION:
1826 /* Walk the sub-statements. */
1827 *handled_ops_p = false;
1828 break;
1830 case GIMPLE_DEBUG:
1831 /* Ignore these. We may generate them before declarations that
1832 are never executed. If there's something to warn about,
1833 there will be non-debug stmts too, and we'll catch those. */
1834 break;
1836 case GIMPLE_CALL:
1837 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1839 *handled_ops_p = false;
1840 break;
1842 /* Fall through. */
1843 default:
1844 /* Save the first "real" statement (not a decl/lexical scope/...). */
1845 wi->info = stmt;
1846 return integer_zero_node;
1848 return NULL_TREE;
1851 /* Possibly warn about unreachable statements between switch's controlling
1852 expression and the first case. SEQ is the body of a switch expression. */
1854 static void
1855 maybe_warn_switch_unreachable (gimple_seq seq)
1857 if (!warn_switch_unreachable
1858 /* This warning doesn't play well with Fortran when optimizations
1859 are on. */
1860 || lang_GNU_Fortran ()
1861 || seq == NULL)
1862 return;
1864 struct walk_stmt_info wi;
1865 memset (&wi, 0, sizeof (wi));
1866 walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1867 gimple *stmt = (gimple *) wi.info;
1869 if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1871 if (gimple_code (stmt) == GIMPLE_GOTO
1872 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1873 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1874 /* Don't warn for compiler-generated gotos. These occur
1875 in Duff's devices, for example. */;
1876 else
1877 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1878 "statement will never be executed");
1883 /* A label entry that pairs label and a location. */
1884 struct label_entry
1886 tree label;
1887 location_t loc;
1890 /* Find LABEL in vector of label entries VEC. */
1892 static struct label_entry *
1893 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1895 unsigned int i;
1896 struct label_entry *l;
1898 FOR_EACH_VEC_ELT (*vec, i, l)
1899 if (l->label == label)
1900 return l;
1901 return NULL;
1904 /* Return true if LABEL, a LABEL_DECL, represents a case label
1905 in a vector of labels CASES. */
1907 static bool
1908 case_label_p (const vec<tree> *cases, tree label)
1910 unsigned int i;
1911 tree l;
1913 FOR_EACH_VEC_ELT (*cases, i, l)
1914 if (CASE_LABEL (l) == label)
1915 return true;
1916 return false;
1919 /* Find the last nondebug statement in a scope STMT. */
1921 static gimple *
1922 last_stmt_in_scope (gimple *stmt)
1924 if (!stmt)
1925 return NULL;
1927 switch (gimple_code (stmt))
1929 case GIMPLE_BIND:
1931 gbind *bind = as_a <gbind *> (stmt);
1932 stmt = gimple_seq_last_nondebug_stmt (gimple_bind_body (bind));
1933 return last_stmt_in_scope (stmt);
1936 case GIMPLE_TRY:
1938 gtry *try_stmt = as_a <gtry *> (stmt);
1939 stmt = gimple_seq_last_nondebug_stmt (gimple_try_eval (try_stmt));
1940 gimple *last_eval = last_stmt_in_scope (stmt);
1941 if (gimple_stmt_may_fallthru (last_eval)
1942 && (last_eval == NULL
1943 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1944 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1946 stmt = gimple_seq_last_nondebug_stmt (gimple_try_cleanup (try_stmt));
1947 return last_stmt_in_scope (stmt);
1949 else
1950 return last_eval;
1953 case GIMPLE_DEBUG:
1954 gcc_unreachable ();
1956 default:
1957 return stmt;
1961 /* Collect interesting labels in LABELS and return the statement preceding
1962 another case label, or a user-defined label. Store a location useful
1963 to give warnings at *PREVLOC (usually the location of the returned
1964 statement or of its surrounding scope). */
1966 static gimple *
1967 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
1968 auto_vec <struct label_entry> *labels,
1969 location_t *prevloc)
1971 gimple *prev = NULL;
1973 *prevloc = UNKNOWN_LOCATION;
1976 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND)
1978 /* Recognize the special GIMPLE_BIND added by gimplify_switch_expr,
1979 which starts on a GIMPLE_SWITCH and ends with a break label.
1980 Handle that as a single statement that can fall through. */
1981 gbind *bind = as_a <gbind *> (gsi_stmt (*gsi_p));
1982 gimple *first = gimple_seq_first_stmt (gimple_bind_body (bind));
1983 gimple *last = gimple_seq_last_stmt (gimple_bind_body (bind));
1984 if (last
1985 && gimple_code (first) == GIMPLE_SWITCH
1986 && gimple_code (last) == GIMPLE_LABEL)
1988 tree label = gimple_label_label (as_a <glabel *> (last));
1989 if (SWITCH_BREAK_LABEL_P (label))
1991 prev = bind;
1992 gsi_next (gsi_p);
1993 continue;
1997 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
1998 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
2000 /* Nested scope. Only look at the last statement of
2001 the innermost scope. */
2002 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
2003 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
2004 if (last)
2006 prev = last;
2007 /* It might be a label without a location. Use the
2008 location of the scope then. */
2009 if (!gimple_has_location (prev))
2010 *prevloc = bind_loc;
2012 gsi_next (gsi_p);
2013 continue;
2016 /* Ifs are tricky. */
2017 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
2019 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
2020 tree false_lab = gimple_cond_false_label (cond_stmt);
2021 location_t if_loc = gimple_location (cond_stmt);
2023 /* If we have e.g.
2024 if (i > 1) goto <D.2259>; else goto D;
2025 we can't do much with the else-branch. */
2026 if (!DECL_ARTIFICIAL (false_lab))
2027 break;
2029 /* Go on until the false label, then one step back. */
2030 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
2032 gimple *stmt = gsi_stmt (*gsi_p);
2033 if (gimple_code (stmt) == GIMPLE_LABEL
2034 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
2035 break;
2038 /* Not found? Oops. */
2039 if (gsi_end_p (*gsi_p))
2040 break;
2042 struct label_entry l = { false_lab, if_loc };
2043 labels->safe_push (l);
2045 /* Go to the last statement of the then branch. */
2046 gsi_prev (gsi_p);
2048 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
2049 <D.1759>:
2050 <stmt>;
2051 goto <D.1761>;
2052 <D.1760>:
2054 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
2055 && !gimple_has_location (gsi_stmt (*gsi_p)))
2057 /* Look at the statement before, it might be
2058 attribute fallthrough, in which case don't warn. */
2059 gsi_prev (gsi_p);
2060 bool fallthru_before_dest
2061 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
2062 gsi_next (gsi_p);
2063 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
2064 if (!fallthru_before_dest)
2066 struct label_entry l = { goto_dest, if_loc };
2067 labels->safe_push (l);
2070 /* And move back. */
2071 gsi_next (gsi_p);
2074 /* Remember the last statement. Skip labels that are of no interest
2075 to us. */
2076 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2078 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
2079 if (find_label_entry (labels, label))
2080 prev = gsi_stmt (*gsi_p);
2082 else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
2084 else if (!is_gimple_debug (gsi_stmt (*gsi_p)))
2085 prev = gsi_stmt (*gsi_p);
2086 gsi_next (gsi_p);
2088 while (!gsi_end_p (*gsi_p)
2089 /* Stop if we find a case or a user-defined label. */
2090 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
2091 || !gimple_has_location (gsi_stmt (*gsi_p))));
2093 if (prev && gimple_has_location (prev))
2094 *prevloc = gimple_location (prev);
2095 return prev;
2098 /* Return true if the switch fallthough warning should occur. LABEL is
2099 the label statement that we're falling through to. */
2101 static bool
2102 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
2104 gimple_stmt_iterator gsi = *gsi_p;
2106 /* Don't warn if the label is marked with a "falls through" comment. */
2107 if (FALLTHROUGH_LABEL_P (label))
2108 return false;
2110 /* Don't warn for non-case labels followed by a statement:
2111 case 0:
2112 foo ();
2113 label:
2114 bar ();
2115 as these are likely intentional. */
2116 if (!case_label_p (&gimplify_ctxp->case_labels, label))
2118 tree l;
2119 while (!gsi_end_p (gsi)
2120 && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2121 && (l = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi))))
2122 && !case_label_p (&gimplify_ctxp->case_labels, l))
2123 gsi_next_nondebug (&gsi);
2124 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
2125 return false;
2128 /* Don't warn for terminated branches, i.e. when the subsequent case labels
2129 immediately breaks. */
2130 gsi = *gsi_p;
2132 /* Skip all immediately following labels. */
2133 while (!gsi_end_p (gsi)
2134 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
2135 || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT))
2136 gsi_next_nondebug (&gsi);
2138 /* { ... something; default:; } */
2139 if (gsi_end_p (gsi)
2140 /* { ... something; default: break; } or
2141 { ... something; default: goto L; } */
2142 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2143 /* { ... something; default: return; } */
2144 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2145 return false;
2147 return true;
2150 /* Callback for walk_gimple_seq. */
2152 static tree
2153 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2154 struct walk_stmt_info *)
2156 gimple *stmt = gsi_stmt (*gsi_p);
2158 *handled_ops_p = true;
2159 switch (gimple_code (stmt))
2161 case GIMPLE_TRY:
2162 case GIMPLE_BIND:
2163 case GIMPLE_CATCH:
2164 case GIMPLE_EH_FILTER:
2165 case GIMPLE_TRANSACTION:
2166 /* Walk the sub-statements. */
2167 *handled_ops_p = false;
2168 break;
2170 /* Find a sequence of form:
2172 GIMPLE_LABEL
2173 [...]
2174 <may fallthru stmt>
2175 GIMPLE_LABEL
2177 and possibly warn. */
2178 case GIMPLE_LABEL:
2180 /* Found a label. Skip all immediately following labels. */
2181 while (!gsi_end_p (*gsi_p)
2182 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2183 gsi_next_nondebug (gsi_p);
2185 /* There might be no more statements. */
2186 if (gsi_end_p (*gsi_p))
2187 return integer_zero_node;
2189 /* Vector of labels that fall through. */
2190 auto_vec <struct label_entry> labels;
2191 location_t prevloc;
2192 gimple *prev = collect_fallthrough_labels (gsi_p, &labels, &prevloc);
2194 /* There might be no more statements. */
2195 if (gsi_end_p (*gsi_p))
2196 return integer_zero_node;
2198 gimple *next = gsi_stmt (*gsi_p);
2199 tree label;
2200 /* If what follows is a label, then we may have a fallthrough. */
2201 if (gimple_code (next) == GIMPLE_LABEL
2202 && gimple_has_location (next)
2203 && (label = gimple_label_label (as_a <glabel *> (next)))
2204 && prev != NULL)
2206 struct label_entry *l;
2207 bool warned_p = false;
2208 auto_diagnostic_group d;
2209 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2210 /* Quiet. */;
2211 else if (gimple_code (prev) == GIMPLE_LABEL
2212 && (label = gimple_label_label (as_a <glabel *> (prev)))
2213 && (l = find_label_entry (&labels, label)))
2214 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2215 "this statement may fall through");
2216 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2217 /* Try to be clever and don't warn when the statement
2218 can't actually fall through. */
2219 && gimple_stmt_may_fallthru (prev)
2220 && prevloc != UNKNOWN_LOCATION)
2221 warned_p = warning_at (prevloc,
2222 OPT_Wimplicit_fallthrough_,
2223 "this statement may fall through");
2224 if (warned_p)
2225 inform (gimple_location (next), "here");
2227 /* Mark this label as processed so as to prevent multiple
2228 warnings in nested switches. */
2229 FALLTHROUGH_LABEL_P (label) = true;
2231 /* So that next warn_implicit_fallthrough_r will start looking for
2232 a new sequence starting with this label. */
2233 gsi_prev (gsi_p);
2236 break;
2237 default:
2238 break;
2240 return NULL_TREE;
2243 /* Warn when a switch case falls through. */
2245 static void
2246 maybe_warn_implicit_fallthrough (gimple_seq seq)
2248 if (!warn_implicit_fallthrough)
2249 return;
2251 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2252 if (!(lang_GNU_C ()
2253 || lang_GNU_CXX ()
2254 || lang_GNU_OBJC ()))
2255 return;
2257 struct walk_stmt_info wi;
2258 memset (&wi, 0, sizeof (wi));
2259 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2262 /* Callback for walk_gimple_seq. */
2264 static tree
2265 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2266 struct walk_stmt_info *)
2268 gimple *stmt = gsi_stmt (*gsi_p);
2270 *handled_ops_p = true;
2271 switch (gimple_code (stmt))
2273 case GIMPLE_TRY:
2274 case GIMPLE_BIND:
2275 case GIMPLE_CATCH:
2276 case GIMPLE_EH_FILTER:
2277 case GIMPLE_TRANSACTION:
2278 /* Walk the sub-statements. */
2279 *handled_ops_p = false;
2280 break;
2281 case GIMPLE_CALL:
2282 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2284 gsi_remove (gsi_p, true);
2285 if (gsi_end_p (*gsi_p))
2286 return integer_zero_node;
2288 bool found = false;
2289 location_t loc = gimple_location (stmt);
2291 gimple_stmt_iterator gsi2 = *gsi_p;
2292 stmt = gsi_stmt (gsi2);
2293 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2295 /* Go on until the artificial label. */
2296 tree goto_dest = gimple_goto_dest (stmt);
2297 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2299 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2300 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2301 == goto_dest)
2302 break;
2305 /* Not found? Stop. */
2306 if (gsi_end_p (gsi2))
2307 break;
2309 /* Look one past it. */
2310 gsi_next (&gsi2);
2313 /* We're looking for a case label or default label here. */
2314 while (!gsi_end_p (gsi2))
2316 stmt = gsi_stmt (gsi2);
2317 if (gimple_code (stmt) == GIMPLE_LABEL)
2319 tree label = gimple_label_label (as_a <glabel *> (stmt));
2320 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2322 found = true;
2323 break;
2326 else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
2328 else if (!is_gimple_debug (stmt))
2329 /* Anything else is not expected. */
2330 break;
2331 gsi_next (&gsi2);
2333 if (!found)
2334 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2335 "a case label or default label");
2337 break;
2338 default:
2339 break;
2341 return NULL_TREE;
2344 /* Expand all FALLTHROUGH () calls in SEQ. */
2346 static void
2347 expand_FALLTHROUGH (gimple_seq *seq_p)
2349 struct walk_stmt_info wi;
2350 memset (&wi, 0, sizeof (wi));
2351 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2355 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2356 branch to. */
2358 static enum gimplify_status
2359 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2361 tree switch_expr = *expr_p;
2362 gimple_seq switch_body_seq = NULL;
2363 enum gimplify_status ret;
2364 tree index_type = TREE_TYPE (switch_expr);
2365 if (index_type == NULL_TREE)
2366 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2368 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2369 fb_rvalue);
2370 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2371 return ret;
2373 if (SWITCH_BODY (switch_expr))
2375 vec<tree> labels;
2376 vec<tree> saved_labels;
2377 hash_set<tree> *saved_live_switch_vars = NULL;
2378 tree default_case = NULL_TREE;
2379 gswitch *switch_stmt;
2381 /* Save old labels, get new ones from body, then restore the old
2382 labels. Save all the things from the switch body to append after. */
2383 saved_labels = gimplify_ctxp->case_labels;
2384 gimplify_ctxp->case_labels.create (8);
2386 /* Do not create live_switch_vars if SWITCH_BODY is not a BIND_EXPR. */
2387 saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2388 tree_code body_type = TREE_CODE (SWITCH_BODY (switch_expr));
2389 if (body_type == BIND_EXPR || body_type == STATEMENT_LIST)
2390 gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2391 else
2392 gimplify_ctxp->live_switch_vars = NULL;
2394 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2395 gimplify_ctxp->in_switch_expr = true;
2397 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2399 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2400 maybe_warn_switch_unreachable (switch_body_seq);
2401 maybe_warn_implicit_fallthrough (switch_body_seq);
2402 /* Only do this for the outermost GIMPLE_SWITCH. */
2403 if (!gimplify_ctxp->in_switch_expr)
2404 expand_FALLTHROUGH (&switch_body_seq);
2406 labels = gimplify_ctxp->case_labels;
2407 gimplify_ctxp->case_labels = saved_labels;
2409 if (gimplify_ctxp->live_switch_vars)
2411 gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0);
2412 delete gimplify_ctxp->live_switch_vars;
2414 gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2416 preprocess_case_label_vec_for_gimple (labels, index_type,
2417 &default_case);
2419 bool add_bind = false;
2420 if (!default_case)
2422 glabel *new_default;
2424 default_case
2425 = build_case_label (NULL_TREE, NULL_TREE,
2426 create_artificial_label (UNKNOWN_LOCATION));
2427 if (old_in_switch_expr)
2429 SWITCH_BREAK_LABEL_P (CASE_LABEL (default_case)) = 1;
2430 add_bind = true;
2432 new_default = gimple_build_label (CASE_LABEL (default_case));
2433 gimplify_seq_add_stmt (&switch_body_seq, new_default);
2435 else if (old_in_switch_expr)
2437 gimple *last = gimple_seq_last_stmt (switch_body_seq);
2438 if (last && gimple_code (last) == GIMPLE_LABEL)
2440 tree label = gimple_label_label (as_a <glabel *> (last));
2441 if (SWITCH_BREAK_LABEL_P (label))
2442 add_bind = true;
2446 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2447 default_case, labels);
2448 /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
2449 ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
2450 wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
2451 so that we can easily find the start and end of the switch
2452 statement. */
2453 if (add_bind)
2455 gimple_seq bind_body = NULL;
2456 gimplify_seq_add_stmt (&bind_body, switch_stmt);
2457 gimple_seq_add_seq (&bind_body, switch_body_seq);
2458 gbind *bind = gimple_build_bind (NULL_TREE, bind_body, NULL_TREE);
2459 gimple_set_location (bind, EXPR_LOCATION (switch_expr));
2460 gimplify_seq_add_stmt (pre_p, bind);
2462 else
2464 gimplify_seq_add_stmt (pre_p, switch_stmt);
2465 gimplify_seq_add_seq (pre_p, switch_body_seq);
2467 labels.release ();
2469 else
2470 gcc_unreachable ();
2472 return GS_ALL_DONE;
2475 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2477 static enum gimplify_status
2478 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2480 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2481 == current_function_decl);
2483 tree label = LABEL_EXPR_LABEL (*expr_p);
2484 glabel *label_stmt = gimple_build_label (label);
2485 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2486 gimplify_seq_add_stmt (pre_p, label_stmt);
2488 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2489 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2490 NOT_TAKEN));
2491 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2492 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2493 TAKEN));
2495 return GS_ALL_DONE;
2498 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2500 static enum gimplify_status
2501 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2503 struct gimplify_ctx *ctxp;
2504 glabel *label_stmt;
2506 /* Invalid programs can play Duff's Device type games with, for example,
2507 #pragma omp parallel. At least in the C front end, we don't
2508 detect such invalid branches until after gimplification, in the
2509 diagnose_omp_blocks pass. */
2510 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2511 if (ctxp->case_labels.exists ())
2512 break;
2514 tree label = CASE_LABEL (*expr_p);
2515 label_stmt = gimple_build_label (label);
2516 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2517 ctxp->case_labels.safe_push (*expr_p);
2518 gimplify_seq_add_stmt (pre_p, label_stmt);
2520 if (lookup_attribute ("cold", DECL_ATTRIBUTES (label)))
2521 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_COLD_LABEL,
2522 NOT_TAKEN));
2523 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (label)))
2524 gimple_seq_add_stmt (pre_p, gimple_build_predict (PRED_HOT_LABEL,
2525 TAKEN));
2527 return GS_ALL_DONE;
2530 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2531 if necessary. */
2533 tree
2534 build_and_jump (tree *label_p)
2536 if (label_p == NULL)
2537 /* If there's nowhere to jump, just fall through. */
2538 return NULL_TREE;
2540 if (*label_p == NULL_TREE)
2542 tree label = create_artificial_label (UNKNOWN_LOCATION);
2543 *label_p = label;
2546 return build1 (GOTO_EXPR, void_type_node, *label_p);
2549 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2550 This also involves building a label to jump to and communicating it to
2551 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2553 static enum gimplify_status
2554 gimplify_exit_expr (tree *expr_p)
2556 tree cond = TREE_OPERAND (*expr_p, 0);
2557 tree expr;
2559 expr = build_and_jump (&gimplify_ctxp->exit_label);
2560 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2561 *expr_p = expr;
2563 return GS_OK;
2566 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2567 different from its canonical type, wrap the whole thing inside a
2568 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2569 type.
2571 The canonical type of a COMPONENT_REF is the type of the field being
2572 referenced--unless the field is a bit-field which can be read directly
2573 in a smaller mode, in which case the canonical type is the
2574 sign-appropriate type corresponding to that mode. */
2576 static void
2577 canonicalize_component_ref (tree *expr_p)
2579 tree expr = *expr_p;
2580 tree type;
2582 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2584 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2585 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2586 else
2587 type = TREE_TYPE (TREE_OPERAND (expr, 1));
2589 /* One could argue that all the stuff below is not necessary for
2590 the non-bitfield case and declare it a FE error if type
2591 adjustment would be needed. */
2592 if (TREE_TYPE (expr) != type)
2594 #ifdef ENABLE_TYPES_CHECKING
2595 tree old_type = TREE_TYPE (expr);
2596 #endif
2597 int type_quals;
2599 /* We need to preserve qualifiers and propagate them from
2600 operand 0. */
2601 type_quals = TYPE_QUALS (type)
2602 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2603 if (TYPE_QUALS (type) != type_quals)
2604 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2606 /* Set the type of the COMPONENT_REF to the underlying type. */
2607 TREE_TYPE (expr) = type;
2609 #ifdef ENABLE_TYPES_CHECKING
2610 /* It is now a FE error, if the conversion from the canonical
2611 type to the original expression type is not useless. */
2612 gcc_assert (useless_type_conversion_p (old_type, type));
2613 #endif
2617 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2618 to foo, embed that change in the ADDR_EXPR by converting
2619 T array[U];
2620 (T *)&array
2622 &array[L]
2623 where L is the lower bound. For simplicity, only do this for constant
2624 lower bound.
2625 The constraint is that the type of &array[L] is trivially convertible
2626 to T *. */
2628 static void
2629 canonicalize_addr_expr (tree *expr_p)
2631 tree expr = *expr_p;
2632 tree addr_expr = TREE_OPERAND (expr, 0);
2633 tree datype, ddatype, pddatype;
2635 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2636 if (!POINTER_TYPE_P (TREE_TYPE (expr))
2637 || TREE_CODE (addr_expr) != ADDR_EXPR)
2638 return;
2640 /* The addr_expr type should be a pointer to an array. */
2641 datype = TREE_TYPE (TREE_TYPE (addr_expr));
2642 if (TREE_CODE (datype) != ARRAY_TYPE)
2643 return;
2645 /* The pointer to element type shall be trivially convertible to
2646 the expression pointer type. */
2647 ddatype = TREE_TYPE (datype);
2648 pddatype = build_pointer_type (ddatype);
2649 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2650 pddatype))
2651 return;
2653 /* The lower bound and element sizes must be constant. */
2654 if (!TYPE_SIZE_UNIT (ddatype)
2655 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2656 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2657 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2658 return;
2660 /* All checks succeeded. Build a new node to merge the cast. */
2661 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2662 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2663 NULL_TREE, NULL_TREE);
2664 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2666 /* We can have stripped a required restrict qualifier above. */
2667 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2668 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2671 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2672 underneath as appropriate. */
2674 static enum gimplify_status
2675 gimplify_conversion (tree *expr_p)
2677 location_t loc = EXPR_LOCATION (*expr_p);
2678 gcc_assert (CONVERT_EXPR_P (*expr_p));
2680 /* Then strip away all but the outermost conversion. */
2681 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2683 /* And remove the outermost conversion if it's useless. */
2684 if (tree_ssa_useless_type_conversion (*expr_p))
2685 *expr_p = TREE_OPERAND (*expr_p, 0);
2687 /* If we still have a conversion at the toplevel,
2688 then canonicalize some constructs. */
2689 if (CONVERT_EXPR_P (*expr_p))
2691 tree sub = TREE_OPERAND (*expr_p, 0);
2693 /* If a NOP conversion is changing the type of a COMPONENT_REF
2694 expression, then canonicalize its type now in order to expose more
2695 redundant conversions. */
2696 if (TREE_CODE (sub) == COMPONENT_REF)
2697 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2699 /* If a NOP conversion is changing a pointer to array of foo
2700 to a pointer to foo, embed that change in the ADDR_EXPR. */
2701 else if (TREE_CODE (sub) == ADDR_EXPR)
2702 canonicalize_addr_expr (expr_p);
2705 /* If we have a conversion to a non-register type force the
2706 use of a VIEW_CONVERT_EXPR instead. */
2707 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2708 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2709 TREE_OPERAND (*expr_p, 0));
2711 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2712 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2713 TREE_SET_CODE (*expr_p, NOP_EXPR);
2715 return GS_OK;
2718 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2719 DECL_VALUE_EXPR, and it's worth re-examining things. */
2721 static enum gimplify_status
2722 gimplify_var_or_parm_decl (tree *expr_p)
2724 tree decl = *expr_p;
2726 /* ??? If this is a local variable, and it has not been seen in any
2727 outer BIND_EXPR, then it's probably the result of a duplicate
2728 declaration, for which we've already issued an error. It would
2729 be really nice if the front end wouldn't leak these at all.
2730 Currently the only known culprit is C++ destructors, as seen
2731 in g++.old-deja/g++.jason/binding.C. */
2732 if (VAR_P (decl)
2733 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2734 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2735 && decl_function_context (decl) == current_function_decl)
2737 gcc_assert (seen_error ());
2738 return GS_ERROR;
2741 /* When within an OMP context, notice uses of variables. */
2742 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2743 return GS_ALL_DONE;
2745 /* If the decl is an alias for another expression, substitute it now. */
2746 if (DECL_HAS_VALUE_EXPR_P (decl))
2748 *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
2749 return GS_OK;
2752 return GS_ALL_DONE;
2755 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2757 static void
2758 recalculate_side_effects (tree t)
2760 enum tree_code code = TREE_CODE (t);
2761 int len = TREE_OPERAND_LENGTH (t);
2762 int i;
2764 switch (TREE_CODE_CLASS (code))
2766 case tcc_expression:
2767 switch (code)
2769 case INIT_EXPR:
2770 case MODIFY_EXPR:
2771 case VA_ARG_EXPR:
2772 case PREDECREMENT_EXPR:
2773 case PREINCREMENT_EXPR:
2774 case POSTDECREMENT_EXPR:
2775 case POSTINCREMENT_EXPR:
2776 /* All of these have side-effects, no matter what their
2777 operands are. */
2778 return;
2780 default:
2781 break;
2783 /* Fall through. */
2785 case tcc_comparison: /* a comparison expression */
2786 case tcc_unary: /* a unary arithmetic expression */
2787 case tcc_binary: /* a binary arithmetic expression */
2788 case tcc_reference: /* a reference */
2789 case tcc_vl_exp: /* a function call */
2790 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2791 for (i = 0; i < len; ++i)
2793 tree op = TREE_OPERAND (t, i);
2794 if (op && TREE_SIDE_EFFECTS (op))
2795 TREE_SIDE_EFFECTS (t) = 1;
2797 break;
2799 case tcc_constant:
2800 /* No side-effects. */
2801 return;
2803 default:
2804 gcc_unreachable ();
2808 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2809 node *EXPR_P.
2811 compound_lval
2812 : min_lval '[' val ']'
2813 | min_lval '.' ID
2814 | compound_lval '[' val ']'
2815 | compound_lval '.' ID
2817 This is not part of the original SIMPLE definition, which separates
2818 array and member references, but it seems reasonable to handle them
2819 together. Also, this way we don't run into problems with union
2820 aliasing; gcc requires that for accesses through a union to alias, the
2821 union reference must be explicit, which was not always the case when we
2822 were splitting up array and member refs.
2824 PRE_P points to the sequence where side effects that must happen before
2825 *EXPR_P should be stored.
2827 POST_P points to the sequence where side effects that must happen after
2828 *EXPR_P should be stored. */
2830 static enum gimplify_status
2831 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2832 fallback_t fallback)
2834 tree *p;
2835 enum gimplify_status ret = GS_ALL_DONE, tret;
2836 int i;
2837 location_t loc = EXPR_LOCATION (*expr_p);
2838 tree expr = *expr_p;
2840 /* Create a stack of the subexpressions so later we can walk them in
2841 order from inner to outer. */
2842 auto_vec<tree, 10> expr_stack;
2844 /* We can handle anything that get_inner_reference can deal with. */
2845 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2847 restart:
2848 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2849 if (TREE_CODE (*p) == INDIRECT_REF)
2850 *p = fold_indirect_ref_loc (loc, *p);
2852 if (handled_component_p (*p))
2854 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2855 additional COMPONENT_REFs. */
2856 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2857 && gimplify_var_or_parm_decl (p) == GS_OK)
2858 goto restart;
2859 else
2860 break;
2862 expr_stack.safe_push (*p);
2865 gcc_assert (expr_stack.length ());
2867 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2868 walked through and P points to the innermost expression.
2870 Java requires that we elaborated nodes in source order. That
2871 means we must gimplify the inner expression followed by each of
2872 the indices, in order. But we can't gimplify the inner
2873 expression until we deal with any variable bounds, sizes, or
2874 positions in order to deal with PLACEHOLDER_EXPRs.
2876 So we do this in three steps. First we deal with the annotations
2877 for any variables in the components, then we gimplify the base,
2878 then we gimplify any indices, from left to right. */
2879 for (i = expr_stack.length () - 1; i >= 0; i--)
2881 tree t = expr_stack[i];
2883 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2885 /* Gimplify the low bound and element type size and put them into
2886 the ARRAY_REF. If these values are set, they have already been
2887 gimplified. */
2888 if (TREE_OPERAND (t, 2) == NULL_TREE)
2890 tree low = unshare_expr (array_ref_low_bound (t));
2891 if (!is_gimple_min_invariant (low))
2893 TREE_OPERAND (t, 2) = low;
2894 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2895 post_p, is_gimple_reg,
2896 fb_rvalue);
2897 ret = MIN (ret, tret);
2900 else
2902 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2903 is_gimple_reg, fb_rvalue);
2904 ret = MIN (ret, tret);
2907 if (TREE_OPERAND (t, 3) == NULL_TREE)
2909 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2910 tree elmt_size = unshare_expr (array_ref_element_size (t));
2911 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2913 /* Divide the element size by the alignment of the element
2914 type (above). */
2915 elmt_size
2916 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2918 if (!is_gimple_min_invariant (elmt_size))
2920 TREE_OPERAND (t, 3) = elmt_size;
2921 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2922 post_p, is_gimple_reg,
2923 fb_rvalue);
2924 ret = MIN (ret, tret);
2927 else
2929 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2930 is_gimple_reg, fb_rvalue);
2931 ret = MIN (ret, tret);
2934 else if (TREE_CODE (t) == COMPONENT_REF)
2936 /* Set the field offset into T and gimplify it. */
2937 if (TREE_OPERAND (t, 2) == NULL_TREE)
2939 tree offset = unshare_expr (component_ref_field_offset (t));
2940 tree field = TREE_OPERAND (t, 1);
2941 tree factor
2942 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2944 /* Divide the offset by its alignment. */
2945 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2947 if (!is_gimple_min_invariant (offset))
2949 TREE_OPERAND (t, 2) = offset;
2950 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2951 post_p, is_gimple_reg,
2952 fb_rvalue);
2953 ret = MIN (ret, tret);
2956 else
2958 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2959 is_gimple_reg, fb_rvalue);
2960 ret = MIN (ret, tret);
2965 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2966 so as to match the min_lval predicate. Failure to do so may result
2967 in the creation of large aggregate temporaries. */
2968 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2969 fallback | fb_lvalue);
2970 ret = MIN (ret, tret);
2972 /* And finally, the indices and operands of ARRAY_REF. During this
2973 loop we also remove any useless conversions. */
2974 for (; expr_stack.length () > 0; )
2976 tree t = expr_stack.pop ();
2978 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2980 /* Gimplify the dimension. */
2981 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2983 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2984 is_gimple_val, fb_rvalue);
2985 ret = MIN (ret, tret);
2989 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2991 /* The innermost expression P may have originally had
2992 TREE_SIDE_EFFECTS set which would have caused all the outer
2993 expressions in *EXPR_P leading to P to also have had
2994 TREE_SIDE_EFFECTS set. */
2995 recalculate_side_effects (t);
2998 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2999 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
3001 canonicalize_component_ref (expr_p);
3004 expr_stack.release ();
3006 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
3008 return ret;
3011 /* Gimplify the self modifying expression pointed to by EXPR_P
3012 (++, --, +=, -=).
3014 PRE_P points to the list where side effects that must happen before
3015 *EXPR_P should be stored.
3017 POST_P points to the list where side effects that must happen after
3018 *EXPR_P should be stored.
3020 WANT_VALUE is nonzero iff we want to use the value of this expression
3021 in another expression.
3023 ARITH_TYPE is the type the computation should be performed in. */
3025 enum gimplify_status
3026 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3027 bool want_value, tree arith_type)
3029 enum tree_code code;
3030 tree lhs, lvalue, rhs, t1;
3031 gimple_seq post = NULL, *orig_post_p = post_p;
3032 bool postfix;
3033 enum tree_code arith_code;
3034 enum gimplify_status ret;
3035 location_t loc = EXPR_LOCATION (*expr_p);
3037 code = TREE_CODE (*expr_p);
3039 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
3040 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
3042 /* Prefix or postfix? */
3043 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3044 /* Faster to treat as prefix if result is not used. */
3045 postfix = want_value;
3046 else
3047 postfix = false;
3049 /* For postfix, make sure the inner expression's post side effects
3050 are executed after side effects from this expression. */
3051 if (postfix)
3052 post_p = &post;
3054 /* Add or subtract? */
3055 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3056 arith_code = PLUS_EXPR;
3057 else
3058 arith_code = MINUS_EXPR;
3060 /* Gimplify the LHS into a GIMPLE lvalue. */
3061 lvalue = TREE_OPERAND (*expr_p, 0);
3062 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3063 if (ret == GS_ERROR)
3064 return ret;
3066 /* Extract the operands to the arithmetic operation. */
3067 lhs = lvalue;
3068 rhs = TREE_OPERAND (*expr_p, 1);
3070 /* For postfix operator, we evaluate the LHS to an rvalue and then use
3071 that as the result value and in the postqueue operation. */
3072 if (postfix)
3074 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
3075 if (ret == GS_ERROR)
3076 return ret;
3078 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
3081 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
3082 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
3084 rhs = convert_to_ptrofftype_loc (loc, rhs);
3085 if (arith_code == MINUS_EXPR)
3086 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3087 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
3089 else
3090 t1 = fold_convert (TREE_TYPE (*expr_p),
3091 fold_build2 (arith_code, arith_type,
3092 fold_convert (arith_type, lhs),
3093 fold_convert (arith_type, rhs)));
3095 if (postfix)
3097 gimplify_assign (lvalue, t1, pre_p);
3098 gimplify_seq_add_seq (orig_post_p, post);
3099 *expr_p = lhs;
3100 return GS_ALL_DONE;
3102 else
3104 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
3105 return GS_OK;
3109 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
3111 static void
3112 maybe_with_size_expr (tree *expr_p)
3114 tree expr = *expr_p;
3115 tree type = TREE_TYPE (expr);
3116 tree size;
3118 /* If we've already wrapped this or the type is error_mark_node, we can't do
3119 anything. */
3120 if (TREE_CODE (expr) == WITH_SIZE_EXPR
3121 || type == error_mark_node)
3122 return;
3124 /* If the size isn't known or is a constant, we have nothing to do. */
3125 size = TYPE_SIZE_UNIT (type);
3126 if (!size || poly_int_tree_p (size))
3127 return;
3129 /* Otherwise, make a WITH_SIZE_EXPR. */
3130 size = unshare_expr (size);
3131 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
3132 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
3135 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
3136 Store any side-effects in PRE_P. CALL_LOCATION is the location of
3137 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
3138 gimplified to an SSA name. */
3140 enum gimplify_status
3141 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
3142 bool allow_ssa)
3144 bool (*test) (tree);
3145 fallback_t fb;
3147 /* In general, we allow lvalues for function arguments to avoid
3148 extra overhead of copying large aggregates out of even larger
3149 aggregates into temporaries only to copy the temporaries to
3150 the argument list. Make optimizers happy by pulling out to
3151 temporaries those types that fit in registers. */
3152 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
3153 test = is_gimple_val, fb = fb_rvalue;
3154 else
3156 test = is_gimple_lvalue, fb = fb_either;
3157 /* Also strip a TARGET_EXPR that would force an extra copy. */
3158 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3160 tree init = TARGET_EXPR_INITIAL (*arg_p);
3161 if (init
3162 && !VOID_TYPE_P (TREE_TYPE (init)))
3163 *arg_p = init;
3167 /* If this is a variable sized type, we must remember the size. */
3168 maybe_with_size_expr (arg_p);
3170 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3171 /* Make sure arguments have the same location as the function call
3172 itself. */
3173 protected_set_expr_location (*arg_p, call_location);
3175 /* There is a sequence point before a function call. Side effects in
3176 the argument list must occur before the actual call. So, when
3177 gimplifying arguments, force gimplify_expr to use an internal
3178 post queue which is then appended to the end of PRE_P. */
3179 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3182 /* Don't fold inside offloading or taskreg regions: it can break code by
3183 adding decl references that weren't in the source. We'll do it during
3184 omplower pass instead. */
3186 static bool
3187 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3189 struct gimplify_omp_ctx *ctx;
3190 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3191 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3192 return false;
3193 else if ((ctx->region_type & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
3194 return false;
3195 /* Delay folding of builtins until the IL is in consistent state
3196 so the diagnostic machinery can do a better job. */
3197 if (gimple_call_builtin_p (gsi_stmt (*gsi)))
3198 return false;
3199 return fold_stmt (gsi);
3202 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3203 WANT_VALUE is true if the result of the call is desired. */
3205 static enum gimplify_status
3206 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3208 tree fndecl, parms, p, fnptrtype;
3209 enum gimplify_status ret;
3210 int i, nargs;
3211 gcall *call;
3212 bool builtin_va_start_p = false;
3213 location_t loc = EXPR_LOCATION (*expr_p);
3215 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3217 /* For reliable diagnostics during inlining, it is necessary that
3218 every call_expr be annotated with file and line. */
3219 if (! EXPR_HAS_LOCATION (*expr_p))
3220 SET_EXPR_LOCATION (*expr_p, input_location);
3222 /* Gimplify internal functions created in the FEs. */
3223 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3225 if (want_value)
3226 return GS_ALL_DONE;
3228 nargs = call_expr_nargs (*expr_p);
3229 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3230 auto_vec<tree> vargs (nargs);
3232 for (i = 0; i < nargs; i++)
3234 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3235 EXPR_LOCATION (*expr_p));
3236 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3239 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
3240 gimple_call_set_nothrow (call, TREE_NOTHROW (*expr_p));
3241 gimplify_seq_add_stmt (pre_p, call);
3242 return GS_ALL_DONE;
3245 /* This may be a call to a builtin function.
3247 Builtin function calls may be transformed into different
3248 (and more efficient) builtin function calls under certain
3249 circumstances. Unfortunately, gimplification can muck things
3250 up enough that the builtin expanders are not aware that certain
3251 transformations are still valid.
3253 So we attempt transformation/gimplification of the call before
3254 we gimplify the CALL_EXPR. At this time we do not manage to
3255 transform all calls in the same manner as the expanders do, but
3256 we do transform most of them. */
3257 fndecl = get_callee_fndecl (*expr_p);
3258 if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
3259 switch (DECL_FUNCTION_CODE (fndecl))
3261 CASE_BUILT_IN_ALLOCA:
3262 /* If the call has been built for a variable-sized object, then we
3263 want to restore the stack level when the enclosing BIND_EXPR is
3264 exited to reclaim the allocated space; otherwise, we precisely
3265 need to do the opposite and preserve the latest stack level. */
3266 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
3267 gimplify_ctxp->save_stack = true;
3268 else
3269 gimplify_ctxp->keep_stack = true;
3270 break;
3272 case BUILT_IN_VA_START:
3274 builtin_va_start_p = TRUE;
3275 if (call_expr_nargs (*expr_p) < 2)
3277 error ("too few arguments to function %<va_start%>");
3278 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3279 return GS_OK;
3282 if (fold_builtin_next_arg (*expr_p, true))
3284 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3285 return GS_OK;
3287 break;
3290 default:
3293 if (fndecl && fndecl_built_in_p (fndecl))
3295 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3296 if (new_tree && new_tree != *expr_p)
3298 /* There was a transformation of this call which computes the
3299 same value, but in a more efficient way. Return and try
3300 again. */
3301 *expr_p = new_tree;
3302 return GS_OK;
3306 /* Remember the original function pointer type. */
3307 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
3309 /* There is a sequence point before the call, so any side effects in
3310 the calling expression must occur before the actual call. Force
3311 gimplify_expr to use an internal post queue. */
3312 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
3313 is_gimple_call_addr, fb_rvalue);
3315 nargs = call_expr_nargs (*expr_p);
3317 /* Get argument types for verification. */
3318 fndecl = get_callee_fndecl (*expr_p);
3319 parms = NULL_TREE;
3320 if (fndecl)
3321 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3322 else
3323 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3325 if (fndecl && DECL_ARGUMENTS (fndecl))
3326 p = DECL_ARGUMENTS (fndecl);
3327 else if (parms)
3328 p = parms;
3329 else
3330 p = NULL_TREE;
3331 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3334 /* If the last argument is __builtin_va_arg_pack () and it is not
3335 passed as a named argument, decrease the number of CALL_EXPR
3336 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3337 if (!p
3338 && i < nargs
3339 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3341 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3342 tree last_arg_fndecl = get_callee_fndecl (last_arg);
3344 if (last_arg_fndecl
3345 && fndecl_built_in_p (last_arg_fndecl, BUILT_IN_VA_ARG_PACK))
3347 tree call = *expr_p;
3349 --nargs;
3350 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3351 CALL_EXPR_FN (call),
3352 nargs, CALL_EXPR_ARGP (call));
3354 /* Copy all CALL_EXPR flags, location and block, except
3355 CALL_EXPR_VA_ARG_PACK flag. */
3356 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3357 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3358 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3359 = CALL_EXPR_RETURN_SLOT_OPT (call);
3360 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3361 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3363 /* Set CALL_EXPR_VA_ARG_PACK. */
3364 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3368 /* If the call returns twice then after building the CFG the call
3369 argument computations will no longer dominate the call because
3370 we add an abnormal incoming edge to the call. So do not use SSA
3371 vars there. */
3372 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3374 /* Gimplify the function arguments. */
3375 if (nargs > 0)
3377 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3378 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3379 PUSH_ARGS_REVERSED ? i-- : i++)
3381 enum gimplify_status t;
3383 /* Avoid gimplifying the second argument to va_start, which needs to
3384 be the plain PARM_DECL. */
3385 if ((i != 1) || !builtin_va_start_p)
3387 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3388 EXPR_LOCATION (*expr_p), ! returns_twice);
3390 if (t == GS_ERROR)
3391 ret = GS_ERROR;
3396 /* Gimplify the static chain. */
3397 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3399 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3400 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3401 else
3403 enum gimplify_status t;
3404 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3405 EXPR_LOCATION (*expr_p), ! returns_twice);
3406 if (t == GS_ERROR)
3407 ret = GS_ERROR;
3411 /* Verify the function result. */
3412 if (want_value && fndecl
3413 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3415 error_at (loc, "using result of function returning %<void%>");
3416 ret = GS_ERROR;
3419 /* Try this again in case gimplification exposed something. */
3420 if (ret != GS_ERROR)
3422 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3424 if (new_tree && new_tree != *expr_p)
3426 /* There was a transformation of this call which computes the
3427 same value, but in a more efficient way. Return and try
3428 again. */
3429 *expr_p = new_tree;
3430 return GS_OK;
3433 else
3435 *expr_p = error_mark_node;
3436 return GS_ERROR;
3439 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3440 decl. This allows us to eliminate redundant or useless
3441 calls to "const" functions. */
3442 if (TREE_CODE (*expr_p) == CALL_EXPR)
3444 int flags = call_expr_flags (*expr_p);
3445 if (flags & (ECF_CONST | ECF_PURE)
3446 /* An infinite loop is considered a side effect. */
3447 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3448 TREE_SIDE_EFFECTS (*expr_p) = 0;
3451 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3452 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3453 form and delegate the creation of a GIMPLE_CALL to
3454 gimplify_modify_expr. This is always possible because when
3455 WANT_VALUE is true, the caller wants the result of this call into
3456 a temporary, which means that we will emit an INIT_EXPR in
3457 internal_get_tmp_var which will then be handled by
3458 gimplify_modify_expr. */
3459 if (!want_value)
3461 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3462 have to do is replicate it as a GIMPLE_CALL tuple. */
3463 gimple_stmt_iterator gsi;
3464 call = gimple_build_call_from_tree (*expr_p, fnptrtype);
3465 notice_special_calls (call);
3466 gimplify_seq_add_stmt (pre_p, call);
3467 gsi = gsi_last (*pre_p);
3468 maybe_fold_stmt (&gsi);
3469 *expr_p = NULL_TREE;
3471 else
3472 /* Remember the original function type. */
3473 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3474 CALL_EXPR_FN (*expr_p));
3476 return ret;
3479 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3480 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3482 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3483 condition is true or false, respectively. If null, we should generate
3484 our own to skip over the evaluation of this specific expression.
3486 LOCUS is the source location of the COND_EXPR.
3488 This function is the tree equivalent of do_jump.
3490 shortcut_cond_r should only be called by shortcut_cond_expr. */
3492 static tree
3493 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3494 location_t locus)
3496 tree local_label = NULL_TREE;
3497 tree t, expr = NULL;
3499 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3500 retain the shortcut semantics. Just insert the gotos here;
3501 shortcut_cond_expr will append the real blocks later. */
3502 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3504 location_t new_locus;
3506 /* Turn if (a && b) into
3508 if (a); else goto no;
3509 if (b) goto yes; else goto no;
3510 (no:) */
3512 if (false_label_p == NULL)
3513 false_label_p = &local_label;
3515 /* Keep the original source location on the first 'if'. */
3516 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3517 append_to_statement_list (t, &expr);
3519 /* Set the source location of the && on the second 'if'. */
3520 new_locus = rexpr_location (pred, locus);
3521 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3522 new_locus);
3523 append_to_statement_list (t, &expr);
3525 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3527 location_t new_locus;
3529 /* Turn if (a || b) into
3531 if (a) goto yes;
3532 if (b) goto yes; else goto no;
3533 (yes:) */
3535 if (true_label_p == NULL)
3536 true_label_p = &local_label;
3538 /* Keep the original source location on the first 'if'. */
3539 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3540 append_to_statement_list (t, &expr);
3542 /* Set the source location of the || on the second 'if'. */
3543 new_locus = rexpr_location (pred, locus);
3544 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3545 new_locus);
3546 append_to_statement_list (t, &expr);
3548 else if (TREE_CODE (pred) == COND_EXPR
3549 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3550 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3552 location_t new_locus;
3554 /* As long as we're messing with gotos, turn if (a ? b : c) into
3555 if (a)
3556 if (b) goto yes; else goto no;
3557 else
3558 if (c) goto yes; else goto no;
3560 Don't do this if one of the arms has void type, which can happen
3561 in C++ when the arm is throw. */
3563 /* Keep the original source location on the first 'if'. Set the source
3564 location of the ? on the second 'if'. */
3565 new_locus = rexpr_location (pred, locus);
3566 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3567 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3568 false_label_p, locus),
3569 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3570 false_label_p, new_locus));
3572 else
3574 expr = build3 (COND_EXPR, void_type_node, pred,
3575 build_and_jump (true_label_p),
3576 build_and_jump (false_label_p));
3577 SET_EXPR_LOCATION (expr, locus);
3580 if (local_label)
3582 t = build1 (LABEL_EXPR, void_type_node, local_label);
3583 append_to_statement_list (t, &expr);
3586 return expr;
3589 /* If EXPR is a GOTO_EXPR, return it. If it is a STATEMENT_LIST, skip
3590 any of its leading DEBUG_BEGIN_STMTS and recurse on the subsequent
3591 statement, if it is the last one. Otherwise, return NULL. */
3593 static tree
3594 find_goto (tree expr)
3596 if (!expr)
3597 return NULL_TREE;
3599 if (TREE_CODE (expr) == GOTO_EXPR)
3600 return expr;
3602 if (TREE_CODE (expr) != STATEMENT_LIST)
3603 return NULL_TREE;
3605 tree_stmt_iterator i = tsi_start (expr);
3607 while (!tsi_end_p (i) && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
3608 tsi_next (&i);
3610 if (!tsi_one_before_end_p (i))
3611 return NULL_TREE;
3613 return find_goto (tsi_stmt (i));
3616 /* Same as find_goto, except that it returns NULL if the destination
3617 is not a LABEL_DECL. */
3619 static inline tree
3620 find_goto_label (tree expr)
3622 tree dest = find_goto (expr);
3623 if (dest && TREE_CODE (GOTO_DESTINATION (dest)) == LABEL_DECL)
3624 return dest;
3625 return NULL_TREE;
3628 /* Given a conditional expression EXPR with short-circuit boolean
3629 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3630 predicate apart into the equivalent sequence of conditionals. */
3632 static tree
3633 shortcut_cond_expr (tree expr)
3635 tree pred = TREE_OPERAND (expr, 0);
3636 tree then_ = TREE_OPERAND (expr, 1);
3637 tree else_ = TREE_OPERAND (expr, 2);
3638 tree true_label, false_label, end_label, t;
3639 tree *true_label_p;
3640 tree *false_label_p;
3641 bool emit_end, emit_false, jump_over_else;
3642 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3643 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3645 /* First do simple transformations. */
3646 if (!else_se)
3648 /* If there is no 'else', turn
3649 if (a && b) then c
3650 into
3651 if (a) if (b) then c. */
3652 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3654 /* Keep the original source location on the first 'if'. */
3655 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3656 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3657 /* Set the source location of the && on the second 'if'. */
3658 if (rexpr_has_location (pred))
3659 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3660 then_ = shortcut_cond_expr (expr);
3661 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3662 pred = TREE_OPERAND (pred, 0);
3663 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3664 SET_EXPR_LOCATION (expr, locus);
3668 if (!then_se)
3670 /* If there is no 'then', turn
3671 if (a || b); else d
3672 into
3673 if (a); else if (b); else d. */
3674 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3676 /* Keep the original source location on the first 'if'. */
3677 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3678 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3679 /* Set the source location of the || on the second 'if'. */
3680 if (rexpr_has_location (pred))
3681 SET_EXPR_LOCATION (expr, rexpr_location (pred));
3682 else_ = shortcut_cond_expr (expr);
3683 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3684 pred = TREE_OPERAND (pred, 0);
3685 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3686 SET_EXPR_LOCATION (expr, locus);
3690 /* If we're done, great. */
3691 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3692 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3693 return expr;
3695 /* Otherwise we need to mess with gotos. Change
3696 if (a) c; else d;
3698 if (a); else goto no;
3699 c; goto end;
3700 no: d; end:
3701 and recursively gimplify the condition. */
3703 true_label = false_label = end_label = NULL_TREE;
3705 /* If our arms just jump somewhere, hijack those labels so we don't
3706 generate jumps to jumps. */
3708 if (tree then_goto = find_goto_label (then_))
3710 true_label = GOTO_DESTINATION (then_goto);
3711 then_ = NULL;
3712 then_se = false;
3715 if (tree else_goto = find_goto_label (else_))
3717 false_label = GOTO_DESTINATION (else_goto);
3718 else_ = NULL;
3719 else_se = false;
3722 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3723 if (true_label)
3724 true_label_p = &true_label;
3725 else
3726 true_label_p = NULL;
3728 /* The 'else' branch also needs a label if it contains interesting code. */
3729 if (false_label || else_se)
3730 false_label_p = &false_label;
3731 else
3732 false_label_p = NULL;
3734 /* If there was nothing else in our arms, just forward the label(s). */
3735 if (!then_se && !else_se)
3736 return shortcut_cond_r (pred, true_label_p, false_label_p,
3737 EXPR_LOC_OR_LOC (expr, input_location));
3739 /* If our last subexpression already has a terminal label, reuse it. */
3740 if (else_se)
3741 t = expr_last (else_);
3742 else if (then_se)
3743 t = expr_last (then_);
3744 else
3745 t = NULL;
3746 if (t && TREE_CODE (t) == LABEL_EXPR)
3747 end_label = LABEL_EXPR_LABEL (t);
3749 /* If we don't care about jumping to the 'else' branch, jump to the end
3750 if the condition is false. */
3751 if (!false_label_p)
3752 false_label_p = &end_label;
3754 /* We only want to emit these labels if we aren't hijacking them. */
3755 emit_end = (end_label == NULL_TREE);
3756 emit_false = (false_label == NULL_TREE);
3758 /* We only emit the jump over the else clause if we have to--if the
3759 then clause may fall through. Otherwise we can wind up with a
3760 useless jump and a useless label at the end of gimplified code,
3761 which will cause us to think that this conditional as a whole
3762 falls through even if it doesn't. If we then inline a function
3763 which ends with such a condition, that can cause us to issue an
3764 inappropriate warning about control reaching the end of a
3765 non-void function. */
3766 jump_over_else = block_may_fallthru (then_);
3768 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3769 EXPR_LOC_OR_LOC (expr, input_location));
3771 expr = NULL;
3772 append_to_statement_list (pred, &expr);
3774 append_to_statement_list (then_, &expr);
3775 if (else_se)
3777 if (jump_over_else)
3779 tree last = expr_last (expr);
3780 t = build_and_jump (&end_label);
3781 if (rexpr_has_location (last))
3782 SET_EXPR_LOCATION (t, rexpr_location (last));
3783 append_to_statement_list (t, &expr);
3785 if (emit_false)
3787 t = build1 (LABEL_EXPR, void_type_node, false_label);
3788 append_to_statement_list (t, &expr);
3790 append_to_statement_list (else_, &expr);
3792 if (emit_end && end_label)
3794 t = build1 (LABEL_EXPR, void_type_node, end_label);
3795 append_to_statement_list (t, &expr);
3798 return expr;
3801 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3803 tree
3804 gimple_boolify (tree expr)
3806 tree type = TREE_TYPE (expr);
3807 location_t loc = EXPR_LOCATION (expr);
3809 if (TREE_CODE (expr) == NE_EXPR
3810 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3811 && integer_zerop (TREE_OPERAND (expr, 1)))
3813 tree call = TREE_OPERAND (expr, 0);
3814 tree fn = get_callee_fndecl (call);
3816 /* For __builtin_expect ((long) (x), y) recurse into x as well
3817 if x is truth_value_p. */
3818 if (fn
3819 && fndecl_built_in_p (fn, BUILT_IN_EXPECT)
3820 && call_expr_nargs (call) == 2)
3822 tree arg = CALL_EXPR_ARG (call, 0);
3823 if (arg)
3825 if (TREE_CODE (arg) == NOP_EXPR
3826 && TREE_TYPE (arg) == TREE_TYPE (call))
3827 arg = TREE_OPERAND (arg, 0);
3828 if (truth_value_p (TREE_CODE (arg)))
3830 arg = gimple_boolify (arg);
3831 CALL_EXPR_ARG (call, 0)
3832 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3838 switch (TREE_CODE (expr))
3840 case TRUTH_AND_EXPR:
3841 case TRUTH_OR_EXPR:
3842 case TRUTH_XOR_EXPR:
3843 case TRUTH_ANDIF_EXPR:
3844 case TRUTH_ORIF_EXPR:
3845 /* Also boolify the arguments of truth exprs. */
3846 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3847 /* FALLTHRU */
3849 case TRUTH_NOT_EXPR:
3850 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3852 /* These expressions always produce boolean results. */
3853 if (TREE_CODE (type) != BOOLEAN_TYPE)
3854 TREE_TYPE (expr) = boolean_type_node;
3855 return expr;
3857 case ANNOTATE_EXPR:
3858 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3860 case annot_expr_ivdep_kind:
3861 case annot_expr_unroll_kind:
3862 case annot_expr_no_vector_kind:
3863 case annot_expr_vector_kind:
3864 case annot_expr_parallel_kind:
3865 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3866 if (TREE_CODE (type) != BOOLEAN_TYPE)
3867 TREE_TYPE (expr) = boolean_type_node;
3868 return expr;
3869 default:
3870 gcc_unreachable ();
3873 default:
3874 if (COMPARISON_CLASS_P (expr))
3876 /* There expressions always prduce boolean results. */
3877 if (TREE_CODE (type) != BOOLEAN_TYPE)
3878 TREE_TYPE (expr) = boolean_type_node;
3879 return expr;
3881 /* Other expressions that get here must have boolean values, but
3882 might need to be converted to the appropriate mode. */
3883 if (TREE_CODE (type) == BOOLEAN_TYPE)
3884 return expr;
3885 return fold_convert_loc (loc, boolean_type_node, expr);
3889 /* Given a conditional expression *EXPR_P without side effects, gimplify
3890 its operands. New statements are inserted to PRE_P. */
3892 static enum gimplify_status
3893 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3895 tree expr = *expr_p, cond;
3896 enum gimplify_status ret, tret;
3897 enum tree_code code;
3899 cond = gimple_boolify (COND_EXPR_COND (expr));
3901 /* We need to handle && and || specially, as their gimplification
3902 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3903 code = TREE_CODE (cond);
3904 if (code == TRUTH_ANDIF_EXPR)
3905 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3906 else if (code == TRUTH_ORIF_EXPR)
3907 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3908 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3909 COND_EXPR_COND (*expr_p) = cond;
3911 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3912 is_gimple_val, fb_rvalue);
3913 ret = MIN (ret, tret);
3914 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3915 is_gimple_val, fb_rvalue);
3917 return MIN (ret, tret);
3920 /* Return true if evaluating EXPR could trap.
3921 EXPR is GENERIC, while tree_could_trap_p can be called
3922 only on GIMPLE. */
3924 bool
3925 generic_expr_could_trap_p (tree expr)
3927 unsigned i, n;
3929 if (!expr || is_gimple_val (expr))
3930 return false;
3932 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3933 return true;
3935 n = TREE_OPERAND_LENGTH (expr);
3936 for (i = 0; i < n; i++)
3937 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3938 return true;
3940 return false;
3943 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3944 into
3946 if (p) if (p)
3947 t1 = a; a;
3948 else or else
3949 t1 = b; b;
3952 The second form is used when *EXPR_P is of type void.
3954 PRE_P points to the list where side effects that must happen before
3955 *EXPR_P should be stored. */
3957 static enum gimplify_status
3958 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3960 tree expr = *expr_p;
3961 tree type = TREE_TYPE (expr);
3962 location_t loc = EXPR_LOCATION (expr);
3963 tree tmp, arm1, arm2;
3964 enum gimplify_status ret;
3965 tree label_true, label_false, label_cont;
3966 bool have_then_clause_p, have_else_clause_p;
3967 gcond *cond_stmt;
3968 enum tree_code pred_code;
3969 gimple_seq seq = NULL;
3971 /* If this COND_EXPR has a value, copy the values into a temporary within
3972 the arms. */
3973 if (!VOID_TYPE_P (type))
3975 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3976 tree result;
3978 /* If either an rvalue is ok or we do not require an lvalue, create the
3979 temporary. But we cannot do that if the type is addressable. */
3980 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3981 && !TREE_ADDRESSABLE (type))
3983 if (gimplify_ctxp->allow_rhs_cond_expr
3984 /* If either branch has side effects or could trap, it can't be
3985 evaluated unconditionally. */
3986 && !TREE_SIDE_EFFECTS (then_)
3987 && !generic_expr_could_trap_p (then_)
3988 && !TREE_SIDE_EFFECTS (else_)
3989 && !generic_expr_could_trap_p (else_))
3990 return gimplify_pure_cond_expr (expr_p, pre_p);
3992 tmp = create_tmp_var (type, "iftmp");
3993 result = tmp;
3996 /* Otherwise, only create and copy references to the values. */
3997 else
3999 type = build_pointer_type (type);
4001 if (!VOID_TYPE_P (TREE_TYPE (then_)))
4002 then_ = build_fold_addr_expr_loc (loc, then_);
4004 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4005 else_ = build_fold_addr_expr_loc (loc, else_);
4007 expr
4008 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
4010 tmp = create_tmp_var (type, "iftmp");
4011 result = build_simple_mem_ref_loc (loc, tmp);
4014 /* Build the new then clause, `tmp = then_;'. But don't build the
4015 assignment if the value is void; in C++ it can be if it's a throw. */
4016 if (!VOID_TYPE_P (TREE_TYPE (then_)))
4017 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
4019 /* Similarly, build the new else clause, `tmp = else_;'. */
4020 if (!VOID_TYPE_P (TREE_TYPE (else_)))
4021 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
4023 TREE_TYPE (expr) = void_type_node;
4024 recalculate_side_effects (expr);
4026 /* Move the COND_EXPR to the prequeue. */
4027 gimplify_stmt (&expr, pre_p);
4029 *expr_p = result;
4030 return GS_ALL_DONE;
4033 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
4034 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
4035 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
4036 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
4038 /* Make sure the condition has BOOLEAN_TYPE. */
4039 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
4041 /* Break apart && and || conditions. */
4042 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
4043 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
4045 expr = shortcut_cond_expr (expr);
4047 if (expr != *expr_p)
4049 *expr_p = expr;
4051 /* We can't rely on gimplify_expr to re-gimplify the expanded
4052 form properly, as cleanups might cause the target labels to be
4053 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
4054 set up a conditional context. */
4055 gimple_push_condition ();
4056 gimplify_stmt (expr_p, &seq);
4057 gimple_pop_condition (pre_p);
4058 gimple_seq_add_seq (pre_p, seq);
4060 return GS_ALL_DONE;
4064 /* Now do the normal gimplification. */
4066 /* Gimplify condition. */
4067 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
4068 fb_rvalue);
4069 if (ret == GS_ERROR)
4070 return GS_ERROR;
4071 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
4073 gimple_push_condition ();
4075 have_then_clause_p = have_else_clause_p = false;
4076 label_true = find_goto_label (TREE_OPERAND (expr, 1));
4077 if (label_true
4078 && DECL_CONTEXT (GOTO_DESTINATION (label_true)) == current_function_decl
4079 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4080 have different locations, otherwise we end up with incorrect
4081 location information on the branches. */
4082 && (optimize
4083 || !EXPR_HAS_LOCATION (expr)
4084 || !rexpr_has_location (label_true)
4085 || EXPR_LOCATION (expr) == rexpr_location (label_true)))
4087 have_then_clause_p = true;
4088 label_true = GOTO_DESTINATION (label_true);
4090 else
4091 label_true = create_artificial_label (UNKNOWN_LOCATION);
4092 label_false = find_goto_label (TREE_OPERAND (expr, 2));
4093 if (label_false
4094 && DECL_CONTEXT (GOTO_DESTINATION (label_false)) == current_function_decl
4095 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
4096 have different locations, otherwise we end up with incorrect
4097 location information on the branches. */
4098 && (optimize
4099 || !EXPR_HAS_LOCATION (expr)
4100 || !rexpr_has_location (label_false)
4101 || EXPR_LOCATION (expr) == rexpr_location (label_false)))
4103 have_else_clause_p = true;
4104 label_false = GOTO_DESTINATION (label_false);
4106 else
4107 label_false = create_artificial_label (UNKNOWN_LOCATION);
4109 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
4110 &arm2);
4111 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
4112 label_false);
4113 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
4114 gimplify_seq_add_stmt (&seq, cond_stmt);
4115 gimple_stmt_iterator gsi = gsi_last (seq);
4116 maybe_fold_stmt (&gsi);
4118 label_cont = NULL_TREE;
4119 if (!have_then_clause_p)
4121 /* For if (...) {} else { code; } put label_true after
4122 the else block. */
4123 if (TREE_OPERAND (expr, 1) == NULL_TREE
4124 && !have_else_clause_p
4125 && TREE_OPERAND (expr, 2) != NULL_TREE)
4126 label_cont = label_true;
4127 else
4129 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
4130 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
4131 /* For if (...) { code; } else {} or
4132 if (...) { code; } else goto label; or
4133 if (...) { code; return; } else { ... }
4134 label_cont isn't needed. */
4135 if (!have_else_clause_p
4136 && TREE_OPERAND (expr, 2) != NULL_TREE
4137 && gimple_seq_may_fallthru (seq))
4139 gimple *g;
4140 label_cont = create_artificial_label (UNKNOWN_LOCATION);
4142 g = gimple_build_goto (label_cont);
4144 /* GIMPLE_COND's are very low level; they have embedded
4145 gotos. This particular embedded goto should not be marked
4146 with the location of the original COND_EXPR, as it would
4147 correspond to the COND_EXPR's condition, not the ELSE or the
4148 THEN arms. To avoid marking it with the wrong location, flag
4149 it as "no location". */
4150 gimple_set_do_not_emit_location (g);
4152 gimplify_seq_add_stmt (&seq, g);
4156 if (!have_else_clause_p)
4158 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
4159 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
4161 if (label_cont)
4162 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
4164 gimple_pop_condition (pre_p);
4165 gimple_seq_add_seq (pre_p, seq);
4167 if (ret == GS_ERROR)
4168 ; /* Do nothing. */
4169 else if (have_then_clause_p || have_else_clause_p)
4170 ret = GS_ALL_DONE;
4171 else
4173 /* Both arms are empty; replace the COND_EXPR with its predicate. */
4174 expr = TREE_OPERAND (expr, 0);
4175 gimplify_stmt (&expr, pre_p);
4178 *expr_p = NULL;
4179 return ret;
4182 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
4183 to be marked addressable.
4185 We cannot rely on such an expression being directly markable if a temporary
4186 has been created by the gimplification. In this case, we create another
4187 temporary and initialize it with a copy, which will become a store after we
4188 mark it addressable. This can happen if the front-end passed us something
4189 that it could not mark addressable yet, like a Fortran pass-by-reference
4190 parameter (int) floatvar. */
4192 static void
4193 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4195 while (handled_component_p (*expr_p))
4196 expr_p = &TREE_OPERAND (*expr_p, 0);
4197 if (is_gimple_reg (*expr_p))
4199 /* Do not allow an SSA name as the temporary. */
4200 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4201 DECL_GIMPLE_REG_P (var) = 0;
4202 *expr_p = var;
4206 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4207 a call to __builtin_memcpy. */
4209 static enum gimplify_status
4210 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4211 gimple_seq *seq_p)
4213 tree t, to, to_ptr, from, from_ptr;
4214 gcall *gs;
4215 location_t loc = EXPR_LOCATION (*expr_p);
4217 to = TREE_OPERAND (*expr_p, 0);
4218 from = TREE_OPERAND (*expr_p, 1);
4220 /* Mark the RHS addressable. Beware that it may not be possible to do so
4221 directly if a temporary has been created by the gimplification. */
4222 prepare_gimple_addressable (&from, seq_p);
4224 mark_addressable (from);
4225 from_ptr = build_fold_addr_expr_loc (loc, from);
4226 gimplify_arg (&from_ptr, seq_p, loc);
4228 mark_addressable (to);
4229 to_ptr = build_fold_addr_expr_loc (loc, to);
4230 gimplify_arg (&to_ptr, seq_p, loc);
4232 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4234 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4236 if (want_value)
4238 /* tmp = memcpy() */
4239 t = create_tmp_var (TREE_TYPE (to_ptr));
4240 gimple_call_set_lhs (gs, t);
4241 gimplify_seq_add_stmt (seq_p, gs);
4243 *expr_p = build_simple_mem_ref (t);
4244 return GS_ALL_DONE;
4247 gimplify_seq_add_stmt (seq_p, gs);
4248 *expr_p = NULL;
4249 return GS_ALL_DONE;
4252 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4253 a call to __builtin_memset. In this case we know that the RHS is
4254 a CONSTRUCTOR with an empty element list. */
4256 static enum gimplify_status
4257 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4258 gimple_seq *seq_p)
4260 tree t, from, to, to_ptr;
4261 gcall *gs;
4262 location_t loc = EXPR_LOCATION (*expr_p);
4264 /* Assert our assumptions, to abort instead of producing wrong code
4265 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4266 not be immediately exposed. */
4267 from = TREE_OPERAND (*expr_p, 1);
4268 if (TREE_CODE (from) == WITH_SIZE_EXPR)
4269 from = TREE_OPERAND (from, 0);
4271 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4272 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4274 /* Now proceed. */
4275 to = TREE_OPERAND (*expr_p, 0);
4277 to_ptr = build_fold_addr_expr_loc (loc, to);
4278 gimplify_arg (&to_ptr, seq_p, loc);
4279 t = builtin_decl_implicit (BUILT_IN_MEMSET);
4281 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4283 if (want_value)
4285 /* tmp = memset() */
4286 t = create_tmp_var (TREE_TYPE (to_ptr));
4287 gimple_call_set_lhs (gs, t);
4288 gimplify_seq_add_stmt (seq_p, gs);
4290 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4291 return GS_ALL_DONE;
4294 gimplify_seq_add_stmt (seq_p, gs);
4295 *expr_p = NULL;
4296 return GS_ALL_DONE;
4299 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4300 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4301 assignment. Return non-null if we detect a potential overlap. */
4303 struct gimplify_init_ctor_preeval_data
4305 /* The base decl of the lhs object. May be NULL, in which case we
4306 have to assume the lhs is indirect. */
4307 tree lhs_base_decl;
4309 /* The alias set of the lhs object. */
4310 alias_set_type lhs_alias_set;
4313 static tree
4314 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4316 struct gimplify_init_ctor_preeval_data *data
4317 = (struct gimplify_init_ctor_preeval_data *) xdata;
4318 tree t = *tp;
4320 /* If we find the base object, obviously we have overlap. */
4321 if (data->lhs_base_decl == t)
4322 return t;
4324 /* If the constructor component is indirect, determine if we have a
4325 potential overlap with the lhs. The only bits of information we
4326 have to go on at this point are addressability and alias sets. */
4327 if ((INDIRECT_REF_P (t)
4328 || TREE_CODE (t) == MEM_REF)
4329 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4330 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4331 return t;
4333 /* If the constructor component is a call, determine if it can hide a
4334 potential overlap with the lhs through an INDIRECT_REF like above.
4335 ??? Ugh - this is completely broken. In fact this whole analysis
4336 doesn't look conservative. */
4337 if (TREE_CODE (t) == CALL_EXPR)
4339 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4341 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4342 if (POINTER_TYPE_P (TREE_VALUE (type))
4343 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4344 && alias_sets_conflict_p (data->lhs_alias_set,
4345 get_alias_set
4346 (TREE_TYPE (TREE_VALUE (type)))))
4347 return t;
4350 if (IS_TYPE_OR_DECL_P (t))
4351 *walk_subtrees = 0;
4352 return NULL;
4355 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4356 force values that overlap with the lhs (as described by *DATA)
4357 into temporaries. */
4359 static void
4360 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4361 struct gimplify_init_ctor_preeval_data *data)
4363 enum gimplify_status one;
4365 /* If the value is constant, then there's nothing to pre-evaluate. */
4366 if (TREE_CONSTANT (*expr_p))
4368 /* Ensure it does not have side effects, it might contain a reference to
4369 the object we're initializing. */
4370 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4371 return;
4374 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4375 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4376 return;
4378 /* Recurse for nested constructors. */
4379 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4381 unsigned HOST_WIDE_INT ix;
4382 constructor_elt *ce;
4383 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4385 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4386 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4388 return;
4391 /* If this is a variable sized type, we must remember the size. */
4392 maybe_with_size_expr (expr_p);
4394 /* Gimplify the constructor element to something appropriate for the rhs
4395 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4396 the gimplifier will consider this a store to memory. Doing this
4397 gimplification now means that we won't have to deal with complicated
4398 language-specific trees, nor trees like SAVE_EXPR that can induce
4399 exponential search behavior. */
4400 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4401 if (one == GS_ERROR)
4403 *expr_p = NULL;
4404 return;
4407 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4408 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4409 always be true for all scalars, since is_gimple_mem_rhs insists on a
4410 temporary variable for them. */
4411 if (DECL_P (*expr_p))
4412 return;
4414 /* If this is of variable size, we have no choice but to assume it doesn't
4415 overlap since we can't make a temporary for it. */
4416 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4417 return;
4419 /* Otherwise, we must search for overlap ... */
4420 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4421 return;
4423 /* ... and if found, force the value into a temporary. */
4424 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4427 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4428 a RANGE_EXPR in a CONSTRUCTOR for an array.
4430 var = lower;
4431 loop_entry:
4432 object[var] = value;
4433 if (var == upper)
4434 goto loop_exit;
4435 var = var + 1;
4436 goto loop_entry;
4437 loop_exit:
4439 We increment var _after_ the loop exit check because we might otherwise
4440 fail if upper == TYPE_MAX_VALUE (type for upper).
4442 Note that we never have to deal with SAVE_EXPRs here, because this has
4443 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4445 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4446 gimple_seq *, bool);
4448 static void
4449 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4450 tree value, tree array_elt_type,
4451 gimple_seq *pre_p, bool cleared)
4453 tree loop_entry_label, loop_exit_label, fall_thru_label;
4454 tree var, var_type, cref, tmp;
4456 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4457 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4458 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4460 /* Create and initialize the index variable. */
4461 var_type = TREE_TYPE (upper);
4462 var = create_tmp_var (var_type);
4463 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4465 /* Add the loop entry label. */
4466 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4468 /* Build the reference. */
4469 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4470 var, NULL_TREE, NULL_TREE);
4472 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4473 the store. Otherwise just assign value to the reference. */
4475 if (TREE_CODE (value) == CONSTRUCTOR)
4476 /* NB we might have to call ourself recursively through
4477 gimplify_init_ctor_eval if the value is a constructor. */
4478 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4479 pre_p, cleared);
4480 else
4481 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4483 /* We exit the loop when the index var is equal to the upper bound. */
4484 gimplify_seq_add_stmt (pre_p,
4485 gimple_build_cond (EQ_EXPR, var, upper,
4486 loop_exit_label, fall_thru_label));
4488 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4490 /* Otherwise, increment the index var... */
4491 tmp = build2 (PLUS_EXPR, var_type, var,
4492 fold_convert (var_type, integer_one_node));
4493 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4495 /* ...and jump back to the loop entry. */
4496 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4498 /* Add the loop exit label. */
4499 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4502 /* Return true if FDECL is accessing a field that is zero sized. */
4504 static bool
4505 zero_sized_field_decl (const_tree fdecl)
4507 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4508 && integer_zerop (DECL_SIZE (fdecl)))
4509 return true;
4510 return false;
4513 /* Return true if TYPE is zero sized. */
4515 static bool
4516 zero_sized_type (const_tree type)
4518 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4519 && integer_zerop (TYPE_SIZE (type)))
4520 return true;
4521 return false;
4524 /* A subroutine of gimplify_init_constructor. Generate individual
4525 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4526 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4527 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4528 zeroed first. */
4530 static void
4531 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4532 gimple_seq *pre_p, bool cleared)
4534 tree array_elt_type = NULL;
4535 unsigned HOST_WIDE_INT ix;
4536 tree purpose, value;
4538 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4539 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4541 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4543 tree cref;
4545 /* NULL values are created above for gimplification errors. */
4546 if (value == NULL)
4547 continue;
4549 if (cleared && initializer_zerop (value))
4550 continue;
4552 /* ??? Here's to hoping the front end fills in all of the indices,
4553 so we don't have to figure out what's missing ourselves. */
4554 gcc_assert (purpose);
4556 /* Skip zero-sized fields, unless value has side-effects. This can
4557 happen with calls to functions returning a zero-sized type, which
4558 we shouldn't discard. As a number of downstream passes don't
4559 expect sets of zero-sized fields, we rely on the gimplification of
4560 the MODIFY_EXPR we make below to drop the assignment statement. */
4561 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4562 continue;
4564 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4565 whole range. */
4566 if (TREE_CODE (purpose) == RANGE_EXPR)
4568 tree lower = TREE_OPERAND (purpose, 0);
4569 tree upper = TREE_OPERAND (purpose, 1);
4571 /* If the lower bound is equal to upper, just treat it as if
4572 upper was the index. */
4573 if (simple_cst_equal (lower, upper))
4574 purpose = upper;
4575 else
4577 gimplify_init_ctor_eval_range (object, lower, upper, value,
4578 array_elt_type, pre_p, cleared);
4579 continue;
4583 if (array_elt_type)
4585 /* Do not use bitsizetype for ARRAY_REF indices. */
4586 if (TYPE_DOMAIN (TREE_TYPE (object)))
4587 purpose
4588 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4589 purpose);
4590 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4591 purpose, NULL_TREE, NULL_TREE);
4593 else
4595 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4596 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4597 unshare_expr (object), purpose, NULL_TREE);
4600 if (TREE_CODE (value) == CONSTRUCTOR
4601 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4602 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4603 pre_p, cleared);
4604 else
4606 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4607 gimplify_and_add (init, pre_p);
4608 ggc_free (init);
4613 /* Return the appropriate RHS predicate for this LHS. */
4615 gimple_predicate
4616 rhs_predicate_for (tree lhs)
4618 if (is_gimple_reg (lhs))
4619 return is_gimple_reg_rhs_or_call;
4620 else
4621 return is_gimple_mem_rhs_or_call;
4624 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4625 before the LHS has been gimplified. */
4627 static gimple_predicate
4628 initial_rhs_predicate_for (tree lhs)
4630 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4631 return is_gimple_reg_rhs_or_call;
4632 else
4633 return is_gimple_mem_rhs_or_call;
4636 /* Gimplify a C99 compound literal expression. This just means adding
4637 the DECL_EXPR before the current statement and using its anonymous
4638 decl instead. */
4640 static enum gimplify_status
4641 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4642 bool (*gimple_test_f) (tree),
4643 fallback_t fallback)
4645 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4646 tree decl = DECL_EXPR_DECL (decl_s);
4647 tree init = DECL_INITIAL (decl);
4648 /* Mark the decl as addressable if the compound literal
4649 expression is addressable now, otherwise it is marked too late
4650 after we gimplify the initialization expression. */
4651 if (TREE_ADDRESSABLE (*expr_p))
4652 TREE_ADDRESSABLE (decl) = 1;
4653 /* Otherwise, if we don't need an lvalue and have a literal directly
4654 substitute it. Check if it matches the gimple predicate, as
4655 otherwise we'd generate a new temporary, and we can as well just
4656 use the decl we already have. */
4657 else if (!TREE_ADDRESSABLE (decl)
4658 && init
4659 && (fallback & fb_lvalue) == 0
4660 && gimple_test_f (init))
4662 *expr_p = init;
4663 return GS_OK;
4666 /* Preliminarily mark non-addressed complex variables as eligible
4667 for promotion to gimple registers. We'll transform their uses
4668 as we find them. */
4669 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4670 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4671 && !TREE_THIS_VOLATILE (decl)
4672 && !needs_to_live_in_memory (decl))
4673 DECL_GIMPLE_REG_P (decl) = 1;
4675 /* If the decl is not addressable, then it is being used in some
4676 expression or on the right hand side of a statement, and it can
4677 be put into a readonly data section. */
4678 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4679 TREE_READONLY (decl) = 1;
4681 /* This decl isn't mentioned in the enclosing block, so add it to the
4682 list of temps. FIXME it seems a bit of a kludge to say that
4683 anonymous artificial vars aren't pushed, but everything else is. */
4684 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4685 gimple_add_tmp_var (decl);
4687 gimplify_and_add (decl_s, pre_p);
4688 *expr_p = decl;
4689 return GS_OK;
4692 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4693 return a new CONSTRUCTOR if something changed. */
4695 static tree
4696 optimize_compound_literals_in_ctor (tree orig_ctor)
4698 tree ctor = orig_ctor;
4699 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4700 unsigned int idx, num = vec_safe_length (elts);
4702 for (idx = 0; idx < num; idx++)
4704 tree value = (*elts)[idx].value;
4705 tree newval = value;
4706 if (TREE_CODE (value) == CONSTRUCTOR)
4707 newval = optimize_compound_literals_in_ctor (value);
4708 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4710 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4711 tree decl = DECL_EXPR_DECL (decl_s);
4712 tree init = DECL_INITIAL (decl);
4714 if (!TREE_ADDRESSABLE (value)
4715 && !TREE_ADDRESSABLE (decl)
4716 && init
4717 && TREE_CODE (init) == CONSTRUCTOR)
4718 newval = optimize_compound_literals_in_ctor (init);
4720 if (newval == value)
4721 continue;
4723 if (ctor == orig_ctor)
4725 ctor = copy_node (orig_ctor);
4726 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4727 elts = CONSTRUCTOR_ELTS (ctor);
4729 (*elts)[idx].value = newval;
4731 return ctor;
4734 /* A subroutine of gimplify_modify_expr. Break out elements of a
4735 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4737 Note that we still need to clear any elements that don't have explicit
4738 initializers, so if not all elements are initialized we keep the
4739 original MODIFY_EXPR, we just remove all of the constructor elements.
4741 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4742 GS_ERROR if we would have to create a temporary when gimplifying
4743 this constructor. Otherwise, return GS_OK.
4745 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4747 static enum gimplify_status
4748 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4749 bool want_value, bool notify_temp_creation)
4751 tree object, ctor, type;
4752 enum gimplify_status ret;
4753 vec<constructor_elt, va_gc> *elts;
4755 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4757 if (!notify_temp_creation)
4759 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4760 is_gimple_lvalue, fb_lvalue);
4761 if (ret == GS_ERROR)
4762 return ret;
4765 object = TREE_OPERAND (*expr_p, 0);
4766 ctor = TREE_OPERAND (*expr_p, 1)
4767 = optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4768 type = TREE_TYPE (ctor);
4769 elts = CONSTRUCTOR_ELTS (ctor);
4770 ret = GS_ALL_DONE;
4772 switch (TREE_CODE (type))
4774 case RECORD_TYPE:
4775 case UNION_TYPE:
4776 case QUAL_UNION_TYPE:
4777 case ARRAY_TYPE:
4779 struct gimplify_init_ctor_preeval_data preeval_data;
4780 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4781 HOST_WIDE_INT num_unique_nonzero_elements;
4782 bool cleared, complete_p, valid_const_initializer;
4783 /* Use readonly data for initializers of this or smaller size
4784 regardless of the num_nonzero_elements / num_unique_nonzero_elements
4785 ratio. */
4786 const HOST_WIDE_INT min_unique_size = 64;
4787 /* If num_nonzero_elements / num_unique_nonzero_elements ratio
4788 is smaller than this, use readonly data. */
4789 const int unique_nonzero_ratio = 8;
4791 /* Aggregate types must lower constructors to initialization of
4792 individual elements. The exception is that a CONSTRUCTOR node
4793 with no elements indicates zero-initialization of the whole. */
4794 if (vec_safe_is_empty (elts))
4796 if (notify_temp_creation)
4797 return GS_OK;
4798 break;
4801 /* Fetch information about the constructor to direct later processing.
4802 We might want to make static versions of it in various cases, and
4803 can only do so if it known to be a valid constant initializer. */
4804 valid_const_initializer
4805 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4806 &num_unique_nonzero_elements,
4807 &num_ctor_elements, &complete_p);
4809 /* If a const aggregate variable is being initialized, then it
4810 should never be a lose to promote the variable to be static. */
4811 if (valid_const_initializer
4812 && num_nonzero_elements > 1
4813 && TREE_READONLY (object)
4814 && VAR_P (object)
4815 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object))
4816 /* For ctors that have many repeated nonzero elements
4817 represented through RANGE_EXPRs, prefer initializing
4818 those through runtime loops over copies of large amounts
4819 of data from readonly data section. */
4820 && (num_unique_nonzero_elements
4821 > num_nonzero_elements / unique_nonzero_ratio
4822 || ((unsigned HOST_WIDE_INT) int_size_in_bytes (type)
4823 <= (unsigned HOST_WIDE_INT) min_unique_size)))
4825 if (notify_temp_creation)
4826 return GS_ERROR;
4827 DECL_INITIAL (object) = ctor;
4828 TREE_STATIC (object) = 1;
4829 if (!DECL_NAME (object))
4830 DECL_NAME (object) = create_tmp_var_name ("C");
4831 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4833 /* ??? C++ doesn't automatically append a .<number> to the
4834 assembler name, and even when it does, it looks at FE private
4835 data structures to figure out what that number should be,
4836 which are not set for this variable. I suppose this is
4837 important for local statics for inline functions, which aren't
4838 "local" in the object file sense. So in order to get a unique
4839 TU-local symbol, we must invoke the lhd version now. */
4840 lhd_set_decl_assembler_name (object);
4842 *expr_p = NULL_TREE;
4843 break;
4846 /* If there are "lots" of initialized elements, even discounting
4847 those that are not address constants (and thus *must* be
4848 computed at runtime), then partition the constructor into
4849 constant and non-constant parts. Block copy the constant
4850 parts in, then generate code for the non-constant parts. */
4851 /* TODO. There's code in cp/typeck.c to do this. */
4853 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4854 /* store_constructor will ignore the clearing of variable-sized
4855 objects. Initializers for such objects must explicitly set
4856 every field that needs to be set. */
4857 cleared = false;
4858 else if (!complete_p)
4859 /* If the constructor isn't complete, clear the whole object
4860 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4862 ??? This ought not to be needed. For any element not present
4863 in the initializer, we should simply set them to zero. Except
4864 we'd need to *find* the elements that are not present, and that
4865 requires trickery to avoid quadratic compile-time behavior in
4866 large cases or excessive memory use in small cases. */
4867 cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
4868 else if (num_ctor_elements - num_nonzero_elements
4869 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4870 && num_nonzero_elements < num_ctor_elements / 4)
4871 /* If there are "lots" of zeros, it's more efficient to clear
4872 the memory and then set the nonzero elements. */
4873 cleared = true;
4874 else
4875 cleared = false;
4877 /* If there are "lots" of initialized elements, and all of them
4878 are valid address constants, then the entire initializer can
4879 be dropped to memory, and then memcpy'd out. Don't do this
4880 for sparse arrays, though, as it's more efficient to follow
4881 the standard CONSTRUCTOR behavior of memset followed by
4882 individual element initialization. Also don't do this for small
4883 all-zero initializers (which aren't big enough to merit
4884 clearing), and don't try to make bitwise copies of
4885 TREE_ADDRESSABLE types. */
4887 if (valid_const_initializer
4888 && !(cleared || num_nonzero_elements == 0)
4889 && !TREE_ADDRESSABLE (type))
4891 HOST_WIDE_INT size = int_size_in_bytes (type);
4892 unsigned int align;
4894 /* ??? We can still get unbounded array types, at least
4895 from the C++ front end. This seems wrong, but attempt
4896 to work around it for now. */
4897 if (size < 0)
4899 size = int_size_in_bytes (TREE_TYPE (object));
4900 if (size >= 0)
4901 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4904 /* Find the maximum alignment we can assume for the object. */
4905 /* ??? Make use of DECL_OFFSET_ALIGN. */
4906 if (DECL_P (object))
4907 align = DECL_ALIGN (object);
4908 else
4909 align = TYPE_ALIGN (type);
4911 /* Do a block move either if the size is so small as to make
4912 each individual move a sub-unit move on average, or if it
4913 is so large as to make individual moves inefficient. */
4914 if (size > 0
4915 && num_nonzero_elements > 1
4916 /* For ctors that have many repeated nonzero elements
4917 represented through RANGE_EXPRs, prefer initializing
4918 those through runtime loops over copies of large amounts
4919 of data from readonly data section. */
4920 && (num_unique_nonzero_elements
4921 > num_nonzero_elements / unique_nonzero_ratio
4922 || size <= min_unique_size)
4923 && (size < num_nonzero_elements
4924 || !can_move_by_pieces (size, align)))
4926 if (notify_temp_creation)
4927 return GS_ERROR;
4929 walk_tree (&ctor, force_labels_r, NULL, NULL);
4930 ctor = tree_output_constant_def (ctor);
4931 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4932 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4933 TREE_OPERAND (*expr_p, 1) = ctor;
4935 /* This is no longer an assignment of a CONSTRUCTOR, but
4936 we still may have processing to do on the LHS. So
4937 pretend we didn't do anything here to let that happen. */
4938 return GS_UNHANDLED;
4942 /* If the target is volatile, we have non-zero elements and more than
4943 one field to assign, initialize the target from a temporary. */
4944 if (TREE_THIS_VOLATILE (object)
4945 && !TREE_ADDRESSABLE (type)
4946 && num_nonzero_elements > 0
4947 && vec_safe_length (elts) > 1)
4949 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4950 TREE_OPERAND (*expr_p, 0) = temp;
4951 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4952 *expr_p,
4953 build2 (MODIFY_EXPR, void_type_node,
4954 object, temp));
4955 return GS_OK;
4958 if (notify_temp_creation)
4959 return GS_OK;
4961 /* If there are nonzero elements and if needed, pre-evaluate to capture
4962 elements overlapping with the lhs into temporaries. We must do this
4963 before clearing to fetch the values before they are zeroed-out. */
4964 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4966 preeval_data.lhs_base_decl = get_base_address (object);
4967 if (!DECL_P (preeval_data.lhs_base_decl))
4968 preeval_data.lhs_base_decl = NULL;
4969 preeval_data.lhs_alias_set = get_alias_set (object);
4971 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4972 pre_p, post_p, &preeval_data);
4975 bool ctor_has_side_effects_p
4976 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4978 if (cleared)
4980 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4981 Note that we still have to gimplify, in order to handle the
4982 case of variable sized types. Avoid shared tree structures. */
4983 CONSTRUCTOR_ELTS (ctor) = NULL;
4984 TREE_SIDE_EFFECTS (ctor) = 0;
4985 object = unshare_expr (object);
4986 gimplify_stmt (expr_p, pre_p);
4989 /* If we have not block cleared the object, or if there are nonzero
4990 elements in the constructor, or if the constructor has side effects,
4991 add assignments to the individual scalar fields of the object. */
4992 if (!cleared
4993 || num_nonzero_elements > 0
4994 || ctor_has_side_effects_p)
4995 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4997 *expr_p = NULL_TREE;
4999 break;
5001 case COMPLEX_TYPE:
5003 tree r, i;
5005 if (notify_temp_creation)
5006 return GS_OK;
5008 /* Extract the real and imaginary parts out of the ctor. */
5009 gcc_assert (elts->length () == 2);
5010 r = (*elts)[0].value;
5011 i = (*elts)[1].value;
5012 if (r == NULL || i == NULL)
5014 tree zero = build_zero_cst (TREE_TYPE (type));
5015 if (r == NULL)
5016 r = zero;
5017 if (i == NULL)
5018 i = zero;
5021 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
5022 represent creation of a complex value. */
5023 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
5025 ctor = build_complex (type, r, i);
5026 TREE_OPERAND (*expr_p, 1) = ctor;
5028 else
5030 ctor = build2 (COMPLEX_EXPR, type, r, i);
5031 TREE_OPERAND (*expr_p, 1) = ctor;
5032 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
5033 pre_p,
5034 post_p,
5035 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
5036 fb_rvalue);
5039 break;
5041 case VECTOR_TYPE:
5043 unsigned HOST_WIDE_INT ix;
5044 constructor_elt *ce;
5046 if (notify_temp_creation)
5047 return GS_OK;
5049 /* Go ahead and simplify constant constructors to VECTOR_CST. */
5050 if (TREE_CONSTANT (ctor))
5052 bool constant_p = true;
5053 tree value;
5055 /* Even when ctor is constant, it might contain non-*_CST
5056 elements, such as addresses or trapping values like
5057 1.0/0.0 - 1.0/0.0. Such expressions don't belong
5058 in VECTOR_CST nodes. */
5059 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
5060 if (!CONSTANT_CLASS_P (value))
5062 constant_p = false;
5063 break;
5066 if (constant_p)
5068 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
5069 break;
5072 TREE_CONSTANT (ctor) = 0;
5075 /* Vector types use CONSTRUCTOR all the way through gimple
5076 compilation as a general initializer. */
5077 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5079 enum gimplify_status tret;
5080 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5081 fb_rvalue);
5082 if (tret == GS_ERROR)
5083 ret = GS_ERROR;
5084 else if (TREE_STATIC (ctor)
5085 && !initializer_constant_valid_p (ce->value,
5086 TREE_TYPE (ce->value)))
5087 TREE_STATIC (ctor) = 0;
5089 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5090 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5092 break;
5094 default:
5095 /* So how did we get a CONSTRUCTOR for a scalar type? */
5096 gcc_unreachable ();
5099 if (ret == GS_ERROR)
5100 return GS_ERROR;
5101 /* If we have gimplified both sides of the initializer but have
5102 not emitted an assignment, do so now. */
5103 if (*expr_p)
5105 tree lhs = TREE_OPERAND (*expr_p, 0);
5106 tree rhs = TREE_OPERAND (*expr_p, 1);
5107 if (want_value && object == lhs)
5108 lhs = unshare_expr (lhs);
5109 gassign *init = gimple_build_assign (lhs, rhs);
5110 gimplify_seq_add_stmt (pre_p, init);
5112 if (want_value)
5114 *expr_p = object;
5115 return GS_OK;
5117 else
5119 *expr_p = NULL;
5120 return GS_ALL_DONE;
5124 /* Given a pointer value OP0, return a simplified version of an
5125 indirection through OP0, or NULL_TREE if no simplification is
5126 possible. This may only be applied to a rhs of an expression.
5127 Note that the resulting type may be different from the type pointed
5128 to in the sense that it is still compatible from the langhooks
5129 point of view. */
5131 static tree
5132 gimple_fold_indirect_ref_rhs (tree t)
5134 return gimple_fold_indirect_ref (t);
5137 /* Subroutine of gimplify_modify_expr to do simplifications of
5138 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5139 something changes. */
5141 static enum gimplify_status
5142 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5143 gimple_seq *pre_p, gimple_seq *post_p,
5144 bool want_value)
5146 enum gimplify_status ret = GS_UNHANDLED;
5147 bool changed;
5151 changed = false;
5152 switch (TREE_CODE (*from_p))
5154 case VAR_DECL:
5155 /* If we're assigning from a read-only variable initialized with
5156 a constructor, do the direct assignment from the constructor,
5157 but only if neither source nor target are volatile since this
5158 latter assignment might end up being done on a per-field basis. */
5159 if (DECL_INITIAL (*from_p)
5160 && TREE_READONLY (*from_p)
5161 && !TREE_THIS_VOLATILE (*from_p)
5162 && !TREE_THIS_VOLATILE (*to_p)
5163 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5165 tree old_from = *from_p;
5166 enum gimplify_status subret;
5168 /* Move the constructor into the RHS. */
5169 *from_p = unshare_expr (DECL_INITIAL (*from_p));
5171 /* Let's see if gimplify_init_constructor will need to put
5172 it in memory. */
5173 subret = gimplify_init_constructor (expr_p, NULL, NULL,
5174 false, true);
5175 if (subret == GS_ERROR)
5177 /* If so, revert the change. */
5178 *from_p = old_from;
5180 else
5182 ret = GS_OK;
5183 changed = true;
5186 break;
5187 case INDIRECT_REF:
5189 /* If we have code like
5191 *(const A*)(A*)&x
5193 where the type of "x" is a (possibly cv-qualified variant
5194 of "A"), treat the entire expression as identical to "x".
5195 This kind of code arises in C++ when an object is bound
5196 to a const reference, and if "x" is a TARGET_EXPR we want
5197 to take advantage of the optimization below. */
5198 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5199 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5200 if (t)
5202 if (TREE_THIS_VOLATILE (t) != volatile_p)
5204 if (DECL_P (t))
5205 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5206 build_fold_addr_expr (t));
5207 if (REFERENCE_CLASS_P (t))
5208 TREE_THIS_VOLATILE (t) = volatile_p;
5210 *from_p = t;
5211 ret = GS_OK;
5212 changed = true;
5214 break;
5217 case TARGET_EXPR:
5219 /* If we are initializing something from a TARGET_EXPR, strip the
5220 TARGET_EXPR and initialize it directly, if possible. This can't
5221 be done if the initializer is void, since that implies that the
5222 temporary is set in some non-trivial way.
5224 ??? What about code that pulls out the temp and uses it
5225 elsewhere? I think that such code never uses the TARGET_EXPR as
5226 an initializer. If I'm wrong, we'll die because the temp won't
5227 have any RTL. In that case, I guess we'll need to replace
5228 references somehow. */
5229 tree init = TARGET_EXPR_INITIAL (*from_p);
5231 if (init
5232 && (TREE_CODE (*expr_p) != MODIFY_EXPR
5233 || !TARGET_EXPR_NO_ELIDE (*from_p))
5234 && !VOID_TYPE_P (TREE_TYPE (init)))
5236 *from_p = init;
5237 ret = GS_OK;
5238 changed = true;
5241 break;
5243 case COMPOUND_EXPR:
5244 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5245 caught. */
5246 gimplify_compound_expr (from_p, pre_p, true);
5247 ret = GS_OK;
5248 changed = true;
5249 break;
5251 case CONSTRUCTOR:
5252 /* If we already made some changes, let the front end have a
5253 crack at this before we break it down. */
5254 if (ret != GS_UNHANDLED)
5255 break;
5256 /* If we're initializing from a CONSTRUCTOR, break this into
5257 individual MODIFY_EXPRs. */
5258 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5259 false);
5261 case COND_EXPR:
5262 /* If we're assigning to a non-register type, push the assignment
5263 down into the branches. This is mandatory for ADDRESSABLE types,
5264 since we cannot generate temporaries for such, but it saves a
5265 copy in other cases as well. */
5266 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5268 /* This code should mirror the code in gimplify_cond_expr. */
5269 enum tree_code code = TREE_CODE (*expr_p);
5270 tree cond = *from_p;
5271 tree result = *to_p;
5273 ret = gimplify_expr (&result, pre_p, post_p,
5274 is_gimple_lvalue, fb_lvalue);
5275 if (ret != GS_ERROR)
5276 ret = GS_OK;
5278 /* If we are going to write RESULT more than once, clear
5279 TREE_READONLY flag, otherwise we might incorrectly promote
5280 the variable to static const and initialize it at compile
5281 time in one of the branches. */
5282 if (VAR_P (result)
5283 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5284 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5285 TREE_READONLY (result) = 0;
5286 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5287 TREE_OPERAND (cond, 1)
5288 = build2 (code, void_type_node, result,
5289 TREE_OPERAND (cond, 1));
5290 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5291 TREE_OPERAND (cond, 2)
5292 = build2 (code, void_type_node, unshare_expr (result),
5293 TREE_OPERAND (cond, 2));
5295 TREE_TYPE (cond) = void_type_node;
5296 recalculate_side_effects (cond);
5298 if (want_value)
5300 gimplify_and_add (cond, pre_p);
5301 *expr_p = unshare_expr (result);
5303 else
5304 *expr_p = cond;
5305 return ret;
5307 break;
5309 case CALL_EXPR:
5310 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5311 return slot so that we don't generate a temporary. */
5312 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5313 && aggregate_value_p (*from_p, *from_p))
5315 bool use_target;
5317 if (!(rhs_predicate_for (*to_p))(*from_p))
5318 /* If we need a temporary, *to_p isn't accurate. */
5319 use_target = false;
5320 /* It's OK to use the return slot directly unless it's an NRV. */
5321 else if (TREE_CODE (*to_p) == RESULT_DECL
5322 && DECL_NAME (*to_p) == NULL_TREE
5323 && needs_to_live_in_memory (*to_p))
5324 use_target = true;
5325 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5326 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5327 /* Don't force regs into memory. */
5328 use_target = false;
5329 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5330 /* It's OK to use the target directly if it's being
5331 initialized. */
5332 use_target = true;
5333 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5334 != INTEGER_CST)
5335 /* Always use the target and thus RSO for variable-sized types.
5336 GIMPLE cannot deal with a variable-sized assignment
5337 embedded in a call statement. */
5338 use_target = true;
5339 else if (TREE_CODE (*to_p) != SSA_NAME
5340 && (!is_gimple_variable (*to_p)
5341 || needs_to_live_in_memory (*to_p)))
5342 /* Don't use the original target if it's already addressable;
5343 if its address escapes, and the called function uses the
5344 NRV optimization, a conforming program could see *to_p
5345 change before the called function returns; see c++/19317.
5346 When optimizing, the return_slot pass marks more functions
5347 as safe after we have escape info. */
5348 use_target = false;
5349 else
5350 use_target = true;
5352 if (use_target)
5354 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5355 mark_addressable (*to_p);
5358 break;
5360 case WITH_SIZE_EXPR:
5361 /* Likewise for calls that return an aggregate of non-constant size,
5362 since we would not be able to generate a temporary at all. */
5363 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5365 *from_p = TREE_OPERAND (*from_p, 0);
5366 /* We don't change ret in this case because the
5367 WITH_SIZE_EXPR might have been added in
5368 gimplify_modify_expr, so returning GS_OK would lead to an
5369 infinite loop. */
5370 changed = true;
5372 break;
5374 /* If we're initializing from a container, push the initialization
5375 inside it. */
5376 case CLEANUP_POINT_EXPR:
5377 case BIND_EXPR:
5378 case STATEMENT_LIST:
5380 tree wrap = *from_p;
5381 tree t;
5383 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5384 fb_lvalue);
5385 if (ret != GS_ERROR)
5386 ret = GS_OK;
5388 t = voidify_wrapper_expr (wrap, *expr_p);
5389 gcc_assert (t == *expr_p);
5391 if (want_value)
5393 gimplify_and_add (wrap, pre_p);
5394 *expr_p = unshare_expr (*to_p);
5396 else
5397 *expr_p = wrap;
5398 return GS_OK;
5401 case COMPOUND_LITERAL_EXPR:
5403 tree complit = TREE_OPERAND (*expr_p, 1);
5404 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5405 tree decl = DECL_EXPR_DECL (decl_s);
5406 tree init = DECL_INITIAL (decl);
5408 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5409 into struct T x = { 0, 1, 2 } if the address of the
5410 compound literal has never been taken. */
5411 if (!TREE_ADDRESSABLE (complit)
5412 && !TREE_ADDRESSABLE (decl)
5413 && init)
5415 *expr_p = copy_node (*expr_p);
5416 TREE_OPERAND (*expr_p, 1) = init;
5417 return GS_OK;
5421 default:
5422 break;
5425 while (changed);
5427 return ret;
5431 /* Return true if T looks like a valid GIMPLE statement. */
5433 static bool
5434 is_gimple_stmt (tree t)
5436 const enum tree_code code = TREE_CODE (t);
5438 switch (code)
5440 case NOP_EXPR:
5441 /* The only valid NOP_EXPR is the empty statement. */
5442 return IS_EMPTY_STMT (t);
5444 case BIND_EXPR:
5445 case COND_EXPR:
5446 /* These are only valid if they're void. */
5447 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5449 case SWITCH_EXPR:
5450 case GOTO_EXPR:
5451 case RETURN_EXPR:
5452 case LABEL_EXPR:
5453 case CASE_LABEL_EXPR:
5454 case TRY_CATCH_EXPR:
5455 case TRY_FINALLY_EXPR:
5456 case EH_FILTER_EXPR:
5457 case CATCH_EXPR:
5458 case ASM_EXPR:
5459 case STATEMENT_LIST:
5460 case OACC_PARALLEL:
5461 case OACC_KERNELS:
5462 case OACC_DATA:
5463 case OACC_HOST_DATA:
5464 case OACC_DECLARE:
5465 case OACC_UPDATE:
5466 case OACC_ENTER_DATA:
5467 case OACC_EXIT_DATA:
5468 case OACC_CACHE:
5469 case OMP_PARALLEL:
5470 case OMP_FOR:
5471 case OMP_SIMD:
5472 case OMP_DISTRIBUTE:
5473 case OACC_LOOP:
5474 case OMP_SECTIONS:
5475 case OMP_SECTION:
5476 case OMP_SINGLE:
5477 case OMP_MASTER:
5478 case OMP_TASKGROUP:
5479 case OMP_ORDERED:
5480 case OMP_CRITICAL:
5481 case OMP_TASK:
5482 case OMP_TARGET:
5483 case OMP_TARGET_DATA:
5484 case OMP_TARGET_UPDATE:
5485 case OMP_TARGET_ENTER_DATA:
5486 case OMP_TARGET_EXIT_DATA:
5487 case OMP_TASKLOOP:
5488 case OMP_TEAMS:
5489 /* These are always void. */
5490 return true;
5492 case CALL_EXPR:
5493 case MODIFY_EXPR:
5494 case PREDICT_EXPR:
5495 /* These are valid regardless of their type. */
5496 return true;
5498 default:
5499 return false;
5504 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5505 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5506 DECL_GIMPLE_REG_P set.
5508 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5509 other, unmodified part of the complex object just before the total store.
5510 As a consequence, if the object is still uninitialized, an undefined value
5511 will be loaded into a register, which may result in a spurious exception
5512 if the register is floating-point and the value happens to be a signaling
5513 NaN for example. Then the fully-fledged complex operations lowering pass
5514 followed by a DCE pass are necessary in order to fix things up. */
5516 static enum gimplify_status
5517 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5518 bool want_value)
5520 enum tree_code code, ocode;
5521 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5523 lhs = TREE_OPERAND (*expr_p, 0);
5524 rhs = TREE_OPERAND (*expr_p, 1);
5525 code = TREE_CODE (lhs);
5526 lhs = TREE_OPERAND (lhs, 0);
5528 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5529 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5530 TREE_NO_WARNING (other) = 1;
5531 other = get_formal_tmp_var (other, pre_p);
5533 realpart = code == REALPART_EXPR ? rhs : other;
5534 imagpart = code == REALPART_EXPR ? other : rhs;
5536 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5537 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5538 else
5539 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5541 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5542 *expr_p = (want_value) ? rhs : NULL_TREE;
5544 return GS_ALL_DONE;
5547 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5549 modify_expr
5550 : varname '=' rhs
5551 | '*' ID '=' rhs
5553 PRE_P points to the list where side effects that must happen before
5554 *EXPR_P should be stored.
5556 POST_P points to the list where side effects that must happen after
5557 *EXPR_P should be stored.
5559 WANT_VALUE is nonzero iff we want to use the value of this expression
5560 in another expression. */
5562 static enum gimplify_status
5563 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5564 bool want_value)
5566 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5567 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5568 enum gimplify_status ret = GS_UNHANDLED;
5569 gimple *assign;
5570 location_t loc = EXPR_LOCATION (*expr_p);
5571 gimple_stmt_iterator gsi;
5573 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5574 || TREE_CODE (*expr_p) == INIT_EXPR);
5576 /* Trying to simplify a clobber using normal logic doesn't work,
5577 so handle it here. */
5578 if (TREE_CLOBBER_P (*from_p))
5580 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5581 if (ret == GS_ERROR)
5582 return ret;
5583 gcc_assert (!want_value);
5584 if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
5586 tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
5587 pre_p, post_p);
5588 *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
5590 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5591 *expr_p = NULL;
5592 return GS_ALL_DONE;
5595 /* Insert pointer conversions required by the middle-end that are not
5596 required by the frontend. This fixes middle-end type checking for
5597 for example gcc.dg/redecl-6.c. */
5598 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5600 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5601 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5602 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5605 /* See if any simplifications can be done based on what the RHS is. */
5606 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5607 want_value);
5608 if (ret != GS_UNHANDLED)
5609 return ret;
5611 /* For zero sized types only gimplify the left hand side and right hand
5612 side as statements and throw away the assignment. Do this after
5613 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5614 types properly. */
5615 if (zero_sized_type (TREE_TYPE (*from_p))
5616 && !want_value
5617 /* Don't do this for calls that return addressable types, expand_call
5618 relies on those having a lhs. */
5619 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5620 && TREE_CODE (*from_p) == CALL_EXPR))
5622 gimplify_stmt (from_p, pre_p);
5623 gimplify_stmt (to_p, pre_p);
5624 *expr_p = NULL_TREE;
5625 return GS_ALL_DONE;
5628 /* If the value being copied is of variable width, compute the length
5629 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5630 before gimplifying any of the operands so that we can resolve any
5631 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5632 the size of the expression to be copied, not of the destination, so
5633 that is what we must do here. */
5634 maybe_with_size_expr (from_p);
5636 /* As a special case, we have to temporarily allow for assignments
5637 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5638 a toplevel statement, when gimplifying the GENERIC expression
5639 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5640 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5642 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5643 prevent gimplify_expr from trying to create a new temporary for
5644 foo's LHS, we tell it that it should only gimplify until it
5645 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5646 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5647 and all we need to do here is set 'a' to be its LHS. */
5649 /* Gimplify the RHS first for C++17 and bug 71104. */
5650 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5651 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5652 if (ret == GS_ERROR)
5653 return ret;
5655 /* Then gimplify the LHS. */
5656 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5657 twice we have to make sure to gimplify into non-SSA as otherwise
5658 the abnormal edge added later will make those defs not dominate
5659 their uses.
5660 ??? Technically this applies only to the registers used in the
5661 resulting non-register *TO_P. */
5662 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5663 if (saved_into_ssa
5664 && TREE_CODE (*from_p) == CALL_EXPR
5665 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5666 gimplify_ctxp->into_ssa = false;
5667 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5668 gimplify_ctxp->into_ssa = saved_into_ssa;
5669 if (ret == GS_ERROR)
5670 return ret;
5672 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5673 guess for the predicate was wrong. */
5674 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5675 if (final_pred != initial_pred)
5677 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5678 if (ret == GS_ERROR)
5679 return ret;
5682 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5683 size as argument to the call. */
5684 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5686 tree call = TREE_OPERAND (*from_p, 0);
5687 tree vlasize = TREE_OPERAND (*from_p, 1);
5689 if (TREE_CODE (call) == CALL_EXPR
5690 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5692 int nargs = call_expr_nargs (call);
5693 tree type = TREE_TYPE (call);
5694 tree ap = CALL_EXPR_ARG (call, 0);
5695 tree tag = CALL_EXPR_ARG (call, 1);
5696 tree aptag = CALL_EXPR_ARG (call, 2);
5697 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5698 IFN_VA_ARG, type,
5699 nargs + 1, ap, tag,
5700 aptag, vlasize);
5701 TREE_OPERAND (*from_p, 0) = newcall;
5705 /* Now see if the above changed *from_p to something we handle specially. */
5706 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5707 want_value);
5708 if (ret != GS_UNHANDLED)
5709 return ret;
5711 /* If we've got a variable sized assignment between two lvalues (i.e. does
5712 not involve a call), then we can make things a bit more straightforward
5713 by converting the assignment to memcpy or memset. */
5714 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5716 tree from = TREE_OPERAND (*from_p, 0);
5717 tree size = TREE_OPERAND (*from_p, 1);
5719 if (TREE_CODE (from) == CONSTRUCTOR)
5720 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5722 if (is_gimple_addressable (from))
5724 *from_p = from;
5725 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5726 pre_p);
5730 /* Transform partial stores to non-addressable complex variables into
5731 total stores. This allows us to use real instead of virtual operands
5732 for these variables, which improves optimization. */
5733 if ((TREE_CODE (*to_p) == REALPART_EXPR
5734 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5735 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5736 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5738 /* Try to alleviate the effects of the gimplification creating artificial
5739 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5740 make sure not to create DECL_DEBUG_EXPR links across functions. */
5741 if (!gimplify_ctxp->into_ssa
5742 && VAR_P (*from_p)
5743 && DECL_IGNORED_P (*from_p)
5744 && DECL_P (*to_p)
5745 && !DECL_IGNORED_P (*to_p)
5746 && decl_function_context (*to_p) == current_function_decl
5747 && decl_function_context (*from_p) == current_function_decl)
5749 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5750 DECL_NAME (*from_p)
5751 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5752 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5753 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5756 if (want_value && TREE_THIS_VOLATILE (*to_p))
5757 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5759 if (TREE_CODE (*from_p) == CALL_EXPR)
5761 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5762 instead of a GIMPLE_ASSIGN. */
5763 gcall *call_stmt;
5764 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5766 /* Gimplify internal functions created in the FEs. */
5767 int nargs = call_expr_nargs (*from_p), i;
5768 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5769 auto_vec<tree> vargs (nargs);
5771 for (i = 0; i < nargs; i++)
5773 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5774 EXPR_LOCATION (*from_p));
5775 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5777 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5778 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5779 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5781 else
5783 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5784 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5785 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5786 tree fndecl = get_callee_fndecl (*from_p);
5787 if (fndecl
5788 && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
5789 && call_expr_nargs (*from_p) == 3)
5790 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5791 CALL_EXPR_ARG (*from_p, 0),
5792 CALL_EXPR_ARG (*from_p, 1),
5793 CALL_EXPR_ARG (*from_p, 2));
5794 else
5796 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5799 notice_special_calls (call_stmt);
5800 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5801 gimple_call_set_lhs (call_stmt, *to_p);
5802 else if (TREE_CODE (*to_p) == SSA_NAME)
5803 /* The above is somewhat premature, avoid ICEing later for a
5804 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5805 ??? This doesn't make it a default-def. */
5806 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5808 assign = call_stmt;
5810 else
5812 assign = gimple_build_assign (*to_p, *from_p);
5813 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5814 if (COMPARISON_CLASS_P (*from_p))
5815 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5818 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5820 /* We should have got an SSA name from the start. */
5821 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5822 || ! gimple_in_ssa_p (cfun));
5825 gimplify_seq_add_stmt (pre_p, assign);
5826 gsi = gsi_last (*pre_p);
5827 maybe_fold_stmt (&gsi);
5829 if (want_value)
5831 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5832 return GS_OK;
5834 else
5835 *expr_p = NULL;
5837 return GS_ALL_DONE;
5840 /* Gimplify a comparison between two variable-sized objects. Do this
5841 with a call to BUILT_IN_MEMCMP. */
5843 static enum gimplify_status
5844 gimplify_variable_sized_compare (tree *expr_p)
5846 location_t loc = EXPR_LOCATION (*expr_p);
5847 tree op0 = TREE_OPERAND (*expr_p, 0);
5848 tree op1 = TREE_OPERAND (*expr_p, 1);
5849 tree t, arg, dest, src, expr;
5851 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5852 arg = unshare_expr (arg);
5853 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5854 src = build_fold_addr_expr_loc (loc, op1);
5855 dest = build_fold_addr_expr_loc (loc, op0);
5856 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5857 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5859 expr
5860 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5861 SET_EXPR_LOCATION (expr, loc);
5862 *expr_p = expr;
5864 return GS_OK;
5867 /* Gimplify a comparison between two aggregate objects of integral scalar
5868 mode as a comparison between the bitwise equivalent scalar values. */
5870 static enum gimplify_status
5871 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5873 location_t loc = EXPR_LOCATION (*expr_p);
5874 tree op0 = TREE_OPERAND (*expr_p, 0);
5875 tree op1 = TREE_OPERAND (*expr_p, 1);
5877 tree type = TREE_TYPE (op0);
5878 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5880 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5881 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5883 *expr_p
5884 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5886 return GS_OK;
5889 /* Gimplify an expression sequence. This function gimplifies each
5890 expression and rewrites the original expression with the last
5891 expression of the sequence in GIMPLE form.
5893 PRE_P points to the list where the side effects for all the
5894 expressions in the sequence will be emitted.
5896 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5898 static enum gimplify_status
5899 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5901 tree t = *expr_p;
5905 tree *sub_p = &TREE_OPERAND (t, 0);
5907 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5908 gimplify_compound_expr (sub_p, pre_p, false);
5909 else
5910 gimplify_stmt (sub_p, pre_p);
5912 t = TREE_OPERAND (t, 1);
5914 while (TREE_CODE (t) == COMPOUND_EXPR);
5916 *expr_p = t;
5917 if (want_value)
5918 return GS_OK;
5919 else
5921 gimplify_stmt (expr_p, pre_p);
5922 return GS_ALL_DONE;
5926 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5927 gimplify. After gimplification, EXPR_P will point to a new temporary
5928 that holds the original value of the SAVE_EXPR node.
5930 PRE_P points to the list where side effects that must happen before
5931 *EXPR_P should be stored. */
5933 static enum gimplify_status
5934 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5936 enum gimplify_status ret = GS_ALL_DONE;
5937 tree val;
5939 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5940 val = TREE_OPERAND (*expr_p, 0);
5942 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5943 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5945 /* The operand may be a void-valued expression. It is
5946 being executed only for its side-effects. */
5947 if (TREE_TYPE (val) == void_type_node)
5949 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5950 is_gimple_stmt, fb_none);
5951 val = NULL;
5953 else
5954 /* The temporary may not be an SSA name as later abnormal and EH
5955 control flow may invalidate use/def domination. When in SSA
5956 form then assume there are no such issues and SAVE_EXPRs only
5957 appear via GENERIC foldings. */
5958 val = get_initialized_tmp_var (val, pre_p, post_p,
5959 gimple_in_ssa_p (cfun));
5961 TREE_OPERAND (*expr_p, 0) = val;
5962 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5965 *expr_p = val;
5967 return ret;
5970 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5972 unary_expr
5973 : ...
5974 | '&' varname
5977 PRE_P points to the list where side effects that must happen before
5978 *EXPR_P should be stored.
5980 POST_P points to the list where side effects that must happen after
5981 *EXPR_P should be stored. */
5983 static enum gimplify_status
5984 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5986 tree expr = *expr_p;
5987 tree op0 = TREE_OPERAND (expr, 0);
5988 enum gimplify_status ret;
5989 location_t loc = EXPR_LOCATION (*expr_p);
5991 switch (TREE_CODE (op0))
5993 case INDIRECT_REF:
5994 do_indirect_ref:
5995 /* Check if we are dealing with an expression of the form '&*ptr'.
5996 While the front end folds away '&*ptr' into 'ptr', these
5997 expressions may be generated internally by the compiler (e.g.,
5998 builtins like __builtin_va_end). */
5999 /* Caution: the silent array decomposition semantics we allow for
6000 ADDR_EXPR means we can't always discard the pair. */
6001 /* Gimplification of the ADDR_EXPR operand may drop
6002 cv-qualification conversions, so make sure we add them if
6003 needed. */
6005 tree op00 = TREE_OPERAND (op0, 0);
6006 tree t_expr = TREE_TYPE (expr);
6007 tree t_op00 = TREE_TYPE (op00);
6009 if (!useless_type_conversion_p (t_expr, t_op00))
6010 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
6011 *expr_p = op00;
6012 ret = GS_OK;
6014 break;
6016 case VIEW_CONVERT_EXPR:
6017 /* Take the address of our operand and then convert it to the type of
6018 this ADDR_EXPR.
6020 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
6021 all clear. The impact of this transformation is even less clear. */
6023 /* If the operand is a useless conversion, look through it. Doing so
6024 guarantees that the ADDR_EXPR and its operand will remain of the
6025 same type. */
6026 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
6027 op0 = TREE_OPERAND (op0, 0);
6029 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
6030 build_fold_addr_expr_loc (loc,
6031 TREE_OPERAND (op0, 0)));
6032 ret = GS_OK;
6033 break;
6035 case MEM_REF:
6036 if (integer_zerop (TREE_OPERAND (op0, 1)))
6037 goto do_indirect_ref;
6039 /* fall through */
6041 default:
6042 /* If we see a call to a declared builtin or see its address
6043 being taken (we can unify those cases here) then we can mark
6044 the builtin for implicit generation by GCC. */
6045 if (TREE_CODE (op0) == FUNCTION_DECL
6046 && fndecl_built_in_p (op0, BUILT_IN_NORMAL)
6047 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
6048 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
6050 /* We use fb_either here because the C frontend sometimes takes
6051 the address of a call that returns a struct; see
6052 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
6053 the implied temporary explicit. */
6055 /* Make the operand addressable. */
6056 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
6057 is_gimple_addressable, fb_either);
6058 if (ret == GS_ERROR)
6059 break;
6061 /* Then mark it. Beware that it may not be possible to do so directly
6062 if a temporary has been created by the gimplification. */
6063 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
6065 op0 = TREE_OPERAND (expr, 0);
6067 /* For various reasons, the gimplification of the expression
6068 may have made a new INDIRECT_REF. */
6069 if (TREE_CODE (op0) == INDIRECT_REF)
6070 goto do_indirect_ref;
6072 mark_addressable (TREE_OPERAND (expr, 0));
6074 /* The FEs may end up building ADDR_EXPRs early on a decl with
6075 an incomplete type. Re-build ADDR_EXPRs in canonical form
6076 here. */
6077 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
6078 *expr_p = build_fold_addr_expr (op0);
6080 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
6081 recompute_tree_invariant_for_addr_expr (*expr_p);
6083 /* If we re-built the ADDR_EXPR add a conversion to the original type
6084 if required. */
6085 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
6086 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
6088 break;
6091 return ret;
6094 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
6095 value; output operands should be a gimple lvalue. */
6097 static enum gimplify_status
6098 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6100 tree expr;
6101 int noutputs;
6102 const char **oconstraints;
6103 int i;
6104 tree link;
6105 const char *constraint;
6106 bool allows_mem, allows_reg, is_inout;
6107 enum gimplify_status ret, tret;
6108 gasm *stmt;
6109 vec<tree, va_gc> *inputs;
6110 vec<tree, va_gc> *outputs;
6111 vec<tree, va_gc> *clobbers;
6112 vec<tree, va_gc> *labels;
6113 tree link_next;
6115 expr = *expr_p;
6116 noutputs = list_length (ASM_OUTPUTS (expr));
6117 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
6119 inputs = NULL;
6120 outputs = NULL;
6121 clobbers = NULL;
6122 labels = NULL;
6124 ret = GS_ALL_DONE;
6125 link_next = NULL_TREE;
6126 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
6128 bool ok;
6129 size_t constraint_len;
6131 link_next = TREE_CHAIN (link);
6133 oconstraints[i]
6134 = constraint
6135 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6136 constraint_len = strlen (constraint);
6137 if (constraint_len == 0)
6138 continue;
6140 ok = parse_output_constraint (&constraint, i, 0, 0,
6141 &allows_mem, &allows_reg, &is_inout);
6142 if (!ok)
6144 ret = GS_ERROR;
6145 is_inout = false;
6148 if (!allows_reg && allows_mem)
6149 mark_addressable (TREE_VALUE (link));
6151 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6152 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
6153 fb_lvalue | fb_mayfail);
6154 if (tret == GS_ERROR)
6156 error ("invalid lvalue in asm output %d", i);
6157 ret = tret;
6160 /* If the constraint does not allow memory make sure we gimplify
6161 it to a register if it is not already but its base is. This
6162 happens for complex and vector components. */
6163 if (!allows_mem)
6165 tree op = TREE_VALUE (link);
6166 if (! is_gimple_val (op)
6167 && is_gimple_reg_type (TREE_TYPE (op))
6168 && is_gimple_reg (get_base_address (op)))
6170 tree tem = create_tmp_reg (TREE_TYPE (op));
6171 tree ass;
6172 if (is_inout)
6174 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
6175 tem, unshare_expr (op));
6176 gimplify_and_add (ass, pre_p);
6178 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
6179 gimplify_and_add (ass, post_p);
6181 TREE_VALUE (link) = tem;
6182 tret = GS_OK;
6186 vec_safe_push (outputs, link);
6187 TREE_CHAIN (link) = NULL_TREE;
6189 if (is_inout)
6191 /* An input/output operand. To give the optimizers more
6192 flexibility, split it into separate input and output
6193 operands. */
6194 tree input;
6195 /* Buffer big enough to format a 32-bit UINT_MAX into. */
6196 char buf[11];
6198 /* Turn the in/out constraint into an output constraint. */
6199 char *p = xstrdup (constraint);
6200 p[0] = '=';
6201 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
6203 /* And add a matching input constraint. */
6204 if (allows_reg)
6206 sprintf (buf, "%u", i);
6208 /* If there are multiple alternatives in the constraint,
6209 handle each of them individually. Those that allow register
6210 will be replaced with operand number, the others will stay
6211 unchanged. */
6212 if (strchr (p, ',') != NULL)
6214 size_t len = 0, buflen = strlen (buf);
6215 char *beg, *end, *str, *dst;
6217 for (beg = p + 1;;)
6219 end = strchr (beg, ',');
6220 if (end == NULL)
6221 end = strchr (beg, '\0');
6222 if ((size_t) (end - beg) < buflen)
6223 len += buflen + 1;
6224 else
6225 len += end - beg + 1;
6226 if (*end)
6227 beg = end + 1;
6228 else
6229 break;
6232 str = (char *) alloca (len);
6233 for (beg = p + 1, dst = str;;)
6235 const char *tem;
6236 bool mem_p, reg_p, inout_p;
6238 end = strchr (beg, ',');
6239 if (end)
6240 *end = '\0';
6241 beg[-1] = '=';
6242 tem = beg - 1;
6243 parse_output_constraint (&tem, i, 0, 0,
6244 &mem_p, &reg_p, &inout_p);
6245 if (dst != str)
6246 *dst++ = ',';
6247 if (reg_p)
6249 memcpy (dst, buf, buflen);
6250 dst += buflen;
6252 else
6254 if (end)
6255 len = end - beg;
6256 else
6257 len = strlen (beg);
6258 memcpy (dst, beg, len);
6259 dst += len;
6261 if (end)
6262 beg = end + 1;
6263 else
6264 break;
6266 *dst = '\0';
6267 input = build_string (dst - str, str);
6269 else
6270 input = build_string (strlen (buf), buf);
6272 else
6273 input = build_string (constraint_len - 1, constraint + 1);
6275 free (p);
6277 input = build_tree_list (build_tree_list (NULL_TREE, input),
6278 unshare_expr (TREE_VALUE (link)));
6279 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6283 link_next = NULL_TREE;
6284 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6286 link_next = TREE_CHAIN (link);
6287 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6288 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6289 oconstraints, &allows_mem, &allows_reg);
6291 /* If we can't make copies, we can only accept memory. */
6292 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6294 if (allows_mem)
6295 allows_reg = 0;
6296 else
6298 error ("impossible constraint in %<asm%>");
6299 error ("non-memory input %d must stay in memory", i);
6300 return GS_ERROR;
6304 /* If the operand is a memory input, it should be an lvalue. */
6305 if (!allows_reg && allows_mem)
6307 tree inputv = TREE_VALUE (link);
6308 STRIP_NOPS (inputv);
6309 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6310 || TREE_CODE (inputv) == PREINCREMENT_EXPR
6311 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6312 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6313 || TREE_CODE (inputv) == MODIFY_EXPR)
6314 TREE_VALUE (link) = error_mark_node;
6315 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6316 is_gimple_lvalue, fb_lvalue | fb_mayfail);
6317 if (tret != GS_ERROR)
6319 /* Unlike output operands, memory inputs are not guaranteed
6320 to be lvalues by the FE, and while the expressions are
6321 marked addressable there, if it is e.g. a statement
6322 expression, temporaries in it might not end up being
6323 addressable. They might be already used in the IL and thus
6324 it is too late to make them addressable now though. */
6325 tree x = TREE_VALUE (link);
6326 while (handled_component_p (x))
6327 x = TREE_OPERAND (x, 0);
6328 if (TREE_CODE (x) == MEM_REF
6329 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6330 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6331 if ((VAR_P (x)
6332 || TREE_CODE (x) == PARM_DECL
6333 || TREE_CODE (x) == RESULT_DECL)
6334 && !TREE_ADDRESSABLE (x)
6335 && is_gimple_reg (x))
6337 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6338 input_location), 0,
6339 "memory input %d is not directly addressable",
6341 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6344 mark_addressable (TREE_VALUE (link));
6345 if (tret == GS_ERROR)
6347 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6348 "memory input %d is not directly addressable", i);
6349 ret = tret;
6352 else
6354 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6355 is_gimple_asm_val, fb_rvalue);
6356 if (tret == GS_ERROR)
6357 ret = tret;
6360 TREE_CHAIN (link) = NULL_TREE;
6361 vec_safe_push (inputs, link);
6364 link_next = NULL_TREE;
6365 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6367 link_next = TREE_CHAIN (link);
6368 TREE_CHAIN (link) = NULL_TREE;
6369 vec_safe_push (clobbers, link);
6372 link_next = NULL_TREE;
6373 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6375 link_next = TREE_CHAIN (link);
6376 TREE_CHAIN (link) = NULL_TREE;
6377 vec_safe_push (labels, link);
6380 /* Do not add ASMs with errors to the gimple IL stream. */
6381 if (ret != GS_ERROR)
6383 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6384 inputs, outputs, clobbers, labels);
6386 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6387 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6388 gimple_asm_set_inline (stmt, ASM_INLINE_P (expr));
6390 gimplify_seq_add_stmt (pre_p, stmt);
6393 return ret;
6396 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6397 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6398 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6399 return to this function.
6401 FIXME should we complexify the prequeue handling instead? Or use flags
6402 for all the cleanups and let the optimizer tighten them up? The current
6403 code seems pretty fragile; it will break on a cleanup within any
6404 non-conditional nesting. But any such nesting would be broken, anyway;
6405 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6406 and continues out of it. We can do that at the RTL level, though, so
6407 having an optimizer to tighten up try/finally regions would be a Good
6408 Thing. */
6410 static enum gimplify_status
6411 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6413 gimple_stmt_iterator iter;
6414 gimple_seq body_sequence = NULL;
6416 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6418 /* We only care about the number of conditions between the innermost
6419 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6420 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6421 int old_conds = gimplify_ctxp->conditions;
6422 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6423 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6424 gimplify_ctxp->conditions = 0;
6425 gimplify_ctxp->conditional_cleanups = NULL;
6426 gimplify_ctxp->in_cleanup_point_expr = true;
6428 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6430 gimplify_ctxp->conditions = old_conds;
6431 gimplify_ctxp->conditional_cleanups = old_cleanups;
6432 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6434 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6436 gimple *wce = gsi_stmt (iter);
6438 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6440 if (gsi_one_before_end_p (iter))
6442 /* Note that gsi_insert_seq_before and gsi_remove do not
6443 scan operands, unlike some other sequence mutators. */
6444 if (!gimple_wce_cleanup_eh_only (wce))
6445 gsi_insert_seq_before_without_update (&iter,
6446 gimple_wce_cleanup (wce),
6447 GSI_SAME_STMT);
6448 gsi_remove (&iter, true);
6449 break;
6451 else
6453 gtry *gtry;
6454 gimple_seq seq;
6455 enum gimple_try_flags kind;
6457 if (gimple_wce_cleanup_eh_only (wce))
6458 kind = GIMPLE_TRY_CATCH;
6459 else
6460 kind = GIMPLE_TRY_FINALLY;
6461 seq = gsi_split_seq_after (iter);
6463 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6464 /* Do not use gsi_replace here, as it may scan operands.
6465 We want to do a simple structural modification only. */
6466 gsi_set_stmt (&iter, gtry);
6467 iter = gsi_start (gtry->eval);
6470 else
6471 gsi_next (&iter);
6474 gimplify_seq_add_seq (pre_p, body_sequence);
6475 if (temp)
6477 *expr_p = temp;
6478 return GS_OK;
6480 else
6482 *expr_p = NULL;
6483 return GS_ALL_DONE;
6487 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6488 is the cleanup action required. EH_ONLY is true if the cleanup should
6489 only be executed if an exception is thrown, not on normal exit.
6490 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
6491 only valid for clobbers. */
6493 static void
6494 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
6495 bool force_uncond = false)
6497 gimple *wce;
6498 gimple_seq cleanup_stmts = NULL;
6500 /* Errors can result in improperly nested cleanups. Which results in
6501 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6502 if (seen_error ())
6503 return;
6505 if (gimple_conditional_context ())
6507 /* If we're in a conditional context, this is more complex. We only
6508 want to run the cleanup if we actually ran the initialization that
6509 necessitates it, but we want to run it after the end of the
6510 conditional context. So we wrap the try/finally around the
6511 condition and use a flag to determine whether or not to actually
6512 run the destructor. Thus
6514 test ? f(A()) : 0
6516 becomes (approximately)
6518 flag = 0;
6519 try {
6520 if (test) { A::A(temp); flag = 1; val = f(temp); }
6521 else { val = 0; }
6522 } finally {
6523 if (flag) A::~A(temp);
6527 if (force_uncond)
6529 gimplify_stmt (&cleanup, &cleanup_stmts);
6530 wce = gimple_build_wce (cleanup_stmts);
6531 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6533 else
6535 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6536 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6537 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6539 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6540 gimplify_stmt (&cleanup, &cleanup_stmts);
6541 wce = gimple_build_wce (cleanup_stmts);
6543 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6544 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6545 gimplify_seq_add_stmt (pre_p, ftrue);
6547 /* Because of this manipulation, and the EH edges that jump
6548 threading cannot redirect, the temporary (VAR) will appear
6549 to be used uninitialized. Don't warn. */
6550 TREE_NO_WARNING (var) = 1;
6553 else
6555 gimplify_stmt (&cleanup, &cleanup_stmts);
6556 wce = gimple_build_wce (cleanup_stmts);
6557 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6558 gimplify_seq_add_stmt (pre_p, wce);
6562 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6564 static enum gimplify_status
6565 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6567 tree targ = *expr_p;
6568 tree temp = TARGET_EXPR_SLOT (targ);
6569 tree init = TARGET_EXPR_INITIAL (targ);
6570 enum gimplify_status ret;
6572 bool unpoison_empty_seq = false;
6573 gimple_stmt_iterator unpoison_it;
6575 if (init)
6577 tree cleanup = NULL_TREE;
6579 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6580 to the temps list. Handle also variable length TARGET_EXPRs. */
6581 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6583 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6584 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6585 gimplify_vla_decl (temp, pre_p);
6587 else
6589 /* Save location where we need to place unpoisoning. It's possible
6590 that a variable will be converted to needs_to_live_in_memory. */
6591 unpoison_it = gsi_last (*pre_p);
6592 unpoison_empty_seq = gsi_end_p (unpoison_it);
6594 gimple_add_tmp_var (temp);
6597 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6598 expression is supposed to initialize the slot. */
6599 if (VOID_TYPE_P (TREE_TYPE (init)))
6600 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6601 else
6603 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6604 init = init_expr;
6605 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6606 init = NULL;
6607 ggc_free (init_expr);
6609 if (ret == GS_ERROR)
6611 /* PR c++/28266 Make sure this is expanded only once. */
6612 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6613 return GS_ERROR;
6615 if (init)
6616 gimplify_and_add (init, pre_p);
6618 /* If needed, push the cleanup for the temp. */
6619 if (TARGET_EXPR_CLEANUP (targ))
6621 if (CLEANUP_EH_ONLY (targ))
6622 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6623 CLEANUP_EH_ONLY (targ), pre_p);
6624 else
6625 cleanup = TARGET_EXPR_CLEANUP (targ);
6628 /* Add a clobber for the temporary going out of scope, like
6629 gimplify_bind_expr. */
6630 if (gimplify_ctxp->in_cleanup_point_expr
6631 && needs_to_live_in_memory (temp))
6633 if (flag_stack_reuse == SR_ALL)
6635 tree clobber = build_clobber (TREE_TYPE (temp));
6636 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6637 gimple_push_cleanup (temp, clobber, false, pre_p, true);
6639 if (asan_poisoned_variables
6640 && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
6641 && dbg_cnt (asan_use_after_scope)
6642 && !gimplify_omp_ctxp)
6644 tree asan_cleanup = build_asan_poison_call_expr (temp);
6645 if (asan_cleanup)
6647 if (unpoison_empty_seq)
6648 unpoison_it = gsi_start (*pre_p);
6650 asan_poison_variable (temp, false, &unpoison_it,
6651 unpoison_empty_seq);
6652 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6656 if (cleanup)
6657 gimple_push_cleanup (temp, cleanup, false, pre_p);
6659 /* Only expand this once. */
6660 TREE_OPERAND (targ, 3) = init;
6661 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6663 else
6664 /* We should have expanded this before. */
6665 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6667 *expr_p = temp;
6668 return GS_OK;
6671 /* Gimplification of expression trees. */
6673 /* Gimplify an expression which appears at statement context. The
6674 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6675 NULL, a new sequence is allocated.
6677 Return true if we actually added a statement to the queue. */
6679 bool
6680 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6682 gimple_seq_node last;
6684 last = gimple_seq_last (*seq_p);
6685 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6686 return last != gimple_seq_last (*seq_p);
6689 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6690 to CTX. If entries already exist, force them to be some flavor of private.
6691 If there is no enclosing parallel, do nothing. */
6693 void
6694 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6696 splay_tree_node n;
6698 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6699 return;
6703 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6704 if (n != NULL)
6706 if (n->value & GOVD_SHARED)
6707 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6708 else if (n->value & GOVD_MAP)
6709 n->value |= GOVD_MAP_TO_ONLY;
6710 else
6711 return;
6713 else if ((ctx->region_type & ORT_TARGET) != 0)
6715 if (ctx->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
6716 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6717 else
6718 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6720 else if (ctx->region_type != ORT_WORKSHARE
6721 && ctx->region_type != ORT_TASKGROUP
6722 && ctx->region_type != ORT_SIMD
6723 && ctx->region_type != ORT_ACC
6724 && !(ctx->region_type & ORT_TARGET_DATA))
6725 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6727 ctx = ctx->outer_context;
6729 while (ctx);
6732 /* Similarly for each of the type sizes of TYPE. */
6734 static void
6735 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6737 if (type == NULL || type == error_mark_node)
6738 return;
6739 type = TYPE_MAIN_VARIANT (type);
6741 if (ctx->privatized_types->add (type))
6742 return;
6744 switch (TREE_CODE (type))
6746 case INTEGER_TYPE:
6747 case ENUMERAL_TYPE:
6748 case BOOLEAN_TYPE:
6749 case REAL_TYPE:
6750 case FIXED_POINT_TYPE:
6751 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6752 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6753 break;
6755 case ARRAY_TYPE:
6756 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6757 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6758 break;
6760 case RECORD_TYPE:
6761 case UNION_TYPE:
6762 case QUAL_UNION_TYPE:
6764 tree field;
6765 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6766 if (TREE_CODE (field) == FIELD_DECL)
6768 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6769 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6772 break;
6774 case POINTER_TYPE:
6775 case REFERENCE_TYPE:
6776 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6777 break;
6779 default:
6780 break;
6783 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6784 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6785 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6788 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6790 static void
6791 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6793 splay_tree_node n;
6794 unsigned int nflags;
6795 tree t;
6797 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6798 return;
6800 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6801 there are constructors involved somewhere. Exception is a shared clause,
6802 there is nothing privatized in that case. */
6803 if ((flags & GOVD_SHARED) == 0
6804 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6805 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6806 flags |= GOVD_SEEN;
6808 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6809 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6811 /* We shouldn't be re-adding the decl with the same data
6812 sharing class. */
6813 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6814 nflags = n->value | flags;
6815 /* The only combination of data sharing classes we should see is
6816 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6817 reduction variables to be used in data sharing clauses. */
6818 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6819 || ((nflags & GOVD_DATA_SHARE_CLASS)
6820 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6821 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6822 n->value = nflags;
6823 return;
6826 /* When adding a variable-sized variable, we have to handle all sorts
6827 of additional bits of data: the pointer replacement variable, and
6828 the parameters of the type. */
6829 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6831 /* Add the pointer replacement variable as PRIVATE if the variable
6832 replacement is private, else FIRSTPRIVATE since we'll need the
6833 address of the original variable either for SHARED, or for the
6834 copy into or out of the context. */
6835 if (!(flags & GOVD_LOCAL) && ctx->region_type != ORT_TASKGROUP)
6837 if (flags & GOVD_MAP)
6838 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6839 else if (flags & GOVD_PRIVATE)
6840 nflags = GOVD_PRIVATE;
6841 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6842 && (flags & GOVD_FIRSTPRIVATE))
6843 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6844 else
6845 nflags = GOVD_FIRSTPRIVATE;
6846 nflags |= flags & GOVD_SEEN;
6847 t = DECL_VALUE_EXPR (decl);
6848 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6849 t = TREE_OPERAND (t, 0);
6850 gcc_assert (DECL_P (t));
6851 omp_add_variable (ctx, t, nflags);
6854 /* Add all of the variable and type parameters (which should have
6855 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6856 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6857 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6858 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6860 /* The variable-sized variable itself is never SHARED, only some form
6861 of PRIVATE. The sharing would take place via the pointer variable
6862 which we remapped above. */
6863 if (flags & GOVD_SHARED)
6864 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6865 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6867 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6868 alloca statement we generate for the variable, so make sure it
6869 is available. This isn't automatically needed for the SHARED
6870 case, since we won't be allocating local storage then.
6871 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6872 in this case omp_notice_variable will be called later
6873 on when it is gimplified. */
6874 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6875 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6876 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6878 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6879 && lang_hooks.decls.omp_privatize_by_reference (decl))
6881 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6883 /* Similar to the direct variable sized case above, we'll need the
6884 size of references being privatized. */
6885 if ((flags & GOVD_SHARED) == 0)
6887 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6888 if (DECL_P (t))
6889 omp_notice_variable (ctx, t, true);
6893 if (n != NULL)
6894 n->value |= flags;
6895 else
6896 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6898 /* For reductions clauses in OpenACC loop directives, by default create a
6899 copy clause on the enclosing parallel construct for carrying back the
6900 results. */
6901 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6903 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6904 while (outer_ctx)
6906 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6907 if (n != NULL)
6909 /* Ignore local variables and explicitly declared clauses. */
6910 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6911 break;
6912 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6914 /* According to the OpenACC spec, such a reduction variable
6915 should already have a copy map on a kernels construct,
6916 verify that here. */
6917 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6918 && (n->value & GOVD_MAP));
6920 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6922 /* Remove firstprivate and make it a copy map. */
6923 n->value &= ~GOVD_FIRSTPRIVATE;
6924 n->value |= GOVD_MAP;
6927 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6929 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6930 GOVD_MAP | GOVD_SEEN);
6931 break;
6933 outer_ctx = outer_ctx->outer_context;
6938 /* Notice a threadprivate variable DECL used in OMP context CTX.
6939 This just prints out diagnostics about threadprivate variable uses
6940 in untied tasks. If DECL2 is non-NULL, prevent this warning
6941 on that variable. */
6943 static bool
6944 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6945 tree decl2)
6947 splay_tree_node n;
6948 struct gimplify_omp_ctx *octx;
6950 for (octx = ctx; octx; octx = octx->outer_context)
6951 if ((octx->region_type & ORT_TARGET) != 0)
6953 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6954 if (n == NULL)
6956 error ("threadprivate variable %qE used in target region",
6957 DECL_NAME (decl));
6958 error_at (octx->location, "enclosing target region");
6959 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6961 if (decl2)
6962 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6965 if (ctx->region_type != ORT_UNTIED_TASK)
6966 return false;
6967 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6968 if (n == NULL)
6970 error ("threadprivate variable %qE used in untied task",
6971 DECL_NAME (decl));
6972 error_at (ctx->location, "enclosing task");
6973 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6975 if (decl2)
6976 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6977 return false;
6980 /* Return true if global var DECL is device resident. */
6982 static bool
6983 device_resident_p (tree decl)
6985 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6987 if (!attr)
6988 return false;
6990 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6992 tree c = TREE_VALUE (t);
6993 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6994 return true;
6997 return false;
7000 /* Return true if DECL has an ACC DECLARE attribute. */
7002 static bool
7003 is_oacc_declared (tree decl)
7005 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
7006 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
7007 return declared != NULL_TREE;
7010 /* Determine outer default flags for DECL mentioned in an OMP region
7011 but not declared in an enclosing clause.
7013 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
7014 remapped firstprivate instead of shared. To some extent this is
7015 addressed in omp_firstprivatize_type_sizes, but not
7016 effectively. */
7018 static unsigned
7019 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
7020 bool in_code, unsigned flags)
7022 enum omp_clause_default_kind default_kind = ctx->default_kind;
7023 enum omp_clause_default_kind kind;
7025 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
7026 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7027 default_kind = kind;
7029 switch (default_kind)
7031 case OMP_CLAUSE_DEFAULT_NONE:
7033 const char *rtype;
7035 if (ctx->region_type & ORT_PARALLEL)
7036 rtype = "parallel";
7037 else if ((ctx->region_type & ORT_TASKLOOP) == ORT_TASKLOOP)
7038 rtype = "taskloop";
7039 else if (ctx->region_type & ORT_TASK)
7040 rtype = "task";
7041 else if (ctx->region_type & ORT_TEAMS)
7042 rtype = "teams";
7043 else
7044 gcc_unreachable ();
7046 error ("%qE not specified in enclosing %qs",
7047 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
7048 error_at (ctx->location, "enclosing %qs", rtype);
7050 /* FALLTHRU */
7051 case OMP_CLAUSE_DEFAULT_SHARED:
7052 flags |= GOVD_SHARED;
7053 break;
7054 case OMP_CLAUSE_DEFAULT_PRIVATE:
7055 flags |= GOVD_PRIVATE;
7056 break;
7057 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
7058 flags |= GOVD_FIRSTPRIVATE;
7059 break;
7060 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7061 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
7062 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
7063 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
7065 omp_notice_variable (octx, decl, in_code);
7066 for (; octx; octx = octx->outer_context)
7068 splay_tree_node n2;
7070 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
7071 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
7072 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
7073 continue;
7074 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7076 flags |= GOVD_FIRSTPRIVATE;
7077 goto found_outer;
7079 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7081 flags |= GOVD_SHARED;
7082 goto found_outer;
7087 if (TREE_CODE (decl) == PARM_DECL
7088 || (!is_global_var (decl)
7089 && DECL_CONTEXT (decl) == current_function_decl))
7090 flags |= GOVD_FIRSTPRIVATE;
7091 else
7092 flags |= GOVD_SHARED;
7093 found_outer:
7094 break;
7096 default:
7097 gcc_unreachable ();
7100 return flags;
7104 /* Determine outer default flags for DECL mentioned in an OACC region
7105 but not declared in an enclosing clause. */
7107 static unsigned
7108 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7110 const char *rkind;
7111 bool on_device = false;
7112 bool declared = is_oacc_declared (decl);
7113 tree type = TREE_TYPE (decl);
7115 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7116 type = TREE_TYPE (type);
7118 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7119 && is_global_var (decl)
7120 && device_resident_p (decl))
7122 on_device = true;
7123 flags |= GOVD_MAP_TO_ONLY;
7126 switch (ctx->region_type)
7128 case ORT_ACC_KERNELS:
7129 rkind = "kernels";
7131 if (AGGREGATE_TYPE_P (type))
7133 /* Aggregates default to 'present_or_copy', or 'present'. */
7134 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7135 flags |= GOVD_MAP;
7136 else
7137 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7139 else
7140 /* Scalars default to 'copy'. */
7141 flags |= GOVD_MAP | GOVD_MAP_FORCE;
7143 break;
7145 case ORT_ACC_PARALLEL:
7146 rkind = "parallel";
7148 if (on_device || declared)
7149 flags |= GOVD_MAP;
7150 else if (AGGREGATE_TYPE_P (type))
7152 /* Aggregates default to 'present_or_copy', or 'present'. */
7153 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7154 flags |= GOVD_MAP;
7155 else
7156 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7158 else
7159 /* Scalars default to 'firstprivate'. */
7160 flags |= GOVD_FIRSTPRIVATE;
7162 break;
7164 default:
7165 gcc_unreachable ();
7168 if (DECL_ARTIFICIAL (decl))
7169 ; /* We can get compiler-generated decls, and should not complain
7170 about them. */
7171 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7173 error ("%qE not specified in enclosing OpenACC %qs construct",
7174 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7175 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7177 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7178 ; /* Handled above. */
7179 else
7180 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7182 return flags;
7185 /* Record the fact that DECL was used within the OMP context CTX.
7186 IN_CODE is true when real code uses DECL, and false when we should
7187 merely emit default(none) errors. Return true if DECL is going to
7188 be remapped and thus DECL shouldn't be gimplified into its
7189 DECL_VALUE_EXPR (if any). */
7191 static bool
7192 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7194 splay_tree_node n;
7195 unsigned flags = in_code ? GOVD_SEEN : 0;
7196 bool ret = false, shared;
7198 if (error_operand_p (decl))
7199 return false;
7201 if (ctx->region_type == ORT_NONE)
7202 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7204 if (is_global_var (decl))
7206 /* Threadprivate variables are predetermined. */
7207 if (DECL_THREAD_LOCAL_P (decl))
7208 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7210 if (DECL_HAS_VALUE_EXPR_P (decl))
7212 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7214 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7215 return omp_notice_threadprivate_variable (ctx, decl, value);
7218 if (gimplify_omp_ctxp->outer_context == NULL
7219 && VAR_P (decl)
7220 && oacc_get_fn_attrib (current_function_decl))
7222 location_t loc = DECL_SOURCE_LOCATION (decl);
7224 if (lookup_attribute ("omp declare target link",
7225 DECL_ATTRIBUTES (decl)))
7227 error_at (loc,
7228 "%qE with %<link%> clause used in %<routine%> function",
7229 DECL_NAME (decl));
7230 return false;
7232 else if (!lookup_attribute ("omp declare target",
7233 DECL_ATTRIBUTES (decl)))
7235 error_at (loc,
7236 "%qE requires a %<declare%> directive for use "
7237 "in a %<routine%> function", DECL_NAME (decl));
7238 return false;
7243 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7244 if ((ctx->region_type & ORT_TARGET) != 0)
7246 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7247 if (n == NULL)
7249 unsigned nflags = flags;
7250 if ((ctx->region_type & ORT_ACC) == 0)
7252 bool is_declare_target = false;
7253 if (is_global_var (decl)
7254 && varpool_node::get_create (decl)->offloadable)
7256 struct gimplify_omp_ctx *octx;
7257 for (octx = ctx->outer_context;
7258 octx; octx = octx->outer_context)
7260 n = splay_tree_lookup (octx->variables,
7261 (splay_tree_key)decl);
7262 if (n
7263 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7264 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7265 break;
7267 is_declare_target = octx == NULL;
7269 if (!is_declare_target)
7271 int gdmk;
7272 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7273 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7274 && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7275 == POINTER_TYPE)))
7276 gdmk = GDMK_POINTER;
7277 else if (lang_hooks.decls.omp_scalar_p (decl))
7278 gdmk = GDMK_SCALAR;
7279 else
7280 gdmk = GDMK_AGGREGATE;
7281 if (ctx->defaultmap[gdmk] == 0)
7283 tree d = lang_hooks.decls.omp_report_decl (decl);
7284 error ("%qE not specified in enclosing %<target%>",
7285 DECL_NAME (d));
7286 error_at (ctx->location, "enclosing %<target%>");
7288 else if (ctx->defaultmap[gdmk]
7289 & (GOVD_MAP_0LEN_ARRAY | GOVD_FIRSTPRIVATE))
7290 nflags |= ctx->defaultmap[gdmk];
7291 else
7293 gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
7294 nflags |= ctx->defaultmap[gdmk] & ~GOVD_MAP;
7299 struct gimplify_omp_ctx *octx = ctx->outer_context;
7300 if ((ctx->region_type & ORT_ACC) && octx)
7302 /* Look in outer OpenACC contexts, to see if there's a
7303 data attribute for this variable. */
7304 omp_notice_variable (octx, decl, in_code);
7306 for (; octx; octx = octx->outer_context)
7308 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7309 break;
7310 splay_tree_node n2
7311 = splay_tree_lookup (octx->variables,
7312 (splay_tree_key) decl);
7313 if (n2)
7315 if (octx->region_type == ORT_ACC_HOST_DATA)
7316 error ("variable %qE declared in enclosing "
7317 "%<host_data%> region", DECL_NAME (decl));
7318 nflags |= GOVD_MAP;
7319 if (octx->region_type == ORT_ACC_DATA
7320 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7321 nflags |= GOVD_MAP_0LEN_ARRAY;
7322 goto found_outer;
7327 if ((nflags & ~(GOVD_MAP_TO_ONLY | GOVD_MAP_FROM_ONLY
7328 | GOVD_MAP_ALLOC_ONLY)) == flags)
7330 tree type = TREE_TYPE (decl);
7332 if (gimplify_omp_ctxp->target_firstprivatize_array_bases
7333 && lang_hooks.decls.omp_privatize_by_reference (decl))
7334 type = TREE_TYPE (type);
7335 if (!lang_hooks.types.omp_mappable_type (type))
7337 error ("%qD referenced in target region does not have "
7338 "a mappable type", decl);
7339 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7341 else
7343 if ((ctx->region_type & ORT_ACC) != 0)
7344 nflags = oacc_default_clause (ctx, decl, flags);
7345 else
7346 nflags |= GOVD_MAP;
7349 found_outer:
7350 omp_add_variable (ctx, decl, nflags);
7352 else
7354 /* If nothing changed, there's nothing left to do. */
7355 if ((n->value & flags) == flags)
7356 return ret;
7357 flags |= n->value;
7358 n->value = flags;
7360 goto do_outer;
7363 if (n == NULL)
7365 if (ctx->region_type == ORT_WORKSHARE
7366 || ctx->region_type == ORT_TASKGROUP
7367 || ctx->region_type == ORT_SIMD
7368 || ctx->region_type == ORT_ACC
7369 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7370 goto do_outer;
7372 flags = omp_default_clause (ctx, decl, in_code, flags);
7374 if ((flags & GOVD_PRIVATE)
7375 && lang_hooks.decls.omp_private_outer_ref (decl))
7376 flags |= GOVD_PRIVATE_OUTER_REF;
7378 omp_add_variable (ctx, decl, flags);
7380 shared = (flags & GOVD_SHARED) != 0;
7381 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7382 goto do_outer;
7385 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7386 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7387 && DECL_SIZE (decl))
7389 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7391 splay_tree_node n2;
7392 tree t = DECL_VALUE_EXPR (decl);
7393 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7394 t = TREE_OPERAND (t, 0);
7395 gcc_assert (DECL_P (t));
7396 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7397 n2->value |= GOVD_SEEN;
7399 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7400 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7401 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7402 != INTEGER_CST))
7404 splay_tree_node n2;
7405 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7406 gcc_assert (DECL_P (t));
7407 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7408 if (n2)
7409 omp_notice_variable (ctx, t, true);
7413 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7414 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7416 /* If nothing changed, there's nothing left to do. */
7417 if ((n->value & flags) == flags)
7418 return ret;
7419 flags |= n->value;
7420 n->value = flags;
7422 do_outer:
7423 /* If the variable is private in the current context, then we don't
7424 need to propagate anything to an outer context. */
7425 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7426 return ret;
7427 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7428 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7429 return ret;
7430 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7431 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7432 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7433 return ret;
7434 if (ctx->outer_context
7435 && omp_notice_variable (ctx->outer_context, decl, in_code))
7436 return true;
7437 return ret;
7440 /* Verify that DECL is private within CTX. If there's specific information
7441 to the contrary in the innermost scope, generate an error. */
7443 static bool
7444 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7446 splay_tree_node n;
7448 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7449 if (n != NULL)
7451 if (n->value & GOVD_SHARED)
7453 if (ctx == gimplify_omp_ctxp)
7455 if (simd)
7456 error ("iteration variable %qE is predetermined linear",
7457 DECL_NAME (decl));
7458 else
7459 error ("iteration variable %qE should be private",
7460 DECL_NAME (decl));
7461 n->value = GOVD_PRIVATE;
7462 return true;
7464 else
7465 return false;
7467 else if ((n->value & GOVD_EXPLICIT) != 0
7468 && (ctx == gimplify_omp_ctxp
7469 || (ctx->region_type == ORT_COMBINED_PARALLEL
7470 && gimplify_omp_ctxp->outer_context == ctx)))
7472 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7473 error ("iteration variable %qE should not be firstprivate",
7474 DECL_NAME (decl));
7475 else if ((n->value & GOVD_REDUCTION) != 0)
7476 error ("iteration variable %qE should not be reduction",
7477 DECL_NAME (decl));
7478 else if (simd != 1 && (n->value & GOVD_LINEAR) != 0)
7479 error ("iteration variable %qE should not be linear",
7480 DECL_NAME (decl));
7482 return (ctx == gimplify_omp_ctxp
7483 || (ctx->region_type == ORT_COMBINED_PARALLEL
7484 && gimplify_omp_ctxp->outer_context == ctx));
7487 if (ctx->region_type != ORT_WORKSHARE
7488 && ctx->region_type != ORT_TASKGROUP
7489 && ctx->region_type != ORT_SIMD
7490 && ctx->region_type != ORT_ACC)
7491 return false;
7492 else if (ctx->outer_context)
7493 return omp_is_private (ctx->outer_context, decl, simd);
7494 return false;
7497 /* Return true if DECL is private within a parallel region
7498 that binds to the current construct's context or in parallel
7499 region's REDUCTION clause. */
7501 static bool
7502 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7504 splay_tree_node n;
7508 ctx = ctx->outer_context;
7509 if (ctx == NULL)
7511 if (is_global_var (decl))
7512 return false;
7514 /* References might be private, but might be shared too,
7515 when checking for copyprivate, assume they might be
7516 private, otherwise assume they might be shared. */
7517 if (copyprivate)
7518 return true;
7520 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7521 return false;
7523 /* Treat C++ privatized non-static data members outside
7524 of the privatization the same. */
7525 if (omp_member_access_dummy_var (decl))
7526 return false;
7528 return true;
7531 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7533 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7534 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7535 continue;
7537 if (n != NULL)
7539 if ((n->value & GOVD_LOCAL) != 0
7540 && omp_member_access_dummy_var (decl))
7541 return false;
7542 return (n->value & GOVD_SHARED) == 0;
7545 while (ctx->region_type == ORT_WORKSHARE
7546 || ctx->region_type == ORT_TASKGROUP
7547 || ctx->region_type == ORT_SIMD
7548 || ctx->region_type == ORT_ACC);
7549 return false;
7552 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7554 static tree
7555 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7557 tree t = *tp;
7559 /* If this node has been visited, unmark it and keep looking. */
7560 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7561 return t;
7563 if (IS_TYPE_OR_DECL_P (t))
7564 *walk_subtrees = 0;
7565 return NULL_TREE;
7568 /* If *LIST_P contains any OpenMP depend clauses with iterators,
7569 lower all the depend clauses by populating corresponding depend
7570 array. Returns 0 if there are no such depend clauses, or
7571 2 if all depend clauses should be removed, 1 otherwise. */
7573 static int
7574 gimplify_omp_depend (tree *list_p, gimple_seq *pre_p)
7576 tree c;
7577 gimple *g;
7578 size_t n[4] = { 0, 0, 0, 0 };
7579 bool unused[4];
7580 tree counts[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
7581 tree last_iter = NULL_TREE, last_count = NULL_TREE;
7582 size_t i, j;
7583 location_t first_loc = UNKNOWN_LOCATION;
7585 for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
7586 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
7588 switch (OMP_CLAUSE_DEPEND_KIND (c))
7590 case OMP_CLAUSE_DEPEND_IN:
7591 i = 2;
7592 break;
7593 case OMP_CLAUSE_DEPEND_OUT:
7594 case OMP_CLAUSE_DEPEND_INOUT:
7595 i = 0;
7596 break;
7597 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
7598 i = 1;
7599 break;
7600 case OMP_CLAUSE_DEPEND_DEPOBJ:
7601 i = 3;
7602 break;
7603 case OMP_CLAUSE_DEPEND_SOURCE:
7604 case OMP_CLAUSE_DEPEND_SINK:
7605 continue;
7606 default:
7607 gcc_unreachable ();
7609 tree t = OMP_CLAUSE_DECL (c);
7610 if (first_loc == UNKNOWN_LOCATION)
7611 first_loc = OMP_CLAUSE_LOCATION (c);
7612 if (TREE_CODE (t) == TREE_LIST
7613 && TREE_PURPOSE (t)
7614 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7616 if (TREE_PURPOSE (t) != last_iter)
7618 tree tcnt = size_one_node;
7619 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
7621 if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
7622 is_gimple_val, fb_rvalue) == GS_ERROR
7623 || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
7624 is_gimple_val, fb_rvalue) == GS_ERROR
7625 || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
7626 is_gimple_val, fb_rvalue) == GS_ERROR
7627 || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
7628 is_gimple_val, fb_rvalue)
7629 == GS_ERROR))
7630 return 2;
7631 tree var = TREE_VEC_ELT (it, 0);
7632 tree begin = TREE_VEC_ELT (it, 1);
7633 tree end = TREE_VEC_ELT (it, 2);
7634 tree step = TREE_VEC_ELT (it, 3);
7635 tree orig_step = TREE_VEC_ELT (it, 4);
7636 tree type = TREE_TYPE (var);
7637 tree stype = TREE_TYPE (step);
7638 location_t loc = DECL_SOURCE_LOCATION (var);
7639 tree endmbegin;
7640 /* Compute count for this iterator as
7641 orig_step > 0
7642 ? (begin < end ? (end - begin + (step - 1)) / step : 0)
7643 : (begin > end ? (end - begin + (step + 1)) / step : 0)
7644 and compute product of those for the entire depend
7645 clause. */
7646 if (POINTER_TYPE_P (type))
7647 endmbegin = fold_build2_loc (loc, POINTER_DIFF_EXPR,
7648 stype, end, begin);
7649 else
7650 endmbegin = fold_build2_loc (loc, MINUS_EXPR, type,
7651 end, begin);
7652 tree stepm1 = fold_build2_loc (loc, MINUS_EXPR, stype,
7653 step,
7654 build_int_cst (stype, 1));
7655 tree stepp1 = fold_build2_loc (loc, PLUS_EXPR, stype, step,
7656 build_int_cst (stype, 1));
7657 tree pos = fold_build2_loc (loc, PLUS_EXPR, stype,
7658 unshare_expr (endmbegin),
7659 stepm1);
7660 pos = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype,
7661 pos, step);
7662 tree neg = fold_build2_loc (loc, PLUS_EXPR, stype,
7663 endmbegin, stepp1);
7664 if (TYPE_UNSIGNED (stype))
7666 neg = fold_build1_loc (loc, NEGATE_EXPR, stype, neg);
7667 step = fold_build1_loc (loc, NEGATE_EXPR, stype, step);
7669 neg = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype,
7670 neg, step);
7671 step = NULL_TREE;
7672 tree cond = fold_build2_loc (loc, LT_EXPR,
7673 boolean_type_node,
7674 begin, end);
7675 pos = fold_build3_loc (loc, COND_EXPR, stype, cond, pos,
7676 build_int_cst (stype, 0));
7677 cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
7678 end, begin);
7679 neg = fold_build3_loc (loc, COND_EXPR, stype, cond, neg,
7680 build_int_cst (stype, 0));
7681 tree osteptype = TREE_TYPE (orig_step);
7682 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
7683 orig_step,
7684 build_int_cst (osteptype, 0));
7685 tree cnt = fold_build3_loc (loc, COND_EXPR, stype,
7686 cond, pos, neg);
7687 cnt = fold_convert_loc (loc, sizetype, cnt);
7688 if (gimplify_expr (&cnt, pre_p, NULL, is_gimple_val,
7689 fb_rvalue) == GS_ERROR)
7690 return 2;
7691 tcnt = size_binop_loc (loc, MULT_EXPR, tcnt, cnt);
7693 if (gimplify_expr (&tcnt, pre_p, NULL, is_gimple_val,
7694 fb_rvalue) == GS_ERROR)
7695 return 2;
7696 last_iter = TREE_PURPOSE (t);
7697 last_count = tcnt;
7699 if (counts[i] == NULL_TREE)
7700 counts[i] = last_count;
7701 else
7702 counts[i] = size_binop_loc (OMP_CLAUSE_LOCATION (c),
7703 PLUS_EXPR, counts[i], last_count);
7705 else
7706 n[i]++;
7708 for (i = 0; i < 4; i++)
7709 if (counts[i])
7710 break;
7711 if (i == 4)
7712 return 0;
7714 tree total = size_zero_node;
7715 for (i = 0; i < 4; i++)
7717 unused[i] = counts[i] == NULL_TREE && n[i] == 0;
7718 if (counts[i] == NULL_TREE)
7719 counts[i] = size_zero_node;
7720 if (n[i])
7721 counts[i] = size_binop (PLUS_EXPR, counts[i], size_int (n[i]));
7722 if (gimplify_expr (&counts[i], pre_p, NULL, is_gimple_val,
7723 fb_rvalue) == GS_ERROR)
7724 return 2;
7725 total = size_binop (PLUS_EXPR, total, counts[i]);
7728 if (gimplify_expr (&total, pre_p, NULL, is_gimple_val, fb_rvalue)
7729 == GS_ERROR)
7730 return 2;
7731 bool is_old = unused[1] && unused[3];
7732 tree totalpx = size_binop (PLUS_EXPR, unshare_expr (total),
7733 size_int (is_old ? 1 : 4));
7734 tree type = build_array_type (ptr_type_node, build_index_type (totalpx));
7735 tree array = create_tmp_var_raw (type);
7736 TREE_ADDRESSABLE (array) = 1;
7737 if (TREE_CODE (totalpx) != INTEGER_CST)
7739 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (array)))
7740 gimplify_type_sizes (TREE_TYPE (array), pre_p);
7741 if (gimplify_omp_ctxp)
7743 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7744 while (ctx
7745 && (ctx->region_type == ORT_WORKSHARE
7746 || ctx->region_type == ORT_TASKGROUP
7747 || ctx->region_type == ORT_SIMD
7748 || ctx->region_type == ORT_ACC))
7749 ctx = ctx->outer_context;
7750 if (ctx)
7751 omp_add_variable (ctx, array, GOVD_LOCAL | GOVD_SEEN);
7753 gimplify_vla_decl (array, pre_p);
7755 else
7756 gimple_add_tmp_var (array);
7757 tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
7758 NULL_TREE);
7759 tree tem;
7760 if (!is_old)
7762 tem = build2 (MODIFY_EXPR, void_type_node, r,
7763 build_int_cst (ptr_type_node, 0));
7764 gimplify_and_add (tem, pre_p);
7765 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
7766 NULL_TREE);
7768 tem = build2 (MODIFY_EXPR, void_type_node, r,
7769 fold_convert (ptr_type_node, total));
7770 gimplify_and_add (tem, pre_p);
7771 for (i = 1; i < (is_old ? 2 : 4); i++)
7773 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (i + !is_old),
7774 NULL_TREE, NULL_TREE);
7775 tem = build2 (MODIFY_EXPR, void_type_node, r, counts[i - 1]);
7776 gimplify_and_add (tem, pre_p);
7779 tree cnts[4];
7780 for (j = 4; j; j--)
7781 if (!unused[j - 1])
7782 break;
7783 for (i = 0; i < 4; i++)
7785 if (i && (i >= j || unused[i - 1]))
7787 cnts[i] = cnts[i - 1];
7788 continue;
7790 cnts[i] = create_tmp_var (sizetype);
7791 if (i == 0)
7792 g = gimple_build_assign (cnts[i], size_int (is_old ? 2 : 5));
7793 else
7795 tree t;
7796 if (is_old)
7797 t = size_binop (PLUS_EXPR, counts[0], size_int (2));
7798 else
7799 t = size_binop (PLUS_EXPR, cnts[i - 1], counts[i - 1]);
7800 if (gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue)
7801 == GS_ERROR)
7802 return 2;
7803 g = gimple_build_assign (cnts[i], t);
7805 gimple_seq_add_stmt (pre_p, g);
7808 last_iter = NULL_TREE;
7809 tree last_bind = NULL_TREE;
7810 tree *last_body = NULL;
7811 for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
7812 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
7814 switch (OMP_CLAUSE_DEPEND_KIND (c))
7816 case OMP_CLAUSE_DEPEND_IN:
7817 i = 2;
7818 break;
7819 case OMP_CLAUSE_DEPEND_OUT:
7820 case OMP_CLAUSE_DEPEND_INOUT:
7821 i = 0;
7822 break;
7823 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
7824 i = 1;
7825 break;
7826 case OMP_CLAUSE_DEPEND_DEPOBJ:
7827 i = 3;
7828 break;
7829 case OMP_CLAUSE_DEPEND_SOURCE:
7830 case OMP_CLAUSE_DEPEND_SINK:
7831 continue;
7832 default:
7833 gcc_unreachable ();
7835 tree t = OMP_CLAUSE_DECL (c);
7836 if (TREE_CODE (t) == TREE_LIST
7837 && TREE_PURPOSE (t)
7838 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7840 if (TREE_PURPOSE (t) != last_iter)
7842 if (last_bind)
7843 gimplify_and_add (last_bind, pre_p);
7844 tree block = TREE_VEC_ELT (TREE_PURPOSE (t), 5);
7845 last_bind = build3 (BIND_EXPR, void_type_node,
7846 BLOCK_VARS (block), NULL, block);
7847 TREE_SIDE_EFFECTS (last_bind) = 1;
7848 SET_EXPR_LOCATION (last_bind, OMP_CLAUSE_LOCATION (c));
7849 tree *p = &BIND_EXPR_BODY (last_bind);
7850 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
7852 tree var = TREE_VEC_ELT (it, 0);
7853 tree begin = TREE_VEC_ELT (it, 1);
7854 tree end = TREE_VEC_ELT (it, 2);
7855 tree step = TREE_VEC_ELT (it, 3);
7856 tree orig_step = TREE_VEC_ELT (it, 4);
7857 tree type = TREE_TYPE (var);
7858 location_t loc = DECL_SOURCE_LOCATION (var);
7859 /* Emit:
7860 var = begin;
7861 goto cond_label;
7862 beg_label:
7864 var = var + step;
7865 cond_label:
7866 if (orig_step > 0) {
7867 if (var < end) goto beg_label;
7868 } else {
7869 if (var > end) goto beg_label;
7871 for each iterator, with inner iterators added to
7872 the ... above. */
7873 tree beg_label = create_artificial_label (loc);
7874 tree cond_label = NULL_TREE;
7875 tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
7876 var, begin);
7877 append_to_statement_list_force (tem, p);
7878 tem = build_and_jump (&cond_label);
7879 append_to_statement_list_force (tem, p);
7880 tem = build1 (LABEL_EXPR, void_type_node, beg_label);
7881 append_to_statement_list (tem, p);
7882 tree bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
7883 NULL_TREE, NULL_TREE);
7884 TREE_SIDE_EFFECTS (bind) = 1;
7885 SET_EXPR_LOCATION (bind, loc);
7886 append_to_statement_list_force (bind, p);
7887 if (POINTER_TYPE_P (type))
7888 tem = build2_loc (loc, POINTER_PLUS_EXPR, type,
7889 var, fold_convert_loc (loc, sizetype,
7890 step));
7891 else
7892 tem = build2_loc (loc, PLUS_EXPR, type, var, step);
7893 tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
7894 var, tem);
7895 append_to_statement_list_force (tem, p);
7896 tem = build1 (LABEL_EXPR, void_type_node, cond_label);
7897 append_to_statement_list (tem, p);
7898 tree cond = fold_build2_loc (loc, LT_EXPR,
7899 boolean_type_node,
7900 var, end);
7901 tree pos
7902 = fold_build3_loc (loc, COND_EXPR, void_type_node,
7903 cond, build_and_jump (&beg_label),
7904 void_node);
7905 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
7906 var, end);
7907 tree neg
7908 = fold_build3_loc (loc, COND_EXPR, void_type_node,
7909 cond, build_and_jump (&beg_label),
7910 void_node);
7911 tree osteptype = TREE_TYPE (orig_step);
7912 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
7913 orig_step,
7914 build_int_cst (osteptype, 0));
7915 tem = fold_build3_loc (loc, COND_EXPR, void_type_node,
7916 cond, pos, neg);
7917 append_to_statement_list_force (tem, p);
7918 p = &BIND_EXPR_BODY (bind);
7920 last_body = p;
7922 last_iter = TREE_PURPOSE (t);
7923 if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
7925 append_to_statement_list (TREE_OPERAND (TREE_VALUE (t),
7926 0), last_body);
7927 TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
7929 if (error_operand_p (TREE_VALUE (t)))
7930 return 2;
7931 TREE_VALUE (t) = build_fold_addr_expr (TREE_VALUE (t));
7932 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
7933 NULL_TREE, NULL_TREE);
7934 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
7935 void_type_node, r, TREE_VALUE (t));
7936 append_to_statement_list_force (tem, last_body);
7937 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
7938 void_type_node, cnts[i],
7939 size_binop (PLUS_EXPR, cnts[i], size_int (1)));
7940 append_to_statement_list_force (tem, last_body);
7941 TREE_VALUE (t) = null_pointer_node;
7943 else
7945 if (last_bind)
7947 gimplify_and_add (last_bind, pre_p);
7948 last_bind = NULL_TREE;
7950 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
7952 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
7953 NULL, is_gimple_val, fb_rvalue);
7954 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
7956 if (error_operand_p (OMP_CLAUSE_DECL (c)))
7957 return 2;
7958 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
7959 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
7960 is_gimple_val, fb_rvalue) == GS_ERROR)
7961 return 2;
7962 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
7963 NULL_TREE, NULL_TREE);
7964 tem = build2 (MODIFY_EXPR, void_type_node, r, OMP_CLAUSE_DECL (c));
7965 gimplify_and_add (tem, pre_p);
7966 g = gimple_build_assign (cnts[i], size_binop (PLUS_EXPR, cnts[i],
7967 size_int (1)));
7968 gimple_seq_add_stmt (pre_p, g);
7971 if (last_bind)
7972 gimplify_and_add (last_bind, pre_p);
7973 tree cond = boolean_false_node;
7974 if (is_old)
7976 if (!unused[0])
7977 cond = build2_loc (first_loc, NE_EXPR, boolean_type_node, cnts[0],
7978 size_binop_loc (first_loc, PLUS_EXPR, counts[0],
7979 size_int (2)));
7980 if (!unused[2])
7981 cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
7982 build2_loc (first_loc, NE_EXPR, boolean_type_node,
7983 cnts[2],
7984 size_binop_loc (first_loc, PLUS_EXPR,
7985 totalpx,
7986 size_int (1))));
7988 else
7990 tree prev = size_int (5);
7991 for (i = 0; i < 4; i++)
7993 if (unused[i])
7994 continue;
7995 prev = size_binop_loc (first_loc, PLUS_EXPR, counts[i], prev);
7996 cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
7997 build2_loc (first_loc, NE_EXPR, boolean_type_node,
7998 cnts[i], unshare_expr (prev)));
8001 tem = build3_loc (first_loc, COND_EXPR, void_type_node, cond,
8002 build_call_expr_loc (first_loc,
8003 builtin_decl_explicit (BUILT_IN_TRAP),
8004 0), void_node);
8005 gimplify_and_add (tem, pre_p);
8006 c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
8007 OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
8008 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
8009 OMP_CLAUSE_CHAIN (c) = *list_p;
8010 *list_p = c;
8011 return 1;
8014 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
8015 and previous omp contexts. */
8017 static void
8018 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
8019 enum omp_region_type region_type,
8020 enum tree_code code)
8022 struct gimplify_omp_ctx *ctx, *outer_ctx;
8023 tree c;
8024 hash_map<tree, tree> *struct_map_to_clause = NULL;
8025 tree *prev_list_p = NULL;
8026 int handled_depend_iterators = -1;
8027 int nowait = -1;
8029 ctx = new_omp_context (region_type);
8030 outer_ctx = ctx->outer_context;
8031 if (code == OMP_TARGET)
8033 if (!lang_GNU_Fortran ())
8034 ctx->defaultmap[GDMK_POINTER] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
8035 ctx->defaultmap[GDMK_SCALAR] = GOVD_FIRSTPRIVATE;
8037 if (!lang_GNU_Fortran ())
8038 switch (code)
8040 case OMP_TARGET:
8041 case OMP_TARGET_DATA:
8042 case OMP_TARGET_ENTER_DATA:
8043 case OMP_TARGET_EXIT_DATA:
8044 case OACC_DECLARE:
8045 case OACC_HOST_DATA:
8046 case OACC_PARALLEL:
8047 case OACC_KERNELS:
8048 ctx->target_firstprivatize_array_bases = true;
8049 default:
8050 break;
8053 while ((c = *list_p) != NULL)
8055 bool remove = false;
8056 bool notice_outer = true;
8057 const char *check_non_private = NULL;
8058 unsigned int flags;
8059 tree decl;
8061 switch (OMP_CLAUSE_CODE (c))
8063 case OMP_CLAUSE_PRIVATE:
8064 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
8065 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
8067 flags |= GOVD_PRIVATE_OUTER_REF;
8068 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
8070 else
8071 notice_outer = false;
8072 goto do_add;
8073 case OMP_CLAUSE_SHARED:
8074 flags = GOVD_SHARED | GOVD_EXPLICIT;
8075 goto do_add;
8076 case OMP_CLAUSE_FIRSTPRIVATE:
8077 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8078 check_non_private = "firstprivate";
8079 goto do_add;
8080 case OMP_CLAUSE_LASTPRIVATE:
8081 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
8082 switch (code)
8084 case OMP_DISTRIBUTE:
8085 error_at (OMP_CLAUSE_LOCATION (c),
8086 "conditional %<lastprivate%> clause on "
8087 "%<distribute%> construct");
8088 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
8089 break;
8090 case OMP_TASKLOOP:
8091 error_at (OMP_CLAUSE_LOCATION (c),
8092 "conditional %<lastprivate%> clause on "
8093 "%<taskloop%> construct");
8094 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
8095 break;
8096 default:
8097 break;
8099 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
8100 check_non_private = "lastprivate";
8101 decl = OMP_CLAUSE_DECL (c);
8102 if (error_operand_p (decl))
8103 goto do_add;
8104 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
8105 && !lang_hooks.decls.omp_scalar_p (decl))
8107 error_at (OMP_CLAUSE_LOCATION (c),
8108 "non-scalar variable %qD in conditional "
8109 "%<lastprivate%> clause", decl);
8110 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
8112 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
8113 sorry_at (OMP_CLAUSE_LOCATION (c),
8114 "%<conditional%> modifier on %<lastprivate%> clause "
8115 "not supported yet");
8116 if (outer_ctx
8117 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
8118 || ((outer_ctx->region_type & ORT_COMBINED_TEAMS)
8119 == ORT_COMBINED_TEAMS))
8120 && splay_tree_lookup (outer_ctx->variables,
8121 (splay_tree_key) decl) == NULL)
8123 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
8124 if (outer_ctx->outer_context)
8125 omp_notice_variable (outer_ctx->outer_context, decl, true);
8127 else if (outer_ctx
8128 && (outer_ctx->region_type & ORT_TASK) != 0
8129 && outer_ctx->combined_loop
8130 && splay_tree_lookup (outer_ctx->variables,
8131 (splay_tree_key) decl) == NULL)
8133 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
8134 if (outer_ctx->outer_context)
8135 omp_notice_variable (outer_ctx->outer_context, decl, true);
8137 else if (outer_ctx
8138 && (outer_ctx->region_type == ORT_WORKSHARE
8139 || outer_ctx->region_type == ORT_ACC)
8140 && outer_ctx->combined_loop
8141 && splay_tree_lookup (outer_ctx->variables,
8142 (splay_tree_key) decl) == NULL
8143 && !omp_check_private (outer_ctx, decl, false))
8145 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
8146 if (outer_ctx->outer_context
8147 && (outer_ctx->outer_context->region_type
8148 == ORT_COMBINED_PARALLEL)
8149 && splay_tree_lookup (outer_ctx->outer_context->variables,
8150 (splay_tree_key) decl) == NULL)
8152 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
8153 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
8154 if (octx->outer_context)
8156 octx = octx->outer_context;
8157 if (octx->region_type == ORT_WORKSHARE
8158 && octx->combined_loop
8159 && splay_tree_lookup (octx->variables,
8160 (splay_tree_key) decl) == NULL
8161 && !omp_check_private (octx, decl, false))
8163 omp_add_variable (octx, decl,
8164 GOVD_LASTPRIVATE | GOVD_SEEN);
8165 octx = octx->outer_context;
8166 if (octx
8167 && ((octx->region_type & ORT_COMBINED_TEAMS)
8168 == ORT_COMBINED_TEAMS)
8169 && (splay_tree_lookup (octx->variables,
8170 (splay_tree_key) decl)
8171 == NULL))
8173 omp_add_variable (octx, decl,
8174 GOVD_SHARED | GOVD_SEEN);
8175 octx = octx->outer_context;
8178 if (octx)
8179 omp_notice_variable (octx, decl, true);
8182 else if (outer_ctx->outer_context)
8183 omp_notice_variable (outer_ctx->outer_context, decl, true);
8185 goto do_add;
8186 case OMP_CLAUSE_REDUCTION:
8187 if (OMP_CLAUSE_REDUCTION_TASK (c))
8189 if (region_type == ORT_WORKSHARE)
8191 if (nowait == -1)
8192 nowait = omp_find_clause (*list_p,
8193 OMP_CLAUSE_NOWAIT) != NULL_TREE;
8194 if (nowait
8195 && (outer_ctx == NULL
8196 || outer_ctx->region_type != ORT_COMBINED_PARALLEL))
8198 error_at (OMP_CLAUSE_LOCATION (c),
8199 "%<task%> reduction modifier on a construct "
8200 "with a %<nowait%> clause");
8201 OMP_CLAUSE_REDUCTION_TASK (c) = 0;
8204 else if ((region_type & ORT_PARALLEL) != ORT_PARALLEL)
8206 error_at (OMP_CLAUSE_LOCATION (c),
8207 "invalid %<task%> reduction modifier on construct "
8208 "other than %<parallel%>, %<for%> or %<sections%>");
8209 OMP_CLAUSE_REDUCTION_TASK (c) = 0;
8212 /* FALLTHRU */
8213 case OMP_CLAUSE_IN_REDUCTION:
8214 case OMP_CLAUSE_TASK_REDUCTION:
8215 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
8216 /* OpenACC permits reductions on private variables. */
8217 if (!(region_type & ORT_ACC)
8218 /* taskgroup is actually not a worksharing region. */
8219 && code != OMP_TASKGROUP)
8220 check_non_private = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
8221 decl = OMP_CLAUSE_DECL (c);
8222 if (TREE_CODE (decl) == MEM_REF)
8224 tree type = TREE_TYPE (decl);
8225 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
8226 NULL, is_gimple_val, fb_rvalue, false)
8227 == GS_ERROR)
8229 remove = true;
8230 break;
8232 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8233 if (DECL_P (v))
8235 omp_firstprivatize_variable (ctx, v);
8236 omp_notice_variable (ctx, v, true);
8238 decl = TREE_OPERAND (decl, 0);
8239 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
8241 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
8242 NULL, is_gimple_val, fb_rvalue, false)
8243 == GS_ERROR)
8245 remove = true;
8246 break;
8248 v = TREE_OPERAND (decl, 1);
8249 if (DECL_P (v))
8251 omp_firstprivatize_variable (ctx, v);
8252 omp_notice_variable (ctx, v, true);
8254 decl = TREE_OPERAND (decl, 0);
8256 if (TREE_CODE (decl) == ADDR_EXPR
8257 || TREE_CODE (decl) == INDIRECT_REF)
8258 decl = TREE_OPERAND (decl, 0);
8260 goto do_add_decl;
8261 case OMP_CLAUSE_LINEAR:
8262 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
8263 is_gimple_val, fb_rvalue) == GS_ERROR)
8265 remove = true;
8266 break;
8268 else
8270 if (code == OMP_SIMD
8271 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
8273 struct gimplify_omp_ctx *octx = outer_ctx;
8274 if (octx
8275 && octx->region_type == ORT_WORKSHARE
8276 && octx->combined_loop
8277 && !octx->distribute)
8279 if (octx->outer_context
8280 && (octx->outer_context->region_type
8281 == ORT_COMBINED_PARALLEL))
8282 octx = octx->outer_context->outer_context;
8283 else
8284 octx = octx->outer_context;
8286 if (octx
8287 && octx->region_type == ORT_WORKSHARE
8288 && octx->combined_loop
8289 && octx->distribute)
8291 error_at (OMP_CLAUSE_LOCATION (c),
8292 "%<linear%> clause for variable other than "
8293 "loop iterator specified on construct "
8294 "combined with %<distribute%>");
8295 remove = true;
8296 break;
8299 /* For combined #pragma omp parallel for simd, need to put
8300 lastprivate and perhaps firstprivate too on the
8301 parallel. Similarly for #pragma omp for simd. */
8302 struct gimplify_omp_ctx *octx = outer_ctx;
8303 decl = NULL_TREE;
8306 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
8307 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8308 break;
8309 decl = OMP_CLAUSE_DECL (c);
8310 if (error_operand_p (decl))
8312 decl = NULL_TREE;
8313 break;
8315 flags = GOVD_SEEN;
8316 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
8317 flags |= GOVD_FIRSTPRIVATE;
8318 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8319 flags |= GOVD_LASTPRIVATE;
8320 if (octx
8321 && octx->region_type == ORT_WORKSHARE
8322 && octx->combined_loop)
8324 if (octx->outer_context
8325 && (octx->outer_context->region_type
8326 == ORT_COMBINED_PARALLEL))
8327 octx = octx->outer_context;
8328 else if (omp_check_private (octx, decl, false))
8329 break;
8331 else if (octx
8332 && (octx->region_type & ORT_TASK) != 0
8333 && octx->combined_loop)
8335 else if (octx
8336 && octx->region_type == ORT_COMBINED_PARALLEL
8337 && ctx->region_type == ORT_WORKSHARE
8338 && octx == outer_ctx)
8339 flags = GOVD_SEEN | GOVD_SHARED;
8340 else if (octx
8341 && ((octx->region_type & ORT_COMBINED_TEAMS)
8342 == ORT_COMBINED_TEAMS))
8343 flags = GOVD_SEEN | GOVD_SHARED;
8344 else if (octx
8345 && octx->region_type == ORT_COMBINED_TARGET)
8347 flags &= ~GOVD_LASTPRIVATE;
8348 if (flags == GOVD_SEEN)
8349 break;
8351 else
8352 break;
8353 splay_tree_node on
8354 = splay_tree_lookup (octx->variables,
8355 (splay_tree_key) decl);
8356 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
8358 octx = NULL;
8359 break;
8361 omp_add_variable (octx, decl, flags);
8362 if (octx->outer_context == NULL)
8363 break;
8364 octx = octx->outer_context;
8366 while (1);
8367 if (octx
8368 && decl
8369 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
8370 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
8371 omp_notice_variable (octx, decl, true);
8373 flags = GOVD_LINEAR | GOVD_EXPLICIT;
8374 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
8375 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8377 notice_outer = false;
8378 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
8380 goto do_add;
8382 case OMP_CLAUSE_MAP:
8383 decl = OMP_CLAUSE_DECL (c);
8384 if (error_operand_p (decl))
8385 remove = true;
8386 switch (code)
8388 case OMP_TARGET:
8389 break;
8390 case OACC_DATA:
8391 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
8392 break;
8393 /* FALLTHRU */
8394 case OMP_TARGET_DATA:
8395 case OMP_TARGET_ENTER_DATA:
8396 case OMP_TARGET_EXIT_DATA:
8397 case OACC_ENTER_DATA:
8398 case OACC_EXIT_DATA:
8399 case OACC_HOST_DATA:
8400 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
8401 || (OMP_CLAUSE_MAP_KIND (c)
8402 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8403 /* For target {,enter ,exit }data only the array slice is
8404 mapped, but not the pointer to it. */
8405 remove = true;
8406 break;
8407 default:
8408 break;
8410 if (remove)
8411 break;
8412 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
8414 struct gimplify_omp_ctx *octx;
8415 for (octx = outer_ctx; octx; octx = octx->outer_context)
8417 if (octx->region_type != ORT_ACC_HOST_DATA)
8418 break;
8419 splay_tree_node n2
8420 = splay_tree_lookup (octx->variables,
8421 (splay_tree_key) decl);
8422 if (n2)
8423 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
8424 "declared in enclosing %<host_data%> region",
8425 DECL_NAME (decl));
8428 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8429 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8430 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8431 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8432 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8434 remove = true;
8435 break;
8437 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
8438 || (OMP_CLAUSE_MAP_KIND (c)
8439 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8440 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
8442 OMP_CLAUSE_SIZE (c)
8443 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
8444 false);
8445 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
8446 GOVD_FIRSTPRIVATE | GOVD_SEEN);
8448 if (!DECL_P (decl))
8450 tree d = decl, *pd;
8451 if (TREE_CODE (d) == ARRAY_REF)
8453 while (TREE_CODE (d) == ARRAY_REF)
8454 d = TREE_OPERAND (d, 0);
8455 if (TREE_CODE (d) == COMPONENT_REF
8456 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
8457 decl = d;
8459 pd = &OMP_CLAUSE_DECL (c);
8460 if (d == decl
8461 && TREE_CODE (decl) == INDIRECT_REF
8462 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
8463 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
8464 == REFERENCE_TYPE))
8466 pd = &TREE_OPERAND (decl, 0);
8467 decl = TREE_OPERAND (decl, 0);
8469 if (TREE_CODE (decl) == COMPONENT_REF)
8471 while (TREE_CODE (decl) == COMPONENT_REF)
8472 decl = TREE_OPERAND (decl, 0);
8473 if (TREE_CODE (decl) == INDIRECT_REF
8474 && DECL_P (TREE_OPERAND (decl, 0))
8475 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
8476 == REFERENCE_TYPE))
8477 decl = TREE_OPERAND (decl, 0);
8479 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
8480 == GS_ERROR)
8482 remove = true;
8483 break;
8485 if (DECL_P (decl))
8487 if (error_operand_p (decl))
8489 remove = true;
8490 break;
8493 tree stype = TREE_TYPE (decl);
8494 if (TREE_CODE (stype) == REFERENCE_TYPE)
8495 stype = TREE_TYPE (stype);
8496 if (TYPE_SIZE_UNIT (stype) == NULL
8497 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
8499 error_at (OMP_CLAUSE_LOCATION (c),
8500 "mapping field %qE of variable length "
8501 "structure", OMP_CLAUSE_DECL (c));
8502 remove = true;
8503 break;
8506 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
8508 /* Error recovery. */
8509 if (prev_list_p == NULL)
8511 remove = true;
8512 break;
8514 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8516 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
8517 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
8519 remove = true;
8520 break;
8525 tree offset;
8526 poly_int64 bitsize, bitpos;
8527 machine_mode mode;
8528 int unsignedp, reversep, volatilep = 0;
8529 tree base = OMP_CLAUSE_DECL (c);
8530 while (TREE_CODE (base) == ARRAY_REF)
8531 base = TREE_OPERAND (base, 0);
8532 if (TREE_CODE (base) == INDIRECT_REF)
8533 base = TREE_OPERAND (base, 0);
8534 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8535 &mode, &unsignedp, &reversep,
8536 &volatilep);
8537 tree orig_base = base;
8538 if ((TREE_CODE (base) == INDIRECT_REF
8539 || (TREE_CODE (base) == MEM_REF
8540 && integer_zerop (TREE_OPERAND (base, 1))))
8541 && DECL_P (TREE_OPERAND (base, 0))
8542 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
8543 == REFERENCE_TYPE))
8544 base = TREE_OPERAND (base, 0);
8545 gcc_assert (base == decl
8546 && (offset == NULL_TREE
8547 || poly_int_tree_p (offset)));
8549 splay_tree_node n
8550 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8551 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
8552 == GOMP_MAP_ALWAYS_POINTER);
8553 if (n == NULL || (n->value & GOVD_MAP) == 0)
8555 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8556 OMP_CLAUSE_MAP);
8557 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
8558 if (orig_base != base)
8559 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
8560 else
8561 OMP_CLAUSE_DECL (l) = decl;
8562 OMP_CLAUSE_SIZE (l) = size_int (1);
8563 if (struct_map_to_clause == NULL)
8564 struct_map_to_clause = new hash_map<tree, tree>;
8565 struct_map_to_clause->put (decl, l);
8566 if (ptr)
8568 enum gomp_map_kind mkind
8569 = code == OMP_TARGET_EXIT_DATA
8570 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8571 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8572 OMP_CLAUSE_MAP);
8573 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8574 OMP_CLAUSE_DECL (c2)
8575 = unshare_expr (OMP_CLAUSE_DECL (c));
8576 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
8577 OMP_CLAUSE_SIZE (c2)
8578 = TYPE_SIZE_UNIT (ptr_type_node);
8579 OMP_CLAUSE_CHAIN (l) = c2;
8580 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8582 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8583 tree c3
8584 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8585 OMP_CLAUSE_MAP);
8586 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8587 OMP_CLAUSE_DECL (c3)
8588 = unshare_expr (OMP_CLAUSE_DECL (c4));
8589 OMP_CLAUSE_SIZE (c3)
8590 = TYPE_SIZE_UNIT (ptr_type_node);
8591 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8592 OMP_CLAUSE_CHAIN (c2) = c3;
8594 *prev_list_p = l;
8595 prev_list_p = NULL;
8597 else
8599 OMP_CLAUSE_CHAIN (l) = c;
8600 *list_p = l;
8601 list_p = &OMP_CLAUSE_CHAIN (l);
8603 if (orig_base != base && code == OMP_TARGET)
8605 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8606 OMP_CLAUSE_MAP);
8607 enum gomp_map_kind mkind
8608 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8609 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8610 OMP_CLAUSE_DECL (c2) = decl;
8611 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8612 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8613 OMP_CLAUSE_CHAIN (l) = c2;
8615 flags = GOVD_MAP | GOVD_EXPLICIT;
8616 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8617 flags |= GOVD_SEEN;
8618 goto do_add_decl;
8620 else
8622 tree *osc = struct_map_to_clause->get (decl);
8623 tree *sc = NULL, *scp = NULL;
8624 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8625 n->value |= GOVD_SEEN;
8626 poly_offset_int o1, o2;
8627 if (offset)
8628 o1 = wi::to_poly_offset (offset);
8629 else
8630 o1 = 0;
8631 if (maybe_ne (bitpos, 0))
8632 o1 += bits_to_bytes_round_down (bitpos);
8633 sc = &OMP_CLAUSE_CHAIN (*osc);
8634 if (*sc != c
8635 && (OMP_CLAUSE_MAP_KIND (*sc)
8636 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8637 sc = &OMP_CLAUSE_CHAIN (*sc);
8638 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8639 if (ptr && sc == prev_list_p)
8640 break;
8641 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8642 != COMPONENT_REF
8643 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8644 != INDIRECT_REF)
8645 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8646 != ARRAY_REF))
8647 break;
8648 else
8650 tree offset2;
8651 poly_int64 bitsize2, bitpos2;
8652 base = OMP_CLAUSE_DECL (*sc);
8653 if (TREE_CODE (base) == ARRAY_REF)
8655 while (TREE_CODE (base) == ARRAY_REF)
8656 base = TREE_OPERAND (base, 0);
8657 if (TREE_CODE (base) != COMPONENT_REF
8658 || (TREE_CODE (TREE_TYPE (base))
8659 != ARRAY_TYPE))
8660 break;
8662 else if (TREE_CODE (base) == INDIRECT_REF
8663 && (TREE_CODE (TREE_OPERAND (base, 0))
8664 == COMPONENT_REF)
8665 && (TREE_CODE (TREE_TYPE
8666 (TREE_OPERAND (base, 0)))
8667 == REFERENCE_TYPE))
8668 base = TREE_OPERAND (base, 0);
8669 base = get_inner_reference (base, &bitsize2,
8670 &bitpos2, &offset2,
8671 &mode, &unsignedp,
8672 &reversep, &volatilep);
8673 if ((TREE_CODE (base) == INDIRECT_REF
8674 || (TREE_CODE (base) == MEM_REF
8675 && integer_zerop (TREE_OPERAND (base,
8676 1))))
8677 && DECL_P (TREE_OPERAND (base, 0))
8678 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8679 0)))
8680 == REFERENCE_TYPE))
8681 base = TREE_OPERAND (base, 0);
8682 if (base != decl)
8683 break;
8684 if (scp)
8685 continue;
8686 gcc_assert (offset == NULL_TREE
8687 || poly_int_tree_p (offset));
8688 tree d1 = OMP_CLAUSE_DECL (*sc);
8689 tree d2 = OMP_CLAUSE_DECL (c);
8690 while (TREE_CODE (d1) == ARRAY_REF)
8691 d1 = TREE_OPERAND (d1, 0);
8692 while (TREE_CODE (d2) == ARRAY_REF)
8693 d2 = TREE_OPERAND (d2, 0);
8694 if (TREE_CODE (d1) == INDIRECT_REF)
8695 d1 = TREE_OPERAND (d1, 0);
8696 if (TREE_CODE (d2) == INDIRECT_REF)
8697 d2 = TREE_OPERAND (d2, 0);
8698 while (TREE_CODE (d1) == COMPONENT_REF)
8699 if (TREE_CODE (d2) == COMPONENT_REF
8700 && TREE_OPERAND (d1, 1)
8701 == TREE_OPERAND (d2, 1))
8703 d1 = TREE_OPERAND (d1, 0);
8704 d2 = TREE_OPERAND (d2, 0);
8706 else
8707 break;
8708 if (d1 == d2)
8710 error_at (OMP_CLAUSE_LOCATION (c),
8711 "%qE appears more than once in map "
8712 "clauses", OMP_CLAUSE_DECL (c));
8713 remove = true;
8714 break;
8716 if (offset2)
8717 o2 = wi::to_poly_offset (offset2);
8718 else
8719 o2 = 0;
8720 o2 += bits_to_bytes_round_down (bitpos2);
8721 if (maybe_lt (o1, o2)
8722 || (known_eq (o1, o2)
8723 && maybe_lt (bitpos, bitpos2)))
8725 if (ptr)
8726 scp = sc;
8727 else
8728 break;
8731 if (remove)
8732 break;
8733 OMP_CLAUSE_SIZE (*osc)
8734 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8735 size_one_node);
8736 if (ptr)
8738 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8739 OMP_CLAUSE_MAP);
8740 tree cl = NULL_TREE;
8741 enum gomp_map_kind mkind
8742 = code == OMP_TARGET_EXIT_DATA
8743 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8744 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8745 OMP_CLAUSE_DECL (c2)
8746 = unshare_expr (OMP_CLAUSE_DECL (c));
8747 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8748 OMP_CLAUSE_SIZE (c2)
8749 = TYPE_SIZE_UNIT (ptr_type_node);
8750 cl = scp ? *prev_list_p : c2;
8751 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8753 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8754 tree c3
8755 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8756 OMP_CLAUSE_MAP);
8757 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8758 OMP_CLAUSE_DECL (c3)
8759 = unshare_expr (OMP_CLAUSE_DECL (c4));
8760 OMP_CLAUSE_SIZE (c3)
8761 = TYPE_SIZE_UNIT (ptr_type_node);
8762 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8763 if (!scp)
8764 OMP_CLAUSE_CHAIN (c2) = c3;
8765 else
8766 cl = c3;
8768 if (scp)
8769 *scp = c2;
8770 if (sc == prev_list_p)
8772 *sc = cl;
8773 prev_list_p = NULL;
8775 else
8777 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8778 list_p = prev_list_p;
8779 prev_list_p = NULL;
8780 OMP_CLAUSE_CHAIN (c) = *sc;
8781 *sc = cl;
8782 continue;
8785 else if (*sc != c)
8787 *list_p = OMP_CLAUSE_CHAIN (c);
8788 OMP_CLAUSE_CHAIN (c) = *sc;
8789 *sc = c;
8790 continue;
8794 if (!remove
8795 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8796 && OMP_CLAUSE_CHAIN (c)
8797 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8798 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8799 == GOMP_MAP_ALWAYS_POINTER))
8800 prev_list_p = list_p;
8801 break;
8803 flags = GOVD_MAP | GOVD_EXPLICIT;
8804 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8805 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8806 flags |= GOVD_MAP_ALWAYS_TO;
8807 goto do_add;
8809 case OMP_CLAUSE_DEPEND:
8810 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8812 tree deps = OMP_CLAUSE_DECL (c);
8813 while (deps && TREE_CODE (deps) == TREE_LIST)
8815 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8816 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8817 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8818 pre_p, NULL, is_gimple_val, fb_rvalue);
8819 deps = TREE_CHAIN (deps);
8821 break;
8823 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8824 break;
8825 if (handled_depend_iterators == -1)
8826 handled_depend_iterators = gimplify_omp_depend (list_p, pre_p);
8827 if (handled_depend_iterators)
8829 if (handled_depend_iterators == 2)
8830 remove = true;
8831 break;
8833 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8835 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8836 NULL, is_gimple_val, fb_rvalue);
8837 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8839 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8841 remove = true;
8842 break;
8844 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8845 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8846 is_gimple_val, fb_rvalue) == GS_ERROR)
8848 remove = true;
8849 break;
8851 break;
8853 case OMP_CLAUSE_TO:
8854 case OMP_CLAUSE_FROM:
8855 case OMP_CLAUSE__CACHE_:
8856 decl = OMP_CLAUSE_DECL (c);
8857 if (error_operand_p (decl))
8859 remove = true;
8860 break;
8862 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8863 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8864 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8865 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8866 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8868 remove = true;
8869 break;
8871 if (!DECL_P (decl))
8873 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8874 NULL, is_gimple_lvalue, fb_lvalue)
8875 == GS_ERROR)
8877 remove = true;
8878 break;
8880 break;
8882 goto do_notice;
8884 case OMP_CLAUSE_USE_DEVICE_PTR:
8885 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8886 goto do_add;
8887 case OMP_CLAUSE_IS_DEVICE_PTR:
8888 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8889 goto do_add;
8891 do_add:
8892 decl = OMP_CLAUSE_DECL (c);
8893 do_add_decl:
8894 if (error_operand_p (decl))
8896 remove = true;
8897 break;
8899 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8901 tree t = omp_member_access_dummy_var (decl);
8902 if (t)
8904 tree v = DECL_VALUE_EXPR (decl);
8905 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8906 if (outer_ctx)
8907 omp_notice_variable (outer_ctx, t, true);
8910 if (code == OACC_DATA
8911 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8912 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8913 flags |= GOVD_MAP_0LEN_ARRAY;
8914 omp_add_variable (ctx, decl, flags);
8915 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8916 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
8917 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
8918 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8920 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8921 GOVD_LOCAL | GOVD_SEEN);
8922 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8923 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8924 find_decl_expr,
8925 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8926 NULL) == NULL_TREE)
8927 omp_add_variable (ctx,
8928 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8929 GOVD_LOCAL | GOVD_SEEN);
8930 gimplify_omp_ctxp = ctx;
8931 push_gimplify_context ();
8933 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8934 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8936 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8937 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8938 pop_gimplify_context
8939 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8940 push_gimplify_context ();
8941 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8942 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8943 pop_gimplify_context
8944 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8945 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8946 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8948 gimplify_omp_ctxp = outer_ctx;
8950 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8951 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8953 gimplify_omp_ctxp = ctx;
8954 push_gimplify_context ();
8955 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8957 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8958 NULL, NULL);
8959 TREE_SIDE_EFFECTS (bind) = 1;
8960 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8961 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8963 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8964 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8965 pop_gimplify_context
8966 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8967 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8969 gimplify_omp_ctxp = outer_ctx;
8971 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8972 && OMP_CLAUSE_LINEAR_STMT (c))
8974 gimplify_omp_ctxp = ctx;
8975 push_gimplify_context ();
8976 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8978 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8979 NULL, NULL);
8980 TREE_SIDE_EFFECTS (bind) = 1;
8981 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8982 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8984 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8985 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8986 pop_gimplify_context
8987 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8988 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8990 gimplify_omp_ctxp = outer_ctx;
8992 if (notice_outer)
8993 goto do_notice;
8994 break;
8996 case OMP_CLAUSE_COPYIN:
8997 case OMP_CLAUSE_COPYPRIVATE:
8998 decl = OMP_CLAUSE_DECL (c);
8999 if (error_operand_p (decl))
9001 remove = true;
9002 break;
9004 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
9005 && !remove
9006 && !omp_check_private (ctx, decl, true))
9008 remove = true;
9009 if (is_global_var (decl))
9011 if (DECL_THREAD_LOCAL_P (decl))
9012 remove = false;
9013 else if (DECL_HAS_VALUE_EXPR_P (decl))
9015 tree value = get_base_address (DECL_VALUE_EXPR (decl));
9017 if (value
9018 && DECL_P (value)
9019 && DECL_THREAD_LOCAL_P (value))
9020 remove = false;
9023 if (remove)
9024 error_at (OMP_CLAUSE_LOCATION (c),
9025 "copyprivate variable %qE is not threadprivate"
9026 " or private in outer context", DECL_NAME (decl));
9028 do_notice:
9029 if ((region_type & ORT_TASKLOOP) == ORT_TASKLOOP
9030 && outer_ctx
9031 && outer_ctx->region_type == ORT_COMBINED_PARALLEL
9032 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9033 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9034 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE))
9036 splay_tree_node on
9037 = splay_tree_lookup (outer_ctx->variables,
9038 (splay_tree_key)decl);
9039 if (on == NULL || (on->value & GOVD_DATA_SHARE_CLASS) == 0)
9041 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9042 && TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
9043 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
9044 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9045 && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
9046 == POINTER_TYPE))))
9047 omp_firstprivatize_variable (outer_ctx, decl);
9048 else
9049 omp_add_variable (outer_ctx, decl,
9050 GOVD_SEEN | GOVD_SHARED);
9051 omp_notice_variable (outer_ctx, decl, true);
9054 if (outer_ctx)
9055 omp_notice_variable (outer_ctx, decl, true);
9056 if (check_non_private
9057 && region_type == ORT_WORKSHARE
9058 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
9059 || decl == OMP_CLAUSE_DECL (c)
9060 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
9061 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
9062 == ADDR_EXPR
9063 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
9064 == POINTER_PLUS_EXPR
9065 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
9066 (OMP_CLAUSE_DECL (c), 0), 0))
9067 == ADDR_EXPR)))))
9068 && omp_check_private (ctx, decl, false))
9070 error ("%s variable %qE is private in outer context",
9071 check_non_private, DECL_NAME (decl));
9072 remove = true;
9074 break;
9076 case OMP_CLAUSE_IF:
9077 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
9078 && OMP_CLAUSE_IF_MODIFIER (c) != code)
9080 const char *p[2];
9081 for (int i = 0; i < 2; i++)
9082 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
9084 case VOID_CST: p[i] = "cancel"; break;
9085 case OMP_PARALLEL: p[i] = "parallel"; break;
9086 case OMP_SIMD: p[i] = "simd"; break;
9087 case OMP_TASK: p[i] = "task"; break;
9088 case OMP_TASKLOOP: p[i] = "taskloop"; break;
9089 case OMP_TARGET_DATA: p[i] = "target data"; break;
9090 case OMP_TARGET: p[i] = "target"; break;
9091 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
9092 case OMP_TARGET_ENTER_DATA:
9093 p[i] = "target enter data"; break;
9094 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
9095 default: gcc_unreachable ();
9097 error_at (OMP_CLAUSE_LOCATION (c),
9098 "expected %qs %<if%> clause modifier rather than %qs",
9099 p[0], p[1]);
9100 remove = true;
9102 /* Fall through. */
9104 case OMP_CLAUSE_FINAL:
9105 OMP_CLAUSE_OPERAND (c, 0)
9106 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
9107 /* Fall through. */
9109 case OMP_CLAUSE_SCHEDULE:
9110 case OMP_CLAUSE_NUM_THREADS:
9111 case OMP_CLAUSE_NUM_TEAMS:
9112 case OMP_CLAUSE_THREAD_LIMIT:
9113 case OMP_CLAUSE_DIST_SCHEDULE:
9114 case OMP_CLAUSE_DEVICE:
9115 case OMP_CLAUSE_PRIORITY:
9116 case OMP_CLAUSE_GRAINSIZE:
9117 case OMP_CLAUSE_NUM_TASKS:
9118 case OMP_CLAUSE_HINT:
9119 case OMP_CLAUSE_ASYNC:
9120 case OMP_CLAUSE_WAIT:
9121 case OMP_CLAUSE_NUM_GANGS:
9122 case OMP_CLAUSE_NUM_WORKERS:
9123 case OMP_CLAUSE_VECTOR_LENGTH:
9124 case OMP_CLAUSE_WORKER:
9125 case OMP_CLAUSE_VECTOR:
9126 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
9127 is_gimple_val, fb_rvalue) == GS_ERROR)
9128 remove = true;
9129 break;
9131 case OMP_CLAUSE_GANG:
9132 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
9133 is_gimple_val, fb_rvalue) == GS_ERROR)
9134 remove = true;
9135 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
9136 is_gimple_val, fb_rvalue) == GS_ERROR)
9137 remove = true;
9138 break;
9140 case OMP_CLAUSE_NOWAIT:
9141 nowait = 1;
9142 break;
9144 case OMP_CLAUSE_ORDERED:
9145 case OMP_CLAUSE_UNTIED:
9146 case OMP_CLAUSE_COLLAPSE:
9147 case OMP_CLAUSE_TILE:
9148 case OMP_CLAUSE_AUTO:
9149 case OMP_CLAUSE_SEQ:
9150 case OMP_CLAUSE_INDEPENDENT:
9151 case OMP_CLAUSE_MERGEABLE:
9152 case OMP_CLAUSE_PROC_BIND:
9153 case OMP_CLAUSE_SAFELEN:
9154 case OMP_CLAUSE_SIMDLEN:
9155 case OMP_CLAUSE_NOGROUP:
9156 case OMP_CLAUSE_THREADS:
9157 case OMP_CLAUSE_SIMD:
9158 case OMP_CLAUSE_IF_PRESENT:
9159 case OMP_CLAUSE_FINALIZE:
9160 break;
9162 case OMP_CLAUSE_DEFAULTMAP:
9163 enum gimplify_defaultmap_kind gdmkmin, gdmkmax;
9164 switch (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c))
9166 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
9167 gdmkmin = GDMK_SCALAR;
9168 gdmkmax = GDMK_POINTER;
9169 break;
9170 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
9171 gdmkmin = gdmkmax = GDMK_SCALAR;
9172 break;
9173 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
9174 gdmkmin = gdmkmax = GDMK_AGGREGATE;
9175 break;
9176 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE:
9177 gdmkmin = gdmkmax = GDMK_ALLOCATABLE;
9178 break;
9179 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
9180 gdmkmin = gdmkmax = GDMK_POINTER;
9181 break;
9182 default:
9183 gcc_unreachable ();
9185 for (int gdmk = gdmkmin; gdmk <= gdmkmax; gdmk++)
9186 switch (OMP_CLAUSE_DEFAULTMAP_BEHAVIOR (c))
9188 case OMP_CLAUSE_DEFAULTMAP_ALLOC:
9189 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_ALLOC_ONLY;
9190 break;
9191 case OMP_CLAUSE_DEFAULTMAP_TO:
9192 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_TO_ONLY;
9193 break;
9194 case OMP_CLAUSE_DEFAULTMAP_FROM:
9195 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FROM_ONLY;
9196 break;
9197 case OMP_CLAUSE_DEFAULTMAP_TOFROM:
9198 ctx->defaultmap[gdmk] = GOVD_MAP;
9199 break;
9200 case OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE:
9201 ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
9202 break;
9203 case OMP_CLAUSE_DEFAULTMAP_NONE:
9204 ctx->defaultmap[gdmk] = 0;
9205 break;
9206 case OMP_CLAUSE_DEFAULTMAP_DEFAULT:
9207 switch (gdmk)
9209 case GDMK_SCALAR:
9210 ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
9211 break;
9212 case GDMK_AGGREGATE:
9213 case GDMK_ALLOCATABLE:
9214 ctx->defaultmap[gdmk] = GOVD_MAP;
9215 break;
9216 case GDMK_POINTER:
9217 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
9218 break;
9219 default:
9220 gcc_unreachable ();
9222 break;
9223 default:
9224 gcc_unreachable ();
9226 break;
9228 case OMP_CLAUSE_ALIGNED:
9229 decl = OMP_CLAUSE_DECL (c);
9230 if (error_operand_p (decl))
9232 remove = true;
9233 break;
9235 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
9236 is_gimple_val, fb_rvalue) == GS_ERROR)
9238 remove = true;
9239 break;
9241 if (!is_global_var (decl)
9242 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9243 omp_add_variable (ctx, decl, GOVD_ALIGNED);
9244 break;
9246 case OMP_CLAUSE_NONTEMPORAL:
9247 decl = OMP_CLAUSE_DECL (c);
9248 if (error_operand_p (decl))
9250 remove = true;
9251 break;
9253 omp_add_variable (ctx, decl, GOVD_NONTEMPORAL);
9254 break;
9256 case OMP_CLAUSE_DEFAULT:
9257 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
9258 break;
9260 default:
9261 gcc_unreachable ();
9264 if (code == OACC_DATA
9265 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9266 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
9267 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9268 remove = true;
9269 if (remove)
9270 *list_p = OMP_CLAUSE_CHAIN (c);
9271 else
9272 list_p = &OMP_CLAUSE_CHAIN (c);
9275 gimplify_omp_ctxp = ctx;
9276 if (struct_map_to_clause)
9277 delete struct_map_to_clause;
9280 /* Return true if DECL is a candidate for shared to firstprivate
9281 optimization. We only consider non-addressable scalars, not
9282 too big, and not references. */
9284 static bool
9285 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
9287 if (TREE_ADDRESSABLE (decl))
9288 return false;
9289 tree type = TREE_TYPE (decl);
9290 if (!is_gimple_reg_type (type)
9291 || TREE_CODE (type) == REFERENCE_TYPE
9292 || TREE_ADDRESSABLE (type))
9293 return false;
9294 /* Don't optimize too large decls, as each thread/task will have
9295 its own. */
9296 HOST_WIDE_INT len = int_size_in_bytes (type);
9297 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
9298 return false;
9299 if (lang_hooks.decls.omp_privatize_by_reference (decl))
9300 return false;
9301 return true;
9304 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
9305 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
9306 GOVD_WRITTEN in outer contexts. */
9308 static void
9309 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
9311 for (; ctx; ctx = ctx->outer_context)
9313 splay_tree_node n = splay_tree_lookup (ctx->variables,
9314 (splay_tree_key) decl);
9315 if (n == NULL)
9316 continue;
9317 else if (n->value & GOVD_SHARED)
9319 n->value |= GOVD_WRITTEN;
9320 return;
9322 else if (n->value & GOVD_DATA_SHARE_CLASS)
9323 return;
9327 /* Helper callback for walk_gimple_seq to discover possible stores
9328 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
9329 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
9330 for those. */
9332 static tree
9333 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
9335 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
9337 *walk_subtrees = 0;
9338 if (!wi->is_lhs)
9339 return NULL_TREE;
9341 tree op = *tp;
9344 if (handled_component_p (op))
9345 op = TREE_OPERAND (op, 0);
9346 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
9347 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
9348 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
9349 else
9350 break;
9352 while (1);
9353 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
9354 return NULL_TREE;
9356 omp_mark_stores (gimplify_omp_ctxp, op);
9357 return NULL_TREE;
9360 /* Helper callback for walk_gimple_seq to discover possible stores
9361 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
9362 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
9363 for those. */
9365 static tree
9366 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
9367 bool *handled_ops_p,
9368 struct walk_stmt_info *wi)
9370 gimple *stmt = gsi_stmt (*gsi_p);
9371 switch (gimple_code (stmt))
9373 /* Don't recurse on OpenMP constructs for which
9374 gimplify_adjust_omp_clauses already handled the bodies,
9375 except handle gimple_omp_for_pre_body. */
9376 case GIMPLE_OMP_FOR:
9377 *handled_ops_p = true;
9378 if (gimple_omp_for_pre_body (stmt))
9379 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
9380 omp_find_stores_stmt, omp_find_stores_op, wi);
9381 break;
9382 case GIMPLE_OMP_PARALLEL:
9383 case GIMPLE_OMP_TASK:
9384 case GIMPLE_OMP_SECTIONS:
9385 case GIMPLE_OMP_SINGLE:
9386 case GIMPLE_OMP_TARGET:
9387 case GIMPLE_OMP_TEAMS:
9388 case GIMPLE_OMP_CRITICAL:
9389 *handled_ops_p = true;
9390 break;
9391 default:
9392 break;
9394 return NULL_TREE;
9397 struct gimplify_adjust_omp_clauses_data
9399 tree *list_p;
9400 gimple_seq *pre_p;
9403 /* For all variables that were not actually used within the context,
9404 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
9406 static int
9407 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
9409 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
9410 gimple_seq *pre_p
9411 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
9412 tree decl = (tree) n->key;
9413 unsigned flags = n->value;
9414 enum omp_clause_code code;
9415 tree clause;
9416 bool private_debug;
9418 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
9419 return 0;
9420 if ((flags & GOVD_SEEN) == 0)
9421 return 0;
9422 if (flags & GOVD_DEBUG_PRIVATE)
9424 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
9425 private_debug = true;
9427 else if (flags & GOVD_MAP)
9428 private_debug = false;
9429 else
9430 private_debug
9431 = lang_hooks.decls.omp_private_debug_clause (decl,
9432 !!(flags & GOVD_SHARED));
9433 if (private_debug)
9434 code = OMP_CLAUSE_PRIVATE;
9435 else if (flags & GOVD_MAP)
9437 code = OMP_CLAUSE_MAP;
9438 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
9439 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
9441 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
9442 return 0;
9445 else if (flags & GOVD_SHARED)
9447 if (is_global_var (decl))
9449 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
9450 while (ctx != NULL)
9452 splay_tree_node on
9453 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9454 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
9455 | GOVD_PRIVATE | GOVD_REDUCTION
9456 | GOVD_LINEAR | GOVD_MAP)) != 0)
9457 break;
9458 ctx = ctx->outer_context;
9460 if (ctx == NULL)
9461 return 0;
9463 code = OMP_CLAUSE_SHARED;
9465 else if (flags & GOVD_PRIVATE)
9466 code = OMP_CLAUSE_PRIVATE;
9467 else if (flags & GOVD_FIRSTPRIVATE)
9469 code = OMP_CLAUSE_FIRSTPRIVATE;
9470 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
9471 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
9472 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
9474 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
9475 "%<target%> construct", decl);
9476 return 0;
9479 else if (flags & GOVD_LASTPRIVATE)
9480 code = OMP_CLAUSE_LASTPRIVATE;
9481 else if (flags & (GOVD_ALIGNED | GOVD_NONTEMPORAL))
9482 return 0;
9483 else
9484 gcc_unreachable ();
9486 if (((flags & GOVD_LASTPRIVATE)
9487 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
9488 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9489 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9491 tree chain = *list_p;
9492 clause = build_omp_clause (input_location, code);
9493 OMP_CLAUSE_DECL (clause) = decl;
9494 OMP_CLAUSE_CHAIN (clause) = chain;
9495 if (private_debug)
9496 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
9497 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
9498 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
9499 else if (code == OMP_CLAUSE_SHARED
9500 && (flags & GOVD_WRITTEN) == 0
9501 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9502 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
9503 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
9504 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
9505 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
9507 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
9508 OMP_CLAUSE_DECL (nc) = decl;
9509 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9510 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
9511 OMP_CLAUSE_DECL (clause)
9512 = build_simple_mem_ref_loc (input_location, decl);
9513 OMP_CLAUSE_DECL (clause)
9514 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
9515 build_int_cst (build_pointer_type (char_type_node), 0));
9516 OMP_CLAUSE_SIZE (clause) = size_zero_node;
9517 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9518 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
9519 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
9520 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
9521 OMP_CLAUSE_CHAIN (nc) = chain;
9522 OMP_CLAUSE_CHAIN (clause) = nc;
9523 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9524 gimplify_omp_ctxp = ctx->outer_context;
9525 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
9526 pre_p, NULL, is_gimple_val, fb_rvalue);
9527 gimplify_omp_ctxp = ctx;
9529 else if (code == OMP_CLAUSE_MAP)
9531 int kind;
9532 /* Not all combinations of these GOVD_MAP flags are actually valid. */
9533 switch (flags & (GOVD_MAP_TO_ONLY
9534 | GOVD_MAP_FORCE
9535 | GOVD_MAP_FORCE_PRESENT
9536 | GOVD_MAP_ALLOC_ONLY
9537 | GOVD_MAP_FROM_ONLY))
9539 case 0:
9540 kind = GOMP_MAP_TOFROM;
9541 break;
9542 case GOVD_MAP_FORCE:
9543 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
9544 break;
9545 case GOVD_MAP_TO_ONLY:
9546 kind = GOMP_MAP_TO;
9547 break;
9548 case GOVD_MAP_FROM_ONLY:
9549 kind = GOMP_MAP_FROM;
9550 break;
9551 case GOVD_MAP_ALLOC_ONLY:
9552 kind = GOMP_MAP_ALLOC;
9553 break;
9554 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
9555 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
9556 break;
9557 case GOVD_MAP_FORCE_PRESENT:
9558 kind = GOMP_MAP_FORCE_PRESENT;
9559 break;
9560 default:
9561 gcc_unreachable ();
9563 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
9564 if (DECL_SIZE (decl)
9565 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9567 tree decl2 = DECL_VALUE_EXPR (decl);
9568 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9569 decl2 = TREE_OPERAND (decl2, 0);
9570 gcc_assert (DECL_P (decl2));
9571 tree mem = build_simple_mem_ref (decl2);
9572 OMP_CLAUSE_DECL (clause) = mem;
9573 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9574 if (gimplify_omp_ctxp->outer_context)
9576 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
9577 omp_notice_variable (ctx, decl2, true);
9578 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
9580 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
9581 OMP_CLAUSE_MAP);
9582 OMP_CLAUSE_DECL (nc) = decl;
9583 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9584 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
9585 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
9586 else
9587 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9588 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
9589 OMP_CLAUSE_CHAIN (clause) = nc;
9591 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
9592 && lang_hooks.decls.omp_privatize_by_reference (decl))
9594 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
9595 OMP_CLAUSE_SIZE (clause)
9596 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
9597 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9598 gimplify_omp_ctxp = ctx->outer_context;
9599 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
9600 pre_p, NULL, is_gimple_val, fb_rvalue);
9601 gimplify_omp_ctxp = ctx;
9602 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
9603 OMP_CLAUSE_MAP);
9604 OMP_CLAUSE_DECL (nc) = decl;
9605 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9606 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
9607 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
9608 OMP_CLAUSE_CHAIN (clause) = nc;
9610 else
9611 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
9613 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
9615 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
9616 OMP_CLAUSE_DECL (nc) = decl;
9617 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
9618 OMP_CLAUSE_CHAIN (nc) = chain;
9619 OMP_CLAUSE_CHAIN (clause) = nc;
9620 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9621 gimplify_omp_ctxp = ctx->outer_context;
9622 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9623 gimplify_omp_ctxp = ctx;
9625 *list_p = clause;
9626 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9627 gimplify_omp_ctxp = ctx->outer_context;
9628 lang_hooks.decls.omp_finish_clause (clause, pre_p);
9629 if (gimplify_omp_ctxp)
9630 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
9631 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
9632 && DECL_P (OMP_CLAUSE_SIZE (clause)))
9633 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
9634 true);
9635 gimplify_omp_ctxp = ctx;
9636 return 0;
9639 static void
9640 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
9641 enum tree_code code)
9643 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9644 tree c, decl;
9646 if (body)
9648 struct gimplify_omp_ctx *octx;
9649 for (octx = ctx; octx; octx = octx->outer_context)
9650 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
9651 break;
9652 if (octx)
9654 struct walk_stmt_info wi;
9655 memset (&wi, 0, sizeof (wi));
9656 walk_gimple_seq (body, omp_find_stores_stmt,
9657 omp_find_stores_op, &wi);
9660 while ((c = *list_p) != NULL)
9662 splay_tree_node n;
9663 bool remove = false;
9665 switch (OMP_CLAUSE_CODE (c))
9667 case OMP_CLAUSE_FIRSTPRIVATE:
9668 if ((ctx->region_type & ORT_TARGET)
9669 && (ctx->region_type & ORT_ACC) == 0
9670 && TYPE_ATOMIC (strip_array_types
9671 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
9673 error_at (OMP_CLAUSE_LOCATION (c),
9674 "%<_Atomic%> %qD in %<firstprivate%> clause on "
9675 "%<target%> construct", OMP_CLAUSE_DECL (c));
9676 remove = true;
9677 break;
9679 /* FALLTHRU */
9680 case OMP_CLAUSE_PRIVATE:
9681 case OMP_CLAUSE_SHARED:
9682 case OMP_CLAUSE_LINEAR:
9683 decl = OMP_CLAUSE_DECL (c);
9684 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9685 remove = !(n->value & GOVD_SEEN);
9686 if (! remove)
9688 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
9689 if ((n->value & GOVD_DEBUG_PRIVATE)
9690 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
9692 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
9693 || ((n->value & GOVD_DATA_SHARE_CLASS)
9694 == GOVD_SHARED));
9695 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
9696 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
9698 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9699 && (n->value & GOVD_WRITTEN) == 0
9700 && DECL_P (decl)
9701 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9702 OMP_CLAUSE_SHARED_READONLY (c) = 1;
9703 else if (DECL_P (decl)
9704 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9705 && (n->value & GOVD_WRITTEN) != 0)
9706 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9707 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
9708 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9709 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9711 break;
9713 case OMP_CLAUSE_LASTPRIVATE:
9714 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9715 accurately reflect the presence of a FIRSTPRIVATE clause. */
9716 decl = OMP_CLAUSE_DECL (c);
9717 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9718 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9719 = (n->value & GOVD_FIRSTPRIVATE) != 0;
9720 if (code == OMP_DISTRIBUTE
9721 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9723 remove = true;
9724 error_at (OMP_CLAUSE_LOCATION (c),
9725 "same variable used in %<firstprivate%> and "
9726 "%<lastprivate%> clauses on %<distribute%> "
9727 "construct");
9729 if (!remove
9730 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9731 && DECL_P (decl)
9732 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9733 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9734 break;
9736 case OMP_CLAUSE_ALIGNED:
9737 decl = OMP_CLAUSE_DECL (c);
9738 if (!is_global_var (decl))
9740 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9741 remove = n == NULL || !(n->value & GOVD_SEEN);
9742 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9744 struct gimplify_omp_ctx *octx;
9745 if (n != NULL
9746 && (n->value & (GOVD_DATA_SHARE_CLASS
9747 & ~GOVD_FIRSTPRIVATE)))
9748 remove = true;
9749 else
9750 for (octx = ctx->outer_context; octx;
9751 octx = octx->outer_context)
9753 n = splay_tree_lookup (octx->variables,
9754 (splay_tree_key) decl);
9755 if (n == NULL)
9756 continue;
9757 if (n->value & GOVD_LOCAL)
9758 break;
9759 /* We have to avoid assigning a shared variable
9760 to itself when trying to add
9761 __builtin_assume_aligned. */
9762 if (n->value & GOVD_SHARED)
9764 remove = true;
9765 break;
9770 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9772 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9773 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9774 remove = true;
9776 break;
9778 case OMP_CLAUSE_NONTEMPORAL:
9779 decl = OMP_CLAUSE_DECL (c);
9780 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9781 remove = n == NULL || !(n->value & GOVD_SEEN);
9782 break;
9784 case OMP_CLAUSE_MAP:
9785 if (code == OMP_TARGET_EXIT_DATA
9786 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9788 remove = true;
9789 break;
9791 decl = OMP_CLAUSE_DECL (c);
9792 /* Data clauses associated with acc parallel reductions must be
9793 compatible with present_or_copy. Warn and adjust the clause
9794 if that is not the case. */
9795 if (ctx->region_type == ORT_ACC_PARALLEL)
9797 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9798 n = NULL;
9800 if (DECL_P (t))
9801 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9803 if (n && (n->value & GOVD_REDUCTION))
9805 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9807 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9808 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9809 && kind != GOMP_MAP_FORCE_PRESENT
9810 && kind != GOMP_MAP_POINTER)
9812 warning_at (OMP_CLAUSE_LOCATION (c), 0,
9813 "incompatible data clause with reduction "
9814 "on %qE; promoting to present_or_copy",
9815 DECL_NAME (t));
9816 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9820 if (!DECL_P (decl))
9822 if ((ctx->region_type & ORT_TARGET) != 0
9823 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9825 if (TREE_CODE (decl) == INDIRECT_REF
9826 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
9827 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
9828 == REFERENCE_TYPE))
9829 decl = TREE_OPERAND (decl, 0);
9830 if (TREE_CODE (decl) == COMPONENT_REF)
9832 while (TREE_CODE (decl) == COMPONENT_REF)
9833 decl = TREE_OPERAND (decl, 0);
9834 if (DECL_P (decl))
9836 n = splay_tree_lookup (ctx->variables,
9837 (splay_tree_key) decl);
9838 if (!(n->value & GOVD_SEEN))
9839 remove = true;
9843 break;
9845 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9846 if ((ctx->region_type & ORT_TARGET) != 0
9847 && !(n->value & GOVD_SEEN)
9848 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
9849 && (!is_global_var (decl)
9850 || !lookup_attribute ("omp declare target link",
9851 DECL_ATTRIBUTES (decl))))
9853 remove = true;
9854 /* For struct element mapping, if struct is never referenced
9855 in target block and none of the mapping has always modifier,
9856 remove all the struct element mappings, which immediately
9857 follow the GOMP_MAP_STRUCT map clause. */
9858 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
9860 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
9861 while (cnt--)
9862 OMP_CLAUSE_CHAIN (c)
9863 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
9866 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
9867 && code == OMP_TARGET_EXIT_DATA)
9868 remove = true;
9869 else if (DECL_SIZE (decl)
9870 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
9871 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
9872 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9873 && (OMP_CLAUSE_MAP_KIND (c)
9874 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9876 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9877 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9878 INTEGER_CST. */
9879 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
9881 tree decl2 = DECL_VALUE_EXPR (decl);
9882 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9883 decl2 = TREE_OPERAND (decl2, 0);
9884 gcc_assert (DECL_P (decl2));
9885 tree mem = build_simple_mem_ref (decl2);
9886 OMP_CLAUSE_DECL (c) = mem;
9887 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9888 if (ctx->outer_context)
9890 omp_notice_variable (ctx->outer_context, decl2, true);
9891 omp_notice_variable (ctx->outer_context,
9892 OMP_CLAUSE_SIZE (c), true);
9894 if (((ctx->region_type & ORT_TARGET) != 0
9895 || !ctx->target_firstprivatize_array_bases)
9896 && ((n->value & GOVD_SEEN) == 0
9897 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
9899 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9900 OMP_CLAUSE_MAP);
9901 OMP_CLAUSE_DECL (nc) = decl;
9902 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9903 if (ctx->target_firstprivatize_array_bases)
9904 OMP_CLAUSE_SET_MAP_KIND (nc,
9905 GOMP_MAP_FIRSTPRIVATE_POINTER);
9906 else
9907 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9908 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9909 OMP_CLAUSE_CHAIN (c) = nc;
9910 c = nc;
9913 else
9915 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9916 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9917 gcc_assert ((n->value & GOVD_SEEN) == 0
9918 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9919 == 0));
9921 break;
9923 case OMP_CLAUSE_TO:
9924 case OMP_CLAUSE_FROM:
9925 case OMP_CLAUSE__CACHE_:
9926 decl = OMP_CLAUSE_DECL (c);
9927 if (!DECL_P (decl))
9928 break;
9929 if (DECL_SIZE (decl)
9930 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9932 tree decl2 = DECL_VALUE_EXPR (decl);
9933 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9934 decl2 = TREE_OPERAND (decl2, 0);
9935 gcc_assert (DECL_P (decl2));
9936 tree mem = build_simple_mem_ref (decl2);
9937 OMP_CLAUSE_DECL (c) = mem;
9938 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9939 if (ctx->outer_context)
9941 omp_notice_variable (ctx->outer_context, decl2, true);
9942 omp_notice_variable (ctx->outer_context,
9943 OMP_CLAUSE_SIZE (c), true);
9946 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9947 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9948 break;
9950 case OMP_CLAUSE_REDUCTION:
9951 case OMP_CLAUSE_IN_REDUCTION:
9952 case OMP_CLAUSE_TASK_REDUCTION:
9953 decl = OMP_CLAUSE_DECL (c);
9954 /* OpenACC reductions need a present_or_copy data clause.
9955 Add one if necessary. Emit error when the reduction is private. */
9956 if (ctx->region_type == ORT_ACC_PARALLEL)
9958 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9959 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9961 remove = true;
9962 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9963 "reduction on %qE", DECL_NAME (decl));
9965 else if ((n->value & GOVD_MAP) == 0)
9967 tree next = OMP_CLAUSE_CHAIN (c);
9968 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9969 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9970 OMP_CLAUSE_DECL (nc) = decl;
9971 OMP_CLAUSE_CHAIN (c) = nc;
9972 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9973 while (1)
9975 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9976 if (OMP_CLAUSE_CHAIN (nc) == NULL)
9977 break;
9978 nc = OMP_CLAUSE_CHAIN (nc);
9980 OMP_CLAUSE_CHAIN (nc) = next;
9981 n->value |= GOVD_MAP;
9984 if (DECL_P (decl)
9985 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9986 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9987 break;
9988 case OMP_CLAUSE_COPYIN:
9989 case OMP_CLAUSE_COPYPRIVATE:
9990 case OMP_CLAUSE_IF:
9991 case OMP_CLAUSE_NUM_THREADS:
9992 case OMP_CLAUSE_NUM_TEAMS:
9993 case OMP_CLAUSE_THREAD_LIMIT:
9994 case OMP_CLAUSE_DIST_SCHEDULE:
9995 case OMP_CLAUSE_DEVICE:
9996 case OMP_CLAUSE_SCHEDULE:
9997 case OMP_CLAUSE_NOWAIT:
9998 case OMP_CLAUSE_ORDERED:
9999 case OMP_CLAUSE_DEFAULT:
10000 case OMP_CLAUSE_UNTIED:
10001 case OMP_CLAUSE_COLLAPSE:
10002 case OMP_CLAUSE_FINAL:
10003 case OMP_CLAUSE_MERGEABLE:
10004 case OMP_CLAUSE_PROC_BIND:
10005 case OMP_CLAUSE_SAFELEN:
10006 case OMP_CLAUSE_SIMDLEN:
10007 case OMP_CLAUSE_DEPEND:
10008 case OMP_CLAUSE_PRIORITY:
10009 case OMP_CLAUSE_GRAINSIZE:
10010 case OMP_CLAUSE_NUM_TASKS:
10011 case OMP_CLAUSE_NOGROUP:
10012 case OMP_CLAUSE_THREADS:
10013 case OMP_CLAUSE_SIMD:
10014 case OMP_CLAUSE_HINT:
10015 case OMP_CLAUSE_DEFAULTMAP:
10016 case OMP_CLAUSE_USE_DEVICE_PTR:
10017 case OMP_CLAUSE_IS_DEVICE_PTR:
10018 case OMP_CLAUSE_ASYNC:
10019 case OMP_CLAUSE_WAIT:
10020 case OMP_CLAUSE_INDEPENDENT:
10021 case OMP_CLAUSE_NUM_GANGS:
10022 case OMP_CLAUSE_NUM_WORKERS:
10023 case OMP_CLAUSE_VECTOR_LENGTH:
10024 case OMP_CLAUSE_GANG:
10025 case OMP_CLAUSE_WORKER:
10026 case OMP_CLAUSE_VECTOR:
10027 case OMP_CLAUSE_AUTO:
10028 case OMP_CLAUSE_SEQ:
10029 case OMP_CLAUSE_TILE:
10030 case OMP_CLAUSE_IF_PRESENT:
10031 case OMP_CLAUSE_FINALIZE:
10032 break;
10034 default:
10035 gcc_unreachable ();
10038 if (remove)
10039 *list_p = OMP_CLAUSE_CHAIN (c);
10040 else
10041 list_p = &OMP_CLAUSE_CHAIN (c);
10044 /* Add in any implicit data sharing. */
10045 struct gimplify_adjust_omp_clauses_data data;
10046 data.list_p = list_p;
10047 data.pre_p = pre_p;
10048 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
10050 gimplify_omp_ctxp = ctx->outer_context;
10051 delete_omp_context (ctx);
10054 /* Gimplify OACC_CACHE. */
10056 static void
10057 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
10059 tree expr = *expr_p;
10061 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
10062 OACC_CACHE);
10063 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
10064 OACC_CACHE);
10066 /* TODO: Do something sensible with this information. */
10068 *expr_p = NULL_TREE;
10071 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
10072 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
10073 kind. The entry kind will replace the one in CLAUSE, while the exit
10074 kind will be used in a new omp_clause and returned to the caller. */
10076 static tree
10077 gimplify_oacc_declare_1 (tree clause)
10079 HOST_WIDE_INT kind, new_op;
10080 bool ret = false;
10081 tree c = NULL;
10083 kind = OMP_CLAUSE_MAP_KIND (clause);
10085 switch (kind)
10087 case GOMP_MAP_ALLOC:
10088 new_op = GOMP_MAP_RELEASE;
10089 ret = true;
10090 break;
10092 case GOMP_MAP_FROM:
10093 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
10094 new_op = GOMP_MAP_FROM;
10095 ret = true;
10096 break;
10098 case GOMP_MAP_TOFROM:
10099 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
10100 new_op = GOMP_MAP_FROM;
10101 ret = true;
10102 break;
10104 case GOMP_MAP_DEVICE_RESIDENT:
10105 case GOMP_MAP_FORCE_DEVICEPTR:
10106 case GOMP_MAP_FORCE_PRESENT:
10107 case GOMP_MAP_LINK:
10108 case GOMP_MAP_POINTER:
10109 case GOMP_MAP_TO:
10110 break;
10112 default:
10113 gcc_unreachable ();
10114 break;
10117 if (ret)
10119 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
10120 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
10121 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
10124 return c;
10127 /* Gimplify OACC_DECLARE. */
10129 static void
10130 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
10132 tree expr = *expr_p;
10133 gomp_target *stmt;
10134 tree clauses, t, decl;
10136 clauses = OACC_DECLARE_CLAUSES (expr);
10138 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
10139 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
10141 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
10143 decl = OMP_CLAUSE_DECL (t);
10145 if (TREE_CODE (decl) == MEM_REF)
10146 decl = TREE_OPERAND (decl, 0);
10148 if (VAR_P (decl) && !is_oacc_declared (decl))
10150 tree attr = get_identifier ("oacc declare target");
10151 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
10152 DECL_ATTRIBUTES (decl));
10155 if (VAR_P (decl)
10156 && !is_global_var (decl)
10157 && DECL_CONTEXT (decl) == current_function_decl)
10159 tree c = gimplify_oacc_declare_1 (t);
10160 if (c)
10162 if (oacc_declare_returns == NULL)
10163 oacc_declare_returns = new hash_map<tree, tree>;
10165 oacc_declare_returns->put (decl, c);
10169 if (gimplify_omp_ctxp)
10170 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
10173 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
10174 clauses);
10176 gimplify_seq_add_stmt (pre_p, stmt);
10178 *expr_p = NULL_TREE;
10181 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
10182 gimplification of the body, as well as scanning the body for used
10183 variables. We need to do this scan now, because variable-sized
10184 decls will be decomposed during gimplification. */
10186 static void
10187 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
10189 tree expr = *expr_p;
10190 gimple *g;
10191 gimple_seq body = NULL;
10193 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
10194 OMP_PARALLEL_COMBINED (expr)
10195 ? ORT_COMBINED_PARALLEL
10196 : ORT_PARALLEL, OMP_PARALLEL);
10198 push_gimplify_context ();
10200 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
10201 if (gimple_code (g) == GIMPLE_BIND)
10202 pop_gimplify_context (g);
10203 else
10204 pop_gimplify_context (NULL);
10206 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
10207 OMP_PARALLEL);
10209 g = gimple_build_omp_parallel (body,
10210 OMP_PARALLEL_CLAUSES (expr),
10211 NULL_TREE, NULL_TREE);
10212 if (OMP_PARALLEL_COMBINED (expr))
10213 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
10214 gimplify_seq_add_stmt (pre_p, g);
10215 *expr_p = NULL_TREE;
10218 /* Gimplify the contents of an OMP_TASK statement. This involves
10219 gimplification of the body, as well as scanning the body for used
10220 variables. We need to do this scan now, because variable-sized
10221 decls will be decomposed during gimplification. */
10223 static void
10224 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
10226 tree expr = *expr_p;
10227 gimple *g;
10228 gimple_seq body = NULL;
10230 if (OMP_TASK_BODY (expr) == NULL_TREE)
10231 for (tree c = OMP_TASK_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
10232 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
10233 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_MUTEXINOUTSET)
10235 error_at (OMP_CLAUSE_LOCATION (c),
10236 "%<mutexinoutset%> kind in %<depend%> clause on a "
10237 "%<taskwait%> construct");
10238 break;
10241 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
10242 omp_find_clause (OMP_TASK_CLAUSES (expr),
10243 OMP_CLAUSE_UNTIED)
10244 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
10246 if (OMP_TASK_BODY (expr))
10248 push_gimplify_context ();
10250 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
10251 if (gimple_code (g) == GIMPLE_BIND)
10252 pop_gimplify_context (g);
10253 else
10254 pop_gimplify_context (NULL);
10257 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
10258 OMP_TASK);
10260 g = gimple_build_omp_task (body,
10261 OMP_TASK_CLAUSES (expr),
10262 NULL_TREE, NULL_TREE,
10263 NULL_TREE, NULL_TREE, NULL_TREE);
10264 if (OMP_TASK_BODY (expr) == NULL_TREE)
10265 gimple_omp_task_set_taskwait_p (g, true);
10266 gimplify_seq_add_stmt (pre_p, g);
10267 *expr_p = NULL_TREE;
10270 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
10271 with non-NULL OMP_FOR_INIT. Also, fill in pdata array,
10272 pdata[0] non-NULL if there is anything non-trivial in between, pdata[1]
10273 is address of OMP_PARALLEL in between if any, pdata[2] is address of
10274 OMP_FOR in between if any and pdata[3] is address of the inner
10275 OMP_FOR/OMP_SIMD. */
10277 static tree
10278 find_combined_omp_for (tree *tp, int *walk_subtrees, void *data)
10280 tree **pdata = (tree **) data;
10281 *walk_subtrees = 0;
10282 switch (TREE_CODE (*tp))
10284 case OMP_FOR:
10285 if (OMP_FOR_INIT (*tp) != NULL_TREE)
10287 pdata[3] = tp;
10288 return *tp;
10290 pdata[2] = tp;
10291 *walk_subtrees = 1;
10292 break;
10293 case OMP_SIMD:
10294 if (OMP_FOR_INIT (*tp) != NULL_TREE)
10296 pdata[3] = tp;
10297 return *tp;
10299 break;
10300 case BIND_EXPR:
10301 if (BIND_EXPR_VARS (*tp)
10302 || (BIND_EXPR_BLOCK (*tp)
10303 && BLOCK_VARS (BIND_EXPR_BLOCK (*tp))))
10304 pdata[0] = tp;
10305 *walk_subtrees = 1;
10306 break;
10307 case STATEMENT_LIST:
10308 if (!tsi_one_before_end_p (tsi_start (*tp)))
10309 pdata[0] = tp;
10310 *walk_subtrees = 1;
10311 break;
10312 case TRY_FINALLY_EXPR:
10313 pdata[0] = tp;
10314 *walk_subtrees = 1;
10315 break;
10316 case OMP_PARALLEL:
10317 pdata[1] = tp;
10318 *walk_subtrees = 1;
10319 break;
10320 default:
10321 break;
10323 return NULL_TREE;
10326 /* Gimplify the gross structure of an OMP_FOR statement. */
10328 static enum gimplify_status
10329 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
10331 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
10332 enum gimplify_status ret = GS_ALL_DONE;
10333 enum gimplify_status tret;
10334 gomp_for *gfor;
10335 gimple_seq for_body, for_pre_body;
10336 int i;
10337 bitmap has_decl_expr = NULL;
10338 enum omp_region_type ort = ORT_WORKSHARE;
10340 orig_for_stmt = for_stmt = *expr_p;
10342 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
10344 tree *data[4] = { NULL, NULL, NULL, NULL };
10345 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
10346 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
10347 find_combined_omp_for, data, NULL);
10348 if (inner_for_stmt == NULL_TREE)
10350 gcc_assert (seen_error ());
10351 *expr_p = NULL_TREE;
10352 return GS_ERROR;
10354 if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
10356 append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
10357 &OMP_FOR_PRE_BODY (for_stmt));
10358 OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
10360 if (OMP_FOR_PRE_BODY (inner_for_stmt))
10362 append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
10363 &OMP_FOR_PRE_BODY (for_stmt));
10364 OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
10367 if (data[0])
10369 /* We have some statements or variable declarations in between
10370 the composite construct directives. Move them around the
10371 inner_for_stmt. */
10372 data[0] = expr_p;
10373 for (i = 0; i < 3; i++)
10374 if (data[i])
10376 tree t = *data[i];
10377 if (i < 2 && data[i + 1] == &OMP_BODY (t))
10378 data[i + 1] = data[i];
10379 *data[i] = OMP_BODY (t);
10380 tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
10381 NULL_TREE, make_node (BLOCK));
10382 OMP_BODY (t) = body;
10383 append_to_statement_list_force (inner_for_stmt,
10384 &BIND_EXPR_BODY (body));
10385 *data[3] = t;
10386 data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
10387 gcc_assert (*data[3] == inner_for_stmt);
10389 return GS_OK;
10392 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
10393 if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
10394 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10395 i)) == TREE_LIST
10396 && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10397 i)))
10399 tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
10400 /* Class iterators aren't allowed on OMP_SIMD, so the only
10401 case we need to solve is distribute parallel for. */
10402 gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
10403 && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
10404 && data[1]);
10405 tree orig_decl = TREE_PURPOSE (orig);
10406 tree last = TREE_VALUE (orig);
10407 tree *pc;
10408 for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
10409 *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
10410 if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
10411 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
10412 && OMP_CLAUSE_DECL (*pc) == orig_decl)
10413 break;
10414 if (*pc == NULL_TREE)
10416 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
10418 /* private clause will appear only on inner_for_stmt.
10419 Change it into firstprivate, and add private clause
10420 on for_stmt. */
10421 tree c = copy_node (*pc);
10422 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10423 OMP_FOR_CLAUSES (for_stmt) = c;
10424 OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
10425 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
10427 else
10429 /* lastprivate clause will appear on both inner_for_stmt
10430 and for_stmt. Add firstprivate clause to
10431 inner_for_stmt. */
10432 tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
10433 OMP_CLAUSE_FIRSTPRIVATE);
10434 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
10435 OMP_CLAUSE_CHAIN (c) = *pc;
10436 *pc = c;
10437 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
10439 tree c = build_omp_clause (UNKNOWN_LOCATION,
10440 OMP_CLAUSE_FIRSTPRIVATE);
10441 OMP_CLAUSE_DECL (c) = last;
10442 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10443 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10444 c = build_omp_clause (UNKNOWN_LOCATION,
10445 *pc ? OMP_CLAUSE_SHARED
10446 : OMP_CLAUSE_FIRSTPRIVATE);
10447 OMP_CLAUSE_DECL (c) = orig_decl;
10448 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10449 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10451 /* Similarly, take care of C++ range for temporaries, those should
10452 be firstprivate on OMP_PARALLEL if any. */
10453 if (data[1])
10454 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
10455 if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
10456 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10457 i)) == TREE_LIST
10458 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10459 i)))
10461 tree orig
10462 = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
10463 tree v = TREE_CHAIN (orig);
10464 tree c = build_omp_clause (UNKNOWN_LOCATION,
10465 OMP_CLAUSE_FIRSTPRIVATE);
10466 /* First add firstprivate clause for the __for_end artificial
10467 decl. */
10468 OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 1);
10469 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
10470 == REFERENCE_TYPE)
10471 OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
10472 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10473 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10474 if (TREE_VEC_ELT (v, 0))
10476 /* And now the same for __for_range artificial decl if it
10477 exists. */
10478 c = build_omp_clause (UNKNOWN_LOCATION,
10479 OMP_CLAUSE_FIRSTPRIVATE);
10480 OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 0);
10481 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
10482 == REFERENCE_TYPE)
10483 OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
10484 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10485 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10490 switch (TREE_CODE (for_stmt))
10492 case OMP_FOR:
10493 case OMP_DISTRIBUTE:
10494 break;
10495 case OACC_LOOP:
10496 ort = ORT_ACC;
10497 break;
10498 case OMP_TASKLOOP:
10499 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
10500 ort = ORT_UNTIED_TASKLOOP;
10501 else
10502 ort = ORT_TASKLOOP;
10503 break;
10504 case OMP_SIMD:
10505 ort = ORT_SIMD;
10506 break;
10507 default:
10508 gcc_unreachable ();
10511 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
10512 clause for the IV. */
10513 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10515 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
10516 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10517 decl = TREE_OPERAND (t, 0);
10518 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
10519 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10520 && OMP_CLAUSE_DECL (c) == decl)
10522 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
10523 break;
10527 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
10528 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
10529 TREE_CODE (for_stmt));
10531 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
10532 gimplify_omp_ctxp->distribute = true;
10534 /* Handle OMP_FOR_INIT. */
10535 for_pre_body = NULL;
10536 if ((ort == ORT_SIMD
10537 || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
10538 && OMP_FOR_PRE_BODY (for_stmt))
10540 has_decl_expr = BITMAP_ALLOC (NULL);
10541 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
10542 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
10543 == VAR_DECL)
10545 t = OMP_FOR_PRE_BODY (for_stmt);
10546 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
10548 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
10550 tree_stmt_iterator si;
10551 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
10552 tsi_next (&si))
10554 t = tsi_stmt (si);
10555 if (TREE_CODE (t) == DECL_EXPR
10556 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
10557 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
10561 if (OMP_FOR_PRE_BODY (for_stmt))
10563 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
10564 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
10565 else
10567 struct gimplify_omp_ctx ctx;
10568 memset (&ctx, 0, sizeof (ctx));
10569 ctx.region_type = ORT_NONE;
10570 gimplify_omp_ctxp = &ctx;
10571 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
10572 gimplify_omp_ctxp = NULL;
10575 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
10577 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
10578 for_stmt = inner_for_stmt;
10580 /* For taskloop, need to gimplify the start, end and step before the
10581 taskloop, outside of the taskloop omp context. */
10582 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10584 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10586 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10587 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
10589 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
10590 TREE_OPERAND (t, 1)
10591 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
10592 gimple_seq_empty_p (for_pre_body)
10593 ? pre_p : &for_pre_body, NULL,
10594 false);
10595 /* Reference to pointer conversion is considered useless,
10596 but is significant for firstprivate clause. Force it
10597 here. */
10598 if (TREE_CODE (type) == POINTER_TYPE
10599 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
10600 == REFERENCE_TYPE))
10602 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
10603 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
10604 TREE_OPERAND (t, 1));
10605 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
10606 ? pre_p : &for_pre_body);
10607 TREE_OPERAND (t, 1) = v;
10609 tree c = build_omp_clause (input_location,
10610 OMP_CLAUSE_FIRSTPRIVATE);
10611 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
10612 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
10613 OMP_FOR_CLAUSES (orig_for_stmt) = c;
10616 /* Handle OMP_FOR_COND. */
10617 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10618 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
10620 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
10621 TREE_OPERAND (t, 1)
10622 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
10623 gimple_seq_empty_p (for_pre_body)
10624 ? pre_p : &for_pre_body, NULL,
10625 false);
10626 /* Reference to pointer conversion is considered useless,
10627 but is significant for firstprivate clause. Force it
10628 here. */
10629 if (TREE_CODE (type) == POINTER_TYPE
10630 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
10631 == REFERENCE_TYPE))
10633 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
10634 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
10635 TREE_OPERAND (t, 1));
10636 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
10637 ? pre_p : &for_pre_body);
10638 TREE_OPERAND (t, 1) = v;
10640 tree c = build_omp_clause (input_location,
10641 OMP_CLAUSE_FIRSTPRIVATE);
10642 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
10643 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
10644 OMP_FOR_CLAUSES (orig_for_stmt) = c;
10647 /* Handle OMP_FOR_INCR. */
10648 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10649 if (TREE_CODE (t) == MODIFY_EXPR)
10651 decl = TREE_OPERAND (t, 0);
10652 t = TREE_OPERAND (t, 1);
10653 tree *tp = &TREE_OPERAND (t, 1);
10654 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
10655 tp = &TREE_OPERAND (t, 0);
10657 if (!is_gimple_constant (*tp))
10659 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
10660 ? pre_p : &for_pre_body;
10661 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
10662 tree c = build_omp_clause (input_location,
10663 OMP_CLAUSE_FIRSTPRIVATE);
10664 OMP_CLAUSE_DECL (c) = *tp;
10665 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
10666 OMP_FOR_CLAUSES (orig_for_stmt) = c;
10671 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
10672 OMP_TASKLOOP);
10675 if (orig_for_stmt != for_stmt)
10676 gimplify_omp_ctxp->combined_loop = true;
10678 for_body = NULL;
10679 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
10680 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
10681 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
10682 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
10684 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
10685 bool is_doacross = false;
10686 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
10688 is_doacross = true;
10689 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
10690 (OMP_FOR_INIT (for_stmt))
10691 * 2);
10693 int collapse = 1, tile = 0;
10694 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
10695 if (c)
10696 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
10697 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
10698 if (c)
10699 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
10700 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10702 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10703 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10704 decl = TREE_OPERAND (t, 0);
10705 gcc_assert (DECL_P (decl));
10706 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
10707 || POINTER_TYPE_P (TREE_TYPE (decl)));
10708 if (is_doacross)
10710 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
10712 tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
10713 if (TREE_CODE (orig_decl) == TREE_LIST)
10715 orig_decl = TREE_PURPOSE (orig_decl);
10716 if (!orig_decl)
10717 orig_decl = decl;
10719 gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
10721 else
10722 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
10723 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
10726 /* Make sure the iteration variable is private. */
10727 tree c = NULL_TREE;
10728 tree c2 = NULL_TREE;
10729 if (orig_for_stmt != for_stmt)
10731 /* Preserve this information until we gimplify the inner simd. */
10732 if (has_decl_expr
10733 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
10734 TREE_PRIVATE (t) = 1;
10736 else if (ort == ORT_SIMD)
10738 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10739 (splay_tree_key) decl);
10740 omp_is_private (gimplify_omp_ctxp, decl,
10741 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
10742 != 1));
10743 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
10744 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10745 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10747 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10748 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
10749 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
10750 if ((has_decl_expr
10751 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
10752 || TREE_PRIVATE (t))
10754 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10755 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10757 struct gimplify_omp_ctx *outer
10758 = gimplify_omp_ctxp->outer_context;
10759 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
10761 if (outer->region_type == ORT_WORKSHARE
10762 && outer->combined_loop)
10764 n = splay_tree_lookup (outer->variables,
10765 (splay_tree_key)decl);
10766 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10768 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10769 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10771 else
10773 struct gimplify_omp_ctx *octx = outer->outer_context;
10774 if (octx
10775 && octx->region_type == ORT_COMBINED_PARALLEL
10776 && octx->outer_context
10777 && (octx->outer_context->region_type
10778 == ORT_WORKSHARE)
10779 && octx->outer_context->combined_loop)
10781 octx = octx->outer_context;
10782 n = splay_tree_lookup (octx->variables,
10783 (splay_tree_key)decl);
10784 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10786 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10787 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10794 OMP_CLAUSE_DECL (c) = decl;
10795 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10796 OMP_FOR_CLAUSES (for_stmt) = c;
10797 omp_add_variable (gimplify_omp_ctxp, decl, flags);
10798 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
10800 if (outer->region_type == ORT_WORKSHARE
10801 && outer->combined_loop)
10803 if (outer->outer_context
10804 && (outer->outer_context->region_type
10805 == ORT_COMBINED_PARALLEL))
10806 outer = outer->outer_context;
10807 else if (omp_check_private (outer, decl, false))
10808 outer = NULL;
10810 else if (((outer->region_type & ORT_TASKLOOP)
10811 == ORT_TASKLOOP)
10812 && outer->combined_loop
10813 && !omp_check_private (gimplify_omp_ctxp,
10814 decl, false))
10816 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10818 omp_notice_variable (outer, decl, true);
10819 outer = NULL;
10821 if (outer)
10823 n = splay_tree_lookup (outer->variables,
10824 (splay_tree_key)decl);
10825 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10827 omp_add_variable (outer, decl,
10828 GOVD_LASTPRIVATE | GOVD_SEEN);
10829 if (outer->region_type == ORT_COMBINED_PARALLEL
10830 && outer->outer_context
10831 && (outer->outer_context->region_type
10832 == ORT_WORKSHARE)
10833 && outer->outer_context->combined_loop)
10835 outer = outer->outer_context;
10836 n = splay_tree_lookup (outer->variables,
10837 (splay_tree_key)decl);
10838 if (omp_check_private (outer, decl, false))
10839 outer = NULL;
10840 else if (n == NULL
10841 || ((n->value & GOVD_DATA_SHARE_CLASS)
10842 == 0))
10843 omp_add_variable (outer, decl,
10844 GOVD_LASTPRIVATE
10845 | GOVD_SEEN);
10846 else
10847 outer = NULL;
10849 if (outer && outer->outer_context
10850 && ((outer->outer_context->region_type
10851 & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS
10852 || (((outer->region_type & ORT_TASKLOOP)
10853 == ORT_TASKLOOP)
10854 && (outer->outer_context->region_type
10855 == ORT_COMBINED_PARALLEL))))
10857 outer = outer->outer_context;
10858 n = splay_tree_lookup (outer->variables,
10859 (splay_tree_key)decl);
10860 if (n == NULL
10861 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10862 omp_add_variable (outer, decl,
10863 GOVD_SHARED | GOVD_SEEN);
10864 else
10865 outer = NULL;
10867 if (outer && outer->outer_context)
10868 omp_notice_variable (outer->outer_context, decl,
10869 true);
10874 else
10876 bool lastprivate
10877 = (!has_decl_expr
10878 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
10879 if (TREE_PRIVATE (t))
10880 lastprivate = false;
10881 struct gimplify_omp_ctx *outer
10882 = gimplify_omp_ctxp->outer_context;
10883 if (outer && lastprivate)
10885 if (outer->region_type == ORT_WORKSHARE
10886 && outer->combined_loop)
10888 n = splay_tree_lookup (outer->variables,
10889 (splay_tree_key)decl);
10890 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10892 lastprivate = false;
10893 outer = NULL;
10895 else if (outer->outer_context
10896 && (outer->outer_context->region_type
10897 == ORT_COMBINED_PARALLEL))
10898 outer = outer->outer_context;
10899 else if (omp_check_private (outer, decl, false))
10900 outer = NULL;
10902 else if (((outer->region_type & ORT_TASKLOOP)
10903 == ORT_TASKLOOP)
10904 && outer->combined_loop
10905 && !omp_check_private (gimplify_omp_ctxp,
10906 decl, false))
10908 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10910 omp_notice_variable (outer, decl, true);
10911 outer = NULL;
10913 if (outer)
10915 n = splay_tree_lookup (outer->variables,
10916 (splay_tree_key)decl);
10917 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10919 omp_add_variable (outer, decl,
10920 GOVD_LASTPRIVATE | GOVD_SEEN);
10921 if (outer->region_type == ORT_COMBINED_PARALLEL
10922 && outer->outer_context
10923 && (outer->outer_context->region_type
10924 == ORT_WORKSHARE)
10925 && outer->outer_context->combined_loop)
10927 outer = outer->outer_context;
10928 n = splay_tree_lookup (outer->variables,
10929 (splay_tree_key)decl);
10930 if (omp_check_private (outer, decl, false))
10931 outer = NULL;
10932 else if (n == NULL
10933 || ((n->value & GOVD_DATA_SHARE_CLASS)
10934 == 0))
10935 omp_add_variable (outer, decl,
10936 GOVD_LASTPRIVATE
10937 | GOVD_SEEN);
10938 else
10939 outer = NULL;
10941 if (outer && outer->outer_context
10942 && ((outer->outer_context->region_type
10943 & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS
10944 || (((outer->region_type & ORT_TASKLOOP)
10945 == ORT_TASKLOOP)
10946 && (outer->outer_context->region_type
10947 == ORT_COMBINED_PARALLEL))))
10949 outer = outer->outer_context;
10950 n = splay_tree_lookup (outer->variables,
10951 (splay_tree_key)decl);
10952 if (n == NULL
10953 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10954 omp_add_variable (outer, decl,
10955 GOVD_SHARED | GOVD_SEEN);
10956 else
10957 outer = NULL;
10959 if (outer && outer->outer_context)
10960 omp_notice_variable (outer->outer_context, decl,
10961 true);
10966 c = build_omp_clause (input_location,
10967 lastprivate ? OMP_CLAUSE_LASTPRIVATE
10968 : OMP_CLAUSE_PRIVATE);
10969 OMP_CLAUSE_DECL (c) = decl;
10970 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10971 OMP_FOR_CLAUSES (for_stmt) = c;
10972 omp_add_variable (gimplify_omp_ctxp, decl,
10973 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
10974 | GOVD_EXPLICIT | GOVD_SEEN);
10975 c = NULL_TREE;
10978 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
10979 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10980 else
10981 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
10983 /* If DECL is not a gimple register, create a temporary variable to act
10984 as an iteration counter. This is valid, since DECL cannot be
10985 modified in the body of the loop. Similarly for any iteration vars
10986 in simd with collapse > 1 where the iterator vars must be
10987 lastprivate. */
10988 if (orig_for_stmt != for_stmt)
10989 var = decl;
10990 else if (!is_gimple_reg (decl)
10991 || (ort == ORT_SIMD
10992 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
10994 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10995 /* Make sure omp_add_variable is not called on it prematurely.
10996 We call it ourselves a few lines later. */
10997 gimplify_omp_ctxp = NULL;
10998 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10999 gimplify_omp_ctxp = ctx;
11000 TREE_OPERAND (t, 0) = var;
11002 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
11004 if (ort == ORT_SIMD
11005 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
11007 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
11008 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
11009 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
11010 OMP_CLAUSE_DECL (c2) = var;
11011 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
11012 OMP_FOR_CLAUSES (for_stmt) = c2;
11013 omp_add_variable (gimplify_omp_ctxp, var,
11014 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
11015 if (c == NULL_TREE)
11017 c = c2;
11018 c2 = NULL_TREE;
11021 else
11022 omp_add_variable (gimplify_omp_ctxp, var,
11023 GOVD_PRIVATE | GOVD_SEEN);
11025 else
11026 var = decl;
11028 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
11029 is_gimple_val, fb_rvalue, false);
11030 ret = MIN (ret, tret);
11031 if (ret == GS_ERROR)
11032 return ret;
11034 /* Handle OMP_FOR_COND. */
11035 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
11036 gcc_assert (COMPARISON_CLASS_P (t));
11037 gcc_assert (TREE_OPERAND (t, 0) == decl);
11039 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
11040 is_gimple_val, fb_rvalue, false);
11041 ret = MIN (ret, tret);
11043 /* Handle OMP_FOR_INCR. */
11044 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11045 switch (TREE_CODE (t))
11047 case PREINCREMENT_EXPR:
11048 case POSTINCREMENT_EXPR:
11050 tree decl = TREE_OPERAND (t, 0);
11051 /* c_omp_for_incr_canonicalize_ptr() should have been
11052 called to massage things appropriately. */
11053 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
11055 if (orig_for_stmt != for_stmt)
11056 break;
11057 t = build_int_cst (TREE_TYPE (decl), 1);
11058 if (c)
11059 OMP_CLAUSE_LINEAR_STEP (c) = t;
11060 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
11061 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
11062 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
11063 break;
11066 case PREDECREMENT_EXPR:
11067 case POSTDECREMENT_EXPR:
11068 /* c_omp_for_incr_canonicalize_ptr() should have been
11069 called to massage things appropriately. */
11070 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
11071 if (orig_for_stmt != for_stmt)
11072 break;
11073 t = build_int_cst (TREE_TYPE (decl), -1);
11074 if (c)
11075 OMP_CLAUSE_LINEAR_STEP (c) = t;
11076 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
11077 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
11078 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
11079 break;
11081 case MODIFY_EXPR:
11082 gcc_assert (TREE_OPERAND (t, 0) == decl);
11083 TREE_OPERAND (t, 0) = var;
11085 t = TREE_OPERAND (t, 1);
11086 switch (TREE_CODE (t))
11088 case PLUS_EXPR:
11089 if (TREE_OPERAND (t, 1) == decl)
11091 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
11092 TREE_OPERAND (t, 0) = var;
11093 break;
11096 /* Fallthru. */
11097 case MINUS_EXPR:
11098 case POINTER_PLUS_EXPR:
11099 gcc_assert (TREE_OPERAND (t, 0) == decl);
11100 TREE_OPERAND (t, 0) = var;
11101 break;
11102 default:
11103 gcc_unreachable ();
11106 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
11107 is_gimple_val, fb_rvalue, false);
11108 ret = MIN (ret, tret);
11109 if (c)
11111 tree step = TREE_OPERAND (t, 1);
11112 tree stept = TREE_TYPE (decl);
11113 if (POINTER_TYPE_P (stept))
11114 stept = sizetype;
11115 step = fold_convert (stept, step);
11116 if (TREE_CODE (t) == MINUS_EXPR)
11117 step = fold_build1 (NEGATE_EXPR, stept, step);
11118 OMP_CLAUSE_LINEAR_STEP (c) = step;
11119 if (step != TREE_OPERAND (t, 1))
11121 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
11122 &for_pre_body, NULL,
11123 is_gimple_val, fb_rvalue, false);
11124 ret = MIN (ret, tret);
11127 break;
11129 default:
11130 gcc_unreachable ();
11133 if (c2)
11135 gcc_assert (c);
11136 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
11139 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
11141 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
11142 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
11143 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
11144 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
11145 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
11146 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
11147 && OMP_CLAUSE_DECL (c) == decl)
11149 if (is_doacross && (collapse == 1 || i >= collapse))
11150 t = var;
11151 else
11153 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11154 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
11155 gcc_assert (TREE_OPERAND (t, 0) == var);
11156 t = TREE_OPERAND (t, 1);
11157 gcc_assert (TREE_CODE (t) == PLUS_EXPR
11158 || TREE_CODE (t) == MINUS_EXPR
11159 || TREE_CODE (t) == POINTER_PLUS_EXPR);
11160 gcc_assert (TREE_OPERAND (t, 0) == var);
11161 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
11162 is_doacross ? var : decl,
11163 TREE_OPERAND (t, 1));
11165 gimple_seq *seq;
11166 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11167 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
11168 else
11169 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
11170 gimplify_assign (decl, t, seq);
11175 BITMAP_FREE (has_decl_expr);
11177 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11179 push_gimplify_context ();
11180 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
11182 OMP_FOR_BODY (orig_for_stmt)
11183 = build3 (BIND_EXPR, void_type_node, NULL,
11184 OMP_FOR_BODY (orig_for_stmt), NULL);
11185 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
11189 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
11190 &for_body);
11192 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11194 if (gimple_code (g) == GIMPLE_BIND)
11195 pop_gimplify_context (g);
11196 else
11197 pop_gimplify_context (NULL);
11200 if (orig_for_stmt != for_stmt)
11201 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
11203 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
11204 decl = TREE_OPERAND (t, 0);
11205 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
11206 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11207 gimplify_omp_ctxp = ctx->outer_context;
11208 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
11209 gimplify_omp_ctxp = ctx;
11210 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
11211 TREE_OPERAND (t, 0) = var;
11212 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11213 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
11214 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
11217 gimplify_adjust_omp_clauses (pre_p, for_body,
11218 &OMP_FOR_CLAUSES (orig_for_stmt),
11219 TREE_CODE (orig_for_stmt));
11221 int kind;
11222 switch (TREE_CODE (orig_for_stmt))
11224 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
11225 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
11226 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
11227 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
11228 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
11229 default:
11230 gcc_unreachable ();
11232 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
11233 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
11234 for_pre_body);
11235 if (orig_for_stmt != for_stmt)
11236 gimple_omp_for_set_combined_p (gfor, true);
11237 if (gimplify_omp_ctxp
11238 && (gimplify_omp_ctxp->combined_loop
11239 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
11240 && gimplify_omp_ctxp->outer_context
11241 && gimplify_omp_ctxp->outer_context->combined_loop)))
11243 gimple_omp_for_set_combined_into_p (gfor, true);
11244 if (gimplify_omp_ctxp->combined_loop)
11245 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
11246 else
11247 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
11250 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
11252 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
11253 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
11254 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
11255 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
11256 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
11257 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
11258 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11259 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
11262 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
11263 constructs with GIMPLE_OMP_TASK sandwiched in between them.
11264 The outer taskloop stands for computing the number of iterations,
11265 counts for collapsed loops and holding taskloop specific clauses.
11266 The task construct stands for the effect of data sharing on the
11267 explicit task it creates and the inner taskloop stands for expansion
11268 of the static loop inside of the explicit task construct. */
11269 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11271 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
11272 tree task_clauses = NULL_TREE;
11273 tree c = *gfor_clauses_ptr;
11274 tree *gtask_clauses_ptr = &task_clauses;
11275 tree outer_for_clauses = NULL_TREE;
11276 tree *gforo_clauses_ptr = &outer_for_clauses;
11277 for (; c; c = OMP_CLAUSE_CHAIN (c))
11278 switch (OMP_CLAUSE_CODE (c))
11280 /* These clauses are allowed on task, move them there. */
11281 case OMP_CLAUSE_SHARED:
11282 case OMP_CLAUSE_FIRSTPRIVATE:
11283 case OMP_CLAUSE_DEFAULT:
11284 case OMP_CLAUSE_IF:
11285 case OMP_CLAUSE_UNTIED:
11286 case OMP_CLAUSE_FINAL:
11287 case OMP_CLAUSE_MERGEABLE:
11288 case OMP_CLAUSE_PRIORITY:
11289 case OMP_CLAUSE_REDUCTION:
11290 case OMP_CLAUSE_IN_REDUCTION:
11291 *gtask_clauses_ptr = c;
11292 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11293 break;
11294 case OMP_CLAUSE_PRIVATE:
11295 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
11297 /* We want private on outer for and firstprivate
11298 on task. */
11299 *gtask_clauses_ptr
11300 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
11301 OMP_CLAUSE_FIRSTPRIVATE);
11302 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
11303 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
11304 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
11305 *gforo_clauses_ptr = c;
11306 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11308 else
11310 *gtask_clauses_ptr = c;
11311 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11313 break;
11314 /* These clauses go into outer taskloop clauses. */
11315 case OMP_CLAUSE_GRAINSIZE:
11316 case OMP_CLAUSE_NUM_TASKS:
11317 case OMP_CLAUSE_NOGROUP:
11318 *gforo_clauses_ptr = c;
11319 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11320 break;
11321 /* Taskloop clause we duplicate on both taskloops. */
11322 case OMP_CLAUSE_COLLAPSE:
11323 *gfor_clauses_ptr = c;
11324 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11325 *gforo_clauses_ptr = copy_node (c);
11326 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
11327 break;
11328 /* For lastprivate, keep the clause on inner taskloop, and add
11329 a shared clause on task. If the same decl is also firstprivate,
11330 add also firstprivate clause on the inner taskloop. */
11331 case OMP_CLAUSE_LASTPRIVATE:
11332 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
11334 /* For taskloop C++ lastprivate IVs, we want:
11335 1) private on outer taskloop
11336 2) firstprivate and shared on task
11337 3) lastprivate on inner taskloop */
11338 *gtask_clauses_ptr
11339 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
11340 OMP_CLAUSE_FIRSTPRIVATE);
11341 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
11342 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
11343 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
11344 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
11345 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
11346 OMP_CLAUSE_PRIVATE);
11347 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
11348 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
11349 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
11350 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
11352 *gfor_clauses_ptr = c;
11353 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11354 *gtask_clauses_ptr
11355 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
11356 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
11357 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
11358 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
11359 gtask_clauses_ptr
11360 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
11361 break;
11362 default:
11363 gcc_unreachable ();
11365 *gfor_clauses_ptr = NULL_TREE;
11366 *gtask_clauses_ptr = NULL_TREE;
11367 *gforo_clauses_ptr = NULL_TREE;
11368 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
11369 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
11370 NULL_TREE, NULL_TREE, NULL_TREE);
11371 gimple_omp_task_set_taskloop_p (g, true);
11372 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
11373 gomp_for *gforo
11374 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
11375 gimple_omp_for_collapse (gfor),
11376 gimple_omp_for_pre_body (gfor));
11377 gimple_omp_for_set_pre_body (gfor, NULL);
11378 gimple_omp_for_set_combined_p (gforo, true);
11379 gimple_omp_for_set_combined_into_p (gfor, true);
11380 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
11382 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
11383 tree v = create_tmp_var (type);
11384 gimple_omp_for_set_index (gforo, i, v);
11385 t = unshare_expr (gimple_omp_for_initial (gfor, i));
11386 gimple_omp_for_set_initial (gforo, i, t);
11387 gimple_omp_for_set_cond (gforo, i,
11388 gimple_omp_for_cond (gfor, i));
11389 t = unshare_expr (gimple_omp_for_final (gfor, i));
11390 gimple_omp_for_set_final (gforo, i, t);
11391 t = unshare_expr (gimple_omp_for_incr (gfor, i));
11392 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
11393 TREE_OPERAND (t, 0) = v;
11394 gimple_omp_for_set_incr (gforo, i, t);
11395 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11396 OMP_CLAUSE_DECL (t) = v;
11397 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
11398 gimple_omp_for_set_clauses (gforo, t);
11400 gimplify_seq_add_stmt (pre_p, gforo);
11402 else
11403 gimplify_seq_add_stmt (pre_p, gfor);
11404 if (ret != GS_ALL_DONE)
11405 return GS_ERROR;
11406 *expr_p = NULL_TREE;
11407 return GS_ALL_DONE;
11410 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
11411 of OMP_TARGET's body. */
11413 static tree
11414 find_omp_teams (tree *tp, int *walk_subtrees, void *)
11416 *walk_subtrees = 0;
11417 switch (TREE_CODE (*tp))
11419 case OMP_TEAMS:
11420 return *tp;
11421 case BIND_EXPR:
11422 case STATEMENT_LIST:
11423 *walk_subtrees = 1;
11424 break;
11425 default:
11426 break;
11428 return NULL_TREE;
11431 /* Helper function of optimize_target_teams, determine if the expression
11432 can be computed safely before the target construct on the host. */
11434 static tree
11435 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
11437 splay_tree_node n;
11439 if (TYPE_P (*tp))
11441 *walk_subtrees = 0;
11442 return NULL_TREE;
11444 switch (TREE_CODE (*tp))
11446 case VAR_DECL:
11447 case PARM_DECL:
11448 case RESULT_DECL:
11449 *walk_subtrees = 0;
11450 if (error_operand_p (*tp)
11451 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
11452 || DECL_HAS_VALUE_EXPR_P (*tp)
11453 || DECL_THREAD_LOCAL_P (*tp)
11454 || TREE_SIDE_EFFECTS (*tp)
11455 || TREE_THIS_VOLATILE (*tp))
11456 return *tp;
11457 if (is_global_var (*tp)
11458 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
11459 || lookup_attribute ("omp declare target link",
11460 DECL_ATTRIBUTES (*tp))))
11461 return *tp;
11462 if (VAR_P (*tp)
11463 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
11464 && !is_global_var (*tp)
11465 && decl_function_context (*tp) == current_function_decl)
11466 return *tp;
11467 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
11468 (splay_tree_key) *tp);
11469 if (n == NULL)
11471 if (gimplify_omp_ctxp->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
11472 return NULL_TREE;
11473 return *tp;
11475 else if (n->value & GOVD_LOCAL)
11476 return *tp;
11477 else if (n->value & GOVD_FIRSTPRIVATE)
11478 return NULL_TREE;
11479 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
11480 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
11481 return NULL_TREE;
11482 return *tp;
11483 case INTEGER_CST:
11484 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
11485 return *tp;
11486 return NULL_TREE;
11487 case TARGET_EXPR:
11488 if (TARGET_EXPR_INITIAL (*tp)
11489 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
11490 return *tp;
11491 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
11492 walk_subtrees, NULL);
11493 /* Allow some reasonable subset of integral arithmetics. */
11494 case PLUS_EXPR:
11495 case MINUS_EXPR:
11496 case MULT_EXPR:
11497 case TRUNC_DIV_EXPR:
11498 case CEIL_DIV_EXPR:
11499 case FLOOR_DIV_EXPR:
11500 case ROUND_DIV_EXPR:
11501 case TRUNC_MOD_EXPR:
11502 case CEIL_MOD_EXPR:
11503 case FLOOR_MOD_EXPR:
11504 case ROUND_MOD_EXPR:
11505 case RDIV_EXPR:
11506 case EXACT_DIV_EXPR:
11507 case MIN_EXPR:
11508 case MAX_EXPR:
11509 case LSHIFT_EXPR:
11510 case RSHIFT_EXPR:
11511 case BIT_IOR_EXPR:
11512 case BIT_XOR_EXPR:
11513 case BIT_AND_EXPR:
11514 case NEGATE_EXPR:
11515 case ABS_EXPR:
11516 case BIT_NOT_EXPR:
11517 case NON_LVALUE_EXPR:
11518 CASE_CONVERT:
11519 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
11520 return *tp;
11521 return NULL_TREE;
11522 /* And disallow anything else, except for comparisons. */
11523 default:
11524 if (COMPARISON_CLASS_P (*tp))
11525 return NULL_TREE;
11526 return *tp;
11530 /* Try to determine if the num_teams and/or thread_limit expressions
11531 can have their values determined already before entering the
11532 target construct.
11533 INTEGER_CSTs trivially are,
11534 integral decls that are firstprivate (explicitly or implicitly)
11535 or explicitly map(always, to:) or map(always, tofrom:) on the target
11536 region too, and expressions involving simple arithmetics on those
11537 too, function calls are not ok, dereferencing something neither etc.
11538 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
11539 EXPR based on what we find:
11540 0 stands for clause not specified at all, use implementation default
11541 -1 stands for value that can't be determined easily before entering
11542 the target construct.
11543 If teams construct is not present at all, use 1 for num_teams
11544 and 0 for thread_limit (only one team is involved, and the thread
11545 limit is implementation defined. */
11547 static void
11548 optimize_target_teams (tree target, gimple_seq *pre_p)
11550 tree body = OMP_BODY (target);
11551 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
11552 tree num_teams = integer_zero_node;
11553 tree thread_limit = integer_zero_node;
11554 location_t num_teams_loc = EXPR_LOCATION (target);
11555 location_t thread_limit_loc = EXPR_LOCATION (target);
11556 tree c, *p, expr;
11557 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
11559 if (teams == NULL_TREE)
11560 num_teams = integer_one_node;
11561 else
11562 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
11564 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
11566 p = &num_teams;
11567 num_teams_loc = OMP_CLAUSE_LOCATION (c);
11569 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
11571 p = &thread_limit;
11572 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
11574 else
11575 continue;
11576 expr = OMP_CLAUSE_OPERAND (c, 0);
11577 if (TREE_CODE (expr) == INTEGER_CST)
11579 *p = expr;
11580 continue;
11582 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
11584 *p = integer_minus_one_node;
11585 continue;
11587 *p = expr;
11588 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
11589 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
11590 == GS_ERROR)
11592 gimplify_omp_ctxp = target_ctx;
11593 *p = integer_minus_one_node;
11594 continue;
11596 gimplify_omp_ctxp = target_ctx;
11597 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
11598 OMP_CLAUSE_OPERAND (c, 0) = *p;
11600 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11601 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
11602 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
11603 OMP_TARGET_CLAUSES (target) = c;
11604 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
11605 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
11606 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
11607 OMP_TARGET_CLAUSES (target) = c;
11610 /* Gimplify the gross structure of several OMP constructs. */
11612 static void
11613 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
11615 tree expr = *expr_p;
11616 gimple *stmt;
11617 gimple_seq body = NULL;
11618 enum omp_region_type ort;
11620 switch (TREE_CODE (expr))
11622 case OMP_SECTIONS:
11623 case OMP_SINGLE:
11624 ort = ORT_WORKSHARE;
11625 break;
11626 case OMP_TARGET:
11627 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
11628 break;
11629 case OACC_KERNELS:
11630 ort = ORT_ACC_KERNELS;
11631 break;
11632 case OACC_PARALLEL:
11633 ort = ORT_ACC_PARALLEL;
11634 break;
11635 case OACC_DATA:
11636 ort = ORT_ACC_DATA;
11637 break;
11638 case OMP_TARGET_DATA:
11639 ort = ORT_TARGET_DATA;
11640 break;
11641 case OMP_TEAMS:
11642 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
11643 if (gimplify_omp_ctxp == NULL
11644 || (gimplify_omp_ctxp->region_type == ORT_TARGET
11645 && gimplify_omp_ctxp->outer_context == NULL
11646 && lookup_attribute ("omp declare target",
11647 DECL_ATTRIBUTES (current_function_decl))))
11648 ort = (enum omp_region_type) (ort | ORT_HOST_TEAMS);
11649 break;
11650 case OACC_HOST_DATA:
11651 ort = ORT_ACC_HOST_DATA;
11652 break;
11653 default:
11654 gcc_unreachable ();
11656 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
11657 TREE_CODE (expr));
11658 if (TREE_CODE (expr) == OMP_TARGET)
11659 optimize_target_teams (expr, pre_p);
11660 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0
11661 || (ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
11663 push_gimplify_context ();
11664 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
11665 if (gimple_code (g) == GIMPLE_BIND)
11666 pop_gimplify_context (g);
11667 else
11668 pop_gimplify_context (NULL);
11669 if ((ort & ORT_TARGET_DATA) != 0)
11671 enum built_in_function end_ix;
11672 switch (TREE_CODE (expr))
11674 case OACC_DATA:
11675 case OACC_HOST_DATA:
11676 end_ix = BUILT_IN_GOACC_DATA_END;
11677 break;
11678 case OMP_TARGET_DATA:
11679 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
11680 break;
11681 default:
11682 gcc_unreachable ();
11684 tree fn = builtin_decl_explicit (end_ix);
11685 g = gimple_build_call (fn, 0);
11686 gimple_seq cleanup = NULL;
11687 gimple_seq_add_stmt (&cleanup, g);
11688 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
11689 body = NULL;
11690 gimple_seq_add_stmt (&body, g);
11693 else
11694 gimplify_and_add (OMP_BODY (expr), &body);
11695 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
11696 TREE_CODE (expr));
11698 switch (TREE_CODE (expr))
11700 case OACC_DATA:
11701 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
11702 OMP_CLAUSES (expr));
11703 break;
11704 case OACC_KERNELS:
11705 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
11706 OMP_CLAUSES (expr));
11707 break;
11708 case OACC_HOST_DATA:
11709 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
11710 OMP_CLAUSES (expr));
11711 break;
11712 case OACC_PARALLEL:
11713 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
11714 OMP_CLAUSES (expr));
11715 break;
11716 case OMP_SECTIONS:
11717 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
11718 break;
11719 case OMP_SINGLE:
11720 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
11721 break;
11722 case OMP_TARGET:
11723 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
11724 OMP_CLAUSES (expr));
11725 break;
11726 case OMP_TARGET_DATA:
11727 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
11728 OMP_CLAUSES (expr));
11729 break;
11730 case OMP_TEAMS:
11731 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
11732 if ((ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
11733 gimple_omp_teams_set_host (as_a <gomp_teams *> (stmt), true);
11734 break;
11735 default:
11736 gcc_unreachable ();
11739 gimplify_seq_add_stmt (pre_p, stmt);
11740 *expr_p = NULL_TREE;
11743 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
11744 target update constructs. */
11746 static void
11747 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
11749 tree expr = *expr_p;
11750 int kind;
11751 gomp_target *stmt;
11752 enum omp_region_type ort = ORT_WORKSHARE;
11754 switch (TREE_CODE (expr))
11756 case OACC_ENTER_DATA:
11757 case OACC_EXIT_DATA:
11758 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
11759 ort = ORT_ACC;
11760 break;
11761 case OACC_UPDATE:
11762 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
11763 ort = ORT_ACC;
11764 break;
11765 case OMP_TARGET_UPDATE:
11766 kind = GF_OMP_TARGET_KIND_UPDATE;
11767 break;
11768 case OMP_TARGET_ENTER_DATA:
11769 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
11770 break;
11771 case OMP_TARGET_EXIT_DATA:
11772 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
11773 break;
11774 default:
11775 gcc_unreachable ();
11777 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
11778 ort, TREE_CODE (expr));
11779 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
11780 TREE_CODE (expr));
11781 if (TREE_CODE (expr) == OACC_UPDATE
11782 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
11783 OMP_CLAUSE_IF_PRESENT))
11785 /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
11786 clause. */
11787 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11788 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
11789 switch (OMP_CLAUSE_MAP_KIND (c))
11791 case GOMP_MAP_FORCE_TO:
11792 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
11793 break;
11794 case GOMP_MAP_FORCE_FROM:
11795 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
11796 break;
11797 default:
11798 break;
11801 else if (TREE_CODE (expr) == OACC_EXIT_DATA
11802 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
11803 OMP_CLAUSE_FINALIZE))
11805 /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote that "finalize"
11806 semantics apply to all mappings of this OpenACC directive. */
11807 bool finalize_marked = false;
11808 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11809 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
11810 switch (OMP_CLAUSE_MAP_KIND (c))
11812 case GOMP_MAP_FROM:
11813 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
11814 finalize_marked = true;
11815 break;
11816 case GOMP_MAP_RELEASE:
11817 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
11818 finalize_marked = true;
11819 break;
11820 default:
11821 /* Check consistency: libgomp relies on the very first data
11822 mapping clause being marked, so make sure we did that before
11823 any other mapping clauses. */
11824 gcc_assert (finalize_marked);
11825 break;
11828 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
11830 gimplify_seq_add_stmt (pre_p, stmt);
11831 *expr_p = NULL_TREE;
11834 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
11835 stabilized the lhs of the atomic operation as *ADDR. Return true if
11836 EXPR is this stabilized form. */
11838 static bool
11839 goa_lhs_expr_p (tree expr, tree addr)
11841 /* Also include casts to other type variants. The C front end is fond
11842 of adding these for e.g. volatile variables. This is like
11843 STRIP_TYPE_NOPS but includes the main variant lookup. */
11844 STRIP_USELESS_TYPE_CONVERSION (expr);
11846 if (TREE_CODE (expr) == INDIRECT_REF)
11848 expr = TREE_OPERAND (expr, 0);
11849 while (expr != addr
11850 && (CONVERT_EXPR_P (expr)
11851 || TREE_CODE (expr) == NON_LVALUE_EXPR)
11852 && TREE_CODE (expr) == TREE_CODE (addr)
11853 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
11855 expr = TREE_OPERAND (expr, 0);
11856 addr = TREE_OPERAND (addr, 0);
11858 if (expr == addr)
11859 return true;
11860 return (TREE_CODE (addr) == ADDR_EXPR
11861 && TREE_CODE (expr) == ADDR_EXPR
11862 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
11864 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
11865 return true;
11866 return false;
11869 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
11870 expression does not involve the lhs, evaluate it into a temporary.
11871 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
11872 or -1 if an error was encountered. */
11874 static int
11875 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
11876 tree lhs_var)
11878 tree expr = *expr_p;
11879 int saw_lhs;
11881 if (goa_lhs_expr_p (expr, lhs_addr))
11883 *expr_p = lhs_var;
11884 return 1;
11886 if (is_gimple_val (expr))
11887 return 0;
11889 saw_lhs = 0;
11890 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
11892 case tcc_binary:
11893 case tcc_comparison:
11894 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
11895 lhs_var);
11896 /* FALLTHRU */
11897 case tcc_unary:
11898 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
11899 lhs_var);
11900 break;
11901 case tcc_expression:
11902 switch (TREE_CODE (expr))
11904 case TRUTH_ANDIF_EXPR:
11905 case TRUTH_ORIF_EXPR:
11906 case TRUTH_AND_EXPR:
11907 case TRUTH_OR_EXPR:
11908 case TRUTH_XOR_EXPR:
11909 case BIT_INSERT_EXPR:
11910 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
11911 lhs_addr, lhs_var);
11912 /* FALLTHRU */
11913 case TRUTH_NOT_EXPR:
11914 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11915 lhs_addr, lhs_var);
11916 break;
11917 case COMPOUND_EXPR:
11918 /* Break out any preevaluations from cp_build_modify_expr. */
11919 for (; TREE_CODE (expr) == COMPOUND_EXPR;
11920 expr = TREE_OPERAND (expr, 1))
11921 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
11922 *expr_p = expr;
11923 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
11924 default:
11925 break;
11927 break;
11928 case tcc_reference:
11929 if (TREE_CODE (expr) == BIT_FIELD_REF)
11930 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11931 lhs_addr, lhs_var);
11932 break;
11933 default:
11934 break;
11937 if (saw_lhs == 0)
11939 enum gimplify_status gs;
11940 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
11941 if (gs != GS_ALL_DONE)
11942 saw_lhs = -1;
11945 return saw_lhs;
11948 /* Gimplify an OMP_ATOMIC statement. */
11950 static enum gimplify_status
11951 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
11953 tree addr = TREE_OPERAND (*expr_p, 0);
11954 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
11955 ? NULL : TREE_OPERAND (*expr_p, 1);
11956 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
11957 tree tmp_load;
11958 gomp_atomic_load *loadstmt;
11959 gomp_atomic_store *storestmt;
11961 tmp_load = create_tmp_reg (type);
11962 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
11963 return GS_ERROR;
11965 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
11966 != GS_ALL_DONE)
11967 return GS_ERROR;
11969 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr,
11970 OMP_ATOMIC_MEMORY_ORDER (*expr_p));
11971 gimplify_seq_add_stmt (pre_p, loadstmt);
11972 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
11973 != GS_ALL_DONE)
11974 return GS_ERROR;
11976 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
11977 rhs = tmp_load;
11978 storestmt
11979 = gimple_build_omp_atomic_store (rhs, OMP_ATOMIC_MEMORY_ORDER (*expr_p));
11980 gimplify_seq_add_stmt (pre_p, storestmt);
11981 switch (TREE_CODE (*expr_p))
11983 case OMP_ATOMIC_READ:
11984 case OMP_ATOMIC_CAPTURE_OLD:
11985 *expr_p = tmp_load;
11986 gimple_omp_atomic_set_need_value (loadstmt);
11987 break;
11988 case OMP_ATOMIC_CAPTURE_NEW:
11989 *expr_p = rhs;
11990 gimple_omp_atomic_set_need_value (storestmt);
11991 break;
11992 default:
11993 *expr_p = NULL;
11994 break;
11997 return GS_ALL_DONE;
12000 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
12001 body, and adding some EH bits. */
12003 static enum gimplify_status
12004 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
12006 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
12007 gimple *body_stmt;
12008 gtransaction *trans_stmt;
12009 gimple_seq body = NULL;
12010 int subcode = 0;
12012 /* Wrap the transaction body in a BIND_EXPR so we have a context
12013 where to put decls for OMP. */
12014 if (TREE_CODE (tbody) != BIND_EXPR)
12016 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
12017 TREE_SIDE_EFFECTS (bind) = 1;
12018 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
12019 TRANSACTION_EXPR_BODY (expr) = bind;
12022 push_gimplify_context ();
12023 temp = voidify_wrapper_expr (*expr_p, NULL);
12025 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
12026 pop_gimplify_context (body_stmt);
12028 trans_stmt = gimple_build_transaction (body);
12029 if (TRANSACTION_EXPR_OUTER (expr))
12030 subcode = GTMA_IS_OUTER;
12031 else if (TRANSACTION_EXPR_RELAXED (expr))
12032 subcode = GTMA_IS_RELAXED;
12033 gimple_transaction_set_subcode (trans_stmt, subcode);
12035 gimplify_seq_add_stmt (pre_p, trans_stmt);
12037 if (temp)
12039 *expr_p = temp;
12040 return GS_OK;
12043 *expr_p = NULL_TREE;
12044 return GS_ALL_DONE;
12047 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
12048 is the OMP_BODY of the original EXPR (which has already been
12049 gimplified so it's not present in the EXPR).
12051 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
12053 static gimple *
12054 gimplify_omp_ordered (tree expr, gimple_seq body)
12056 tree c, decls;
12057 int failures = 0;
12058 unsigned int i;
12059 tree source_c = NULL_TREE;
12060 tree sink_c = NULL_TREE;
12062 if (gimplify_omp_ctxp)
12064 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
12065 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12066 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
12067 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
12068 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
12070 error_at (OMP_CLAUSE_LOCATION (c),
12071 "%<ordered%> construct with %<depend%> clause must be "
12072 "closely nested inside a loop with %<ordered%> clause "
12073 "with a parameter");
12074 failures++;
12076 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12077 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
12079 bool fail = false;
12080 for (decls = OMP_CLAUSE_DECL (c), i = 0;
12081 decls && TREE_CODE (decls) == TREE_LIST;
12082 decls = TREE_CHAIN (decls), ++i)
12083 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
12084 continue;
12085 else if (TREE_VALUE (decls)
12086 != gimplify_omp_ctxp->loop_iter_var[2 * i])
12088 error_at (OMP_CLAUSE_LOCATION (c),
12089 "variable %qE is not an iteration "
12090 "of outermost loop %d, expected %qE",
12091 TREE_VALUE (decls), i + 1,
12092 gimplify_omp_ctxp->loop_iter_var[2 * i]);
12093 fail = true;
12094 failures++;
12096 else
12097 TREE_VALUE (decls)
12098 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
12099 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
12101 error_at (OMP_CLAUSE_LOCATION (c),
12102 "number of variables in %<depend(sink)%> "
12103 "clause does not match number of "
12104 "iteration variables");
12105 failures++;
12107 sink_c = c;
12109 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12110 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
12112 if (source_c)
12114 error_at (OMP_CLAUSE_LOCATION (c),
12115 "more than one %<depend(source)%> clause on an "
12116 "%<ordered%> construct");
12117 failures++;
12119 else
12120 source_c = c;
12123 if (source_c && sink_c)
12125 error_at (OMP_CLAUSE_LOCATION (source_c),
12126 "%<depend(source)%> clause specified together with "
12127 "%<depend(sink:)%> clauses on the same construct");
12128 failures++;
12131 if (failures)
12132 return gimple_build_nop ();
12133 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
12136 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
12137 expression produces a value to be used as an operand inside a GIMPLE
12138 statement, the value will be stored back in *EXPR_P. This value will
12139 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
12140 an SSA_NAME. The corresponding sequence of GIMPLE statements is
12141 emitted in PRE_P and POST_P.
12143 Additionally, this process may overwrite parts of the input
12144 expression during gimplification. Ideally, it should be
12145 possible to do non-destructive gimplification.
12147 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
12148 the expression needs to evaluate to a value to be used as
12149 an operand in a GIMPLE statement, this value will be stored in
12150 *EXPR_P on exit. This happens when the caller specifies one
12151 of fb_lvalue or fb_rvalue fallback flags.
12153 PRE_P will contain the sequence of GIMPLE statements corresponding
12154 to the evaluation of EXPR and all the side-effects that must
12155 be executed before the main expression. On exit, the last
12156 statement of PRE_P is the core statement being gimplified. For
12157 instance, when gimplifying 'if (++a)' the last statement in
12158 PRE_P will be 'if (t.1)' where t.1 is the result of
12159 pre-incrementing 'a'.
12161 POST_P will contain the sequence of GIMPLE statements corresponding
12162 to the evaluation of all the side-effects that must be executed
12163 after the main expression. If this is NULL, the post
12164 side-effects are stored at the end of PRE_P.
12166 The reason why the output is split in two is to handle post
12167 side-effects explicitly. In some cases, an expression may have
12168 inner and outer post side-effects which need to be emitted in
12169 an order different from the one given by the recursive
12170 traversal. For instance, for the expression (*p--)++ the post
12171 side-effects of '--' must actually occur *after* the post
12172 side-effects of '++'. However, gimplification will first visit
12173 the inner expression, so if a separate POST sequence was not
12174 used, the resulting sequence would be:
12176 1 t.1 = *p
12177 2 p = p - 1
12178 3 t.2 = t.1 + 1
12179 4 *p = t.2
12181 However, the post-decrement operation in line #2 must not be
12182 evaluated until after the store to *p at line #4, so the
12183 correct sequence should be:
12185 1 t.1 = *p
12186 2 t.2 = t.1 + 1
12187 3 *p = t.2
12188 4 p = p - 1
12190 So, by specifying a separate post queue, it is possible
12191 to emit the post side-effects in the correct order.
12192 If POST_P is NULL, an internal queue will be used. Before
12193 returning to the caller, the sequence POST_P is appended to
12194 the main output sequence PRE_P.
12196 GIMPLE_TEST_F points to a function that takes a tree T and
12197 returns nonzero if T is in the GIMPLE form requested by the
12198 caller. The GIMPLE predicates are in gimple.c.
12200 FALLBACK tells the function what sort of a temporary we want if
12201 gimplification cannot produce an expression that complies with
12202 GIMPLE_TEST_F.
12204 fb_none means that no temporary should be generated
12205 fb_rvalue means that an rvalue is OK to generate
12206 fb_lvalue means that an lvalue is OK to generate
12207 fb_either means that either is OK, but an lvalue is preferable.
12208 fb_mayfail means that gimplification may fail (in which case
12209 GS_ERROR will be returned)
12211 The return value is either GS_ERROR or GS_ALL_DONE, since this
12212 function iterates until EXPR is completely gimplified or an error
12213 occurs. */
12215 enum gimplify_status
12216 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12217 bool (*gimple_test_f) (tree), fallback_t fallback)
12219 tree tmp;
12220 gimple_seq internal_pre = NULL;
12221 gimple_seq internal_post = NULL;
12222 tree save_expr;
12223 bool is_statement;
12224 location_t saved_location;
12225 enum gimplify_status ret;
12226 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
12227 tree label;
12229 save_expr = *expr_p;
12230 if (save_expr == NULL_TREE)
12231 return GS_ALL_DONE;
12233 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
12234 is_statement = gimple_test_f == is_gimple_stmt;
12235 if (is_statement)
12236 gcc_assert (pre_p);
12238 /* Consistency checks. */
12239 if (gimple_test_f == is_gimple_reg)
12240 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
12241 else if (gimple_test_f == is_gimple_val
12242 || gimple_test_f == is_gimple_call_addr
12243 || gimple_test_f == is_gimple_condexpr
12244 || gimple_test_f == is_gimple_mem_rhs
12245 || gimple_test_f == is_gimple_mem_rhs_or_call
12246 || gimple_test_f == is_gimple_reg_rhs
12247 || gimple_test_f == is_gimple_reg_rhs_or_call
12248 || gimple_test_f == is_gimple_asm_val
12249 || gimple_test_f == is_gimple_mem_ref_addr)
12250 gcc_assert (fallback & fb_rvalue);
12251 else if (gimple_test_f == is_gimple_min_lval
12252 || gimple_test_f == is_gimple_lvalue)
12253 gcc_assert (fallback & fb_lvalue);
12254 else if (gimple_test_f == is_gimple_addressable)
12255 gcc_assert (fallback & fb_either);
12256 else if (gimple_test_f == is_gimple_stmt)
12257 gcc_assert (fallback == fb_none);
12258 else
12260 /* We should have recognized the GIMPLE_TEST_F predicate to
12261 know what kind of fallback to use in case a temporary is
12262 needed to hold the value or address of *EXPR_P. */
12263 gcc_unreachable ();
12266 /* We used to check the predicate here and return immediately if it
12267 succeeds. This is wrong; the design is for gimplification to be
12268 idempotent, and for the predicates to only test for valid forms, not
12269 whether they are fully simplified. */
12270 if (pre_p == NULL)
12271 pre_p = &internal_pre;
12273 if (post_p == NULL)
12274 post_p = &internal_post;
12276 /* Remember the last statements added to PRE_P and POST_P. Every
12277 new statement added by the gimplification helpers needs to be
12278 annotated with location information. To centralize the
12279 responsibility, we remember the last statement that had been
12280 added to both queues before gimplifying *EXPR_P. If
12281 gimplification produces new statements in PRE_P and POST_P, those
12282 statements will be annotated with the same location information
12283 as *EXPR_P. */
12284 pre_last_gsi = gsi_last (*pre_p);
12285 post_last_gsi = gsi_last (*post_p);
12287 saved_location = input_location;
12288 if (save_expr != error_mark_node
12289 && EXPR_HAS_LOCATION (*expr_p))
12290 input_location = EXPR_LOCATION (*expr_p);
12292 /* Loop over the specific gimplifiers until the toplevel node
12293 remains the same. */
12296 /* Strip away as many useless type conversions as possible
12297 at the toplevel. */
12298 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
12300 /* Remember the expr. */
12301 save_expr = *expr_p;
12303 /* Die, die, die, my darling. */
12304 if (error_operand_p (save_expr))
12306 ret = GS_ERROR;
12307 break;
12310 /* Do any language-specific gimplification. */
12311 ret = ((enum gimplify_status)
12312 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
12313 if (ret == GS_OK)
12315 if (*expr_p == NULL_TREE)
12316 break;
12317 if (*expr_p != save_expr)
12318 continue;
12320 else if (ret != GS_UNHANDLED)
12321 break;
12323 /* Make sure that all the cases set 'ret' appropriately. */
12324 ret = GS_UNHANDLED;
12325 switch (TREE_CODE (*expr_p))
12327 /* First deal with the special cases. */
12329 case POSTINCREMENT_EXPR:
12330 case POSTDECREMENT_EXPR:
12331 case PREINCREMENT_EXPR:
12332 case PREDECREMENT_EXPR:
12333 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
12334 fallback != fb_none,
12335 TREE_TYPE (*expr_p));
12336 break;
12338 case VIEW_CONVERT_EXPR:
12339 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
12340 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
12342 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12343 post_p, is_gimple_val, fb_rvalue);
12344 recalculate_side_effects (*expr_p);
12345 break;
12347 /* Fallthru. */
12349 case ARRAY_REF:
12350 case ARRAY_RANGE_REF:
12351 case REALPART_EXPR:
12352 case IMAGPART_EXPR:
12353 case COMPONENT_REF:
12354 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
12355 fallback ? fallback : fb_rvalue);
12356 break;
12358 case COND_EXPR:
12359 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
12361 /* C99 code may assign to an array in a structure value of a
12362 conditional expression, and this has undefined behavior
12363 only on execution, so create a temporary if an lvalue is
12364 required. */
12365 if (fallback == fb_lvalue)
12367 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
12368 mark_addressable (*expr_p);
12369 ret = GS_OK;
12371 break;
12373 case CALL_EXPR:
12374 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
12376 /* C99 code may assign to an array in a structure returned
12377 from a function, and this has undefined behavior only on
12378 execution, so create a temporary if an lvalue is
12379 required. */
12380 if (fallback == fb_lvalue)
12382 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
12383 mark_addressable (*expr_p);
12384 ret = GS_OK;
12386 break;
12388 case TREE_LIST:
12389 gcc_unreachable ();
12391 case COMPOUND_EXPR:
12392 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
12393 break;
12395 case COMPOUND_LITERAL_EXPR:
12396 ret = gimplify_compound_literal_expr (expr_p, pre_p,
12397 gimple_test_f, fallback);
12398 break;
12400 case MODIFY_EXPR:
12401 case INIT_EXPR:
12402 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
12403 fallback != fb_none);
12404 break;
12406 case TRUTH_ANDIF_EXPR:
12407 case TRUTH_ORIF_EXPR:
12409 /* Preserve the original type of the expression and the
12410 source location of the outer expression. */
12411 tree org_type = TREE_TYPE (*expr_p);
12412 *expr_p = gimple_boolify (*expr_p);
12413 *expr_p = build3_loc (input_location, COND_EXPR,
12414 org_type, *expr_p,
12415 fold_convert_loc
12416 (input_location,
12417 org_type, boolean_true_node),
12418 fold_convert_loc
12419 (input_location,
12420 org_type, boolean_false_node));
12421 ret = GS_OK;
12422 break;
12425 case TRUTH_NOT_EXPR:
12427 tree type = TREE_TYPE (*expr_p);
12428 /* The parsers are careful to generate TRUTH_NOT_EXPR
12429 only with operands that are always zero or one.
12430 We do not fold here but handle the only interesting case
12431 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
12432 *expr_p = gimple_boolify (*expr_p);
12433 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
12434 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
12435 TREE_TYPE (*expr_p),
12436 TREE_OPERAND (*expr_p, 0));
12437 else
12438 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
12439 TREE_TYPE (*expr_p),
12440 TREE_OPERAND (*expr_p, 0),
12441 build_int_cst (TREE_TYPE (*expr_p), 1));
12442 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
12443 *expr_p = fold_convert_loc (input_location, type, *expr_p);
12444 ret = GS_OK;
12445 break;
12448 case ADDR_EXPR:
12449 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
12450 break;
12452 case ANNOTATE_EXPR:
12454 tree cond = TREE_OPERAND (*expr_p, 0);
12455 tree kind = TREE_OPERAND (*expr_p, 1);
12456 tree data = TREE_OPERAND (*expr_p, 2);
12457 tree type = TREE_TYPE (cond);
12458 if (!INTEGRAL_TYPE_P (type))
12460 *expr_p = cond;
12461 ret = GS_OK;
12462 break;
12464 tree tmp = create_tmp_var (type);
12465 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
12466 gcall *call
12467 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
12468 gimple_call_set_lhs (call, tmp);
12469 gimplify_seq_add_stmt (pre_p, call);
12470 *expr_p = tmp;
12471 ret = GS_ALL_DONE;
12472 break;
12475 case VA_ARG_EXPR:
12476 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
12477 break;
12479 CASE_CONVERT:
12480 if (IS_EMPTY_STMT (*expr_p))
12482 ret = GS_ALL_DONE;
12483 break;
12486 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
12487 || fallback == fb_none)
12489 /* Just strip a conversion to void (or in void context) and
12490 try again. */
12491 *expr_p = TREE_OPERAND (*expr_p, 0);
12492 ret = GS_OK;
12493 break;
12496 ret = gimplify_conversion (expr_p);
12497 if (ret == GS_ERROR)
12498 break;
12499 if (*expr_p != save_expr)
12500 break;
12501 /* FALLTHRU */
12503 case FIX_TRUNC_EXPR:
12504 /* unary_expr: ... | '(' cast ')' val | ... */
12505 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12506 is_gimple_val, fb_rvalue);
12507 recalculate_side_effects (*expr_p);
12508 break;
12510 case INDIRECT_REF:
12512 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
12513 bool notrap = TREE_THIS_NOTRAP (*expr_p);
12514 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
12516 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
12517 if (*expr_p != save_expr)
12519 ret = GS_OK;
12520 break;
12523 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12524 is_gimple_reg, fb_rvalue);
12525 if (ret == GS_ERROR)
12526 break;
12528 recalculate_side_effects (*expr_p);
12529 *expr_p = fold_build2_loc (input_location, MEM_REF,
12530 TREE_TYPE (*expr_p),
12531 TREE_OPERAND (*expr_p, 0),
12532 build_int_cst (saved_ptr_type, 0));
12533 TREE_THIS_VOLATILE (*expr_p) = volatilep;
12534 TREE_THIS_NOTRAP (*expr_p) = notrap;
12535 ret = GS_OK;
12536 break;
12539 /* We arrive here through the various re-gimplifcation paths. */
12540 case MEM_REF:
12541 /* First try re-folding the whole thing. */
12542 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
12543 TREE_OPERAND (*expr_p, 0),
12544 TREE_OPERAND (*expr_p, 1));
12545 if (tmp)
12547 REF_REVERSE_STORAGE_ORDER (tmp)
12548 = REF_REVERSE_STORAGE_ORDER (*expr_p);
12549 *expr_p = tmp;
12550 recalculate_side_effects (*expr_p);
12551 ret = GS_OK;
12552 break;
12554 /* Avoid re-gimplifying the address operand if it is already
12555 in suitable form. Re-gimplifying would mark the address
12556 operand addressable. Always gimplify when not in SSA form
12557 as we still may have to gimplify decls with value-exprs. */
12558 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
12559 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
12561 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12562 is_gimple_mem_ref_addr, fb_rvalue);
12563 if (ret == GS_ERROR)
12564 break;
12566 recalculate_side_effects (*expr_p);
12567 ret = GS_ALL_DONE;
12568 break;
12570 /* Constants need not be gimplified. */
12571 case INTEGER_CST:
12572 case REAL_CST:
12573 case FIXED_CST:
12574 case STRING_CST:
12575 case COMPLEX_CST:
12576 case VECTOR_CST:
12577 /* Drop the overflow flag on constants, we do not want
12578 that in the GIMPLE IL. */
12579 if (TREE_OVERFLOW_P (*expr_p))
12580 *expr_p = drop_tree_overflow (*expr_p);
12581 ret = GS_ALL_DONE;
12582 break;
12584 case CONST_DECL:
12585 /* If we require an lvalue, such as for ADDR_EXPR, retain the
12586 CONST_DECL node. Otherwise the decl is replaceable by its
12587 value. */
12588 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
12589 if (fallback & fb_lvalue)
12590 ret = GS_ALL_DONE;
12591 else
12593 *expr_p = DECL_INITIAL (*expr_p);
12594 ret = GS_OK;
12596 break;
12598 case DECL_EXPR:
12599 ret = gimplify_decl_expr (expr_p, pre_p);
12600 break;
12602 case BIND_EXPR:
12603 ret = gimplify_bind_expr (expr_p, pre_p);
12604 break;
12606 case LOOP_EXPR:
12607 ret = gimplify_loop_expr (expr_p, pre_p);
12608 break;
12610 case SWITCH_EXPR:
12611 ret = gimplify_switch_expr (expr_p, pre_p);
12612 break;
12614 case EXIT_EXPR:
12615 ret = gimplify_exit_expr (expr_p);
12616 break;
12618 case GOTO_EXPR:
12619 /* If the target is not LABEL, then it is a computed jump
12620 and the target needs to be gimplified. */
12621 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
12623 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
12624 NULL, is_gimple_val, fb_rvalue);
12625 if (ret == GS_ERROR)
12626 break;
12628 gimplify_seq_add_stmt (pre_p,
12629 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
12630 ret = GS_ALL_DONE;
12631 break;
12633 case PREDICT_EXPR:
12634 gimplify_seq_add_stmt (pre_p,
12635 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
12636 PREDICT_EXPR_OUTCOME (*expr_p)));
12637 ret = GS_ALL_DONE;
12638 break;
12640 case LABEL_EXPR:
12641 ret = gimplify_label_expr (expr_p, pre_p);
12642 label = LABEL_EXPR_LABEL (*expr_p);
12643 gcc_assert (decl_function_context (label) == current_function_decl);
12645 /* If the label is used in a goto statement, or address of the label
12646 is taken, we need to unpoison all variables that were seen so far.
12647 Doing so would prevent us from reporting a false positives. */
12648 if (asan_poisoned_variables
12649 && asan_used_labels != NULL
12650 && asan_used_labels->contains (label))
12651 asan_poison_variables (asan_poisoned_variables, false, pre_p);
12652 break;
12654 case CASE_LABEL_EXPR:
12655 ret = gimplify_case_label_expr (expr_p, pre_p);
12657 if (gimplify_ctxp->live_switch_vars)
12658 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
12659 pre_p);
12660 break;
12662 case RETURN_EXPR:
12663 ret = gimplify_return_expr (*expr_p, pre_p);
12664 break;
12666 case CONSTRUCTOR:
12667 /* Don't reduce this in place; let gimplify_init_constructor work its
12668 magic. Buf if we're just elaborating this for side effects, just
12669 gimplify any element that has side-effects. */
12670 if (fallback == fb_none)
12672 unsigned HOST_WIDE_INT ix;
12673 tree val;
12674 tree temp = NULL_TREE;
12675 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
12676 if (TREE_SIDE_EFFECTS (val))
12677 append_to_statement_list (val, &temp);
12679 *expr_p = temp;
12680 ret = temp ? GS_OK : GS_ALL_DONE;
12682 /* C99 code may assign to an array in a constructed
12683 structure or union, and this has undefined behavior only
12684 on execution, so create a temporary if an lvalue is
12685 required. */
12686 else if (fallback == fb_lvalue)
12688 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
12689 mark_addressable (*expr_p);
12690 ret = GS_OK;
12692 else
12693 ret = GS_ALL_DONE;
12694 break;
12696 /* The following are special cases that are not handled by the
12697 original GIMPLE grammar. */
12699 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
12700 eliminated. */
12701 case SAVE_EXPR:
12702 ret = gimplify_save_expr (expr_p, pre_p, post_p);
12703 break;
12705 case BIT_FIELD_REF:
12706 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12707 post_p, is_gimple_lvalue, fb_either);
12708 recalculate_side_effects (*expr_p);
12709 break;
12711 case TARGET_MEM_REF:
12713 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
12715 if (TMR_BASE (*expr_p))
12716 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
12717 post_p, is_gimple_mem_ref_addr, fb_either);
12718 if (TMR_INDEX (*expr_p))
12719 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
12720 post_p, is_gimple_val, fb_rvalue);
12721 if (TMR_INDEX2 (*expr_p))
12722 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
12723 post_p, is_gimple_val, fb_rvalue);
12724 /* TMR_STEP and TMR_OFFSET are always integer constants. */
12725 ret = MIN (r0, r1);
12727 break;
12729 case NON_LVALUE_EXPR:
12730 /* This should have been stripped above. */
12731 gcc_unreachable ();
12733 case ASM_EXPR:
12734 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
12735 break;
12737 case TRY_FINALLY_EXPR:
12738 case TRY_CATCH_EXPR:
12740 gimple_seq eval, cleanup;
12741 gtry *try_;
12743 /* Calls to destructors are generated automatically in FINALLY/CATCH
12744 block. They should have location as UNKNOWN_LOCATION. However,
12745 gimplify_call_expr will reset these call stmts to input_location
12746 if it finds stmt's location is unknown. To prevent resetting for
12747 destructors, we set the input_location to unknown.
12748 Note that this only affects the destructor calls in FINALLY/CATCH
12749 block, and will automatically reset to its original value by the
12750 end of gimplify_expr. */
12751 input_location = UNKNOWN_LOCATION;
12752 eval = cleanup = NULL;
12753 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
12754 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
12755 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
12756 if (gimple_seq_empty_p (cleanup))
12758 gimple_seq_add_seq (pre_p, eval);
12759 ret = GS_ALL_DONE;
12760 break;
12762 try_ = gimple_build_try (eval, cleanup,
12763 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
12764 ? GIMPLE_TRY_FINALLY
12765 : GIMPLE_TRY_CATCH);
12766 if (EXPR_HAS_LOCATION (save_expr))
12767 gimple_set_location (try_, EXPR_LOCATION (save_expr));
12768 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
12769 gimple_set_location (try_, saved_location);
12770 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
12771 gimple_try_set_catch_is_cleanup (try_,
12772 TRY_CATCH_IS_CLEANUP (*expr_p));
12773 gimplify_seq_add_stmt (pre_p, try_);
12774 ret = GS_ALL_DONE;
12775 break;
12778 case CLEANUP_POINT_EXPR:
12779 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
12780 break;
12782 case TARGET_EXPR:
12783 ret = gimplify_target_expr (expr_p, pre_p, post_p);
12784 break;
12786 case CATCH_EXPR:
12788 gimple *c;
12789 gimple_seq handler = NULL;
12790 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
12791 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
12792 gimplify_seq_add_stmt (pre_p, c);
12793 ret = GS_ALL_DONE;
12794 break;
12797 case EH_FILTER_EXPR:
12799 gimple *ehf;
12800 gimple_seq failure = NULL;
12802 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
12803 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
12804 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
12805 gimplify_seq_add_stmt (pre_p, ehf);
12806 ret = GS_ALL_DONE;
12807 break;
12810 case OBJ_TYPE_REF:
12812 enum gimplify_status r0, r1;
12813 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
12814 post_p, is_gimple_val, fb_rvalue);
12815 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
12816 post_p, is_gimple_val, fb_rvalue);
12817 TREE_SIDE_EFFECTS (*expr_p) = 0;
12818 ret = MIN (r0, r1);
12820 break;
12822 case LABEL_DECL:
12823 /* We get here when taking the address of a label. We mark
12824 the label as "forced"; meaning it can never be removed and
12825 it is a potential target for any computed goto. */
12826 FORCED_LABEL (*expr_p) = 1;
12827 ret = GS_ALL_DONE;
12828 break;
12830 case STATEMENT_LIST:
12831 ret = gimplify_statement_list (expr_p, pre_p);
12832 break;
12834 case WITH_SIZE_EXPR:
12836 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12837 post_p == &internal_post ? NULL : post_p,
12838 gimple_test_f, fallback);
12839 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12840 is_gimple_val, fb_rvalue);
12841 ret = GS_ALL_DONE;
12843 break;
12845 case VAR_DECL:
12846 case PARM_DECL:
12847 ret = gimplify_var_or_parm_decl (expr_p);
12848 break;
12850 case RESULT_DECL:
12851 /* When within an OMP context, notice uses of variables. */
12852 if (gimplify_omp_ctxp)
12853 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
12854 ret = GS_ALL_DONE;
12855 break;
12857 case DEBUG_EXPR_DECL:
12858 gcc_unreachable ();
12860 case DEBUG_BEGIN_STMT:
12861 gimplify_seq_add_stmt (pre_p,
12862 gimple_build_debug_begin_stmt
12863 (TREE_BLOCK (*expr_p),
12864 EXPR_LOCATION (*expr_p)));
12865 ret = GS_ALL_DONE;
12866 *expr_p = NULL;
12867 break;
12869 case SSA_NAME:
12870 /* Allow callbacks into the gimplifier during optimization. */
12871 ret = GS_ALL_DONE;
12872 break;
12874 case OMP_PARALLEL:
12875 gimplify_omp_parallel (expr_p, pre_p);
12876 ret = GS_ALL_DONE;
12877 break;
12879 case OMP_TASK:
12880 gimplify_omp_task (expr_p, pre_p);
12881 ret = GS_ALL_DONE;
12882 break;
12884 case OMP_FOR:
12885 case OMP_SIMD:
12886 case OMP_DISTRIBUTE:
12887 case OMP_TASKLOOP:
12888 case OACC_LOOP:
12889 ret = gimplify_omp_for (expr_p, pre_p);
12890 break;
12892 case OACC_CACHE:
12893 gimplify_oacc_cache (expr_p, pre_p);
12894 ret = GS_ALL_DONE;
12895 break;
12897 case OACC_DECLARE:
12898 gimplify_oacc_declare (expr_p, pre_p);
12899 ret = GS_ALL_DONE;
12900 break;
12902 case OACC_HOST_DATA:
12903 case OACC_DATA:
12904 case OACC_KERNELS:
12905 case OACC_PARALLEL:
12906 case OMP_SECTIONS:
12907 case OMP_SINGLE:
12908 case OMP_TARGET:
12909 case OMP_TARGET_DATA:
12910 case OMP_TEAMS:
12911 gimplify_omp_workshare (expr_p, pre_p);
12912 ret = GS_ALL_DONE;
12913 break;
12915 case OACC_ENTER_DATA:
12916 case OACC_EXIT_DATA:
12917 case OACC_UPDATE:
12918 case OMP_TARGET_UPDATE:
12919 case OMP_TARGET_ENTER_DATA:
12920 case OMP_TARGET_EXIT_DATA:
12921 gimplify_omp_target_update (expr_p, pre_p);
12922 ret = GS_ALL_DONE;
12923 break;
12925 case OMP_SECTION:
12926 case OMP_MASTER:
12927 case OMP_ORDERED:
12928 case OMP_CRITICAL:
12930 gimple_seq body = NULL;
12931 gimple *g;
12933 gimplify_and_add (OMP_BODY (*expr_p), &body);
12934 switch (TREE_CODE (*expr_p))
12936 case OMP_SECTION:
12937 g = gimple_build_omp_section (body);
12938 break;
12939 case OMP_MASTER:
12940 g = gimple_build_omp_master (body);
12941 break;
12942 case OMP_ORDERED:
12943 g = gimplify_omp_ordered (*expr_p, body);
12944 break;
12945 case OMP_CRITICAL:
12946 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
12947 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
12948 gimplify_adjust_omp_clauses (pre_p, body,
12949 &OMP_CRITICAL_CLAUSES (*expr_p),
12950 OMP_CRITICAL);
12951 g = gimple_build_omp_critical (body,
12952 OMP_CRITICAL_NAME (*expr_p),
12953 OMP_CRITICAL_CLAUSES (*expr_p));
12954 break;
12955 default:
12956 gcc_unreachable ();
12958 gimplify_seq_add_stmt (pre_p, g);
12959 ret = GS_ALL_DONE;
12960 break;
12963 case OMP_TASKGROUP:
12965 gimple_seq body = NULL;
12967 tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
12968 gimplify_scan_omp_clauses (pclauses, pre_p, ORT_TASKGROUP,
12969 OMP_TASKGROUP);
12970 gimplify_adjust_omp_clauses (pre_p, NULL, pclauses, OMP_TASKGROUP);
12971 gimplify_and_add (OMP_BODY (*expr_p), &body);
12972 gimple_seq cleanup = NULL;
12973 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
12974 gimple *g = gimple_build_call (fn, 0);
12975 gimple_seq_add_stmt (&cleanup, g);
12976 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
12977 body = NULL;
12978 gimple_seq_add_stmt (&body, g);
12979 g = gimple_build_omp_taskgroup (body, *pclauses);
12980 gimplify_seq_add_stmt (pre_p, g);
12981 ret = GS_ALL_DONE;
12982 break;
12985 case OMP_ATOMIC:
12986 case OMP_ATOMIC_READ:
12987 case OMP_ATOMIC_CAPTURE_OLD:
12988 case OMP_ATOMIC_CAPTURE_NEW:
12989 ret = gimplify_omp_atomic (expr_p, pre_p);
12990 break;
12992 case TRANSACTION_EXPR:
12993 ret = gimplify_transaction (expr_p, pre_p);
12994 break;
12996 case TRUTH_AND_EXPR:
12997 case TRUTH_OR_EXPR:
12998 case TRUTH_XOR_EXPR:
13000 tree orig_type = TREE_TYPE (*expr_p);
13001 tree new_type, xop0, xop1;
13002 *expr_p = gimple_boolify (*expr_p);
13003 new_type = TREE_TYPE (*expr_p);
13004 if (!useless_type_conversion_p (orig_type, new_type))
13006 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
13007 ret = GS_OK;
13008 break;
13011 /* Boolified binary truth expressions are semantically equivalent
13012 to bitwise binary expressions. Canonicalize them to the
13013 bitwise variant. */
13014 switch (TREE_CODE (*expr_p))
13016 case TRUTH_AND_EXPR:
13017 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
13018 break;
13019 case TRUTH_OR_EXPR:
13020 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
13021 break;
13022 case TRUTH_XOR_EXPR:
13023 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
13024 break;
13025 default:
13026 break;
13028 /* Now make sure that operands have compatible type to
13029 expression's new_type. */
13030 xop0 = TREE_OPERAND (*expr_p, 0);
13031 xop1 = TREE_OPERAND (*expr_p, 1);
13032 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
13033 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
13034 new_type,
13035 xop0);
13036 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
13037 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
13038 new_type,
13039 xop1);
13040 /* Continue classified as tcc_binary. */
13041 goto expr_2;
13044 case VEC_COND_EXPR:
13046 enum gimplify_status r0, r1, r2;
13048 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13049 post_p, is_gimple_condexpr, fb_rvalue);
13050 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13051 post_p, is_gimple_val, fb_rvalue);
13052 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
13053 post_p, is_gimple_val, fb_rvalue);
13055 ret = MIN (MIN (r0, r1), r2);
13056 recalculate_side_effects (*expr_p);
13058 break;
13060 case VEC_PERM_EXPR:
13061 /* Classified as tcc_expression. */
13062 goto expr_3;
13064 case BIT_INSERT_EXPR:
13065 /* Argument 3 is a constant. */
13066 goto expr_2;
13068 case POINTER_PLUS_EXPR:
13070 enum gimplify_status r0, r1;
13071 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13072 post_p, is_gimple_val, fb_rvalue);
13073 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13074 post_p, is_gimple_val, fb_rvalue);
13075 recalculate_side_effects (*expr_p);
13076 ret = MIN (r0, r1);
13077 break;
13080 default:
13081 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
13083 case tcc_comparison:
13084 /* Handle comparison of objects of non scalar mode aggregates
13085 with a call to memcmp. It would be nice to only have to do
13086 this for variable-sized objects, but then we'd have to allow
13087 the same nest of reference nodes we allow for MODIFY_EXPR and
13088 that's too complex.
13090 Compare scalar mode aggregates as scalar mode values. Using
13091 memcmp for them would be very inefficient at best, and is
13092 plain wrong if bitfields are involved. */
13094 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
13096 /* Vector comparisons need no boolification. */
13097 if (TREE_CODE (type) == VECTOR_TYPE)
13098 goto expr_2;
13099 else if (!AGGREGATE_TYPE_P (type))
13101 tree org_type = TREE_TYPE (*expr_p);
13102 *expr_p = gimple_boolify (*expr_p);
13103 if (!useless_type_conversion_p (org_type,
13104 TREE_TYPE (*expr_p)))
13106 *expr_p = fold_convert_loc (input_location,
13107 org_type, *expr_p);
13108 ret = GS_OK;
13110 else
13111 goto expr_2;
13113 else if (TYPE_MODE (type) != BLKmode)
13114 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
13115 else
13116 ret = gimplify_variable_sized_compare (expr_p);
13118 break;
13121 /* If *EXPR_P does not need to be special-cased, handle it
13122 according to its class. */
13123 case tcc_unary:
13124 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13125 post_p, is_gimple_val, fb_rvalue);
13126 break;
13128 case tcc_binary:
13129 expr_2:
13131 enum gimplify_status r0, r1;
13133 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13134 post_p, is_gimple_val, fb_rvalue);
13135 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13136 post_p, is_gimple_val, fb_rvalue);
13138 ret = MIN (r0, r1);
13139 break;
13142 expr_3:
13144 enum gimplify_status r0, r1, r2;
13146 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13147 post_p, is_gimple_val, fb_rvalue);
13148 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13149 post_p, is_gimple_val, fb_rvalue);
13150 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
13151 post_p, is_gimple_val, fb_rvalue);
13153 ret = MIN (MIN (r0, r1), r2);
13154 break;
13157 case tcc_declaration:
13158 case tcc_constant:
13159 ret = GS_ALL_DONE;
13160 goto dont_recalculate;
13162 default:
13163 gcc_unreachable ();
13166 recalculate_side_effects (*expr_p);
13168 dont_recalculate:
13169 break;
13172 gcc_assert (*expr_p || ret != GS_OK);
13174 while (ret == GS_OK);
13176 /* If we encountered an error_mark somewhere nested inside, either
13177 stub out the statement or propagate the error back out. */
13178 if (ret == GS_ERROR)
13180 if (is_statement)
13181 *expr_p = NULL;
13182 goto out;
13185 /* This was only valid as a return value from the langhook, which
13186 we handled. Make sure it doesn't escape from any other context. */
13187 gcc_assert (ret != GS_UNHANDLED);
13189 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
13191 /* We aren't looking for a value, and we don't have a valid
13192 statement. If it doesn't have side-effects, throw it away.
13193 We can also get here with code such as "*&&L;", where L is
13194 a LABEL_DECL that is marked as FORCED_LABEL. */
13195 if (TREE_CODE (*expr_p) == LABEL_DECL
13196 || !TREE_SIDE_EFFECTS (*expr_p))
13197 *expr_p = NULL;
13198 else if (!TREE_THIS_VOLATILE (*expr_p))
13200 /* This is probably a _REF that contains something nested that
13201 has side effects. Recurse through the operands to find it. */
13202 enum tree_code code = TREE_CODE (*expr_p);
13204 switch (code)
13206 case COMPONENT_REF:
13207 case REALPART_EXPR:
13208 case IMAGPART_EXPR:
13209 case VIEW_CONVERT_EXPR:
13210 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
13211 gimple_test_f, fallback);
13212 break;
13214 case ARRAY_REF:
13215 case ARRAY_RANGE_REF:
13216 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
13217 gimple_test_f, fallback);
13218 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
13219 gimple_test_f, fallback);
13220 break;
13222 default:
13223 /* Anything else with side-effects must be converted to
13224 a valid statement before we get here. */
13225 gcc_unreachable ();
13228 *expr_p = NULL;
13230 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
13231 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
13233 /* Historically, the compiler has treated a bare reference
13234 to a non-BLKmode volatile lvalue as forcing a load. */
13235 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
13237 /* Normally, we do not want to create a temporary for a
13238 TREE_ADDRESSABLE type because such a type should not be
13239 copied by bitwise-assignment. However, we make an
13240 exception here, as all we are doing here is ensuring that
13241 we read the bytes that make up the type. We use
13242 create_tmp_var_raw because create_tmp_var will abort when
13243 given a TREE_ADDRESSABLE type. */
13244 tree tmp = create_tmp_var_raw (type, "vol");
13245 gimple_add_tmp_var (tmp);
13246 gimplify_assign (tmp, *expr_p, pre_p);
13247 *expr_p = NULL;
13249 else
13250 /* We can't do anything useful with a volatile reference to
13251 an incomplete type, so just throw it away. Likewise for
13252 a BLKmode type, since any implicit inner load should
13253 already have been turned into an explicit one by the
13254 gimplification process. */
13255 *expr_p = NULL;
13258 /* If we are gimplifying at the statement level, we're done. Tack
13259 everything together and return. */
13260 if (fallback == fb_none || is_statement)
13262 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
13263 it out for GC to reclaim it. */
13264 *expr_p = NULL_TREE;
13266 if (!gimple_seq_empty_p (internal_pre)
13267 || !gimple_seq_empty_p (internal_post))
13269 gimplify_seq_add_seq (&internal_pre, internal_post);
13270 gimplify_seq_add_seq (pre_p, internal_pre);
13273 /* The result of gimplifying *EXPR_P is going to be the last few
13274 statements in *PRE_P and *POST_P. Add location information
13275 to all the statements that were added by the gimplification
13276 helpers. */
13277 if (!gimple_seq_empty_p (*pre_p))
13278 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
13280 if (!gimple_seq_empty_p (*post_p))
13281 annotate_all_with_location_after (*post_p, post_last_gsi,
13282 input_location);
13284 goto out;
13287 #ifdef ENABLE_GIMPLE_CHECKING
13288 if (*expr_p)
13290 enum tree_code code = TREE_CODE (*expr_p);
13291 /* These expressions should already be in gimple IR form. */
13292 gcc_assert (code != MODIFY_EXPR
13293 && code != ASM_EXPR
13294 && code != BIND_EXPR
13295 && code != CATCH_EXPR
13296 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
13297 && code != EH_FILTER_EXPR
13298 && code != GOTO_EXPR
13299 && code != LABEL_EXPR
13300 && code != LOOP_EXPR
13301 && code != SWITCH_EXPR
13302 && code != TRY_FINALLY_EXPR
13303 && code != OACC_PARALLEL
13304 && code != OACC_KERNELS
13305 && code != OACC_DATA
13306 && code != OACC_HOST_DATA
13307 && code != OACC_DECLARE
13308 && code != OACC_UPDATE
13309 && code != OACC_ENTER_DATA
13310 && code != OACC_EXIT_DATA
13311 && code != OACC_CACHE
13312 && code != OMP_CRITICAL
13313 && code != OMP_FOR
13314 && code != OACC_LOOP
13315 && code != OMP_MASTER
13316 && code != OMP_TASKGROUP
13317 && code != OMP_ORDERED
13318 && code != OMP_PARALLEL
13319 && code != OMP_SECTIONS
13320 && code != OMP_SECTION
13321 && code != OMP_SINGLE);
13323 #endif
13325 /* Otherwise we're gimplifying a subexpression, so the resulting
13326 value is interesting. If it's a valid operand that matches
13327 GIMPLE_TEST_F, we're done. Unless we are handling some
13328 post-effects internally; if that's the case, we need to copy into
13329 a temporary before adding the post-effects to POST_P. */
13330 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
13331 goto out;
13333 /* Otherwise, we need to create a new temporary for the gimplified
13334 expression. */
13336 /* We can't return an lvalue if we have an internal postqueue. The
13337 object the lvalue refers to would (probably) be modified by the
13338 postqueue; we need to copy the value out first, which means an
13339 rvalue. */
13340 if ((fallback & fb_lvalue)
13341 && gimple_seq_empty_p (internal_post)
13342 && is_gimple_addressable (*expr_p))
13344 /* An lvalue will do. Take the address of the expression, store it
13345 in a temporary, and replace the expression with an INDIRECT_REF of
13346 that temporary. */
13347 tree ref_alias_type = reference_alias_ptr_type (*expr_p);
13348 unsigned int ref_align = get_object_alignment (*expr_p);
13349 tree ref_type = TREE_TYPE (*expr_p);
13350 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
13351 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
13352 if (TYPE_ALIGN (ref_type) != ref_align)
13353 ref_type = build_aligned_type (ref_type, ref_align);
13354 *expr_p = build2 (MEM_REF, ref_type,
13355 tmp, build_zero_cst (ref_alias_type));
13357 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
13359 /* An rvalue will do. Assign the gimplified expression into a
13360 new temporary TMP and replace the original expression with
13361 TMP. First, make sure that the expression has a type so that
13362 it can be assigned into a temporary. */
13363 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
13364 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
13366 else
13368 #ifdef ENABLE_GIMPLE_CHECKING
13369 if (!(fallback & fb_mayfail))
13371 fprintf (stderr, "gimplification failed:\n");
13372 print_generic_expr (stderr, *expr_p);
13373 debug_tree (*expr_p);
13374 internal_error ("gimplification failed");
13376 #endif
13377 gcc_assert (fallback & fb_mayfail);
13379 /* If this is an asm statement, and the user asked for the
13380 impossible, don't die. Fail and let gimplify_asm_expr
13381 issue an error. */
13382 ret = GS_ERROR;
13383 goto out;
13386 /* Make sure the temporary matches our predicate. */
13387 gcc_assert ((*gimple_test_f) (*expr_p));
13389 if (!gimple_seq_empty_p (internal_post))
13391 annotate_all_with_location (internal_post, input_location);
13392 gimplify_seq_add_seq (pre_p, internal_post);
13395 out:
13396 input_location = saved_location;
13397 return ret;
13400 /* Like gimplify_expr but make sure the gimplified result is not itself
13401 a SSA name (but a decl if it were). Temporaries required by
13402 evaluating *EXPR_P may be still SSA names. */
13404 static enum gimplify_status
13405 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
13406 bool (*gimple_test_f) (tree), fallback_t fallback,
13407 bool allow_ssa)
13409 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
13410 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
13411 gimple_test_f, fallback);
13412 if (! allow_ssa
13413 && TREE_CODE (*expr_p) == SSA_NAME)
13415 tree name = *expr_p;
13416 if (was_ssa_name_p)
13417 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
13418 else
13420 /* Avoid the extra copy if possible. */
13421 *expr_p = create_tmp_reg (TREE_TYPE (name));
13422 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
13423 release_ssa_name (name);
13426 return ret;
13429 /* Look through TYPE for variable-sized objects and gimplify each such
13430 size that we find. Add to LIST_P any statements generated. */
13432 void
13433 gimplify_type_sizes (tree type, gimple_seq *list_p)
13435 tree field, t;
13437 if (type == NULL || type == error_mark_node)
13438 return;
13440 /* We first do the main variant, then copy into any other variants. */
13441 type = TYPE_MAIN_VARIANT (type);
13443 /* Avoid infinite recursion. */
13444 if (TYPE_SIZES_GIMPLIFIED (type))
13445 return;
13447 TYPE_SIZES_GIMPLIFIED (type) = 1;
13449 switch (TREE_CODE (type))
13451 case INTEGER_TYPE:
13452 case ENUMERAL_TYPE:
13453 case BOOLEAN_TYPE:
13454 case REAL_TYPE:
13455 case FIXED_POINT_TYPE:
13456 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
13457 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
13459 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13461 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
13462 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
13464 break;
13466 case ARRAY_TYPE:
13467 /* These types may not have declarations, so handle them here. */
13468 gimplify_type_sizes (TREE_TYPE (type), list_p);
13469 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
13470 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
13471 with assigned stack slots, for -O1+ -g they should be tracked
13472 by VTA. */
13473 if (!(TYPE_NAME (type)
13474 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13475 && DECL_IGNORED_P (TYPE_NAME (type)))
13476 && TYPE_DOMAIN (type)
13477 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
13479 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
13480 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
13481 DECL_IGNORED_P (t) = 0;
13482 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13483 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
13484 DECL_IGNORED_P (t) = 0;
13486 break;
13488 case RECORD_TYPE:
13489 case UNION_TYPE:
13490 case QUAL_UNION_TYPE:
13491 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
13492 if (TREE_CODE (field) == FIELD_DECL)
13494 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
13495 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
13496 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
13497 gimplify_type_sizes (TREE_TYPE (field), list_p);
13499 break;
13501 case POINTER_TYPE:
13502 case REFERENCE_TYPE:
13503 /* We used to recurse on the pointed-to type here, which turned out to
13504 be incorrect because its definition might refer to variables not
13505 yet initialized at this point if a forward declaration is involved.
13507 It was actually useful for anonymous pointed-to types to ensure
13508 that the sizes evaluation dominates every possible later use of the
13509 values. Restricting to such types here would be safe since there
13510 is no possible forward declaration around, but would introduce an
13511 undesirable middle-end semantic to anonymity. We then defer to
13512 front-ends the responsibility of ensuring that the sizes are
13513 evaluated both early and late enough, e.g. by attaching artificial
13514 type declarations to the tree. */
13515 break;
13517 default:
13518 break;
13521 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
13522 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
13524 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13526 TYPE_SIZE (t) = TYPE_SIZE (type);
13527 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
13528 TYPE_SIZES_GIMPLIFIED (t) = 1;
13532 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
13533 a size or position, has had all of its SAVE_EXPRs evaluated.
13534 We add any required statements to *STMT_P. */
13536 void
13537 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
13539 tree expr = *expr_p;
13541 /* We don't do anything if the value isn't there, is constant, or contains
13542 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
13543 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
13544 will want to replace it with a new variable, but that will cause problems
13545 if this type is from outside the function. It's OK to have that here. */
13546 if (expr == NULL_TREE
13547 || is_gimple_constant (expr)
13548 || TREE_CODE (expr) == VAR_DECL
13549 || CONTAINS_PLACEHOLDER_P (expr))
13550 return;
13552 *expr_p = unshare_expr (expr);
13554 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
13555 if the def vanishes. */
13556 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
13558 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
13559 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
13560 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
13561 if (is_gimple_constant (*expr_p))
13562 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
13565 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
13566 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
13567 is true, also gimplify the parameters. */
13569 gbind *
13570 gimplify_body (tree fndecl, bool do_parms)
13572 location_t saved_location = input_location;
13573 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
13574 gimple *outer_stmt;
13575 gbind *outer_bind;
13577 timevar_push (TV_TREE_GIMPLIFY);
13579 init_tree_ssa (cfun);
13581 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
13582 gimplification. */
13583 default_rtl_profile ();
13585 gcc_assert (gimplify_ctxp == NULL);
13586 push_gimplify_context (true);
13588 if (flag_openacc || flag_openmp)
13590 gcc_assert (gimplify_omp_ctxp == NULL);
13591 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
13592 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
13595 /* Unshare most shared trees in the body and in that of any nested functions.
13596 It would seem we don't have to do this for nested functions because
13597 they are supposed to be output and then the outer function gimplified
13598 first, but the g++ front end doesn't always do it that way. */
13599 unshare_body (fndecl);
13600 unvisit_body (fndecl);
13602 /* Make sure input_location isn't set to something weird. */
13603 input_location = DECL_SOURCE_LOCATION (fndecl);
13605 /* Resolve callee-copies. This has to be done before processing
13606 the body so that DECL_VALUE_EXPR gets processed correctly. */
13607 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
13609 /* Gimplify the function's body. */
13610 seq = NULL;
13611 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
13612 outer_stmt = gimple_seq_first_stmt (seq);
13613 if (!outer_stmt)
13615 outer_stmt = gimple_build_nop ();
13616 gimplify_seq_add_stmt (&seq, outer_stmt);
13619 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
13620 not the case, wrap everything in a GIMPLE_BIND to make it so. */
13621 if (gimple_code (outer_stmt) == GIMPLE_BIND
13622 && gimple_seq_first (seq) == gimple_seq_last (seq))
13623 outer_bind = as_a <gbind *> (outer_stmt);
13624 else
13625 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
13627 DECL_SAVED_TREE (fndecl) = NULL_TREE;
13629 /* If we had callee-copies statements, insert them at the beginning
13630 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
13631 if (!gimple_seq_empty_p (parm_stmts))
13633 tree parm;
13635 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
13636 if (parm_cleanup)
13638 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
13639 GIMPLE_TRY_FINALLY);
13640 parm_stmts = NULL;
13641 gimple_seq_add_stmt (&parm_stmts, g);
13643 gimple_bind_set_body (outer_bind, parm_stmts);
13645 for (parm = DECL_ARGUMENTS (current_function_decl);
13646 parm; parm = DECL_CHAIN (parm))
13647 if (DECL_HAS_VALUE_EXPR_P (parm))
13649 DECL_HAS_VALUE_EXPR_P (parm) = 0;
13650 DECL_IGNORED_P (parm) = 0;
13654 if ((flag_openacc || flag_openmp || flag_openmp_simd)
13655 && gimplify_omp_ctxp)
13657 delete_omp_context (gimplify_omp_ctxp);
13658 gimplify_omp_ctxp = NULL;
13661 pop_gimplify_context (outer_bind);
13662 gcc_assert (gimplify_ctxp == NULL);
13664 if (flag_checking && !seen_error ())
13665 verify_gimple_in_seq (gimple_bind_body (outer_bind));
13667 timevar_pop (TV_TREE_GIMPLIFY);
13668 input_location = saved_location;
13670 return outer_bind;
13673 typedef char *char_p; /* For DEF_VEC_P. */
13675 /* Return whether we should exclude FNDECL from instrumentation. */
13677 static bool
13678 flag_instrument_functions_exclude_p (tree fndecl)
13680 vec<char_p> *v;
13682 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
13683 if (v && v->length () > 0)
13685 const char *name;
13686 int i;
13687 char *s;
13689 name = lang_hooks.decl_printable_name (fndecl, 0);
13690 FOR_EACH_VEC_ELT (*v, i, s)
13691 if (strstr (name, s) != NULL)
13692 return true;
13695 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
13696 if (v && v->length () > 0)
13698 const char *name;
13699 int i;
13700 char *s;
13702 name = DECL_SOURCE_FILE (fndecl);
13703 FOR_EACH_VEC_ELT (*v, i, s)
13704 if (strstr (name, s) != NULL)
13705 return true;
13708 return false;
13711 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
13712 node for the function we want to gimplify.
13714 Return the sequence of GIMPLE statements corresponding to the body
13715 of FNDECL. */
13717 void
13718 gimplify_function_tree (tree fndecl)
13720 tree parm, ret;
13721 gimple_seq seq;
13722 gbind *bind;
13724 gcc_assert (!gimple_body (fndecl));
13726 if (DECL_STRUCT_FUNCTION (fndecl))
13727 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
13728 else
13729 push_struct_function (fndecl);
13731 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
13732 if necessary. */
13733 cfun->curr_properties |= PROP_gimple_lva;
13735 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
13737 /* Preliminarily mark non-addressed complex variables as eligible
13738 for promotion to gimple registers. We'll transform their uses
13739 as we find them. */
13740 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
13741 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
13742 && !TREE_THIS_VOLATILE (parm)
13743 && !needs_to_live_in_memory (parm))
13744 DECL_GIMPLE_REG_P (parm) = 1;
13747 ret = DECL_RESULT (fndecl);
13748 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
13749 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
13750 && !needs_to_live_in_memory (ret))
13751 DECL_GIMPLE_REG_P (ret) = 1;
13753 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
13754 asan_poisoned_variables = new hash_set<tree> ();
13755 bind = gimplify_body (fndecl, true);
13756 if (asan_poisoned_variables)
13758 delete asan_poisoned_variables;
13759 asan_poisoned_variables = NULL;
13762 /* The tree body of the function is no longer needed, replace it
13763 with the new GIMPLE body. */
13764 seq = NULL;
13765 gimple_seq_add_stmt (&seq, bind);
13766 gimple_set_body (fndecl, seq);
13768 /* If we're instrumenting function entry/exit, then prepend the call to
13769 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
13770 catch the exit hook. */
13771 /* ??? Add some way to ignore exceptions for this TFE. */
13772 if (flag_instrument_function_entry_exit
13773 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
13774 /* Do not instrument extern inline functions. */
13775 && !(DECL_DECLARED_INLINE_P (fndecl)
13776 && DECL_EXTERNAL (fndecl)
13777 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
13778 && !flag_instrument_functions_exclude_p (fndecl))
13780 tree x;
13781 gbind *new_bind;
13782 gimple *tf;
13783 gimple_seq cleanup = NULL, body = NULL;
13784 tree tmp_var, this_fn_addr;
13785 gcall *call;
13787 /* The instrumentation hooks aren't going to call the instrumented
13788 function and the address they receive is expected to be matchable
13789 against symbol addresses. Make sure we don't create a trampoline,
13790 in case the current function is nested. */
13791 this_fn_addr = build_fold_addr_expr (current_function_decl);
13792 TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
13794 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
13795 call = gimple_build_call (x, 1, integer_zero_node);
13796 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
13797 gimple_call_set_lhs (call, tmp_var);
13798 gimplify_seq_add_stmt (&cleanup, call);
13799 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
13800 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
13801 gimplify_seq_add_stmt (&cleanup, call);
13802 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
13804 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
13805 call = gimple_build_call (x, 1, integer_zero_node);
13806 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
13807 gimple_call_set_lhs (call, tmp_var);
13808 gimplify_seq_add_stmt (&body, call);
13809 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
13810 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
13811 gimplify_seq_add_stmt (&body, call);
13812 gimplify_seq_add_stmt (&body, tf);
13813 new_bind = gimple_build_bind (NULL, body, NULL);
13815 /* Replace the current function body with the body
13816 wrapped in the try/finally TF. */
13817 seq = NULL;
13818 gimple_seq_add_stmt (&seq, new_bind);
13819 gimple_set_body (fndecl, seq);
13820 bind = new_bind;
13823 if (sanitize_flags_p (SANITIZE_THREAD))
13825 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
13826 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
13827 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
13828 /* Replace the current function body with the body
13829 wrapped in the try/finally TF. */
13830 seq = NULL;
13831 gimple_seq_add_stmt (&seq, new_bind);
13832 gimple_set_body (fndecl, seq);
13835 DECL_SAVED_TREE (fndecl) = NULL_TREE;
13836 cfun->curr_properties |= PROP_gimple_any;
13838 pop_cfun ();
13840 dump_function (TDI_gimple, fndecl);
13843 /* Return a dummy expression of type TYPE in order to keep going after an
13844 error. */
13846 static tree
13847 dummy_object (tree type)
13849 tree t = build_int_cst (build_pointer_type (type), 0);
13850 return build2 (MEM_REF, type, t, t);
13853 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
13854 builtin function, but a very special sort of operator. */
13856 enum gimplify_status
13857 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
13858 gimple_seq *post_p ATTRIBUTE_UNUSED)
13860 tree promoted_type, have_va_type;
13861 tree valist = TREE_OPERAND (*expr_p, 0);
13862 tree type = TREE_TYPE (*expr_p);
13863 tree t, tag, aptag;
13864 location_t loc = EXPR_LOCATION (*expr_p);
13866 /* Verify that valist is of the proper type. */
13867 have_va_type = TREE_TYPE (valist);
13868 if (have_va_type == error_mark_node)
13869 return GS_ERROR;
13870 have_va_type = targetm.canonical_va_list_type (have_va_type);
13871 if (have_va_type == NULL_TREE
13872 && POINTER_TYPE_P (TREE_TYPE (valist)))
13873 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
13874 have_va_type
13875 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
13876 gcc_assert (have_va_type != NULL_TREE);
13878 /* Generate a diagnostic for requesting data of a type that cannot
13879 be passed through `...' due to type promotion at the call site. */
13880 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
13881 != type)
13883 static bool gave_help;
13884 bool warned;
13885 /* Use the expansion point to handle cases such as passing bool (defined
13886 in a system header) through `...'. */
13887 location_t xloc
13888 = expansion_point_location_if_in_system_header (loc);
13890 /* Unfortunately, this is merely undefined, rather than a constraint
13891 violation, so we cannot make this an error. If this call is never
13892 executed, the program is still strictly conforming. */
13893 auto_diagnostic_group d;
13894 warned = warning_at (xloc, 0,
13895 "%qT is promoted to %qT when passed through %<...%>",
13896 type, promoted_type);
13897 if (!gave_help && warned)
13899 gave_help = true;
13900 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
13901 promoted_type, type);
13904 /* We can, however, treat "undefined" any way we please.
13905 Call abort to encourage the user to fix the program. */
13906 if (warned)
13907 inform (xloc, "if this code is reached, the program will abort");
13908 /* Before the abort, allow the evaluation of the va_list
13909 expression to exit or longjmp. */
13910 gimplify_and_add (valist, pre_p);
13911 t = build_call_expr_loc (loc,
13912 builtin_decl_implicit (BUILT_IN_TRAP), 0);
13913 gimplify_and_add (t, pre_p);
13915 /* This is dead code, but go ahead and finish so that the
13916 mode of the result comes out right. */
13917 *expr_p = dummy_object (type);
13918 return GS_ALL_DONE;
13921 tag = build_int_cst (build_pointer_type (type), 0);
13922 aptag = build_int_cst (TREE_TYPE (valist), 0);
13924 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
13925 valist, tag, aptag);
13927 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
13928 needs to be expanded. */
13929 cfun->curr_properties &= ~PROP_gimple_lva;
13931 return GS_OK;
13934 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
13936 DST/SRC are the destination and source respectively. You can pass
13937 ungimplified trees in DST or SRC, in which case they will be
13938 converted to a gimple operand if necessary.
13940 This function returns the newly created GIMPLE_ASSIGN tuple. */
13942 gimple *
13943 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
13945 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13946 gimplify_and_add (t, seq_p);
13947 ggc_free (t);
13948 return gimple_seq_last_stmt (*seq_p);
13951 inline hashval_t
13952 gimplify_hasher::hash (const elt_t *p)
13954 tree t = p->val;
13955 return iterative_hash_expr (t, 0);
13958 inline bool
13959 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
13961 tree t1 = p1->val;
13962 tree t2 = p2->val;
13963 enum tree_code code = TREE_CODE (t1);
13965 if (TREE_CODE (t2) != code
13966 || TREE_TYPE (t1) != TREE_TYPE (t2))
13967 return false;
13969 if (!operand_equal_p (t1, t2, 0))
13970 return false;
13972 /* Only allow them to compare equal if they also hash equal; otherwise
13973 results are nondeterminate, and we fail bootstrap comparison. */
13974 gcc_checking_assert (hash (p1) == hash (p2));
13976 return true;