match_asm_constraints: Use copy_rtx where needed (PR88001)
[official-gcc.git] / gcc / gimplify.c
blob9022ef8b2ee6575fb011012fa76792f9ebbf0c20
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2018 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "gimple.h"
33 #include "gimple-predict.h"
34 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
35 #include "ssa.h"
36 #include "cgraph.h"
37 #include "tree-pretty-print.h"
38 #include "diagnostic-core.h"
39 #include "alias.h"
40 #include "fold-const.h"
41 #include "calls.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "gimple-fold.h"
46 #include "tree-eh.h"
47 #include "gimplify.h"
48 #include "gimple-iterator.h"
49 #include "stor-layout.h"
50 #include "print-tree.h"
51 #include "tree-iterator.h"
52 #include "tree-inline.h"
53 #include "langhooks.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa.h"
56 #include "omp-general.h"
57 #include "omp-low.h"
58 #include "gimple-low.h"
59 #include "gomp-constants.h"
60 #include "splay-tree.h"
61 #include "gimple-walk.h"
62 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
63 #include "builtins.h"
64 #include "stringpool.h"
65 #include "attribs.h"
66 #include "asan.h"
67 #include "dbgcnt.h"
69 /* Hash set of poisoned variables in a bind expr. */
70 static hash_set<tree> *asan_poisoned_variables = NULL;
72 enum gimplify_omp_var_data
74 GOVD_SEEN = 1,
75 GOVD_EXPLICIT = 2,
76 GOVD_SHARED = 4,
77 GOVD_PRIVATE = 8,
78 GOVD_FIRSTPRIVATE = 16,
79 GOVD_LASTPRIVATE = 32,
80 GOVD_REDUCTION = 64,
81 GOVD_LOCAL = 128,
82 GOVD_MAP = 256,
83 GOVD_DEBUG_PRIVATE = 512,
84 GOVD_PRIVATE_OUTER_REF = 1024,
85 GOVD_LINEAR = 2048,
86 GOVD_ALIGNED = 4096,
88 /* Flag for GOVD_MAP: don't copy back. */
89 GOVD_MAP_TO_ONLY = 8192,
91 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
92 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
94 GOVD_MAP_0LEN_ARRAY = 32768,
96 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
97 GOVD_MAP_ALWAYS_TO = 65536,
99 /* Flag for shared vars that are or might be stored to in the region. */
100 GOVD_WRITTEN = 131072,
102 /* Flag for GOVD_MAP, if it is a forced mapping. */
103 GOVD_MAP_FORCE = 262144,
105 /* Flag for GOVD_MAP: must be present already. */
106 GOVD_MAP_FORCE_PRESENT = 524288,
108 /* 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 bool cleared, complete_p, valid_const_initializer;
4783 /* Aggregate types must lower constructors to initialization of
4784 individual elements. The exception is that a CONSTRUCTOR node
4785 with no elements indicates zero-initialization of the whole. */
4786 if (vec_safe_is_empty (elts))
4788 if (notify_temp_creation)
4789 return GS_OK;
4790 break;
4793 /* Fetch information about the constructor to direct later processing.
4794 We might want to make static versions of it in various cases, and
4795 can only do so if it known to be a valid constant initializer. */
4796 valid_const_initializer
4797 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4798 &num_ctor_elements, &complete_p);
4800 /* If a const aggregate variable is being initialized, then it
4801 should never be a lose to promote the variable to be static. */
4802 if (valid_const_initializer
4803 && num_nonzero_elements > 1
4804 && TREE_READONLY (object)
4805 && VAR_P (object)
4806 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4808 if (notify_temp_creation)
4809 return GS_ERROR;
4810 DECL_INITIAL (object) = ctor;
4811 TREE_STATIC (object) = 1;
4812 if (!DECL_NAME (object))
4813 DECL_NAME (object) = create_tmp_var_name ("C");
4814 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4816 /* ??? C++ doesn't automatically append a .<number> to the
4817 assembler name, and even when it does, it looks at FE private
4818 data structures to figure out what that number should be,
4819 which are not set for this variable. I suppose this is
4820 important for local statics for inline functions, which aren't
4821 "local" in the object file sense. So in order to get a unique
4822 TU-local symbol, we must invoke the lhd version now. */
4823 lhd_set_decl_assembler_name (object);
4825 *expr_p = NULL_TREE;
4826 break;
4829 /* If there are "lots" of initialized elements, even discounting
4830 those that are not address constants (and thus *must* be
4831 computed at runtime), then partition the constructor into
4832 constant and non-constant parts. Block copy the constant
4833 parts in, then generate code for the non-constant parts. */
4834 /* TODO. There's code in cp/typeck.c to do this. */
4836 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4837 /* store_constructor will ignore the clearing of variable-sized
4838 objects. Initializers for such objects must explicitly set
4839 every field that needs to be set. */
4840 cleared = false;
4841 else if (!complete_p)
4842 /* If the constructor isn't complete, clear the whole object
4843 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4845 ??? This ought not to be needed. For any element not present
4846 in the initializer, we should simply set them to zero. Except
4847 we'd need to *find* the elements that are not present, and that
4848 requires trickery to avoid quadratic compile-time behavior in
4849 large cases or excessive memory use in small cases. */
4850 cleared = !CONSTRUCTOR_NO_CLEARING (ctor);
4851 else if (num_ctor_elements - num_nonzero_elements
4852 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4853 && num_nonzero_elements < num_ctor_elements / 4)
4854 /* If there are "lots" of zeros, it's more efficient to clear
4855 the memory and then set the nonzero elements. */
4856 cleared = true;
4857 else
4858 cleared = false;
4860 /* If there are "lots" of initialized elements, and all of them
4861 are valid address constants, then the entire initializer can
4862 be dropped to memory, and then memcpy'd out. Don't do this
4863 for sparse arrays, though, as it's more efficient to follow
4864 the standard CONSTRUCTOR behavior of memset followed by
4865 individual element initialization. Also don't do this for small
4866 all-zero initializers (which aren't big enough to merit
4867 clearing), and don't try to make bitwise copies of
4868 TREE_ADDRESSABLE types. */
4870 if (valid_const_initializer
4871 && !(cleared || num_nonzero_elements == 0)
4872 && !TREE_ADDRESSABLE (type))
4874 HOST_WIDE_INT size = int_size_in_bytes (type);
4875 unsigned int align;
4877 /* ??? We can still get unbounded array types, at least
4878 from the C++ front end. This seems wrong, but attempt
4879 to work around it for now. */
4880 if (size < 0)
4882 size = int_size_in_bytes (TREE_TYPE (object));
4883 if (size >= 0)
4884 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4887 /* Find the maximum alignment we can assume for the object. */
4888 /* ??? Make use of DECL_OFFSET_ALIGN. */
4889 if (DECL_P (object))
4890 align = DECL_ALIGN (object);
4891 else
4892 align = TYPE_ALIGN (type);
4894 /* Do a block move either if the size is so small as to make
4895 each individual move a sub-unit move on average, or if it
4896 is so large as to make individual moves inefficient. */
4897 if (size > 0
4898 && num_nonzero_elements > 1
4899 && (size < num_nonzero_elements
4900 || !can_move_by_pieces (size, align)))
4902 if (notify_temp_creation)
4903 return GS_ERROR;
4905 walk_tree (&ctor, force_labels_r, NULL, NULL);
4906 ctor = tree_output_constant_def (ctor);
4907 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4908 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4909 TREE_OPERAND (*expr_p, 1) = ctor;
4911 /* This is no longer an assignment of a CONSTRUCTOR, but
4912 we still may have processing to do on the LHS. So
4913 pretend we didn't do anything here to let that happen. */
4914 return GS_UNHANDLED;
4918 /* If the target is volatile, we have non-zero elements and more than
4919 one field to assign, initialize the target from a temporary. */
4920 if (TREE_THIS_VOLATILE (object)
4921 && !TREE_ADDRESSABLE (type)
4922 && num_nonzero_elements > 0
4923 && vec_safe_length (elts) > 1)
4925 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4926 TREE_OPERAND (*expr_p, 0) = temp;
4927 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4928 *expr_p,
4929 build2 (MODIFY_EXPR, void_type_node,
4930 object, temp));
4931 return GS_OK;
4934 if (notify_temp_creation)
4935 return GS_OK;
4937 /* If there are nonzero elements and if needed, pre-evaluate to capture
4938 elements overlapping with the lhs into temporaries. We must do this
4939 before clearing to fetch the values before they are zeroed-out. */
4940 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4942 preeval_data.lhs_base_decl = get_base_address (object);
4943 if (!DECL_P (preeval_data.lhs_base_decl))
4944 preeval_data.lhs_base_decl = NULL;
4945 preeval_data.lhs_alias_set = get_alias_set (object);
4947 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4948 pre_p, post_p, &preeval_data);
4951 bool ctor_has_side_effects_p
4952 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4954 if (cleared)
4956 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4957 Note that we still have to gimplify, in order to handle the
4958 case of variable sized types. Avoid shared tree structures. */
4959 CONSTRUCTOR_ELTS (ctor) = NULL;
4960 TREE_SIDE_EFFECTS (ctor) = 0;
4961 object = unshare_expr (object);
4962 gimplify_stmt (expr_p, pre_p);
4965 /* If we have not block cleared the object, or if there are nonzero
4966 elements in the constructor, or if the constructor has side effects,
4967 add assignments to the individual scalar fields of the object. */
4968 if (!cleared
4969 || num_nonzero_elements > 0
4970 || ctor_has_side_effects_p)
4971 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4973 *expr_p = NULL_TREE;
4975 break;
4977 case COMPLEX_TYPE:
4979 tree r, i;
4981 if (notify_temp_creation)
4982 return GS_OK;
4984 /* Extract the real and imaginary parts out of the ctor. */
4985 gcc_assert (elts->length () == 2);
4986 r = (*elts)[0].value;
4987 i = (*elts)[1].value;
4988 if (r == NULL || i == NULL)
4990 tree zero = build_zero_cst (TREE_TYPE (type));
4991 if (r == NULL)
4992 r = zero;
4993 if (i == NULL)
4994 i = zero;
4997 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4998 represent creation of a complex value. */
4999 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
5001 ctor = build_complex (type, r, i);
5002 TREE_OPERAND (*expr_p, 1) = ctor;
5004 else
5006 ctor = build2 (COMPLEX_EXPR, type, r, i);
5007 TREE_OPERAND (*expr_p, 1) = ctor;
5008 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
5009 pre_p,
5010 post_p,
5011 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
5012 fb_rvalue);
5015 break;
5017 case VECTOR_TYPE:
5019 unsigned HOST_WIDE_INT ix;
5020 constructor_elt *ce;
5022 if (notify_temp_creation)
5023 return GS_OK;
5025 /* Go ahead and simplify constant constructors to VECTOR_CST. */
5026 if (TREE_CONSTANT (ctor))
5028 bool constant_p = true;
5029 tree value;
5031 /* Even when ctor is constant, it might contain non-*_CST
5032 elements, such as addresses or trapping values like
5033 1.0/0.0 - 1.0/0.0. Such expressions don't belong
5034 in VECTOR_CST nodes. */
5035 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
5036 if (!CONSTANT_CLASS_P (value))
5038 constant_p = false;
5039 break;
5042 if (constant_p)
5044 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
5045 break;
5048 TREE_CONSTANT (ctor) = 0;
5051 /* Vector types use CONSTRUCTOR all the way through gimple
5052 compilation as a general initializer. */
5053 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
5055 enum gimplify_status tret;
5056 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
5057 fb_rvalue);
5058 if (tret == GS_ERROR)
5059 ret = GS_ERROR;
5060 else if (TREE_STATIC (ctor)
5061 && !initializer_constant_valid_p (ce->value,
5062 TREE_TYPE (ce->value)))
5063 TREE_STATIC (ctor) = 0;
5065 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
5066 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
5068 break;
5070 default:
5071 /* So how did we get a CONSTRUCTOR for a scalar type? */
5072 gcc_unreachable ();
5075 if (ret == GS_ERROR)
5076 return GS_ERROR;
5077 /* If we have gimplified both sides of the initializer but have
5078 not emitted an assignment, do so now. */
5079 if (*expr_p)
5081 tree lhs = TREE_OPERAND (*expr_p, 0);
5082 tree rhs = TREE_OPERAND (*expr_p, 1);
5083 if (want_value && object == lhs)
5084 lhs = unshare_expr (lhs);
5085 gassign *init = gimple_build_assign (lhs, rhs);
5086 gimplify_seq_add_stmt (pre_p, init);
5088 if (want_value)
5090 *expr_p = object;
5091 return GS_OK;
5093 else
5095 *expr_p = NULL;
5096 return GS_ALL_DONE;
5100 /* Given a pointer value OP0, return a simplified version of an
5101 indirection through OP0, or NULL_TREE if no simplification is
5102 possible. This may only be applied to a rhs of an expression.
5103 Note that the resulting type may be different from the type pointed
5104 to in the sense that it is still compatible from the langhooks
5105 point of view. */
5107 static tree
5108 gimple_fold_indirect_ref_rhs (tree t)
5110 return gimple_fold_indirect_ref (t);
5113 /* Subroutine of gimplify_modify_expr to do simplifications of
5114 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
5115 something changes. */
5117 static enum gimplify_status
5118 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
5119 gimple_seq *pre_p, gimple_seq *post_p,
5120 bool want_value)
5122 enum gimplify_status ret = GS_UNHANDLED;
5123 bool changed;
5127 changed = false;
5128 switch (TREE_CODE (*from_p))
5130 case VAR_DECL:
5131 /* If we're assigning from a read-only variable initialized with
5132 a constructor, do the direct assignment from the constructor,
5133 but only if neither source nor target are volatile since this
5134 latter assignment might end up being done on a per-field basis. */
5135 if (DECL_INITIAL (*from_p)
5136 && TREE_READONLY (*from_p)
5137 && !TREE_THIS_VOLATILE (*from_p)
5138 && !TREE_THIS_VOLATILE (*to_p)
5139 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
5141 tree old_from = *from_p;
5142 enum gimplify_status subret;
5144 /* Move the constructor into the RHS. */
5145 *from_p = unshare_expr (DECL_INITIAL (*from_p));
5147 /* Let's see if gimplify_init_constructor will need to put
5148 it in memory. */
5149 subret = gimplify_init_constructor (expr_p, NULL, NULL,
5150 false, true);
5151 if (subret == GS_ERROR)
5153 /* If so, revert the change. */
5154 *from_p = old_from;
5156 else
5158 ret = GS_OK;
5159 changed = true;
5162 break;
5163 case INDIRECT_REF:
5165 /* If we have code like
5167 *(const A*)(A*)&x
5169 where the type of "x" is a (possibly cv-qualified variant
5170 of "A"), treat the entire expression as identical to "x".
5171 This kind of code arises in C++ when an object is bound
5172 to a const reference, and if "x" is a TARGET_EXPR we want
5173 to take advantage of the optimization below. */
5174 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
5175 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
5176 if (t)
5178 if (TREE_THIS_VOLATILE (t) != volatile_p)
5180 if (DECL_P (t))
5181 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
5182 build_fold_addr_expr (t));
5183 if (REFERENCE_CLASS_P (t))
5184 TREE_THIS_VOLATILE (t) = volatile_p;
5186 *from_p = t;
5187 ret = GS_OK;
5188 changed = true;
5190 break;
5193 case TARGET_EXPR:
5195 /* If we are initializing something from a TARGET_EXPR, strip the
5196 TARGET_EXPR and initialize it directly, if possible. This can't
5197 be done if the initializer is void, since that implies that the
5198 temporary is set in some non-trivial way.
5200 ??? What about code that pulls out the temp and uses it
5201 elsewhere? I think that such code never uses the TARGET_EXPR as
5202 an initializer. If I'm wrong, we'll die because the temp won't
5203 have any RTL. In that case, I guess we'll need to replace
5204 references somehow. */
5205 tree init = TARGET_EXPR_INITIAL (*from_p);
5207 if (init
5208 && (TREE_CODE (*expr_p) != MODIFY_EXPR
5209 || !TARGET_EXPR_NO_ELIDE (*from_p))
5210 && !VOID_TYPE_P (TREE_TYPE (init)))
5212 *from_p = init;
5213 ret = GS_OK;
5214 changed = true;
5217 break;
5219 case COMPOUND_EXPR:
5220 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5221 caught. */
5222 gimplify_compound_expr (from_p, pre_p, true);
5223 ret = GS_OK;
5224 changed = true;
5225 break;
5227 case CONSTRUCTOR:
5228 /* If we already made some changes, let the front end have a
5229 crack at this before we break it down. */
5230 if (ret != GS_UNHANDLED)
5231 break;
5232 /* If we're initializing from a CONSTRUCTOR, break this into
5233 individual MODIFY_EXPRs. */
5234 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5235 false);
5237 case COND_EXPR:
5238 /* If we're assigning to a non-register type, push the assignment
5239 down into the branches. This is mandatory for ADDRESSABLE types,
5240 since we cannot generate temporaries for such, but it saves a
5241 copy in other cases as well. */
5242 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5244 /* This code should mirror the code in gimplify_cond_expr. */
5245 enum tree_code code = TREE_CODE (*expr_p);
5246 tree cond = *from_p;
5247 tree result = *to_p;
5249 ret = gimplify_expr (&result, pre_p, post_p,
5250 is_gimple_lvalue, fb_lvalue);
5251 if (ret != GS_ERROR)
5252 ret = GS_OK;
5254 /* If we are going to write RESULT more than once, clear
5255 TREE_READONLY flag, otherwise we might incorrectly promote
5256 the variable to static const and initialize it at compile
5257 time in one of the branches. */
5258 if (VAR_P (result)
5259 && TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node
5260 && TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5261 TREE_READONLY (result) = 0;
5262 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5263 TREE_OPERAND (cond, 1)
5264 = build2 (code, void_type_node, result,
5265 TREE_OPERAND (cond, 1));
5266 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5267 TREE_OPERAND (cond, 2)
5268 = build2 (code, void_type_node, unshare_expr (result),
5269 TREE_OPERAND (cond, 2));
5271 TREE_TYPE (cond) = void_type_node;
5272 recalculate_side_effects (cond);
5274 if (want_value)
5276 gimplify_and_add (cond, pre_p);
5277 *expr_p = unshare_expr (result);
5279 else
5280 *expr_p = cond;
5281 return ret;
5283 break;
5285 case CALL_EXPR:
5286 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5287 return slot so that we don't generate a temporary. */
5288 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5289 && aggregate_value_p (*from_p, *from_p))
5291 bool use_target;
5293 if (!(rhs_predicate_for (*to_p))(*from_p))
5294 /* If we need a temporary, *to_p isn't accurate. */
5295 use_target = false;
5296 /* It's OK to use the return slot directly unless it's an NRV. */
5297 else if (TREE_CODE (*to_p) == RESULT_DECL
5298 && DECL_NAME (*to_p) == NULL_TREE
5299 && needs_to_live_in_memory (*to_p))
5300 use_target = true;
5301 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5302 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5303 /* Don't force regs into memory. */
5304 use_target = false;
5305 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5306 /* It's OK to use the target directly if it's being
5307 initialized. */
5308 use_target = true;
5309 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5310 != INTEGER_CST)
5311 /* Always use the target and thus RSO for variable-sized types.
5312 GIMPLE cannot deal with a variable-sized assignment
5313 embedded in a call statement. */
5314 use_target = true;
5315 else if (TREE_CODE (*to_p) != SSA_NAME
5316 && (!is_gimple_variable (*to_p)
5317 || needs_to_live_in_memory (*to_p)))
5318 /* Don't use the original target if it's already addressable;
5319 if its address escapes, and the called function uses the
5320 NRV optimization, a conforming program could see *to_p
5321 change before the called function returns; see c++/19317.
5322 When optimizing, the return_slot pass marks more functions
5323 as safe after we have escape info. */
5324 use_target = false;
5325 else
5326 use_target = true;
5328 if (use_target)
5330 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5331 mark_addressable (*to_p);
5334 break;
5336 case WITH_SIZE_EXPR:
5337 /* Likewise for calls that return an aggregate of non-constant size,
5338 since we would not be able to generate a temporary at all. */
5339 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5341 *from_p = TREE_OPERAND (*from_p, 0);
5342 /* We don't change ret in this case because the
5343 WITH_SIZE_EXPR might have been added in
5344 gimplify_modify_expr, so returning GS_OK would lead to an
5345 infinite loop. */
5346 changed = true;
5348 break;
5350 /* If we're initializing from a container, push the initialization
5351 inside it. */
5352 case CLEANUP_POINT_EXPR:
5353 case BIND_EXPR:
5354 case STATEMENT_LIST:
5356 tree wrap = *from_p;
5357 tree t;
5359 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5360 fb_lvalue);
5361 if (ret != GS_ERROR)
5362 ret = GS_OK;
5364 t = voidify_wrapper_expr (wrap, *expr_p);
5365 gcc_assert (t == *expr_p);
5367 if (want_value)
5369 gimplify_and_add (wrap, pre_p);
5370 *expr_p = unshare_expr (*to_p);
5372 else
5373 *expr_p = wrap;
5374 return GS_OK;
5377 case COMPOUND_LITERAL_EXPR:
5379 tree complit = TREE_OPERAND (*expr_p, 1);
5380 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5381 tree decl = DECL_EXPR_DECL (decl_s);
5382 tree init = DECL_INITIAL (decl);
5384 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5385 into struct T x = { 0, 1, 2 } if the address of the
5386 compound literal has never been taken. */
5387 if (!TREE_ADDRESSABLE (complit)
5388 && !TREE_ADDRESSABLE (decl)
5389 && init)
5391 *expr_p = copy_node (*expr_p);
5392 TREE_OPERAND (*expr_p, 1) = init;
5393 return GS_OK;
5397 default:
5398 break;
5401 while (changed);
5403 return ret;
5407 /* Return true if T looks like a valid GIMPLE statement. */
5409 static bool
5410 is_gimple_stmt (tree t)
5412 const enum tree_code code = TREE_CODE (t);
5414 switch (code)
5416 case NOP_EXPR:
5417 /* The only valid NOP_EXPR is the empty statement. */
5418 return IS_EMPTY_STMT (t);
5420 case BIND_EXPR:
5421 case COND_EXPR:
5422 /* These are only valid if they're void. */
5423 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5425 case SWITCH_EXPR:
5426 case GOTO_EXPR:
5427 case RETURN_EXPR:
5428 case LABEL_EXPR:
5429 case CASE_LABEL_EXPR:
5430 case TRY_CATCH_EXPR:
5431 case TRY_FINALLY_EXPR:
5432 case EH_FILTER_EXPR:
5433 case CATCH_EXPR:
5434 case ASM_EXPR:
5435 case STATEMENT_LIST:
5436 case OACC_PARALLEL:
5437 case OACC_KERNELS:
5438 case OACC_DATA:
5439 case OACC_HOST_DATA:
5440 case OACC_DECLARE:
5441 case OACC_UPDATE:
5442 case OACC_ENTER_DATA:
5443 case OACC_EXIT_DATA:
5444 case OACC_CACHE:
5445 case OMP_PARALLEL:
5446 case OMP_FOR:
5447 case OMP_SIMD:
5448 case OMP_DISTRIBUTE:
5449 case OACC_LOOP:
5450 case OMP_SECTIONS:
5451 case OMP_SECTION:
5452 case OMP_SINGLE:
5453 case OMP_MASTER:
5454 case OMP_TASKGROUP:
5455 case OMP_ORDERED:
5456 case OMP_CRITICAL:
5457 case OMP_TASK:
5458 case OMP_TARGET:
5459 case OMP_TARGET_DATA:
5460 case OMP_TARGET_UPDATE:
5461 case OMP_TARGET_ENTER_DATA:
5462 case OMP_TARGET_EXIT_DATA:
5463 case OMP_TASKLOOP:
5464 case OMP_TEAMS:
5465 /* These are always void. */
5466 return true;
5468 case CALL_EXPR:
5469 case MODIFY_EXPR:
5470 case PREDICT_EXPR:
5471 /* These are valid regardless of their type. */
5472 return true;
5474 default:
5475 return false;
5480 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5481 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5482 DECL_GIMPLE_REG_P set.
5484 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5485 other, unmodified part of the complex object just before the total store.
5486 As a consequence, if the object is still uninitialized, an undefined value
5487 will be loaded into a register, which may result in a spurious exception
5488 if the register is floating-point and the value happens to be a signaling
5489 NaN for example. Then the fully-fledged complex operations lowering pass
5490 followed by a DCE pass are necessary in order to fix things up. */
5492 static enum gimplify_status
5493 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5494 bool want_value)
5496 enum tree_code code, ocode;
5497 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5499 lhs = TREE_OPERAND (*expr_p, 0);
5500 rhs = TREE_OPERAND (*expr_p, 1);
5501 code = TREE_CODE (lhs);
5502 lhs = TREE_OPERAND (lhs, 0);
5504 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5505 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5506 TREE_NO_WARNING (other) = 1;
5507 other = get_formal_tmp_var (other, pre_p);
5509 realpart = code == REALPART_EXPR ? rhs : other;
5510 imagpart = code == REALPART_EXPR ? other : rhs;
5512 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5513 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5514 else
5515 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5517 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5518 *expr_p = (want_value) ? rhs : NULL_TREE;
5520 return GS_ALL_DONE;
5523 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5525 modify_expr
5526 : varname '=' rhs
5527 | '*' ID '=' rhs
5529 PRE_P points to the list where side effects that must happen before
5530 *EXPR_P should be stored.
5532 POST_P points to the list where side effects that must happen after
5533 *EXPR_P should be stored.
5535 WANT_VALUE is nonzero iff we want to use the value of this expression
5536 in another expression. */
5538 static enum gimplify_status
5539 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5540 bool want_value)
5542 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5543 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5544 enum gimplify_status ret = GS_UNHANDLED;
5545 gimple *assign;
5546 location_t loc = EXPR_LOCATION (*expr_p);
5547 gimple_stmt_iterator gsi;
5549 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5550 || TREE_CODE (*expr_p) == INIT_EXPR);
5552 /* Trying to simplify a clobber using normal logic doesn't work,
5553 so handle it here. */
5554 if (TREE_CLOBBER_P (*from_p))
5556 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5557 if (ret == GS_ERROR)
5558 return ret;
5559 gcc_assert (!want_value);
5560 if (!VAR_P (*to_p) && TREE_CODE (*to_p) != MEM_REF)
5562 tree addr = get_initialized_tmp_var (build_fold_addr_expr (*to_p),
5563 pre_p, post_p);
5564 *to_p = build_simple_mem_ref_loc (EXPR_LOCATION (*to_p), addr);
5566 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5567 *expr_p = NULL;
5568 return GS_ALL_DONE;
5571 /* Insert pointer conversions required by the middle-end that are not
5572 required by the frontend. This fixes middle-end type checking for
5573 for example gcc.dg/redecl-6.c. */
5574 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5576 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5577 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5578 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5581 /* See if any simplifications can be done based on what the RHS is. */
5582 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5583 want_value);
5584 if (ret != GS_UNHANDLED)
5585 return ret;
5587 /* For zero sized types only gimplify the left hand side and right hand
5588 side as statements and throw away the assignment. Do this after
5589 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5590 types properly. */
5591 if (zero_sized_type (TREE_TYPE (*from_p))
5592 && !want_value
5593 /* Don't do this for calls that return addressable types, expand_call
5594 relies on those having a lhs. */
5595 && !(TREE_ADDRESSABLE (TREE_TYPE (*from_p))
5596 && TREE_CODE (*from_p) == CALL_EXPR))
5598 gimplify_stmt (from_p, pre_p);
5599 gimplify_stmt (to_p, pre_p);
5600 *expr_p = NULL_TREE;
5601 return GS_ALL_DONE;
5604 /* If the value being copied is of variable width, compute the length
5605 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5606 before gimplifying any of the operands so that we can resolve any
5607 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5608 the size of the expression to be copied, not of the destination, so
5609 that is what we must do here. */
5610 maybe_with_size_expr (from_p);
5612 /* As a special case, we have to temporarily allow for assignments
5613 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5614 a toplevel statement, when gimplifying the GENERIC expression
5615 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5616 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5618 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5619 prevent gimplify_expr from trying to create a new temporary for
5620 foo's LHS, we tell it that it should only gimplify until it
5621 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5622 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5623 and all we need to do here is set 'a' to be its LHS. */
5625 /* Gimplify the RHS first for C++17 and bug 71104. */
5626 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5627 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5628 if (ret == GS_ERROR)
5629 return ret;
5631 /* Then gimplify the LHS. */
5632 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5633 twice we have to make sure to gimplify into non-SSA as otherwise
5634 the abnormal edge added later will make those defs not dominate
5635 their uses.
5636 ??? Technically this applies only to the registers used in the
5637 resulting non-register *TO_P. */
5638 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5639 if (saved_into_ssa
5640 && TREE_CODE (*from_p) == CALL_EXPR
5641 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5642 gimplify_ctxp->into_ssa = false;
5643 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5644 gimplify_ctxp->into_ssa = saved_into_ssa;
5645 if (ret == GS_ERROR)
5646 return ret;
5648 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5649 guess for the predicate was wrong. */
5650 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5651 if (final_pred != initial_pred)
5653 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5654 if (ret == GS_ERROR)
5655 return ret;
5658 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5659 size as argument to the call. */
5660 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5662 tree call = TREE_OPERAND (*from_p, 0);
5663 tree vlasize = TREE_OPERAND (*from_p, 1);
5665 if (TREE_CODE (call) == CALL_EXPR
5666 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5668 int nargs = call_expr_nargs (call);
5669 tree type = TREE_TYPE (call);
5670 tree ap = CALL_EXPR_ARG (call, 0);
5671 tree tag = CALL_EXPR_ARG (call, 1);
5672 tree aptag = CALL_EXPR_ARG (call, 2);
5673 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5674 IFN_VA_ARG, type,
5675 nargs + 1, ap, tag,
5676 aptag, vlasize);
5677 TREE_OPERAND (*from_p, 0) = newcall;
5681 /* Now see if the above changed *from_p to something we handle specially. */
5682 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5683 want_value);
5684 if (ret != GS_UNHANDLED)
5685 return ret;
5687 /* If we've got a variable sized assignment between two lvalues (i.e. does
5688 not involve a call), then we can make things a bit more straightforward
5689 by converting the assignment to memcpy or memset. */
5690 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5692 tree from = TREE_OPERAND (*from_p, 0);
5693 tree size = TREE_OPERAND (*from_p, 1);
5695 if (TREE_CODE (from) == CONSTRUCTOR)
5696 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5698 if (is_gimple_addressable (from))
5700 *from_p = from;
5701 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5702 pre_p);
5706 /* Transform partial stores to non-addressable complex variables into
5707 total stores. This allows us to use real instead of virtual operands
5708 for these variables, which improves optimization. */
5709 if ((TREE_CODE (*to_p) == REALPART_EXPR
5710 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5711 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5712 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5714 /* Try to alleviate the effects of the gimplification creating artificial
5715 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5716 make sure not to create DECL_DEBUG_EXPR links across functions. */
5717 if (!gimplify_ctxp->into_ssa
5718 && VAR_P (*from_p)
5719 && DECL_IGNORED_P (*from_p)
5720 && DECL_P (*to_p)
5721 && !DECL_IGNORED_P (*to_p)
5722 && decl_function_context (*to_p) == current_function_decl
5723 && decl_function_context (*from_p) == current_function_decl)
5725 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5726 DECL_NAME (*from_p)
5727 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5728 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5729 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5732 if (want_value && TREE_THIS_VOLATILE (*to_p))
5733 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5735 if (TREE_CODE (*from_p) == CALL_EXPR)
5737 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5738 instead of a GIMPLE_ASSIGN. */
5739 gcall *call_stmt;
5740 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5742 /* Gimplify internal functions created in the FEs. */
5743 int nargs = call_expr_nargs (*from_p), i;
5744 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5745 auto_vec<tree> vargs (nargs);
5747 for (i = 0; i < nargs; i++)
5749 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5750 EXPR_LOCATION (*from_p));
5751 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5753 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5754 gimple_call_set_nothrow (call_stmt, TREE_NOTHROW (*from_p));
5755 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5757 else
5759 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5760 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5761 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5762 tree fndecl = get_callee_fndecl (*from_p);
5763 if (fndecl
5764 && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
5765 && call_expr_nargs (*from_p) == 3)
5766 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5767 CALL_EXPR_ARG (*from_p, 0),
5768 CALL_EXPR_ARG (*from_p, 1),
5769 CALL_EXPR_ARG (*from_p, 2));
5770 else
5772 call_stmt = gimple_build_call_from_tree (*from_p, fnptrtype);
5775 notice_special_calls (call_stmt);
5776 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5777 gimple_call_set_lhs (call_stmt, *to_p);
5778 else if (TREE_CODE (*to_p) == SSA_NAME)
5779 /* The above is somewhat premature, avoid ICEing later for a
5780 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5781 ??? This doesn't make it a default-def. */
5782 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5784 assign = call_stmt;
5786 else
5788 assign = gimple_build_assign (*to_p, *from_p);
5789 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5790 if (COMPARISON_CLASS_P (*from_p))
5791 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5794 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5796 /* We should have got an SSA name from the start. */
5797 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5798 || ! gimple_in_ssa_p (cfun));
5801 gimplify_seq_add_stmt (pre_p, assign);
5802 gsi = gsi_last (*pre_p);
5803 maybe_fold_stmt (&gsi);
5805 if (want_value)
5807 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5808 return GS_OK;
5810 else
5811 *expr_p = NULL;
5813 return GS_ALL_DONE;
5816 /* Gimplify a comparison between two variable-sized objects. Do this
5817 with a call to BUILT_IN_MEMCMP. */
5819 static enum gimplify_status
5820 gimplify_variable_sized_compare (tree *expr_p)
5822 location_t loc = EXPR_LOCATION (*expr_p);
5823 tree op0 = TREE_OPERAND (*expr_p, 0);
5824 tree op1 = TREE_OPERAND (*expr_p, 1);
5825 tree t, arg, dest, src, expr;
5827 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5828 arg = unshare_expr (arg);
5829 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5830 src = build_fold_addr_expr_loc (loc, op1);
5831 dest = build_fold_addr_expr_loc (loc, op0);
5832 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5833 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5835 expr
5836 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5837 SET_EXPR_LOCATION (expr, loc);
5838 *expr_p = expr;
5840 return GS_OK;
5843 /* Gimplify a comparison between two aggregate objects of integral scalar
5844 mode as a comparison between the bitwise equivalent scalar values. */
5846 static enum gimplify_status
5847 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5849 location_t loc = EXPR_LOCATION (*expr_p);
5850 tree op0 = TREE_OPERAND (*expr_p, 0);
5851 tree op1 = TREE_OPERAND (*expr_p, 1);
5853 tree type = TREE_TYPE (op0);
5854 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5856 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5857 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5859 *expr_p
5860 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5862 return GS_OK;
5865 /* Gimplify an expression sequence. This function gimplifies each
5866 expression and rewrites the original expression with the last
5867 expression of the sequence in GIMPLE form.
5869 PRE_P points to the list where the side effects for all the
5870 expressions in the sequence will be emitted.
5872 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5874 static enum gimplify_status
5875 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5877 tree t = *expr_p;
5881 tree *sub_p = &TREE_OPERAND (t, 0);
5883 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5884 gimplify_compound_expr (sub_p, pre_p, false);
5885 else
5886 gimplify_stmt (sub_p, pre_p);
5888 t = TREE_OPERAND (t, 1);
5890 while (TREE_CODE (t) == COMPOUND_EXPR);
5892 *expr_p = t;
5893 if (want_value)
5894 return GS_OK;
5895 else
5897 gimplify_stmt (expr_p, pre_p);
5898 return GS_ALL_DONE;
5902 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5903 gimplify. After gimplification, EXPR_P will point to a new temporary
5904 that holds the original value of the SAVE_EXPR node.
5906 PRE_P points to the list where side effects that must happen before
5907 *EXPR_P should be stored. */
5909 static enum gimplify_status
5910 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5912 enum gimplify_status ret = GS_ALL_DONE;
5913 tree val;
5915 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5916 val = TREE_OPERAND (*expr_p, 0);
5918 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5919 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5921 /* The operand may be a void-valued expression. It is
5922 being executed only for its side-effects. */
5923 if (TREE_TYPE (val) == void_type_node)
5925 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5926 is_gimple_stmt, fb_none);
5927 val = NULL;
5929 else
5930 /* The temporary may not be an SSA name as later abnormal and EH
5931 control flow may invalidate use/def domination. When in SSA
5932 form then assume there are no such issues and SAVE_EXPRs only
5933 appear via GENERIC foldings. */
5934 val = get_initialized_tmp_var (val, pre_p, post_p,
5935 gimple_in_ssa_p (cfun));
5937 TREE_OPERAND (*expr_p, 0) = val;
5938 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5941 *expr_p = val;
5943 return ret;
5946 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5948 unary_expr
5949 : ...
5950 | '&' varname
5953 PRE_P points to the list where side effects that must happen before
5954 *EXPR_P should be stored.
5956 POST_P points to the list where side effects that must happen after
5957 *EXPR_P should be stored. */
5959 static enum gimplify_status
5960 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5962 tree expr = *expr_p;
5963 tree op0 = TREE_OPERAND (expr, 0);
5964 enum gimplify_status ret;
5965 location_t loc = EXPR_LOCATION (*expr_p);
5967 switch (TREE_CODE (op0))
5969 case INDIRECT_REF:
5970 do_indirect_ref:
5971 /* Check if we are dealing with an expression of the form '&*ptr'.
5972 While the front end folds away '&*ptr' into 'ptr', these
5973 expressions may be generated internally by the compiler (e.g.,
5974 builtins like __builtin_va_end). */
5975 /* Caution: the silent array decomposition semantics we allow for
5976 ADDR_EXPR means we can't always discard the pair. */
5977 /* Gimplification of the ADDR_EXPR operand may drop
5978 cv-qualification conversions, so make sure we add them if
5979 needed. */
5981 tree op00 = TREE_OPERAND (op0, 0);
5982 tree t_expr = TREE_TYPE (expr);
5983 tree t_op00 = TREE_TYPE (op00);
5985 if (!useless_type_conversion_p (t_expr, t_op00))
5986 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5987 *expr_p = op00;
5988 ret = GS_OK;
5990 break;
5992 case VIEW_CONVERT_EXPR:
5993 /* Take the address of our operand and then convert it to the type of
5994 this ADDR_EXPR.
5996 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5997 all clear. The impact of this transformation is even less clear. */
5999 /* If the operand is a useless conversion, look through it. Doing so
6000 guarantees that the ADDR_EXPR and its operand will remain of the
6001 same type. */
6002 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
6003 op0 = TREE_OPERAND (op0, 0);
6005 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
6006 build_fold_addr_expr_loc (loc,
6007 TREE_OPERAND (op0, 0)));
6008 ret = GS_OK;
6009 break;
6011 case MEM_REF:
6012 if (integer_zerop (TREE_OPERAND (op0, 1)))
6013 goto do_indirect_ref;
6015 /* fall through */
6017 default:
6018 /* If we see a call to a declared builtin or see its address
6019 being taken (we can unify those cases here) then we can mark
6020 the builtin for implicit generation by GCC. */
6021 if (TREE_CODE (op0) == FUNCTION_DECL
6022 && fndecl_built_in_p (op0, BUILT_IN_NORMAL)
6023 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
6024 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
6026 /* We use fb_either here because the C frontend sometimes takes
6027 the address of a call that returns a struct; see
6028 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
6029 the implied temporary explicit. */
6031 /* Make the operand addressable. */
6032 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
6033 is_gimple_addressable, fb_either);
6034 if (ret == GS_ERROR)
6035 break;
6037 /* Then mark it. Beware that it may not be possible to do so directly
6038 if a temporary has been created by the gimplification. */
6039 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
6041 op0 = TREE_OPERAND (expr, 0);
6043 /* For various reasons, the gimplification of the expression
6044 may have made a new INDIRECT_REF. */
6045 if (TREE_CODE (op0) == INDIRECT_REF)
6046 goto do_indirect_ref;
6048 mark_addressable (TREE_OPERAND (expr, 0));
6050 /* The FEs may end up building ADDR_EXPRs early on a decl with
6051 an incomplete type. Re-build ADDR_EXPRs in canonical form
6052 here. */
6053 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
6054 *expr_p = build_fold_addr_expr (op0);
6056 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
6057 recompute_tree_invariant_for_addr_expr (*expr_p);
6059 /* If we re-built the ADDR_EXPR add a conversion to the original type
6060 if required. */
6061 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
6062 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
6064 break;
6067 return ret;
6070 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
6071 value; output operands should be a gimple lvalue. */
6073 static enum gimplify_status
6074 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6076 tree expr;
6077 int noutputs;
6078 const char **oconstraints;
6079 int i;
6080 tree link;
6081 const char *constraint;
6082 bool allows_mem, allows_reg, is_inout;
6083 enum gimplify_status ret, tret;
6084 gasm *stmt;
6085 vec<tree, va_gc> *inputs;
6086 vec<tree, va_gc> *outputs;
6087 vec<tree, va_gc> *clobbers;
6088 vec<tree, va_gc> *labels;
6089 tree link_next;
6091 expr = *expr_p;
6092 noutputs = list_length (ASM_OUTPUTS (expr));
6093 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
6095 inputs = NULL;
6096 outputs = NULL;
6097 clobbers = NULL;
6098 labels = NULL;
6100 ret = GS_ALL_DONE;
6101 link_next = NULL_TREE;
6102 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
6104 bool ok;
6105 size_t constraint_len;
6107 link_next = TREE_CHAIN (link);
6109 oconstraints[i]
6110 = constraint
6111 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6112 constraint_len = strlen (constraint);
6113 if (constraint_len == 0)
6114 continue;
6116 ok = parse_output_constraint (&constraint, i, 0, 0,
6117 &allows_mem, &allows_reg, &is_inout);
6118 if (!ok)
6120 ret = GS_ERROR;
6121 is_inout = false;
6124 if (!allows_reg && allows_mem)
6125 mark_addressable (TREE_VALUE (link));
6127 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6128 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
6129 fb_lvalue | fb_mayfail);
6130 if (tret == GS_ERROR)
6132 error ("invalid lvalue in asm output %d", i);
6133 ret = tret;
6136 /* If the constraint does not allow memory make sure we gimplify
6137 it to a register if it is not already but its base is. This
6138 happens for complex and vector components. */
6139 if (!allows_mem)
6141 tree op = TREE_VALUE (link);
6142 if (! is_gimple_val (op)
6143 && is_gimple_reg_type (TREE_TYPE (op))
6144 && is_gimple_reg (get_base_address (op)))
6146 tree tem = create_tmp_reg (TREE_TYPE (op));
6147 tree ass;
6148 if (is_inout)
6150 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
6151 tem, unshare_expr (op));
6152 gimplify_and_add (ass, pre_p);
6154 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
6155 gimplify_and_add (ass, post_p);
6157 TREE_VALUE (link) = tem;
6158 tret = GS_OK;
6162 vec_safe_push (outputs, link);
6163 TREE_CHAIN (link) = NULL_TREE;
6165 if (is_inout)
6167 /* An input/output operand. To give the optimizers more
6168 flexibility, split it into separate input and output
6169 operands. */
6170 tree input;
6171 /* Buffer big enough to format a 32-bit UINT_MAX into. */
6172 char buf[11];
6174 /* Turn the in/out constraint into an output constraint. */
6175 char *p = xstrdup (constraint);
6176 p[0] = '=';
6177 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
6179 /* And add a matching input constraint. */
6180 if (allows_reg)
6182 sprintf (buf, "%u", i);
6184 /* If there are multiple alternatives in the constraint,
6185 handle each of them individually. Those that allow register
6186 will be replaced with operand number, the others will stay
6187 unchanged. */
6188 if (strchr (p, ',') != NULL)
6190 size_t len = 0, buflen = strlen (buf);
6191 char *beg, *end, *str, *dst;
6193 for (beg = p + 1;;)
6195 end = strchr (beg, ',');
6196 if (end == NULL)
6197 end = strchr (beg, '\0');
6198 if ((size_t) (end - beg) < buflen)
6199 len += buflen + 1;
6200 else
6201 len += end - beg + 1;
6202 if (*end)
6203 beg = end + 1;
6204 else
6205 break;
6208 str = (char *) alloca (len);
6209 for (beg = p + 1, dst = str;;)
6211 const char *tem;
6212 bool mem_p, reg_p, inout_p;
6214 end = strchr (beg, ',');
6215 if (end)
6216 *end = '\0';
6217 beg[-1] = '=';
6218 tem = beg - 1;
6219 parse_output_constraint (&tem, i, 0, 0,
6220 &mem_p, &reg_p, &inout_p);
6221 if (dst != str)
6222 *dst++ = ',';
6223 if (reg_p)
6225 memcpy (dst, buf, buflen);
6226 dst += buflen;
6228 else
6230 if (end)
6231 len = end - beg;
6232 else
6233 len = strlen (beg);
6234 memcpy (dst, beg, len);
6235 dst += len;
6237 if (end)
6238 beg = end + 1;
6239 else
6240 break;
6242 *dst = '\0';
6243 input = build_string (dst - str, str);
6245 else
6246 input = build_string (strlen (buf), buf);
6248 else
6249 input = build_string (constraint_len - 1, constraint + 1);
6251 free (p);
6253 input = build_tree_list (build_tree_list (NULL_TREE, input),
6254 unshare_expr (TREE_VALUE (link)));
6255 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6259 link_next = NULL_TREE;
6260 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6262 link_next = TREE_CHAIN (link);
6263 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6264 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6265 oconstraints, &allows_mem, &allows_reg);
6267 /* If we can't make copies, we can only accept memory. */
6268 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6270 if (allows_mem)
6271 allows_reg = 0;
6272 else
6274 error ("impossible constraint in %<asm%>");
6275 error ("non-memory input %d must stay in memory", i);
6276 return GS_ERROR;
6280 /* If the operand is a memory input, it should be an lvalue. */
6281 if (!allows_reg && allows_mem)
6283 tree inputv = TREE_VALUE (link);
6284 STRIP_NOPS (inputv);
6285 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6286 || TREE_CODE (inputv) == PREINCREMENT_EXPR
6287 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6288 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6289 || TREE_CODE (inputv) == MODIFY_EXPR)
6290 TREE_VALUE (link) = error_mark_node;
6291 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6292 is_gimple_lvalue, fb_lvalue | fb_mayfail);
6293 if (tret != GS_ERROR)
6295 /* Unlike output operands, memory inputs are not guaranteed
6296 to be lvalues by the FE, and while the expressions are
6297 marked addressable there, if it is e.g. a statement
6298 expression, temporaries in it might not end up being
6299 addressable. They might be already used in the IL and thus
6300 it is too late to make them addressable now though. */
6301 tree x = TREE_VALUE (link);
6302 while (handled_component_p (x))
6303 x = TREE_OPERAND (x, 0);
6304 if (TREE_CODE (x) == MEM_REF
6305 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6306 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6307 if ((VAR_P (x)
6308 || TREE_CODE (x) == PARM_DECL
6309 || TREE_CODE (x) == RESULT_DECL)
6310 && !TREE_ADDRESSABLE (x)
6311 && is_gimple_reg (x))
6313 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6314 input_location), 0,
6315 "memory input %d is not directly addressable",
6317 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6320 mark_addressable (TREE_VALUE (link));
6321 if (tret == GS_ERROR)
6323 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6324 "memory input %d is not directly addressable", i);
6325 ret = tret;
6328 else
6330 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6331 is_gimple_asm_val, fb_rvalue);
6332 if (tret == GS_ERROR)
6333 ret = tret;
6336 TREE_CHAIN (link) = NULL_TREE;
6337 vec_safe_push (inputs, link);
6340 link_next = NULL_TREE;
6341 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6343 link_next = TREE_CHAIN (link);
6344 TREE_CHAIN (link) = NULL_TREE;
6345 vec_safe_push (clobbers, link);
6348 link_next = NULL_TREE;
6349 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6351 link_next = TREE_CHAIN (link);
6352 TREE_CHAIN (link) = NULL_TREE;
6353 vec_safe_push (labels, link);
6356 /* Do not add ASMs with errors to the gimple IL stream. */
6357 if (ret != GS_ERROR)
6359 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6360 inputs, outputs, clobbers, labels);
6362 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6363 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6364 gimple_asm_set_inline (stmt, ASM_INLINE_P (expr));
6366 gimplify_seq_add_stmt (pre_p, stmt);
6369 return ret;
6372 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6373 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6374 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6375 return to this function.
6377 FIXME should we complexify the prequeue handling instead? Or use flags
6378 for all the cleanups and let the optimizer tighten them up? The current
6379 code seems pretty fragile; it will break on a cleanup within any
6380 non-conditional nesting. But any such nesting would be broken, anyway;
6381 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6382 and continues out of it. We can do that at the RTL level, though, so
6383 having an optimizer to tighten up try/finally regions would be a Good
6384 Thing. */
6386 static enum gimplify_status
6387 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6389 gimple_stmt_iterator iter;
6390 gimple_seq body_sequence = NULL;
6392 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6394 /* We only care about the number of conditions between the innermost
6395 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6396 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6397 int old_conds = gimplify_ctxp->conditions;
6398 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6399 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6400 gimplify_ctxp->conditions = 0;
6401 gimplify_ctxp->conditional_cleanups = NULL;
6402 gimplify_ctxp->in_cleanup_point_expr = true;
6404 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6406 gimplify_ctxp->conditions = old_conds;
6407 gimplify_ctxp->conditional_cleanups = old_cleanups;
6408 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6410 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6412 gimple *wce = gsi_stmt (iter);
6414 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6416 if (gsi_one_before_end_p (iter))
6418 /* Note that gsi_insert_seq_before and gsi_remove do not
6419 scan operands, unlike some other sequence mutators. */
6420 if (!gimple_wce_cleanup_eh_only (wce))
6421 gsi_insert_seq_before_without_update (&iter,
6422 gimple_wce_cleanup (wce),
6423 GSI_SAME_STMT);
6424 gsi_remove (&iter, true);
6425 break;
6427 else
6429 gtry *gtry;
6430 gimple_seq seq;
6431 enum gimple_try_flags kind;
6433 if (gimple_wce_cleanup_eh_only (wce))
6434 kind = GIMPLE_TRY_CATCH;
6435 else
6436 kind = GIMPLE_TRY_FINALLY;
6437 seq = gsi_split_seq_after (iter);
6439 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6440 /* Do not use gsi_replace here, as it may scan operands.
6441 We want to do a simple structural modification only. */
6442 gsi_set_stmt (&iter, gtry);
6443 iter = gsi_start (gtry->eval);
6446 else
6447 gsi_next (&iter);
6450 gimplify_seq_add_seq (pre_p, body_sequence);
6451 if (temp)
6453 *expr_p = temp;
6454 return GS_OK;
6456 else
6458 *expr_p = NULL;
6459 return GS_ALL_DONE;
6463 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6464 is the cleanup action required. EH_ONLY is true if the cleanup should
6465 only be executed if an exception is thrown, not on normal exit.
6466 If FORCE_UNCOND is true perform the cleanup unconditionally; this is
6467 only valid for clobbers. */
6469 static void
6470 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p,
6471 bool force_uncond = false)
6473 gimple *wce;
6474 gimple_seq cleanup_stmts = NULL;
6476 /* Errors can result in improperly nested cleanups. Which results in
6477 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6478 if (seen_error ())
6479 return;
6481 if (gimple_conditional_context ())
6483 /* If we're in a conditional context, this is more complex. We only
6484 want to run the cleanup if we actually ran the initialization that
6485 necessitates it, but we want to run it after the end of the
6486 conditional context. So we wrap the try/finally around the
6487 condition and use a flag to determine whether or not to actually
6488 run the destructor. Thus
6490 test ? f(A()) : 0
6492 becomes (approximately)
6494 flag = 0;
6495 try {
6496 if (test) { A::A(temp); flag = 1; val = f(temp); }
6497 else { val = 0; }
6498 } finally {
6499 if (flag) A::~A(temp);
6503 if (force_uncond)
6505 gimplify_stmt (&cleanup, &cleanup_stmts);
6506 wce = gimple_build_wce (cleanup_stmts);
6507 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6509 else
6511 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6512 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6513 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6515 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6516 gimplify_stmt (&cleanup, &cleanup_stmts);
6517 wce = gimple_build_wce (cleanup_stmts);
6519 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6520 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6521 gimplify_seq_add_stmt (pre_p, ftrue);
6523 /* Because of this manipulation, and the EH edges that jump
6524 threading cannot redirect, the temporary (VAR) will appear
6525 to be used uninitialized. Don't warn. */
6526 TREE_NO_WARNING (var) = 1;
6529 else
6531 gimplify_stmt (&cleanup, &cleanup_stmts);
6532 wce = gimple_build_wce (cleanup_stmts);
6533 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6534 gimplify_seq_add_stmt (pre_p, wce);
6538 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6540 static enum gimplify_status
6541 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6543 tree targ = *expr_p;
6544 tree temp = TARGET_EXPR_SLOT (targ);
6545 tree init = TARGET_EXPR_INITIAL (targ);
6546 enum gimplify_status ret;
6548 bool unpoison_empty_seq = false;
6549 gimple_stmt_iterator unpoison_it;
6551 if (init)
6553 tree cleanup = NULL_TREE;
6555 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6556 to the temps list. Handle also variable length TARGET_EXPRs. */
6557 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6559 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6560 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6561 gimplify_vla_decl (temp, pre_p);
6563 else
6565 /* Save location where we need to place unpoisoning. It's possible
6566 that a variable will be converted to needs_to_live_in_memory. */
6567 unpoison_it = gsi_last (*pre_p);
6568 unpoison_empty_seq = gsi_end_p (unpoison_it);
6570 gimple_add_tmp_var (temp);
6573 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6574 expression is supposed to initialize the slot. */
6575 if (VOID_TYPE_P (TREE_TYPE (init)))
6576 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6577 else
6579 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6580 init = init_expr;
6581 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6582 init = NULL;
6583 ggc_free (init_expr);
6585 if (ret == GS_ERROR)
6587 /* PR c++/28266 Make sure this is expanded only once. */
6588 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6589 return GS_ERROR;
6591 if (init)
6592 gimplify_and_add (init, pre_p);
6594 /* If needed, push the cleanup for the temp. */
6595 if (TARGET_EXPR_CLEANUP (targ))
6597 if (CLEANUP_EH_ONLY (targ))
6598 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6599 CLEANUP_EH_ONLY (targ), pre_p);
6600 else
6601 cleanup = TARGET_EXPR_CLEANUP (targ);
6604 /* Add a clobber for the temporary going out of scope, like
6605 gimplify_bind_expr. */
6606 if (gimplify_ctxp->in_cleanup_point_expr
6607 && needs_to_live_in_memory (temp))
6609 if (flag_stack_reuse == SR_ALL)
6611 tree clobber = build_clobber (TREE_TYPE (temp));
6612 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6613 gimple_push_cleanup (temp, clobber, false, pre_p, true);
6615 if (asan_poisoned_variables
6616 && DECL_ALIGN (temp) <= MAX_SUPPORTED_STACK_ALIGNMENT
6617 && dbg_cnt (asan_use_after_scope)
6618 && !gimplify_omp_ctxp)
6620 tree asan_cleanup = build_asan_poison_call_expr (temp);
6621 if (asan_cleanup)
6623 if (unpoison_empty_seq)
6624 unpoison_it = gsi_start (*pre_p);
6626 asan_poison_variable (temp, false, &unpoison_it,
6627 unpoison_empty_seq);
6628 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6632 if (cleanup)
6633 gimple_push_cleanup (temp, cleanup, false, pre_p);
6635 /* Only expand this once. */
6636 TREE_OPERAND (targ, 3) = init;
6637 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6639 else
6640 /* We should have expanded this before. */
6641 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6643 *expr_p = temp;
6644 return GS_OK;
6647 /* Gimplification of expression trees. */
6649 /* Gimplify an expression which appears at statement context. The
6650 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6651 NULL, a new sequence is allocated.
6653 Return true if we actually added a statement to the queue. */
6655 bool
6656 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6658 gimple_seq_node last;
6660 last = gimple_seq_last (*seq_p);
6661 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6662 return last != gimple_seq_last (*seq_p);
6665 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6666 to CTX. If entries already exist, force them to be some flavor of private.
6667 If there is no enclosing parallel, do nothing. */
6669 void
6670 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6672 splay_tree_node n;
6674 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6675 return;
6679 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6680 if (n != NULL)
6682 if (n->value & GOVD_SHARED)
6683 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6684 else if (n->value & GOVD_MAP)
6685 n->value |= GOVD_MAP_TO_ONLY;
6686 else
6687 return;
6689 else if ((ctx->region_type & ORT_TARGET) != 0)
6691 if (ctx->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
6692 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6693 else
6694 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6696 else if (ctx->region_type != ORT_WORKSHARE
6697 && ctx->region_type != ORT_TASKGROUP
6698 && ctx->region_type != ORT_SIMD
6699 && ctx->region_type != ORT_ACC
6700 && !(ctx->region_type & ORT_TARGET_DATA))
6701 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6703 ctx = ctx->outer_context;
6705 while (ctx);
6708 /* Similarly for each of the type sizes of TYPE. */
6710 static void
6711 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6713 if (type == NULL || type == error_mark_node)
6714 return;
6715 type = TYPE_MAIN_VARIANT (type);
6717 if (ctx->privatized_types->add (type))
6718 return;
6720 switch (TREE_CODE (type))
6722 case INTEGER_TYPE:
6723 case ENUMERAL_TYPE:
6724 case BOOLEAN_TYPE:
6725 case REAL_TYPE:
6726 case FIXED_POINT_TYPE:
6727 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6728 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6729 break;
6731 case ARRAY_TYPE:
6732 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6733 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6734 break;
6736 case RECORD_TYPE:
6737 case UNION_TYPE:
6738 case QUAL_UNION_TYPE:
6740 tree field;
6741 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6742 if (TREE_CODE (field) == FIELD_DECL)
6744 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6745 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6748 break;
6750 case POINTER_TYPE:
6751 case REFERENCE_TYPE:
6752 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6753 break;
6755 default:
6756 break;
6759 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6760 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6761 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6764 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6766 static void
6767 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6769 splay_tree_node n;
6770 unsigned int nflags;
6771 tree t;
6773 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6774 return;
6776 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6777 there are constructors involved somewhere. Exception is a shared clause,
6778 there is nothing privatized in that case. */
6779 if ((flags & GOVD_SHARED) == 0
6780 && (TREE_ADDRESSABLE (TREE_TYPE (decl))
6781 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
6782 flags |= GOVD_SEEN;
6784 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6785 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6787 /* We shouldn't be re-adding the decl with the same data
6788 sharing class. */
6789 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6790 nflags = n->value | flags;
6791 /* The only combination of data sharing classes we should see is
6792 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6793 reduction variables to be used in data sharing clauses. */
6794 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6795 || ((nflags & GOVD_DATA_SHARE_CLASS)
6796 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6797 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6798 n->value = nflags;
6799 return;
6802 /* When adding a variable-sized variable, we have to handle all sorts
6803 of additional bits of data: the pointer replacement variable, and
6804 the parameters of the type. */
6805 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6807 /* Add the pointer replacement variable as PRIVATE if the variable
6808 replacement is private, else FIRSTPRIVATE since we'll need the
6809 address of the original variable either for SHARED, or for the
6810 copy into or out of the context. */
6811 if (!(flags & GOVD_LOCAL) && ctx->region_type != ORT_TASKGROUP)
6813 if (flags & GOVD_MAP)
6814 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6815 else if (flags & GOVD_PRIVATE)
6816 nflags = GOVD_PRIVATE;
6817 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6818 && (flags & GOVD_FIRSTPRIVATE))
6819 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6820 else
6821 nflags = GOVD_FIRSTPRIVATE;
6822 nflags |= flags & GOVD_SEEN;
6823 t = DECL_VALUE_EXPR (decl);
6824 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6825 t = TREE_OPERAND (t, 0);
6826 gcc_assert (DECL_P (t));
6827 omp_add_variable (ctx, t, nflags);
6830 /* Add all of the variable and type parameters (which should have
6831 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6832 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6833 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6834 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6836 /* The variable-sized variable itself is never SHARED, only some form
6837 of PRIVATE. The sharing would take place via the pointer variable
6838 which we remapped above. */
6839 if (flags & GOVD_SHARED)
6840 flags = GOVD_SHARED | GOVD_DEBUG_PRIVATE
6841 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6843 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6844 alloca statement we generate for the variable, so make sure it
6845 is available. This isn't automatically needed for the SHARED
6846 case, since we won't be allocating local storage then.
6847 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6848 in this case omp_notice_variable will be called later
6849 on when it is gimplified. */
6850 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6851 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6852 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6854 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6855 && lang_hooks.decls.omp_privatize_by_reference (decl))
6857 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6859 /* Similar to the direct variable sized case above, we'll need the
6860 size of references being privatized. */
6861 if ((flags & GOVD_SHARED) == 0)
6863 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6864 if (DECL_P (t))
6865 omp_notice_variable (ctx, t, true);
6869 if (n != NULL)
6870 n->value |= flags;
6871 else
6872 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6874 /* For reductions clauses in OpenACC loop directives, by default create a
6875 copy clause on the enclosing parallel construct for carrying back the
6876 results. */
6877 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6879 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6880 while (outer_ctx)
6882 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6883 if (n != NULL)
6885 /* Ignore local variables and explicitly declared clauses. */
6886 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6887 break;
6888 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6890 /* According to the OpenACC spec, such a reduction variable
6891 should already have a copy map on a kernels construct,
6892 verify that here. */
6893 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6894 && (n->value & GOVD_MAP));
6896 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6898 /* Remove firstprivate and make it a copy map. */
6899 n->value &= ~GOVD_FIRSTPRIVATE;
6900 n->value |= GOVD_MAP;
6903 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6905 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6906 GOVD_MAP | GOVD_SEEN);
6907 break;
6909 outer_ctx = outer_ctx->outer_context;
6914 /* Notice a threadprivate variable DECL used in OMP context CTX.
6915 This just prints out diagnostics about threadprivate variable uses
6916 in untied tasks. If DECL2 is non-NULL, prevent this warning
6917 on that variable. */
6919 static bool
6920 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6921 tree decl2)
6923 splay_tree_node n;
6924 struct gimplify_omp_ctx *octx;
6926 for (octx = ctx; octx; octx = octx->outer_context)
6927 if ((octx->region_type & ORT_TARGET) != 0)
6929 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6930 if (n == NULL)
6932 error ("threadprivate variable %qE used in target region",
6933 DECL_NAME (decl));
6934 error_at (octx->location, "enclosing target region");
6935 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6937 if (decl2)
6938 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6941 if (ctx->region_type != ORT_UNTIED_TASK)
6942 return false;
6943 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6944 if (n == NULL)
6946 error ("threadprivate variable %qE used in untied task",
6947 DECL_NAME (decl));
6948 error_at (ctx->location, "enclosing task");
6949 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6951 if (decl2)
6952 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6953 return false;
6956 /* Return true if global var DECL is device resident. */
6958 static bool
6959 device_resident_p (tree decl)
6961 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6963 if (!attr)
6964 return false;
6966 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6968 tree c = TREE_VALUE (t);
6969 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6970 return true;
6973 return false;
6976 /* Return true if DECL has an ACC DECLARE attribute. */
6978 static bool
6979 is_oacc_declared (tree decl)
6981 tree t = TREE_CODE (decl) == MEM_REF ? TREE_OPERAND (decl, 0) : decl;
6982 tree declared = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (t));
6983 return declared != NULL_TREE;
6986 /* Determine outer default flags for DECL mentioned in an OMP region
6987 but not declared in an enclosing clause.
6989 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6990 remapped firstprivate instead of shared. To some extent this is
6991 addressed in omp_firstprivatize_type_sizes, but not
6992 effectively. */
6994 static unsigned
6995 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
6996 bool in_code, unsigned flags)
6998 enum omp_clause_default_kind default_kind = ctx->default_kind;
6999 enum omp_clause_default_kind kind;
7001 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
7002 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7003 default_kind = kind;
7005 switch (default_kind)
7007 case OMP_CLAUSE_DEFAULT_NONE:
7009 const char *rtype;
7011 if (ctx->region_type & ORT_PARALLEL)
7012 rtype = "parallel";
7013 else if ((ctx->region_type & ORT_TASKLOOP) == ORT_TASKLOOP)
7014 rtype = "taskloop";
7015 else if (ctx->region_type & ORT_TASK)
7016 rtype = "task";
7017 else if (ctx->region_type & ORT_TEAMS)
7018 rtype = "teams";
7019 else
7020 gcc_unreachable ();
7022 error ("%qE not specified in enclosing %qs",
7023 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
7024 error_at (ctx->location, "enclosing %qs", rtype);
7026 /* FALLTHRU */
7027 case OMP_CLAUSE_DEFAULT_SHARED:
7028 flags |= GOVD_SHARED;
7029 break;
7030 case OMP_CLAUSE_DEFAULT_PRIVATE:
7031 flags |= GOVD_PRIVATE;
7032 break;
7033 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
7034 flags |= GOVD_FIRSTPRIVATE;
7035 break;
7036 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7037 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
7038 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
7039 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
7041 omp_notice_variable (octx, decl, in_code);
7042 for (; octx; octx = octx->outer_context)
7044 splay_tree_node n2;
7046 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
7047 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
7048 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
7049 continue;
7050 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
7052 flags |= GOVD_FIRSTPRIVATE;
7053 goto found_outer;
7055 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
7057 flags |= GOVD_SHARED;
7058 goto found_outer;
7063 if (TREE_CODE (decl) == PARM_DECL
7064 || (!is_global_var (decl)
7065 && DECL_CONTEXT (decl) == current_function_decl))
7066 flags |= GOVD_FIRSTPRIVATE;
7067 else
7068 flags |= GOVD_SHARED;
7069 found_outer:
7070 break;
7072 default:
7073 gcc_unreachable ();
7076 return flags;
7080 /* Determine outer default flags for DECL mentioned in an OACC region
7081 but not declared in an enclosing clause. */
7083 static unsigned
7084 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
7086 const char *rkind;
7087 bool on_device = false;
7088 bool declared = is_oacc_declared (decl);
7089 tree type = TREE_TYPE (decl);
7091 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7092 type = TREE_TYPE (type);
7094 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
7095 && is_global_var (decl)
7096 && device_resident_p (decl))
7098 on_device = true;
7099 flags |= GOVD_MAP_TO_ONLY;
7102 switch (ctx->region_type)
7104 case ORT_ACC_KERNELS:
7105 rkind = "kernels";
7107 if (AGGREGATE_TYPE_P (type))
7109 /* Aggregates default to 'present_or_copy', or 'present'. */
7110 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7111 flags |= GOVD_MAP;
7112 else
7113 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7115 else
7116 /* Scalars default to 'copy'. */
7117 flags |= GOVD_MAP | GOVD_MAP_FORCE;
7119 break;
7121 case ORT_ACC_PARALLEL:
7122 rkind = "parallel";
7124 if (on_device || declared)
7125 flags |= GOVD_MAP;
7126 else if (AGGREGATE_TYPE_P (type))
7128 /* Aggregates default to 'present_or_copy', or 'present'. */
7129 if (ctx->default_kind != OMP_CLAUSE_DEFAULT_PRESENT)
7130 flags |= GOVD_MAP;
7131 else
7132 flags |= GOVD_MAP | GOVD_MAP_FORCE_PRESENT;
7134 else
7135 /* Scalars default to 'firstprivate'. */
7136 flags |= GOVD_FIRSTPRIVATE;
7138 break;
7140 default:
7141 gcc_unreachable ();
7144 if (DECL_ARTIFICIAL (decl))
7145 ; /* We can get compiler-generated decls, and should not complain
7146 about them. */
7147 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
7149 error ("%qE not specified in enclosing OpenACC %qs construct",
7150 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
7151 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
7153 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_PRESENT)
7154 ; /* Handled above. */
7155 else
7156 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
7158 return flags;
7161 /* Record the fact that DECL was used within the OMP context CTX.
7162 IN_CODE is true when real code uses DECL, and false when we should
7163 merely emit default(none) errors. Return true if DECL is going to
7164 be remapped and thus DECL shouldn't be gimplified into its
7165 DECL_VALUE_EXPR (if any). */
7167 static bool
7168 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
7170 splay_tree_node n;
7171 unsigned flags = in_code ? GOVD_SEEN : 0;
7172 bool ret = false, shared;
7174 if (error_operand_p (decl))
7175 return false;
7177 if (ctx->region_type == ORT_NONE)
7178 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
7180 if (is_global_var (decl))
7182 /* Threadprivate variables are predetermined. */
7183 if (DECL_THREAD_LOCAL_P (decl))
7184 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
7186 if (DECL_HAS_VALUE_EXPR_P (decl))
7188 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7190 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
7191 return omp_notice_threadprivate_variable (ctx, decl, value);
7194 if (gimplify_omp_ctxp->outer_context == NULL
7195 && VAR_P (decl)
7196 && oacc_get_fn_attrib (current_function_decl))
7198 location_t loc = DECL_SOURCE_LOCATION (decl);
7200 if (lookup_attribute ("omp declare target link",
7201 DECL_ATTRIBUTES (decl)))
7203 error_at (loc,
7204 "%qE with %<link%> clause used in %<routine%> function",
7205 DECL_NAME (decl));
7206 return false;
7208 else if (!lookup_attribute ("omp declare target",
7209 DECL_ATTRIBUTES (decl)))
7211 error_at (loc,
7212 "%qE requires a %<declare%> directive for use "
7213 "in a %<routine%> function", DECL_NAME (decl));
7214 return false;
7219 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7220 if ((ctx->region_type & ORT_TARGET) != 0)
7222 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
7223 if (n == NULL)
7225 unsigned nflags = flags;
7226 if ((ctx->region_type & ORT_ACC) == 0)
7228 bool is_declare_target = false;
7229 if (is_global_var (decl)
7230 && varpool_node::get_create (decl)->offloadable)
7232 struct gimplify_omp_ctx *octx;
7233 for (octx = ctx->outer_context;
7234 octx; octx = octx->outer_context)
7236 n = splay_tree_lookup (octx->variables,
7237 (splay_tree_key)decl);
7238 if (n
7239 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
7240 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7241 break;
7243 is_declare_target = octx == NULL;
7245 if (!is_declare_target)
7247 int gdmk;
7248 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7249 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7250 && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7251 == POINTER_TYPE)))
7252 gdmk = GDMK_POINTER;
7253 else if (lang_hooks.decls.omp_scalar_p (decl))
7254 gdmk = GDMK_SCALAR;
7255 else
7256 gdmk = GDMK_AGGREGATE;
7257 if (ctx->defaultmap[gdmk] == 0)
7259 tree d = lang_hooks.decls.omp_report_decl (decl);
7260 error ("%qE not specified in enclosing %<target%>",
7261 DECL_NAME (d));
7262 error_at (ctx->location, "enclosing %<target%>");
7264 else if (ctx->defaultmap[gdmk]
7265 & (GOVD_MAP_0LEN_ARRAY | GOVD_FIRSTPRIVATE))
7266 nflags |= ctx->defaultmap[gdmk];
7267 else
7269 gcc_assert (ctx->defaultmap[gdmk] & GOVD_MAP);
7270 nflags |= ctx->defaultmap[gdmk] & ~GOVD_MAP;
7275 struct gimplify_omp_ctx *octx = ctx->outer_context;
7276 if ((ctx->region_type & ORT_ACC) && octx)
7278 /* Look in outer OpenACC contexts, to see if there's a
7279 data attribute for this variable. */
7280 omp_notice_variable (octx, decl, in_code);
7282 for (; octx; octx = octx->outer_context)
7284 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7285 break;
7286 splay_tree_node n2
7287 = splay_tree_lookup (octx->variables,
7288 (splay_tree_key) decl);
7289 if (n2)
7291 if (octx->region_type == ORT_ACC_HOST_DATA)
7292 error ("variable %qE declared in enclosing "
7293 "%<host_data%> region", DECL_NAME (decl));
7294 nflags |= GOVD_MAP;
7295 if (octx->region_type == ORT_ACC_DATA
7296 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7297 nflags |= GOVD_MAP_0LEN_ARRAY;
7298 goto found_outer;
7303 if ((nflags & ~(GOVD_MAP_TO_ONLY | GOVD_MAP_FROM_ONLY
7304 | GOVD_MAP_ALLOC_ONLY)) == flags)
7306 tree type = TREE_TYPE (decl);
7308 if (gimplify_omp_ctxp->target_firstprivatize_array_bases
7309 && lang_hooks.decls.omp_privatize_by_reference (decl))
7310 type = TREE_TYPE (type);
7311 if (!lang_hooks.types.omp_mappable_type (type))
7313 error ("%qD referenced in target region does not have "
7314 "a mappable type", decl);
7315 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7317 else
7319 if ((ctx->region_type & ORT_ACC) != 0)
7320 nflags = oacc_default_clause (ctx, decl, flags);
7321 else
7322 nflags |= GOVD_MAP;
7325 found_outer:
7326 omp_add_variable (ctx, decl, nflags);
7328 else
7330 /* If nothing changed, there's nothing left to do. */
7331 if ((n->value & flags) == flags)
7332 return ret;
7333 flags |= n->value;
7334 n->value = flags;
7336 goto do_outer;
7339 if (n == NULL)
7341 if (ctx->region_type == ORT_WORKSHARE
7342 || ctx->region_type == ORT_TASKGROUP
7343 || ctx->region_type == ORT_SIMD
7344 || ctx->region_type == ORT_ACC
7345 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7346 goto do_outer;
7348 flags = omp_default_clause (ctx, decl, in_code, flags);
7350 if ((flags & GOVD_PRIVATE)
7351 && lang_hooks.decls.omp_private_outer_ref (decl))
7352 flags |= GOVD_PRIVATE_OUTER_REF;
7354 omp_add_variable (ctx, decl, flags);
7356 shared = (flags & GOVD_SHARED) != 0;
7357 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7358 goto do_outer;
7361 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7362 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7363 && DECL_SIZE (decl))
7365 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7367 splay_tree_node n2;
7368 tree t = DECL_VALUE_EXPR (decl);
7369 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7370 t = TREE_OPERAND (t, 0);
7371 gcc_assert (DECL_P (t));
7372 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7373 n2->value |= GOVD_SEEN;
7375 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7376 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7377 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7378 != INTEGER_CST))
7380 splay_tree_node n2;
7381 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7382 gcc_assert (DECL_P (t));
7383 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7384 if (n2)
7385 omp_notice_variable (ctx, t, true);
7389 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7390 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7392 /* If nothing changed, there's nothing left to do. */
7393 if ((n->value & flags) == flags)
7394 return ret;
7395 flags |= n->value;
7396 n->value = flags;
7398 do_outer:
7399 /* If the variable is private in the current context, then we don't
7400 need to propagate anything to an outer context. */
7401 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7402 return ret;
7403 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7404 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7405 return ret;
7406 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7407 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7408 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7409 return ret;
7410 if (ctx->outer_context
7411 && omp_notice_variable (ctx->outer_context, decl, in_code))
7412 return true;
7413 return ret;
7416 /* Verify that DECL is private within CTX. If there's specific information
7417 to the contrary in the innermost scope, generate an error. */
7419 static bool
7420 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7422 splay_tree_node n;
7424 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7425 if (n != NULL)
7427 if (n->value & GOVD_SHARED)
7429 if (ctx == gimplify_omp_ctxp)
7431 if (simd)
7432 error ("iteration variable %qE is predetermined linear",
7433 DECL_NAME (decl));
7434 else
7435 error ("iteration variable %qE should be private",
7436 DECL_NAME (decl));
7437 n->value = GOVD_PRIVATE;
7438 return true;
7440 else
7441 return false;
7443 else if ((n->value & GOVD_EXPLICIT) != 0
7444 && (ctx == gimplify_omp_ctxp
7445 || (ctx->region_type == ORT_COMBINED_PARALLEL
7446 && gimplify_omp_ctxp->outer_context == ctx)))
7448 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7449 error ("iteration variable %qE should not be firstprivate",
7450 DECL_NAME (decl));
7451 else if ((n->value & GOVD_REDUCTION) != 0)
7452 error ("iteration variable %qE should not be reduction",
7453 DECL_NAME (decl));
7454 else if (simd != 1 && (n->value & GOVD_LINEAR) != 0)
7455 error ("iteration variable %qE should not be linear",
7456 DECL_NAME (decl));
7458 return (ctx == gimplify_omp_ctxp
7459 || (ctx->region_type == ORT_COMBINED_PARALLEL
7460 && gimplify_omp_ctxp->outer_context == ctx));
7463 if (ctx->region_type != ORT_WORKSHARE
7464 && ctx->region_type != ORT_TASKGROUP
7465 && ctx->region_type != ORT_SIMD
7466 && ctx->region_type != ORT_ACC)
7467 return false;
7468 else if (ctx->outer_context)
7469 return omp_is_private (ctx->outer_context, decl, simd);
7470 return false;
7473 /* Return true if DECL is private within a parallel region
7474 that binds to the current construct's context or in parallel
7475 region's REDUCTION clause. */
7477 static bool
7478 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7480 splay_tree_node n;
7484 ctx = ctx->outer_context;
7485 if (ctx == NULL)
7487 if (is_global_var (decl))
7488 return false;
7490 /* References might be private, but might be shared too,
7491 when checking for copyprivate, assume they might be
7492 private, otherwise assume they might be shared. */
7493 if (copyprivate)
7494 return true;
7496 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7497 return false;
7499 /* Treat C++ privatized non-static data members outside
7500 of the privatization the same. */
7501 if (omp_member_access_dummy_var (decl))
7502 return false;
7504 return true;
7507 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7509 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7510 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7511 continue;
7513 if (n != NULL)
7515 if ((n->value & GOVD_LOCAL) != 0
7516 && omp_member_access_dummy_var (decl))
7517 return false;
7518 return (n->value & GOVD_SHARED) == 0;
7521 while (ctx->region_type == ORT_WORKSHARE
7522 || ctx->region_type == ORT_TASKGROUP
7523 || ctx->region_type == ORT_SIMD
7524 || ctx->region_type == ORT_ACC);
7525 return false;
7528 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7530 static tree
7531 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7533 tree t = *tp;
7535 /* If this node has been visited, unmark it and keep looking. */
7536 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7537 return t;
7539 if (IS_TYPE_OR_DECL_P (t))
7540 *walk_subtrees = 0;
7541 return NULL_TREE;
7544 /* If *LIST_P contains any OpenMP depend clauses with iterators,
7545 lower all the depend clauses by populating corresponding depend
7546 array. Returns 0 if there are no such depend clauses, or
7547 2 if all depend clauses should be removed, 1 otherwise. */
7549 static int
7550 gimplify_omp_depend (tree *list_p, gimple_seq *pre_p)
7552 tree c;
7553 gimple *g;
7554 size_t n[4] = { 0, 0, 0, 0 };
7555 bool unused[4];
7556 tree counts[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
7557 tree last_iter = NULL_TREE, last_count = NULL_TREE;
7558 size_t i, j;
7559 location_t first_loc = UNKNOWN_LOCATION;
7561 for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
7562 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
7564 switch (OMP_CLAUSE_DEPEND_KIND (c))
7566 case OMP_CLAUSE_DEPEND_IN:
7567 i = 2;
7568 break;
7569 case OMP_CLAUSE_DEPEND_OUT:
7570 case OMP_CLAUSE_DEPEND_INOUT:
7571 i = 0;
7572 break;
7573 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
7574 i = 1;
7575 break;
7576 case OMP_CLAUSE_DEPEND_DEPOBJ:
7577 i = 3;
7578 break;
7579 case OMP_CLAUSE_DEPEND_SOURCE:
7580 case OMP_CLAUSE_DEPEND_SINK:
7581 continue;
7582 default:
7583 gcc_unreachable ();
7585 tree t = OMP_CLAUSE_DECL (c);
7586 if (first_loc == UNKNOWN_LOCATION)
7587 first_loc = OMP_CLAUSE_LOCATION (c);
7588 if (TREE_CODE (t) == TREE_LIST
7589 && TREE_PURPOSE (t)
7590 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7592 if (TREE_PURPOSE (t) != last_iter)
7594 tree tcnt = size_one_node;
7595 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
7597 if (gimplify_expr (&TREE_VEC_ELT (it, 1), pre_p, NULL,
7598 is_gimple_val, fb_rvalue) == GS_ERROR
7599 || gimplify_expr (&TREE_VEC_ELT (it, 2), pre_p, NULL,
7600 is_gimple_val, fb_rvalue) == GS_ERROR
7601 || gimplify_expr (&TREE_VEC_ELT (it, 3), pre_p, NULL,
7602 is_gimple_val, fb_rvalue) == GS_ERROR
7603 || (gimplify_expr (&TREE_VEC_ELT (it, 4), pre_p, NULL,
7604 is_gimple_val, fb_rvalue)
7605 == GS_ERROR))
7606 return 2;
7607 tree var = TREE_VEC_ELT (it, 0);
7608 tree begin = TREE_VEC_ELT (it, 1);
7609 tree end = TREE_VEC_ELT (it, 2);
7610 tree step = TREE_VEC_ELT (it, 3);
7611 tree orig_step = TREE_VEC_ELT (it, 4);
7612 tree type = TREE_TYPE (var);
7613 tree stype = TREE_TYPE (step);
7614 location_t loc = DECL_SOURCE_LOCATION (var);
7615 tree endmbegin;
7616 /* Compute count for this iterator as
7617 orig_step > 0
7618 ? (begin < end ? (end - begin + (step - 1)) / step : 0)
7619 : (begin > end ? (end - begin + (step + 1)) / step : 0)
7620 and compute product of those for the entire depend
7621 clause. */
7622 if (POINTER_TYPE_P (type))
7623 endmbegin = fold_build2_loc (loc, POINTER_DIFF_EXPR,
7624 stype, end, begin);
7625 else
7626 endmbegin = fold_build2_loc (loc, MINUS_EXPR, type,
7627 end, begin);
7628 tree stepm1 = fold_build2_loc (loc, MINUS_EXPR, stype,
7629 step,
7630 build_int_cst (stype, 1));
7631 tree stepp1 = fold_build2_loc (loc, PLUS_EXPR, stype, step,
7632 build_int_cst (stype, 1));
7633 tree pos = fold_build2_loc (loc, PLUS_EXPR, stype,
7634 unshare_expr (endmbegin),
7635 stepm1);
7636 pos = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype,
7637 pos, step);
7638 tree neg = fold_build2_loc (loc, PLUS_EXPR, stype,
7639 endmbegin, stepp1);
7640 if (TYPE_UNSIGNED (stype))
7642 neg = fold_build1_loc (loc, NEGATE_EXPR, stype, neg);
7643 step = fold_build1_loc (loc, NEGATE_EXPR, stype, step);
7645 neg = fold_build2_loc (loc, TRUNC_DIV_EXPR, stype,
7646 neg, step);
7647 step = NULL_TREE;
7648 tree cond = fold_build2_loc (loc, LT_EXPR,
7649 boolean_type_node,
7650 begin, end);
7651 pos = fold_build3_loc (loc, COND_EXPR, stype, cond, pos,
7652 build_int_cst (stype, 0));
7653 cond = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
7654 end, begin);
7655 neg = fold_build3_loc (loc, COND_EXPR, stype, cond, neg,
7656 build_int_cst (stype, 0));
7657 tree osteptype = TREE_TYPE (orig_step);
7658 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
7659 orig_step,
7660 build_int_cst (osteptype, 0));
7661 tree cnt = fold_build3_loc (loc, COND_EXPR, stype,
7662 cond, pos, neg);
7663 cnt = fold_convert_loc (loc, sizetype, cnt);
7664 if (gimplify_expr (&cnt, pre_p, NULL, is_gimple_val,
7665 fb_rvalue) == GS_ERROR)
7666 return 2;
7667 tcnt = size_binop_loc (loc, MULT_EXPR, tcnt, cnt);
7669 if (gimplify_expr (&tcnt, pre_p, NULL, is_gimple_val,
7670 fb_rvalue) == GS_ERROR)
7671 return 2;
7672 last_iter = TREE_PURPOSE (t);
7673 last_count = tcnt;
7675 if (counts[i] == NULL_TREE)
7676 counts[i] = last_count;
7677 else
7678 counts[i] = size_binop_loc (OMP_CLAUSE_LOCATION (c),
7679 PLUS_EXPR, counts[i], last_count);
7681 else
7682 n[i]++;
7684 for (i = 0; i < 4; i++)
7685 if (counts[i])
7686 break;
7687 if (i == 4)
7688 return 0;
7690 tree total = size_zero_node;
7691 for (i = 0; i < 4; i++)
7693 unused[i] = counts[i] == NULL_TREE && n[i] == 0;
7694 if (counts[i] == NULL_TREE)
7695 counts[i] = size_zero_node;
7696 if (n[i])
7697 counts[i] = size_binop (PLUS_EXPR, counts[i], size_int (n[i]));
7698 if (gimplify_expr (&counts[i], pre_p, NULL, is_gimple_val,
7699 fb_rvalue) == GS_ERROR)
7700 return 2;
7701 total = size_binop (PLUS_EXPR, total, counts[i]);
7704 if (gimplify_expr (&total, pre_p, NULL, is_gimple_val, fb_rvalue)
7705 == GS_ERROR)
7706 return 2;
7707 bool is_old = unused[1] && unused[3];
7708 tree totalpx = size_binop (PLUS_EXPR, unshare_expr (total),
7709 size_int (is_old ? 1 : 4));
7710 tree type = build_array_type (ptr_type_node, build_index_type (totalpx));
7711 tree array = create_tmp_var_raw (type);
7712 TREE_ADDRESSABLE (array) = 1;
7713 if (TREE_CODE (totalpx) != INTEGER_CST)
7715 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (array)))
7716 gimplify_type_sizes (TREE_TYPE (array), pre_p);
7717 if (gimplify_omp_ctxp)
7719 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7720 while (ctx
7721 && (ctx->region_type == ORT_WORKSHARE
7722 || ctx->region_type == ORT_TASKGROUP
7723 || ctx->region_type == ORT_SIMD
7724 || ctx->region_type == ORT_ACC))
7725 ctx = ctx->outer_context;
7726 if (ctx)
7727 omp_add_variable (ctx, array, GOVD_LOCAL | GOVD_SEEN);
7729 gimplify_vla_decl (array, pre_p);
7731 else
7732 gimple_add_tmp_var (array);
7733 tree r = build4 (ARRAY_REF, ptr_type_node, array, size_int (0), NULL_TREE,
7734 NULL_TREE);
7735 tree tem;
7736 if (!is_old)
7738 tem = build2 (MODIFY_EXPR, void_type_node, r,
7739 build_int_cst (ptr_type_node, 0));
7740 gimplify_and_add (tem, pre_p);
7741 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (1), NULL_TREE,
7742 NULL_TREE);
7744 tem = build2 (MODIFY_EXPR, void_type_node, r,
7745 fold_convert (ptr_type_node, total));
7746 gimplify_and_add (tem, pre_p);
7747 for (i = 1; i < (is_old ? 2 : 4); i++)
7749 r = build4 (ARRAY_REF, ptr_type_node, array, size_int (i + !is_old),
7750 NULL_TREE, NULL_TREE);
7751 tem = build2 (MODIFY_EXPR, void_type_node, r, counts[i - 1]);
7752 gimplify_and_add (tem, pre_p);
7755 tree cnts[4];
7756 for (j = 4; j; j--)
7757 if (!unused[j - 1])
7758 break;
7759 for (i = 0; i < 4; i++)
7761 if (i && (i >= j || unused[i - 1]))
7763 cnts[i] = cnts[i - 1];
7764 continue;
7766 cnts[i] = create_tmp_var (sizetype);
7767 if (i == 0)
7768 g = gimple_build_assign (cnts[i], size_int (is_old ? 2 : 5));
7769 else
7771 tree t;
7772 if (is_old)
7773 t = size_binop (PLUS_EXPR, counts[0], size_int (2));
7774 else
7775 t = size_binop (PLUS_EXPR, cnts[i - 1], counts[i - 1]);
7776 if (gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue)
7777 == GS_ERROR)
7778 return 2;
7779 g = gimple_build_assign (cnts[i], t);
7781 gimple_seq_add_stmt (pre_p, g);
7784 last_iter = NULL_TREE;
7785 tree last_bind = NULL_TREE;
7786 tree *last_body = NULL;
7787 for (c = *list_p; c; c = OMP_CLAUSE_CHAIN (c))
7788 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
7790 switch (OMP_CLAUSE_DEPEND_KIND (c))
7792 case OMP_CLAUSE_DEPEND_IN:
7793 i = 2;
7794 break;
7795 case OMP_CLAUSE_DEPEND_OUT:
7796 case OMP_CLAUSE_DEPEND_INOUT:
7797 i = 0;
7798 break;
7799 case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
7800 i = 1;
7801 break;
7802 case OMP_CLAUSE_DEPEND_DEPOBJ:
7803 i = 3;
7804 break;
7805 case OMP_CLAUSE_DEPEND_SOURCE:
7806 case OMP_CLAUSE_DEPEND_SINK:
7807 continue;
7808 default:
7809 gcc_unreachable ();
7811 tree t = OMP_CLAUSE_DECL (c);
7812 if (TREE_CODE (t) == TREE_LIST
7813 && TREE_PURPOSE (t)
7814 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7816 if (TREE_PURPOSE (t) != last_iter)
7818 if (last_bind)
7819 gimplify_and_add (last_bind, pre_p);
7820 tree block = TREE_VEC_ELT (TREE_PURPOSE (t), 5);
7821 last_bind = build3 (BIND_EXPR, void_type_node,
7822 BLOCK_VARS (block), NULL, block);
7823 TREE_SIDE_EFFECTS (last_bind) = 1;
7824 SET_EXPR_LOCATION (last_bind, OMP_CLAUSE_LOCATION (c));
7825 tree *p = &BIND_EXPR_BODY (last_bind);
7826 for (tree it = TREE_PURPOSE (t); it; it = TREE_CHAIN (it))
7828 tree var = TREE_VEC_ELT (it, 0);
7829 tree begin = TREE_VEC_ELT (it, 1);
7830 tree end = TREE_VEC_ELT (it, 2);
7831 tree step = TREE_VEC_ELT (it, 3);
7832 tree orig_step = TREE_VEC_ELT (it, 4);
7833 tree type = TREE_TYPE (var);
7834 location_t loc = DECL_SOURCE_LOCATION (var);
7835 /* Emit:
7836 var = begin;
7837 goto cond_label;
7838 beg_label:
7840 var = var + step;
7841 cond_label:
7842 if (orig_step > 0) {
7843 if (var < end) goto beg_label;
7844 } else {
7845 if (var > end) goto beg_label;
7847 for each iterator, with inner iterators added to
7848 the ... above. */
7849 tree beg_label = create_artificial_label (loc);
7850 tree cond_label = NULL_TREE;
7851 tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
7852 var, begin);
7853 append_to_statement_list_force (tem, p);
7854 tem = build_and_jump (&cond_label);
7855 append_to_statement_list_force (tem, p);
7856 tem = build1 (LABEL_EXPR, void_type_node, beg_label);
7857 append_to_statement_list (tem, p);
7858 tree bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
7859 NULL_TREE, NULL_TREE);
7860 TREE_SIDE_EFFECTS (bind) = 1;
7861 SET_EXPR_LOCATION (bind, loc);
7862 append_to_statement_list_force (bind, p);
7863 if (POINTER_TYPE_P (type))
7864 tem = build2_loc (loc, POINTER_PLUS_EXPR, type,
7865 var, fold_convert_loc (loc, sizetype,
7866 step));
7867 else
7868 tem = build2_loc (loc, PLUS_EXPR, type, var, step);
7869 tem = build2_loc (loc, MODIFY_EXPR, void_type_node,
7870 var, tem);
7871 append_to_statement_list_force (tem, p);
7872 tem = build1 (LABEL_EXPR, void_type_node, cond_label);
7873 append_to_statement_list (tem, p);
7874 tree cond = fold_build2_loc (loc, LT_EXPR,
7875 boolean_type_node,
7876 var, end);
7877 tree pos
7878 = fold_build3_loc (loc, COND_EXPR, void_type_node,
7879 cond, build_and_jump (&beg_label),
7880 void_node);
7881 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
7882 var, end);
7883 tree neg
7884 = fold_build3_loc (loc, COND_EXPR, void_type_node,
7885 cond, build_and_jump (&beg_label),
7886 void_node);
7887 tree osteptype = TREE_TYPE (orig_step);
7888 cond = fold_build2_loc (loc, GT_EXPR, boolean_type_node,
7889 orig_step,
7890 build_int_cst (osteptype, 0));
7891 tem = fold_build3_loc (loc, COND_EXPR, void_type_node,
7892 cond, pos, neg);
7893 append_to_statement_list_force (tem, p);
7894 p = &BIND_EXPR_BODY (bind);
7896 last_body = p;
7898 last_iter = TREE_PURPOSE (t);
7899 if (TREE_CODE (TREE_VALUE (t)) == COMPOUND_EXPR)
7901 append_to_statement_list (TREE_OPERAND (TREE_VALUE (t),
7902 0), last_body);
7903 TREE_VALUE (t) = TREE_OPERAND (TREE_VALUE (t), 1);
7905 if (error_operand_p (TREE_VALUE (t)))
7906 return 2;
7907 TREE_VALUE (t) = build_fold_addr_expr (TREE_VALUE (t));
7908 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
7909 NULL_TREE, NULL_TREE);
7910 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
7911 void_type_node, r, TREE_VALUE (t));
7912 append_to_statement_list_force (tem, last_body);
7913 tem = build2_loc (OMP_CLAUSE_LOCATION (c), MODIFY_EXPR,
7914 void_type_node, cnts[i],
7915 size_binop (PLUS_EXPR, cnts[i], size_int (1)));
7916 append_to_statement_list_force (tem, last_body);
7917 TREE_VALUE (t) = null_pointer_node;
7919 else
7921 if (last_bind)
7923 gimplify_and_add (last_bind, pre_p);
7924 last_bind = NULL_TREE;
7926 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
7928 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
7929 NULL, is_gimple_val, fb_rvalue);
7930 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
7932 if (error_operand_p (OMP_CLAUSE_DECL (c)))
7933 return 2;
7934 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
7935 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
7936 is_gimple_val, fb_rvalue) == GS_ERROR)
7937 return 2;
7938 r = build4 (ARRAY_REF, ptr_type_node, array, cnts[i],
7939 NULL_TREE, NULL_TREE);
7940 tem = build2 (MODIFY_EXPR, void_type_node, r, OMP_CLAUSE_DECL (c));
7941 gimplify_and_add (tem, pre_p);
7942 g = gimple_build_assign (cnts[i], size_binop (PLUS_EXPR, cnts[i],
7943 size_int (1)));
7944 gimple_seq_add_stmt (pre_p, g);
7947 if (last_bind)
7948 gimplify_and_add (last_bind, pre_p);
7949 tree cond = boolean_false_node;
7950 if (is_old)
7952 if (!unused[0])
7953 cond = build2_loc (first_loc, NE_EXPR, boolean_type_node, cnts[0],
7954 size_binop_loc (first_loc, PLUS_EXPR, counts[0],
7955 size_int (2)));
7956 if (!unused[2])
7957 cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
7958 build2_loc (first_loc, NE_EXPR, boolean_type_node,
7959 cnts[2],
7960 size_binop_loc (first_loc, PLUS_EXPR,
7961 totalpx,
7962 size_int (1))));
7964 else
7966 tree prev = size_int (5);
7967 for (i = 0; i < 4; i++)
7969 if (unused[i])
7970 continue;
7971 prev = size_binop_loc (first_loc, PLUS_EXPR, counts[i], prev);
7972 cond = build2_loc (first_loc, TRUTH_OR_EXPR, boolean_type_node, cond,
7973 build2_loc (first_loc, NE_EXPR, boolean_type_node,
7974 cnts[i], unshare_expr (prev)));
7977 tem = build3_loc (first_loc, COND_EXPR, void_type_node, cond,
7978 build_call_expr_loc (first_loc,
7979 builtin_decl_explicit (BUILT_IN_TRAP),
7980 0), void_node);
7981 gimplify_and_add (tem, pre_p);
7982 c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_DEPEND);
7983 OMP_CLAUSE_DEPEND_KIND (c) = OMP_CLAUSE_DEPEND_LAST;
7984 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (array);
7985 OMP_CLAUSE_CHAIN (c) = *list_p;
7986 *list_p = c;
7987 return 1;
7990 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7991 and previous omp contexts. */
7993 static void
7994 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7995 enum omp_region_type region_type,
7996 enum tree_code code)
7998 struct gimplify_omp_ctx *ctx, *outer_ctx;
7999 tree c;
8000 hash_map<tree, tree> *struct_map_to_clause = NULL;
8001 tree *prev_list_p = NULL;
8002 int handled_depend_iterators = -1;
8003 int nowait = -1;
8005 ctx = new_omp_context (region_type);
8006 outer_ctx = ctx->outer_context;
8007 if (code == OMP_TARGET)
8009 if (!lang_GNU_Fortran ())
8010 ctx->defaultmap[GDMK_POINTER] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
8011 ctx->defaultmap[GDMK_SCALAR] = GOVD_FIRSTPRIVATE;
8013 if (!lang_GNU_Fortran ())
8014 switch (code)
8016 case OMP_TARGET:
8017 case OMP_TARGET_DATA:
8018 case OMP_TARGET_ENTER_DATA:
8019 case OMP_TARGET_EXIT_DATA:
8020 case OACC_DECLARE:
8021 case OACC_HOST_DATA:
8022 case OACC_PARALLEL:
8023 case OACC_KERNELS:
8024 ctx->target_firstprivatize_array_bases = true;
8025 default:
8026 break;
8029 while ((c = *list_p) != NULL)
8031 bool remove = false;
8032 bool notice_outer = true;
8033 const char *check_non_private = NULL;
8034 unsigned int flags;
8035 tree decl;
8037 switch (OMP_CLAUSE_CODE (c))
8039 case OMP_CLAUSE_PRIVATE:
8040 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
8041 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
8043 flags |= GOVD_PRIVATE_OUTER_REF;
8044 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
8046 else
8047 notice_outer = false;
8048 goto do_add;
8049 case OMP_CLAUSE_SHARED:
8050 flags = GOVD_SHARED | GOVD_EXPLICIT;
8051 goto do_add;
8052 case OMP_CLAUSE_FIRSTPRIVATE:
8053 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8054 check_non_private = "firstprivate";
8055 goto do_add;
8056 case OMP_CLAUSE_LASTPRIVATE:
8057 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
8058 switch (code)
8060 case OMP_DISTRIBUTE:
8061 error_at (OMP_CLAUSE_LOCATION (c),
8062 "conditional %<lastprivate%> clause on "
8063 "%<distribute%> construct");
8064 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
8065 break;
8066 case OMP_TASKLOOP:
8067 error_at (OMP_CLAUSE_LOCATION (c),
8068 "conditional %<lastprivate%> clause on "
8069 "%<taskloop%> construct");
8070 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
8071 break;
8072 default:
8073 break;
8075 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
8076 check_non_private = "lastprivate";
8077 decl = OMP_CLAUSE_DECL (c);
8078 if (error_operand_p (decl))
8079 goto do_add;
8080 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
8081 && !lang_hooks.decls.omp_scalar_p (decl))
8083 error_at (OMP_CLAUSE_LOCATION (c),
8084 "non-scalar variable %qD in conditional "
8085 "%<lastprivate%> clause", decl);
8086 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 0;
8088 if (OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c))
8089 sorry_at (OMP_CLAUSE_LOCATION (c),
8090 "%<conditional%> modifier on %<lastprivate%> clause "
8091 "not supported yet");
8092 if (outer_ctx
8093 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
8094 || ((outer_ctx->region_type & ORT_COMBINED_TEAMS)
8095 == ORT_COMBINED_TEAMS))
8096 && splay_tree_lookup (outer_ctx->variables,
8097 (splay_tree_key) decl) == NULL)
8099 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
8100 if (outer_ctx->outer_context)
8101 omp_notice_variable (outer_ctx->outer_context, decl, true);
8103 else if (outer_ctx
8104 && (outer_ctx->region_type & ORT_TASK) != 0
8105 && outer_ctx->combined_loop
8106 && splay_tree_lookup (outer_ctx->variables,
8107 (splay_tree_key) decl) == NULL)
8109 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
8110 if (outer_ctx->outer_context)
8111 omp_notice_variable (outer_ctx->outer_context, decl, true);
8113 else if (outer_ctx
8114 && (outer_ctx->region_type == ORT_WORKSHARE
8115 || outer_ctx->region_type == ORT_ACC)
8116 && outer_ctx->combined_loop
8117 && splay_tree_lookup (outer_ctx->variables,
8118 (splay_tree_key) decl) == NULL
8119 && !omp_check_private (outer_ctx, decl, false))
8121 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
8122 if (outer_ctx->outer_context
8123 && (outer_ctx->outer_context->region_type
8124 == ORT_COMBINED_PARALLEL)
8125 && splay_tree_lookup (outer_ctx->outer_context->variables,
8126 (splay_tree_key) decl) == NULL)
8128 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
8129 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
8130 if (octx->outer_context)
8132 octx = octx->outer_context;
8133 if (octx->region_type == ORT_WORKSHARE
8134 && octx->combined_loop
8135 && splay_tree_lookup (octx->variables,
8136 (splay_tree_key) decl) == NULL
8137 && !omp_check_private (octx, decl, false))
8139 omp_add_variable (octx, decl,
8140 GOVD_LASTPRIVATE | GOVD_SEEN);
8141 octx = octx->outer_context;
8142 if (octx
8143 && ((octx->region_type & ORT_COMBINED_TEAMS)
8144 == ORT_COMBINED_TEAMS)
8145 && (splay_tree_lookup (octx->variables,
8146 (splay_tree_key) decl)
8147 == NULL))
8149 omp_add_variable (octx, decl,
8150 GOVD_SHARED | GOVD_SEEN);
8151 octx = octx->outer_context;
8154 if (octx)
8155 omp_notice_variable (octx, decl, true);
8158 else if (outer_ctx->outer_context)
8159 omp_notice_variable (outer_ctx->outer_context, decl, true);
8161 goto do_add;
8162 case OMP_CLAUSE_REDUCTION:
8163 if (OMP_CLAUSE_REDUCTION_TASK (c))
8165 if (region_type == ORT_WORKSHARE)
8167 if (nowait == -1)
8168 nowait = omp_find_clause (*list_p,
8169 OMP_CLAUSE_NOWAIT) != NULL_TREE;
8170 if (nowait
8171 && (outer_ctx == NULL
8172 || outer_ctx->region_type != ORT_COMBINED_PARALLEL))
8174 error_at (OMP_CLAUSE_LOCATION (c),
8175 "%<task%> reduction modifier on a construct "
8176 "with a %<nowait%> clause");
8177 OMP_CLAUSE_REDUCTION_TASK (c) = 0;
8180 else if ((region_type & ORT_PARALLEL) != ORT_PARALLEL)
8182 error_at (OMP_CLAUSE_LOCATION (c),
8183 "invalid %<task%> reduction modifier on construct "
8184 "other than %<parallel%>, %<for%> or %<sections%>");
8185 OMP_CLAUSE_REDUCTION_TASK (c) = 0;
8188 /* FALLTHRU */
8189 case OMP_CLAUSE_IN_REDUCTION:
8190 case OMP_CLAUSE_TASK_REDUCTION:
8191 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
8192 /* OpenACC permits reductions on private variables. */
8193 if (!(region_type & ORT_ACC)
8194 /* taskgroup is actually not a worksharing region. */
8195 && code != OMP_TASKGROUP)
8196 check_non_private = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
8197 decl = OMP_CLAUSE_DECL (c);
8198 if (TREE_CODE (decl) == MEM_REF)
8200 tree type = TREE_TYPE (decl);
8201 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
8202 NULL, is_gimple_val, fb_rvalue, false)
8203 == GS_ERROR)
8205 remove = true;
8206 break;
8208 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8209 if (DECL_P (v))
8211 omp_firstprivatize_variable (ctx, v);
8212 omp_notice_variable (ctx, v, true);
8214 decl = TREE_OPERAND (decl, 0);
8215 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
8217 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
8218 NULL, is_gimple_val, fb_rvalue, false)
8219 == GS_ERROR)
8221 remove = true;
8222 break;
8224 v = TREE_OPERAND (decl, 1);
8225 if (DECL_P (v))
8227 omp_firstprivatize_variable (ctx, v);
8228 omp_notice_variable (ctx, v, true);
8230 decl = TREE_OPERAND (decl, 0);
8232 if (TREE_CODE (decl) == ADDR_EXPR
8233 || TREE_CODE (decl) == INDIRECT_REF)
8234 decl = TREE_OPERAND (decl, 0);
8236 goto do_add_decl;
8237 case OMP_CLAUSE_LINEAR:
8238 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
8239 is_gimple_val, fb_rvalue) == GS_ERROR)
8241 remove = true;
8242 break;
8244 else
8246 if (code == OMP_SIMD
8247 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
8249 struct gimplify_omp_ctx *octx = outer_ctx;
8250 if (octx
8251 && octx->region_type == ORT_WORKSHARE
8252 && octx->combined_loop
8253 && !octx->distribute)
8255 if (octx->outer_context
8256 && (octx->outer_context->region_type
8257 == ORT_COMBINED_PARALLEL))
8258 octx = octx->outer_context->outer_context;
8259 else
8260 octx = octx->outer_context;
8262 if (octx
8263 && octx->region_type == ORT_WORKSHARE
8264 && octx->combined_loop
8265 && octx->distribute)
8267 error_at (OMP_CLAUSE_LOCATION (c),
8268 "%<linear%> clause for variable other than "
8269 "loop iterator specified on construct "
8270 "combined with %<distribute%>");
8271 remove = true;
8272 break;
8275 /* For combined #pragma omp parallel for simd, need to put
8276 lastprivate and perhaps firstprivate too on the
8277 parallel. Similarly for #pragma omp for simd. */
8278 struct gimplify_omp_ctx *octx = outer_ctx;
8279 decl = NULL_TREE;
8282 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
8283 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8284 break;
8285 decl = OMP_CLAUSE_DECL (c);
8286 if (error_operand_p (decl))
8288 decl = NULL_TREE;
8289 break;
8291 flags = GOVD_SEEN;
8292 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
8293 flags |= GOVD_FIRSTPRIVATE;
8294 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8295 flags |= GOVD_LASTPRIVATE;
8296 if (octx
8297 && octx->region_type == ORT_WORKSHARE
8298 && octx->combined_loop)
8300 if (octx->outer_context
8301 && (octx->outer_context->region_type
8302 == ORT_COMBINED_PARALLEL))
8303 octx = octx->outer_context;
8304 else if (omp_check_private (octx, decl, false))
8305 break;
8307 else if (octx
8308 && (octx->region_type & ORT_TASK) != 0
8309 && octx->combined_loop)
8311 else if (octx
8312 && octx->region_type == ORT_COMBINED_PARALLEL
8313 && ctx->region_type == ORT_WORKSHARE
8314 && octx == outer_ctx)
8315 flags = GOVD_SEEN | GOVD_SHARED;
8316 else if (octx
8317 && ((octx->region_type & ORT_COMBINED_TEAMS)
8318 == ORT_COMBINED_TEAMS))
8319 flags = GOVD_SEEN | GOVD_SHARED;
8320 else if (octx
8321 && octx->region_type == ORT_COMBINED_TARGET)
8323 flags &= ~GOVD_LASTPRIVATE;
8324 if (flags == GOVD_SEEN)
8325 break;
8327 else
8328 break;
8329 splay_tree_node on
8330 = splay_tree_lookup (octx->variables,
8331 (splay_tree_key) decl);
8332 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
8334 octx = NULL;
8335 break;
8337 omp_add_variable (octx, decl, flags);
8338 if (octx->outer_context == NULL)
8339 break;
8340 octx = octx->outer_context;
8342 while (1);
8343 if (octx
8344 && decl
8345 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
8346 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
8347 omp_notice_variable (octx, decl, true);
8349 flags = GOVD_LINEAR | GOVD_EXPLICIT;
8350 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
8351 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8353 notice_outer = false;
8354 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
8356 goto do_add;
8358 case OMP_CLAUSE_MAP:
8359 decl = OMP_CLAUSE_DECL (c);
8360 if (error_operand_p (decl))
8361 remove = true;
8362 switch (code)
8364 case OMP_TARGET:
8365 break;
8366 case OACC_DATA:
8367 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
8368 break;
8369 /* FALLTHRU */
8370 case OMP_TARGET_DATA:
8371 case OMP_TARGET_ENTER_DATA:
8372 case OMP_TARGET_EXIT_DATA:
8373 case OACC_ENTER_DATA:
8374 case OACC_EXIT_DATA:
8375 case OACC_HOST_DATA:
8376 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
8377 || (OMP_CLAUSE_MAP_KIND (c)
8378 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8379 /* For target {,enter ,exit }data only the array slice is
8380 mapped, but not the pointer to it. */
8381 remove = true;
8382 break;
8383 default:
8384 break;
8386 if (remove)
8387 break;
8388 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
8390 struct gimplify_omp_ctx *octx;
8391 for (octx = outer_ctx; octx; octx = octx->outer_context)
8393 if (octx->region_type != ORT_ACC_HOST_DATA)
8394 break;
8395 splay_tree_node n2
8396 = splay_tree_lookup (octx->variables,
8397 (splay_tree_key) decl);
8398 if (n2)
8399 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
8400 "declared in enclosing %<host_data%> region",
8401 DECL_NAME (decl));
8404 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8405 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8406 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8407 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8408 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8410 remove = true;
8411 break;
8413 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
8414 || (OMP_CLAUSE_MAP_KIND (c)
8415 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8416 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
8418 OMP_CLAUSE_SIZE (c)
8419 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
8420 false);
8421 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
8422 GOVD_FIRSTPRIVATE | GOVD_SEEN);
8424 if (!DECL_P (decl))
8426 tree d = decl, *pd;
8427 if (TREE_CODE (d) == ARRAY_REF)
8429 while (TREE_CODE (d) == ARRAY_REF)
8430 d = TREE_OPERAND (d, 0);
8431 if (TREE_CODE (d) == COMPONENT_REF
8432 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
8433 decl = d;
8435 pd = &OMP_CLAUSE_DECL (c);
8436 if (d == decl
8437 && TREE_CODE (decl) == INDIRECT_REF
8438 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
8439 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
8440 == REFERENCE_TYPE))
8442 pd = &TREE_OPERAND (decl, 0);
8443 decl = TREE_OPERAND (decl, 0);
8445 if (TREE_CODE (decl) == COMPONENT_REF)
8447 while (TREE_CODE (decl) == COMPONENT_REF)
8448 decl = TREE_OPERAND (decl, 0);
8449 if (TREE_CODE (decl) == INDIRECT_REF
8450 && DECL_P (TREE_OPERAND (decl, 0))
8451 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
8452 == REFERENCE_TYPE))
8453 decl = TREE_OPERAND (decl, 0);
8455 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
8456 == GS_ERROR)
8458 remove = true;
8459 break;
8461 if (DECL_P (decl))
8463 if (error_operand_p (decl))
8465 remove = true;
8466 break;
8469 tree stype = TREE_TYPE (decl);
8470 if (TREE_CODE (stype) == REFERENCE_TYPE)
8471 stype = TREE_TYPE (stype);
8472 if (TYPE_SIZE_UNIT (stype) == NULL
8473 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
8475 error_at (OMP_CLAUSE_LOCATION (c),
8476 "mapping field %qE of variable length "
8477 "structure", OMP_CLAUSE_DECL (c));
8478 remove = true;
8479 break;
8482 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
8484 /* Error recovery. */
8485 if (prev_list_p == NULL)
8487 remove = true;
8488 break;
8490 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8492 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
8493 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
8495 remove = true;
8496 break;
8501 tree offset;
8502 poly_int64 bitsize, bitpos;
8503 machine_mode mode;
8504 int unsignedp, reversep, volatilep = 0;
8505 tree base = OMP_CLAUSE_DECL (c);
8506 while (TREE_CODE (base) == ARRAY_REF)
8507 base = TREE_OPERAND (base, 0);
8508 if (TREE_CODE (base) == INDIRECT_REF)
8509 base = TREE_OPERAND (base, 0);
8510 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8511 &mode, &unsignedp, &reversep,
8512 &volatilep);
8513 tree orig_base = base;
8514 if ((TREE_CODE (base) == INDIRECT_REF
8515 || (TREE_CODE (base) == MEM_REF
8516 && integer_zerop (TREE_OPERAND (base, 1))))
8517 && DECL_P (TREE_OPERAND (base, 0))
8518 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
8519 == REFERENCE_TYPE))
8520 base = TREE_OPERAND (base, 0);
8521 gcc_assert (base == decl
8522 && (offset == NULL_TREE
8523 || poly_int_tree_p (offset)));
8525 splay_tree_node n
8526 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
8527 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
8528 == GOMP_MAP_ALWAYS_POINTER);
8529 if (n == NULL || (n->value & GOVD_MAP) == 0)
8531 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8532 OMP_CLAUSE_MAP);
8533 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
8534 if (orig_base != base)
8535 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
8536 else
8537 OMP_CLAUSE_DECL (l) = decl;
8538 OMP_CLAUSE_SIZE (l) = size_int (1);
8539 if (struct_map_to_clause == NULL)
8540 struct_map_to_clause = new hash_map<tree, tree>;
8541 struct_map_to_clause->put (decl, l);
8542 if (ptr)
8544 enum gomp_map_kind mkind
8545 = code == OMP_TARGET_EXIT_DATA
8546 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8547 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8548 OMP_CLAUSE_MAP);
8549 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8550 OMP_CLAUSE_DECL (c2)
8551 = unshare_expr (OMP_CLAUSE_DECL (c));
8552 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
8553 OMP_CLAUSE_SIZE (c2)
8554 = TYPE_SIZE_UNIT (ptr_type_node);
8555 OMP_CLAUSE_CHAIN (l) = c2;
8556 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8558 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8559 tree c3
8560 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8561 OMP_CLAUSE_MAP);
8562 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8563 OMP_CLAUSE_DECL (c3)
8564 = unshare_expr (OMP_CLAUSE_DECL (c4));
8565 OMP_CLAUSE_SIZE (c3)
8566 = TYPE_SIZE_UNIT (ptr_type_node);
8567 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8568 OMP_CLAUSE_CHAIN (c2) = c3;
8570 *prev_list_p = l;
8571 prev_list_p = NULL;
8573 else
8575 OMP_CLAUSE_CHAIN (l) = c;
8576 *list_p = l;
8577 list_p = &OMP_CLAUSE_CHAIN (l);
8579 if (orig_base != base && code == OMP_TARGET)
8581 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8582 OMP_CLAUSE_MAP);
8583 enum gomp_map_kind mkind
8584 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
8585 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8586 OMP_CLAUSE_DECL (c2) = decl;
8587 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8588 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
8589 OMP_CLAUSE_CHAIN (l) = c2;
8591 flags = GOVD_MAP | GOVD_EXPLICIT;
8592 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8593 flags |= GOVD_SEEN;
8594 goto do_add_decl;
8596 else
8598 tree *osc = struct_map_to_clause->get (decl);
8599 tree *sc = NULL, *scp = NULL;
8600 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
8601 n->value |= GOVD_SEEN;
8602 poly_offset_int o1, o2;
8603 if (offset)
8604 o1 = wi::to_poly_offset (offset);
8605 else
8606 o1 = 0;
8607 if (maybe_ne (bitpos, 0))
8608 o1 += bits_to_bytes_round_down (bitpos);
8609 sc = &OMP_CLAUSE_CHAIN (*osc);
8610 if (*sc != c
8611 && (OMP_CLAUSE_MAP_KIND (*sc)
8612 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8613 sc = &OMP_CLAUSE_CHAIN (*sc);
8614 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
8615 if (ptr && sc == prev_list_p)
8616 break;
8617 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8618 != COMPONENT_REF
8619 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8620 != INDIRECT_REF)
8621 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
8622 != ARRAY_REF))
8623 break;
8624 else
8626 tree offset2;
8627 poly_int64 bitsize2, bitpos2;
8628 base = OMP_CLAUSE_DECL (*sc);
8629 if (TREE_CODE (base) == ARRAY_REF)
8631 while (TREE_CODE (base) == ARRAY_REF)
8632 base = TREE_OPERAND (base, 0);
8633 if (TREE_CODE (base) != COMPONENT_REF
8634 || (TREE_CODE (TREE_TYPE (base))
8635 != ARRAY_TYPE))
8636 break;
8638 else if (TREE_CODE (base) == INDIRECT_REF
8639 && (TREE_CODE (TREE_OPERAND (base, 0))
8640 == COMPONENT_REF)
8641 && (TREE_CODE (TREE_TYPE
8642 (TREE_OPERAND (base, 0)))
8643 == REFERENCE_TYPE))
8644 base = TREE_OPERAND (base, 0);
8645 base = get_inner_reference (base, &bitsize2,
8646 &bitpos2, &offset2,
8647 &mode, &unsignedp,
8648 &reversep, &volatilep);
8649 if ((TREE_CODE (base) == INDIRECT_REF
8650 || (TREE_CODE (base) == MEM_REF
8651 && integer_zerop (TREE_OPERAND (base,
8652 1))))
8653 && DECL_P (TREE_OPERAND (base, 0))
8654 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
8655 0)))
8656 == REFERENCE_TYPE))
8657 base = TREE_OPERAND (base, 0);
8658 if (base != decl)
8659 break;
8660 if (scp)
8661 continue;
8662 gcc_assert (offset == NULL_TREE
8663 || poly_int_tree_p (offset));
8664 tree d1 = OMP_CLAUSE_DECL (*sc);
8665 tree d2 = OMP_CLAUSE_DECL (c);
8666 while (TREE_CODE (d1) == ARRAY_REF)
8667 d1 = TREE_OPERAND (d1, 0);
8668 while (TREE_CODE (d2) == ARRAY_REF)
8669 d2 = TREE_OPERAND (d2, 0);
8670 if (TREE_CODE (d1) == INDIRECT_REF)
8671 d1 = TREE_OPERAND (d1, 0);
8672 if (TREE_CODE (d2) == INDIRECT_REF)
8673 d2 = TREE_OPERAND (d2, 0);
8674 while (TREE_CODE (d1) == COMPONENT_REF)
8675 if (TREE_CODE (d2) == COMPONENT_REF
8676 && TREE_OPERAND (d1, 1)
8677 == TREE_OPERAND (d2, 1))
8679 d1 = TREE_OPERAND (d1, 0);
8680 d2 = TREE_OPERAND (d2, 0);
8682 else
8683 break;
8684 if (d1 == d2)
8686 error_at (OMP_CLAUSE_LOCATION (c),
8687 "%qE appears more than once in map "
8688 "clauses", OMP_CLAUSE_DECL (c));
8689 remove = true;
8690 break;
8692 if (offset2)
8693 o2 = wi::to_poly_offset (offset2);
8694 else
8695 o2 = 0;
8696 o2 += bits_to_bytes_round_down (bitpos2);
8697 if (maybe_lt (o1, o2)
8698 || (known_eq (o1, 2)
8699 && maybe_lt (bitpos, bitpos2)))
8701 if (ptr)
8702 scp = sc;
8703 else
8704 break;
8707 if (remove)
8708 break;
8709 OMP_CLAUSE_SIZE (*osc)
8710 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
8711 size_one_node);
8712 if (ptr)
8714 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8715 OMP_CLAUSE_MAP);
8716 tree cl = NULL_TREE;
8717 enum gomp_map_kind mkind
8718 = code == OMP_TARGET_EXIT_DATA
8719 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
8720 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
8721 OMP_CLAUSE_DECL (c2)
8722 = unshare_expr (OMP_CLAUSE_DECL (c));
8723 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
8724 OMP_CLAUSE_SIZE (c2)
8725 = TYPE_SIZE_UNIT (ptr_type_node);
8726 cl = scp ? *prev_list_p : c2;
8727 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
8729 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
8730 tree c3
8731 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8732 OMP_CLAUSE_MAP);
8733 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
8734 OMP_CLAUSE_DECL (c3)
8735 = unshare_expr (OMP_CLAUSE_DECL (c4));
8736 OMP_CLAUSE_SIZE (c3)
8737 = TYPE_SIZE_UNIT (ptr_type_node);
8738 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
8739 if (!scp)
8740 OMP_CLAUSE_CHAIN (c2) = c3;
8741 else
8742 cl = c3;
8744 if (scp)
8745 *scp = c2;
8746 if (sc == prev_list_p)
8748 *sc = cl;
8749 prev_list_p = NULL;
8751 else
8753 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8754 list_p = prev_list_p;
8755 prev_list_p = NULL;
8756 OMP_CLAUSE_CHAIN (c) = *sc;
8757 *sc = cl;
8758 continue;
8761 else if (*sc != c)
8763 *list_p = OMP_CLAUSE_CHAIN (c);
8764 OMP_CLAUSE_CHAIN (c) = *sc;
8765 *sc = c;
8766 continue;
8770 if (!remove
8771 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8772 && OMP_CLAUSE_CHAIN (c)
8773 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8774 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8775 == GOMP_MAP_ALWAYS_POINTER))
8776 prev_list_p = list_p;
8777 break;
8779 flags = GOVD_MAP | GOVD_EXPLICIT;
8780 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8781 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8782 flags |= GOVD_MAP_ALWAYS_TO;
8783 goto do_add;
8785 case OMP_CLAUSE_DEPEND:
8786 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8788 tree deps = OMP_CLAUSE_DECL (c);
8789 while (deps && TREE_CODE (deps) == TREE_LIST)
8791 if (TREE_CODE (TREE_PURPOSE (deps)) == TRUNC_DIV_EXPR
8792 && DECL_P (TREE_OPERAND (TREE_PURPOSE (deps), 1)))
8793 gimplify_expr (&TREE_OPERAND (TREE_PURPOSE (deps), 1),
8794 pre_p, NULL, is_gimple_val, fb_rvalue);
8795 deps = TREE_CHAIN (deps);
8797 break;
8799 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8800 break;
8801 if (handled_depend_iterators == -1)
8802 handled_depend_iterators = gimplify_omp_depend (list_p, pre_p);
8803 if (handled_depend_iterators)
8805 if (handled_depend_iterators == 2)
8806 remove = true;
8807 break;
8809 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8811 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8812 NULL, is_gimple_val, fb_rvalue);
8813 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8815 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8817 remove = true;
8818 break;
8820 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8821 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8822 is_gimple_val, fb_rvalue) == GS_ERROR)
8824 remove = true;
8825 break;
8827 break;
8829 case OMP_CLAUSE_TO:
8830 case OMP_CLAUSE_FROM:
8831 case OMP_CLAUSE__CACHE_:
8832 decl = OMP_CLAUSE_DECL (c);
8833 if (error_operand_p (decl))
8835 remove = true;
8836 break;
8838 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8839 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8840 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8841 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8842 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8844 remove = true;
8845 break;
8847 if (!DECL_P (decl))
8849 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8850 NULL, is_gimple_lvalue, fb_lvalue)
8851 == GS_ERROR)
8853 remove = true;
8854 break;
8856 break;
8858 goto do_notice;
8860 case OMP_CLAUSE_USE_DEVICE_PTR:
8861 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8862 goto do_add;
8863 case OMP_CLAUSE_IS_DEVICE_PTR:
8864 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8865 goto do_add;
8867 do_add:
8868 decl = OMP_CLAUSE_DECL (c);
8869 do_add_decl:
8870 if (error_operand_p (decl))
8872 remove = true;
8873 break;
8875 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8877 tree t = omp_member_access_dummy_var (decl);
8878 if (t)
8880 tree v = DECL_VALUE_EXPR (decl);
8881 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8882 if (outer_ctx)
8883 omp_notice_variable (outer_ctx, t, true);
8886 if (code == OACC_DATA
8887 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8888 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8889 flags |= GOVD_MAP_0LEN_ARRAY;
8890 omp_add_variable (ctx, decl, flags);
8891 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8892 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
8893 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
8894 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8896 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8897 GOVD_LOCAL | GOVD_SEEN);
8898 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8899 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8900 find_decl_expr,
8901 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8902 NULL) == NULL_TREE)
8903 omp_add_variable (ctx,
8904 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8905 GOVD_LOCAL | GOVD_SEEN);
8906 gimplify_omp_ctxp = ctx;
8907 push_gimplify_context ();
8909 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8910 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8912 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8913 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8914 pop_gimplify_context
8915 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8916 push_gimplify_context ();
8917 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8918 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8919 pop_gimplify_context
8920 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8921 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8922 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8924 gimplify_omp_ctxp = outer_ctx;
8926 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8927 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8929 gimplify_omp_ctxp = ctx;
8930 push_gimplify_context ();
8931 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8933 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8934 NULL, NULL);
8935 TREE_SIDE_EFFECTS (bind) = 1;
8936 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8937 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8939 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8940 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8941 pop_gimplify_context
8942 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8943 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8945 gimplify_omp_ctxp = outer_ctx;
8947 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8948 && OMP_CLAUSE_LINEAR_STMT (c))
8950 gimplify_omp_ctxp = ctx;
8951 push_gimplify_context ();
8952 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8954 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8955 NULL, NULL);
8956 TREE_SIDE_EFFECTS (bind) = 1;
8957 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8958 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8960 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8961 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8962 pop_gimplify_context
8963 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8964 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8966 gimplify_omp_ctxp = outer_ctx;
8968 if (notice_outer)
8969 goto do_notice;
8970 break;
8972 case OMP_CLAUSE_COPYIN:
8973 case OMP_CLAUSE_COPYPRIVATE:
8974 decl = OMP_CLAUSE_DECL (c);
8975 if (error_operand_p (decl))
8977 remove = true;
8978 break;
8980 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8981 && !remove
8982 && !omp_check_private (ctx, decl, true))
8984 remove = true;
8985 if (is_global_var (decl))
8987 if (DECL_THREAD_LOCAL_P (decl))
8988 remove = false;
8989 else if (DECL_HAS_VALUE_EXPR_P (decl))
8991 tree value = get_base_address (DECL_VALUE_EXPR (decl));
8993 if (value
8994 && DECL_P (value)
8995 && DECL_THREAD_LOCAL_P (value))
8996 remove = false;
8999 if (remove)
9000 error_at (OMP_CLAUSE_LOCATION (c),
9001 "copyprivate variable %qE is not threadprivate"
9002 " or private in outer context", DECL_NAME (decl));
9004 do_notice:
9005 if ((region_type & ORT_TASKLOOP) == ORT_TASKLOOP
9006 && outer_ctx
9007 && outer_ctx->region_type == ORT_COMBINED_PARALLEL
9008 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9009 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9010 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE))
9012 splay_tree_node on
9013 = splay_tree_lookup (outer_ctx->variables,
9014 (splay_tree_key)decl);
9015 if (on == NULL || (on->value & GOVD_DATA_SHARE_CLASS) == 0)
9017 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9018 && TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
9019 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
9020 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9021 && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
9022 == POINTER_TYPE))))
9023 omp_firstprivatize_variable (outer_ctx, decl);
9024 else
9025 omp_add_variable (outer_ctx, decl,
9026 GOVD_SEEN | GOVD_SHARED);
9027 omp_notice_variable (outer_ctx, decl, true);
9030 if (outer_ctx)
9031 omp_notice_variable (outer_ctx, decl, true);
9032 if (check_non_private
9033 && region_type == ORT_WORKSHARE
9034 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
9035 || decl == OMP_CLAUSE_DECL (c)
9036 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
9037 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
9038 == ADDR_EXPR
9039 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
9040 == POINTER_PLUS_EXPR
9041 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
9042 (OMP_CLAUSE_DECL (c), 0), 0))
9043 == ADDR_EXPR)))))
9044 && omp_check_private (ctx, decl, false))
9046 error ("%s variable %qE is private in outer context",
9047 check_non_private, DECL_NAME (decl));
9048 remove = true;
9050 break;
9052 case OMP_CLAUSE_IF:
9053 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
9054 && OMP_CLAUSE_IF_MODIFIER (c) != code)
9056 const char *p[2];
9057 for (int i = 0; i < 2; i++)
9058 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
9060 case VOID_CST: p[i] = "cancel"; break;
9061 case OMP_PARALLEL: p[i] = "parallel"; break;
9062 case OMP_SIMD: p[i] = "simd"; break;
9063 case OMP_TASK: p[i] = "task"; break;
9064 case OMP_TASKLOOP: p[i] = "taskloop"; break;
9065 case OMP_TARGET_DATA: p[i] = "target data"; break;
9066 case OMP_TARGET: p[i] = "target"; break;
9067 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
9068 case OMP_TARGET_ENTER_DATA:
9069 p[i] = "target enter data"; break;
9070 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
9071 default: gcc_unreachable ();
9073 error_at (OMP_CLAUSE_LOCATION (c),
9074 "expected %qs %<if%> clause modifier rather than %qs",
9075 p[0], p[1]);
9076 remove = true;
9078 /* Fall through. */
9080 case OMP_CLAUSE_FINAL:
9081 OMP_CLAUSE_OPERAND (c, 0)
9082 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
9083 /* Fall through. */
9085 case OMP_CLAUSE_SCHEDULE:
9086 case OMP_CLAUSE_NUM_THREADS:
9087 case OMP_CLAUSE_NUM_TEAMS:
9088 case OMP_CLAUSE_THREAD_LIMIT:
9089 case OMP_CLAUSE_DIST_SCHEDULE:
9090 case OMP_CLAUSE_DEVICE:
9091 case OMP_CLAUSE_PRIORITY:
9092 case OMP_CLAUSE_GRAINSIZE:
9093 case OMP_CLAUSE_NUM_TASKS:
9094 case OMP_CLAUSE_HINT:
9095 case OMP_CLAUSE_ASYNC:
9096 case OMP_CLAUSE_WAIT:
9097 case OMP_CLAUSE_NUM_GANGS:
9098 case OMP_CLAUSE_NUM_WORKERS:
9099 case OMP_CLAUSE_VECTOR_LENGTH:
9100 case OMP_CLAUSE_WORKER:
9101 case OMP_CLAUSE_VECTOR:
9102 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
9103 is_gimple_val, fb_rvalue) == GS_ERROR)
9104 remove = true;
9105 break;
9107 case OMP_CLAUSE_GANG:
9108 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
9109 is_gimple_val, fb_rvalue) == GS_ERROR)
9110 remove = true;
9111 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
9112 is_gimple_val, fb_rvalue) == GS_ERROR)
9113 remove = true;
9114 break;
9116 case OMP_CLAUSE_NOWAIT:
9117 nowait = 1;
9118 break;
9120 case OMP_CLAUSE_ORDERED:
9121 case OMP_CLAUSE_UNTIED:
9122 case OMP_CLAUSE_COLLAPSE:
9123 case OMP_CLAUSE_TILE:
9124 case OMP_CLAUSE_AUTO:
9125 case OMP_CLAUSE_SEQ:
9126 case OMP_CLAUSE_INDEPENDENT:
9127 case OMP_CLAUSE_MERGEABLE:
9128 case OMP_CLAUSE_PROC_BIND:
9129 case OMP_CLAUSE_SAFELEN:
9130 case OMP_CLAUSE_SIMDLEN:
9131 case OMP_CLAUSE_NOGROUP:
9132 case OMP_CLAUSE_THREADS:
9133 case OMP_CLAUSE_SIMD:
9134 case OMP_CLAUSE_IF_PRESENT:
9135 case OMP_CLAUSE_FINALIZE:
9136 break;
9138 case OMP_CLAUSE_DEFAULTMAP:
9139 enum gimplify_defaultmap_kind gdmkmin, gdmkmax;
9140 switch (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c))
9142 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
9143 gdmkmin = GDMK_SCALAR;
9144 gdmkmax = GDMK_POINTER;
9145 break;
9146 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
9147 gdmkmin = gdmkmax = GDMK_SCALAR;
9148 break;
9149 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
9150 gdmkmin = gdmkmax = GDMK_AGGREGATE;
9151 break;
9152 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALLOCATABLE:
9153 gdmkmin = gdmkmax = GDMK_ALLOCATABLE;
9154 break;
9155 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
9156 gdmkmin = gdmkmax = GDMK_POINTER;
9157 break;
9158 default:
9159 gcc_unreachable ();
9161 for (int gdmk = gdmkmin; gdmk <= gdmkmax; gdmk++)
9162 switch (OMP_CLAUSE_DEFAULTMAP_BEHAVIOR (c))
9164 case OMP_CLAUSE_DEFAULTMAP_ALLOC:
9165 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_ALLOC_ONLY;
9166 break;
9167 case OMP_CLAUSE_DEFAULTMAP_TO:
9168 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_TO_ONLY;
9169 break;
9170 case OMP_CLAUSE_DEFAULTMAP_FROM:
9171 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_FROM_ONLY;
9172 break;
9173 case OMP_CLAUSE_DEFAULTMAP_TOFROM:
9174 ctx->defaultmap[gdmk] = GOVD_MAP;
9175 break;
9176 case OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE:
9177 ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
9178 break;
9179 case OMP_CLAUSE_DEFAULTMAP_NONE:
9180 ctx->defaultmap[gdmk] = 0;
9181 break;
9182 case OMP_CLAUSE_DEFAULTMAP_DEFAULT:
9183 switch (gdmk)
9185 case GDMK_SCALAR:
9186 ctx->defaultmap[gdmk] = GOVD_FIRSTPRIVATE;
9187 break;
9188 case GDMK_AGGREGATE:
9189 case GDMK_ALLOCATABLE:
9190 ctx->defaultmap[gdmk] = GOVD_MAP;
9191 break;
9192 case GDMK_POINTER:
9193 ctx->defaultmap[gdmk] = GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
9194 break;
9195 default:
9196 gcc_unreachable ();
9198 break;
9199 default:
9200 gcc_unreachable ();
9202 break;
9204 case OMP_CLAUSE_ALIGNED:
9205 decl = OMP_CLAUSE_DECL (c);
9206 if (error_operand_p (decl))
9208 remove = true;
9209 break;
9211 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
9212 is_gimple_val, fb_rvalue) == GS_ERROR)
9214 remove = true;
9215 break;
9217 if (!is_global_var (decl)
9218 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9219 omp_add_variable (ctx, decl, GOVD_ALIGNED);
9220 break;
9222 case OMP_CLAUSE_NONTEMPORAL:
9223 decl = OMP_CLAUSE_DECL (c);
9224 if (error_operand_p (decl))
9226 remove = true;
9227 break;
9229 omp_add_variable (ctx, decl, GOVD_NONTEMPORAL);
9230 break;
9232 case OMP_CLAUSE_DEFAULT:
9233 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
9234 break;
9236 default:
9237 gcc_unreachable ();
9240 if (code == OACC_DATA
9241 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9242 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
9243 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9244 remove = true;
9245 if (remove)
9246 *list_p = OMP_CLAUSE_CHAIN (c);
9247 else
9248 list_p = &OMP_CLAUSE_CHAIN (c);
9251 gimplify_omp_ctxp = ctx;
9252 if (struct_map_to_clause)
9253 delete struct_map_to_clause;
9256 /* Return true if DECL is a candidate for shared to firstprivate
9257 optimization. We only consider non-addressable scalars, not
9258 too big, and not references. */
9260 static bool
9261 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
9263 if (TREE_ADDRESSABLE (decl))
9264 return false;
9265 tree type = TREE_TYPE (decl);
9266 if (!is_gimple_reg_type (type)
9267 || TREE_CODE (type) == REFERENCE_TYPE
9268 || TREE_ADDRESSABLE (type))
9269 return false;
9270 /* Don't optimize too large decls, as each thread/task will have
9271 its own. */
9272 HOST_WIDE_INT len = int_size_in_bytes (type);
9273 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
9274 return false;
9275 if (lang_hooks.decls.omp_privatize_by_reference (decl))
9276 return false;
9277 return true;
9280 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
9281 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
9282 GOVD_WRITTEN in outer contexts. */
9284 static void
9285 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
9287 for (; ctx; ctx = ctx->outer_context)
9289 splay_tree_node n = splay_tree_lookup (ctx->variables,
9290 (splay_tree_key) decl);
9291 if (n == NULL)
9292 continue;
9293 else if (n->value & GOVD_SHARED)
9295 n->value |= GOVD_WRITTEN;
9296 return;
9298 else if (n->value & GOVD_DATA_SHARE_CLASS)
9299 return;
9303 /* Helper callback for walk_gimple_seq to discover possible stores
9304 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
9305 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
9306 for those. */
9308 static tree
9309 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
9311 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
9313 *walk_subtrees = 0;
9314 if (!wi->is_lhs)
9315 return NULL_TREE;
9317 tree op = *tp;
9320 if (handled_component_p (op))
9321 op = TREE_OPERAND (op, 0);
9322 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
9323 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
9324 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
9325 else
9326 break;
9328 while (1);
9329 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
9330 return NULL_TREE;
9332 omp_mark_stores (gimplify_omp_ctxp, op);
9333 return NULL_TREE;
9336 /* Helper callback for walk_gimple_seq to discover possible stores
9337 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
9338 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
9339 for those. */
9341 static tree
9342 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
9343 bool *handled_ops_p,
9344 struct walk_stmt_info *wi)
9346 gimple *stmt = gsi_stmt (*gsi_p);
9347 switch (gimple_code (stmt))
9349 /* Don't recurse on OpenMP constructs for which
9350 gimplify_adjust_omp_clauses already handled the bodies,
9351 except handle gimple_omp_for_pre_body. */
9352 case GIMPLE_OMP_FOR:
9353 *handled_ops_p = true;
9354 if (gimple_omp_for_pre_body (stmt))
9355 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
9356 omp_find_stores_stmt, omp_find_stores_op, wi);
9357 break;
9358 case GIMPLE_OMP_PARALLEL:
9359 case GIMPLE_OMP_TASK:
9360 case GIMPLE_OMP_SECTIONS:
9361 case GIMPLE_OMP_SINGLE:
9362 case GIMPLE_OMP_TARGET:
9363 case GIMPLE_OMP_TEAMS:
9364 case GIMPLE_OMP_CRITICAL:
9365 *handled_ops_p = true;
9366 break;
9367 default:
9368 break;
9370 return NULL_TREE;
9373 struct gimplify_adjust_omp_clauses_data
9375 tree *list_p;
9376 gimple_seq *pre_p;
9379 /* For all variables that were not actually used within the context,
9380 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
9382 static int
9383 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
9385 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
9386 gimple_seq *pre_p
9387 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
9388 tree decl = (tree) n->key;
9389 unsigned flags = n->value;
9390 enum omp_clause_code code;
9391 tree clause;
9392 bool private_debug;
9394 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
9395 return 0;
9396 if ((flags & GOVD_SEEN) == 0)
9397 return 0;
9398 if (flags & GOVD_DEBUG_PRIVATE)
9400 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_SHARED);
9401 private_debug = true;
9403 else if (flags & GOVD_MAP)
9404 private_debug = false;
9405 else
9406 private_debug
9407 = lang_hooks.decls.omp_private_debug_clause (decl,
9408 !!(flags & GOVD_SHARED));
9409 if (private_debug)
9410 code = OMP_CLAUSE_PRIVATE;
9411 else if (flags & GOVD_MAP)
9413 code = OMP_CLAUSE_MAP;
9414 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
9415 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
9417 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
9418 return 0;
9421 else if (flags & GOVD_SHARED)
9423 if (is_global_var (decl))
9425 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
9426 while (ctx != NULL)
9428 splay_tree_node on
9429 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9430 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
9431 | GOVD_PRIVATE | GOVD_REDUCTION
9432 | GOVD_LINEAR | GOVD_MAP)) != 0)
9433 break;
9434 ctx = ctx->outer_context;
9436 if (ctx == NULL)
9437 return 0;
9439 code = OMP_CLAUSE_SHARED;
9441 else if (flags & GOVD_PRIVATE)
9442 code = OMP_CLAUSE_PRIVATE;
9443 else if (flags & GOVD_FIRSTPRIVATE)
9445 code = OMP_CLAUSE_FIRSTPRIVATE;
9446 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
9447 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
9448 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
9450 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
9451 "%<target%> construct", decl);
9452 return 0;
9455 else if (flags & GOVD_LASTPRIVATE)
9456 code = OMP_CLAUSE_LASTPRIVATE;
9457 else if (flags & (GOVD_ALIGNED | GOVD_NONTEMPORAL))
9458 return 0;
9459 else
9460 gcc_unreachable ();
9462 if (((flags & GOVD_LASTPRIVATE)
9463 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
9464 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9465 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9467 tree chain = *list_p;
9468 clause = build_omp_clause (input_location, code);
9469 OMP_CLAUSE_DECL (clause) = decl;
9470 OMP_CLAUSE_CHAIN (clause) = chain;
9471 if (private_debug)
9472 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
9473 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
9474 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
9475 else if (code == OMP_CLAUSE_SHARED
9476 && (flags & GOVD_WRITTEN) == 0
9477 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9478 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
9479 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
9480 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
9481 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
9483 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
9484 OMP_CLAUSE_DECL (nc) = decl;
9485 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
9486 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
9487 OMP_CLAUSE_DECL (clause)
9488 = build_simple_mem_ref_loc (input_location, decl);
9489 OMP_CLAUSE_DECL (clause)
9490 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
9491 build_int_cst (build_pointer_type (char_type_node), 0));
9492 OMP_CLAUSE_SIZE (clause) = size_zero_node;
9493 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9494 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
9495 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
9496 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
9497 OMP_CLAUSE_CHAIN (nc) = chain;
9498 OMP_CLAUSE_CHAIN (clause) = nc;
9499 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9500 gimplify_omp_ctxp = ctx->outer_context;
9501 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
9502 pre_p, NULL, is_gimple_val, fb_rvalue);
9503 gimplify_omp_ctxp = ctx;
9505 else if (code == OMP_CLAUSE_MAP)
9507 int kind;
9508 /* Not all combinations of these GOVD_MAP flags are actually valid. */
9509 switch (flags & (GOVD_MAP_TO_ONLY
9510 | GOVD_MAP_FORCE
9511 | GOVD_MAP_FORCE_PRESENT
9512 | GOVD_MAP_ALLOC_ONLY
9513 | GOVD_MAP_FROM_ONLY))
9515 case 0:
9516 kind = GOMP_MAP_TOFROM;
9517 break;
9518 case GOVD_MAP_FORCE:
9519 kind = GOMP_MAP_TOFROM | GOMP_MAP_FLAG_FORCE;
9520 break;
9521 case GOVD_MAP_TO_ONLY:
9522 kind = GOMP_MAP_TO;
9523 break;
9524 case GOVD_MAP_FROM_ONLY:
9525 kind = GOMP_MAP_FROM;
9526 break;
9527 case GOVD_MAP_ALLOC_ONLY:
9528 kind = GOMP_MAP_ALLOC;
9529 break;
9530 case GOVD_MAP_TO_ONLY | GOVD_MAP_FORCE:
9531 kind = GOMP_MAP_TO | GOMP_MAP_FLAG_FORCE;
9532 break;
9533 case GOVD_MAP_FORCE_PRESENT:
9534 kind = GOMP_MAP_FORCE_PRESENT;
9535 break;
9536 default:
9537 gcc_unreachable ();
9539 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
9540 if (DECL_SIZE (decl)
9541 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9543 tree decl2 = DECL_VALUE_EXPR (decl);
9544 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9545 decl2 = TREE_OPERAND (decl2, 0);
9546 gcc_assert (DECL_P (decl2));
9547 tree mem = build_simple_mem_ref (decl2);
9548 OMP_CLAUSE_DECL (clause) = mem;
9549 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9550 if (gimplify_omp_ctxp->outer_context)
9552 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
9553 omp_notice_variable (ctx, decl2, true);
9554 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
9556 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
9557 OMP_CLAUSE_MAP);
9558 OMP_CLAUSE_DECL (nc) = decl;
9559 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9560 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
9561 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
9562 else
9563 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9564 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
9565 OMP_CLAUSE_CHAIN (clause) = nc;
9567 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
9568 && lang_hooks.decls.omp_privatize_by_reference (decl))
9570 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
9571 OMP_CLAUSE_SIZE (clause)
9572 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
9573 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9574 gimplify_omp_ctxp = ctx->outer_context;
9575 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
9576 pre_p, NULL, is_gimple_val, fb_rvalue);
9577 gimplify_omp_ctxp = ctx;
9578 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
9579 OMP_CLAUSE_MAP);
9580 OMP_CLAUSE_DECL (nc) = decl;
9581 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9582 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
9583 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
9584 OMP_CLAUSE_CHAIN (clause) = nc;
9586 else
9587 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
9589 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
9591 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
9592 OMP_CLAUSE_DECL (nc) = decl;
9593 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
9594 OMP_CLAUSE_CHAIN (nc) = chain;
9595 OMP_CLAUSE_CHAIN (clause) = nc;
9596 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9597 gimplify_omp_ctxp = ctx->outer_context;
9598 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9599 gimplify_omp_ctxp = ctx;
9601 *list_p = clause;
9602 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9603 gimplify_omp_ctxp = ctx->outer_context;
9604 lang_hooks.decls.omp_finish_clause (clause, pre_p);
9605 if (gimplify_omp_ctxp)
9606 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
9607 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
9608 && DECL_P (OMP_CLAUSE_SIZE (clause)))
9609 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
9610 true);
9611 gimplify_omp_ctxp = ctx;
9612 return 0;
9615 static void
9616 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
9617 enum tree_code code)
9619 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9620 tree c, decl;
9622 if (body)
9624 struct gimplify_omp_ctx *octx;
9625 for (octx = ctx; octx; octx = octx->outer_context)
9626 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
9627 break;
9628 if (octx)
9630 struct walk_stmt_info wi;
9631 memset (&wi, 0, sizeof (wi));
9632 walk_gimple_seq (body, omp_find_stores_stmt,
9633 omp_find_stores_op, &wi);
9636 while ((c = *list_p) != NULL)
9638 splay_tree_node n;
9639 bool remove = false;
9641 switch (OMP_CLAUSE_CODE (c))
9643 case OMP_CLAUSE_FIRSTPRIVATE:
9644 if ((ctx->region_type & ORT_TARGET)
9645 && (ctx->region_type & ORT_ACC) == 0
9646 && TYPE_ATOMIC (strip_array_types
9647 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
9649 error_at (OMP_CLAUSE_LOCATION (c),
9650 "%<_Atomic%> %qD in %<firstprivate%> clause on "
9651 "%<target%> construct", OMP_CLAUSE_DECL (c));
9652 remove = true;
9653 break;
9655 /* FALLTHRU */
9656 case OMP_CLAUSE_PRIVATE:
9657 case OMP_CLAUSE_SHARED:
9658 case OMP_CLAUSE_LINEAR:
9659 decl = OMP_CLAUSE_DECL (c);
9660 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9661 remove = !(n->value & GOVD_SEEN);
9662 if (! remove)
9664 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
9665 if ((n->value & GOVD_DEBUG_PRIVATE)
9666 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
9668 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
9669 || ((n->value & GOVD_DATA_SHARE_CLASS)
9670 == GOVD_SHARED));
9671 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
9672 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
9674 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9675 && (n->value & GOVD_WRITTEN) == 0
9676 && DECL_P (decl)
9677 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9678 OMP_CLAUSE_SHARED_READONLY (c) = 1;
9679 else if (DECL_P (decl)
9680 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9681 && (n->value & GOVD_WRITTEN) != 0)
9682 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9683 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
9684 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9685 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9687 break;
9689 case OMP_CLAUSE_LASTPRIVATE:
9690 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
9691 accurately reflect the presence of a FIRSTPRIVATE clause. */
9692 decl = OMP_CLAUSE_DECL (c);
9693 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9694 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
9695 = (n->value & GOVD_FIRSTPRIVATE) != 0;
9696 if (code == OMP_DISTRIBUTE
9697 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9699 remove = true;
9700 error_at (OMP_CLAUSE_LOCATION (c),
9701 "same variable used in %<firstprivate%> and "
9702 "%<lastprivate%> clauses on %<distribute%> "
9703 "construct");
9705 if (!remove
9706 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9707 && DECL_P (decl)
9708 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9709 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9710 break;
9712 case OMP_CLAUSE_ALIGNED:
9713 decl = OMP_CLAUSE_DECL (c);
9714 if (!is_global_var (decl))
9716 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9717 remove = n == NULL || !(n->value & GOVD_SEEN);
9718 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
9720 struct gimplify_omp_ctx *octx;
9721 if (n != NULL
9722 && (n->value & (GOVD_DATA_SHARE_CLASS
9723 & ~GOVD_FIRSTPRIVATE)))
9724 remove = true;
9725 else
9726 for (octx = ctx->outer_context; octx;
9727 octx = octx->outer_context)
9729 n = splay_tree_lookup (octx->variables,
9730 (splay_tree_key) decl);
9731 if (n == NULL)
9732 continue;
9733 if (n->value & GOVD_LOCAL)
9734 break;
9735 /* We have to avoid assigning a shared variable
9736 to itself when trying to add
9737 __builtin_assume_aligned. */
9738 if (n->value & GOVD_SHARED)
9740 remove = true;
9741 break;
9746 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9748 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9749 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9750 remove = true;
9752 break;
9754 case OMP_CLAUSE_NONTEMPORAL:
9755 decl = OMP_CLAUSE_DECL (c);
9756 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9757 remove = n == NULL || !(n->value & GOVD_SEEN);
9758 break;
9760 case OMP_CLAUSE_MAP:
9761 if (code == OMP_TARGET_EXIT_DATA
9762 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
9764 remove = true;
9765 break;
9767 decl = OMP_CLAUSE_DECL (c);
9768 /* Data clauses associated with acc parallel reductions must be
9769 compatible with present_or_copy. Warn and adjust the clause
9770 if that is not the case. */
9771 if (ctx->region_type == ORT_ACC_PARALLEL)
9773 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
9774 n = NULL;
9776 if (DECL_P (t))
9777 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
9779 if (n && (n->value & GOVD_REDUCTION))
9781 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
9783 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9784 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
9785 && kind != GOMP_MAP_FORCE_PRESENT
9786 && kind != GOMP_MAP_POINTER)
9788 warning_at (OMP_CLAUSE_LOCATION (c), 0,
9789 "incompatible data clause with reduction "
9790 "on %qE; promoting to present_or_copy",
9791 DECL_NAME (t));
9792 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9796 if (!DECL_P (decl))
9798 if ((ctx->region_type & ORT_TARGET) != 0
9799 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
9801 if (TREE_CODE (decl) == INDIRECT_REF
9802 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
9803 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
9804 == REFERENCE_TYPE))
9805 decl = TREE_OPERAND (decl, 0);
9806 if (TREE_CODE (decl) == COMPONENT_REF)
9808 while (TREE_CODE (decl) == COMPONENT_REF)
9809 decl = TREE_OPERAND (decl, 0);
9810 if (DECL_P (decl))
9812 n = splay_tree_lookup (ctx->variables,
9813 (splay_tree_key) decl);
9814 if (!(n->value & GOVD_SEEN))
9815 remove = true;
9819 break;
9821 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9822 if ((ctx->region_type & ORT_TARGET) != 0
9823 && !(n->value & GOVD_SEEN)
9824 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
9825 && (!is_global_var (decl)
9826 || !lookup_attribute ("omp declare target link",
9827 DECL_ATTRIBUTES (decl))))
9829 remove = true;
9830 /* For struct element mapping, if struct is never referenced
9831 in target block and none of the mapping has always modifier,
9832 remove all the struct element mappings, which immediately
9833 follow the GOMP_MAP_STRUCT map clause. */
9834 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
9836 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
9837 while (cnt--)
9838 OMP_CLAUSE_CHAIN (c)
9839 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
9842 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
9843 && code == OMP_TARGET_EXIT_DATA)
9844 remove = true;
9845 else if (DECL_SIZE (decl)
9846 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
9847 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
9848 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9849 && (OMP_CLAUSE_MAP_KIND (c)
9850 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9852 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
9853 for these, TREE_CODE (DECL_SIZE (decl)) will always be
9854 INTEGER_CST. */
9855 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
9857 tree decl2 = DECL_VALUE_EXPR (decl);
9858 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9859 decl2 = TREE_OPERAND (decl2, 0);
9860 gcc_assert (DECL_P (decl2));
9861 tree mem = build_simple_mem_ref (decl2);
9862 OMP_CLAUSE_DECL (c) = mem;
9863 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9864 if (ctx->outer_context)
9866 omp_notice_variable (ctx->outer_context, decl2, true);
9867 omp_notice_variable (ctx->outer_context,
9868 OMP_CLAUSE_SIZE (c), true);
9870 if (((ctx->region_type & ORT_TARGET) != 0
9871 || !ctx->target_firstprivatize_array_bases)
9872 && ((n->value & GOVD_SEEN) == 0
9873 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
9875 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9876 OMP_CLAUSE_MAP);
9877 OMP_CLAUSE_DECL (nc) = decl;
9878 OMP_CLAUSE_SIZE (nc) = size_zero_node;
9879 if (ctx->target_firstprivatize_array_bases)
9880 OMP_CLAUSE_SET_MAP_KIND (nc,
9881 GOMP_MAP_FIRSTPRIVATE_POINTER);
9882 else
9883 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
9884 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9885 OMP_CLAUSE_CHAIN (c) = nc;
9886 c = nc;
9889 else
9891 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9892 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9893 gcc_assert ((n->value & GOVD_SEEN) == 0
9894 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9895 == 0));
9897 break;
9899 case OMP_CLAUSE_TO:
9900 case OMP_CLAUSE_FROM:
9901 case OMP_CLAUSE__CACHE_:
9902 decl = OMP_CLAUSE_DECL (c);
9903 if (!DECL_P (decl))
9904 break;
9905 if (DECL_SIZE (decl)
9906 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9908 tree decl2 = DECL_VALUE_EXPR (decl);
9909 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9910 decl2 = TREE_OPERAND (decl2, 0);
9911 gcc_assert (DECL_P (decl2));
9912 tree mem = build_simple_mem_ref (decl2);
9913 OMP_CLAUSE_DECL (c) = mem;
9914 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9915 if (ctx->outer_context)
9917 omp_notice_variable (ctx->outer_context, decl2, true);
9918 omp_notice_variable (ctx->outer_context,
9919 OMP_CLAUSE_SIZE (c), true);
9922 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9923 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9924 break;
9926 case OMP_CLAUSE_REDUCTION:
9927 case OMP_CLAUSE_IN_REDUCTION:
9928 case OMP_CLAUSE_TASK_REDUCTION:
9929 decl = OMP_CLAUSE_DECL (c);
9930 /* OpenACC reductions need a present_or_copy data clause.
9931 Add one if necessary. Emit error when the reduction is private. */
9932 if (ctx->region_type == ORT_ACC_PARALLEL)
9934 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9935 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9937 remove = true;
9938 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9939 "reduction on %qE", DECL_NAME (decl));
9941 else if ((n->value & GOVD_MAP) == 0)
9943 tree next = OMP_CLAUSE_CHAIN (c);
9944 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9945 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9946 OMP_CLAUSE_DECL (nc) = decl;
9947 OMP_CLAUSE_CHAIN (c) = nc;
9948 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9949 while (1)
9951 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9952 if (OMP_CLAUSE_CHAIN (nc) == NULL)
9953 break;
9954 nc = OMP_CLAUSE_CHAIN (nc);
9956 OMP_CLAUSE_CHAIN (nc) = next;
9957 n->value |= GOVD_MAP;
9960 if (DECL_P (decl)
9961 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9962 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9963 break;
9964 case OMP_CLAUSE_COPYIN:
9965 case OMP_CLAUSE_COPYPRIVATE:
9966 case OMP_CLAUSE_IF:
9967 case OMP_CLAUSE_NUM_THREADS:
9968 case OMP_CLAUSE_NUM_TEAMS:
9969 case OMP_CLAUSE_THREAD_LIMIT:
9970 case OMP_CLAUSE_DIST_SCHEDULE:
9971 case OMP_CLAUSE_DEVICE:
9972 case OMP_CLAUSE_SCHEDULE:
9973 case OMP_CLAUSE_NOWAIT:
9974 case OMP_CLAUSE_ORDERED:
9975 case OMP_CLAUSE_DEFAULT:
9976 case OMP_CLAUSE_UNTIED:
9977 case OMP_CLAUSE_COLLAPSE:
9978 case OMP_CLAUSE_FINAL:
9979 case OMP_CLAUSE_MERGEABLE:
9980 case OMP_CLAUSE_PROC_BIND:
9981 case OMP_CLAUSE_SAFELEN:
9982 case OMP_CLAUSE_SIMDLEN:
9983 case OMP_CLAUSE_DEPEND:
9984 case OMP_CLAUSE_PRIORITY:
9985 case OMP_CLAUSE_GRAINSIZE:
9986 case OMP_CLAUSE_NUM_TASKS:
9987 case OMP_CLAUSE_NOGROUP:
9988 case OMP_CLAUSE_THREADS:
9989 case OMP_CLAUSE_SIMD:
9990 case OMP_CLAUSE_HINT:
9991 case OMP_CLAUSE_DEFAULTMAP:
9992 case OMP_CLAUSE_USE_DEVICE_PTR:
9993 case OMP_CLAUSE_IS_DEVICE_PTR:
9994 case OMP_CLAUSE_ASYNC:
9995 case OMP_CLAUSE_WAIT:
9996 case OMP_CLAUSE_INDEPENDENT:
9997 case OMP_CLAUSE_NUM_GANGS:
9998 case OMP_CLAUSE_NUM_WORKERS:
9999 case OMP_CLAUSE_VECTOR_LENGTH:
10000 case OMP_CLAUSE_GANG:
10001 case OMP_CLAUSE_WORKER:
10002 case OMP_CLAUSE_VECTOR:
10003 case OMP_CLAUSE_AUTO:
10004 case OMP_CLAUSE_SEQ:
10005 case OMP_CLAUSE_TILE:
10006 case OMP_CLAUSE_IF_PRESENT:
10007 case OMP_CLAUSE_FINALIZE:
10008 break;
10010 default:
10011 gcc_unreachable ();
10014 if (remove)
10015 *list_p = OMP_CLAUSE_CHAIN (c);
10016 else
10017 list_p = &OMP_CLAUSE_CHAIN (c);
10020 /* Add in any implicit data sharing. */
10021 struct gimplify_adjust_omp_clauses_data data;
10022 data.list_p = list_p;
10023 data.pre_p = pre_p;
10024 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
10026 gimplify_omp_ctxp = ctx->outer_context;
10027 delete_omp_context (ctx);
10030 /* Gimplify OACC_CACHE. */
10032 static void
10033 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
10035 tree expr = *expr_p;
10037 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
10038 OACC_CACHE);
10039 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
10040 OACC_CACHE);
10042 /* TODO: Do something sensible with this information. */
10044 *expr_p = NULL_TREE;
10047 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
10048 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
10049 kind. The entry kind will replace the one in CLAUSE, while the exit
10050 kind will be used in a new omp_clause and returned to the caller. */
10052 static tree
10053 gimplify_oacc_declare_1 (tree clause)
10055 HOST_WIDE_INT kind, new_op;
10056 bool ret = false;
10057 tree c = NULL;
10059 kind = OMP_CLAUSE_MAP_KIND (clause);
10061 switch (kind)
10063 case GOMP_MAP_ALLOC:
10064 new_op = GOMP_MAP_RELEASE;
10065 ret = true;
10066 break;
10068 case GOMP_MAP_FROM:
10069 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
10070 new_op = GOMP_MAP_FROM;
10071 ret = true;
10072 break;
10074 case GOMP_MAP_TOFROM:
10075 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
10076 new_op = GOMP_MAP_FROM;
10077 ret = true;
10078 break;
10080 case GOMP_MAP_DEVICE_RESIDENT:
10081 case GOMP_MAP_FORCE_DEVICEPTR:
10082 case GOMP_MAP_FORCE_PRESENT:
10083 case GOMP_MAP_LINK:
10084 case GOMP_MAP_POINTER:
10085 case GOMP_MAP_TO:
10086 break;
10088 default:
10089 gcc_unreachable ();
10090 break;
10093 if (ret)
10095 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
10096 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
10097 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
10100 return c;
10103 /* Gimplify OACC_DECLARE. */
10105 static void
10106 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
10108 tree expr = *expr_p;
10109 gomp_target *stmt;
10110 tree clauses, t, decl;
10112 clauses = OACC_DECLARE_CLAUSES (expr);
10114 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
10115 gimplify_adjust_omp_clauses (pre_p, NULL, &clauses, OACC_DECLARE);
10117 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
10119 decl = OMP_CLAUSE_DECL (t);
10121 if (TREE_CODE (decl) == MEM_REF)
10122 decl = TREE_OPERAND (decl, 0);
10124 if (VAR_P (decl) && !is_oacc_declared (decl))
10126 tree attr = get_identifier ("oacc declare target");
10127 DECL_ATTRIBUTES (decl) = tree_cons (attr, NULL_TREE,
10128 DECL_ATTRIBUTES (decl));
10131 if (VAR_P (decl)
10132 && !is_global_var (decl)
10133 && DECL_CONTEXT (decl) == current_function_decl)
10135 tree c = gimplify_oacc_declare_1 (t);
10136 if (c)
10138 if (oacc_declare_returns == NULL)
10139 oacc_declare_returns = new hash_map<tree, tree>;
10141 oacc_declare_returns->put (decl, c);
10145 if (gimplify_omp_ctxp)
10146 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
10149 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
10150 clauses);
10152 gimplify_seq_add_stmt (pre_p, stmt);
10154 *expr_p = NULL_TREE;
10157 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
10158 gimplification of the body, as well as scanning the body for used
10159 variables. We need to do this scan now, because variable-sized
10160 decls will be decomposed during gimplification. */
10162 static void
10163 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
10165 tree expr = *expr_p;
10166 gimple *g;
10167 gimple_seq body = NULL;
10169 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
10170 OMP_PARALLEL_COMBINED (expr)
10171 ? ORT_COMBINED_PARALLEL
10172 : ORT_PARALLEL, OMP_PARALLEL);
10174 push_gimplify_context ();
10176 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
10177 if (gimple_code (g) == GIMPLE_BIND)
10178 pop_gimplify_context (g);
10179 else
10180 pop_gimplify_context (NULL);
10182 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
10183 OMP_PARALLEL);
10185 g = gimple_build_omp_parallel (body,
10186 OMP_PARALLEL_CLAUSES (expr),
10187 NULL_TREE, NULL_TREE);
10188 if (OMP_PARALLEL_COMBINED (expr))
10189 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
10190 gimplify_seq_add_stmt (pre_p, g);
10191 *expr_p = NULL_TREE;
10194 /* Gimplify the contents of an OMP_TASK statement. This involves
10195 gimplification of the body, as well as scanning the body for used
10196 variables. We need to do this scan now, because variable-sized
10197 decls will be decomposed during gimplification. */
10199 static void
10200 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
10202 tree expr = *expr_p;
10203 gimple *g;
10204 gimple_seq body = NULL;
10206 if (OMP_TASK_BODY (expr) == NULL_TREE)
10207 for (tree c = OMP_TASK_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
10208 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
10209 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_MUTEXINOUTSET)
10211 error_at (OMP_CLAUSE_LOCATION (c),
10212 "%<mutexinoutset%> kind in %<depend%> clause on a "
10213 "%<taskwait%> construct");
10214 break;
10217 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
10218 omp_find_clause (OMP_TASK_CLAUSES (expr),
10219 OMP_CLAUSE_UNTIED)
10220 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
10222 if (OMP_TASK_BODY (expr))
10224 push_gimplify_context ();
10226 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
10227 if (gimple_code (g) == GIMPLE_BIND)
10228 pop_gimplify_context (g);
10229 else
10230 pop_gimplify_context (NULL);
10233 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
10234 OMP_TASK);
10236 g = gimple_build_omp_task (body,
10237 OMP_TASK_CLAUSES (expr),
10238 NULL_TREE, NULL_TREE,
10239 NULL_TREE, NULL_TREE, NULL_TREE);
10240 if (OMP_TASK_BODY (expr) == NULL_TREE)
10241 gimple_omp_task_set_taskwait_p (g, true);
10242 gimplify_seq_add_stmt (pre_p, g);
10243 *expr_p = NULL_TREE;
10246 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
10247 with non-NULL OMP_FOR_INIT. Also, fill in pdata array,
10248 pdata[0] non-NULL if there is anything non-trivial in between, pdata[1]
10249 is address of OMP_PARALLEL in between if any, pdata[2] is address of
10250 OMP_FOR in between if any and pdata[3] is address of the inner
10251 OMP_FOR/OMP_SIMD. */
10253 static tree
10254 find_combined_omp_for (tree *tp, int *walk_subtrees, void *data)
10256 tree **pdata = (tree **) data;
10257 *walk_subtrees = 0;
10258 switch (TREE_CODE (*tp))
10260 case OMP_FOR:
10261 if (OMP_FOR_INIT (*tp) != NULL_TREE)
10263 pdata[3] = tp;
10264 return *tp;
10266 pdata[2] = tp;
10267 *walk_subtrees = 1;
10268 break;
10269 case OMP_SIMD:
10270 if (OMP_FOR_INIT (*tp) != NULL_TREE)
10272 pdata[3] = tp;
10273 return *tp;
10275 break;
10276 case BIND_EXPR:
10277 if (BIND_EXPR_VARS (*tp)
10278 || (BIND_EXPR_BLOCK (*tp)
10279 && BLOCK_VARS (BIND_EXPR_BLOCK (*tp))))
10280 pdata[0] = tp;
10281 *walk_subtrees = 1;
10282 break;
10283 case STATEMENT_LIST:
10284 if (!tsi_one_before_end_p (tsi_start (*tp)))
10285 pdata[0] = tp;
10286 *walk_subtrees = 1;
10287 break;
10288 case TRY_FINALLY_EXPR:
10289 pdata[0] = tp;
10290 *walk_subtrees = 1;
10291 break;
10292 case OMP_PARALLEL:
10293 pdata[1] = tp;
10294 *walk_subtrees = 1;
10295 break;
10296 default:
10297 break;
10299 return NULL_TREE;
10302 /* Gimplify the gross structure of an OMP_FOR statement. */
10304 static enum gimplify_status
10305 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
10307 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
10308 enum gimplify_status ret = GS_ALL_DONE;
10309 enum gimplify_status tret;
10310 gomp_for *gfor;
10311 gimple_seq for_body, for_pre_body;
10312 int i;
10313 bitmap has_decl_expr = NULL;
10314 enum omp_region_type ort = ORT_WORKSHARE;
10316 orig_for_stmt = for_stmt = *expr_p;
10318 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
10320 tree *data[4] = { NULL, NULL, NULL, NULL };
10321 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
10322 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
10323 find_combined_omp_for, data, NULL);
10324 if (inner_for_stmt == NULL_TREE)
10326 gcc_assert (seen_error ());
10327 *expr_p = NULL_TREE;
10328 return GS_ERROR;
10330 if (data[2] && OMP_FOR_PRE_BODY (*data[2]))
10332 append_to_statement_list_force (OMP_FOR_PRE_BODY (*data[2]),
10333 &OMP_FOR_PRE_BODY (for_stmt));
10334 OMP_FOR_PRE_BODY (*data[2]) = NULL_TREE;
10336 if (OMP_FOR_PRE_BODY (inner_for_stmt))
10338 append_to_statement_list_force (OMP_FOR_PRE_BODY (inner_for_stmt),
10339 &OMP_FOR_PRE_BODY (for_stmt));
10340 OMP_FOR_PRE_BODY (inner_for_stmt) = NULL_TREE;
10343 if (data[0])
10345 /* We have some statements or variable declarations in between
10346 the composite construct directives. Move them around the
10347 inner_for_stmt. */
10348 data[0] = expr_p;
10349 for (i = 0; i < 3; i++)
10350 if (data[i])
10352 tree t = *data[i];
10353 if (i < 2 && data[i + 1] == &OMP_BODY (t))
10354 data[i + 1] = data[i];
10355 *data[i] = OMP_BODY (t);
10356 tree body = build3 (BIND_EXPR, void_type_node, NULL_TREE,
10357 NULL_TREE, make_node (BLOCK));
10358 OMP_BODY (t) = body;
10359 append_to_statement_list_force (inner_for_stmt,
10360 &BIND_EXPR_BODY (body));
10361 *data[3] = t;
10362 data[3] = tsi_stmt_ptr (tsi_start (BIND_EXPR_BODY (body)));
10363 gcc_assert (*data[3] == inner_for_stmt);
10365 return GS_OK;
10368 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
10369 if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
10370 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10371 i)) == TREE_LIST
10372 && TREE_PURPOSE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10373 i)))
10375 tree orig = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
10376 /* Class iterators aren't allowed on OMP_SIMD, so the only
10377 case we need to solve is distribute parallel for. */
10378 gcc_assert (TREE_CODE (inner_for_stmt) == OMP_FOR
10379 && TREE_CODE (for_stmt) == OMP_DISTRIBUTE
10380 && data[1]);
10381 tree orig_decl = TREE_PURPOSE (orig);
10382 tree last = TREE_VALUE (orig);
10383 tree *pc;
10384 for (pc = &OMP_FOR_CLAUSES (inner_for_stmt);
10385 *pc; pc = &OMP_CLAUSE_CHAIN (*pc))
10386 if ((OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
10387 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE)
10388 && OMP_CLAUSE_DECL (*pc) == orig_decl)
10389 break;
10390 if (*pc == NULL_TREE)
10392 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE)
10394 /* private clause will appear only on inner_for_stmt.
10395 Change it into firstprivate, and add private clause
10396 on for_stmt. */
10397 tree c = copy_node (*pc);
10398 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10399 OMP_FOR_CLAUSES (for_stmt) = c;
10400 OMP_CLAUSE_CODE (*pc) = OMP_CLAUSE_FIRSTPRIVATE;
10401 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
10403 else
10405 /* lastprivate clause will appear on both inner_for_stmt
10406 and for_stmt. Add firstprivate clause to
10407 inner_for_stmt. */
10408 tree c = build_omp_clause (OMP_CLAUSE_LOCATION (*pc),
10409 OMP_CLAUSE_FIRSTPRIVATE);
10410 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (*pc);
10411 OMP_CLAUSE_CHAIN (c) = *pc;
10412 *pc = c;
10413 lang_hooks.decls.omp_finish_clause (*pc, pre_p);
10415 tree c = build_omp_clause (UNKNOWN_LOCATION,
10416 OMP_CLAUSE_FIRSTPRIVATE);
10417 OMP_CLAUSE_DECL (c) = last;
10418 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10419 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10420 c = build_omp_clause (UNKNOWN_LOCATION,
10421 *pc ? OMP_CLAUSE_SHARED
10422 : OMP_CLAUSE_FIRSTPRIVATE);
10423 OMP_CLAUSE_DECL (c) = orig_decl;
10424 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10425 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10427 /* Similarly, take care of C++ range for temporaries, those should
10428 be firstprivate on OMP_PARALLEL if any. */
10429 if (data[1])
10430 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (inner_for_stmt)); i++)
10431 if (OMP_FOR_ORIG_DECLS (inner_for_stmt)
10432 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10433 i)) == TREE_LIST
10434 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt),
10435 i)))
10437 tree orig
10438 = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (inner_for_stmt), i);
10439 tree v = TREE_CHAIN (orig);
10440 tree c = build_omp_clause (UNKNOWN_LOCATION,
10441 OMP_CLAUSE_FIRSTPRIVATE);
10442 /* First add firstprivate clause for the __for_end artificial
10443 decl. */
10444 OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 1);
10445 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
10446 == REFERENCE_TYPE)
10447 OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
10448 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10449 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10450 if (TREE_VEC_ELT (v, 0))
10452 /* And now the same for __for_range artificial decl if it
10453 exists. */
10454 c = build_omp_clause (UNKNOWN_LOCATION,
10455 OMP_CLAUSE_FIRSTPRIVATE);
10456 OMP_CLAUSE_DECL (c) = TREE_VEC_ELT (v, 0);
10457 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
10458 == REFERENCE_TYPE)
10459 OMP_CLAUSE_FIRSTPRIVATE_NO_REFERENCE (c) = 1;
10460 OMP_CLAUSE_CHAIN (c) = OMP_PARALLEL_CLAUSES (*data[1]);
10461 OMP_PARALLEL_CLAUSES (*data[1]) = c;
10466 switch (TREE_CODE (for_stmt))
10468 case OMP_FOR:
10469 case OMP_DISTRIBUTE:
10470 break;
10471 case OACC_LOOP:
10472 ort = ORT_ACC;
10473 break;
10474 case OMP_TASKLOOP:
10475 if (omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
10476 ort = ORT_UNTIED_TASKLOOP;
10477 else
10478 ort = ORT_TASKLOOP;
10479 break;
10480 case OMP_SIMD:
10481 ort = ORT_SIMD;
10482 break;
10483 default:
10484 gcc_unreachable ();
10487 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
10488 clause for the IV. */
10489 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10491 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
10492 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10493 decl = TREE_OPERAND (t, 0);
10494 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
10495 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10496 && OMP_CLAUSE_DECL (c) == decl)
10498 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
10499 break;
10503 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
10504 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
10505 TREE_CODE (for_stmt));
10507 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
10508 gimplify_omp_ctxp->distribute = true;
10510 /* Handle OMP_FOR_INIT. */
10511 for_pre_body = NULL;
10512 if ((ort == ORT_SIMD
10513 || (inner_for_stmt && TREE_CODE (inner_for_stmt) == OMP_SIMD))
10514 && OMP_FOR_PRE_BODY (for_stmt))
10516 has_decl_expr = BITMAP_ALLOC (NULL);
10517 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
10518 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
10519 == VAR_DECL)
10521 t = OMP_FOR_PRE_BODY (for_stmt);
10522 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
10524 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
10526 tree_stmt_iterator si;
10527 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
10528 tsi_next (&si))
10530 t = tsi_stmt (si);
10531 if (TREE_CODE (t) == DECL_EXPR
10532 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
10533 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
10537 if (OMP_FOR_PRE_BODY (for_stmt))
10539 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
10540 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
10541 else
10543 struct gimplify_omp_ctx ctx;
10544 memset (&ctx, 0, sizeof (ctx));
10545 ctx.region_type = ORT_NONE;
10546 gimplify_omp_ctxp = &ctx;
10547 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
10548 gimplify_omp_ctxp = NULL;
10551 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
10553 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
10554 for_stmt = inner_for_stmt;
10556 /* For taskloop, need to gimplify the start, end and step before the
10557 taskloop, outside of the taskloop omp context. */
10558 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10560 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10562 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10563 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
10565 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
10566 TREE_OPERAND (t, 1)
10567 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
10568 gimple_seq_empty_p (for_pre_body)
10569 ? pre_p : &for_pre_body, NULL,
10570 false);
10571 /* Reference to pointer conversion is considered useless,
10572 but is significant for firstprivate clause. Force it
10573 here. */
10574 if (TREE_CODE (type) == POINTER_TYPE
10575 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
10576 == REFERENCE_TYPE))
10578 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
10579 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
10580 TREE_OPERAND (t, 1));
10581 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
10582 ? pre_p : &for_pre_body);
10583 TREE_OPERAND (t, 1) = v;
10585 tree c = build_omp_clause (input_location,
10586 OMP_CLAUSE_FIRSTPRIVATE);
10587 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
10588 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
10589 OMP_FOR_CLAUSES (orig_for_stmt) = c;
10592 /* Handle OMP_FOR_COND. */
10593 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10594 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
10596 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
10597 TREE_OPERAND (t, 1)
10598 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
10599 gimple_seq_empty_p (for_pre_body)
10600 ? pre_p : &for_pre_body, NULL,
10601 false);
10602 /* Reference to pointer conversion is considered useless,
10603 but is significant for firstprivate clause. Force it
10604 here. */
10605 if (TREE_CODE (type) == POINTER_TYPE
10606 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 1)))
10607 == REFERENCE_TYPE))
10609 tree v = create_tmp_var (TYPE_MAIN_VARIANT (type));
10610 tree m = build2 (INIT_EXPR, TREE_TYPE (v), v,
10611 TREE_OPERAND (t, 1));
10612 gimplify_and_add (m, gimple_seq_empty_p (for_pre_body)
10613 ? pre_p : &for_pre_body);
10614 TREE_OPERAND (t, 1) = v;
10616 tree c = build_omp_clause (input_location,
10617 OMP_CLAUSE_FIRSTPRIVATE);
10618 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
10619 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
10620 OMP_FOR_CLAUSES (orig_for_stmt) = c;
10623 /* Handle OMP_FOR_INCR. */
10624 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10625 if (TREE_CODE (t) == MODIFY_EXPR)
10627 decl = TREE_OPERAND (t, 0);
10628 t = TREE_OPERAND (t, 1);
10629 tree *tp = &TREE_OPERAND (t, 1);
10630 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
10631 tp = &TREE_OPERAND (t, 0);
10633 if (!is_gimple_constant (*tp))
10635 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
10636 ? pre_p : &for_pre_body;
10637 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
10638 tree c = build_omp_clause (input_location,
10639 OMP_CLAUSE_FIRSTPRIVATE);
10640 OMP_CLAUSE_DECL (c) = *tp;
10641 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
10642 OMP_FOR_CLAUSES (orig_for_stmt) = c;
10647 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
10648 OMP_TASKLOOP);
10651 if (orig_for_stmt != for_stmt)
10652 gimplify_omp_ctxp->combined_loop = true;
10654 for_body = NULL;
10655 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
10656 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
10657 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
10658 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
10660 tree c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
10661 bool is_doacross = false;
10662 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
10664 is_doacross = true;
10665 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
10666 (OMP_FOR_INIT (for_stmt))
10667 * 2);
10669 int collapse = 1, tile = 0;
10670 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
10671 if (c)
10672 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
10673 c = omp_find_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_TILE);
10674 if (c)
10675 tile = list_length (OMP_CLAUSE_TILE_LIST (c));
10676 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10678 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10679 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10680 decl = TREE_OPERAND (t, 0);
10681 gcc_assert (DECL_P (decl));
10682 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
10683 || POINTER_TYPE_P (TREE_TYPE (decl)));
10684 if (is_doacross)
10686 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
10688 tree orig_decl = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i);
10689 if (TREE_CODE (orig_decl) == TREE_LIST)
10691 orig_decl = TREE_PURPOSE (orig_decl);
10692 if (!orig_decl)
10693 orig_decl = decl;
10695 gimplify_omp_ctxp->loop_iter_var.quick_push (orig_decl);
10697 else
10698 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
10699 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
10702 /* Make sure the iteration variable is private. */
10703 tree c = NULL_TREE;
10704 tree c2 = NULL_TREE;
10705 if (orig_for_stmt != for_stmt)
10707 /* Preserve this information until we gimplify the inner simd. */
10708 if (has_decl_expr
10709 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
10710 TREE_PRIVATE (t) = 1;
10712 else if (ort == ORT_SIMD)
10714 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10715 (splay_tree_key) decl);
10716 omp_is_private (gimplify_omp_ctxp, decl,
10717 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
10718 != 1));
10719 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
10720 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10721 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10723 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10724 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
10725 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
10726 if ((has_decl_expr
10727 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
10728 || TREE_PRIVATE (t))
10730 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10731 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10733 struct gimplify_omp_ctx *outer
10734 = gimplify_omp_ctxp->outer_context;
10735 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
10737 if (outer->region_type == ORT_WORKSHARE
10738 && outer->combined_loop)
10740 n = splay_tree_lookup (outer->variables,
10741 (splay_tree_key)decl);
10742 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10744 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10745 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10747 else
10749 struct gimplify_omp_ctx *octx = outer->outer_context;
10750 if (octx
10751 && octx->region_type == ORT_COMBINED_PARALLEL
10752 && octx->outer_context
10753 && (octx->outer_context->region_type
10754 == ORT_WORKSHARE)
10755 && octx->outer_context->combined_loop)
10757 octx = octx->outer_context;
10758 n = splay_tree_lookup (octx->variables,
10759 (splay_tree_key)decl);
10760 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10762 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
10763 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
10770 OMP_CLAUSE_DECL (c) = decl;
10771 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10772 OMP_FOR_CLAUSES (for_stmt) = c;
10773 omp_add_variable (gimplify_omp_ctxp, decl, flags);
10774 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
10776 if (outer->region_type == ORT_WORKSHARE
10777 && outer->combined_loop)
10779 if (outer->outer_context
10780 && (outer->outer_context->region_type
10781 == ORT_COMBINED_PARALLEL))
10782 outer = outer->outer_context;
10783 else if (omp_check_private (outer, decl, false))
10784 outer = NULL;
10786 else if (((outer->region_type & ORT_TASKLOOP)
10787 == ORT_TASKLOOP)
10788 && outer->combined_loop
10789 && !omp_check_private (gimplify_omp_ctxp,
10790 decl, false))
10792 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10794 omp_notice_variable (outer, decl, true);
10795 outer = NULL;
10797 if (outer)
10799 n = splay_tree_lookup (outer->variables,
10800 (splay_tree_key)decl);
10801 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10803 omp_add_variable (outer, decl,
10804 GOVD_LASTPRIVATE | GOVD_SEEN);
10805 if (outer->region_type == ORT_COMBINED_PARALLEL
10806 && outer->outer_context
10807 && (outer->outer_context->region_type
10808 == ORT_WORKSHARE)
10809 && outer->outer_context->combined_loop)
10811 outer = outer->outer_context;
10812 n = splay_tree_lookup (outer->variables,
10813 (splay_tree_key)decl);
10814 if (omp_check_private (outer, decl, false))
10815 outer = NULL;
10816 else if (n == NULL
10817 || ((n->value & GOVD_DATA_SHARE_CLASS)
10818 == 0))
10819 omp_add_variable (outer, decl,
10820 GOVD_LASTPRIVATE
10821 | GOVD_SEEN);
10822 else
10823 outer = NULL;
10825 if (outer && outer->outer_context
10826 && ((outer->outer_context->region_type
10827 & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS
10828 || (((outer->region_type & ORT_TASKLOOP)
10829 == ORT_TASKLOOP)
10830 && (outer->outer_context->region_type
10831 == ORT_COMBINED_PARALLEL))))
10833 outer = outer->outer_context;
10834 n = splay_tree_lookup (outer->variables,
10835 (splay_tree_key)decl);
10836 if (n == NULL
10837 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10838 omp_add_variable (outer, decl,
10839 GOVD_SHARED | GOVD_SEEN);
10840 else
10841 outer = NULL;
10843 if (outer && outer->outer_context)
10844 omp_notice_variable (outer->outer_context, decl,
10845 true);
10850 else
10852 bool lastprivate
10853 = (!has_decl_expr
10854 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
10855 if (TREE_PRIVATE (t))
10856 lastprivate = false;
10857 struct gimplify_omp_ctx *outer
10858 = gimplify_omp_ctxp->outer_context;
10859 if (outer && lastprivate)
10861 if (outer->region_type == ORT_WORKSHARE
10862 && outer->combined_loop)
10864 n = splay_tree_lookup (outer->variables,
10865 (splay_tree_key)decl);
10866 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
10868 lastprivate = false;
10869 outer = NULL;
10871 else if (outer->outer_context
10872 && (outer->outer_context->region_type
10873 == ORT_COMBINED_PARALLEL))
10874 outer = outer->outer_context;
10875 else if (omp_check_private (outer, decl, false))
10876 outer = NULL;
10878 else if (((outer->region_type & ORT_TASKLOOP)
10879 == ORT_TASKLOOP)
10880 && outer->combined_loop
10881 && !omp_check_private (gimplify_omp_ctxp,
10882 decl, false))
10884 else if (outer->region_type != ORT_COMBINED_PARALLEL)
10886 omp_notice_variable (outer, decl, true);
10887 outer = NULL;
10889 if (outer)
10891 n = splay_tree_lookup (outer->variables,
10892 (splay_tree_key)decl);
10893 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10895 omp_add_variable (outer, decl,
10896 GOVD_LASTPRIVATE | GOVD_SEEN);
10897 if (outer->region_type == ORT_COMBINED_PARALLEL
10898 && outer->outer_context
10899 && (outer->outer_context->region_type
10900 == ORT_WORKSHARE)
10901 && outer->outer_context->combined_loop)
10903 outer = outer->outer_context;
10904 n = splay_tree_lookup (outer->variables,
10905 (splay_tree_key)decl);
10906 if (omp_check_private (outer, decl, false))
10907 outer = NULL;
10908 else if (n == NULL
10909 || ((n->value & GOVD_DATA_SHARE_CLASS)
10910 == 0))
10911 omp_add_variable (outer, decl,
10912 GOVD_LASTPRIVATE
10913 | GOVD_SEEN);
10914 else
10915 outer = NULL;
10917 if (outer && outer->outer_context
10918 && ((outer->outer_context->region_type
10919 & ORT_COMBINED_TEAMS) == ORT_COMBINED_TEAMS
10920 || (((outer->region_type & ORT_TASKLOOP)
10921 == ORT_TASKLOOP)
10922 && (outer->outer_context->region_type
10923 == ORT_COMBINED_PARALLEL))))
10925 outer = outer->outer_context;
10926 n = splay_tree_lookup (outer->variables,
10927 (splay_tree_key)decl);
10928 if (n == NULL
10929 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
10930 omp_add_variable (outer, decl,
10931 GOVD_SHARED | GOVD_SEEN);
10932 else
10933 outer = NULL;
10935 if (outer && outer->outer_context)
10936 omp_notice_variable (outer->outer_context, decl,
10937 true);
10942 c = build_omp_clause (input_location,
10943 lastprivate ? OMP_CLAUSE_LASTPRIVATE
10944 : OMP_CLAUSE_PRIVATE);
10945 OMP_CLAUSE_DECL (c) = decl;
10946 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
10947 OMP_FOR_CLAUSES (for_stmt) = c;
10948 omp_add_variable (gimplify_omp_ctxp, decl,
10949 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
10950 | GOVD_EXPLICIT | GOVD_SEEN);
10951 c = NULL_TREE;
10954 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
10955 omp_notice_variable (gimplify_omp_ctxp, decl, true);
10956 else
10957 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
10959 /* If DECL is not a gimple register, create a temporary variable to act
10960 as an iteration counter. This is valid, since DECL cannot be
10961 modified in the body of the loop. Similarly for any iteration vars
10962 in simd with collapse > 1 where the iterator vars must be
10963 lastprivate. */
10964 if (orig_for_stmt != for_stmt)
10965 var = decl;
10966 else if (!is_gimple_reg (decl)
10967 || (ort == ORT_SIMD
10968 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
10970 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10971 /* Make sure omp_add_variable is not called on it prematurely.
10972 We call it ourselves a few lines later. */
10973 gimplify_omp_ctxp = NULL;
10974 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10975 gimplify_omp_ctxp = ctx;
10976 TREE_OPERAND (t, 0) = var;
10978 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
10980 if (ort == ORT_SIMD
10981 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
10983 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
10984 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
10985 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
10986 OMP_CLAUSE_DECL (c2) = var;
10987 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
10988 OMP_FOR_CLAUSES (for_stmt) = c2;
10989 omp_add_variable (gimplify_omp_ctxp, var,
10990 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
10991 if (c == NULL_TREE)
10993 c = c2;
10994 c2 = NULL_TREE;
10997 else
10998 omp_add_variable (gimplify_omp_ctxp, var,
10999 GOVD_PRIVATE | GOVD_SEEN);
11001 else
11002 var = decl;
11004 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
11005 is_gimple_val, fb_rvalue, false);
11006 ret = MIN (ret, tret);
11007 if (ret == GS_ERROR)
11008 return ret;
11010 /* Handle OMP_FOR_COND. */
11011 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
11012 gcc_assert (COMPARISON_CLASS_P (t));
11013 gcc_assert (TREE_OPERAND (t, 0) == decl);
11015 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
11016 is_gimple_val, fb_rvalue, false);
11017 ret = MIN (ret, tret);
11019 /* Handle OMP_FOR_INCR. */
11020 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11021 switch (TREE_CODE (t))
11023 case PREINCREMENT_EXPR:
11024 case POSTINCREMENT_EXPR:
11026 tree decl = TREE_OPERAND (t, 0);
11027 /* c_omp_for_incr_canonicalize_ptr() should have been
11028 called to massage things appropriately. */
11029 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
11031 if (orig_for_stmt != for_stmt)
11032 break;
11033 t = build_int_cst (TREE_TYPE (decl), 1);
11034 if (c)
11035 OMP_CLAUSE_LINEAR_STEP (c) = t;
11036 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
11037 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
11038 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
11039 break;
11042 case PREDECREMENT_EXPR:
11043 case POSTDECREMENT_EXPR:
11044 /* c_omp_for_incr_canonicalize_ptr() should have been
11045 called to massage things appropriately. */
11046 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
11047 if (orig_for_stmt != for_stmt)
11048 break;
11049 t = build_int_cst (TREE_TYPE (decl), -1);
11050 if (c)
11051 OMP_CLAUSE_LINEAR_STEP (c) = t;
11052 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
11053 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
11054 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
11055 break;
11057 case MODIFY_EXPR:
11058 gcc_assert (TREE_OPERAND (t, 0) == decl);
11059 TREE_OPERAND (t, 0) = var;
11061 t = TREE_OPERAND (t, 1);
11062 switch (TREE_CODE (t))
11064 case PLUS_EXPR:
11065 if (TREE_OPERAND (t, 1) == decl)
11067 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
11068 TREE_OPERAND (t, 0) = var;
11069 break;
11072 /* Fallthru. */
11073 case MINUS_EXPR:
11074 case POINTER_PLUS_EXPR:
11075 gcc_assert (TREE_OPERAND (t, 0) == decl);
11076 TREE_OPERAND (t, 0) = var;
11077 break;
11078 default:
11079 gcc_unreachable ();
11082 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
11083 is_gimple_val, fb_rvalue, false);
11084 ret = MIN (ret, tret);
11085 if (c)
11087 tree step = TREE_OPERAND (t, 1);
11088 tree stept = TREE_TYPE (decl);
11089 if (POINTER_TYPE_P (stept))
11090 stept = sizetype;
11091 step = fold_convert (stept, step);
11092 if (TREE_CODE (t) == MINUS_EXPR)
11093 step = fold_build1 (NEGATE_EXPR, stept, step);
11094 OMP_CLAUSE_LINEAR_STEP (c) = step;
11095 if (step != TREE_OPERAND (t, 1))
11097 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
11098 &for_pre_body, NULL,
11099 is_gimple_val, fb_rvalue, false);
11100 ret = MIN (ret, tret);
11103 break;
11105 default:
11106 gcc_unreachable ();
11109 if (c2)
11111 gcc_assert (c);
11112 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
11115 if ((var != decl || collapse > 1 || tile) && orig_for_stmt == for_stmt)
11117 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
11118 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
11119 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
11120 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
11121 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
11122 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
11123 && OMP_CLAUSE_DECL (c) == decl)
11125 if (is_doacross && (collapse == 1 || i >= collapse))
11126 t = var;
11127 else
11129 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11130 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
11131 gcc_assert (TREE_OPERAND (t, 0) == var);
11132 t = TREE_OPERAND (t, 1);
11133 gcc_assert (TREE_CODE (t) == PLUS_EXPR
11134 || TREE_CODE (t) == MINUS_EXPR
11135 || TREE_CODE (t) == POINTER_PLUS_EXPR);
11136 gcc_assert (TREE_OPERAND (t, 0) == var);
11137 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
11138 is_doacross ? var : decl,
11139 TREE_OPERAND (t, 1));
11141 gimple_seq *seq;
11142 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11143 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
11144 else
11145 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
11146 gimplify_assign (decl, t, seq);
11151 BITMAP_FREE (has_decl_expr);
11153 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11155 push_gimplify_context ();
11156 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
11158 OMP_FOR_BODY (orig_for_stmt)
11159 = build3 (BIND_EXPR, void_type_node, NULL,
11160 OMP_FOR_BODY (orig_for_stmt), NULL);
11161 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
11165 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
11166 &for_body);
11168 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11170 if (gimple_code (g) == GIMPLE_BIND)
11171 pop_gimplify_context (g);
11172 else
11173 pop_gimplify_context (NULL);
11176 if (orig_for_stmt != for_stmt)
11177 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
11179 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
11180 decl = TREE_OPERAND (t, 0);
11181 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
11182 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11183 gimplify_omp_ctxp = ctx->outer_context;
11184 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
11185 gimplify_omp_ctxp = ctx;
11186 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
11187 TREE_OPERAND (t, 0) = var;
11188 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11189 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
11190 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
11193 gimplify_adjust_omp_clauses (pre_p, for_body,
11194 &OMP_FOR_CLAUSES (orig_for_stmt),
11195 TREE_CODE (orig_for_stmt));
11197 int kind;
11198 switch (TREE_CODE (orig_for_stmt))
11200 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
11201 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
11202 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
11203 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
11204 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
11205 default:
11206 gcc_unreachable ();
11208 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
11209 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
11210 for_pre_body);
11211 if (orig_for_stmt != for_stmt)
11212 gimple_omp_for_set_combined_p (gfor, true);
11213 if (gimplify_omp_ctxp
11214 && (gimplify_omp_ctxp->combined_loop
11215 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
11216 && gimplify_omp_ctxp->outer_context
11217 && gimplify_omp_ctxp->outer_context->combined_loop)))
11219 gimple_omp_for_set_combined_into_p (gfor, true);
11220 if (gimplify_omp_ctxp->combined_loop)
11221 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
11222 else
11223 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
11226 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
11228 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
11229 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
11230 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
11231 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
11232 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
11233 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
11234 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
11235 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
11238 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
11239 constructs with GIMPLE_OMP_TASK sandwiched in between them.
11240 The outer taskloop stands for computing the number of iterations,
11241 counts for collapsed loops and holding taskloop specific clauses.
11242 The task construct stands for the effect of data sharing on the
11243 explicit task it creates and the inner taskloop stands for expansion
11244 of the static loop inside of the explicit task construct. */
11245 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
11247 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
11248 tree task_clauses = NULL_TREE;
11249 tree c = *gfor_clauses_ptr;
11250 tree *gtask_clauses_ptr = &task_clauses;
11251 tree outer_for_clauses = NULL_TREE;
11252 tree *gforo_clauses_ptr = &outer_for_clauses;
11253 for (; c; c = OMP_CLAUSE_CHAIN (c))
11254 switch (OMP_CLAUSE_CODE (c))
11256 /* These clauses are allowed on task, move them there. */
11257 case OMP_CLAUSE_SHARED:
11258 case OMP_CLAUSE_FIRSTPRIVATE:
11259 case OMP_CLAUSE_DEFAULT:
11260 case OMP_CLAUSE_IF:
11261 case OMP_CLAUSE_UNTIED:
11262 case OMP_CLAUSE_FINAL:
11263 case OMP_CLAUSE_MERGEABLE:
11264 case OMP_CLAUSE_PRIORITY:
11265 case OMP_CLAUSE_REDUCTION:
11266 case OMP_CLAUSE_IN_REDUCTION:
11267 *gtask_clauses_ptr = c;
11268 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11269 break;
11270 case OMP_CLAUSE_PRIVATE:
11271 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
11273 /* We want private on outer for and firstprivate
11274 on task. */
11275 *gtask_clauses_ptr
11276 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
11277 OMP_CLAUSE_FIRSTPRIVATE);
11278 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
11279 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
11280 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
11281 *gforo_clauses_ptr = c;
11282 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11284 else
11286 *gtask_clauses_ptr = c;
11287 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11289 break;
11290 /* These clauses go into outer taskloop clauses. */
11291 case OMP_CLAUSE_GRAINSIZE:
11292 case OMP_CLAUSE_NUM_TASKS:
11293 case OMP_CLAUSE_NOGROUP:
11294 *gforo_clauses_ptr = c;
11295 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11296 break;
11297 /* Taskloop clause we duplicate on both taskloops. */
11298 case OMP_CLAUSE_COLLAPSE:
11299 *gfor_clauses_ptr = c;
11300 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11301 *gforo_clauses_ptr = copy_node (c);
11302 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
11303 break;
11304 /* For lastprivate, keep the clause on inner taskloop, and add
11305 a shared clause on task. If the same decl is also firstprivate,
11306 add also firstprivate clause on the inner taskloop. */
11307 case OMP_CLAUSE_LASTPRIVATE:
11308 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
11310 /* For taskloop C++ lastprivate IVs, we want:
11311 1) private on outer taskloop
11312 2) firstprivate and shared on task
11313 3) lastprivate on inner taskloop */
11314 *gtask_clauses_ptr
11315 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
11316 OMP_CLAUSE_FIRSTPRIVATE);
11317 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
11318 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
11319 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
11320 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
11321 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
11322 OMP_CLAUSE_PRIVATE);
11323 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
11324 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
11325 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
11326 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
11328 *gfor_clauses_ptr = c;
11329 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
11330 *gtask_clauses_ptr
11331 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
11332 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
11333 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
11334 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
11335 gtask_clauses_ptr
11336 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
11337 break;
11338 default:
11339 gcc_unreachable ();
11341 *gfor_clauses_ptr = NULL_TREE;
11342 *gtask_clauses_ptr = NULL_TREE;
11343 *gforo_clauses_ptr = NULL_TREE;
11344 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
11345 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
11346 NULL_TREE, NULL_TREE, NULL_TREE);
11347 gimple_omp_task_set_taskloop_p (g, true);
11348 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
11349 gomp_for *gforo
11350 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
11351 gimple_omp_for_collapse (gfor),
11352 gimple_omp_for_pre_body (gfor));
11353 gimple_omp_for_set_pre_body (gfor, NULL);
11354 gimple_omp_for_set_combined_p (gforo, true);
11355 gimple_omp_for_set_combined_into_p (gfor, true);
11356 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
11358 tree type = TREE_TYPE (gimple_omp_for_index (gfor, i));
11359 tree v = create_tmp_var (type);
11360 gimple_omp_for_set_index (gforo, i, v);
11361 t = unshare_expr (gimple_omp_for_initial (gfor, i));
11362 gimple_omp_for_set_initial (gforo, i, t);
11363 gimple_omp_for_set_cond (gforo, i,
11364 gimple_omp_for_cond (gfor, i));
11365 t = unshare_expr (gimple_omp_for_final (gfor, i));
11366 gimple_omp_for_set_final (gforo, i, t);
11367 t = unshare_expr (gimple_omp_for_incr (gfor, i));
11368 gcc_assert (TREE_OPERAND (t, 0) == gimple_omp_for_index (gfor, i));
11369 TREE_OPERAND (t, 0) = v;
11370 gimple_omp_for_set_incr (gforo, i, t);
11371 t = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11372 OMP_CLAUSE_DECL (t) = v;
11373 OMP_CLAUSE_CHAIN (t) = gimple_omp_for_clauses (gforo);
11374 gimple_omp_for_set_clauses (gforo, t);
11376 gimplify_seq_add_stmt (pre_p, gforo);
11378 else
11379 gimplify_seq_add_stmt (pre_p, gfor);
11380 if (ret != GS_ALL_DONE)
11381 return GS_ERROR;
11382 *expr_p = NULL_TREE;
11383 return GS_ALL_DONE;
11386 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
11387 of OMP_TARGET's body. */
11389 static tree
11390 find_omp_teams (tree *tp, int *walk_subtrees, void *)
11392 *walk_subtrees = 0;
11393 switch (TREE_CODE (*tp))
11395 case OMP_TEAMS:
11396 return *tp;
11397 case BIND_EXPR:
11398 case STATEMENT_LIST:
11399 *walk_subtrees = 1;
11400 break;
11401 default:
11402 break;
11404 return NULL_TREE;
11407 /* Helper function of optimize_target_teams, determine if the expression
11408 can be computed safely before the target construct on the host. */
11410 static tree
11411 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
11413 splay_tree_node n;
11415 if (TYPE_P (*tp))
11417 *walk_subtrees = 0;
11418 return NULL_TREE;
11420 switch (TREE_CODE (*tp))
11422 case VAR_DECL:
11423 case PARM_DECL:
11424 case RESULT_DECL:
11425 *walk_subtrees = 0;
11426 if (error_operand_p (*tp)
11427 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
11428 || DECL_HAS_VALUE_EXPR_P (*tp)
11429 || DECL_THREAD_LOCAL_P (*tp)
11430 || TREE_SIDE_EFFECTS (*tp)
11431 || TREE_THIS_VOLATILE (*tp))
11432 return *tp;
11433 if (is_global_var (*tp)
11434 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
11435 || lookup_attribute ("omp declare target link",
11436 DECL_ATTRIBUTES (*tp))))
11437 return *tp;
11438 if (VAR_P (*tp)
11439 && !DECL_SEEN_IN_BIND_EXPR_P (*tp)
11440 && !is_global_var (*tp)
11441 && decl_function_context (*tp) == current_function_decl)
11442 return *tp;
11443 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
11444 (splay_tree_key) *tp);
11445 if (n == NULL)
11447 if (gimplify_omp_ctxp->defaultmap[GDMK_SCALAR] & GOVD_FIRSTPRIVATE)
11448 return NULL_TREE;
11449 return *tp;
11451 else if (n->value & GOVD_LOCAL)
11452 return *tp;
11453 else if (n->value & GOVD_FIRSTPRIVATE)
11454 return NULL_TREE;
11455 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
11456 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
11457 return NULL_TREE;
11458 return *tp;
11459 case INTEGER_CST:
11460 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
11461 return *tp;
11462 return NULL_TREE;
11463 case TARGET_EXPR:
11464 if (TARGET_EXPR_INITIAL (*tp)
11465 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
11466 return *tp;
11467 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
11468 walk_subtrees, NULL);
11469 /* Allow some reasonable subset of integral arithmetics. */
11470 case PLUS_EXPR:
11471 case MINUS_EXPR:
11472 case MULT_EXPR:
11473 case TRUNC_DIV_EXPR:
11474 case CEIL_DIV_EXPR:
11475 case FLOOR_DIV_EXPR:
11476 case ROUND_DIV_EXPR:
11477 case TRUNC_MOD_EXPR:
11478 case CEIL_MOD_EXPR:
11479 case FLOOR_MOD_EXPR:
11480 case ROUND_MOD_EXPR:
11481 case RDIV_EXPR:
11482 case EXACT_DIV_EXPR:
11483 case MIN_EXPR:
11484 case MAX_EXPR:
11485 case LSHIFT_EXPR:
11486 case RSHIFT_EXPR:
11487 case BIT_IOR_EXPR:
11488 case BIT_XOR_EXPR:
11489 case BIT_AND_EXPR:
11490 case NEGATE_EXPR:
11491 case ABS_EXPR:
11492 case BIT_NOT_EXPR:
11493 case NON_LVALUE_EXPR:
11494 CASE_CONVERT:
11495 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
11496 return *tp;
11497 return NULL_TREE;
11498 /* And disallow anything else, except for comparisons. */
11499 default:
11500 if (COMPARISON_CLASS_P (*tp))
11501 return NULL_TREE;
11502 return *tp;
11506 /* Try to determine if the num_teams and/or thread_limit expressions
11507 can have their values determined already before entering the
11508 target construct.
11509 INTEGER_CSTs trivially are,
11510 integral decls that are firstprivate (explicitly or implicitly)
11511 or explicitly map(always, to:) or map(always, tofrom:) on the target
11512 region too, and expressions involving simple arithmetics on those
11513 too, function calls are not ok, dereferencing something neither etc.
11514 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
11515 EXPR based on what we find:
11516 0 stands for clause not specified at all, use implementation default
11517 -1 stands for value that can't be determined easily before entering
11518 the target construct.
11519 If teams construct is not present at all, use 1 for num_teams
11520 and 0 for thread_limit (only one team is involved, and the thread
11521 limit is implementation defined. */
11523 static void
11524 optimize_target_teams (tree target, gimple_seq *pre_p)
11526 tree body = OMP_BODY (target);
11527 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
11528 tree num_teams = integer_zero_node;
11529 tree thread_limit = integer_zero_node;
11530 location_t num_teams_loc = EXPR_LOCATION (target);
11531 location_t thread_limit_loc = EXPR_LOCATION (target);
11532 tree c, *p, expr;
11533 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
11535 if (teams == NULL_TREE)
11536 num_teams = integer_one_node;
11537 else
11538 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
11540 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
11542 p = &num_teams;
11543 num_teams_loc = OMP_CLAUSE_LOCATION (c);
11545 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
11547 p = &thread_limit;
11548 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
11550 else
11551 continue;
11552 expr = OMP_CLAUSE_OPERAND (c, 0);
11553 if (TREE_CODE (expr) == INTEGER_CST)
11555 *p = expr;
11556 continue;
11558 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
11560 *p = integer_minus_one_node;
11561 continue;
11563 *p = expr;
11564 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
11565 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
11566 == GS_ERROR)
11568 gimplify_omp_ctxp = target_ctx;
11569 *p = integer_minus_one_node;
11570 continue;
11572 gimplify_omp_ctxp = target_ctx;
11573 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
11574 OMP_CLAUSE_OPERAND (c, 0) = *p;
11576 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11577 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
11578 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
11579 OMP_TARGET_CLAUSES (target) = c;
11580 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
11581 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
11582 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
11583 OMP_TARGET_CLAUSES (target) = c;
11586 /* Gimplify the gross structure of several OMP constructs. */
11588 static void
11589 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
11591 tree expr = *expr_p;
11592 gimple *stmt;
11593 gimple_seq body = NULL;
11594 enum omp_region_type ort;
11596 switch (TREE_CODE (expr))
11598 case OMP_SECTIONS:
11599 case OMP_SINGLE:
11600 ort = ORT_WORKSHARE;
11601 break;
11602 case OMP_TARGET:
11603 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
11604 break;
11605 case OACC_KERNELS:
11606 ort = ORT_ACC_KERNELS;
11607 break;
11608 case OACC_PARALLEL:
11609 ort = ORT_ACC_PARALLEL;
11610 break;
11611 case OACC_DATA:
11612 ort = ORT_ACC_DATA;
11613 break;
11614 case OMP_TARGET_DATA:
11615 ort = ORT_TARGET_DATA;
11616 break;
11617 case OMP_TEAMS:
11618 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
11619 if (gimplify_omp_ctxp == NULL
11620 || (gimplify_omp_ctxp->region_type == ORT_TARGET
11621 && gimplify_omp_ctxp->outer_context == NULL
11622 && lookup_attribute ("omp declare target",
11623 DECL_ATTRIBUTES (current_function_decl))))
11624 ort = (enum omp_region_type) (ort | ORT_HOST_TEAMS);
11625 break;
11626 case OACC_HOST_DATA:
11627 ort = ORT_ACC_HOST_DATA;
11628 break;
11629 default:
11630 gcc_unreachable ();
11632 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
11633 TREE_CODE (expr));
11634 if (TREE_CODE (expr) == OMP_TARGET)
11635 optimize_target_teams (expr, pre_p);
11636 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0
11637 || (ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
11639 push_gimplify_context ();
11640 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
11641 if (gimple_code (g) == GIMPLE_BIND)
11642 pop_gimplify_context (g);
11643 else
11644 pop_gimplify_context (NULL);
11645 if ((ort & ORT_TARGET_DATA) != 0)
11647 enum built_in_function end_ix;
11648 switch (TREE_CODE (expr))
11650 case OACC_DATA:
11651 case OACC_HOST_DATA:
11652 end_ix = BUILT_IN_GOACC_DATA_END;
11653 break;
11654 case OMP_TARGET_DATA:
11655 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
11656 break;
11657 default:
11658 gcc_unreachable ();
11660 tree fn = builtin_decl_explicit (end_ix);
11661 g = gimple_build_call (fn, 0);
11662 gimple_seq cleanup = NULL;
11663 gimple_seq_add_stmt (&cleanup, g);
11664 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
11665 body = NULL;
11666 gimple_seq_add_stmt (&body, g);
11669 else
11670 gimplify_and_add (OMP_BODY (expr), &body);
11671 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
11672 TREE_CODE (expr));
11674 switch (TREE_CODE (expr))
11676 case OACC_DATA:
11677 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
11678 OMP_CLAUSES (expr));
11679 break;
11680 case OACC_KERNELS:
11681 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
11682 OMP_CLAUSES (expr));
11683 break;
11684 case OACC_HOST_DATA:
11685 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
11686 OMP_CLAUSES (expr));
11687 break;
11688 case OACC_PARALLEL:
11689 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
11690 OMP_CLAUSES (expr));
11691 break;
11692 case OMP_SECTIONS:
11693 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
11694 break;
11695 case OMP_SINGLE:
11696 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
11697 break;
11698 case OMP_TARGET:
11699 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
11700 OMP_CLAUSES (expr));
11701 break;
11702 case OMP_TARGET_DATA:
11703 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
11704 OMP_CLAUSES (expr));
11705 break;
11706 case OMP_TEAMS:
11707 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
11708 if ((ort & ORT_HOST_TEAMS) == ORT_HOST_TEAMS)
11709 gimple_omp_teams_set_host (as_a <gomp_teams *> (stmt), true);
11710 break;
11711 default:
11712 gcc_unreachable ();
11715 gimplify_seq_add_stmt (pre_p, stmt);
11716 *expr_p = NULL_TREE;
11719 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
11720 target update constructs. */
11722 static void
11723 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
11725 tree expr = *expr_p;
11726 int kind;
11727 gomp_target *stmt;
11728 enum omp_region_type ort = ORT_WORKSHARE;
11730 switch (TREE_CODE (expr))
11732 case OACC_ENTER_DATA:
11733 case OACC_EXIT_DATA:
11734 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
11735 ort = ORT_ACC;
11736 break;
11737 case OACC_UPDATE:
11738 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
11739 ort = ORT_ACC;
11740 break;
11741 case OMP_TARGET_UPDATE:
11742 kind = GF_OMP_TARGET_KIND_UPDATE;
11743 break;
11744 case OMP_TARGET_ENTER_DATA:
11745 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
11746 break;
11747 case OMP_TARGET_EXIT_DATA:
11748 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
11749 break;
11750 default:
11751 gcc_unreachable ();
11753 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
11754 ort, TREE_CODE (expr));
11755 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
11756 TREE_CODE (expr));
11757 if (TREE_CODE (expr) == OACC_UPDATE
11758 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
11759 OMP_CLAUSE_IF_PRESENT))
11761 /* The runtime uses GOMP_MAP_{TO,FROM} to denote the if_present
11762 clause. */
11763 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11764 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
11765 switch (OMP_CLAUSE_MAP_KIND (c))
11767 case GOMP_MAP_FORCE_TO:
11768 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
11769 break;
11770 case GOMP_MAP_FORCE_FROM:
11771 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FROM);
11772 break;
11773 default:
11774 break;
11777 else if (TREE_CODE (expr) == OACC_EXIT_DATA
11778 && omp_find_clause (OMP_STANDALONE_CLAUSES (expr),
11779 OMP_CLAUSE_FINALIZE))
11781 /* Use GOMP_MAP_DELETE/GOMP_MAP_FORCE_FROM to denote that "finalize"
11782 semantics apply to all mappings of this OpenACC directive. */
11783 bool finalize_marked = false;
11784 for (tree c = OMP_STANDALONE_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
11785 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
11786 switch (OMP_CLAUSE_MAP_KIND (c))
11788 case GOMP_MAP_FROM:
11789 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_FORCE_FROM);
11790 finalize_marked = true;
11791 break;
11792 case GOMP_MAP_RELEASE:
11793 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_DELETE);
11794 finalize_marked = true;
11795 break;
11796 default:
11797 /* Check consistency: libgomp relies on the very first data
11798 mapping clause being marked, so make sure we did that before
11799 any other mapping clauses. */
11800 gcc_assert (finalize_marked);
11801 break;
11804 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
11806 gimplify_seq_add_stmt (pre_p, stmt);
11807 *expr_p = NULL_TREE;
11810 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
11811 stabilized the lhs of the atomic operation as *ADDR. Return true if
11812 EXPR is this stabilized form. */
11814 static bool
11815 goa_lhs_expr_p (tree expr, tree addr)
11817 /* Also include casts to other type variants. The C front end is fond
11818 of adding these for e.g. volatile variables. This is like
11819 STRIP_TYPE_NOPS but includes the main variant lookup. */
11820 STRIP_USELESS_TYPE_CONVERSION (expr);
11822 if (TREE_CODE (expr) == INDIRECT_REF)
11824 expr = TREE_OPERAND (expr, 0);
11825 while (expr != addr
11826 && (CONVERT_EXPR_P (expr)
11827 || TREE_CODE (expr) == NON_LVALUE_EXPR)
11828 && TREE_CODE (expr) == TREE_CODE (addr)
11829 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
11831 expr = TREE_OPERAND (expr, 0);
11832 addr = TREE_OPERAND (addr, 0);
11834 if (expr == addr)
11835 return true;
11836 return (TREE_CODE (addr) == ADDR_EXPR
11837 && TREE_CODE (expr) == ADDR_EXPR
11838 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
11840 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
11841 return true;
11842 return false;
11845 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
11846 expression does not involve the lhs, evaluate it into a temporary.
11847 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
11848 or -1 if an error was encountered. */
11850 static int
11851 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
11852 tree lhs_var)
11854 tree expr = *expr_p;
11855 int saw_lhs;
11857 if (goa_lhs_expr_p (expr, lhs_addr))
11859 *expr_p = lhs_var;
11860 return 1;
11862 if (is_gimple_val (expr))
11863 return 0;
11865 saw_lhs = 0;
11866 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
11868 case tcc_binary:
11869 case tcc_comparison:
11870 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
11871 lhs_var);
11872 /* FALLTHRU */
11873 case tcc_unary:
11874 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
11875 lhs_var);
11876 break;
11877 case tcc_expression:
11878 switch (TREE_CODE (expr))
11880 case TRUTH_ANDIF_EXPR:
11881 case TRUTH_ORIF_EXPR:
11882 case TRUTH_AND_EXPR:
11883 case TRUTH_OR_EXPR:
11884 case TRUTH_XOR_EXPR:
11885 case BIT_INSERT_EXPR:
11886 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
11887 lhs_addr, lhs_var);
11888 /* FALLTHRU */
11889 case TRUTH_NOT_EXPR:
11890 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11891 lhs_addr, lhs_var);
11892 break;
11893 case COMPOUND_EXPR:
11894 /* Break out any preevaluations from cp_build_modify_expr. */
11895 for (; TREE_CODE (expr) == COMPOUND_EXPR;
11896 expr = TREE_OPERAND (expr, 1))
11897 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
11898 *expr_p = expr;
11899 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
11900 default:
11901 break;
11903 break;
11904 case tcc_reference:
11905 if (TREE_CODE (expr) == BIT_FIELD_REF)
11906 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
11907 lhs_addr, lhs_var);
11908 break;
11909 default:
11910 break;
11913 if (saw_lhs == 0)
11915 enum gimplify_status gs;
11916 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
11917 if (gs != GS_ALL_DONE)
11918 saw_lhs = -1;
11921 return saw_lhs;
11924 /* Gimplify an OMP_ATOMIC statement. */
11926 static enum gimplify_status
11927 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
11929 tree addr = TREE_OPERAND (*expr_p, 0);
11930 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
11931 ? NULL : TREE_OPERAND (*expr_p, 1);
11932 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
11933 tree tmp_load;
11934 gomp_atomic_load *loadstmt;
11935 gomp_atomic_store *storestmt;
11937 tmp_load = create_tmp_reg (type);
11938 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
11939 return GS_ERROR;
11941 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
11942 != GS_ALL_DONE)
11943 return GS_ERROR;
11945 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr,
11946 OMP_ATOMIC_MEMORY_ORDER (*expr_p));
11947 gimplify_seq_add_stmt (pre_p, loadstmt);
11948 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
11949 != GS_ALL_DONE)
11950 return GS_ERROR;
11952 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
11953 rhs = tmp_load;
11954 storestmt
11955 = gimple_build_omp_atomic_store (rhs, OMP_ATOMIC_MEMORY_ORDER (*expr_p));
11956 gimplify_seq_add_stmt (pre_p, storestmt);
11957 switch (TREE_CODE (*expr_p))
11959 case OMP_ATOMIC_READ:
11960 case OMP_ATOMIC_CAPTURE_OLD:
11961 *expr_p = tmp_load;
11962 gimple_omp_atomic_set_need_value (loadstmt);
11963 break;
11964 case OMP_ATOMIC_CAPTURE_NEW:
11965 *expr_p = rhs;
11966 gimple_omp_atomic_set_need_value (storestmt);
11967 break;
11968 default:
11969 *expr_p = NULL;
11970 break;
11973 return GS_ALL_DONE;
11976 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
11977 body, and adding some EH bits. */
11979 static enum gimplify_status
11980 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
11982 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
11983 gimple *body_stmt;
11984 gtransaction *trans_stmt;
11985 gimple_seq body = NULL;
11986 int subcode = 0;
11988 /* Wrap the transaction body in a BIND_EXPR so we have a context
11989 where to put decls for OMP. */
11990 if (TREE_CODE (tbody) != BIND_EXPR)
11992 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
11993 TREE_SIDE_EFFECTS (bind) = 1;
11994 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
11995 TRANSACTION_EXPR_BODY (expr) = bind;
11998 push_gimplify_context ();
11999 temp = voidify_wrapper_expr (*expr_p, NULL);
12001 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
12002 pop_gimplify_context (body_stmt);
12004 trans_stmt = gimple_build_transaction (body);
12005 if (TRANSACTION_EXPR_OUTER (expr))
12006 subcode = GTMA_IS_OUTER;
12007 else if (TRANSACTION_EXPR_RELAXED (expr))
12008 subcode = GTMA_IS_RELAXED;
12009 gimple_transaction_set_subcode (trans_stmt, subcode);
12011 gimplify_seq_add_stmt (pre_p, trans_stmt);
12013 if (temp)
12015 *expr_p = temp;
12016 return GS_OK;
12019 *expr_p = NULL_TREE;
12020 return GS_ALL_DONE;
12023 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
12024 is the OMP_BODY of the original EXPR (which has already been
12025 gimplified so it's not present in the EXPR).
12027 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
12029 static gimple *
12030 gimplify_omp_ordered (tree expr, gimple_seq body)
12032 tree c, decls;
12033 int failures = 0;
12034 unsigned int i;
12035 tree source_c = NULL_TREE;
12036 tree sink_c = NULL_TREE;
12038 if (gimplify_omp_ctxp)
12040 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
12041 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12042 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
12043 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
12044 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
12046 error_at (OMP_CLAUSE_LOCATION (c),
12047 "%<ordered%> construct with %<depend%> clause must be "
12048 "closely nested inside a loop with %<ordered%> clause "
12049 "with a parameter");
12050 failures++;
12052 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12053 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
12055 bool fail = false;
12056 for (decls = OMP_CLAUSE_DECL (c), i = 0;
12057 decls && TREE_CODE (decls) == TREE_LIST;
12058 decls = TREE_CHAIN (decls), ++i)
12059 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
12060 continue;
12061 else if (TREE_VALUE (decls)
12062 != gimplify_omp_ctxp->loop_iter_var[2 * i])
12064 error_at (OMP_CLAUSE_LOCATION (c),
12065 "variable %qE is not an iteration "
12066 "of outermost loop %d, expected %qE",
12067 TREE_VALUE (decls), i + 1,
12068 gimplify_omp_ctxp->loop_iter_var[2 * i]);
12069 fail = true;
12070 failures++;
12072 else
12073 TREE_VALUE (decls)
12074 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
12075 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
12077 error_at (OMP_CLAUSE_LOCATION (c),
12078 "number of variables in %<depend(sink)%> "
12079 "clause does not match number of "
12080 "iteration variables");
12081 failures++;
12083 sink_c = c;
12085 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12086 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
12088 if (source_c)
12090 error_at (OMP_CLAUSE_LOCATION (c),
12091 "more than one %<depend(source)%> clause on an "
12092 "%<ordered%> construct");
12093 failures++;
12095 else
12096 source_c = c;
12099 if (source_c && sink_c)
12101 error_at (OMP_CLAUSE_LOCATION (source_c),
12102 "%<depend(source)%> clause specified together with "
12103 "%<depend(sink:)%> clauses on the same construct");
12104 failures++;
12107 if (failures)
12108 return gimple_build_nop ();
12109 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
12112 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
12113 expression produces a value to be used as an operand inside a GIMPLE
12114 statement, the value will be stored back in *EXPR_P. This value will
12115 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
12116 an SSA_NAME. The corresponding sequence of GIMPLE statements is
12117 emitted in PRE_P and POST_P.
12119 Additionally, this process may overwrite parts of the input
12120 expression during gimplification. Ideally, it should be
12121 possible to do non-destructive gimplification.
12123 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
12124 the expression needs to evaluate to a value to be used as
12125 an operand in a GIMPLE statement, this value will be stored in
12126 *EXPR_P on exit. This happens when the caller specifies one
12127 of fb_lvalue or fb_rvalue fallback flags.
12129 PRE_P will contain the sequence of GIMPLE statements corresponding
12130 to the evaluation of EXPR and all the side-effects that must
12131 be executed before the main expression. On exit, the last
12132 statement of PRE_P is the core statement being gimplified. For
12133 instance, when gimplifying 'if (++a)' the last statement in
12134 PRE_P will be 'if (t.1)' where t.1 is the result of
12135 pre-incrementing 'a'.
12137 POST_P will contain the sequence of GIMPLE statements corresponding
12138 to the evaluation of all the side-effects that must be executed
12139 after the main expression. If this is NULL, the post
12140 side-effects are stored at the end of PRE_P.
12142 The reason why the output is split in two is to handle post
12143 side-effects explicitly. In some cases, an expression may have
12144 inner and outer post side-effects which need to be emitted in
12145 an order different from the one given by the recursive
12146 traversal. For instance, for the expression (*p--)++ the post
12147 side-effects of '--' must actually occur *after* the post
12148 side-effects of '++'. However, gimplification will first visit
12149 the inner expression, so if a separate POST sequence was not
12150 used, the resulting sequence would be:
12152 1 t.1 = *p
12153 2 p = p - 1
12154 3 t.2 = t.1 + 1
12155 4 *p = t.2
12157 However, the post-decrement operation in line #2 must not be
12158 evaluated until after the store to *p at line #4, so the
12159 correct sequence should be:
12161 1 t.1 = *p
12162 2 t.2 = t.1 + 1
12163 3 *p = t.2
12164 4 p = p - 1
12166 So, by specifying a separate post queue, it is possible
12167 to emit the post side-effects in the correct order.
12168 If POST_P is NULL, an internal queue will be used. Before
12169 returning to the caller, the sequence POST_P is appended to
12170 the main output sequence PRE_P.
12172 GIMPLE_TEST_F points to a function that takes a tree T and
12173 returns nonzero if T is in the GIMPLE form requested by the
12174 caller. The GIMPLE predicates are in gimple.c.
12176 FALLBACK tells the function what sort of a temporary we want if
12177 gimplification cannot produce an expression that complies with
12178 GIMPLE_TEST_F.
12180 fb_none means that no temporary should be generated
12181 fb_rvalue means that an rvalue is OK to generate
12182 fb_lvalue means that an lvalue is OK to generate
12183 fb_either means that either is OK, but an lvalue is preferable.
12184 fb_mayfail means that gimplification may fail (in which case
12185 GS_ERROR will be returned)
12187 The return value is either GS_ERROR or GS_ALL_DONE, since this
12188 function iterates until EXPR is completely gimplified or an error
12189 occurs. */
12191 enum gimplify_status
12192 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12193 bool (*gimple_test_f) (tree), fallback_t fallback)
12195 tree tmp;
12196 gimple_seq internal_pre = NULL;
12197 gimple_seq internal_post = NULL;
12198 tree save_expr;
12199 bool is_statement;
12200 location_t saved_location;
12201 enum gimplify_status ret;
12202 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
12203 tree label;
12205 save_expr = *expr_p;
12206 if (save_expr == NULL_TREE)
12207 return GS_ALL_DONE;
12209 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
12210 is_statement = gimple_test_f == is_gimple_stmt;
12211 if (is_statement)
12212 gcc_assert (pre_p);
12214 /* Consistency checks. */
12215 if (gimple_test_f == is_gimple_reg)
12216 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
12217 else if (gimple_test_f == is_gimple_val
12218 || gimple_test_f == is_gimple_call_addr
12219 || gimple_test_f == is_gimple_condexpr
12220 || gimple_test_f == is_gimple_mem_rhs
12221 || gimple_test_f == is_gimple_mem_rhs_or_call
12222 || gimple_test_f == is_gimple_reg_rhs
12223 || gimple_test_f == is_gimple_reg_rhs_or_call
12224 || gimple_test_f == is_gimple_asm_val
12225 || gimple_test_f == is_gimple_mem_ref_addr)
12226 gcc_assert (fallback & fb_rvalue);
12227 else if (gimple_test_f == is_gimple_min_lval
12228 || gimple_test_f == is_gimple_lvalue)
12229 gcc_assert (fallback & fb_lvalue);
12230 else if (gimple_test_f == is_gimple_addressable)
12231 gcc_assert (fallback & fb_either);
12232 else if (gimple_test_f == is_gimple_stmt)
12233 gcc_assert (fallback == fb_none);
12234 else
12236 /* We should have recognized the GIMPLE_TEST_F predicate to
12237 know what kind of fallback to use in case a temporary is
12238 needed to hold the value or address of *EXPR_P. */
12239 gcc_unreachable ();
12242 /* We used to check the predicate here and return immediately if it
12243 succeeds. This is wrong; the design is for gimplification to be
12244 idempotent, and for the predicates to only test for valid forms, not
12245 whether they are fully simplified. */
12246 if (pre_p == NULL)
12247 pre_p = &internal_pre;
12249 if (post_p == NULL)
12250 post_p = &internal_post;
12252 /* Remember the last statements added to PRE_P and POST_P. Every
12253 new statement added by the gimplification helpers needs to be
12254 annotated with location information. To centralize the
12255 responsibility, we remember the last statement that had been
12256 added to both queues before gimplifying *EXPR_P. If
12257 gimplification produces new statements in PRE_P and POST_P, those
12258 statements will be annotated with the same location information
12259 as *EXPR_P. */
12260 pre_last_gsi = gsi_last (*pre_p);
12261 post_last_gsi = gsi_last (*post_p);
12263 saved_location = input_location;
12264 if (save_expr != error_mark_node
12265 && EXPR_HAS_LOCATION (*expr_p))
12266 input_location = EXPR_LOCATION (*expr_p);
12268 /* Loop over the specific gimplifiers until the toplevel node
12269 remains the same. */
12272 /* Strip away as many useless type conversions as possible
12273 at the toplevel. */
12274 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
12276 /* Remember the expr. */
12277 save_expr = *expr_p;
12279 /* Die, die, die, my darling. */
12280 if (error_operand_p (save_expr))
12282 ret = GS_ERROR;
12283 break;
12286 /* Do any language-specific gimplification. */
12287 ret = ((enum gimplify_status)
12288 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
12289 if (ret == GS_OK)
12291 if (*expr_p == NULL_TREE)
12292 break;
12293 if (*expr_p != save_expr)
12294 continue;
12296 else if (ret != GS_UNHANDLED)
12297 break;
12299 /* Make sure that all the cases set 'ret' appropriately. */
12300 ret = GS_UNHANDLED;
12301 switch (TREE_CODE (*expr_p))
12303 /* First deal with the special cases. */
12305 case POSTINCREMENT_EXPR:
12306 case POSTDECREMENT_EXPR:
12307 case PREINCREMENT_EXPR:
12308 case PREDECREMENT_EXPR:
12309 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
12310 fallback != fb_none,
12311 TREE_TYPE (*expr_p));
12312 break;
12314 case VIEW_CONVERT_EXPR:
12315 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
12316 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
12318 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12319 post_p, is_gimple_val, fb_rvalue);
12320 recalculate_side_effects (*expr_p);
12321 break;
12323 /* Fallthru. */
12325 case ARRAY_REF:
12326 case ARRAY_RANGE_REF:
12327 case REALPART_EXPR:
12328 case IMAGPART_EXPR:
12329 case COMPONENT_REF:
12330 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
12331 fallback ? fallback : fb_rvalue);
12332 break;
12334 case COND_EXPR:
12335 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
12337 /* C99 code may assign to an array in a structure value of a
12338 conditional expression, and this has undefined behavior
12339 only on execution, so create a temporary if an lvalue is
12340 required. */
12341 if (fallback == fb_lvalue)
12343 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
12344 mark_addressable (*expr_p);
12345 ret = GS_OK;
12347 break;
12349 case CALL_EXPR:
12350 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
12352 /* C99 code may assign to an array in a structure returned
12353 from a function, and this has undefined behavior only on
12354 execution, so create a temporary if an lvalue is
12355 required. */
12356 if (fallback == fb_lvalue)
12358 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
12359 mark_addressable (*expr_p);
12360 ret = GS_OK;
12362 break;
12364 case TREE_LIST:
12365 gcc_unreachable ();
12367 case COMPOUND_EXPR:
12368 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
12369 break;
12371 case COMPOUND_LITERAL_EXPR:
12372 ret = gimplify_compound_literal_expr (expr_p, pre_p,
12373 gimple_test_f, fallback);
12374 break;
12376 case MODIFY_EXPR:
12377 case INIT_EXPR:
12378 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
12379 fallback != fb_none);
12380 break;
12382 case TRUTH_ANDIF_EXPR:
12383 case TRUTH_ORIF_EXPR:
12385 /* Preserve the original type of the expression and the
12386 source location of the outer expression. */
12387 tree org_type = TREE_TYPE (*expr_p);
12388 *expr_p = gimple_boolify (*expr_p);
12389 *expr_p = build3_loc (input_location, COND_EXPR,
12390 org_type, *expr_p,
12391 fold_convert_loc
12392 (input_location,
12393 org_type, boolean_true_node),
12394 fold_convert_loc
12395 (input_location,
12396 org_type, boolean_false_node));
12397 ret = GS_OK;
12398 break;
12401 case TRUTH_NOT_EXPR:
12403 tree type = TREE_TYPE (*expr_p);
12404 /* The parsers are careful to generate TRUTH_NOT_EXPR
12405 only with operands that are always zero or one.
12406 We do not fold here but handle the only interesting case
12407 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
12408 *expr_p = gimple_boolify (*expr_p);
12409 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
12410 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
12411 TREE_TYPE (*expr_p),
12412 TREE_OPERAND (*expr_p, 0));
12413 else
12414 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
12415 TREE_TYPE (*expr_p),
12416 TREE_OPERAND (*expr_p, 0),
12417 build_int_cst (TREE_TYPE (*expr_p), 1));
12418 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
12419 *expr_p = fold_convert_loc (input_location, type, *expr_p);
12420 ret = GS_OK;
12421 break;
12424 case ADDR_EXPR:
12425 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
12426 break;
12428 case ANNOTATE_EXPR:
12430 tree cond = TREE_OPERAND (*expr_p, 0);
12431 tree kind = TREE_OPERAND (*expr_p, 1);
12432 tree data = TREE_OPERAND (*expr_p, 2);
12433 tree type = TREE_TYPE (cond);
12434 if (!INTEGRAL_TYPE_P (type))
12436 *expr_p = cond;
12437 ret = GS_OK;
12438 break;
12440 tree tmp = create_tmp_var (type);
12441 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
12442 gcall *call
12443 = gimple_build_call_internal (IFN_ANNOTATE, 3, cond, kind, data);
12444 gimple_call_set_lhs (call, tmp);
12445 gimplify_seq_add_stmt (pre_p, call);
12446 *expr_p = tmp;
12447 ret = GS_ALL_DONE;
12448 break;
12451 case VA_ARG_EXPR:
12452 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
12453 break;
12455 CASE_CONVERT:
12456 if (IS_EMPTY_STMT (*expr_p))
12458 ret = GS_ALL_DONE;
12459 break;
12462 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
12463 || fallback == fb_none)
12465 /* Just strip a conversion to void (or in void context) and
12466 try again. */
12467 *expr_p = TREE_OPERAND (*expr_p, 0);
12468 ret = GS_OK;
12469 break;
12472 ret = gimplify_conversion (expr_p);
12473 if (ret == GS_ERROR)
12474 break;
12475 if (*expr_p != save_expr)
12476 break;
12477 /* FALLTHRU */
12479 case FIX_TRUNC_EXPR:
12480 /* unary_expr: ... | '(' cast ')' val | ... */
12481 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12482 is_gimple_val, fb_rvalue);
12483 recalculate_side_effects (*expr_p);
12484 break;
12486 case INDIRECT_REF:
12488 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
12489 bool notrap = TREE_THIS_NOTRAP (*expr_p);
12490 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
12492 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
12493 if (*expr_p != save_expr)
12495 ret = GS_OK;
12496 break;
12499 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12500 is_gimple_reg, fb_rvalue);
12501 if (ret == GS_ERROR)
12502 break;
12504 recalculate_side_effects (*expr_p);
12505 *expr_p = fold_build2_loc (input_location, MEM_REF,
12506 TREE_TYPE (*expr_p),
12507 TREE_OPERAND (*expr_p, 0),
12508 build_int_cst (saved_ptr_type, 0));
12509 TREE_THIS_VOLATILE (*expr_p) = volatilep;
12510 TREE_THIS_NOTRAP (*expr_p) = notrap;
12511 ret = GS_OK;
12512 break;
12515 /* We arrive here through the various re-gimplifcation paths. */
12516 case MEM_REF:
12517 /* First try re-folding the whole thing. */
12518 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
12519 TREE_OPERAND (*expr_p, 0),
12520 TREE_OPERAND (*expr_p, 1));
12521 if (tmp)
12523 REF_REVERSE_STORAGE_ORDER (tmp)
12524 = REF_REVERSE_STORAGE_ORDER (*expr_p);
12525 *expr_p = tmp;
12526 recalculate_side_effects (*expr_p);
12527 ret = GS_OK;
12528 break;
12530 /* Avoid re-gimplifying the address operand if it is already
12531 in suitable form. Re-gimplifying would mark the address
12532 operand addressable. Always gimplify when not in SSA form
12533 as we still may have to gimplify decls with value-exprs. */
12534 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
12535 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
12537 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12538 is_gimple_mem_ref_addr, fb_rvalue);
12539 if (ret == GS_ERROR)
12540 break;
12542 recalculate_side_effects (*expr_p);
12543 ret = GS_ALL_DONE;
12544 break;
12546 /* Constants need not be gimplified. */
12547 case INTEGER_CST:
12548 case REAL_CST:
12549 case FIXED_CST:
12550 case STRING_CST:
12551 case COMPLEX_CST:
12552 case VECTOR_CST:
12553 /* Drop the overflow flag on constants, we do not want
12554 that in the GIMPLE IL. */
12555 if (TREE_OVERFLOW_P (*expr_p))
12556 *expr_p = drop_tree_overflow (*expr_p);
12557 ret = GS_ALL_DONE;
12558 break;
12560 case CONST_DECL:
12561 /* If we require an lvalue, such as for ADDR_EXPR, retain the
12562 CONST_DECL node. Otherwise the decl is replaceable by its
12563 value. */
12564 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
12565 if (fallback & fb_lvalue)
12566 ret = GS_ALL_DONE;
12567 else
12569 *expr_p = DECL_INITIAL (*expr_p);
12570 ret = GS_OK;
12572 break;
12574 case DECL_EXPR:
12575 ret = gimplify_decl_expr (expr_p, pre_p);
12576 break;
12578 case BIND_EXPR:
12579 ret = gimplify_bind_expr (expr_p, pre_p);
12580 break;
12582 case LOOP_EXPR:
12583 ret = gimplify_loop_expr (expr_p, pre_p);
12584 break;
12586 case SWITCH_EXPR:
12587 ret = gimplify_switch_expr (expr_p, pre_p);
12588 break;
12590 case EXIT_EXPR:
12591 ret = gimplify_exit_expr (expr_p);
12592 break;
12594 case GOTO_EXPR:
12595 /* If the target is not LABEL, then it is a computed jump
12596 and the target needs to be gimplified. */
12597 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
12599 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
12600 NULL, is_gimple_val, fb_rvalue);
12601 if (ret == GS_ERROR)
12602 break;
12604 gimplify_seq_add_stmt (pre_p,
12605 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
12606 ret = GS_ALL_DONE;
12607 break;
12609 case PREDICT_EXPR:
12610 gimplify_seq_add_stmt (pre_p,
12611 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
12612 PREDICT_EXPR_OUTCOME (*expr_p)));
12613 ret = GS_ALL_DONE;
12614 break;
12616 case LABEL_EXPR:
12617 ret = gimplify_label_expr (expr_p, pre_p);
12618 label = LABEL_EXPR_LABEL (*expr_p);
12619 gcc_assert (decl_function_context (label) == current_function_decl);
12621 /* If the label is used in a goto statement, or address of the label
12622 is taken, we need to unpoison all variables that were seen so far.
12623 Doing so would prevent us from reporting a false positives. */
12624 if (asan_poisoned_variables
12625 && asan_used_labels != NULL
12626 && asan_used_labels->contains (label))
12627 asan_poison_variables (asan_poisoned_variables, false, pre_p);
12628 break;
12630 case CASE_LABEL_EXPR:
12631 ret = gimplify_case_label_expr (expr_p, pre_p);
12633 if (gimplify_ctxp->live_switch_vars)
12634 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
12635 pre_p);
12636 break;
12638 case RETURN_EXPR:
12639 ret = gimplify_return_expr (*expr_p, pre_p);
12640 break;
12642 case CONSTRUCTOR:
12643 /* Don't reduce this in place; let gimplify_init_constructor work its
12644 magic. Buf if we're just elaborating this for side effects, just
12645 gimplify any element that has side-effects. */
12646 if (fallback == fb_none)
12648 unsigned HOST_WIDE_INT ix;
12649 tree val;
12650 tree temp = NULL_TREE;
12651 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
12652 if (TREE_SIDE_EFFECTS (val))
12653 append_to_statement_list (val, &temp);
12655 *expr_p = temp;
12656 ret = temp ? GS_OK : GS_ALL_DONE;
12658 /* C99 code may assign to an array in a constructed
12659 structure or union, and this has undefined behavior only
12660 on execution, so create a temporary if an lvalue is
12661 required. */
12662 else if (fallback == fb_lvalue)
12664 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
12665 mark_addressable (*expr_p);
12666 ret = GS_OK;
12668 else
12669 ret = GS_ALL_DONE;
12670 break;
12672 /* The following are special cases that are not handled by the
12673 original GIMPLE grammar. */
12675 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
12676 eliminated. */
12677 case SAVE_EXPR:
12678 ret = gimplify_save_expr (expr_p, pre_p, post_p);
12679 break;
12681 case BIT_FIELD_REF:
12682 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12683 post_p, is_gimple_lvalue, fb_either);
12684 recalculate_side_effects (*expr_p);
12685 break;
12687 case TARGET_MEM_REF:
12689 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
12691 if (TMR_BASE (*expr_p))
12692 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
12693 post_p, is_gimple_mem_ref_addr, fb_either);
12694 if (TMR_INDEX (*expr_p))
12695 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
12696 post_p, is_gimple_val, fb_rvalue);
12697 if (TMR_INDEX2 (*expr_p))
12698 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
12699 post_p, is_gimple_val, fb_rvalue);
12700 /* TMR_STEP and TMR_OFFSET are always integer constants. */
12701 ret = MIN (r0, r1);
12703 break;
12705 case NON_LVALUE_EXPR:
12706 /* This should have been stripped above. */
12707 gcc_unreachable ();
12709 case ASM_EXPR:
12710 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
12711 break;
12713 case TRY_FINALLY_EXPR:
12714 case TRY_CATCH_EXPR:
12716 gimple_seq eval, cleanup;
12717 gtry *try_;
12719 /* Calls to destructors are generated automatically in FINALLY/CATCH
12720 block. They should have location as UNKNOWN_LOCATION. However,
12721 gimplify_call_expr will reset these call stmts to input_location
12722 if it finds stmt's location is unknown. To prevent resetting for
12723 destructors, we set the input_location to unknown.
12724 Note that this only affects the destructor calls in FINALLY/CATCH
12725 block, and will automatically reset to its original value by the
12726 end of gimplify_expr. */
12727 input_location = UNKNOWN_LOCATION;
12728 eval = cleanup = NULL;
12729 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
12730 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
12731 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
12732 if (gimple_seq_empty_p (cleanup))
12734 gimple_seq_add_seq (pre_p, eval);
12735 ret = GS_ALL_DONE;
12736 break;
12738 try_ = gimple_build_try (eval, cleanup,
12739 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
12740 ? GIMPLE_TRY_FINALLY
12741 : GIMPLE_TRY_CATCH);
12742 if (EXPR_HAS_LOCATION (save_expr))
12743 gimple_set_location (try_, EXPR_LOCATION (save_expr));
12744 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
12745 gimple_set_location (try_, saved_location);
12746 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
12747 gimple_try_set_catch_is_cleanup (try_,
12748 TRY_CATCH_IS_CLEANUP (*expr_p));
12749 gimplify_seq_add_stmt (pre_p, try_);
12750 ret = GS_ALL_DONE;
12751 break;
12754 case CLEANUP_POINT_EXPR:
12755 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
12756 break;
12758 case TARGET_EXPR:
12759 ret = gimplify_target_expr (expr_p, pre_p, post_p);
12760 break;
12762 case CATCH_EXPR:
12764 gimple *c;
12765 gimple_seq handler = NULL;
12766 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
12767 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
12768 gimplify_seq_add_stmt (pre_p, c);
12769 ret = GS_ALL_DONE;
12770 break;
12773 case EH_FILTER_EXPR:
12775 gimple *ehf;
12776 gimple_seq failure = NULL;
12778 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
12779 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
12780 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
12781 gimplify_seq_add_stmt (pre_p, ehf);
12782 ret = GS_ALL_DONE;
12783 break;
12786 case OBJ_TYPE_REF:
12788 enum gimplify_status r0, r1;
12789 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
12790 post_p, is_gimple_val, fb_rvalue);
12791 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
12792 post_p, is_gimple_val, fb_rvalue);
12793 TREE_SIDE_EFFECTS (*expr_p) = 0;
12794 ret = MIN (r0, r1);
12796 break;
12798 case LABEL_DECL:
12799 /* We get here when taking the address of a label. We mark
12800 the label as "forced"; meaning it can never be removed and
12801 it is a potential target for any computed goto. */
12802 FORCED_LABEL (*expr_p) = 1;
12803 ret = GS_ALL_DONE;
12804 break;
12806 case STATEMENT_LIST:
12807 ret = gimplify_statement_list (expr_p, pre_p);
12808 break;
12810 case WITH_SIZE_EXPR:
12812 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
12813 post_p == &internal_post ? NULL : post_p,
12814 gimple_test_f, fallback);
12815 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12816 is_gimple_val, fb_rvalue);
12817 ret = GS_ALL_DONE;
12819 break;
12821 case VAR_DECL:
12822 case PARM_DECL:
12823 ret = gimplify_var_or_parm_decl (expr_p);
12824 break;
12826 case RESULT_DECL:
12827 /* When within an OMP context, notice uses of variables. */
12828 if (gimplify_omp_ctxp)
12829 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
12830 ret = GS_ALL_DONE;
12831 break;
12833 case DEBUG_EXPR_DECL:
12834 gcc_unreachable ();
12836 case DEBUG_BEGIN_STMT:
12837 gimplify_seq_add_stmt (pre_p,
12838 gimple_build_debug_begin_stmt
12839 (TREE_BLOCK (*expr_p),
12840 EXPR_LOCATION (*expr_p)));
12841 ret = GS_ALL_DONE;
12842 *expr_p = NULL;
12843 break;
12845 case SSA_NAME:
12846 /* Allow callbacks into the gimplifier during optimization. */
12847 ret = GS_ALL_DONE;
12848 break;
12850 case OMP_PARALLEL:
12851 gimplify_omp_parallel (expr_p, pre_p);
12852 ret = GS_ALL_DONE;
12853 break;
12855 case OMP_TASK:
12856 gimplify_omp_task (expr_p, pre_p);
12857 ret = GS_ALL_DONE;
12858 break;
12860 case OMP_FOR:
12861 case OMP_SIMD:
12862 case OMP_DISTRIBUTE:
12863 case OMP_TASKLOOP:
12864 case OACC_LOOP:
12865 ret = gimplify_omp_for (expr_p, pre_p);
12866 break;
12868 case OACC_CACHE:
12869 gimplify_oacc_cache (expr_p, pre_p);
12870 ret = GS_ALL_DONE;
12871 break;
12873 case OACC_DECLARE:
12874 gimplify_oacc_declare (expr_p, pre_p);
12875 ret = GS_ALL_DONE;
12876 break;
12878 case OACC_HOST_DATA:
12879 case OACC_DATA:
12880 case OACC_KERNELS:
12881 case OACC_PARALLEL:
12882 case OMP_SECTIONS:
12883 case OMP_SINGLE:
12884 case OMP_TARGET:
12885 case OMP_TARGET_DATA:
12886 case OMP_TEAMS:
12887 gimplify_omp_workshare (expr_p, pre_p);
12888 ret = GS_ALL_DONE;
12889 break;
12891 case OACC_ENTER_DATA:
12892 case OACC_EXIT_DATA:
12893 case OACC_UPDATE:
12894 case OMP_TARGET_UPDATE:
12895 case OMP_TARGET_ENTER_DATA:
12896 case OMP_TARGET_EXIT_DATA:
12897 gimplify_omp_target_update (expr_p, pre_p);
12898 ret = GS_ALL_DONE;
12899 break;
12901 case OMP_SECTION:
12902 case OMP_MASTER:
12903 case OMP_ORDERED:
12904 case OMP_CRITICAL:
12906 gimple_seq body = NULL;
12907 gimple *g;
12909 gimplify_and_add (OMP_BODY (*expr_p), &body);
12910 switch (TREE_CODE (*expr_p))
12912 case OMP_SECTION:
12913 g = gimple_build_omp_section (body);
12914 break;
12915 case OMP_MASTER:
12916 g = gimple_build_omp_master (body);
12917 break;
12918 case OMP_ORDERED:
12919 g = gimplify_omp_ordered (*expr_p, body);
12920 break;
12921 case OMP_CRITICAL:
12922 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
12923 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
12924 gimplify_adjust_omp_clauses (pre_p, body,
12925 &OMP_CRITICAL_CLAUSES (*expr_p),
12926 OMP_CRITICAL);
12927 g = gimple_build_omp_critical (body,
12928 OMP_CRITICAL_NAME (*expr_p),
12929 OMP_CRITICAL_CLAUSES (*expr_p));
12930 break;
12931 default:
12932 gcc_unreachable ();
12934 gimplify_seq_add_stmt (pre_p, g);
12935 ret = GS_ALL_DONE;
12936 break;
12939 case OMP_TASKGROUP:
12941 gimple_seq body = NULL;
12943 tree *pclauses = &OMP_TASKGROUP_CLAUSES (*expr_p);
12944 gimplify_scan_omp_clauses (pclauses, pre_p, ORT_TASKGROUP,
12945 OMP_TASKGROUP);
12946 gimplify_adjust_omp_clauses (pre_p, NULL, pclauses, OMP_TASKGROUP);
12947 gimplify_and_add (OMP_BODY (*expr_p), &body);
12948 gimple_seq cleanup = NULL;
12949 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
12950 gimple *g = gimple_build_call (fn, 0);
12951 gimple_seq_add_stmt (&cleanup, g);
12952 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
12953 body = NULL;
12954 gimple_seq_add_stmt (&body, g);
12955 g = gimple_build_omp_taskgroup (body, *pclauses);
12956 gimplify_seq_add_stmt (pre_p, g);
12957 ret = GS_ALL_DONE;
12958 break;
12961 case OMP_ATOMIC:
12962 case OMP_ATOMIC_READ:
12963 case OMP_ATOMIC_CAPTURE_OLD:
12964 case OMP_ATOMIC_CAPTURE_NEW:
12965 ret = gimplify_omp_atomic (expr_p, pre_p);
12966 break;
12968 case TRANSACTION_EXPR:
12969 ret = gimplify_transaction (expr_p, pre_p);
12970 break;
12972 case TRUTH_AND_EXPR:
12973 case TRUTH_OR_EXPR:
12974 case TRUTH_XOR_EXPR:
12976 tree orig_type = TREE_TYPE (*expr_p);
12977 tree new_type, xop0, xop1;
12978 *expr_p = gimple_boolify (*expr_p);
12979 new_type = TREE_TYPE (*expr_p);
12980 if (!useless_type_conversion_p (orig_type, new_type))
12982 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
12983 ret = GS_OK;
12984 break;
12987 /* Boolified binary truth expressions are semantically equivalent
12988 to bitwise binary expressions. Canonicalize them to the
12989 bitwise variant. */
12990 switch (TREE_CODE (*expr_p))
12992 case TRUTH_AND_EXPR:
12993 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
12994 break;
12995 case TRUTH_OR_EXPR:
12996 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
12997 break;
12998 case TRUTH_XOR_EXPR:
12999 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
13000 break;
13001 default:
13002 break;
13004 /* Now make sure that operands have compatible type to
13005 expression's new_type. */
13006 xop0 = TREE_OPERAND (*expr_p, 0);
13007 xop1 = TREE_OPERAND (*expr_p, 1);
13008 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
13009 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
13010 new_type,
13011 xop0);
13012 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
13013 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
13014 new_type,
13015 xop1);
13016 /* Continue classified as tcc_binary. */
13017 goto expr_2;
13020 case VEC_COND_EXPR:
13022 enum gimplify_status r0, r1, r2;
13024 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13025 post_p, is_gimple_condexpr, fb_rvalue);
13026 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13027 post_p, is_gimple_val, fb_rvalue);
13028 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
13029 post_p, is_gimple_val, fb_rvalue);
13031 ret = MIN (MIN (r0, r1), r2);
13032 recalculate_side_effects (*expr_p);
13034 break;
13036 case VEC_PERM_EXPR:
13037 /* Classified as tcc_expression. */
13038 goto expr_3;
13040 case BIT_INSERT_EXPR:
13041 /* Argument 3 is a constant. */
13042 goto expr_2;
13044 case POINTER_PLUS_EXPR:
13046 enum gimplify_status r0, r1;
13047 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13048 post_p, is_gimple_val, fb_rvalue);
13049 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13050 post_p, is_gimple_val, fb_rvalue);
13051 recalculate_side_effects (*expr_p);
13052 ret = MIN (r0, r1);
13053 break;
13056 default:
13057 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
13059 case tcc_comparison:
13060 /* Handle comparison of objects of non scalar mode aggregates
13061 with a call to memcmp. It would be nice to only have to do
13062 this for variable-sized objects, but then we'd have to allow
13063 the same nest of reference nodes we allow for MODIFY_EXPR and
13064 that's too complex.
13066 Compare scalar mode aggregates as scalar mode values. Using
13067 memcmp for them would be very inefficient at best, and is
13068 plain wrong if bitfields are involved. */
13070 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
13072 /* Vector comparisons need no boolification. */
13073 if (TREE_CODE (type) == VECTOR_TYPE)
13074 goto expr_2;
13075 else if (!AGGREGATE_TYPE_P (type))
13077 tree org_type = TREE_TYPE (*expr_p);
13078 *expr_p = gimple_boolify (*expr_p);
13079 if (!useless_type_conversion_p (org_type,
13080 TREE_TYPE (*expr_p)))
13082 *expr_p = fold_convert_loc (input_location,
13083 org_type, *expr_p);
13084 ret = GS_OK;
13086 else
13087 goto expr_2;
13089 else if (TYPE_MODE (type) != BLKmode)
13090 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
13091 else
13092 ret = gimplify_variable_sized_compare (expr_p);
13094 break;
13097 /* If *EXPR_P does not need to be special-cased, handle it
13098 according to its class. */
13099 case tcc_unary:
13100 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13101 post_p, is_gimple_val, fb_rvalue);
13102 break;
13104 case tcc_binary:
13105 expr_2:
13107 enum gimplify_status r0, r1;
13109 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13110 post_p, is_gimple_val, fb_rvalue);
13111 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13112 post_p, is_gimple_val, fb_rvalue);
13114 ret = MIN (r0, r1);
13115 break;
13118 expr_3:
13120 enum gimplify_status r0, r1, r2;
13122 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
13123 post_p, is_gimple_val, fb_rvalue);
13124 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
13125 post_p, is_gimple_val, fb_rvalue);
13126 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
13127 post_p, is_gimple_val, fb_rvalue);
13129 ret = MIN (MIN (r0, r1), r2);
13130 break;
13133 case tcc_declaration:
13134 case tcc_constant:
13135 ret = GS_ALL_DONE;
13136 goto dont_recalculate;
13138 default:
13139 gcc_unreachable ();
13142 recalculate_side_effects (*expr_p);
13144 dont_recalculate:
13145 break;
13148 gcc_assert (*expr_p || ret != GS_OK);
13150 while (ret == GS_OK);
13152 /* If we encountered an error_mark somewhere nested inside, either
13153 stub out the statement or propagate the error back out. */
13154 if (ret == GS_ERROR)
13156 if (is_statement)
13157 *expr_p = NULL;
13158 goto out;
13161 /* This was only valid as a return value from the langhook, which
13162 we handled. Make sure it doesn't escape from any other context. */
13163 gcc_assert (ret != GS_UNHANDLED);
13165 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
13167 /* We aren't looking for a value, and we don't have a valid
13168 statement. If it doesn't have side-effects, throw it away.
13169 We can also get here with code such as "*&&L;", where L is
13170 a LABEL_DECL that is marked as FORCED_LABEL. */
13171 if (TREE_CODE (*expr_p) == LABEL_DECL
13172 || !TREE_SIDE_EFFECTS (*expr_p))
13173 *expr_p = NULL;
13174 else if (!TREE_THIS_VOLATILE (*expr_p))
13176 /* This is probably a _REF that contains something nested that
13177 has side effects. Recurse through the operands to find it. */
13178 enum tree_code code = TREE_CODE (*expr_p);
13180 switch (code)
13182 case COMPONENT_REF:
13183 case REALPART_EXPR:
13184 case IMAGPART_EXPR:
13185 case VIEW_CONVERT_EXPR:
13186 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
13187 gimple_test_f, fallback);
13188 break;
13190 case ARRAY_REF:
13191 case ARRAY_RANGE_REF:
13192 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
13193 gimple_test_f, fallback);
13194 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
13195 gimple_test_f, fallback);
13196 break;
13198 default:
13199 /* Anything else with side-effects must be converted to
13200 a valid statement before we get here. */
13201 gcc_unreachable ();
13204 *expr_p = NULL;
13206 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
13207 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
13209 /* Historically, the compiler has treated a bare reference
13210 to a non-BLKmode volatile lvalue as forcing a load. */
13211 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
13213 /* Normally, we do not want to create a temporary for a
13214 TREE_ADDRESSABLE type because such a type should not be
13215 copied by bitwise-assignment. However, we make an
13216 exception here, as all we are doing here is ensuring that
13217 we read the bytes that make up the type. We use
13218 create_tmp_var_raw because create_tmp_var will abort when
13219 given a TREE_ADDRESSABLE type. */
13220 tree tmp = create_tmp_var_raw (type, "vol");
13221 gimple_add_tmp_var (tmp);
13222 gimplify_assign (tmp, *expr_p, pre_p);
13223 *expr_p = NULL;
13225 else
13226 /* We can't do anything useful with a volatile reference to
13227 an incomplete type, so just throw it away. Likewise for
13228 a BLKmode type, since any implicit inner load should
13229 already have been turned into an explicit one by the
13230 gimplification process. */
13231 *expr_p = NULL;
13234 /* If we are gimplifying at the statement level, we're done. Tack
13235 everything together and return. */
13236 if (fallback == fb_none || is_statement)
13238 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
13239 it out for GC to reclaim it. */
13240 *expr_p = NULL_TREE;
13242 if (!gimple_seq_empty_p (internal_pre)
13243 || !gimple_seq_empty_p (internal_post))
13245 gimplify_seq_add_seq (&internal_pre, internal_post);
13246 gimplify_seq_add_seq (pre_p, internal_pre);
13249 /* The result of gimplifying *EXPR_P is going to be the last few
13250 statements in *PRE_P and *POST_P. Add location information
13251 to all the statements that were added by the gimplification
13252 helpers. */
13253 if (!gimple_seq_empty_p (*pre_p))
13254 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
13256 if (!gimple_seq_empty_p (*post_p))
13257 annotate_all_with_location_after (*post_p, post_last_gsi,
13258 input_location);
13260 goto out;
13263 #ifdef ENABLE_GIMPLE_CHECKING
13264 if (*expr_p)
13266 enum tree_code code = TREE_CODE (*expr_p);
13267 /* These expressions should already be in gimple IR form. */
13268 gcc_assert (code != MODIFY_EXPR
13269 && code != ASM_EXPR
13270 && code != BIND_EXPR
13271 && code != CATCH_EXPR
13272 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
13273 && code != EH_FILTER_EXPR
13274 && code != GOTO_EXPR
13275 && code != LABEL_EXPR
13276 && code != LOOP_EXPR
13277 && code != SWITCH_EXPR
13278 && code != TRY_FINALLY_EXPR
13279 && code != OACC_PARALLEL
13280 && code != OACC_KERNELS
13281 && code != OACC_DATA
13282 && code != OACC_HOST_DATA
13283 && code != OACC_DECLARE
13284 && code != OACC_UPDATE
13285 && code != OACC_ENTER_DATA
13286 && code != OACC_EXIT_DATA
13287 && code != OACC_CACHE
13288 && code != OMP_CRITICAL
13289 && code != OMP_FOR
13290 && code != OACC_LOOP
13291 && code != OMP_MASTER
13292 && code != OMP_TASKGROUP
13293 && code != OMP_ORDERED
13294 && code != OMP_PARALLEL
13295 && code != OMP_SECTIONS
13296 && code != OMP_SECTION
13297 && code != OMP_SINGLE);
13299 #endif
13301 /* Otherwise we're gimplifying a subexpression, so the resulting
13302 value is interesting. If it's a valid operand that matches
13303 GIMPLE_TEST_F, we're done. Unless we are handling some
13304 post-effects internally; if that's the case, we need to copy into
13305 a temporary before adding the post-effects to POST_P. */
13306 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
13307 goto out;
13309 /* Otherwise, we need to create a new temporary for the gimplified
13310 expression. */
13312 /* We can't return an lvalue if we have an internal postqueue. The
13313 object the lvalue refers to would (probably) be modified by the
13314 postqueue; we need to copy the value out first, which means an
13315 rvalue. */
13316 if ((fallback & fb_lvalue)
13317 && gimple_seq_empty_p (internal_post)
13318 && is_gimple_addressable (*expr_p))
13320 /* An lvalue will do. Take the address of the expression, store it
13321 in a temporary, and replace the expression with an INDIRECT_REF of
13322 that temporary. */
13323 tree ref_alias_type = reference_alias_ptr_type (*expr_p);
13324 unsigned int ref_align = get_object_alignment (*expr_p);
13325 tree ref_type = TREE_TYPE (*expr_p);
13326 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
13327 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
13328 if (TYPE_ALIGN (ref_type) != ref_align)
13329 ref_type = build_aligned_type (ref_type, ref_align);
13330 *expr_p = build2 (MEM_REF, ref_type,
13331 tmp, build_zero_cst (ref_alias_type));
13333 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
13335 /* An rvalue will do. Assign the gimplified expression into a
13336 new temporary TMP and replace the original expression with
13337 TMP. First, make sure that the expression has a type so that
13338 it can be assigned into a temporary. */
13339 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
13340 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
13342 else
13344 #ifdef ENABLE_GIMPLE_CHECKING
13345 if (!(fallback & fb_mayfail))
13347 fprintf (stderr, "gimplification failed:\n");
13348 print_generic_expr (stderr, *expr_p);
13349 debug_tree (*expr_p);
13350 internal_error ("gimplification failed");
13352 #endif
13353 gcc_assert (fallback & fb_mayfail);
13355 /* If this is an asm statement, and the user asked for the
13356 impossible, don't die. Fail and let gimplify_asm_expr
13357 issue an error. */
13358 ret = GS_ERROR;
13359 goto out;
13362 /* Make sure the temporary matches our predicate. */
13363 gcc_assert ((*gimple_test_f) (*expr_p));
13365 if (!gimple_seq_empty_p (internal_post))
13367 annotate_all_with_location (internal_post, input_location);
13368 gimplify_seq_add_seq (pre_p, internal_post);
13371 out:
13372 input_location = saved_location;
13373 return ret;
13376 /* Like gimplify_expr but make sure the gimplified result is not itself
13377 a SSA name (but a decl if it were). Temporaries required by
13378 evaluating *EXPR_P may be still SSA names. */
13380 static enum gimplify_status
13381 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
13382 bool (*gimple_test_f) (tree), fallback_t fallback,
13383 bool allow_ssa)
13385 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
13386 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
13387 gimple_test_f, fallback);
13388 if (! allow_ssa
13389 && TREE_CODE (*expr_p) == SSA_NAME)
13391 tree name = *expr_p;
13392 if (was_ssa_name_p)
13393 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
13394 else
13396 /* Avoid the extra copy if possible. */
13397 *expr_p = create_tmp_reg (TREE_TYPE (name));
13398 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
13399 release_ssa_name (name);
13402 return ret;
13405 /* Look through TYPE for variable-sized objects and gimplify each such
13406 size that we find. Add to LIST_P any statements generated. */
13408 void
13409 gimplify_type_sizes (tree type, gimple_seq *list_p)
13411 tree field, t;
13413 if (type == NULL || type == error_mark_node)
13414 return;
13416 /* We first do the main variant, then copy into any other variants. */
13417 type = TYPE_MAIN_VARIANT (type);
13419 /* Avoid infinite recursion. */
13420 if (TYPE_SIZES_GIMPLIFIED (type))
13421 return;
13423 TYPE_SIZES_GIMPLIFIED (type) = 1;
13425 switch (TREE_CODE (type))
13427 case INTEGER_TYPE:
13428 case ENUMERAL_TYPE:
13429 case BOOLEAN_TYPE:
13430 case REAL_TYPE:
13431 case FIXED_POINT_TYPE:
13432 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
13433 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
13435 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13437 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
13438 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
13440 break;
13442 case ARRAY_TYPE:
13443 /* These types may not have declarations, so handle them here. */
13444 gimplify_type_sizes (TREE_TYPE (type), list_p);
13445 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
13446 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
13447 with assigned stack slots, for -O1+ -g they should be tracked
13448 by VTA. */
13449 if (!(TYPE_NAME (type)
13450 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13451 && DECL_IGNORED_P (TYPE_NAME (type)))
13452 && TYPE_DOMAIN (type)
13453 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
13455 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
13456 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
13457 DECL_IGNORED_P (t) = 0;
13458 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13459 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
13460 DECL_IGNORED_P (t) = 0;
13462 break;
13464 case RECORD_TYPE:
13465 case UNION_TYPE:
13466 case QUAL_UNION_TYPE:
13467 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
13468 if (TREE_CODE (field) == FIELD_DECL)
13470 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
13471 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
13472 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
13473 gimplify_type_sizes (TREE_TYPE (field), list_p);
13475 break;
13477 case POINTER_TYPE:
13478 case REFERENCE_TYPE:
13479 /* We used to recurse on the pointed-to type here, which turned out to
13480 be incorrect because its definition might refer to variables not
13481 yet initialized at this point if a forward declaration is involved.
13483 It was actually useful for anonymous pointed-to types to ensure
13484 that the sizes evaluation dominates every possible later use of the
13485 values. Restricting to such types here would be safe since there
13486 is no possible forward declaration around, but would introduce an
13487 undesirable middle-end semantic to anonymity. We then defer to
13488 front-ends the responsibility of ensuring that the sizes are
13489 evaluated both early and late enough, e.g. by attaching artificial
13490 type declarations to the tree. */
13491 break;
13493 default:
13494 break;
13497 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
13498 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
13500 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13502 TYPE_SIZE (t) = TYPE_SIZE (type);
13503 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
13504 TYPE_SIZES_GIMPLIFIED (t) = 1;
13508 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
13509 a size or position, has had all of its SAVE_EXPRs evaluated.
13510 We add any required statements to *STMT_P. */
13512 void
13513 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
13515 tree expr = *expr_p;
13517 /* We don't do anything if the value isn't there, is constant, or contains
13518 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
13519 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
13520 will want to replace it with a new variable, but that will cause problems
13521 if this type is from outside the function. It's OK to have that here. */
13522 if (expr == NULL_TREE
13523 || is_gimple_constant (expr)
13524 || TREE_CODE (expr) == VAR_DECL
13525 || CONTAINS_PLACEHOLDER_P (expr))
13526 return;
13528 *expr_p = unshare_expr (expr);
13530 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
13531 if the def vanishes. */
13532 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
13534 /* If expr wasn't already is_gimple_sizepos or is_gimple_constant from the
13535 FE, ensure that it is a VAR_DECL, otherwise we might handle some decls
13536 as gimplify_vla_decl even when they would have all sizes INTEGER_CSTs. */
13537 if (is_gimple_constant (*expr_p))
13538 *expr_p = get_initialized_tmp_var (*expr_p, stmt_p, NULL, false);
13541 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
13542 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
13543 is true, also gimplify the parameters. */
13545 gbind *
13546 gimplify_body (tree fndecl, bool do_parms)
13548 location_t saved_location = input_location;
13549 gimple_seq parm_stmts, parm_cleanup = NULL, seq;
13550 gimple *outer_stmt;
13551 gbind *outer_bind;
13553 timevar_push (TV_TREE_GIMPLIFY);
13555 init_tree_ssa (cfun);
13557 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
13558 gimplification. */
13559 default_rtl_profile ();
13561 gcc_assert (gimplify_ctxp == NULL);
13562 push_gimplify_context (true);
13564 if (flag_openacc || flag_openmp)
13566 gcc_assert (gimplify_omp_ctxp == NULL);
13567 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
13568 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
13571 /* Unshare most shared trees in the body and in that of any nested functions.
13572 It would seem we don't have to do this for nested functions because
13573 they are supposed to be output and then the outer function gimplified
13574 first, but the g++ front end doesn't always do it that way. */
13575 unshare_body (fndecl);
13576 unvisit_body (fndecl);
13578 /* Make sure input_location isn't set to something weird. */
13579 input_location = DECL_SOURCE_LOCATION (fndecl);
13581 /* Resolve callee-copies. This has to be done before processing
13582 the body so that DECL_VALUE_EXPR gets processed correctly. */
13583 parm_stmts = do_parms ? gimplify_parameters (&parm_cleanup) : NULL;
13585 /* Gimplify the function's body. */
13586 seq = NULL;
13587 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
13588 outer_stmt = gimple_seq_first_stmt (seq);
13589 if (!outer_stmt)
13591 outer_stmt = gimple_build_nop ();
13592 gimplify_seq_add_stmt (&seq, outer_stmt);
13595 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
13596 not the case, wrap everything in a GIMPLE_BIND to make it so. */
13597 if (gimple_code (outer_stmt) == GIMPLE_BIND
13598 && gimple_seq_first (seq) == gimple_seq_last (seq))
13599 outer_bind = as_a <gbind *> (outer_stmt);
13600 else
13601 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
13603 DECL_SAVED_TREE (fndecl) = NULL_TREE;
13605 /* If we had callee-copies statements, insert them at the beginning
13606 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
13607 if (!gimple_seq_empty_p (parm_stmts))
13609 tree parm;
13611 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
13612 if (parm_cleanup)
13614 gtry *g = gimple_build_try (parm_stmts, parm_cleanup,
13615 GIMPLE_TRY_FINALLY);
13616 parm_stmts = NULL;
13617 gimple_seq_add_stmt (&parm_stmts, g);
13619 gimple_bind_set_body (outer_bind, parm_stmts);
13621 for (parm = DECL_ARGUMENTS (current_function_decl);
13622 parm; parm = DECL_CHAIN (parm))
13623 if (DECL_HAS_VALUE_EXPR_P (parm))
13625 DECL_HAS_VALUE_EXPR_P (parm) = 0;
13626 DECL_IGNORED_P (parm) = 0;
13630 if ((flag_openacc || flag_openmp || flag_openmp_simd)
13631 && gimplify_omp_ctxp)
13633 delete_omp_context (gimplify_omp_ctxp);
13634 gimplify_omp_ctxp = NULL;
13637 pop_gimplify_context (outer_bind);
13638 gcc_assert (gimplify_ctxp == NULL);
13640 if (flag_checking && !seen_error ())
13641 verify_gimple_in_seq (gimple_bind_body (outer_bind));
13643 timevar_pop (TV_TREE_GIMPLIFY);
13644 input_location = saved_location;
13646 return outer_bind;
13649 typedef char *char_p; /* For DEF_VEC_P. */
13651 /* Return whether we should exclude FNDECL from instrumentation. */
13653 static bool
13654 flag_instrument_functions_exclude_p (tree fndecl)
13656 vec<char_p> *v;
13658 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
13659 if (v && v->length () > 0)
13661 const char *name;
13662 int i;
13663 char *s;
13665 name = lang_hooks.decl_printable_name (fndecl, 0);
13666 FOR_EACH_VEC_ELT (*v, i, s)
13667 if (strstr (name, s) != NULL)
13668 return true;
13671 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
13672 if (v && v->length () > 0)
13674 const char *name;
13675 int i;
13676 char *s;
13678 name = DECL_SOURCE_FILE (fndecl);
13679 FOR_EACH_VEC_ELT (*v, i, s)
13680 if (strstr (name, s) != NULL)
13681 return true;
13684 return false;
13687 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
13688 node for the function we want to gimplify.
13690 Return the sequence of GIMPLE statements corresponding to the body
13691 of FNDECL. */
13693 void
13694 gimplify_function_tree (tree fndecl)
13696 tree parm, ret;
13697 gimple_seq seq;
13698 gbind *bind;
13700 gcc_assert (!gimple_body (fndecl));
13702 if (DECL_STRUCT_FUNCTION (fndecl))
13703 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
13704 else
13705 push_struct_function (fndecl);
13707 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
13708 if necessary. */
13709 cfun->curr_properties |= PROP_gimple_lva;
13711 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
13713 /* Preliminarily mark non-addressed complex variables as eligible
13714 for promotion to gimple registers. We'll transform their uses
13715 as we find them. */
13716 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
13717 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
13718 && !TREE_THIS_VOLATILE (parm)
13719 && !needs_to_live_in_memory (parm))
13720 DECL_GIMPLE_REG_P (parm) = 1;
13723 ret = DECL_RESULT (fndecl);
13724 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
13725 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
13726 && !needs_to_live_in_memory (ret))
13727 DECL_GIMPLE_REG_P (ret) = 1;
13729 if (asan_sanitize_use_after_scope () && sanitize_flags_p (SANITIZE_ADDRESS))
13730 asan_poisoned_variables = new hash_set<tree> ();
13731 bind = gimplify_body (fndecl, true);
13732 if (asan_poisoned_variables)
13734 delete asan_poisoned_variables;
13735 asan_poisoned_variables = NULL;
13738 /* The tree body of the function is no longer needed, replace it
13739 with the new GIMPLE body. */
13740 seq = NULL;
13741 gimple_seq_add_stmt (&seq, bind);
13742 gimple_set_body (fndecl, seq);
13744 /* If we're instrumenting function entry/exit, then prepend the call to
13745 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
13746 catch the exit hook. */
13747 /* ??? Add some way to ignore exceptions for this TFE. */
13748 if (flag_instrument_function_entry_exit
13749 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
13750 /* Do not instrument extern inline functions. */
13751 && !(DECL_DECLARED_INLINE_P (fndecl)
13752 && DECL_EXTERNAL (fndecl)
13753 && DECL_DISREGARD_INLINE_LIMITS (fndecl))
13754 && !flag_instrument_functions_exclude_p (fndecl))
13756 tree x;
13757 gbind *new_bind;
13758 gimple *tf;
13759 gimple_seq cleanup = NULL, body = NULL;
13760 tree tmp_var, this_fn_addr;
13761 gcall *call;
13763 /* The instrumentation hooks aren't going to call the instrumented
13764 function and the address they receive is expected to be matchable
13765 against symbol addresses. Make sure we don't create a trampoline,
13766 in case the current function is nested. */
13767 this_fn_addr = build_fold_addr_expr (current_function_decl);
13768 TREE_NO_TRAMPOLINE (this_fn_addr) = 1;
13770 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
13771 call = gimple_build_call (x, 1, integer_zero_node);
13772 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
13773 gimple_call_set_lhs (call, tmp_var);
13774 gimplify_seq_add_stmt (&cleanup, call);
13775 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
13776 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
13777 gimplify_seq_add_stmt (&cleanup, call);
13778 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
13780 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
13781 call = gimple_build_call (x, 1, integer_zero_node);
13782 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
13783 gimple_call_set_lhs (call, tmp_var);
13784 gimplify_seq_add_stmt (&body, call);
13785 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
13786 call = gimple_build_call (x, 2, this_fn_addr, tmp_var);
13787 gimplify_seq_add_stmt (&body, call);
13788 gimplify_seq_add_stmt (&body, tf);
13789 new_bind = gimple_build_bind (NULL, body, NULL);
13791 /* Replace the current function body with the body
13792 wrapped in the try/finally TF. */
13793 seq = NULL;
13794 gimple_seq_add_stmt (&seq, new_bind);
13795 gimple_set_body (fndecl, seq);
13796 bind = new_bind;
13799 if (sanitize_flags_p (SANITIZE_THREAD))
13801 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
13802 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
13803 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
13804 /* Replace the current function body with the body
13805 wrapped in the try/finally TF. */
13806 seq = NULL;
13807 gimple_seq_add_stmt (&seq, new_bind);
13808 gimple_set_body (fndecl, seq);
13811 DECL_SAVED_TREE (fndecl) = NULL_TREE;
13812 cfun->curr_properties |= PROP_gimple_any;
13814 pop_cfun ();
13816 dump_function (TDI_gimple, fndecl);
13819 /* Return a dummy expression of type TYPE in order to keep going after an
13820 error. */
13822 static tree
13823 dummy_object (tree type)
13825 tree t = build_int_cst (build_pointer_type (type), 0);
13826 return build2 (MEM_REF, type, t, t);
13829 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
13830 builtin function, but a very special sort of operator. */
13832 enum gimplify_status
13833 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
13834 gimple_seq *post_p ATTRIBUTE_UNUSED)
13836 tree promoted_type, have_va_type;
13837 tree valist = TREE_OPERAND (*expr_p, 0);
13838 tree type = TREE_TYPE (*expr_p);
13839 tree t, tag, aptag;
13840 location_t loc = EXPR_LOCATION (*expr_p);
13842 /* Verify that valist is of the proper type. */
13843 have_va_type = TREE_TYPE (valist);
13844 if (have_va_type == error_mark_node)
13845 return GS_ERROR;
13846 have_va_type = targetm.canonical_va_list_type (have_va_type);
13847 if (have_va_type == NULL_TREE
13848 && POINTER_TYPE_P (TREE_TYPE (valist)))
13849 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
13850 have_va_type
13851 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
13852 gcc_assert (have_va_type != NULL_TREE);
13854 /* Generate a diagnostic for requesting data of a type that cannot
13855 be passed through `...' due to type promotion at the call site. */
13856 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
13857 != type)
13859 static bool gave_help;
13860 bool warned;
13861 /* Use the expansion point to handle cases such as passing bool (defined
13862 in a system header) through `...'. */
13863 location_t xloc
13864 = expansion_point_location_if_in_system_header (loc);
13866 /* Unfortunately, this is merely undefined, rather than a constraint
13867 violation, so we cannot make this an error. If this call is never
13868 executed, the program is still strictly conforming. */
13869 auto_diagnostic_group d;
13870 warned = warning_at (xloc, 0,
13871 "%qT is promoted to %qT when passed through %<...%>",
13872 type, promoted_type);
13873 if (!gave_help && warned)
13875 gave_help = true;
13876 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
13877 promoted_type, type);
13880 /* We can, however, treat "undefined" any way we please.
13881 Call abort to encourage the user to fix the program. */
13882 if (warned)
13883 inform (xloc, "if this code is reached, the program will abort");
13884 /* Before the abort, allow the evaluation of the va_list
13885 expression to exit or longjmp. */
13886 gimplify_and_add (valist, pre_p);
13887 t = build_call_expr_loc (loc,
13888 builtin_decl_implicit (BUILT_IN_TRAP), 0);
13889 gimplify_and_add (t, pre_p);
13891 /* This is dead code, but go ahead and finish so that the
13892 mode of the result comes out right. */
13893 *expr_p = dummy_object (type);
13894 return GS_ALL_DONE;
13897 tag = build_int_cst (build_pointer_type (type), 0);
13898 aptag = build_int_cst (TREE_TYPE (valist), 0);
13900 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
13901 valist, tag, aptag);
13903 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
13904 needs to be expanded. */
13905 cfun->curr_properties &= ~PROP_gimple_lva;
13907 return GS_OK;
13910 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
13912 DST/SRC are the destination and source respectively. You can pass
13913 ungimplified trees in DST or SRC, in which case they will be
13914 converted to a gimple operand if necessary.
13916 This function returns the newly created GIMPLE_ASSIGN tuple. */
13918 gimple *
13919 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
13921 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13922 gimplify_and_add (t, seq_p);
13923 ggc_free (t);
13924 return gimple_seq_last_stmt (*seq_p);
13927 inline hashval_t
13928 gimplify_hasher::hash (const elt_t *p)
13930 tree t = p->val;
13931 return iterative_hash_expr (t, 0);
13934 inline bool
13935 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
13937 tree t1 = p1->val;
13938 tree t2 = p2->val;
13939 enum tree_code code = TREE_CODE (t1);
13941 if (TREE_CODE (t2) != code
13942 || TREE_TYPE (t1) != TREE_TYPE (t2))
13943 return false;
13945 if (!operand_equal_p (t1, t2, 0))
13946 return false;
13948 /* Only allow them to compare equal if they also hash equal; otherwise
13949 results are nondeterminate, and we fail bootstrap comparison. */
13950 gcc_checking_assert (hash (p1) == hash (p2));
13952 return true;