* lib/scanasm.exp (hidden-scan-for): Add XCOFF support.
[official-gcc.git] / gcc / gimplify.c
blobe5930e6136847c0e692b3fb8d08d273c3869697a
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2016 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 "gimple.h"
31 #include "gimple-predict.h"
32 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
33 #include "ssa.h"
34 #include "cgraph.h"
35 #include "tree-pretty-print.h"
36 #include "diagnostic-core.h"
37 #include "alias.h"
38 #include "fold-const.h"
39 #include "calls.h"
40 #include "varasm.h"
41 #include "stmt.h"
42 #include "expr.h"
43 #include "gimple-fold.h"
44 #include "tree-eh.h"
45 #include "gimplify.h"
46 #include "gimple-iterator.h"
47 #include "stor-layout.h"
48 #include "print-tree.h"
49 #include "tree-iterator.h"
50 #include "tree-inline.h"
51 #include "langhooks.h"
52 #include "tree-cfg.h"
53 #include "tree-ssa.h"
54 #include "omp-low.h"
55 #include "gimple-low.h"
56 #include "cilk.h"
57 #include "gomp-constants.h"
58 #include "tree-dump.h"
59 #include "gimple-walk.h"
60 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
61 #include "builtins.h"
62 #include "asan.h"
64 /* Hash set of poisoned variables in a bind expr. */
65 static hash_set<tree> *asan_poisoned_variables = NULL;
67 enum gimplify_omp_var_data
69 GOVD_SEEN = 1,
70 GOVD_EXPLICIT = 2,
71 GOVD_SHARED = 4,
72 GOVD_PRIVATE = 8,
73 GOVD_FIRSTPRIVATE = 16,
74 GOVD_LASTPRIVATE = 32,
75 GOVD_REDUCTION = 64,
76 GOVD_LOCAL = 128,
77 GOVD_MAP = 256,
78 GOVD_DEBUG_PRIVATE = 512,
79 GOVD_PRIVATE_OUTER_REF = 1024,
80 GOVD_LINEAR = 2048,
81 GOVD_ALIGNED = 4096,
83 /* Flag for GOVD_MAP: don't copy back. */
84 GOVD_MAP_TO_ONLY = 8192,
86 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
87 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
89 GOVD_MAP_0LEN_ARRAY = 32768,
91 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
92 GOVD_MAP_ALWAYS_TO = 65536,
94 /* Flag for shared vars that are or might be stored to in the region. */
95 GOVD_WRITTEN = 131072,
97 /* Flag for GOVD_MAP, if it is a forced mapping. */
98 GOVD_MAP_FORCE = 262144,
100 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
101 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
102 | GOVD_LOCAL)
106 enum omp_region_type
108 ORT_WORKSHARE = 0x00,
109 ORT_SIMD = 0x01,
111 ORT_PARALLEL = 0x02,
112 ORT_COMBINED_PARALLEL = 0x03,
114 ORT_TASK = 0x04,
115 ORT_UNTIED_TASK = 0x05,
117 ORT_TEAMS = 0x08,
118 ORT_COMBINED_TEAMS = 0x09,
120 /* Data region. */
121 ORT_TARGET_DATA = 0x10,
123 /* Data region with offloading. */
124 ORT_TARGET = 0x20,
125 ORT_COMBINED_TARGET = 0x21,
127 /* OpenACC variants. */
128 ORT_ACC = 0x40, /* A generic OpenACC region. */
129 ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
130 ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
131 ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 0x80, /* Kernels construct. */
132 ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 0x80, /* Host data. */
134 /* Dummy OpenMP region, used to disable expansion of
135 DECL_VALUE_EXPRs in taskloop pre body. */
136 ORT_NONE = 0x100
139 /* Gimplify hashtable helper. */
141 struct gimplify_hasher : free_ptr_hash <elt_t>
143 static inline hashval_t hash (const elt_t *);
144 static inline bool equal (const elt_t *, const elt_t *);
147 struct gimplify_ctx
149 struct gimplify_ctx *prev_context;
151 vec<gbind *> bind_expr_stack;
152 tree temps;
153 gimple_seq conditional_cleanups;
154 tree exit_label;
155 tree return_temp;
157 vec<tree> case_labels;
158 hash_set<tree> *live_switch_vars;
159 /* The formal temporary table. Should this be persistent? */
160 hash_table<gimplify_hasher> *temp_htab;
162 int conditions;
163 unsigned into_ssa : 1;
164 unsigned allow_rhs_cond_expr : 1;
165 unsigned in_cleanup_point_expr : 1;
166 unsigned keep_stack : 1;
167 unsigned save_stack : 1;
168 unsigned in_switch_expr : 1;
171 struct gimplify_omp_ctx
173 struct gimplify_omp_ctx *outer_context;
174 splay_tree variables;
175 hash_set<tree> *privatized_types;
176 /* Iteration variables in an OMP_FOR. */
177 vec<tree> loop_iter_var;
178 location_t location;
179 enum omp_clause_default_kind default_kind;
180 enum omp_region_type region_type;
181 bool combined_loop;
182 bool distribute;
183 bool target_map_scalars_firstprivate;
184 bool target_map_pointers_as_0len_arrays;
185 bool target_firstprivatize_array_bases;
188 static struct gimplify_ctx *gimplify_ctxp;
189 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
191 /* Forward declaration. */
192 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
193 static hash_map<tree, tree> *oacc_declare_returns;
194 static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
195 bool (*) (tree), fallback_t, bool);
197 /* Shorter alias name for the above function for use in gimplify.c
198 only. */
200 static inline void
201 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
203 gimple_seq_add_stmt_without_update (seq_p, gs);
206 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
207 NULL, a new sequence is allocated. This function is
208 similar to gimple_seq_add_seq, but does not scan the operands.
209 During gimplification, we need to manipulate statement sequences
210 before the def/use vectors have been constructed. */
212 static void
213 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
215 gimple_stmt_iterator si;
217 if (src == NULL)
218 return;
220 si = gsi_last (*dst_p);
221 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
225 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
226 and popping gimplify contexts. */
228 static struct gimplify_ctx *ctx_pool = NULL;
230 /* Return a gimplify context struct from the pool. */
232 static inline struct gimplify_ctx *
233 ctx_alloc (void)
235 struct gimplify_ctx * c = ctx_pool;
237 if (c)
238 ctx_pool = c->prev_context;
239 else
240 c = XNEW (struct gimplify_ctx);
242 memset (c, '\0', sizeof (*c));
243 return c;
246 /* Put gimplify context C back into the pool. */
248 static inline void
249 ctx_free (struct gimplify_ctx *c)
251 c->prev_context = ctx_pool;
252 ctx_pool = c;
255 /* Free allocated ctx stack memory. */
257 void
258 free_gimplify_stack (void)
260 struct gimplify_ctx *c;
262 while ((c = ctx_pool))
264 ctx_pool = c->prev_context;
265 free (c);
270 /* Set up a context for the gimplifier. */
272 void
273 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
275 struct gimplify_ctx *c = ctx_alloc ();
277 c->prev_context = gimplify_ctxp;
278 gimplify_ctxp = c;
279 gimplify_ctxp->into_ssa = in_ssa;
280 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
283 /* Tear down a context for the gimplifier. If BODY is non-null, then
284 put the temporaries into the outer BIND_EXPR. Otherwise, put them
285 in the local_decls.
287 BODY is not a sequence, but the first tuple in a sequence. */
289 void
290 pop_gimplify_context (gimple *body)
292 struct gimplify_ctx *c = gimplify_ctxp;
294 gcc_assert (c
295 && (!c->bind_expr_stack.exists ()
296 || c->bind_expr_stack.is_empty ()));
297 c->bind_expr_stack.release ();
298 gimplify_ctxp = c->prev_context;
300 if (body)
301 declare_vars (c->temps, body, false);
302 else
303 record_vars (c->temps);
305 delete c->temp_htab;
306 c->temp_htab = NULL;
307 ctx_free (c);
310 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
312 static void
313 gimple_push_bind_expr (gbind *bind_stmt)
315 gimplify_ctxp->bind_expr_stack.reserve (8);
316 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
319 /* Pop the first element off the stack of bindings. */
321 static void
322 gimple_pop_bind_expr (void)
324 gimplify_ctxp->bind_expr_stack.pop ();
327 /* Return the first element of the stack of bindings. */
329 gbind *
330 gimple_current_bind_expr (void)
332 return gimplify_ctxp->bind_expr_stack.last ();
335 /* Return the stack of bindings created during gimplification. */
337 vec<gbind *>
338 gimple_bind_expr_stack (void)
340 return gimplify_ctxp->bind_expr_stack;
343 /* Return true iff there is a COND_EXPR between us and the innermost
344 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
346 static bool
347 gimple_conditional_context (void)
349 return gimplify_ctxp->conditions > 0;
352 /* Note that we've entered a COND_EXPR. */
354 static void
355 gimple_push_condition (void)
357 #ifdef ENABLE_GIMPLE_CHECKING
358 if (gimplify_ctxp->conditions == 0)
359 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
360 #endif
361 ++(gimplify_ctxp->conditions);
364 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
365 now, add any conditional cleanups we've seen to the prequeue. */
367 static void
368 gimple_pop_condition (gimple_seq *pre_p)
370 int conds = --(gimplify_ctxp->conditions);
372 gcc_assert (conds >= 0);
373 if (conds == 0)
375 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
376 gimplify_ctxp->conditional_cleanups = NULL;
380 /* A stable comparison routine for use with splay trees and DECLs. */
382 static int
383 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
385 tree a = (tree) xa;
386 tree b = (tree) xb;
388 return DECL_UID (a) - DECL_UID (b);
391 /* Create a new omp construct that deals with variable remapping. */
393 static struct gimplify_omp_ctx *
394 new_omp_context (enum omp_region_type region_type)
396 struct gimplify_omp_ctx *c;
398 c = XCNEW (struct gimplify_omp_ctx);
399 c->outer_context = gimplify_omp_ctxp;
400 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
401 c->privatized_types = new hash_set<tree>;
402 c->location = input_location;
403 c->region_type = region_type;
404 if ((region_type & ORT_TASK) == 0)
405 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
406 else
407 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
409 return c;
412 /* Destroy an omp construct that deals with variable remapping. */
414 static void
415 delete_omp_context (struct gimplify_omp_ctx *c)
417 splay_tree_delete (c->variables);
418 delete c->privatized_types;
419 c->loop_iter_var.release ();
420 XDELETE (c);
423 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
424 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
426 /* Both gimplify the statement T and append it to *SEQ_P. This function
427 behaves exactly as gimplify_stmt, but you don't have to pass T as a
428 reference. */
430 void
431 gimplify_and_add (tree t, gimple_seq *seq_p)
433 gimplify_stmt (&t, seq_p);
436 /* Gimplify statement T into sequence *SEQ_P, and return the first
437 tuple in the sequence of generated tuples for this statement.
438 Return NULL if gimplifying T produced no tuples. */
440 static gimple *
441 gimplify_and_return_first (tree t, gimple_seq *seq_p)
443 gimple_stmt_iterator last = gsi_last (*seq_p);
445 gimplify_and_add (t, seq_p);
447 if (!gsi_end_p (last))
449 gsi_next (&last);
450 return gsi_stmt (last);
452 else
453 return gimple_seq_first_stmt (*seq_p);
456 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
457 LHS, or for a call argument. */
459 static bool
460 is_gimple_mem_rhs (tree t)
462 /* If we're dealing with a renamable type, either source or dest must be
463 a renamed variable. */
464 if (is_gimple_reg_type (TREE_TYPE (t)))
465 return is_gimple_val (t);
466 else
467 return is_gimple_val (t) || is_gimple_lvalue (t);
470 /* Return true if T is a CALL_EXPR or an expression that can be
471 assigned to a temporary. Note that this predicate should only be
472 used during gimplification. See the rationale for this in
473 gimplify_modify_expr. */
475 static bool
476 is_gimple_reg_rhs_or_call (tree t)
478 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
479 || TREE_CODE (t) == CALL_EXPR);
482 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
483 this predicate should only be used during gimplification. See the
484 rationale for this in gimplify_modify_expr. */
486 static bool
487 is_gimple_mem_rhs_or_call (tree t)
489 /* If we're dealing with a renamable type, either source or dest must be
490 a renamed variable. */
491 if (is_gimple_reg_type (TREE_TYPE (t)))
492 return is_gimple_val (t);
493 else
494 return (is_gimple_val (t) || is_gimple_lvalue (t)
495 || TREE_CODE (t) == CALL_EXPR);
498 /* Create a temporary with a name derived from VAL. Subroutine of
499 lookup_tmp_var; nobody else should call this function. */
501 static inline tree
502 create_tmp_from_val (tree val)
504 /* Drop all qualifiers and address-space information from the value type. */
505 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
506 tree var = create_tmp_var (type, get_name (val));
507 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
508 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
509 DECL_GIMPLE_REG_P (var) = 1;
510 return var;
513 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
514 an existing expression temporary. */
516 static tree
517 lookup_tmp_var (tree val, bool is_formal)
519 tree ret;
521 /* If not optimizing, never really reuse a temporary. local-alloc
522 won't allocate any variable that is used in more than one basic
523 block, which means it will go into memory, causing much extra
524 work in reload and final and poorer code generation, outweighing
525 the extra memory allocation here. */
526 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
527 ret = create_tmp_from_val (val);
528 else
530 elt_t elt, *elt_p;
531 elt_t **slot;
533 elt.val = val;
534 if (!gimplify_ctxp->temp_htab)
535 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
536 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
537 if (*slot == NULL)
539 elt_p = XNEW (elt_t);
540 elt_p->val = val;
541 elt_p->temp = ret = create_tmp_from_val (val);
542 *slot = elt_p;
544 else
546 elt_p = *slot;
547 ret = elt_p->temp;
551 return ret;
554 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
556 static tree
557 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
558 bool is_formal, bool allow_ssa)
560 tree t, mod;
562 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
563 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
564 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
565 fb_rvalue);
567 if (allow_ssa
568 && gimplify_ctxp->into_ssa
569 && is_gimple_reg_type (TREE_TYPE (val)))
571 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
572 if (! gimple_in_ssa_p (cfun))
574 const char *name = get_name (val);
575 if (name)
576 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
579 else
580 t = lookup_tmp_var (val, is_formal);
582 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
584 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
586 /* gimplify_modify_expr might want to reduce this further. */
587 gimplify_and_add (mod, pre_p);
588 ggc_free (mod);
590 return t;
593 /* Return a formal temporary variable initialized with VAL. PRE_P is as
594 in gimplify_expr. Only use this function if:
596 1) The value of the unfactored expression represented by VAL will not
597 change between the initialization and use of the temporary, and
598 2) The temporary will not be otherwise modified.
600 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
601 and #2 means it is inappropriate for && temps.
603 For other cases, use get_initialized_tmp_var instead. */
605 tree
606 get_formal_tmp_var (tree val, gimple_seq *pre_p)
608 return internal_get_tmp_var (val, pre_p, NULL, true, true);
611 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
612 are as in gimplify_expr. */
614 tree
615 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
616 bool allow_ssa)
618 return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa);
621 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
622 generate debug info for them; otherwise don't. */
624 void
625 declare_vars (tree vars, gimple *gs, bool debug_info)
627 tree last = vars;
628 if (last)
630 tree temps, block;
632 gbind *scope = as_a <gbind *> (gs);
634 temps = nreverse (last);
636 block = gimple_bind_block (scope);
637 gcc_assert (!block || TREE_CODE (block) == BLOCK);
638 if (!block || !debug_info)
640 DECL_CHAIN (last) = gimple_bind_vars (scope);
641 gimple_bind_set_vars (scope, temps);
643 else
645 /* We need to attach the nodes both to the BIND_EXPR and to its
646 associated BLOCK for debugging purposes. The key point here
647 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
648 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
649 if (BLOCK_VARS (block))
650 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
651 else
653 gimple_bind_set_vars (scope,
654 chainon (gimple_bind_vars (scope), temps));
655 BLOCK_VARS (block) = temps;
661 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
662 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
663 no such upper bound can be obtained. */
665 static void
666 force_constant_size (tree var)
668 /* The only attempt we make is by querying the maximum size of objects
669 of the variable's type. */
671 HOST_WIDE_INT max_size;
673 gcc_assert (VAR_P (var));
675 max_size = max_int_size_in_bytes (TREE_TYPE (var));
677 gcc_assert (max_size >= 0);
679 DECL_SIZE_UNIT (var)
680 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
681 DECL_SIZE (var)
682 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
685 /* Push the temporary variable TMP into the current binding. */
687 void
688 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
690 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
692 /* Later processing assumes that the object size is constant, which might
693 not be true at this point. Force the use of a constant upper bound in
694 this case. */
695 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
696 force_constant_size (tmp);
698 DECL_CONTEXT (tmp) = fn->decl;
699 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
701 record_vars_into (tmp, fn->decl);
704 /* Push the temporary variable TMP into the current binding. */
706 void
707 gimple_add_tmp_var (tree tmp)
709 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
711 /* Later processing assumes that the object size is constant, which might
712 not be true at this point. Force the use of a constant upper bound in
713 this case. */
714 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
715 force_constant_size (tmp);
717 DECL_CONTEXT (tmp) = current_function_decl;
718 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
720 if (gimplify_ctxp)
722 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
723 gimplify_ctxp->temps = tmp;
725 /* Mark temporaries local within the nearest enclosing parallel. */
726 if (gimplify_omp_ctxp)
728 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
729 while (ctx
730 && (ctx->region_type == ORT_WORKSHARE
731 || ctx->region_type == ORT_SIMD
732 || ctx->region_type == ORT_ACC))
733 ctx = ctx->outer_context;
734 if (ctx)
735 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
738 else if (cfun)
739 record_vars (tmp);
740 else
742 gimple_seq body_seq;
744 /* This case is for nested functions. We need to expose the locals
745 they create. */
746 body_seq = gimple_body (current_function_decl);
747 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
753 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
754 nodes that are referenced more than once in GENERIC functions. This is
755 necessary because gimplification (translation into GIMPLE) is performed
756 by modifying tree nodes in-place, so gimplication of a shared node in a
757 first context could generate an invalid GIMPLE form in a second context.
759 This is achieved with a simple mark/copy/unmark algorithm that walks the
760 GENERIC representation top-down, marks nodes with TREE_VISITED the first
761 time it encounters them, duplicates them if they already have TREE_VISITED
762 set, and finally removes the TREE_VISITED marks it has set.
764 The algorithm works only at the function level, i.e. it generates a GENERIC
765 representation of a function with no nodes shared within the function when
766 passed a GENERIC function (except for nodes that are allowed to be shared).
768 At the global level, it is also necessary to unshare tree nodes that are
769 referenced in more than one function, for the same aforementioned reason.
770 This requires some cooperation from the front-end. There are 2 strategies:
772 1. Manual unsharing. The front-end needs to call unshare_expr on every
773 expression that might end up being shared across functions.
775 2. Deep unsharing. This is an extension of regular unsharing. Instead
776 of calling unshare_expr on expressions that might be shared across
777 functions, the front-end pre-marks them with TREE_VISITED. This will
778 ensure that they are unshared on the first reference within functions
779 when the regular unsharing algorithm runs. The counterpart is that
780 this algorithm must look deeper than for manual unsharing, which is
781 specified by LANG_HOOKS_DEEP_UNSHARING.
783 If there are only few specific cases of node sharing across functions, it is
784 probably easier for a front-end to unshare the expressions manually. On the
785 contrary, if the expressions generated at the global level are as widespread
786 as expressions generated within functions, deep unsharing is very likely the
787 way to go. */
789 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
790 These nodes model computations that must be done once. If we were to
791 unshare something like SAVE_EXPR(i++), the gimplification process would
792 create wrong code. However, if DATA is non-null, it must hold a pointer
793 set that is used to unshare the subtrees of these nodes. */
795 static tree
796 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
798 tree t = *tp;
799 enum tree_code code = TREE_CODE (t);
801 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
802 copy their subtrees if we can make sure to do it only once. */
803 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
805 if (data && !((hash_set<tree> *)data)->add (t))
807 else
808 *walk_subtrees = 0;
811 /* Stop at types, decls, constants like copy_tree_r. */
812 else if (TREE_CODE_CLASS (code) == tcc_type
813 || TREE_CODE_CLASS (code) == tcc_declaration
814 || TREE_CODE_CLASS (code) == tcc_constant
815 /* We can't do anything sensible with a BLOCK used as an
816 expression, but we also can't just die when we see it
817 because of non-expression uses. So we avert our eyes
818 and cross our fingers. Silly Java. */
819 || code == BLOCK)
820 *walk_subtrees = 0;
822 /* Cope with the statement expression extension. */
823 else if (code == STATEMENT_LIST)
826 /* Leave the bulk of the work to copy_tree_r itself. */
827 else
828 copy_tree_r (tp, walk_subtrees, NULL);
830 return NULL_TREE;
833 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
834 If *TP has been visited already, then *TP is deeply copied by calling
835 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
837 static tree
838 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
840 tree t = *tp;
841 enum tree_code code = TREE_CODE (t);
843 /* Skip types, decls, and constants. But we do want to look at their
844 types and the bounds of types. Mark them as visited so we properly
845 unmark their subtrees on the unmark pass. If we've already seen them,
846 don't look down further. */
847 if (TREE_CODE_CLASS (code) == tcc_type
848 || TREE_CODE_CLASS (code) == tcc_declaration
849 || TREE_CODE_CLASS (code) == tcc_constant)
851 if (TREE_VISITED (t))
852 *walk_subtrees = 0;
853 else
854 TREE_VISITED (t) = 1;
857 /* If this node has been visited already, unshare it and don't look
858 any deeper. */
859 else if (TREE_VISITED (t))
861 walk_tree (tp, mostly_copy_tree_r, data, NULL);
862 *walk_subtrees = 0;
865 /* Otherwise, mark the node as visited and keep looking. */
866 else
867 TREE_VISITED (t) = 1;
869 return NULL_TREE;
872 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
873 copy_if_shared_r callback unmodified. */
875 static inline void
876 copy_if_shared (tree *tp, void *data)
878 walk_tree (tp, copy_if_shared_r, data, NULL);
881 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
882 any nested functions. */
884 static void
885 unshare_body (tree fndecl)
887 struct cgraph_node *cgn = cgraph_node::get (fndecl);
888 /* If the language requires deep unsharing, we need a pointer set to make
889 sure we don't repeatedly unshare subtrees of unshareable nodes. */
890 hash_set<tree> *visited
891 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
893 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
894 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
895 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
897 delete visited;
899 if (cgn)
900 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
901 unshare_body (cgn->decl);
904 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
905 Subtrees are walked until the first unvisited node is encountered. */
907 static tree
908 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
910 tree t = *tp;
912 /* If this node has been visited, unmark it and keep looking. */
913 if (TREE_VISITED (t))
914 TREE_VISITED (t) = 0;
916 /* Otherwise, don't look any deeper. */
917 else
918 *walk_subtrees = 0;
920 return NULL_TREE;
923 /* Unmark the visited trees rooted at *TP. */
925 static inline void
926 unmark_visited (tree *tp)
928 walk_tree (tp, unmark_visited_r, NULL, NULL);
931 /* Likewise, but mark all trees as not visited. */
933 static void
934 unvisit_body (tree fndecl)
936 struct cgraph_node *cgn = cgraph_node::get (fndecl);
938 unmark_visited (&DECL_SAVED_TREE (fndecl));
939 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
940 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
942 if (cgn)
943 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
944 unvisit_body (cgn->decl);
947 /* Unconditionally make an unshared copy of EXPR. This is used when using
948 stored expressions which span multiple functions, such as BINFO_VTABLE,
949 as the normal unsharing process can't tell that they're shared. */
951 tree
952 unshare_expr (tree expr)
954 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
955 return expr;
958 /* Worker for unshare_expr_without_location. */
960 static tree
961 prune_expr_location (tree *tp, int *walk_subtrees, void *)
963 if (EXPR_P (*tp))
964 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
965 else
966 *walk_subtrees = 0;
967 return NULL_TREE;
970 /* Similar to unshare_expr but also prune all expression locations
971 from EXPR. */
973 tree
974 unshare_expr_without_location (tree expr)
976 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
977 if (EXPR_P (expr))
978 walk_tree (&expr, prune_expr_location, NULL, NULL);
979 return expr;
982 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
983 contain statements and have a value. Assign its value to a temporary
984 and give it void_type_node. Return the temporary, or NULL_TREE if
985 WRAPPER was already void. */
987 tree
988 voidify_wrapper_expr (tree wrapper, tree temp)
990 tree type = TREE_TYPE (wrapper);
991 if (type && !VOID_TYPE_P (type))
993 tree *p;
995 /* Set p to point to the body of the wrapper. Loop until we find
996 something that isn't a wrapper. */
997 for (p = &wrapper; p && *p; )
999 switch (TREE_CODE (*p))
1001 case BIND_EXPR:
1002 TREE_SIDE_EFFECTS (*p) = 1;
1003 TREE_TYPE (*p) = void_type_node;
1004 /* For a BIND_EXPR, the body is operand 1. */
1005 p = &BIND_EXPR_BODY (*p);
1006 break;
1008 case CLEANUP_POINT_EXPR:
1009 case TRY_FINALLY_EXPR:
1010 case TRY_CATCH_EXPR:
1011 TREE_SIDE_EFFECTS (*p) = 1;
1012 TREE_TYPE (*p) = void_type_node;
1013 p = &TREE_OPERAND (*p, 0);
1014 break;
1016 case STATEMENT_LIST:
1018 tree_stmt_iterator i = tsi_last (*p);
1019 TREE_SIDE_EFFECTS (*p) = 1;
1020 TREE_TYPE (*p) = void_type_node;
1021 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1023 break;
1025 case COMPOUND_EXPR:
1026 /* Advance to the last statement. Set all container types to
1027 void. */
1028 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1030 TREE_SIDE_EFFECTS (*p) = 1;
1031 TREE_TYPE (*p) = void_type_node;
1033 break;
1035 case TRANSACTION_EXPR:
1036 TREE_SIDE_EFFECTS (*p) = 1;
1037 TREE_TYPE (*p) = void_type_node;
1038 p = &TRANSACTION_EXPR_BODY (*p);
1039 break;
1041 default:
1042 /* Assume that any tree upon which voidify_wrapper_expr is
1043 directly called is a wrapper, and that its body is op0. */
1044 if (p == &wrapper)
1046 TREE_SIDE_EFFECTS (*p) = 1;
1047 TREE_TYPE (*p) = void_type_node;
1048 p = &TREE_OPERAND (*p, 0);
1049 break;
1051 goto out;
1055 out:
1056 if (p == NULL || IS_EMPTY_STMT (*p))
1057 temp = NULL_TREE;
1058 else if (temp)
1060 /* The wrapper is on the RHS of an assignment that we're pushing
1061 down. */
1062 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1063 || TREE_CODE (temp) == MODIFY_EXPR);
1064 TREE_OPERAND (temp, 1) = *p;
1065 *p = temp;
1067 else
1069 temp = create_tmp_var (type, "retval");
1070 *p = build2 (INIT_EXPR, type, temp, *p);
1073 return temp;
1076 return NULL_TREE;
1079 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1080 a temporary through which they communicate. */
1082 static void
1083 build_stack_save_restore (gcall **save, gcall **restore)
1085 tree tmp_var;
1087 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1088 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1089 gimple_call_set_lhs (*save, tmp_var);
1091 *restore
1092 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1093 1, tmp_var);
1096 /* Generate IFN_ASAN_MARK call that poisons shadow of a for DECL variable. */
1098 static tree
1099 build_asan_poison_call_expr (tree decl)
1101 /* Do not poison variables that have size equal to zero. */
1102 tree unit_size = DECL_SIZE_UNIT (decl);
1103 if (zerop (unit_size))
1104 return NULL_TREE;
1106 tree base = build_fold_addr_expr (decl);
1108 return build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_ASAN_MARK,
1109 void_type_node, 3,
1110 build_int_cst (integer_type_node,
1111 ASAN_MARK_CLOBBER),
1112 base, unit_size);
1115 /* Generate IFN_ASAN_MARK call that would poison or unpoison, depending
1116 on POISON flag, shadow memory of a DECL variable. The call will be
1117 put on location identified by IT iterator, where BEFORE flag drives
1118 position where the stmt will be put. */
1120 static void
1121 asan_poison_variable (tree decl, bool poison, gimple_stmt_iterator *it,
1122 bool before)
1124 /* When within an OMP context, do not emit ASAN_MARK internal fns. */
1125 if (gimplify_omp_ctxp)
1126 return;
1128 tree unit_size = DECL_SIZE_UNIT (decl);
1129 tree base = build_fold_addr_expr (decl);
1131 /* Do not poison variables that have size equal to zero. */
1132 if (zerop (unit_size))
1133 return;
1135 /* It's necessary to have all stack variables aligned to ASAN granularity
1136 bytes. */
1137 if (DECL_ALIGN_UNIT (decl) <= ASAN_SHADOW_GRANULARITY)
1138 SET_DECL_ALIGN (decl, BITS_PER_UNIT * ASAN_SHADOW_GRANULARITY);
1140 HOST_WIDE_INT flags = poison ? ASAN_MARK_CLOBBER : ASAN_MARK_UNCLOBBER;
1142 gimple *g
1143 = gimple_build_call_internal (IFN_ASAN_MARK, 3,
1144 build_int_cst (integer_type_node, flags),
1145 base, unit_size);
1147 if (before)
1148 gsi_insert_before (it, g, GSI_NEW_STMT);
1149 else
1150 gsi_insert_after (it, g, GSI_NEW_STMT);
1153 /* Generate IFN_ASAN_MARK internal call that depending on POISON flag
1154 either poisons or unpoisons a DECL. Created statement is appended
1155 to SEQ_P gimple sequence. */
1157 static void
1158 asan_poison_variable (tree decl, bool poison, gimple_seq *seq_p)
1160 gimple_stmt_iterator it = gsi_last (*seq_p);
1161 bool before = false;
1163 if (gsi_end_p (it))
1164 before = true;
1166 asan_poison_variable (decl, poison, &it, before);
1169 /* Sort pair of VAR_DECLs A and B by DECL_UID. */
1171 static int
1172 sort_by_decl_uid (const void *a, const void *b)
1174 const tree *t1 = (const tree *)a;
1175 const tree *t2 = (const tree *)b;
1177 int uid1 = DECL_UID (*t1);
1178 int uid2 = DECL_UID (*t2);
1180 if (uid1 < uid2)
1181 return -1;
1182 else if (uid1 > uid2)
1183 return 1;
1184 else
1185 return 0;
1188 /* Generate IFN_ASAN_MARK internal call for all VARIABLES
1189 depending on POISON flag. Created statement is appended
1190 to SEQ_P gimple sequence. */
1192 static void
1193 asan_poison_variables (hash_set<tree> *variables, bool poison, gimple_seq *seq_p)
1195 unsigned c = variables->elements ();
1196 if (c == 0)
1197 return;
1199 auto_vec<tree> sorted_variables (c);
1201 for (hash_set<tree>::iterator it = variables->begin ();
1202 it != variables->end (); ++it)
1203 sorted_variables.safe_push (*it);
1205 sorted_variables.qsort (sort_by_decl_uid);
1207 for (unsigned i = 0; i < sorted_variables.length (); i++)
1208 asan_poison_variable (sorted_variables[i], poison, seq_p);
1211 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1213 static enum gimplify_status
1214 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1216 tree bind_expr = *expr_p;
1217 bool old_keep_stack = gimplify_ctxp->keep_stack;
1218 bool old_save_stack = gimplify_ctxp->save_stack;
1219 tree t;
1220 gbind *bind_stmt;
1221 gimple_seq body, cleanup;
1222 gcall *stack_save;
1223 location_t start_locus = 0, end_locus = 0;
1224 tree ret_clauses = NULL;
1226 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1228 /* Mark variables seen in this bind expr. */
1229 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1231 if (VAR_P (t))
1233 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1235 /* Mark variable as local. */
1236 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1237 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1238 || splay_tree_lookup (ctx->variables,
1239 (splay_tree_key) t) == NULL))
1241 if (ctx->region_type == ORT_SIMD
1242 && TREE_ADDRESSABLE (t)
1243 && !TREE_STATIC (t))
1244 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1245 else
1246 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1249 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1251 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1252 cfun->has_local_explicit_reg_vars = true;
1255 /* Preliminarily mark non-addressed complex variables as eligible
1256 for promotion to gimple registers. We'll transform their uses
1257 as we find them. */
1258 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1259 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1260 && !TREE_THIS_VOLATILE (t)
1261 && (VAR_P (t) && !DECL_HARD_REGISTER (t))
1262 && !needs_to_live_in_memory (t))
1263 DECL_GIMPLE_REG_P (t) = 1;
1266 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1267 BIND_EXPR_BLOCK (bind_expr));
1268 gimple_push_bind_expr (bind_stmt);
1270 gimplify_ctxp->keep_stack = false;
1271 gimplify_ctxp->save_stack = false;
1273 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1274 body = NULL;
1275 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1276 gimple_bind_set_body (bind_stmt, body);
1278 /* Source location wise, the cleanup code (stack_restore and clobbers)
1279 belongs to the end of the block, so propagate what we have. The
1280 stack_save operation belongs to the beginning of block, which we can
1281 infer from the bind_expr directly if the block has no explicit
1282 assignment. */
1283 if (BIND_EXPR_BLOCK (bind_expr))
1285 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1286 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1288 if (start_locus == 0)
1289 start_locus = EXPR_LOCATION (bind_expr);
1291 cleanup = NULL;
1292 stack_save = NULL;
1294 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1295 the stack space allocated to the VLAs. */
1296 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1298 gcall *stack_restore;
1300 /* Save stack on entry and restore it on exit. Add a try_finally
1301 block to achieve this. */
1302 build_stack_save_restore (&stack_save, &stack_restore);
1304 gimple_set_location (stack_save, start_locus);
1305 gimple_set_location (stack_restore, end_locus);
1307 gimplify_seq_add_stmt (&cleanup, stack_restore);
1310 /* Add clobbers for all variables that go out of scope. */
1311 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1313 if (VAR_P (t)
1314 && !is_global_var (t)
1315 && DECL_CONTEXT (t) == current_function_decl)
1317 if (!DECL_HARD_REGISTER (t)
1318 && !TREE_THIS_VOLATILE (t)
1319 && !DECL_HAS_VALUE_EXPR_P (t)
1320 /* Only care for variables that have to be in memory. Others
1321 will be rewritten into SSA names, hence moved to the
1322 top-level. */
1323 && !is_gimple_reg (t)
1324 && flag_stack_reuse != SR_NONE)
1326 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1327 gimple *clobber_stmt;
1328 TREE_THIS_VOLATILE (clobber) = 1;
1329 clobber_stmt = gimple_build_assign (t, clobber);
1330 gimple_set_location (clobber_stmt, end_locus);
1331 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1334 if (flag_openacc && oacc_declare_returns != NULL)
1336 tree *c = oacc_declare_returns->get (t);
1337 if (c != NULL)
1339 if (ret_clauses)
1340 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1342 ret_clauses = *c;
1344 oacc_declare_returns->remove (t);
1346 if (oacc_declare_returns->elements () == 0)
1348 delete oacc_declare_returns;
1349 oacc_declare_returns = NULL;
1355 if (asan_poisoned_variables != NULL
1356 && asan_poisoned_variables->contains (t))
1358 asan_poisoned_variables->remove (t);
1359 asan_poison_variable (t, true, &cleanup);
1362 if (gimplify_ctxp->live_switch_vars != NULL
1363 && gimplify_ctxp->live_switch_vars->contains (t))
1364 gimplify_ctxp->live_switch_vars->remove (t);
1367 if (ret_clauses)
1369 gomp_target *stmt;
1370 gimple_stmt_iterator si = gsi_start (cleanup);
1372 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1373 ret_clauses);
1374 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1377 if (cleanup)
1379 gtry *gs;
1380 gimple_seq new_body;
1382 new_body = NULL;
1383 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1384 GIMPLE_TRY_FINALLY);
1386 if (stack_save)
1387 gimplify_seq_add_stmt (&new_body, stack_save);
1388 gimplify_seq_add_stmt (&new_body, gs);
1389 gimple_bind_set_body (bind_stmt, new_body);
1392 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1393 if (!gimplify_ctxp->keep_stack)
1394 gimplify_ctxp->keep_stack = old_keep_stack;
1395 gimplify_ctxp->save_stack = old_save_stack;
1397 gimple_pop_bind_expr ();
1399 gimplify_seq_add_stmt (pre_p, bind_stmt);
1401 if (temp)
1403 *expr_p = temp;
1404 return GS_OK;
1407 *expr_p = NULL_TREE;
1408 return GS_ALL_DONE;
1411 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1412 GIMPLE value, it is assigned to a new temporary and the statement is
1413 re-written to return the temporary.
1415 PRE_P points to the sequence where side effects that must happen before
1416 STMT should be stored. */
1418 static enum gimplify_status
1419 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1421 greturn *ret;
1422 tree ret_expr = TREE_OPERAND (stmt, 0);
1423 tree result_decl, result;
1425 if (ret_expr == error_mark_node)
1426 return GS_ERROR;
1428 /* Implicit _Cilk_sync must be inserted right before any return statement
1429 if there is a _Cilk_spawn in the function. If the user has provided a
1430 _Cilk_sync, the optimizer should remove this duplicate one. */
1431 if (fn_contains_cilk_spawn_p (cfun))
1433 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1434 gimplify_and_add (impl_sync, pre_p);
1437 if (!ret_expr
1438 || TREE_CODE (ret_expr) == RESULT_DECL
1439 || ret_expr == error_mark_node)
1441 greturn *ret = gimple_build_return (ret_expr);
1442 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1443 gimplify_seq_add_stmt (pre_p, ret);
1444 return GS_ALL_DONE;
1447 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1448 result_decl = NULL_TREE;
1449 else
1451 result_decl = TREE_OPERAND (ret_expr, 0);
1453 /* See through a return by reference. */
1454 if (TREE_CODE (result_decl) == INDIRECT_REF)
1455 result_decl = TREE_OPERAND (result_decl, 0);
1457 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1458 || TREE_CODE (ret_expr) == INIT_EXPR)
1459 && TREE_CODE (result_decl) == RESULT_DECL);
1462 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1463 Recall that aggregate_value_p is FALSE for any aggregate type that is
1464 returned in registers. If we're returning values in registers, then
1465 we don't want to extend the lifetime of the RESULT_DECL, particularly
1466 across another call. In addition, for those aggregates for which
1467 hard_function_value generates a PARALLEL, we'll die during normal
1468 expansion of structure assignments; there's special code in expand_return
1469 to handle this case that does not exist in expand_expr. */
1470 if (!result_decl)
1471 result = NULL_TREE;
1472 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1474 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1476 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1477 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1478 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1479 should be effectively allocated by the caller, i.e. all calls to
1480 this function must be subject to the Return Slot Optimization. */
1481 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1482 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1484 result = result_decl;
1486 else if (gimplify_ctxp->return_temp)
1487 result = gimplify_ctxp->return_temp;
1488 else
1490 result = create_tmp_reg (TREE_TYPE (result_decl));
1492 /* ??? With complex control flow (usually involving abnormal edges),
1493 we can wind up warning about an uninitialized value for this. Due
1494 to how this variable is constructed and initialized, this is never
1495 true. Give up and never warn. */
1496 TREE_NO_WARNING (result) = 1;
1498 gimplify_ctxp->return_temp = result;
1501 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1502 Then gimplify the whole thing. */
1503 if (result != result_decl)
1504 TREE_OPERAND (ret_expr, 0) = result;
1506 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1508 ret = gimple_build_return (result);
1509 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1510 gimplify_seq_add_stmt (pre_p, ret);
1512 return GS_ALL_DONE;
1515 /* Gimplify a variable-length array DECL. */
1517 static void
1518 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1520 /* This is a variable-sized decl. Simplify its size and mark it
1521 for deferred expansion. */
1522 tree t, addr, ptr_type;
1524 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1525 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1527 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1528 if (DECL_HAS_VALUE_EXPR_P (decl))
1529 return;
1531 /* All occurrences of this decl in final gimplified code will be
1532 replaced by indirection. Setting DECL_VALUE_EXPR does two
1533 things: First, it lets the rest of the gimplifier know what
1534 replacement to use. Second, it lets the debug info know
1535 where to find the value. */
1536 ptr_type = build_pointer_type (TREE_TYPE (decl));
1537 addr = create_tmp_var (ptr_type, get_name (decl));
1538 DECL_IGNORED_P (addr) = 0;
1539 t = build_fold_indirect_ref (addr);
1540 TREE_THIS_NOTRAP (t) = 1;
1541 SET_DECL_VALUE_EXPR (decl, t);
1542 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1544 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1545 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1546 size_int (DECL_ALIGN (decl)));
1547 /* The call has been built for a variable-sized object. */
1548 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1549 t = fold_convert (ptr_type, t);
1550 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1552 gimplify_and_add (t, seq_p);
1555 /* A helper function to be called via walk_tree. Mark all labels under *TP
1556 as being forced. To be called for DECL_INITIAL of static variables. */
1558 static tree
1559 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1561 if (TYPE_P (*tp))
1562 *walk_subtrees = 0;
1563 if (TREE_CODE (*tp) == LABEL_DECL)
1565 FORCED_LABEL (*tp) = 1;
1566 cfun->has_forced_label_in_static = 1;
1569 return NULL_TREE;
1572 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1573 and initialization explicit. */
1575 static enum gimplify_status
1576 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1578 tree stmt = *stmt_p;
1579 tree decl = DECL_EXPR_DECL (stmt);
1581 *stmt_p = NULL_TREE;
1583 if (TREE_TYPE (decl) == error_mark_node)
1584 return GS_ERROR;
1586 if ((TREE_CODE (decl) == TYPE_DECL
1587 || VAR_P (decl))
1588 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1590 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1591 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1592 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1595 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1596 in case its size expressions contain problematic nodes like CALL_EXPR. */
1597 if (TREE_CODE (decl) == TYPE_DECL
1598 && DECL_ORIGINAL_TYPE (decl)
1599 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1601 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1602 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1603 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1606 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1608 tree init = DECL_INITIAL (decl);
1609 bool is_vla = false;
1611 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1612 || (!TREE_STATIC (decl)
1613 && flag_stack_check == GENERIC_STACK_CHECK
1614 && compare_tree_int (DECL_SIZE_UNIT (decl),
1615 STACK_CHECK_MAX_VAR_SIZE) > 0))
1617 gimplify_vla_decl (decl, seq_p);
1618 is_vla = true;
1621 if (asan_sanitize_use_after_scope ()
1622 && !asan_no_sanitize_address_p ()
1623 && !is_vla
1624 && TREE_ADDRESSABLE (decl)
1625 && !TREE_STATIC (decl))
1627 asan_poisoned_variables->add (decl);
1628 asan_poison_variable (decl, false, seq_p);
1629 if (gimplify_ctxp->live_switch_vars)
1630 gimplify_ctxp->live_switch_vars->add (decl);
1633 /* Some front ends do not explicitly declare all anonymous
1634 artificial variables. We compensate here by declaring the
1635 variables, though it would be better if the front ends would
1636 explicitly declare them. */
1637 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1638 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1639 gimple_add_tmp_var (decl);
1641 if (init && init != error_mark_node)
1643 if (!TREE_STATIC (decl))
1645 DECL_INITIAL (decl) = NULL_TREE;
1646 init = build2 (INIT_EXPR, void_type_node, decl, init);
1647 gimplify_and_add (init, seq_p);
1648 ggc_free (init);
1650 else
1651 /* We must still examine initializers for static variables
1652 as they may contain a label address. */
1653 walk_tree (&init, force_labels_r, NULL, NULL);
1657 return GS_ALL_DONE;
1660 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1661 and replacing the LOOP_EXPR with goto, but if the loop contains an
1662 EXIT_EXPR, we need to append a label for it to jump to. */
1664 static enum gimplify_status
1665 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1667 tree saved_label = gimplify_ctxp->exit_label;
1668 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1670 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1672 gimplify_ctxp->exit_label = NULL_TREE;
1674 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1676 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1678 if (gimplify_ctxp->exit_label)
1679 gimplify_seq_add_stmt (pre_p,
1680 gimple_build_label (gimplify_ctxp->exit_label));
1682 gimplify_ctxp->exit_label = saved_label;
1684 *expr_p = NULL;
1685 return GS_ALL_DONE;
1688 /* Gimplify a statement list onto a sequence. These may be created either
1689 by an enlightened front-end, or by shortcut_cond_expr. */
1691 static enum gimplify_status
1692 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1694 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1696 tree_stmt_iterator i = tsi_start (*expr_p);
1698 while (!tsi_end_p (i))
1700 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1701 tsi_delink (&i);
1704 if (temp)
1706 *expr_p = temp;
1707 return GS_OK;
1710 return GS_ALL_DONE;
1713 /* Callback for walk_gimple_seq. */
1715 static tree
1716 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1717 struct walk_stmt_info *wi)
1719 gimple *stmt = gsi_stmt (*gsi_p);
1721 *handled_ops_p = true;
1722 switch (gimple_code (stmt))
1724 case GIMPLE_TRY:
1725 /* A compiler-generated cleanup or a user-written try block.
1726 If it's empty, don't dive into it--that would result in
1727 worse location info. */
1728 if (gimple_try_eval (stmt) == NULL)
1730 wi->info = stmt;
1731 return integer_zero_node;
1733 /* Fall through. */
1734 case GIMPLE_BIND:
1735 case GIMPLE_CATCH:
1736 case GIMPLE_EH_FILTER:
1737 case GIMPLE_TRANSACTION:
1738 /* Walk the sub-statements. */
1739 *handled_ops_p = false;
1740 break;
1741 case GIMPLE_CALL:
1742 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1744 *handled_ops_p = false;
1745 break;
1747 /* Fall through. */
1748 default:
1749 /* Save the first "real" statement (not a decl/lexical scope/...). */
1750 wi->info = stmt;
1751 return integer_zero_node;
1753 return NULL_TREE;
1756 /* Possibly warn about unreachable statements between switch's controlling
1757 expression and the first case. SEQ is the body of a switch expression. */
1759 static void
1760 maybe_warn_switch_unreachable (gimple_seq seq)
1762 if (!warn_switch_unreachable
1763 /* This warning doesn't play well with Fortran when optimizations
1764 are on. */
1765 || lang_GNU_Fortran ()
1766 || seq == NULL)
1767 return;
1769 struct walk_stmt_info wi;
1770 memset (&wi, 0, sizeof (wi));
1771 walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1772 gimple *stmt = (gimple *) wi.info;
1774 if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1776 if (gimple_code (stmt) == GIMPLE_GOTO
1777 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1778 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1779 /* Don't warn for compiler-generated gotos. These occur
1780 in Duff's devices, for example. */;
1781 else
1782 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1783 "statement will never be executed");
1788 /* A label entry that pairs label and a location. */
1789 struct label_entry
1791 tree label;
1792 location_t loc;
1795 /* Find LABEL in vector of label entries VEC. */
1797 static struct label_entry *
1798 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1800 unsigned int i;
1801 struct label_entry *l;
1803 FOR_EACH_VEC_ELT (*vec, i, l)
1804 if (l->label == label)
1805 return l;
1806 return NULL;
1809 /* Return true if LABEL, a LABEL_DECL, represents a case label
1810 in a vector of labels CASES. */
1812 static bool
1813 case_label_p (const vec<tree> *cases, tree label)
1815 unsigned int i;
1816 tree l;
1818 FOR_EACH_VEC_ELT (*cases, i, l)
1819 if (CASE_LABEL (l) == label)
1820 return true;
1821 return false;
1824 /* Find the last statement in a scope STMT. */
1826 static gimple *
1827 last_stmt_in_scope (gimple *stmt)
1829 if (!stmt)
1830 return NULL;
1832 switch (gimple_code (stmt))
1834 case GIMPLE_BIND:
1836 gbind *bind = as_a <gbind *> (stmt);
1837 stmt = gimple_seq_last_stmt (gimple_bind_body (bind));
1838 return last_stmt_in_scope (stmt);
1841 case GIMPLE_TRY:
1843 gtry *try_stmt = as_a <gtry *> (stmt);
1844 stmt = gimple_seq_last_stmt (gimple_try_eval (try_stmt));
1845 gimple *last_eval = last_stmt_in_scope (stmt);
1846 if (gimple_stmt_may_fallthru (last_eval)
1847 && (last_eval == NULL
1848 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1849 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1851 stmt = gimple_seq_last_stmt (gimple_try_cleanup (try_stmt));
1852 return last_stmt_in_scope (stmt);
1854 else
1855 return last_eval;
1858 default:
1859 return stmt;
1863 /* Collect interesting labels in LABELS and return the statement preceding
1864 another case label, or a user-defined label. */
1866 static gimple *
1867 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
1868 auto_vec <struct label_entry> *labels)
1870 gimple *prev = NULL;
1874 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
1875 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
1877 /* Nested scope. Only look at the last statement of
1878 the innermost scope. */
1879 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
1880 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
1881 if (last)
1883 prev = last;
1884 /* It might be a label without a location. Use the
1885 location of the scope then. */
1886 if (!gimple_has_location (prev))
1887 gimple_set_location (prev, bind_loc);
1889 gsi_next (gsi_p);
1890 continue;
1893 /* Ifs are tricky. */
1894 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
1896 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
1897 tree false_lab = gimple_cond_false_label (cond_stmt);
1898 location_t if_loc = gimple_location (cond_stmt);
1900 /* If we have e.g.
1901 if (i > 1) goto <D.2259>; else goto D;
1902 we can't do much with the else-branch. */
1903 if (!DECL_ARTIFICIAL (false_lab))
1904 break;
1906 /* Go on until the false label, then one step back. */
1907 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
1909 gimple *stmt = gsi_stmt (*gsi_p);
1910 if (gimple_code (stmt) == GIMPLE_LABEL
1911 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
1912 break;
1915 /* Not found? Oops. */
1916 if (gsi_end_p (*gsi_p))
1917 break;
1919 struct label_entry l = { false_lab, if_loc };
1920 labels->safe_push (l);
1922 /* Go to the last statement of the then branch. */
1923 gsi_prev (gsi_p);
1925 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
1926 <D.1759>:
1927 <stmt>;
1928 goto <D.1761>;
1929 <D.1760>:
1931 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
1932 && !gimple_has_location (gsi_stmt (*gsi_p)))
1934 /* Look at the statement before, it might be
1935 attribute fallthrough, in which case don't warn. */
1936 gsi_prev (gsi_p);
1937 bool fallthru_before_dest
1938 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
1939 gsi_next (gsi_p);
1940 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
1941 if (!fallthru_before_dest)
1943 struct label_entry l = { goto_dest, if_loc };
1944 labels->safe_push (l);
1947 /* And move back. */
1948 gsi_next (gsi_p);
1951 /* Remember the last statement. Skip labels that are of no interest
1952 to us. */
1953 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
1955 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
1956 if (find_label_entry (labels, label))
1957 prev = gsi_stmt (*gsi_p);
1959 else if (gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_ASAN_MARK))
1961 else
1962 prev = gsi_stmt (*gsi_p);
1963 gsi_next (gsi_p);
1965 while (!gsi_end_p (*gsi_p)
1966 /* Stop if we find a case or a user-defined label. */
1967 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
1968 || !gimple_has_location (gsi_stmt (*gsi_p))));
1970 return prev;
1973 /* Return true if the switch fallthough warning should occur. LABEL is
1974 the label statement that we're falling through to. */
1976 static bool
1977 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
1979 gimple_stmt_iterator gsi = *gsi_p;
1981 /* Don't warn if the label is marked with a "falls through" comment. */
1982 if (FALLTHROUGH_LABEL_P (label))
1983 return false;
1985 /* Don't warn for a non-case label followed by a statement:
1986 case 0:
1987 foo ();
1988 label:
1989 bar ();
1990 as these are likely intentional. */
1991 if (!case_label_p (&gimplify_ctxp->case_labels, label))
1993 gsi_next (&gsi);
1994 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
1995 return false;
1998 /* Don't warn for terminated branches, i.e. when the subsequent case labels
1999 immediately breaks. */
2000 gsi = *gsi_p;
2002 /* Skip all immediately following labels. */
2003 while (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
2004 gsi_next (&gsi);
2006 /* { ... something; default:; } */
2007 if (gsi_end_p (gsi)
2008 /* { ... something; default: break; } or
2009 { ... something; default: goto L; } */
2010 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
2011 /* { ... something; default: return; } */
2012 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
2013 return false;
2015 return true;
2018 /* Callback for walk_gimple_seq. */
2020 static tree
2021 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2022 struct walk_stmt_info *)
2024 gimple *stmt = gsi_stmt (*gsi_p);
2026 *handled_ops_p = true;
2027 switch (gimple_code (stmt))
2029 case GIMPLE_TRY:
2030 case GIMPLE_BIND:
2031 case GIMPLE_CATCH:
2032 case GIMPLE_EH_FILTER:
2033 case GIMPLE_TRANSACTION:
2034 /* Walk the sub-statements. */
2035 *handled_ops_p = false;
2036 break;
2038 /* Find a sequence of form:
2040 GIMPLE_LABEL
2041 [...]
2042 <may fallthru stmt>
2043 GIMPLE_LABEL
2045 and possibly warn. */
2046 case GIMPLE_LABEL:
2048 /* Found a label. Skip all immediately following labels. */
2049 while (!gsi_end_p (*gsi_p)
2050 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
2051 gsi_next (gsi_p);
2053 /* There might be no more statements. */
2054 if (gsi_end_p (*gsi_p))
2055 return integer_zero_node;
2057 /* Vector of labels that fall through. */
2058 auto_vec <struct label_entry> labels;
2059 gimple *prev = collect_fallthrough_labels (gsi_p, &labels);
2061 /* There might be no more statements. */
2062 if (gsi_end_p (*gsi_p))
2063 return integer_zero_node;
2065 gimple *next = gsi_stmt (*gsi_p);
2066 tree label;
2067 /* If what follows is a label, then we may have a fallthrough. */
2068 if (gimple_code (next) == GIMPLE_LABEL
2069 && gimple_has_location (next)
2070 && (label = gimple_label_label (as_a <glabel *> (next)))
2071 && prev != NULL)
2073 struct label_entry *l;
2074 bool warned_p = false;
2075 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
2076 /* Quiet. */;
2077 else if (gimple_code (prev) == GIMPLE_LABEL
2078 && (label = gimple_label_label (as_a <glabel *> (prev)))
2079 && (l = find_label_entry (&labels, label)))
2080 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
2081 "this statement may fall through");
2082 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
2083 /* Try to be clever and don't warn when the statement
2084 can't actually fall through. */
2085 && gimple_stmt_may_fallthru (prev)
2086 && gimple_has_location (prev))
2087 warned_p = warning_at (gimple_location (prev),
2088 OPT_Wimplicit_fallthrough_,
2089 "this statement may fall through");
2090 if (warned_p)
2091 inform (gimple_location (next), "here");
2093 /* Mark this label as processed so as to prevent multiple
2094 warnings in nested switches. */
2095 FALLTHROUGH_LABEL_P (label) = true;
2097 /* So that next warn_implicit_fallthrough_r will start looking for
2098 a new sequence starting with this label. */
2099 gsi_prev (gsi_p);
2102 break;
2103 default:
2104 break;
2106 return NULL_TREE;
2109 /* Warn when a switch case falls through. */
2111 static void
2112 maybe_warn_implicit_fallthrough (gimple_seq seq)
2114 if (!warn_implicit_fallthrough)
2115 return;
2117 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
2118 if (!(lang_GNU_C ()
2119 || lang_GNU_CXX ()
2120 || lang_GNU_OBJC ()))
2121 return;
2123 struct walk_stmt_info wi;
2124 memset (&wi, 0, sizeof (wi));
2125 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
2128 /* Callback for walk_gimple_seq. */
2130 static tree
2131 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
2132 struct walk_stmt_info *)
2134 gimple *stmt = gsi_stmt (*gsi_p);
2136 *handled_ops_p = true;
2137 switch (gimple_code (stmt))
2139 case GIMPLE_TRY:
2140 case GIMPLE_BIND:
2141 case GIMPLE_CATCH:
2142 case GIMPLE_EH_FILTER:
2143 case GIMPLE_TRANSACTION:
2144 /* Walk the sub-statements. */
2145 *handled_ops_p = false;
2146 break;
2147 case GIMPLE_CALL:
2148 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
2150 gsi_remove (gsi_p, true);
2151 if (gsi_end_p (*gsi_p))
2152 return integer_zero_node;
2154 bool found = false;
2155 location_t loc = gimple_location (stmt);
2157 gimple_stmt_iterator gsi2 = *gsi_p;
2158 stmt = gsi_stmt (gsi2);
2159 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2161 /* Go on until the artificial label. */
2162 tree goto_dest = gimple_goto_dest (stmt);
2163 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2165 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2166 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2167 == goto_dest)
2168 break;
2171 /* Not found? Stop. */
2172 if (gsi_end_p (gsi2))
2173 break;
2175 /* Look one past it. */
2176 gsi_next (&gsi2);
2179 /* We're looking for a case label or default label here. */
2180 while (!gsi_end_p (gsi2))
2182 stmt = gsi_stmt (gsi2);
2183 if (gimple_code (stmt) == GIMPLE_LABEL)
2185 tree label = gimple_label_label (as_a <glabel *> (stmt));
2186 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2188 found = true;
2189 break;
2192 else
2193 /* Something other than a label. That's not expected. */
2194 break;
2195 gsi_next (&gsi2);
2197 if (!found)
2198 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2199 "a case label or default label");
2201 break;
2202 default:
2203 break;
2205 return NULL_TREE;
2208 /* Expand all FALLTHROUGH () calls in SEQ. */
2210 static void
2211 expand_FALLTHROUGH (gimple_seq *seq_p)
2213 struct walk_stmt_info wi;
2214 memset (&wi, 0, sizeof (wi));
2215 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2219 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2220 branch to. */
2222 static enum gimplify_status
2223 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2225 tree switch_expr = *expr_p;
2226 gimple_seq switch_body_seq = NULL;
2227 enum gimplify_status ret;
2228 tree index_type = TREE_TYPE (switch_expr);
2229 if (index_type == NULL_TREE)
2230 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2232 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2233 fb_rvalue);
2234 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2235 return ret;
2237 if (SWITCH_BODY (switch_expr))
2239 vec<tree> labels;
2240 vec<tree> saved_labels;
2241 hash_set<tree> *saved_live_switch_vars;
2242 tree default_case = NULL_TREE;
2243 gswitch *switch_stmt;
2245 /* If someone can be bothered to fill in the labels, they can
2246 be bothered to null out the body too. */
2247 gcc_assert (!SWITCH_LABELS (switch_expr));
2249 /* Save old labels, get new ones from body, then restore the old
2250 labels. Save all the things from the switch body to append after. */
2251 saved_labels = gimplify_ctxp->case_labels;
2252 gimplify_ctxp->case_labels.create (8);
2253 saved_live_switch_vars = gimplify_ctxp->live_switch_vars;
2254 gimplify_ctxp->live_switch_vars = new hash_set<tree> (4);
2255 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2256 gimplify_ctxp->in_switch_expr = true;
2258 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2260 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2261 maybe_warn_switch_unreachable (switch_body_seq);
2262 maybe_warn_implicit_fallthrough (switch_body_seq);
2263 /* Only do this for the outermost GIMPLE_SWITCH. */
2264 if (!gimplify_ctxp->in_switch_expr)
2265 expand_FALLTHROUGH (&switch_body_seq);
2267 labels = gimplify_ctxp->case_labels;
2268 gimplify_ctxp->case_labels = saved_labels;
2269 gcc_assert (gimplify_ctxp->live_switch_vars->elements () == 0);
2270 delete gimplify_ctxp->live_switch_vars;
2271 gimplify_ctxp->live_switch_vars = saved_live_switch_vars;
2273 preprocess_case_label_vec_for_gimple (labels, index_type,
2274 &default_case);
2276 if (!default_case)
2278 glabel *new_default;
2280 default_case
2281 = build_case_label (NULL_TREE, NULL_TREE,
2282 create_artificial_label (UNKNOWN_LOCATION));
2283 new_default = gimple_build_label (CASE_LABEL (default_case));
2284 gimplify_seq_add_stmt (&switch_body_seq, new_default);
2287 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2288 default_case, labels);
2289 gimplify_seq_add_stmt (pre_p, switch_stmt);
2290 gimplify_seq_add_seq (pre_p, switch_body_seq);
2291 labels.release ();
2293 else
2294 gcc_assert (SWITCH_LABELS (switch_expr));
2296 return GS_ALL_DONE;
2299 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2301 static enum gimplify_status
2302 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2304 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2305 == current_function_decl);
2307 glabel *label_stmt = gimple_build_label (LABEL_EXPR_LABEL (*expr_p));
2308 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2309 gimplify_seq_add_stmt (pre_p, label_stmt);
2311 return GS_ALL_DONE;
2314 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2316 static enum gimplify_status
2317 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2319 struct gimplify_ctx *ctxp;
2320 glabel *label_stmt;
2322 /* Invalid programs can play Duff's Device type games with, for example,
2323 #pragma omp parallel. At least in the C front end, we don't
2324 detect such invalid branches until after gimplification, in the
2325 diagnose_omp_blocks pass. */
2326 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2327 if (ctxp->case_labels.exists ())
2328 break;
2330 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
2331 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2332 ctxp->case_labels.safe_push (*expr_p);
2333 gimplify_seq_add_stmt (pre_p, label_stmt);
2335 return GS_ALL_DONE;
2338 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2339 if necessary. */
2341 tree
2342 build_and_jump (tree *label_p)
2344 if (label_p == NULL)
2345 /* If there's nowhere to jump, just fall through. */
2346 return NULL_TREE;
2348 if (*label_p == NULL_TREE)
2350 tree label = create_artificial_label (UNKNOWN_LOCATION);
2351 *label_p = label;
2354 return build1 (GOTO_EXPR, void_type_node, *label_p);
2357 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2358 This also involves building a label to jump to and communicating it to
2359 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2361 static enum gimplify_status
2362 gimplify_exit_expr (tree *expr_p)
2364 tree cond = TREE_OPERAND (*expr_p, 0);
2365 tree expr;
2367 expr = build_and_jump (&gimplify_ctxp->exit_label);
2368 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2369 *expr_p = expr;
2371 return GS_OK;
2374 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2375 different from its canonical type, wrap the whole thing inside a
2376 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2377 type.
2379 The canonical type of a COMPONENT_REF is the type of the field being
2380 referenced--unless the field is a bit-field which can be read directly
2381 in a smaller mode, in which case the canonical type is the
2382 sign-appropriate type corresponding to that mode. */
2384 static void
2385 canonicalize_component_ref (tree *expr_p)
2387 tree expr = *expr_p;
2388 tree type;
2390 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2392 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2393 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2394 else
2395 type = TREE_TYPE (TREE_OPERAND (expr, 1));
2397 /* One could argue that all the stuff below is not necessary for
2398 the non-bitfield case and declare it a FE error if type
2399 adjustment would be needed. */
2400 if (TREE_TYPE (expr) != type)
2402 #ifdef ENABLE_TYPES_CHECKING
2403 tree old_type = TREE_TYPE (expr);
2404 #endif
2405 int type_quals;
2407 /* We need to preserve qualifiers and propagate them from
2408 operand 0. */
2409 type_quals = TYPE_QUALS (type)
2410 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2411 if (TYPE_QUALS (type) != type_quals)
2412 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2414 /* Set the type of the COMPONENT_REF to the underlying type. */
2415 TREE_TYPE (expr) = type;
2417 #ifdef ENABLE_TYPES_CHECKING
2418 /* It is now a FE error, if the conversion from the canonical
2419 type to the original expression type is not useless. */
2420 gcc_assert (useless_type_conversion_p (old_type, type));
2421 #endif
2425 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2426 to foo, embed that change in the ADDR_EXPR by converting
2427 T array[U];
2428 (T *)&array
2430 &array[L]
2431 where L is the lower bound. For simplicity, only do this for constant
2432 lower bound.
2433 The constraint is that the type of &array[L] is trivially convertible
2434 to T *. */
2436 static void
2437 canonicalize_addr_expr (tree *expr_p)
2439 tree expr = *expr_p;
2440 tree addr_expr = TREE_OPERAND (expr, 0);
2441 tree datype, ddatype, pddatype;
2443 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2444 if (!POINTER_TYPE_P (TREE_TYPE (expr))
2445 || TREE_CODE (addr_expr) != ADDR_EXPR)
2446 return;
2448 /* The addr_expr type should be a pointer to an array. */
2449 datype = TREE_TYPE (TREE_TYPE (addr_expr));
2450 if (TREE_CODE (datype) != ARRAY_TYPE)
2451 return;
2453 /* The pointer to element type shall be trivially convertible to
2454 the expression pointer type. */
2455 ddatype = TREE_TYPE (datype);
2456 pddatype = build_pointer_type (ddatype);
2457 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2458 pddatype))
2459 return;
2461 /* The lower bound and element sizes must be constant. */
2462 if (!TYPE_SIZE_UNIT (ddatype)
2463 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2464 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2465 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2466 return;
2468 /* All checks succeeded. Build a new node to merge the cast. */
2469 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2470 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2471 NULL_TREE, NULL_TREE);
2472 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2474 /* We can have stripped a required restrict qualifier above. */
2475 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2476 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2479 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2480 underneath as appropriate. */
2482 static enum gimplify_status
2483 gimplify_conversion (tree *expr_p)
2485 location_t loc = EXPR_LOCATION (*expr_p);
2486 gcc_assert (CONVERT_EXPR_P (*expr_p));
2488 /* Then strip away all but the outermost conversion. */
2489 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2491 /* And remove the outermost conversion if it's useless. */
2492 if (tree_ssa_useless_type_conversion (*expr_p))
2493 *expr_p = TREE_OPERAND (*expr_p, 0);
2495 /* If we still have a conversion at the toplevel,
2496 then canonicalize some constructs. */
2497 if (CONVERT_EXPR_P (*expr_p))
2499 tree sub = TREE_OPERAND (*expr_p, 0);
2501 /* If a NOP conversion is changing the type of a COMPONENT_REF
2502 expression, then canonicalize its type now in order to expose more
2503 redundant conversions. */
2504 if (TREE_CODE (sub) == COMPONENT_REF)
2505 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2507 /* If a NOP conversion is changing a pointer to array of foo
2508 to a pointer to foo, embed that change in the ADDR_EXPR. */
2509 else if (TREE_CODE (sub) == ADDR_EXPR)
2510 canonicalize_addr_expr (expr_p);
2513 /* If we have a conversion to a non-register type force the
2514 use of a VIEW_CONVERT_EXPR instead. */
2515 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2516 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2517 TREE_OPERAND (*expr_p, 0));
2519 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2520 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2521 TREE_SET_CODE (*expr_p, NOP_EXPR);
2523 return GS_OK;
2526 /* Nonlocal VLAs seen in the current function. */
2527 static hash_set<tree> *nonlocal_vlas;
2529 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
2530 static tree nonlocal_vla_vars;
2532 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2533 DECL_VALUE_EXPR, and it's worth re-examining things. */
2535 static enum gimplify_status
2536 gimplify_var_or_parm_decl (tree *expr_p)
2538 tree decl = *expr_p;
2540 /* ??? If this is a local variable, and it has not been seen in any
2541 outer BIND_EXPR, then it's probably the result of a duplicate
2542 declaration, for which we've already issued an error. It would
2543 be really nice if the front end wouldn't leak these at all.
2544 Currently the only known culprit is C++ destructors, as seen
2545 in g++.old-deja/g++.jason/binding.C. */
2546 if (VAR_P (decl)
2547 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2548 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2549 && decl_function_context (decl) == current_function_decl)
2551 gcc_assert (seen_error ());
2552 return GS_ERROR;
2555 /* When within an OMP context, notice uses of variables. */
2556 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2557 return GS_ALL_DONE;
2559 /* If the decl is an alias for another expression, substitute it now. */
2560 if (DECL_HAS_VALUE_EXPR_P (decl))
2562 tree value_expr = DECL_VALUE_EXPR (decl);
2564 /* For referenced nonlocal VLAs add a decl for debugging purposes
2565 to the current function. */
2566 if (VAR_P (decl)
2567 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2568 && nonlocal_vlas != NULL
2569 && TREE_CODE (value_expr) == INDIRECT_REF
2570 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2571 && decl_function_context (decl) != current_function_decl)
2573 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2574 while (ctx
2575 && (ctx->region_type == ORT_WORKSHARE
2576 || ctx->region_type == ORT_SIMD
2577 || ctx->region_type == ORT_ACC))
2578 ctx = ctx->outer_context;
2579 if (!ctx && !nonlocal_vlas->add (decl))
2581 tree copy = copy_node (decl);
2583 lang_hooks.dup_lang_specific_decl (copy);
2584 SET_DECL_RTL (copy, 0);
2585 TREE_USED (copy) = 1;
2586 DECL_CHAIN (copy) = nonlocal_vla_vars;
2587 nonlocal_vla_vars = copy;
2588 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2589 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2593 *expr_p = unshare_expr (value_expr);
2594 return GS_OK;
2597 return GS_ALL_DONE;
2600 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2602 static void
2603 recalculate_side_effects (tree t)
2605 enum tree_code code = TREE_CODE (t);
2606 int len = TREE_OPERAND_LENGTH (t);
2607 int i;
2609 switch (TREE_CODE_CLASS (code))
2611 case tcc_expression:
2612 switch (code)
2614 case INIT_EXPR:
2615 case MODIFY_EXPR:
2616 case VA_ARG_EXPR:
2617 case PREDECREMENT_EXPR:
2618 case PREINCREMENT_EXPR:
2619 case POSTDECREMENT_EXPR:
2620 case POSTINCREMENT_EXPR:
2621 /* All of these have side-effects, no matter what their
2622 operands are. */
2623 return;
2625 default:
2626 break;
2628 /* Fall through. */
2630 case tcc_comparison: /* a comparison expression */
2631 case tcc_unary: /* a unary arithmetic expression */
2632 case tcc_binary: /* a binary arithmetic expression */
2633 case tcc_reference: /* a reference */
2634 case tcc_vl_exp: /* a function call */
2635 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2636 for (i = 0; i < len; ++i)
2638 tree op = TREE_OPERAND (t, i);
2639 if (op && TREE_SIDE_EFFECTS (op))
2640 TREE_SIDE_EFFECTS (t) = 1;
2642 break;
2644 case tcc_constant:
2645 /* No side-effects. */
2646 return;
2648 default:
2649 gcc_unreachable ();
2653 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2654 node *EXPR_P.
2656 compound_lval
2657 : min_lval '[' val ']'
2658 | min_lval '.' ID
2659 | compound_lval '[' val ']'
2660 | compound_lval '.' ID
2662 This is not part of the original SIMPLE definition, which separates
2663 array and member references, but it seems reasonable to handle them
2664 together. Also, this way we don't run into problems with union
2665 aliasing; gcc requires that for accesses through a union to alias, the
2666 union reference must be explicit, which was not always the case when we
2667 were splitting up array and member refs.
2669 PRE_P points to the sequence where side effects that must happen before
2670 *EXPR_P should be stored.
2672 POST_P points to the sequence where side effects that must happen after
2673 *EXPR_P should be stored. */
2675 static enum gimplify_status
2676 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2677 fallback_t fallback)
2679 tree *p;
2680 enum gimplify_status ret = GS_ALL_DONE, tret;
2681 int i;
2682 location_t loc = EXPR_LOCATION (*expr_p);
2683 tree expr = *expr_p;
2685 /* Create a stack of the subexpressions so later we can walk them in
2686 order from inner to outer. */
2687 auto_vec<tree, 10> expr_stack;
2689 /* We can handle anything that get_inner_reference can deal with. */
2690 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2692 restart:
2693 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2694 if (TREE_CODE (*p) == INDIRECT_REF)
2695 *p = fold_indirect_ref_loc (loc, *p);
2697 if (handled_component_p (*p))
2699 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2700 additional COMPONENT_REFs. */
2701 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2702 && gimplify_var_or_parm_decl (p) == GS_OK)
2703 goto restart;
2704 else
2705 break;
2707 expr_stack.safe_push (*p);
2710 gcc_assert (expr_stack.length ());
2712 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2713 walked through and P points to the innermost expression.
2715 Java requires that we elaborated nodes in source order. That
2716 means we must gimplify the inner expression followed by each of
2717 the indices, in order. But we can't gimplify the inner
2718 expression until we deal with any variable bounds, sizes, or
2719 positions in order to deal with PLACEHOLDER_EXPRs.
2721 So we do this in three steps. First we deal with the annotations
2722 for any variables in the components, then we gimplify the base,
2723 then we gimplify any indices, from left to right. */
2724 for (i = expr_stack.length () - 1; i >= 0; i--)
2726 tree t = expr_stack[i];
2728 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2730 /* Gimplify the low bound and element type size and put them into
2731 the ARRAY_REF. If these values are set, they have already been
2732 gimplified. */
2733 if (TREE_OPERAND (t, 2) == NULL_TREE)
2735 tree low = unshare_expr (array_ref_low_bound (t));
2736 if (!is_gimple_min_invariant (low))
2738 TREE_OPERAND (t, 2) = low;
2739 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2740 post_p, is_gimple_reg,
2741 fb_rvalue);
2742 ret = MIN (ret, tret);
2745 else
2747 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2748 is_gimple_reg, fb_rvalue);
2749 ret = MIN (ret, tret);
2752 if (TREE_OPERAND (t, 3) == NULL_TREE)
2754 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2755 tree elmt_size = unshare_expr (array_ref_element_size (t));
2756 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2758 /* Divide the element size by the alignment of the element
2759 type (above). */
2760 elmt_size
2761 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2763 if (!is_gimple_min_invariant (elmt_size))
2765 TREE_OPERAND (t, 3) = elmt_size;
2766 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2767 post_p, is_gimple_reg,
2768 fb_rvalue);
2769 ret = MIN (ret, tret);
2772 else
2774 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2775 is_gimple_reg, fb_rvalue);
2776 ret = MIN (ret, tret);
2779 else if (TREE_CODE (t) == COMPONENT_REF)
2781 /* Set the field offset into T and gimplify it. */
2782 if (TREE_OPERAND (t, 2) == NULL_TREE)
2784 tree offset = unshare_expr (component_ref_field_offset (t));
2785 tree field = TREE_OPERAND (t, 1);
2786 tree factor
2787 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2789 /* Divide the offset by its alignment. */
2790 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2792 if (!is_gimple_min_invariant (offset))
2794 TREE_OPERAND (t, 2) = offset;
2795 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2796 post_p, is_gimple_reg,
2797 fb_rvalue);
2798 ret = MIN (ret, tret);
2801 else
2803 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2804 is_gimple_reg, fb_rvalue);
2805 ret = MIN (ret, tret);
2810 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2811 so as to match the min_lval predicate. Failure to do so may result
2812 in the creation of large aggregate temporaries. */
2813 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2814 fallback | fb_lvalue);
2815 ret = MIN (ret, tret);
2817 /* And finally, the indices and operands of ARRAY_REF. During this
2818 loop we also remove any useless conversions. */
2819 for (; expr_stack.length () > 0; )
2821 tree t = expr_stack.pop ();
2823 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2825 /* Gimplify the dimension. */
2826 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2828 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2829 is_gimple_val, fb_rvalue);
2830 ret = MIN (ret, tret);
2834 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2836 /* The innermost expression P may have originally had
2837 TREE_SIDE_EFFECTS set which would have caused all the outer
2838 expressions in *EXPR_P leading to P to also have had
2839 TREE_SIDE_EFFECTS set. */
2840 recalculate_side_effects (t);
2843 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2844 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2846 canonicalize_component_ref (expr_p);
2849 expr_stack.release ();
2851 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2853 return ret;
2856 /* Gimplify the self modifying expression pointed to by EXPR_P
2857 (++, --, +=, -=).
2859 PRE_P points to the list where side effects that must happen before
2860 *EXPR_P should be stored.
2862 POST_P points to the list where side effects that must happen after
2863 *EXPR_P should be stored.
2865 WANT_VALUE is nonzero iff we want to use the value of this expression
2866 in another expression.
2868 ARITH_TYPE is the type the computation should be performed in. */
2870 enum gimplify_status
2871 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2872 bool want_value, tree arith_type)
2874 enum tree_code code;
2875 tree lhs, lvalue, rhs, t1;
2876 gimple_seq post = NULL, *orig_post_p = post_p;
2877 bool postfix;
2878 enum tree_code arith_code;
2879 enum gimplify_status ret;
2880 location_t loc = EXPR_LOCATION (*expr_p);
2882 code = TREE_CODE (*expr_p);
2884 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2885 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2887 /* Prefix or postfix? */
2888 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2889 /* Faster to treat as prefix if result is not used. */
2890 postfix = want_value;
2891 else
2892 postfix = false;
2894 /* For postfix, make sure the inner expression's post side effects
2895 are executed after side effects from this expression. */
2896 if (postfix)
2897 post_p = &post;
2899 /* Add or subtract? */
2900 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2901 arith_code = PLUS_EXPR;
2902 else
2903 arith_code = MINUS_EXPR;
2905 /* Gimplify the LHS into a GIMPLE lvalue. */
2906 lvalue = TREE_OPERAND (*expr_p, 0);
2907 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2908 if (ret == GS_ERROR)
2909 return ret;
2911 /* Extract the operands to the arithmetic operation. */
2912 lhs = lvalue;
2913 rhs = TREE_OPERAND (*expr_p, 1);
2915 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2916 that as the result value and in the postqueue operation. */
2917 if (postfix)
2919 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2920 if (ret == GS_ERROR)
2921 return ret;
2923 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2926 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2927 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2929 rhs = convert_to_ptrofftype_loc (loc, rhs);
2930 if (arith_code == MINUS_EXPR)
2931 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2932 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2934 else
2935 t1 = fold_convert (TREE_TYPE (*expr_p),
2936 fold_build2 (arith_code, arith_type,
2937 fold_convert (arith_type, lhs),
2938 fold_convert (arith_type, rhs)));
2940 if (postfix)
2942 gimplify_assign (lvalue, t1, pre_p);
2943 gimplify_seq_add_seq (orig_post_p, post);
2944 *expr_p = lhs;
2945 return GS_ALL_DONE;
2947 else
2949 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2950 return GS_OK;
2954 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2956 static void
2957 maybe_with_size_expr (tree *expr_p)
2959 tree expr = *expr_p;
2960 tree type = TREE_TYPE (expr);
2961 tree size;
2963 /* If we've already wrapped this or the type is error_mark_node, we can't do
2964 anything. */
2965 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2966 || type == error_mark_node)
2967 return;
2969 /* If the size isn't known or is a constant, we have nothing to do. */
2970 size = TYPE_SIZE_UNIT (type);
2971 if (!size || TREE_CODE (size) == INTEGER_CST)
2972 return;
2974 /* Otherwise, make a WITH_SIZE_EXPR. */
2975 size = unshare_expr (size);
2976 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2977 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2980 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2981 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2982 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
2983 gimplified to an SSA name. */
2985 enum gimplify_status
2986 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
2987 bool allow_ssa)
2989 bool (*test) (tree);
2990 fallback_t fb;
2992 /* In general, we allow lvalues for function arguments to avoid
2993 extra overhead of copying large aggregates out of even larger
2994 aggregates into temporaries only to copy the temporaries to
2995 the argument list. Make optimizers happy by pulling out to
2996 temporaries those types that fit in registers. */
2997 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2998 test = is_gimple_val, fb = fb_rvalue;
2999 else
3001 test = is_gimple_lvalue, fb = fb_either;
3002 /* Also strip a TARGET_EXPR that would force an extra copy. */
3003 if (TREE_CODE (*arg_p) == TARGET_EXPR)
3005 tree init = TARGET_EXPR_INITIAL (*arg_p);
3006 if (init
3007 && !VOID_TYPE_P (TREE_TYPE (init)))
3008 *arg_p = init;
3012 /* If this is a variable sized type, we must remember the size. */
3013 maybe_with_size_expr (arg_p);
3015 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
3016 /* Make sure arguments have the same location as the function call
3017 itself. */
3018 protected_set_expr_location (*arg_p, call_location);
3020 /* There is a sequence point before a function call. Side effects in
3021 the argument list must occur before the actual call. So, when
3022 gimplifying arguments, force gimplify_expr to use an internal
3023 post queue which is then appended to the end of PRE_P. */
3024 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
3027 /* Don't fold inside offloading or taskreg regions: it can break code by
3028 adding decl references that weren't in the source. We'll do it during
3029 omplower pass instead. */
3031 static bool
3032 maybe_fold_stmt (gimple_stmt_iterator *gsi)
3034 struct gimplify_omp_ctx *ctx;
3035 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
3036 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
3037 return false;
3038 return fold_stmt (gsi);
3041 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
3042 WANT_VALUE is true if the result of the call is desired. */
3044 static enum gimplify_status
3045 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
3047 tree fndecl, parms, p, fnptrtype;
3048 enum gimplify_status ret;
3049 int i, nargs;
3050 gcall *call;
3051 bool builtin_va_start_p = false;
3052 location_t loc = EXPR_LOCATION (*expr_p);
3054 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
3056 /* For reliable diagnostics during inlining, it is necessary that
3057 every call_expr be annotated with file and line. */
3058 if (! EXPR_HAS_LOCATION (*expr_p))
3059 SET_EXPR_LOCATION (*expr_p, input_location);
3061 /* Gimplify internal functions created in the FEs. */
3062 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
3064 if (want_value)
3065 return GS_ALL_DONE;
3067 nargs = call_expr_nargs (*expr_p);
3068 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
3069 auto_vec<tree> vargs (nargs);
3071 for (i = 0; i < nargs; i++)
3073 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3074 EXPR_LOCATION (*expr_p));
3075 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
3077 gimple *call = gimple_build_call_internal_vec (ifn, vargs);
3078 gimplify_seq_add_stmt (pre_p, call);
3079 return GS_ALL_DONE;
3082 /* This may be a call to a builtin function.
3084 Builtin function calls may be transformed into different
3085 (and more efficient) builtin function calls under certain
3086 circumstances. Unfortunately, gimplification can muck things
3087 up enough that the builtin expanders are not aware that certain
3088 transformations are still valid.
3090 So we attempt transformation/gimplification of the call before
3091 we gimplify the CALL_EXPR. At this time we do not manage to
3092 transform all calls in the same manner as the expanders do, but
3093 we do transform most of them. */
3094 fndecl = get_callee_fndecl (*expr_p);
3095 if (fndecl
3096 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3097 switch (DECL_FUNCTION_CODE (fndecl))
3099 case BUILT_IN_ALLOCA:
3100 case BUILT_IN_ALLOCA_WITH_ALIGN:
3101 /* If the call has been built for a variable-sized object, then we
3102 want to restore the stack level when the enclosing BIND_EXPR is
3103 exited to reclaim the allocated space; otherwise, we precisely
3104 need to do the opposite and preserve the latest stack level. */
3105 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
3106 gimplify_ctxp->save_stack = true;
3107 else
3108 gimplify_ctxp->keep_stack = true;
3109 break;
3111 case BUILT_IN_VA_START:
3113 builtin_va_start_p = TRUE;
3114 if (call_expr_nargs (*expr_p) < 2)
3116 error ("too few arguments to function %<va_start%>");
3117 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3118 return GS_OK;
3121 if (fold_builtin_next_arg (*expr_p, true))
3123 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
3124 return GS_OK;
3126 break;
3129 default:
3132 if (fndecl && DECL_BUILT_IN (fndecl))
3134 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3135 if (new_tree && new_tree != *expr_p)
3137 /* There was a transformation of this call which computes the
3138 same value, but in a more efficient way. Return and try
3139 again. */
3140 *expr_p = new_tree;
3141 return GS_OK;
3145 /* Remember the original function pointer type. */
3146 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
3148 /* There is a sequence point before the call, so any side effects in
3149 the calling expression must occur before the actual call. Force
3150 gimplify_expr to use an internal post queue. */
3151 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
3152 is_gimple_call_addr, fb_rvalue);
3154 nargs = call_expr_nargs (*expr_p);
3156 /* Get argument types for verification. */
3157 fndecl = get_callee_fndecl (*expr_p);
3158 parms = NULL_TREE;
3159 if (fndecl)
3160 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3161 else
3162 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3164 if (fndecl && DECL_ARGUMENTS (fndecl))
3165 p = DECL_ARGUMENTS (fndecl);
3166 else if (parms)
3167 p = parms;
3168 else
3169 p = NULL_TREE;
3170 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3173 /* If the last argument is __builtin_va_arg_pack () and it is not
3174 passed as a named argument, decrease the number of CALL_EXPR
3175 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3176 if (!p
3177 && i < nargs
3178 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3180 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3181 tree last_arg_fndecl = get_callee_fndecl (last_arg);
3183 if (last_arg_fndecl
3184 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
3185 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
3186 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
3188 tree call = *expr_p;
3190 --nargs;
3191 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3192 CALL_EXPR_FN (call),
3193 nargs, CALL_EXPR_ARGP (call));
3195 /* Copy all CALL_EXPR flags, location and block, except
3196 CALL_EXPR_VA_ARG_PACK flag. */
3197 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3198 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3199 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3200 = CALL_EXPR_RETURN_SLOT_OPT (call);
3201 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3202 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3204 /* Set CALL_EXPR_VA_ARG_PACK. */
3205 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3209 /* If the call returns twice then after building the CFG the call
3210 argument computations will no longer dominate the call because
3211 we add an abnormal incoming edge to the call. So do not use SSA
3212 vars there. */
3213 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3215 /* Gimplify the function arguments. */
3216 if (nargs > 0)
3218 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3219 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3220 PUSH_ARGS_REVERSED ? i-- : i++)
3222 enum gimplify_status t;
3224 /* Avoid gimplifying the second argument to va_start, which needs to
3225 be the plain PARM_DECL. */
3226 if ((i != 1) || !builtin_va_start_p)
3228 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3229 EXPR_LOCATION (*expr_p), ! returns_twice);
3231 if (t == GS_ERROR)
3232 ret = GS_ERROR;
3237 /* Gimplify the static chain. */
3238 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3240 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3241 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3242 else
3244 enum gimplify_status t;
3245 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3246 EXPR_LOCATION (*expr_p), ! returns_twice);
3247 if (t == GS_ERROR)
3248 ret = GS_ERROR;
3252 /* Verify the function result. */
3253 if (want_value && fndecl
3254 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3256 error_at (loc, "using result of function returning %<void%>");
3257 ret = GS_ERROR;
3260 /* Try this again in case gimplification exposed something. */
3261 if (ret != GS_ERROR)
3263 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3265 if (new_tree && new_tree != *expr_p)
3267 /* There was a transformation of this call which computes the
3268 same value, but in a more efficient way. Return and try
3269 again. */
3270 *expr_p = new_tree;
3271 return GS_OK;
3274 else
3276 *expr_p = error_mark_node;
3277 return GS_ERROR;
3280 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3281 decl. This allows us to eliminate redundant or useless
3282 calls to "const" functions. */
3283 if (TREE_CODE (*expr_p) == CALL_EXPR)
3285 int flags = call_expr_flags (*expr_p);
3286 if (flags & (ECF_CONST | ECF_PURE)
3287 /* An infinite loop is considered a side effect. */
3288 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3289 TREE_SIDE_EFFECTS (*expr_p) = 0;
3292 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3293 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3294 form and delegate the creation of a GIMPLE_CALL to
3295 gimplify_modify_expr. This is always possible because when
3296 WANT_VALUE is true, the caller wants the result of this call into
3297 a temporary, which means that we will emit an INIT_EXPR in
3298 internal_get_tmp_var which will then be handled by
3299 gimplify_modify_expr. */
3300 if (!want_value)
3302 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3303 have to do is replicate it as a GIMPLE_CALL tuple. */
3304 gimple_stmt_iterator gsi;
3305 call = gimple_build_call_from_tree (*expr_p);
3306 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
3307 notice_special_calls (call);
3308 gimplify_seq_add_stmt (pre_p, call);
3309 gsi = gsi_last (*pre_p);
3310 maybe_fold_stmt (&gsi);
3311 *expr_p = NULL_TREE;
3313 else
3314 /* Remember the original function type. */
3315 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3316 CALL_EXPR_FN (*expr_p));
3318 return ret;
3321 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3322 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3324 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3325 condition is true or false, respectively. If null, we should generate
3326 our own to skip over the evaluation of this specific expression.
3328 LOCUS is the source location of the COND_EXPR.
3330 This function is the tree equivalent of do_jump.
3332 shortcut_cond_r should only be called by shortcut_cond_expr. */
3334 static tree
3335 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3336 location_t locus)
3338 tree local_label = NULL_TREE;
3339 tree t, expr = NULL;
3341 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3342 retain the shortcut semantics. Just insert the gotos here;
3343 shortcut_cond_expr will append the real blocks later. */
3344 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3346 location_t new_locus;
3348 /* Turn if (a && b) into
3350 if (a); else goto no;
3351 if (b) goto yes; else goto no;
3352 (no:) */
3354 if (false_label_p == NULL)
3355 false_label_p = &local_label;
3357 /* Keep the original source location on the first 'if'. */
3358 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3359 append_to_statement_list (t, &expr);
3361 /* Set the source location of the && on the second 'if'. */
3362 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
3363 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3364 new_locus);
3365 append_to_statement_list (t, &expr);
3367 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3369 location_t new_locus;
3371 /* Turn if (a || b) into
3373 if (a) goto yes;
3374 if (b) goto yes; else goto no;
3375 (yes:) */
3377 if (true_label_p == NULL)
3378 true_label_p = &local_label;
3380 /* Keep the original source location on the first 'if'. */
3381 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3382 append_to_statement_list (t, &expr);
3384 /* Set the source location of the || on the second 'if'. */
3385 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
3386 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3387 new_locus);
3388 append_to_statement_list (t, &expr);
3390 else if (TREE_CODE (pred) == COND_EXPR
3391 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3392 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3394 location_t new_locus;
3396 /* As long as we're messing with gotos, turn if (a ? b : c) into
3397 if (a)
3398 if (b) goto yes; else goto no;
3399 else
3400 if (c) goto yes; else goto no;
3402 Don't do this if one of the arms has void type, which can happen
3403 in C++ when the arm is throw. */
3405 /* Keep the original source location on the first 'if'. Set the source
3406 location of the ? on the second 'if'. */
3407 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
3408 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3409 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3410 false_label_p, locus),
3411 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3412 false_label_p, new_locus));
3414 else
3416 expr = build3 (COND_EXPR, void_type_node, pred,
3417 build_and_jump (true_label_p),
3418 build_and_jump (false_label_p));
3419 SET_EXPR_LOCATION (expr, locus);
3422 if (local_label)
3424 t = build1 (LABEL_EXPR, void_type_node, local_label);
3425 append_to_statement_list (t, &expr);
3428 return expr;
3431 /* Given a conditional expression EXPR with short-circuit boolean
3432 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3433 predicate apart into the equivalent sequence of conditionals. */
3435 static tree
3436 shortcut_cond_expr (tree expr)
3438 tree pred = TREE_OPERAND (expr, 0);
3439 tree then_ = TREE_OPERAND (expr, 1);
3440 tree else_ = TREE_OPERAND (expr, 2);
3441 tree true_label, false_label, end_label, t;
3442 tree *true_label_p;
3443 tree *false_label_p;
3444 bool emit_end, emit_false, jump_over_else;
3445 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3446 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3448 /* First do simple transformations. */
3449 if (!else_se)
3451 /* If there is no 'else', turn
3452 if (a && b) then c
3453 into
3454 if (a) if (b) then c. */
3455 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3457 /* Keep the original source location on the first 'if'. */
3458 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3459 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3460 /* Set the source location of the && on the second 'if'. */
3461 if (EXPR_HAS_LOCATION (pred))
3462 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
3463 then_ = shortcut_cond_expr (expr);
3464 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3465 pred = TREE_OPERAND (pred, 0);
3466 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3467 SET_EXPR_LOCATION (expr, locus);
3471 if (!then_se)
3473 /* If there is no 'then', turn
3474 if (a || b); else d
3475 into
3476 if (a); else if (b); else d. */
3477 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3479 /* Keep the original source location on the first 'if'. */
3480 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3481 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3482 /* Set the source location of the || on the second 'if'. */
3483 if (EXPR_HAS_LOCATION (pred))
3484 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
3485 else_ = shortcut_cond_expr (expr);
3486 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3487 pred = TREE_OPERAND (pred, 0);
3488 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3489 SET_EXPR_LOCATION (expr, locus);
3493 /* If we're done, great. */
3494 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3495 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3496 return expr;
3498 /* Otherwise we need to mess with gotos. Change
3499 if (a) c; else d;
3501 if (a); else goto no;
3502 c; goto end;
3503 no: d; end:
3504 and recursively gimplify the condition. */
3506 true_label = false_label = end_label = NULL_TREE;
3508 /* If our arms just jump somewhere, hijack those labels so we don't
3509 generate jumps to jumps. */
3511 if (then_
3512 && TREE_CODE (then_) == GOTO_EXPR
3513 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
3515 true_label = GOTO_DESTINATION (then_);
3516 then_ = NULL;
3517 then_se = false;
3520 if (else_
3521 && TREE_CODE (else_) == GOTO_EXPR
3522 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
3524 false_label = GOTO_DESTINATION (else_);
3525 else_ = NULL;
3526 else_se = false;
3529 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3530 if (true_label)
3531 true_label_p = &true_label;
3532 else
3533 true_label_p = NULL;
3535 /* The 'else' branch also needs a label if it contains interesting code. */
3536 if (false_label || else_se)
3537 false_label_p = &false_label;
3538 else
3539 false_label_p = NULL;
3541 /* If there was nothing else in our arms, just forward the label(s). */
3542 if (!then_se && !else_se)
3543 return shortcut_cond_r (pred, true_label_p, false_label_p,
3544 EXPR_LOC_OR_LOC (expr, input_location));
3546 /* If our last subexpression already has a terminal label, reuse it. */
3547 if (else_se)
3548 t = expr_last (else_);
3549 else if (then_se)
3550 t = expr_last (then_);
3551 else
3552 t = NULL;
3553 if (t && TREE_CODE (t) == LABEL_EXPR)
3554 end_label = LABEL_EXPR_LABEL (t);
3556 /* If we don't care about jumping to the 'else' branch, jump to the end
3557 if the condition is false. */
3558 if (!false_label_p)
3559 false_label_p = &end_label;
3561 /* We only want to emit these labels if we aren't hijacking them. */
3562 emit_end = (end_label == NULL_TREE);
3563 emit_false = (false_label == NULL_TREE);
3565 /* We only emit the jump over the else clause if we have to--if the
3566 then clause may fall through. Otherwise we can wind up with a
3567 useless jump and a useless label at the end of gimplified code,
3568 which will cause us to think that this conditional as a whole
3569 falls through even if it doesn't. If we then inline a function
3570 which ends with such a condition, that can cause us to issue an
3571 inappropriate warning about control reaching the end of a
3572 non-void function. */
3573 jump_over_else = block_may_fallthru (then_);
3575 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3576 EXPR_LOC_OR_LOC (expr, input_location));
3578 expr = NULL;
3579 append_to_statement_list (pred, &expr);
3581 append_to_statement_list (then_, &expr);
3582 if (else_se)
3584 if (jump_over_else)
3586 tree last = expr_last (expr);
3587 t = build_and_jump (&end_label);
3588 if (EXPR_HAS_LOCATION (last))
3589 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
3590 append_to_statement_list (t, &expr);
3592 if (emit_false)
3594 t = build1 (LABEL_EXPR, void_type_node, false_label);
3595 append_to_statement_list (t, &expr);
3597 append_to_statement_list (else_, &expr);
3599 if (emit_end && end_label)
3601 t = build1 (LABEL_EXPR, void_type_node, end_label);
3602 append_to_statement_list (t, &expr);
3605 return expr;
3608 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3610 tree
3611 gimple_boolify (tree expr)
3613 tree type = TREE_TYPE (expr);
3614 location_t loc = EXPR_LOCATION (expr);
3616 if (TREE_CODE (expr) == NE_EXPR
3617 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3618 && integer_zerop (TREE_OPERAND (expr, 1)))
3620 tree call = TREE_OPERAND (expr, 0);
3621 tree fn = get_callee_fndecl (call);
3623 /* For __builtin_expect ((long) (x), y) recurse into x as well
3624 if x is truth_value_p. */
3625 if (fn
3626 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3627 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3628 && call_expr_nargs (call) == 2)
3630 tree arg = CALL_EXPR_ARG (call, 0);
3631 if (arg)
3633 if (TREE_CODE (arg) == NOP_EXPR
3634 && TREE_TYPE (arg) == TREE_TYPE (call))
3635 arg = TREE_OPERAND (arg, 0);
3636 if (truth_value_p (TREE_CODE (arg)))
3638 arg = gimple_boolify (arg);
3639 CALL_EXPR_ARG (call, 0)
3640 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3646 switch (TREE_CODE (expr))
3648 case TRUTH_AND_EXPR:
3649 case TRUTH_OR_EXPR:
3650 case TRUTH_XOR_EXPR:
3651 case TRUTH_ANDIF_EXPR:
3652 case TRUTH_ORIF_EXPR:
3653 /* Also boolify the arguments of truth exprs. */
3654 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3655 /* FALLTHRU */
3657 case TRUTH_NOT_EXPR:
3658 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3660 /* These expressions always produce boolean results. */
3661 if (TREE_CODE (type) != BOOLEAN_TYPE)
3662 TREE_TYPE (expr) = boolean_type_node;
3663 return expr;
3665 case ANNOTATE_EXPR:
3666 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3668 case annot_expr_ivdep_kind:
3669 case annot_expr_no_vector_kind:
3670 case annot_expr_vector_kind:
3671 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3672 if (TREE_CODE (type) != BOOLEAN_TYPE)
3673 TREE_TYPE (expr) = boolean_type_node;
3674 return expr;
3675 default:
3676 gcc_unreachable ();
3679 default:
3680 if (COMPARISON_CLASS_P (expr))
3682 /* There expressions always prduce boolean results. */
3683 if (TREE_CODE (type) != BOOLEAN_TYPE)
3684 TREE_TYPE (expr) = boolean_type_node;
3685 return expr;
3687 /* Other expressions that get here must have boolean values, but
3688 might need to be converted to the appropriate mode. */
3689 if (TREE_CODE (type) == BOOLEAN_TYPE)
3690 return expr;
3691 return fold_convert_loc (loc, boolean_type_node, expr);
3695 /* Given a conditional expression *EXPR_P without side effects, gimplify
3696 its operands. New statements are inserted to PRE_P. */
3698 static enum gimplify_status
3699 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3701 tree expr = *expr_p, cond;
3702 enum gimplify_status ret, tret;
3703 enum tree_code code;
3705 cond = gimple_boolify (COND_EXPR_COND (expr));
3707 /* We need to handle && and || specially, as their gimplification
3708 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3709 code = TREE_CODE (cond);
3710 if (code == TRUTH_ANDIF_EXPR)
3711 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3712 else if (code == TRUTH_ORIF_EXPR)
3713 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3714 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3715 COND_EXPR_COND (*expr_p) = cond;
3717 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3718 is_gimple_val, fb_rvalue);
3719 ret = MIN (ret, tret);
3720 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3721 is_gimple_val, fb_rvalue);
3723 return MIN (ret, tret);
3726 /* Return true if evaluating EXPR could trap.
3727 EXPR is GENERIC, while tree_could_trap_p can be called
3728 only on GIMPLE. */
3730 static bool
3731 generic_expr_could_trap_p (tree expr)
3733 unsigned i, n;
3735 if (!expr || is_gimple_val (expr))
3736 return false;
3738 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3739 return true;
3741 n = TREE_OPERAND_LENGTH (expr);
3742 for (i = 0; i < n; i++)
3743 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3744 return true;
3746 return false;
3749 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3750 into
3752 if (p) if (p)
3753 t1 = a; a;
3754 else or else
3755 t1 = b; b;
3758 The second form is used when *EXPR_P is of type void.
3760 PRE_P points to the list where side effects that must happen before
3761 *EXPR_P should be stored. */
3763 static enum gimplify_status
3764 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3766 tree expr = *expr_p;
3767 tree type = TREE_TYPE (expr);
3768 location_t loc = EXPR_LOCATION (expr);
3769 tree tmp, arm1, arm2;
3770 enum gimplify_status ret;
3771 tree label_true, label_false, label_cont;
3772 bool have_then_clause_p, have_else_clause_p;
3773 gcond *cond_stmt;
3774 enum tree_code pred_code;
3775 gimple_seq seq = NULL;
3777 /* If this COND_EXPR has a value, copy the values into a temporary within
3778 the arms. */
3779 if (!VOID_TYPE_P (type))
3781 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3782 tree result;
3784 /* If either an rvalue is ok or we do not require an lvalue, create the
3785 temporary. But we cannot do that if the type is addressable. */
3786 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3787 && !TREE_ADDRESSABLE (type))
3789 if (gimplify_ctxp->allow_rhs_cond_expr
3790 /* If either branch has side effects or could trap, it can't be
3791 evaluated unconditionally. */
3792 && !TREE_SIDE_EFFECTS (then_)
3793 && !generic_expr_could_trap_p (then_)
3794 && !TREE_SIDE_EFFECTS (else_)
3795 && !generic_expr_could_trap_p (else_))
3796 return gimplify_pure_cond_expr (expr_p, pre_p);
3798 tmp = create_tmp_var (type, "iftmp");
3799 result = tmp;
3802 /* Otherwise, only create and copy references to the values. */
3803 else
3805 type = build_pointer_type (type);
3807 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3808 then_ = build_fold_addr_expr_loc (loc, then_);
3810 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3811 else_ = build_fold_addr_expr_loc (loc, else_);
3813 expr
3814 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3816 tmp = create_tmp_var (type, "iftmp");
3817 result = build_simple_mem_ref_loc (loc, tmp);
3820 /* Build the new then clause, `tmp = then_;'. But don't build the
3821 assignment if the value is void; in C++ it can be if it's a throw. */
3822 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3823 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3825 /* Similarly, build the new else clause, `tmp = else_;'. */
3826 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3827 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3829 TREE_TYPE (expr) = void_type_node;
3830 recalculate_side_effects (expr);
3832 /* Move the COND_EXPR to the prequeue. */
3833 gimplify_stmt (&expr, pre_p);
3835 *expr_p = result;
3836 return GS_ALL_DONE;
3839 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3840 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3841 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3842 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3844 /* Make sure the condition has BOOLEAN_TYPE. */
3845 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3847 /* Break apart && and || conditions. */
3848 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3849 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3851 expr = shortcut_cond_expr (expr);
3853 if (expr != *expr_p)
3855 *expr_p = expr;
3857 /* We can't rely on gimplify_expr to re-gimplify the expanded
3858 form properly, as cleanups might cause the target labels to be
3859 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3860 set up a conditional context. */
3861 gimple_push_condition ();
3862 gimplify_stmt (expr_p, &seq);
3863 gimple_pop_condition (pre_p);
3864 gimple_seq_add_seq (pre_p, seq);
3866 return GS_ALL_DONE;
3870 /* Now do the normal gimplification. */
3872 /* Gimplify condition. */
3873 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3874 fb_rvalue);
3875 if (ret == GS_ERROR)
3876 return GS_ERROR;
3877 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3879 gimple_push_condition ();
3881 have_then_clause_p = have_else_clause_p = false;
3882 if (TREE_OPERAND (expr, 1) != NULL
3883 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3884 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3885 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3886 == current_function_decl)
3887 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3888 have different locations, otherwise we end up with incorrect
3889 location information on the branches. */
3890 && (optimize
3891 || !EXPR_HAS_LOCATION (expr)
3892 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3893 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3895 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3896 have_then_clause_p = true;
3898 else
3899 label_true = create_artificial_label (UNKNOWN_LOCATION);
3900 if (TREE_OPERAND (expr, 2) != NULL
3901 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3902 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3903 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3904 == current_function_decl)
3905 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3906 have different locations, otherwise we end up with incorrect
3907 location information on the branches. */
3908 && (optimize
3909 || !EXPR_HAS_LOCATION (expr)
3910 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3911 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3913 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3914 have_else_clause_p = true;
3916 else
3917 label_false = create_artificial_label (UNKNOWN_LOCATION);
3919 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3920 &arm2);
3921 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3922 label_false);
3923 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
3924 gimplify_seq_add_stmt (&seq, cond_stmt);
3925 gimple_stmt_iterator gsi = gsi_last (seq);
3926 maybe_fold_stmt (&gsi);
3928 label_cont = NULL_TREE;
3929 if (!have_then_clause_p)
3931 /* For if (...) {} else { code; } put label_true after
3932 the else block. */
3933 if (TREE_OPERAND (expr, 1) == NULL_TREE
3934 && !have_else_clause_p
3935 && TREE_OPERAND (expr, 2) != NULL_TREE)
3936 label_cont = label_true;
3937 else
3939 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3940 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3941 /* For if (...) { code; } else {} or
3942 if (...) { code; } else goto label; or
3943 if (...) { code; return; } else { ... }
3944 label_cont isn't needed. */
3945 if (!have_else_clause_p
3946 && TREE_OPERAND (expr, 2) != NULL_TREE
3947 && gimple_seq_may_fallthru (seq))
3949 gimple *g;
3950 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3952 g = gimple_build_goto (label_cont);
3954 /* GIMPLE_COND's are very low level; they have embedded
3955 gotos. This particular embedded goto should not be marked
3956 with the location of the original COND_EXPR, as it would
3957 correspond to the COND_EXPR's condition, not the ELSE or the
3958 THEN arms. To avoid marking it with the wrong location, flag
3959 it as "no location". */
3960 gimple_set_do_not_emit_location (g);
3962 gimplify_seq_add_stmt (&seq, g);
3966 if (!have_else_clause_p)
3968 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3969 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3971 if (label_cont)
3972 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3974 gimple_pop_condition (pre_p);
3975 gimple_seq_add_seq (pre_p, seq);
3977 if (ret == GS_ERROR)
3978 ; /* Do nothing. */
3979 else if (have_then_clause_p || have_else_clause_p)
3980 ret = GS_ALL_DONE;
3981 else
3983 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3984 expr = TREE_OPERAND (expr, 0);
3985 gimplify_stmt (&expr, pre_p);
3988 *expr_p = NULL;
3989 return ret;
3992 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3993 to be marked addressable.
3995 We cannot rely on such an expression being directly markable if a temporary
3996 has been created by the gimplification. In this case, we create another
3997 temporary and initialize it with a copy, which will become a store after we
3998 mark it addressable. This can happen if the front-end passed us something
3999 that it could not mark addressable yet, like a Fortran pass-by-reference
4000 parameter (int) floatvar. */
4002 static void
4003 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
4005 while (handled_component_p (*expr_p))
4006 expr_p = &TREE_OPERAND (*expr_p, 0);
4007 if (is_gimple_reg (*expr_p))
4009 /* Do not allow an SSA name as the temporary. */
4010 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
4011 DECL_GIMPLE_REG_P (var) = 0;
4012 *expr_p = var;
4016 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4017 a call to __builtin_memcpy. */
4019 static enum gimplify_status
4020 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
4021 gimple_seq *seq_p)
4023 tree t, to, to_ptr, from, from_ptr;
4024 gcall *gs;
4025 location_t loc = EXPR_LOCATION (*expr_p);
4027 to = TREE_OPERAND (*expr_p, 0);
4028 from = TREE_OPERAND (*expr_p, 1);
4030 /* Mark the RHS addressable. Beware that it may not be possible to do so
4031 directly if a temporary has been created by the gimplification. */
4032 prepare_gimple_addressable (&from, seq_p);
4034 mark_addressable (from);
4035 from_ptr = build_fold_addr_expr_loc (loc, from);
4036 gimplify_arg (&from_ptr, seq_p, loc);
4038 mark_addressable (to);
4039 to_ptr = build_fold_addr_expr_loc (loc, to);
4040 gimplify_arg (&to_ptr, seq_p, loc);
4042 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
4044 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
4046 if (want_value)
4048 /* tmp = memcpy() */
4049 t = create_tmp_var (TREE_TYPE (to_ptr));
4050 gimple_call_set_lhs (gs, t);
4051 gimplify_seq_add_stmt (seq_p, gs);
4053 *expr_p = build_simple_mem_ref (t);
4054 return GS_ALL_DONE;
4057 gimplify_seq_add_stmt (seq_p, gs);
4058 *expr_p = NULL;
4059 return GS_ALL_DONE;
4062 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
4063 a call to __builtin_memset. In this case we know that the RHS is
4064 a CONSTRUCTOR with an empty element list. */
4066 static enum gimplify_status
4067 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
4068 gimple_seq *seq_p)
4070 tree t, from, to, to_ptr;
4071 gcall *gs;
4072 location_t loc = EXPR_LOCATION (*expr_p);
4074 /* Assert our assumptions, to abort instead of producing wrong code
4075 silently if they are not met. Beware that the RHS CONSTRUCTOR might
4076 not be immediately exposed. */
4077 from = TREE_OPERAND (*expr_p, 1);
4078 if (TREE_CODE (from) == WITH_SIZE_EXPR)
4079 from = TREE_OPERAND (from, 0);
4081 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
4082 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
4084 /* Now proceed. */
4085 to = TREE_OPERAND (*expr_p, 0);
4087 to_ptr = build_fold_addr_expr_loc (loc, to);
4088 gimplify_arg (&to_ptr, seq_p, loc);
4089 t = builtin_decl_implicit (BUILT_IN_MEMSET);
4091 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
4093 if (want_value)
4095 /* tmp = memset() */
4096 t = create_tmp_var (TREE_TYPE (to_ptr));
4097 gimple_call_set_lhs (gs, t);
4098 gimplify_seq_add_stmt (seq_p, gs);
4100 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
4101 return GS_ALL_DONE;
4104 gimplify_seq_add_stmt (seq_p, gs);
4105 *expr_p = NULL;
4106 return GS_ALL_DONE;
4109 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
4110 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
4111 assignment. Return non-null if we detect a potential overlap. */
4113 struct gimplify_init_ctor_preeval_data
4115 /* The base decl of the lhs object. May be NULL, in which case we
4116 have to assume the lhs is indirect. */
4117 tree lhs_base_decl;
4119 /* The alias set of the lhs object. */
4120 alias_set_type lhs_alias_set;
4123 static tree
4124 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
4126 struct gimplify_init_ctor_preeval_data *data
4127 = (struct gimplify_init_ctor_preeval_data *) xdata;
4128 tree t = *tp;
4130 /* If we find the base object, obviously we have overlap. */
4131 if (data->lhs_base_decl == t)
4132 return t;
4134 /* If the constructor component is indirect, determine if we have a
4135 potential overlap with the lhs. The only bits of information we
4136 have to go on at this point are addressability and alias sets. */
4137 if ((INDIRECT_REF_P (t)
4138 || TREE_CODE (t) == MEM_REF)
4139 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4140 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
4141 return t;
4143 /* If the constructor component is a call, determine if it can hide a
4144 potential overlap with the lhs through an INDIRECT_REF like above.
4145 ??? Ugh - this is completely broken. In fact this whole analysis
4146 doesn't look conservative. */
4147 if (TREE_CODE (t) == CALL_EXPR)
4149 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
4151 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
4152 if (POINTER_TYPE_P (TREE_VALUE (type))
4153 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
4154 && alias_sets_conflict_p (data->lhs_alias_set,
4155 get_alias_set
4156 (TREE_TYPE (TREE_VALUE (type)))))
4157 return t;
4160 if (IS_TYPE_OR_DECL_P (t))
4161 *walk_subtrees = 0;
4162 return NULL;
4165 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4166 force values that overlap with the lhs (as described by *DATA)
4167 into temporaries. */
4169 static void
4170 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4171 struct gimplify_init_ctor_preeval_data *data)
4173 enum gimplify_status one;
4175 /* If the value is constant, then there's nothing to pre-evaluate. */
4176 if (TREE_CONSTANT (*expr_p))
4178 /* Ensure it does not have side effects, it might contain a reference to
4179 the object we're initializing. */
4180 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4181 return;
4184 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4185 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4186 return;
4188 /* Recurse for nested constructors. */
4189 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4191 unsigned HOST_WIDE_INT ix;
4192 constructor_elt *ce;
4193 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4195 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4196 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4198 return;
4201 /* If this is a variable sized type, we must remember the size. */
4202 maybe_with_size_expr (expr_p);
4204 /* Gimplify the constructor element to something appropriate for the rhs
4205 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4206 the gimplifier will consider this a store to memory. Doing this
4207 gimplification now means that we won't have to deal with complicated
4208 language-specific trees, nor trees like SAVE_EXPR that can induce
4209 exponential search behavior. */
4210 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4211 if (one == GS_ERROR)
4213 *expr_p = NULL;
4214 return;
4217 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4218 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4219 always be true for all scalars, since is_gimple_mem_rhs insists on a
4220 temporary variable for them. */
4221 if (DECL_P (*expr_p))
4222 return;
4224 /* If this is of variable size, we have no choice but to assume it doesn't
4225 overlap since we can't make a temporary for it. */
4226 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4227 return;
4229 /* Otherwise, we must search for overlap ... */
4230 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4231 return;
4233 /* ... and if found, force the value into a temporary. */
4234 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4237 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4238 a RANGE_EXPR in a CONSTRUCTOR for an array.
4240 var = lower;
4241 loop_entry:
4242 object[var] = value;
4243 if (var == upper)
4244 goto loop_exit;
4245 var = var + 1;
4246 goto loop_entry;
4247 loop_exit:
4249 We increment var _after_ the loop exit check because we might otherwise
4250 fail if upper == TYPE_MAX_VALUE (type for upper).
4252 Note that we never have to deal with SAVE_EXPRs here, because this has
4253 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4255 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4256 gimple_seq *, bool);
4258 static void
4259 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4260 tree value, tree array_elt_type,
4261 gimple_seq *pre_p, bool cleared)
4263 tree loop_entry_label, loop_exit_label, fall_thru_label;
4264 tree var, var_type, cref, tmp;
4266 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4267 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4268 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4270 /* Create and initialize the index variable. */
4271 var_type = TREE_TYPE (upper);
4272 var = create_tmp_var (var_type);
4273 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4275 /* Add the loop entry label. */
4276 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4278 /* Build the reference. */
4279 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4280 var, NULL_TREE, NULL_TREE);
4282 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4283 the store. Otherwise just assign value to the reference. */
4285 if (TREE_CODE (value) == CONSTRUCTOR)
4286 /* NB we might have to call ourself recursively through
4287 gimplify_init_ctor_eval if the value is a constructor. */
4288 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4289 pre_p, cleared);
4290 else
4291 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4293 /* We exit the loop when the index var is equal to the upper bound. */
4294 gimplify_seq_add_stmt (pre_p,
4295 gimple_build_cond (EQ_EXPR, var, upper,
4296 loop_exit_label, fall_thru_label));
4298 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4300 /* Otherwise, increment the index var... */
4301 tmp = build2 (PLUS_EXPR, var_type, var,
4302 fold_convert (var_type, integer_one_node));
4303 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4305 /* ...and jump back to the loop entry. */
4306 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4308 /* Add the loop exit label. */
4309 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4312 /* Return true if FDECL is accessing a field that is zero sized. */
4314 static bool
4315 zero_sized_field_decl (const_tree fdecl)
4317 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4318 && integer_zerop (DECL_SIZE (fdecl)))
4319 return true;
4320 return false;
4323 /* Return true if TYPE is zero sized. */
4325 static bool
4326 zero_sized_type (const_tree type)
4328 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4329 && integer_zerop (TYPE_SIZE (type)))
4330 return true;
4331 return false;
4334 /* A subroutine of gimplify_init_constructor. Generate individual
4335 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4336 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4337 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4338 zeroed first. */
4340 static void
4341 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4342 gimple_seq *pre_p, bool cleared)
4344 tree array_elt_type = NULL;
4345 unsigned HOST_WIDE_INT ix;
4346 tree purpose, value;
4348 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4349 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4351 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4353 tree cref;
4355 /* NULL values are created above for gimplification errors. */
4356 if (value == NULL)
4357 continue;
4359 if (cleared && initializer_zerop (value))
4360 continue;
4362 /* ??? Here's to hoping the front end fills in all of the indices,
4363 so we don't have to figure out what's missing ourselves. */
4364 gcc_assert (purpose);
4366 /* Skip zero-sized fields, unless value has side-effects. This can
4367 happen with calls to functions returning a zero-sized type, which
4368 we shouldn't discard. As a number of downstream passes don't
4369 expect sets of zero-sized fields, we rely on the gimplification of
4370 the MODIFY_EXPR we make below to drop the assignment statement. */
4371 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4372 continue;
4374 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4375 whole range. */
4376 if (TREE_CODE (purpose) == RANGE_EXPR)
4378 tree lower = TREE_OPERAND (purpose, 0);
4379 tree upper = TREE_OPERAND (purpose, 1);
4381 /* If the lower bound is equal to upper, just treat it as if
4382 upper was the index. */
4383 if (simple_cst_equal (lower, upper))
4384 purpose = upper;
4385 else
4387 gimplify_init_ctor_eval_range (object, lower, upper, value,
4388 array_elt_type, pre_p, cleared);
4389 continue;
4393 if (array_elt_type)
4395 /* Do not use bitsizetype for ARRAY_REF indices. */
4396 if (TYPE_DOMAIN (TREE_TYPE (object)))
4397 purpose
4398 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4399 purpose);
4400 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4401 purpose, NULL_TREE, NULL_TREE);
4403 else
4405 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4406 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4407 unshare_expr (object), purpose, NULL_TREE);
4410 if (TREE_CODE (value) == CONSTRUCTOR
4411 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4412 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4413 pre_p, cleared);
4414 else
4416 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4417 gimplify_and_add (init, pre_p);
4418 ggc_free (init);
4423 /* Return the appropriate RHS predicate for this LHS. */
4425 gimple_predicate
4426 rhs_predicate_for (tree lhs)
4428 if (is_gimple_reg (lhs))
4429 return is_gimple_reg_rhs_or_call;
4430 else
4431 return is_gimple_mem_rhs_or_call;
4434 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4435 before the LHS has been gimplified. */
4437 static gimple_predicate
4438 initial_rhs_predicate_for (tree lhs)
4440 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4441 return is_gimple_reg_rhs_or_call;
4442 else
4443 return is_gimple_mem_rhs_or_call;
4446 /* Gimplify a C99 compound literal expression. This just means adding
4447 the DECL_EXPR before the current statement and using its anonymous
4448 decl instead. */
4450 static enum gimplify_status
4451 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4452 bool (*gimple_test_f) (tree),
4453 fallback_t fallback)
4455 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4456 tree decl = DECL_EXPR_DECL (decl_s);
4457 tree init = DECL_INITIAL (decl);
4458 /* Mark the decl as addressable if the compound literal
4459 expression is addressable now, otherwise it is marked too late
4460 after we gimplify the initialization expression. */
4461 if (TREE_ADDRESSABLE (*expr_p))
4462 TREE_ADDRESSABLE (decl) = 1;
4463 /* Otherwise, if we don't need an lvalue and have a literal directly
4464 substitute it. Check if it matches the gimple predicate, as
4465 otherwise we'd generate a new temporary, and we can as well just
4466 use the decl we already have. */
4467 else if (!TREE_ADDRESSABLE (decl)
4468 && init
4469 && (fallback & fb_lvalue) == 0
4470 && gimple_test_f (init))
4472 *expr_p = init;
4473 return GS_OK;
4476 /* Preliminarily mark non-addressed complex variables as eligible
4477 for promotion to gimple registers. We'll transform their uses
4478 as we find them. */
4479 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4480 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4481 && !TREE_THIS_VOLATILE (decl)
4482 && !needs_to_live_in_memory (decl))
4483 DECL_GIMPLE_REG_P (decl) = 1;
4485 /* If the decl is not addressable, then it is being used in some
4486 expression or on the right hand side of a statement, and it can
4487 be put into a readonly data section. */
4488 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4489 TREE_READONLY (decl) = 1;
4491 /* This decl isn't mentioned in the enclosing block, so add it to the
4492 list of temps. FIXME it seems a bit of a kludge to say that
4493 anonymous artificial vars aren't pushed, but everything else is. */
4494 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4495 gimple_add_tmp_var (decl);
4497 gimplify_and_add (decl_s, pre_p);
4498 *expr_p = decl;
4499 return GS_OK;
4502 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4503 return a new CONSTRUCTOR if something changed. */
4505 static tree
4506 optimize_compound_literals_in_ctor (tree orig_ctor)
4508 tree ctor = orig_ctor;
4509 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4510 unsigned int idx, num = vec_safe_length (elts);
4512 for (idx = 0; idx < num; idx++)
4514 tree value = (*elts)[idx].value;
4515 tree newval = value;
4516 if (TREE_CODE (value) == CONSTRUCTOR)
4517 newval = optimize_compound_literals_in_ctor (value);
4518 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4520 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4521 tree decl = DECL_EXPR_DECL (decl_s);
4522 tree init = DECL_INITIAL (decl);
4524 if (!TREE_ADDRESSABLE (value)
4525 && !TREE_ADDRESSABLE (decl)
4526 && init
4527 && TREE_CODE (init) == CONSTRUCTOR)
4528 newval = optimize_compound_literals_in_ctor (init);
4530 if (newval == value)
4531 continue;
4533 if (ctor == orig_ctor)
4535 ctor = copy_node (orig_ctor);
4536 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4537 elts = CONSTRUCTOR_ELTS (ctor);
4539 (*elts)[idx].value = newval;
4541 return ctor;
4544 /* A subroutine of gimplify_modify_expr. Break out elements of a
4545 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4547 Note that we still need to clear any elements that don't have explicit
4548 initializers, so if not all elements are initialized we keep the
4549 original MODIFY_EXPR, we just remove all of the constructor elements.
4551 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4552 GS_ERROR if we would have to create a temporary when gimplifying
4553 this constructor. Otherwise, return GS_OK.
4555 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4557 static enum gimplify_status
4558 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4559 bool want_value, bool notify_temp_creation)
4561 tree object, ctor, type;
4562 enum gimplify_status ret;
4563 vec<constructor_elt, va_gc> *elts;
4565 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4567 if (!notify_temp_creation)
4569 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4570 is_gimple_lvalue, fb_lvalue);
4571 if (ret == GS_ERROR)
4572 return ret;
4575 object = TREE_OPERAND (*expr_p, 0);
4576 ctor = TREE_OPERAND (*expr_p, 1) =
4577 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4578 type = TREE_TYPE (ctor);
4579 elts = CONSTRUCTOR_ELTS (ctor);
4580 ret = GS_ALL_DONE;
4582 switch (TREE_CODE (type))
4584 case RECORD_TYPE:
4585 case UNION_TYPE:
4586 case QUAL_UNION_TYPE:
4587 case ARRAY_TYPE:
4589 struct gimplify_init_ctor_preeval_data preeval_data;
4590 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4591 bool cleared, complete_p, valid_const_initializer;
4593 /* Aggregate types must lower constructors to initialization of
4594 individual elements. The exception is that a CONSTRUCTOR node
4595 with no elements indicates zero-initialization of the whole. */
4596 if (vec_safe_is_empty (elts))
4598 if (notify_temp_creation)
4599 return GS_OK;
4600 break;
4603 /* Fetch information about the constructor to direct later processing.
4604 We might want to make static versions of it in various cases, and
4605 can only do so if it known to be a valid constant initializer. */
4606 valid_const_initializer
4607 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4608 &num_ctor_elements, &complete_p);
4610 /* If a const aggregate variable is being initialized, then it
4611 should never be a lose to promote the variable to be static. */
4612 if (valid_const_initializer
4613 && num_nonzero_elements > 1
4614 && TREE_READONLY (object)
4615 && VAR_P (object)
4616 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4618 if (notify_temp_creation)
4619 return GS_ERROR;
4620 DECL_INITIAL (object) = ctor;
4621 TREE_STATIC (object) = 1;
4622 if (!DECL_NAME (object))
4623 DECL_NAME (object) = create_tmp_var_name ("C");
4624 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4626 /* ??? C++ doesn't automatically append a .<number> to the
4627 assembler name, and even when it does, it looks at FE private
4628 data structures to figure out what that number should be,
4629 which are not set for this variable. I suppose this is
4630 important for local statics for inline functions, which aren't
4631 "local" in the object file sense. So in order to get a unique
4632 TU-local symbol, we must invoke the lhd version now. */
4633 lhd_set_decl_assembler_name (object);
4635 *expr_p = NULL_TREE;
4636 break;
4639 /* If there are "lots" of initialized elements, even discounting
4640 those that are not address constants (and thus *must* be
4641 computed at runtime), then partition the constructor into
4642 constant and non-constant parts. Block copy the constant
4643 parts in, then generate code for the non-constant parts. */
4644 /* TODO. There's code in cp/typeck.c to do this. */
4646 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4647 /* store_constructor will ignore the clearing of variable-sized
4648 objects. Initializers for such objects must explicitly set
4649 every field that needs to be set. */
4650 cleared = false;
4651 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
4652 /* If the constructor isn't complete, clear the whole object
4653 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4655 ??? This ought not to be needed. For any element not present
4656 in the initializer, we should simply set them to zero. Except
4657 we'd need to *find* the elements that are not present, and that
4658 requires trickery to avoid quadratic compile-time behavior in
4659 large cases or excessive memory use in small cases. */
4660 cleared = true;
4661 else if (num_ctor_elements - num_nonzero_elements
4662 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4663 && num_nonzero_elements < num_ctor_elements / 4)
4664 /* If there are "lots" of zeros, it's more efficient to clear
4665 the memory and then set the nonzero elements. */
4666 cleared = true;
4667 else
4668 cleared = false;
4670 /* If there are "lots" of initialized elements, and all of them
4671 are valid address constants, then the entire initializer can
4672 be dropped to memory, and then memcpy'd out. Don't do this
4673 for sparse arrays, though, as it's more efficient to follow
4674 the standard CONSTRUCTOR behavior of memset followed by
4675 individual element initialization. Also don't do this for small
4676 all-zero initializers (which aren't big enough to merit
4677 clearing), and don't try to make bitwise copies of
4678 TREE_ADDRESSABLE types.
4680 We cannot apply such transformation when compiling chkp static
4681 initializer because creation of initializer image in the memory
4682 will require static initialization of bounds for it. It should
4683 result in another gimplification of similar initializer and we
4684 may fall into infinite loop. */
4685 if (valid_const_initializer
4686 && !(cleared || num_nonzero_elements == 0)
4687 && !TREE_ADDRESSABLE (type)
4688 && (!current_function_decl
4689 || !lookup_attribute ("chkp ctor",
4690 DECL_ATTRIBUTES (current_function_decl))))
4692 HOST_WIDE_INT size = int_size_in_bytes (type);
4693 unsigned int align;
4695 /* ??? We can still get unbounded array types, at least
4696 from the C++ front end. This seems wrong, but attempt
4697 to work around it for now. */
4698 if (size < 0)
4700 size = int_size_in_bytes (TREE_TYPE (object));
4701 if (size >= 0)
4702 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4705 /* Find the maximum alignment we can assume for the object. */
4706 /* ??? Make use of DECL_OFFSET_ALIGN. */
4707 if (DECL_P (object))
4708 align = DECL_ALIGN (object);
4709 else
4710 align = TYPE_ALIGN (type);
4712 /* Do a block move either if the size is so small as to make
4713 each individual move a sub-unit move on average, or if it
4714 is so large as to make individual moves inefficient. */
4715 if (size > 0
4716 && num_nonzero_elements > 1
4717 && (size < num_nonzero_elements
4718 || !can_move_by_pieces (size, align)))
4720 if (notify_temp_creation)
4721 return GS_ERROR;
4723 walk_tree (&ctor, force_labels_r, NULL, NULL);
4724 ctor = tree_output_constant_def (ctor);
4725 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4726 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4727 TREE_OPERAND (*expr_p, 1) = ctor;
4729 /* This is no longer an assignment of a CONSTRUCTOR, but
4730 we still may have processing to do on the LHS. So
4731 pretend we didn't do anything here to let that happen. */
4732 return GS_UNHANDLED;
4736 /* If the target is volatile, we have non-zero elements and more than
4737 one field to assign, initialize the target from a temporary. */
4738 if (TREE_THIS_VOLATILE (object)
4739 && !TREE_ADDRESSABLE (type)
4740 && num_nonzero_elements > 0
4741 && vec_safe_length (elts) > 1)
4743 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4744 TREE_OPERAND (*expr_p, 0) = temp;
4745 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4746 *expr_p,
4747 build2 (MODIFY_EXPR, void_type_node,
4748 object, temp));
4749 return GS_OK;
4752 if (notify_temp_creation)
4753 return GS_OK;
4755 /* If there are nonzero elements and if needed, pre-evaluate to capture
4756 elements overlapping with the lhs into temporaries. We must do this
4757 before clearing to fetch the values before they are zeroed-out. */
4758 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4760 preeval_data.lhs_base_decl = get_base_address (object);
4761 if (!DECL_P (preeval_data.lhs_base_decl))
4762 preeval_data.lhs_base_decl = NULL;
4763 preeval_data.lhs_alias_set = get_alias_set (object);
4765 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4766 pre_p, post_p, &preeval_data);
4769 bool ctor_has_side_effects_p
4770 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4772 if (cleared)
4774 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4775 Note that we still have to gimplify, in order to handle the
4776 case of variable sized types. Avoid shared tree structures. */
4777 CONSTRUCTOR_ELTS (ctor) = NULL;
4778 TREE_SIDE_EFFECTS (ctor) = 0;
4779 object = unshare_expr (object);
4780 gimplify_stmt (expr_p, pre_p);
4783 /* If we have not block cleared the object, or if there are nonzero
4784 elements in the constructor, or if the constructor has side effects,
4785 add assignments to the individual scalar fields of the object. */
4786 if (!cleared
4787 || num_nonzero_elements > 0
4788 || ctor_has_side_effects_p)
4789 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4791 *expr_p = NULL_TREE;
4793 break;
4795 case COMPLEX_TYPE:
4797 tree r, i;
4799 if (notify_temp_creation)
4800 return GS_OK;
4802 /* Extract the real and imaginary parts out of the ctor. */
4803 gcc_assert (elts->length () == 2);
4804 r = (*elts)[0].value;
4805 i = (*elts)[1].value;
4806 if (r == NULL || i == NULL)
4808 tree zero = build_zero_cst (TREE_TYPE (type));
4809 if (r == NULL)
4810 r = zero;
4811 if (i == NULL)
4812 i = zero;
4815 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4816 represent creation of a complex value. */
4817 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4819 ctor = build_complex (type, r, i);
4820 TREE_OPERAND (*expr_p, 1) = ctor;
4822 else
4824 ctor = build2 (COMPLEX_EXPR, type, r, i);
4825 TREE_OPERAND (*expr_p, 1) = ctor;
4826 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4827 pre_p,
4828 post_p,
4829 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4830 fb_rvalue);
4833 break;
4835 case VECTOR_TYPE:
4837 unsigned HOST_WIDE_INT ix;
4838 constructor_elt *ce;
4840 if (notify_temp_creation)
4841 return GS_OK;
4843 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4844 if (TREE_CONSTANT (ctor))
4846 bool constant_p = true;
4847 tree value;
4849 /* Even when ctor is constant, it might contain non-*_CST
4850 elements, such as addresses or trapping values like
4851 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4852 in VECTOR_CST nodes. */
4853 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4854 if (!CONSTANT_CLASS_P (value))
4856 constant_p = false;
4857 break;
4860 if (constant_p)
4862 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4863 break;
4866 TREE_CONSTANT (ctor) = 0;
4869 /* Vector types use CONSTRUCTOR all the way through gimple
4870 compilation as a general initializer. */
4871 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4873 enum gimplify_status tret;
4874 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4875 fb_rvalue);
4876 if (tret == GS_ERROR)
4877 ret = GS_ERROR;
4878 else if (TREE_STATIC (ctor)
4879 && !initializer_constant_valid_p (ce->value,
4880 TREE_TYPE (ce->value)))
4881 TREE_STATIC (ctor) = 0;
4883 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4884 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4886 break;
4888 default:
4889 /* So how did we get a CONSTRUCTOR for a scalar type? */
4890 gcc_unreachable ();
4893 if (ret == GS_ERROR)
4894 return GS_ERROR;
4895 /* If we have gimplified both sides of the initializer but have
4896 not emitted an assignment, do so now. */
4897 if (*expr_p)
4899 tree lhs = TREE_OPERAND (*expr_p, 0);
4900 tree rhs = TREE_OPERAND (*expr_p, 1);
4901 gassign *init = gimple_build_assign (lhs, rhs);
4902 gimplify_seq_add_stmt (pre_p, init);
4904 if (want_value)
4906 *expr_p = object;
4907 return GS_OK;
4909 else
4911 *expr_p = NULL;
4912 return GS_ALL_DONE;
4916 /* Given a pointer value OP0, return a simplified version of an
4917 indirection through OP0, or NULL_TREE if no simplification is
4918 possible. This may only be applied to a rhs of an expression.
4919 Note that the resulting type may be different from the type pointed
4920 to in the sense that it is still compatible from the langhooks
4921 point of view. */
4923 static tree
4924 gimple_fold_indirect_ref_rhs (tree t)
4926 return gimple_fold_indirect_ref (t);
4929 /* Subroutine of gimplify_modify_expr to do simplifications of
4930 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4931 something changes. */
4933 static enum gimplify_status
4934 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4935 gimple_seq *pre_p, gimple_seq *post_p,
4936 bool want_value)
4938 enum gimplify_status ret = GS_UNHANDLED;
4939 bool changed;
4943 changed = false;
4944 switch (TREE_CODE (*from_p))
4946 case VAR_DECL:
4947 /* If we're assigning from a read-only variable initialized with
4948 a constructor, do the direct assignment from the constructor,
4949 but only if neither source nor target are volatile since this
4950 latter assignment might end up being done on a per-field basis. */
4951 if (DECL_INITIAL (*from_p)
4952 && TREE_READONLY (*from_p)
4953 && !TREE_THIS_VOLATILE (*from_p)
4954 && !TREE_THIS_VOLATILE (*to_p)
4955 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4957 tree old_from = *from_p;
4958 enum gimplify_status subret;
4960 /* Move the constructor into the RHS. */
4961 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4963 /* Let's see if gimplify_init_constructor will need to put
4964 it in memory. */
4965 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4966 false, true);
4967 if (subret == GS_ERROR)
4969 /* If so, revert the change. */
4970 *from_p = old_from;
4972 else
4974 ret = GS_OK;
4975 changed = true;
4978 break;
4979 case INDIRECT_REF:
4981 /* If we have code like
4983 *(const A*)(A*)&x
4985 where the type of "x" is a (possibly cv-qualified variant
4986 of "A"), treat the entire expression as identical to "x".
4987 This kind of code arises in C++ when an object is bound
4988 to a const reference, and if "x" is a TARGET_EXPR we want
4989 to take advantage of the optimization below. */
4990 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4991 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4992 if (t)
4994 if (TREE_THIS_VOLATILE (t) != volatile_p)
4996 if (DECL_P (t))
4997 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4998 build_fold_addr_expr (t));
4999 if (REFERENCE_CLASS_P (t))
5000 TREE_THIS_VOLATILE (t) = volatile_p;
5002 *from_p = t;
5003 ret = GS_OK;
5004 changed = true;
5006 break;
5009 case TARGET_EXPR:
5011 /* If we are initializing something from a TARGET_EXPR, strip the
5012 TARGET_EXPR and initialize it directly, if possible. This can't
5013 be done if the initializer is void, since that implies that the
5014 temporary is set in some non-trivial way.
5016 ??? What about code that pulls out the temp and uses it
5017 elsewhere? I think that such code never uses the TARGET_EXPR as
5018 an initializer. If I'm wrong, we'll die because the temp won't
5019 have any RTL. In that case, I guess we'll need to replace
5020 references somehow. */
5021 tree init = TARGET_EXPR_INITIAL (*from_p);
5023 if (init
5024 && !VOID_TYPE_P (TREE_TYPE (init)))
5026 *from_p = init;
5027 ret = GS_OK;
5028 changed = true;
5031 break;
5033 case COMPOUND_EXPR:
5034 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
5035 caught. */
5036 gimplify_compound_expr (from_p, pre_p, true);
5037 ret = GS_OK;
5038 changed = true;
5039 break;
5041 case CONSTRUCTOR:
5042 /* If we already made some changes, let the front end have a
5043 crack at this before we break it down. */
5044 if (ret != GS_UNHANDLED)
5045 break;
5046 /* If we're initializing from a CONSTRUCTOR, break this into
5047 individual MODIFY_EXPRs. */
5048 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
5049 false);
5051 case COND_EXPR:
5052 /* If we're assigning to a non-register type, push the assignment
5053 down into the branches. This is mandatory for ADDRESSABLE types,
5054 since we cannot generate temporaries for such, but it saves a
5055 copy in other cases as well. */
5056 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
5058 /* This code should mirror the code in gimplify_cond_expr. */
5059 enum tree_code code = TREE_CODE (*expr_p);
5060 tree cond = *from_p;
5061 tree result = *to_p;
5063 ret = gimplify_expr (&result, pre_p, post_p,
5064 is_gimple_lvalue, fb_lvalue);
5065 if (ret != GS_ERROR)
5066 ret = GS_OK;
5068 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
5069 TREE_OPERAND (cond, 1)
5070 = build2 (code, void_type_node, result,
5071 TREE_OPERAND (cond, 1));
5072 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
5073 TREE_OPERAND (cond, 2)
5074 = build2 (code, void_type_node, unshare_expr (result),
5075 TREE_OPERAND (cond, 2));
5077 TREE_TYPE (cond) = void_type_node;
5078 recalculate_side_effects (cond);
5080 if (want_value)
5082 gimplify_and_add (cond, pre_p);
5083 *expr_p = unshare_expr (result);
5085 else
5086 *expr_p = cond;
5087 return ret;
5089 break;
5091 case CALL_EXPR:
5092 /* For calls that return in memory, give *to_p as the CALL_EXPR's
5093 return slot so that we don't generate a temporary. */
5094 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
5095 && aggregate_value_p (*from_p, *from_p))
5097 bool use_target;
5099 if (!(rhs_predicate_for (*to_p))(*from_p))
5100 /* If we need a temporary, *to_p isn't accurate. */
5101 use_target = false;
5102 /* It's OK to use the return slot directly unless it's an NRV. */
5103 else if (TREE_CODE (*to_p) == RESULT_DECL
5104 && DECL_NAME (*to_p) == NULL_TREE
5105 && needs_to_live_in_memory (*to_p))
5106 use_target = true;
5107 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
5108 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
5109 /* Don't force regs into memory. */
5110 use_target = false;
5111 else if (TREE_CODE (*expr_p) == INIT_EXPR)
5112 /* It's OK to use the target directly if it's being
5113 initialized. */
5114 use_target = true;
5115 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
5116 != INTEGER_CST)
5117 /* Always use the target and thus RSO for variable-sized types.
5118 GIMPLE cannot deal with a variable-sized assignment
5119 embedded in a call statement. */
5120 use_target = true;
5121 else if (TREE_CODE (*to_p) != SSA_NAME
5122 && (!is_gimple_variable (*to_p)
5123 || needs_to_live_in_memory (*to_p)))
5124 /* Don't use the original target if it's already addressable;
5125 if its address escapes, and the called function uses the
5126 NRV optimization, a conforming program could see *to_p
5127 change before the called function returns; see c++/19317.
5128 When optimizing, the return_slot pass marks more functions
5129 as safe after we have escape info. */
5130 use_target = false;
5131 else
5132 use_target = true;
5134 if (use_target)
5136 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
5137 mark_addressable (*to_p);
5140 break;
5142 case WITH_SIZE_EXPR:
5143 /* Likewise for calls that return an aggregate of non-constant size,
5144 since we would not be able to generate a temporary at all. */
5145 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
5147 *from_p = TREE_OPERAND (*from_p, 0);
5148 /* We don't change ret in this case because the
5149 WITH_SIZE_EXPR might have been added in
5150 gimplify_modify_expr, so returning GS_OK would lead to an
5151 infinite loop. */
5152 changed = true;
5154 break;
5156 /* If we're initializing from a container, push the initialization
5157 inside it. */
5158 case CLEANUP_POINT_EXPR:
5159 case BIND_EXPR:
5160 case STATEMENT_LIST:
5162 tree wrap = *from_p;
5163 tree t;
5165 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5166 fb_lvalue);
5167 if (ret != GS_ERROR)
5168 ret = GS_OK;
5170 t = voidify_wrapper_expr (wrap, *expr_p);
5171 gcc_assert (t == *expr_p);
5173 if (want_value)
5175 gimplify_and_add (wrap, pre_p);
5176 *expr_p = unshare_expr (*to_p);
5178 else
5179 *expr_p = wrap;
5180 return GS_OK;
5183 case COMPOUND_LITERAL_EXPR:
5185 tree complit = TREE_OPERAND (*expr_p, 1);
5186 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5187 tree decl = DECL_EXPR_DECL (decl_s);
5188 tree init = DECL_INITIAL (decl);
5190 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5191 into struct T x = { 0, 1, 2 } if the address of the
5192 compound literal has never been taken. */
5193 if (!TREE_ADDRESSABLE (complit)
5194 && !TREE_ADDRESSABLE (decl)
5195 && init)
5197 *expr_p = copy_node (*expr_p);
5198 TREE_OPERAND (*expr_p, 1) = init;
5199 return GS_OK;
5203 default:
5204 break;
5207 while (changed);
5209 return ret;
5213 /* Return true if T looks like a valid GIMPLE statement. */
5215 static bool
5216 is_gimple_stmt (tree t)
5218 const enum tree_code code = TREE_CODE (t);
5220 switch (code)
5222 case NOP_EXPR:
5223 /* The only valid NOP_EXPR is the empty statement. */
5224 return IS_EMPTY_STMT (t);
5226 case BIND_EXPR:
5227 case COND_EXPR:
5228 /* These are only valid if they're void. */
5229 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5231 case SWITCH_EXPR:
5232 case GOTO_EXPR:
5233 case RETURN_EXPR:
5234 case LABEL_EXPR:
5235 case CASE_LABEL_EXPR:
5236 case TRY_CATCH_EXPR:
5237 case TRY_FINALLY_EXPR:
5238 case EH_FILTER_EXPR:
5239 case CATCH_EXPR:
5240 case ASM_EXPR:
5241 case STATEMENT_LIST:
5242 case OACC_PARALLEL:
5243 case OACC_KERNELS:
5244 case OACC_DATA:
5245 case OACC_HOST_DATA:
5246 case OACC_DECLARE:
5247 case OACC_UPDATE:
5248 case OACC_ENTER_DATA:
5249 case OACC_EXIT_DATA:
5250 case OACC_CACHE:
5251 case OMP_PARALLEL:
5252 case OMP_FOR:
5253 case OMP_SIMD:
5254 case CILK_SIMD:
5255 case OMP_DISTRIBUTE:
5256 case OACC_LOOP:
5257 case OMP_SECTIONS:
5258 case OMP_SECTION:
5259 case OMP_SINGLE:
5260 case OMP_MASTER:
5261 case OMP_TASKGROUP:
5262 case OMP_ORDERED:
5263 case OMP_CRITICAL:
5264 case OMP_TASK:
5265 case OMP_TARGET:
5266 case OMP_TARGET_DATA:
5267 case OMP_TARGET_UPDATE:
5268 case OMP_TARGET_ENTER_DATA:
5269 case OMP_TARGET_EXIT_DATA:
5270 case OMP_TASKLOOP:
5271 case OMP_TEAMS:
5272 /* These are always void. */
5273 return true;
5275 case CALL_EXPR:
5276 case MODIFY_EXPR:
5277 case PREDICT_EXPR:
5278 /* These are valid regardless of their type. */
5279 return true;
5281 default:
5282 return false;
5287 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5288 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5289 DECL_GIMPLE_REG_P set.
5291 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5292 other, unmodified part of the complex object just before the total store.
5293 As a consequence, if the object is still uninitialized, an undefined value
5294 will be loaded into a register, which may result in a spurious exception
5295 if the register is floating-point and the value happens to be a signaling
5296 NaN for example. Then the fully-fledged complex operations lowering pass
5297 followed by a DCE pass are necessary in order to fix things up. */
5299 static enum gimplify_status
5300 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5301 bool want_value)
5303 enum tree_code code, ocode;
5304 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5306 lhs = TREE_OPERAND (*expr_p, 0);
5307 rhs = TREE_OPERAND (*expr_p, 1);
5308 code = TREE_CODE (lhs);
5309 lhs = TREE_OPERAND (lhs, 0);
5311 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5312 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5313 TREE_NO_WARNING (other) = 1;
5314 other = get_formal_tmp_var (other, pre_p);
5316 realpart = code == REALPART_EXPR ? rhs : other;
5317 imagpart = code == REALPART_EXPR ? other : rhs;
5319 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5320 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5321 else
5322 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5324 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5325 *expr_p = (want_value) ? rhs : NULL_TREE;
5327 return GS_ALL_DONE;
5330 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5332 modify_expr
5333 : varname '=' rhs
5334 | '*' ID '=' rhs
5336 PRE_P points to the list where side effects that must happen before
5337 *EXPR_P should be stored.
5339 POST_P points to the list where side effects that must happen after
5340 *EXPR_P should be stored.
5342 WANT_VALUE is nonzero iff we want to use the value of this expression
5343 in another expression. */
5345 static enum gimplify_status
5346 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5347 bool want_value)
5349 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5350 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5351 enum gimplify_status ret = GS_UNHANDLED;
5352 gimple *assign;
5353 location_t loc = EXPR_LOCATION (*expr_p);
5354 gimple_stmt_iterator gsi;
5356 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5357 || TREE_CODE (*expr_p) == INIT_EXPR);
5359 /* Trying to simplify a clobber using normal logic doesn't work,
5360 so handle it here. */
5361 if (TREE_CLOBBER_P (*from_p))
5363 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5364 if (ret == GS_ERROR)
5365 return ret;
5366 gcc_assert (!want_value
5367 && (VAR_P (*to_p) || TREE_CODE (*to_p) == MEM_REF));
5368 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5369 *expr_p = NULL;
5370 return GS_ALL_DONE;
5373 /* Insert pointer conversions required by the middle-end that are not
5374 required by the frontend. This fixes middle-end type checking for
5375 for example gcc.dg/redecl-6.c. */
5376 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5378 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5379 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5380 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5383 /* See if any simplifications can be done based on what the RHS is. */
5384 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5385 want_value);
5386 if (ret != GS_UNHANDLED)
5387 return ret;
5389 /* For zero sized types only gimplify the left hand side and right hand
5390 side as statements and throw away the assignment. Do this after
5391 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5392 types properly. */
5393 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
5395 gimplify_stmt (from_p, pre_p);
5396 gimplify_stmt (to_p, pre_p);
5397 *expr_p = NULL_TREE;
5398 return GS_ALL_DONE;
5401 /* If the value being copied is of variable width, compute the length
5402 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5403 before gimplifying any of the operands so that we can resolve any
5404 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5405 the size of the expression to be copied, not of the destination, so
5406 that is what we must do here. */
5407 maybe_with_size_expr (from_p);
5409 /* As a special case, we have to temporarily allow for assignments
5410 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5411 a toplevel statement, when gimplifying the GENERIC expression
5412 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5413 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5415 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5416 prevent gimplify_expr from trying to create a new temporary for
5417 foo's LHS, we tell it that it should only gimplify until it
5418 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5419 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5420 and all we need to do here is set 'a' to be its LHS. */
5422 /* Gimplify the RHS first for C++17 and bug 71104. */
5423 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5424 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5425 if (ret == GS_ERROR)
5426 return ret;
5428 /* Then gimplify the LHS. */
5429 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5430 twice we have to make sure to gimplify into non-SSA as otherwise
5431 the abnormal edge added later will make those defs not dominate
5432 their uses.
5433 ??? Technically this applies only to the registers used in the
5434 resulting non-register *TO_P. */
5435 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5436 if (saved_into_ssa
5437 && TREE_CODE (*from_p) == CALL_EXPR
5438 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5439 gimplify_ctxp->into_ssa = false;
5440 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5441 gimplify_ctxp->into_ssa = saved_into_ssa;
5442 if (ret == GS_ERROR)
5443 return ret;
5445 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5446 guess for the predicate was wrong. */
5447 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5448 if (final_pred != initial_pred)
5450 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5451 if (ret == GS_ERROR)
5452 return ret;
5455 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5456 size as argument to the call. */
5457 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5459 tree call = TREE_OPERAND (*from_p, 0);
5460 tree vlasize = TREE_OPERAND (*from_p, 1);
5462 if (TREE_CODE (call) == CALL_EXPR
5463 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5465 int nargs = call_expr_nargs (call);
5466 tree type = TREE_TYPE (call);
5467 tree ap = CALL_EXPR_ARG (call, 0);
5468 tree tag = CALL_EXPR_ARG (call, 1);
5469 tree aptag = CALL_EXPR_ARG (call, 2);
5470 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5471 IFN_VA_ARG, type,
5472 nargs + 1, ap, tag,
5473 aptag, vlasize);
5474 TREE_OPERAND (*from_p, 0) = newcall;
5478 /* Now see if the above changed *from_p to something we handle specially. */
5479 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5480 want_value);
5481 if (ret != GS_UNHANDLED)
5482 return ret;
5484 /* If we've got a variable sized assignment between two lvalues (i.e. does
5485 not involve a call), then we can make things a bit more straightforward
5486 by converting the assignment to memcpy or memset. */
5487 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5489 tree from = TREE_OPERAND (*from_p, 0);
5490 tree size = TREE_OPERAND (*from_p, 1);
5492 if (TREE_CODE (from) == CONSTRUCTOR)
5493 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5495 if (is_gimple_addressable (from))
5497 *from_p = from;
5498 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5499 pre_p);
5503 /* Transform partial stores to non-addressable complex variables into
5504 total stores. This allows us to use real instead of virtual operands
5505 for these variables, which improves optimization. */
5506 if ((TREE_CODE (*to_p) == REALPART_EXPR
5507 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5508 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5509 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5511 /* Try to alleviate the effects of the gimplification creating artificial
5512 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5513 make sure not to create DECL_DEBUG_EXPR links across functions. */
5514 if (!gimplify_ctxp->into_ssa
5515 && VAR_P (*from_p)
5516 && DECL_IGNORED_P (*from_p)
5517 && DECL_P (*to_p)
5518 && !DECL_IGNORED_P (*to_p)
5519 && decl_function_context (*to_p) == current_function_decl)
5521 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5522 DECL_NAME (*from_p)
5523 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5524 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5525 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5528 if (want_value && TREE_THIS_VOLATILE (*to_p))
5529 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5531 if (TREE_CODE (*from_p) == CALL_EXPR)
5533 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5534 instead of a GIMPLE_ASSIGN. */
5535 gcall *call_stmt;
5536 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5538 /* Gimplify internal functions created in the FEs. */
5539 int nargs = call_expr_nargs (*from_p), i;
5540 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5541 auto_vec<tree> vargs (nargs);
5543 for (i = 0; i < nargs; i++)
5545 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5546 EXPR_LOCATION (*from_p));
5547 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5549 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5550 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5552 else
5554 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5555 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5556 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5557 tree fndecl = get_callee_fndecl (*from_p);
5558 if (fndecl
5559 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5560 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
5561 && call_expr_nargs (*from_p) == 3)
5562 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5563 CALL_EXPR_ARG (*from_p, 0),
5564 CALL_EXPR_ARG (*from_p, 1),
5565 CALL_EXPR_ARG (*from_p, 2));
5566 else
5568 call_stmt = gimple_build_call_from_tree (*from_p);
5569 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
5572 notice_special_calls (call_stmt);
5573 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5574 gimple_call_set_lhs (call_stmt, *to_p);
5575 else if (TREE_CODE (*to_p) == SSA_NAME)
5576 /* The above is somewhat premature, avoid ICEing later for a
5577 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5578 ??? This doesn't make it a default-def. */
5579 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5580 assign = call_stmt;
5582 else
5584 assign = gimple_build_assign (*to_p, *from_p);
5585 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5586 if (COMPARISON_CLASS_P (*from_p))
5587 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5590 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5592 /* We should have got an SSA name from the start. */
5593 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5594 || ! gimple_in_ssa_p (cfun));
5597 gimplify_seq_add_stmt (pre_p, assign);
5598 gsi = gsi_last (*pre_p);
5599 maybe_fold_stmt (&gsi);
5601 if (want_value)
5603 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5604 return GS_OK;
5606 else
5607 *expr_p = NULL;
5609 return GS_ALL_DONE;
5612 /* Gimplify a comparison between two variable-sized objects. Do this
5613 with a call to BUILT_IN_MEMCMP. */
5615 static enum gimplify_status
5616 gimplify_variable_sized_compare (tree *expr_p)
5618 location_t loc = EXPR_LOCATION (*expr_p);
5619 tree op0 = TREE_OPERAND (*expr_p, 0);
5620 tree op1 = TREE_OPERAND (*expr_p, 1);
5621 tree t, arg, dest, src, expr;
5623 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5624 arg = unshare_expr (arg);
5625 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5626 src = build_fold_addr_expr_loc (loc, op1);
5627 dest = build_fold_addr_expr_loc (loc, op0);
5628 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5629 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5631 expr
5632 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5633 SET_EXPR_LOCATION (expr, loc);
5634 *expr_p = expr;
5636 return GS_OK;
5639 /* Gimplify a comparison between two aggregate objects of integral scalar
5640 mode as a comparison between the bitwise equivalent scalar values. */
5642 static enum gimplify_status
5643 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5645 location_t loc = EXPR_LOCATION (*expr_p);
5646 tree op0 = TREE_OPERAND (*expr_p, 0);
5647 tree op1 = TREE_OPERAND (*expr_p, 1);
5649 tree type = TREE_TYPE (op0);
5650 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5652 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5653 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5655 *expr_p
5656 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5658 return GS_OK;
5661 /* Gimplify an expression sequence. This function gimplifies each
5662 expression and rewrites the original expression with the last
5663 expression of the sequence in GIMPLE form.
5665 PRE_P points to the list where the side effects for all the
5666 expressions in the sequence will be emitted.
5668 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5670 static enum gimplify_status
5671 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5673 tree t = *expr_p;
5677 tree *sub_p = &TREE_OPERAND (t, 0);
5679 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5680 gimplify_compound_expr (sub_p, pre_p, false);
5681 else
5682 gimplify_stmt (sub_p, pre_p);
5684 t = TREE_OPERAND (t, 1);
5686 while (TREE_CODE (t) == COMPOUND_EXPR);
5688 *expr_p = t;
5689 if (want_value)
5690 return GS_OK;
5691 else
5693 gimplify_stmt (expr_p, pre_p);
5694 return GS_ALL_DONE;
5698 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5699 gimplify. After gimplification, EXPR_P will point to a new temporary
5700 that holds the original value of the SAVE_EXPR node.
5702 PRE_P points to the list where side effects that must happen before
5703 *EXPR_P should be stored. */
5705 static enum gimplify_status
5706 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5708 enum gimplify_status ret = GS_ALL_DONE;
5709 tree val;
5711 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5712 val = TREE_OPERAND (*expr_p, 0);
5714 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5715 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5717 /* The operand may be a void-valued expression such as SAVE_EXPRs
5718 generated by the Java frontend for class initialization. It is
5719 being executed only for its side-effects. */
5720 if (TREE_TYPE (val) == void_type_node)
5722 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5723 is_gimple_stmt, fb_none);
5724 val = NULL;
5726 else
5727 /* The temporary may not be an SSA name as later abnormal and EH
5728 control flow may invalidate use/def domination. */
5729 val = get_initialized_tmp_var (val, pre_p, post_p, false);
5731 TREE_OPERAND (*expr_p, 0) = val;
5732 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5735 *expr_p = val;
5737 return ret;
5740 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5742 unary_expr
5743 : ...
5744 | '&' varname
5747 PRE_P points to the list where side effects that must happen before
5748 *EXPR_P should be stored.
5750 POST_P points to the list where side effects that must happen after
5751 *EXPR_P should be stored. */
5753 static enum gimplify_status
5754 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5756 tree expr = *expr_p;
5757 tree op0 = TREE_OPERAND (expr, 0);
5758 enum gimplify_status ret;
5759 location_t loc = EXPR_LOCATION (*expr_p);
5761 switch (TREE_CODE (op0))
5763 case INDIRECT_REF:
5764 do_indirect_ref:
5765 /* Check if we are dealing with an expression of the form '&*ptr'.
5766 While the front end folds away '&*ptr' into 'ptr', these
5767 expressions may be generated internally by the compiler (e.g.,
5768 builtins like __builtin_va_end). */
5769 /* Caution: the silent array decomposition semantics we allow for
5770 ADDR_EXPR means we can't always discard the pair. */
5771 /* Gimplification of the ADDR_EXPR operand may drop
5772 cv-qualification conversions, so make sure we add them if
5773 needed. */
5775 tree op00 = TREE_OPERAND (op0, 0);
5776 tree t_expr = TREE_TYPE (expr);
5777 tree t_op00 = TREE_TYPE (op00);
5779 if (!useless_type_conversion_p (t_expr, t_op00))
5780 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5781 *expr_p = op00;
5782 ret = GS_OK;
5784 break;
5786 case VIEW_CONVERT_EXPR:
5787 /* Take the address of our operand and then convert it to the type of
5788 this ADDR_EXPR.
5790 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5791 all clear. The impact of this transformation is even less clear. */
5793 /* If the operand is a useless conversion, look through it. Doing so
5794 guarantees that the ADDR_EXPR and its operand will remain of the
5795 same type. */
5796 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5797 op0 = TREE_OPERAND (op0, 0);
5799 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5800 build_fold_addr_expr_loc (loc,
5801 TREE_OPERAND (op0, 0)));
5802 ret = GS_OK;
5803 break;
5805 case MEM_REF:
5806 if (integer_zerop (TREE_OPERAND (op0, 1)))
5807 goto do_indirect_ref;
5809 /* fall through */
5811 default:
5812 /* If we see a call to a declared builtin or see its address
5813 being taken (we can unify those cases here) then we can mark
5814 the builtin for implicit generation by GCC. */
5815 if (TREE_CODE (op0) == FUNCTION_DECL
5816 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
5817 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
5818 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
5820 /* We use fb_either here because the C frontend sometimes takes
5821 the address of a call that returns a struct; see
5822 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5823 the implied temporary explicit. */
5825 /* Make the operand addressable. */
5826 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5827 is_gimple_addressable, fb_either);
5828 if (ret == GS_ERROR)
5829 break;
5831 /* Then mark it. Beware that it may not be possible to do so directly
5832 if a temporary has been created by the gimplification. */
5833 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5835 op0 = TREE_OPERAND (expr, 0);
5837 /* For various reasons, the gimplification of the expression
5838 may have made a new INDIRECT_REF. */
5839 if (TREE_CODE (op0) == INDIRECT_REF)
5840 goto do_indirect_ref;
5842 mark_addressable (TREE_OPERAND (expr, 0));
5844 /* The FEs may end up building ADDR_EXPRs early on a decl with
5845 an incomplete type. Re-build ADDR_EXPRs in canonical form
5846 here. */
5847 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5848 *expr_p = build_fold_addr_expr (op0);
5850 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5851 recompute_tree_invariant_for_addr_expr (*expr_p);
5853 /* If we re-built the ADDR_EXPR add a conversion to the original type
5854 if required. */
5855 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5856 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5858 break;
5861 return ret;
5864 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5865 value; output operands should be a gimple lvalue. */
5867 static enum gimplify_status
5868 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5870 tree expr;
5871 int noutputs;
5872 const char **oconstraints;
5873 int i;
5874 tree link;
5875 const char *constraint;
5876 bool allows_mem, allows_reg, is_inout;
5877 enum gimplify_status ret, tret;
5878 gasm *stmt;
5879 vec<tree, va_gc> *inputs;
5880 vec<tree, va_gc> *outputs;
5881 vec<tree, va_gc> *clobbers;
5882 vec<tree, va_gc> *labels;
5883 tree link_next;
5885 expr = *expr_p;
5886 noutputs = list_length (ASM_OUTPUTS (expr));
5887 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5889 inputs = NULL;
5890 outputs = NULL;
5891 clobbers = NULL;
5892 labels = NULL;
5894 ret = GS_ALL_DONE;
5895 link_next = NULL_TREE;
5896 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5898 bool ok;
5899 size_t constraint_len;
5901 link_next = TREE_CHAIN (link);
5903 oconstraints[i]
5904 = constraint
5905 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5906 constraint_len = strlen (constraint);
5907 if (constraint_len == 0)
5908 continue;
5910 ok = parse_output_constraint (&constraint, i, 0, 0,
5911 &allows_mem, &allows_reg, &is_inout);
5912 if (!ok)
5914 ret = GS_ERROR;
5915 is_inout = false;
5918 if (!allows_reg && allows_mem)
5919 mark_addressable (TREE_VALUE (link));
5921 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5922 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5923 fb_lvalue | fb_mayfail);
5924 if (tret == GS_ERROR)
5926 error ("invalid lvalue in asm output %d", i);
5927 ret = tret;
5930 /* If the constraint does not allow memory make sure we gimplify
5931 it to a register if it is not already but its base is. This
5932 happens for complex and vector components. */
5933 if (!allows_mem)
5935 tree op = TREE_VALUE (link);
5936 if (! is_gimple_val (op)
5937 && is_gimple_reg_type (TREE_TYPE (op))
5938 && is_gimple_reg (get_base_address (op)))
5940 tree tem = create_tmp_reg (TREE_TYPE (op));
5941 tree ass;
5942 if (is_inout)
5944 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
5945 tem, unshare_expr (op));
5946 gimplify_and_add (ass, pre_p);
5948 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
5949 gimplify_and_add (ass, post_p);
5951 TREE_VALUE (link) = tem;
5952 tret = GS_OK;
5956 vec_safe_push (outputs, link);
5957 TREE_CHAIN (link) = NULL_TREE;
5959 if (is_inout)
5961 /* An input/output operand. To give the optimizers more
5962 flexibility, split it into separate input and output
5963 operands. */
5964 tree input;
5965 /* Buffer big enough to format a 32-bit UINT_MAX into. */
5966 char buf[11];
5968 /* Turn the in/out constraint into an output constraint. */
5969 char *p = xstrdup (constraint);
5970 p[0] = '=';
5971 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5973 /* And add a matching input constraint. */
5974 if (allows_reg)
5976 sprintf (buf, "%u", i);
5978 /* If there are multiple alternatives in the constraint,
5979 handle each of them individually. Those that allow register
5980 will be replaced with operand number, the others will stay
5981 unchanged. */
5982 if (strchr (p, ',') != NULL)
5984 size_t len = 0, buflen = strlen (buf);
5985 char *beg, *end, *str, *dst;
5987 for (beg = p + 1;;)
5989 end = strchr (beg, ',');
5990 if (end == NULL)
5991 end = strchr (beg, '\0');
5992 if ((size_t) (end - beg) < buflen)
5993 len += buflen + 1;
5994 else
5995 len += end - beg + 1;
5996 if (*end)
5997 beg = end + 1;
5998 else
5999 break;
6002 str = (char *) alloca (len);
6003 for (beg = p + 1, dst = str;;)
6005 const char *tem;
6006 bool mem_p, reg_p, inout_p;
6008 end = strchr (beg, ',');
6009 if (end)
6010 *end = '\0';
6011 beg[-1] = '=';
6012 tem = beg - 1;
6013 parse_output_constraint (&tem, i, 0, 0,
6014 &mem_p, &reg_p, &inout_p);
6015 if (dst != str)
6016 *dst++ = ',';
6017 if (reg_p)
6019 memcpy (dst, buf, buflen);
6020 dst += buflen;
6022 else
6024 if (end)
6025 len = end - beg;
6026 else
6027 len = strlen (beg);
6028 memcpy (dst, beg, len);
6029 dst += len;
6031 if (end)
6032 beg = end + 1;
6033 else
6034 break;
6036 *dst = '\0';
6037 input = build_string (dst - str, str);
6039 else
6040 input = build_string (strlen (buf), buf);
6042 else
6043 input = build_string (constraint_len - 1, constraint + 1);
6045 free (p);
6047 input = build_tree_list (build_tree_list (NULL_TREE, input),
6048 unshare_expr (TREE_VALUE (link)));
6049 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
6053 link_next = NULL_TREE;
6054 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
6056 link_next = TREE_CHAIN (link);
6057 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
6058 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
6059 oconstraints, &allows_mem, &allows_reg);
6061 /* If we can't make copies, we can only accept memory. */
6062 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
6064 if (allows_mem)
6065 allows_reg = 0;
6066 else
6068 error ("impossible constraint in %<asm%>");
6069 error ("non-memory input %d must stay in memory", i);
6070 return GS_ERROR;
6074 /* If the operand is a memory input, it should be an lvalue. */
6075 if (!allows_reg && allows_mem)
6077 tree inputv = TREE_VALUE (link);
6078 STRIP_NOPS (inputv);
6079 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
6080 || TREE_CODE (inputv) == PREINCREMENT_EXPR
6081 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
6082 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
6083 || TREE_CODE (inputv) == MODIFY_EXPR)
6084 TREE_VALUE (link) = error_mark_node;
6085 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6086 is_gimple_lvalue, fb_lvalue | fb_mayfail);
6087 if (tret != GS_ERROR)
6089 /* Unlike output operands, memory inputs are not guaranteed
6090 to be lvalues by the FE, and while the expressions are
6091 marked addressable there, if it is e.g. a statement
6092 expression, temporaries in it might not end up being
6093 addressable. They might be already used in the IL and thus
6094 it is too late to make them addressable now though. */
6095 tree x = TREE_VALUE (link);
6096 while (handled_component_p (x))
6097 x = TREE_OPERAND (x, 0);
6098 if (TREE_CODE (x) == MEM_REF
6099 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
6100 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
6101 if ((VAR_P (x)
6102 || TREE_CODE (x) == PARM_DECL
6103 || TREE_CODE (x) == RESULT_DECL)
6104 && !TREE_ADDRESSABLE (x)
6105 && is_gimple_reg (x))
6107 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
6108 input_location), 0,
6109 "memory input %d is not directly addressable",
6111 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
6114 mark_addressable (TREE_VALUE (link));
6115 if (tret == GS_ERROR)
6117 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
6118 "memory input %d is not directly addressable", i);
6119 ret = tret;
6122 else
6124 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
6125 is_gimple_asm_val, fb_rvalue);
6126 if (tret == GS_ERROR)
6127 ret = tret;
6130 TREE_CHAIN (link) = NULL_TREE;
6131 vec_safe_push (inputs, link);
6134 link_next = NULL_TREE;
6135 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
6137 link_next = TREE_CHAIN (link);
6138 TREE_CHAIN (link) = NULL_TREE;
6139 vec_safe_push (clobbers, link);
6142 link_next = NULL_TREE;
6143 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
6145 link_next = TREE_CHAIN (link);
6146 TREE_CHAIN (link) = NULL_TREE;
6147 vec_safe_push (labels, link);
6150 /* Do not add ASMs with errors to the gimple IL stream. */
6151 if (ret != GS_ERROR)
6153 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
6154 inputs, outputs, clobbers, labels);
6156 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
6157 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
6159 gimplify_seq_add_stmt (pre_p, stmt);
6162 return ret;
6165 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6166 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6167 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6168 return to this function.
6170 FIXME should we complexify the prequeue handling instead? Or use flags
6171 for all the cleanups and let the optimizer tighten them up? The current
6172 code seems pretty fragile; it will break on a cleanup within any
6173 non-conditional nesting. But any such nesting would be broken, anyway;
6174 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6175 and continues out of it. We can do that at the RTL level, though, so
6176 having an optimizer to tighten up try/finally regions would be a Good
6177 Thing. */
6179 static enum gimplify_status
6180 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6182 gimple_stmt_iterator iter;
6183 gimple_seq body_sequence = NULL;
6185 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6187 /* We only care about the number of conditions between the innermost
6188 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6189 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6190 int old_conds = gimplify_ctxp->conditions;
6191 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6192 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6193 gimplify_ctxp->conditions = 0;
6194 gimplify_ctxp->conditional_cleanups = NULL;
6195 gimplify_ctxp->in_cleanup_point_expr = true;
6197 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6199 gimplify_ctxp->conditions = old_conds;
6200 gimplify_ctxp->conditional_cleanups = old_cleanups;
6201 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6203 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6205 gimple *wce = gsi_stmt (iter);
6207 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6209 if (gsi_one_before_end_p (iter))
6211 /* Note that gsi_insert_seq_before and gsi_remove do not
6212 scan operands, unlike some other sequence mutators. */
6213 if (!gimple_wce_cleanup_eh_only (wce))
6214 gsi_insert_seq_before_without_update (&iter,
6215 gimple_wce_cleanup (wce),
6216 GSI_SAME_STMT);
6217 gsi_remove (&iter, true);
6218 break;
6220 else
6222 gtry *gtry;
6223 gimple_seq seq;
6224 enum gimple_try_flags kind;
6226 if (gimple_wce_cleanup_eh_only (wce))
6227 kind = GIMPLE_TRY_CATCH;
6228 else
6229 kind = GIMPLE_TRY_FINALLY;
6230 seq = gsi_split_seq_after (iter);
6232 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6233 /* Do not use gsi_replace here, as it may scan operands.
6234 We want to do a simple structural modification only. */
6235 gsi_set_stmt (&iter, gtry);
6236 iter = gsi_start (gtry->eval);
6239 else
6240 gsi_next (&iter);
6243 gimplify_seq_add_seq (pre_p, body_sequence);
6244 if (temp)
6246 *expr_p = temp;
6247 return GS_OK;
6249 else
6251 *expr_p = NULL;
6252 return GS_ALL_DONE;
6256 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6257 is the cleanup action required. EH_ONLY is true if the cleanup should
6258 only be executed if an exception is thrown, not on normal exit. */
6260 static void
6261 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
6263 gimple *wce;
6264 gimple_seq cleanup_stmts = NULL;
6266 /* Errors can result in improperly nested cleanups. Which results in
6267 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6268 if (seen_error ())
6269 return;
6271 if (gimple_conditional_context ())
6273 /* If we're in a conditional context, this is more complex. We only
6274 want to run the cleanup if we actually ran the initialization that
6275 necessitates it, but we want to run it after the end of the
6276 conditional context. So we wrap the try/finally around the
6277 condition and use a flag to determine whether or not to actually
6278 run the destructor. Thus
6280 test ? f(A()) : 0
6282 becomes (approximately)
6284 flag = 0;
6285 try {
6286 if (test) { A::A(temp); flag = 1; val = f(temp); }
6287 else { val = 0; }
6288 } finally {
6289 if (flag) A::~A(temp);
6293 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6294 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6295 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6297 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6298 gimplify_stmt (&cleanup, &cleanup_stmts);
6299 wce = gimple_build_wce (cleanup_stmts);
6301 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6302 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6303 gimplify_seq_add_stmt (pre_p, ftrue);
6305 /* Because of this manipulation, and the EH edges that jump
6306 threading cannot redirect, the temporary (VAR) will appear
6307 to be used uninitialized. Don't warn. */
6308 TREE_NO_WARNING (var) = 1;
6310 else
6312 gimplify_stmt (&cleanup, &cleanup_stmts);
6313 wce = gimple_build_wce (cleanup_stmts);
6314 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6315 gimplify_seq_add_stmt (pre_p, wce);
6319 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6321 static enum gimplify_status
6322 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6324 tree targ = *expr_p;
6325 tree temp = TARGET_EXPR_SLOT (targ);
6326 tree init = TARGET_EXPR_INITIAL (targ);
6327 enum gimplify_status ret;
6329 bool unpoison_empty_seq = false;
6330 gimple_stmt_iterator unpoison_it;
6332 if (init)
6334 tree cleanup = NULL_TREE;
6336 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6337 to the temps list. Handle also variable length TARGET_EXPRs. */
6338 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6340 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6341 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6342 gimplify_vla_decl (temp, pre_p);
6344 else
6346 /* Save location where we need to place unpoisoning. It's possible
6347 that a variable will be converted to needs_to_live_in_memory. */
6348 unpoison_it = gsi_last (*pre_p);
6349 unpoison_empty_seq = gsi_end_p (unpoison_it);
6351 gimple_add_tmp_var (temp);
6354 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6355 expression is supposed to initialize the slot. */
6356 if (VOID_TYPE_P (TREE_TYPE (init)))
6357 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6358 else
6360 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6361 init = init_expr;
6362 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6363 init = NULL;
6364 ggc_free (init_expr);
6366 if (ret == GS_ERROR)
6368 /* PR c++/28266 Make sure this is expanded only once. */
6369 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6370 return GS_ERROR;
6372 if (init)
6373 gimplify_and_add (init, pre_p);
6375 /* If needed, push the cleanup for the temp. */
6376 if (TARGET_EXPR_CLEANUP (targ))
6378 if (CLEANUP_EH_ONLY (targ))
6379 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6380 CLEANUP_EH_ONLY (targ), pre_p);
6381 else
6382 cleanup = TARGET_EXPR_CLEANUP (targ);
6385 /* Add a clobber for the temporary going out of scope, like
6386 gimplify_bind_expr. */
6387 if (gimplify_ctxp->in_cleanup_point_expr
6388 && needs_to_live_in_memory (temp))
6390 if (flag_stack_reuse == SR_ALL)
6392 tree clobber = build_constructor (TREE_TYPE (temp),
6393 NULL);
6394 TREE_THIS_VOLATILE (clobber) = true;
6395 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6396 if (cleanup)
6397 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
6398 clobber);
6399 else
6400 cleanup = clobber;
6402 if (asan_sanitize_use_after_scope ())
6404 tree asan_cleanup = build_asan_poison_call_expr (temp);
6405 if (asan_cleanup)
6407 if (unpoison_empty_seq)
6408 unpoison_it = gsi_start (*pre_p);
6410 asan_poison_variable (temp, false, &unpoison_it,
6411 unpoison_empty_seq);
6412 gimple_push_cleanup (temp, asan_cleanup, false, pre_p);
6416 if (cleanup)
6417 gimple_push_cleanup (temp, cleanup, false, pre_p);
6419 /* Only expand this once. */
6420 TREE_OPERAND (targ, 3) = init;
6421 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6423 else
6424 /* We should have expanded this before. */
6425 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6427 *expr_p = temp;
6428 return GS_OK;
6431 /* Gimplification of expression trees. */
6433 /* Gimplify an expression which appears at statement context. The
6434 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6435 NULL, a new sequence is allocated.
6437 Return true if we actually added a statement to the queue. */
6439 bool
6440 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6442 gimple_seq_node last;
6444 last = gimple_seq_last (*seq_p);
6445 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6446 return last != gimple_seq_last (*seq_p);
6449 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6450 to CTX. If entries already exist, force them to be some flavor of private.
6451 If there is no enclosing parallel, do nothing. */
6453 void
6454 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6456 splay_tree_node n;
6458 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6459 return;
6463 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6464 if (n != NULL)
6466 if (n->value & GOVD_SHARED)
6467 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6468 else if (n->value & GOVD_MAP)
6469 n->value |= GOVD_MAP_TO_ONLY;
6470 else
6471 return;
6473 else if ((ctx->region_type & ORT_TARGET) != 0)
6475 if (ctx->target_map_scalars_firstprivate)
6476 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6477 else
6478 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6480 else if (ctx->region_type != ORT_WORKSHARE
6481 && ctx->region_type != ORT_SIMD
6482 && ctx->region_type != ORT_ACC
6483 && !(ctx->region_type & ORT_TARGET_DATA))
6484 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6486 ctx = ctx->outer_context;
6488 while (ctx);
6491 /* Similarly for each of the type sizes of TYPE. */
6493 static void
6494 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6496 if (type == NULL || type == error_mark_node)
6497 return;
6498 type = TYPE_MAIN_VARIANT (type);
6500 if (ctx->privatized_types->add (type))
6501 return;
6503 switch (TREE_CODE (type))
6505 case INTEGER_TYPE:
6506 case ENUMERAL_TYPE:
6507 case BOOLEAN_TYPE:
6508 case REAL_TYPE:
6509 case FIXED_POINT_TYPE:
6510 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6511 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6512 break;
6514 case ARRAY_TYPE:
6515 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6516 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6517 break;
6519 case RECORD_TYPE:
6520 case UNION_TYPE:
6521 case QUAL_UNION_TYPE:
6523 tree field;
6524 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6525 if (TREE_CODE (field) == FIELD_DECL)
6527 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6528 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6531 break;
6533 case POINTER_TYPE:
6534 case REFERENCE_TYPE:
6535 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6536 break;
6538 default:
6539 break;
6542 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6543 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6544 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6547 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6549 static void
6550 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6552 splay_tree_node n;
6553 unsigned int nflags;
6554 tree t;
6556 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6557 return;
6559 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6560 there are constructors involved somewhere. */
6561 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
6562 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
6563 flags |= GOVD_SEEN;
6565 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6566 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6568 /* We shouldn't be re-adding the decl with the same data
6569 sharing class. */
6570 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6571 nflags = n->value | flags;
6572 /* The only combination of data sharing classes we should see is
6573 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6574 reduction variables to be used in data sharing clauses. */
6575 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6576 || ((nflags & GOVD_DATA_SHARE_CLASS)
6577 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6578 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6579 n->value = nflags;
6580 return;
6583 /* When adding a variable-sized variable, we have to handle all sorts
6584 of additional bits of data: the pointer replacement variable, and
6585 the parameters of the type. */
6586 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6588 /* Add the pointer replacement variable as PRIVATE if the variable
6589 replacement is private, else FIRSTPRIVATE since we'll need the
6590 address of the original variable either for SHARED, or for the
6591 copy into or out of the context. */
6592 if (!(flags & GOVD_LOCAL))
6594 if (flags & GOVD_MAP)
6595 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6596 else if (flags & GOVD_PRIVATE)
6597 nflags = GOVD_PRIVATE;
6598 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6599 && (flags & GOVD_FIRSTPRIVATE))
6600 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6601 else
6602 nflags = GOVD_FIRSTPRIVATE;
6603 nflags |= flags & GOVD_SEEN;
6604 t = DECL_VALUE_EXPR (decl);
6605 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6606 t = TREE_OPERAND (t, 0);
6607 gcc_assert (DECL_P (t));
6608 omp_add_variable (ctx, t, nflags);
6611 /* Add all of the variable and type parameters (which should have
6612 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6613 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6614 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6615 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6617 /* The variable-sized variable itself is never SHARED, only some form
6618 of PRIVATE. The sharing would take place via the pointer variable
6619 which we remapped above. */
6620 if (flags & GOVD_SHARED)
6621 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
6622 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6624 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6625 alloca statement we generate for the variable, so make sure it
6626 is available. This isn't automatically needed for the SHARED
6627 case, since we won't be allocating local storage then.
6628 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6629 in this case omp_notice_variable will be called later
6630 on when it is gimplified. */
6631 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6632 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6633 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6635 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6636 && lang_hooks.decls.omp_privatize_by_reference (decl))
6638 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6640 /* Similar to the direct variable sized case above, we'll need the
6641 size of references being privatized. */
6642 if ((flags & GOVD_SHARED) == 0)
6644 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6645 if (DECL_P (t))
6646 omp_notice_variable (ctx, t, true);
6650 if (n != NULL)
6651 n->value |= flags;
6652 else
6653 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6655 /* For reductions clauses in OpenACC loop directives, by default create a
6656 copy clause on the enclosing parallel construct for carrying back the
6657 results. */
6658 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6660 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6661 while (outer_ctx)
6663 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6664 if (n != NULL)
6666 /* Ignore local variables and explicitly declared clauses. */
6667 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6668 break;
6669 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6671 /* According to the OpenACC spec, such a reduction variable
6672 should already have a copy map on a kernels construct,
6673 verify that here. */
6674 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6675 && (n->value & GOVD_MAP));
6677 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6679 /* Remove firstprivate and make it a copy map. */
6680 n->value &= ~GOVD_FIRSTPRIVATE;
6681 n->value |= GOVD_MAP;
6684 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6686 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6687 GOVD_MAP | GOVD_SEEN);
6688 break;
6690 outer_ctx = outer_ctx->outer_context;
6695 /* Notice a threadprivate variable DECL used in OMP context CTX.
6696 This just prints out diagnostics about threadprivate variable uses
6697 in untied tasks. If DECL2 is non-NULL, prevent this warning
6698 on that variable. */
6700 static bool
6701 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6702 tree decl2)
6704 splay_tree_node n;
6705 struct gimplify_omp_ctx *octx;
6707 for (octx = ctx; octx; octx = octx->outer_context)
6708 if ((octx->region_type & ORT_TARGET) != 0)
6710 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6711 if (n == NULL)
6713 error ("threadprivate variable %qE used in target region",
6714 DECL_NAME (decl));
6715 error_at (octx->location, "enclosing target region");
6716 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6718 if (decl2)
6719 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6722 if (ctx->region_type != ORT_UNTIED_TASK)
6723 return false;
6724 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6725 if (n == NULL)
6727 error ("threadprivate variable %qE used in untied task",
6728 DECL_NAME (decl));
6729 error_at (ctx->location, "enclosing task");
6730 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6732 if (decl2)
6733 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6734 return false;
6737 /* Return true if global var DECL is device resident. */
6739 static bool
6740 device_resident_p (tree decl)
6742 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6744 if (!attr)
6745 return false;
6747 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6749 tree c = TREE_VALUE (t);
6750 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6751 return true;
6754 return false;
6757 /* Determine outer default flags for DECL mentioned in an OMP region
6758 but not declared in an enclosing clause.
6760 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6761 remapped firstprivate instead of shared. To some extent this is
6762 addressed in omp_firstprivatize_type_sizes, but not
6763 effectively. */
6765 static unsigned
6766 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
6767 bool in_code, unsigned flags)
6769 enum omp_clause_default_kind default_kind = ctx->default_kind;
6770 enum omp_clause_default_kind kind;
6772 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
6773 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6774 default_kind = kind;
6776 switch (default_kind)
6778 case OMP_CLAUSE_DEFAULT_NONE:
6780 const char *rtype;
6782 if (ctx->region_type & ORT_PARALLEL)
6783 rtype = "parallel";
6784 else if (ctx->region_type & ORT_TASK)
6785 rtype = "task";
6786 else if (ctx->region_type & ORT_TEAMS)
6787 rtype = "teams";
6788 else
6789 gcc_unreachable ();
6791 error ("%qE not specified in enclosing %s",
6792 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
6793 error_at (ctx->location, "enclosing %s", rtype);
6795 /* FALLTHRU */
6796 case OMP_CLAUSE_DEFAULT_SHARED:
6797 flags |= GOVD_SHARED;
6798 break;
6799 case OMP_CLAUSE_DEFAULT_PRIVATE:
6800 flags |= GOVD_PRIVATE;
6801 break;
6802 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
6803 flags |= GOVD_FIRSTPRIVATE;
6804 break;
6805 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6806 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
6807 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
6808 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
6810 omp_notice_variable (octx, decl, in_code);
6811 for (; octx; octx = octx->outer_context)
6813 splay_tree_node n2;
6815 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
6816 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
6817 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
6818 continue;
6819 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
6821 flags |= GOVD_FIRSTPRIVATE;
6822 goto found_outer;
6824 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
6826 flags |= GOVD_SHARED;
6827 goto found_outer;
6832 if (TREE_CODE (decl) == PARM_DECL
6833 || (!is_global_var (decl)
6834 && DECL_CONTEXT (decl) == current_function_decl))
6835 flags |= GOVD_FIRSTPRIVATE;
6836 else
6837 flags |= GOVD_SHARED;
6838 found_outer:
6839 break;
6841 default:
6842 gcc_unreachable ();
6845 return flags;
6849 /* Determine outer default flags for DECL mentioned in an OACC region
6850 but not declared in an enclosing clause. */
6852 static unsigned
6853 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
6855 const char *rkind;
6856 bool on_device = false;
6857 tree type = TREE_TYPE (decl);
6859 if (lang_hooks.decls.omp_privatize_by_reference (decl))
6860 type = TREE_TYPE (type);
6862 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
6863 && is_global_var (decl)
6864 && device_resident_p (decl))
6866 on_device = true;
6867 flags |= GOVD_MAP_TO_ONLY;
6870 switch (ctx->region_type)
6872 default:
6873 gcc_unreachable ();
6875 case ORT_ACC_KERNELS:
6876 /* Scalars are default 'copy' under kernels, non-scalars are default
6877 'present_or_copy'. */
6878 flags |= GOVD_MAP;
6879 if (!AGGREGATE_TYPE_P (type))
6880 flags |= GOVD_MAP_FORCE;
6882 rkind = "kernels";
6883 break;
6885 case ORT_ACC_PARALLEL:
6887 if (on_device || AGGREGATE_TYPE_P (type))
6888 /* Aggregates default to 'present_or_copy'. */
6889 flags |= GOVD_MAP;
6890 else
6891 /* Scalars default to 'firstprivate'. */
6892 flags |= GOVD_FIRSTPRIVATE;
6893 rkind = "parallel";
6895 break;
6898 if (DECL_ARTIFICIAL (decl))
6899 ; /* We can get compiler-generated decls, and should not complain
6900 about them. */
6901 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
6903 error ("%qE not specified in enclosing OpenACC %qs construct",
6904 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
6905 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
6907 else
6908 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
6910 return flags;
6913 /* Record the fact that DECL was used within the OMP context CTX.
6914 IN_CODE is true when real code uses DECL, and false when we should
6915 merely emit default(none) errors. Return true if DECL is going to
6916 be remapped and thus DECL shouldn't be gimplified into its
6917 DECL_VALUE_EXPR (if any). */
6919 static bool
6920 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
6922 splay_tree_node n;
6923 unsigned flags = in_code ? GOVD_SEEN : 0;
6924 bool ret = false, shared;
6926 if (error_operand_p (decl))
6927 return false;
6929 if (ctx->region_type == ORT_NONE)
6930 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
6932 if (is_global_var (decl))
6934 /* Threadprivate variables are predetermined. */
6935 if (DECL_THREAD_LOCAL_P (decl))
6936 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
6938 if (DECL_HAS_VALUE_EXPR_P (decl))
6940 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6942 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
6943 return omp_notice_threadprivate_variable (ctx, decl, value);
6946 if (gimplify_omp_ctxp->outer_context == NULL
6947 && VAR_P (decl)
6948 && get_oacc_fn_attrib (current_function_decl))
6950 location_t loc = DECL_SOURCE_LOCATION (decl);
6952 if (lookup_attribute ("omp declare target link",
6953 DECL_ATTRIBUTES (decl)))
6955 error_at (loc,
6956 "%qE with %<link%> clause used in %<routine%> function",
6957 DECL_NAME (decl));
6958 return false;
6960 else if (!lookup_attribute ("omp declare target",
6961 DECL_ATTRIBUTES (decl)))
6963 error_at (loc,
6964 "%qE requires a %<declare%> directive for use "
6965 "in a %<routine%> function", DECL_NAME (decl));
6966 return false;
6971 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6972 if ((ctx->region_type & ORT_TARGET) != 0)
6974 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
6975 if (n == NULL)
6977 unsigned nflags = flags;
6978 if (ctx->target_map_pointers_as_0len_arrays
6979 || ctx->target_map_scalars_firstprivate)
6981 bool is_declare_target = false;
6982 bool is_scalar = false;
6983 if (is_global_var (decl)
6984 && varpool_node::get_create (decl)->offloadable)
6986 struct gimplify_omp_ctx *octx;
6987 for (octx = ctx->outer_context;
6988 octx; octx = octx->outer_context)
6990 n = splay_tree_lookup (octx->variables,
6991 (splay_tree_key)decl);
6992 if (n
6993 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
6994 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6995 break;
6997 is_declare_target = octx == NULL;
6999 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
7001 tree type = TREE_TYPE (decl);
7002 if (TREE_CODE (type) == REFERENCE_TYPE)
7003 type = TREE_TYPE (type);
7004 if (TREE_CODE (type) == COMPLEX_TYPE)
7005 type = TREE_TYPE (type);
7006 if (INTEGRAL_TYPE_P (type)
7007 || SCALAR_FLOAT_TYPE_P (type)
7008 || TREE_CODE (type) == POINTER_TYPE)
7009 is_scalar = true;
7011 if (is_declare_target)
7013 else if (ctx->target_map_pointers_as_0len_arrays
7014 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
7015 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7016 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
7017 == POINTER_TYPE)))
7018 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
7019 else if (is_scalar)
7020 nflags |= GOVD_FIRSTPRIVATE;
7023 struct gimplify_omp_ctx *octx = ctx->outer_context;
7024 if ((ctx->region_type & ORT_ACC) && octx)
7026 /* Look in outer OpenACC contexts, to see if there's a
7027 data attribute for this variable. */
7028 omp_notice_variable (octx, decl, in_code);
7030 for (; octx; octx = octx->outer_context)
7032 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
7033 break;
7034 splay_tree_node n2
7035 = splay_tree_lookup (octx->variables,
7036 (splay_tree_key) decl);
7037 if (n2)
7039 if (octx->region_type == ORT_ACC_HOST_DATA)
7040 error ("variable %qE declared in enclosing "
7041 "%<host_data%> region", DECL_NAME (decl));
7042 nflags |= GOVD_MAP;
7043 if (octx->region_type == ORT_ACC_DATA
7044 && (n2->value & GOVD_MAP_0LEN_ARRAY))
7045 nflags |= GOVD_MAP_0LEN_ARRAY;
7046 goto found_outer;
7052 tree type = TREE_TYPE (decl);
7054 if (nflags == flags
7055 && gimplify_omp_ctxp->target_firstprivatize_array_bases
7056 && lang_hooks.decls.omp_privatize_by_reference (decl))
7057 type = TREE_TYPE (type);
7058 if (nflags == flags
7059 && !lang_hooks.types.omp_mappable_type (type))
7061 error ("%qD referenced in target region does not have "
7062 "a mappable type", decl);
7063 nflags |= GOVD_MAP | GOVD_EXPLICIT;
7065 else if (nflags == flags)
7067 if ((ctx->region_type & ORT_ACC) != 0)
7068 nflags = oacc_default_clause (ctx, decl, flags);
7069 else
7070 nflags |= GOVD_MAP;
7073 found_outer:
7074 omp_add_variable (ctx, decl, nflags);
7076 else
7078 /* If nothing changed, there's nothing left to do. */
7079 if ((n->value & flags) == flags)
7080 return ret;
7081 flags |= n->value;
7082 n->value = flags;
7084 goto do_outer;
7087 if (n == NULL)
7089 if (ctx->region_type == ORT_WORKSHARE
7090 || ctx->region_type == ORT_SIMD
7091 || ctx->region_type == ORT_ACC
7092 || (ctx->region_type & ORT_TARGET_DATA) != 0)
7093 goto do_outer;
7095 flags = omp_default_clause (ctx, decl, in_code, flags);
7097 if ((flags & GOVD_PRIVATE)
7098 && lang_hooks.decls.omp_private_outer_ref (decl))
7099 flags |= GOVD_PRIVATE_OUTER_REF;
7101 omp_add_variable (ctx, decl, flags);
7103 shared = (flags & GOVD_SHARED) != 0;
7104 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7105 goto do_outer;
7108 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
7109 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
7110 && DECL_SIZE (decl))
7112 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7114 splay_tree_node n2;
7115 tree t = DECL_VALUE_EXPR (decl);
7116 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
7117 t = TREE_OPERAND (t, 0);
7118 gcc_assert (DECL_P (t));
7119 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7120 n2->value |= GOVD_SEEN;
7122 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
7123 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
7124 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
7125 != INTEGER_CST))
7127 splay_tree_node n2;
7128 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
7129 gcc_assert (DECL_P (t));
7130 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
7131 if (n2)
7132 n2->value |= GOVD_SEEN;
7136 shared = ((flags | n->value) & GOVD_SHARED) != 0;
7137 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
7139 /* If nothing changed, there's nothing left to do. */
7140 if ((n->value & flags) == flags)
7141 return ret;
7142 flags |= n->value;
7143 n->value = flags;
7145 do_outer:
7146 /* If the variable is private in the current context, then we don't
7147 need to propagate anything to an outer context. */
7148 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
7149 return ret;
7150 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7151 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7152 return ret;
7153 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7154 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7155 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
7156 return ret;
7157 if (ctx->outer_context
7158 && omp_notice_variable (ctx->outer_context, decl, in_code))
7159 return true;
7160 return ret;
7163 /* Verify that DECL is private within CTX. If there's specific information
7164 to the contrary in the innermost scope, generate an error. */
7166 static bool
7167 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
7169 splay_tree_node n;
7171 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7172 if (n != NULL)
7174 if (n->value & GOVD_SHARED)
7176 if (ctx == gimplify_omp_ctxp)
7178 if (simd)
7179 error ("iteration variable %qE is predetermined linear",
7180 DECL_NAME (decl));
7181 else
7182 error ("iteration variable %qE should be private",
7183 DECL_NAME (decl));
7184 n->value = GOVD_PRIVATE;
7185 return true;
7187 else
7188 return false;
7190 else if ((n->value & GOVD_EXPLICIT) != 0
7191 && (ctx == gimplify_omp_ctxp
7192 || (ctx->region_type == ORT_COMBINED_PARALLEL
7193 && gimplify_omp_ctxp->outer_context == ctx)))
7195 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7196 error ("iteration variable %qE should not be firstprivate",
7197 DECL_NAME (decl));
7198 else if ((n->value & GOVD_REDUCTION) != 0)
7199 error ("iteration variable %qE should not be reduction",
7200 DECL_NAME (decl));
7201 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
7202 error ("iteration variable %qE should not be linear",
7203 DECL_NAME (decl));
7204 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
7205 error ("iteration variable %qE should not be lastprivate",
7206 DECL_NAME (decl));
7207 else if (simd && (n->value & GOVD_PRIVATE) != 0)
7208 error ("iteration variable %qE should not be private",
7209 DECL_NAME (decl));
7210 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
7211 error ("iteration variable %qE is predetermined linear",
7212 DECL_NAME (decl));
7214 return (ctx == gimplify_omp_ctxp
7215 || (ctx->region_type == ORT_COMBINED_PARALLEL
7216 && gimplify_omp_ctxp->outer_context == ctx));
7219 if (ctx->region_type != ORT_WORKSHARE
7220 && ctx->region_type != ORT_SIMD
7221 && ctx->region_type != ORT_ACC)
7222 return false;
7223 else if (ctx->outer_context)
7224 return omp_is_private (ctx->outer_context, decl, simd);
7225 return false;
7228 /* Return true if DECL is private within a parallel region
7229 that binds to the current construct's context or in parallel
7230 region's REDUCTION clause. */
7232 static bool
7233 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7235 splay_tree_node n;
7239 ctx = ctx->outer_context;
7240 if (ctx == NULL)
7242 if (is_global_var (decl))
7243 return false;
7245 /* References might be private, but might be shared too,
7246 when checking for copyprivate, assume they might be
7247 private, otherwise assume they might be shared. */
7248 if (copyprivate)
7249 return true;
7251 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7252 return false;
7254 /* Treat C++ privatized non-static data members outside
7255 of the privatization the same. */
7256 if (omp_member_access_dummy_var (decl))
7257 return false;
7259 return true;
7262 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7264 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7265 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7266 continue;
7268 if (n != NULL)
7270 if ((n->value & GOVD_LOCAL) != 0
7271 && omp_member_access_dummy_var (decl))
7272 return false;
7273 return (n->value & GOVD_SHARED) == 0;
7276 while (ctx->region_type == ORT_WORKSHARE
7277 || ctx->region_type == ORT_SIMD
7278 || ctx->region_type == ORT_ACC);
7279 return false;
7282 /* Return true if the CTX is combined with distribute and thus
7283 lastprivate can't be supported. */
7285 static bool
7286 omp_no_lastprivate (struct gimplify_omp_ctx *ctx)
7290 if (ctx->outer_context == NULL)
7291 return false;
7292 ctx = ctx->outer_context;
7293 switch (ctx->region_type)
7295 case ORT_WORKSHARE:
7296 if (!ctx->combined_loop)
7297 return false;
7298 if (ctx->distribute)
7299 return lang_GNU_Fortran ();
7300 break;
7301 case ORT_COMBINED_PARALLEL:
7302 break;
7303 case ORT_COMBINED_TEAMS:
7304 return lang_GNU_Fortran ();
7305 default:
7306 return false;
7309 while (1);
7312 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7314 static tree
7315 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7317 tree t = *tp;
7319 /* If this node has been visited, unmark it and keep looking. */
7320 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7321 return t;
7323 if (IS_TYPE_OR_DECL_P (t))
7324 *walk_subtrees = 0;
7325 return NULL_TREE;
7328 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7329 and previous omp contexts. */
7331 static void
7332 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7333 enum omp_region_type region_type,
7334 enum tree_code code)
7336 struct gimplify_omp_ctx *ctx, *outer_ctx;
7337 tree c;
7338 hash_map<tree, tree> *struct_map_to_clause = NULL;
7339 tree *prev_list_p = NULL;
7341 ctx = new_omp_context (region_type);
7342 outer_ctx = ctx->outer_context;
7343 if (code == OMP_TARGET && !lang_GNU_Fortran ())
7345 ctx->target_map_pointers_as_0len_arrays = true;
7346 /* FIXME: For Fortran we want to set this too, when
7347 the Fortran FE is updated to OpenMP 4.5. */
7348 ctx->target_map_scalars_firstprivate = true;
7350 if (!lang_GNU_Fortran ())
7351 switch (code)
7353 case OMP_TARGET:
7354 case OMP_TARGET_DATA:
7355 case OMP_TARGET_ENTER_DATA:
7356 case OMP_TARGET_EXIT_DATA:
7357 case OACC_HOST_DATA:
7358 ctx->target_firstprivatize_array_bases = true;
7359 default:
7360 break;
7363 while ((c = *list_p) != NULL)
7365 bool remove = false;
7366 bool notice_outer = true;
7367 const char *check_non_private = NULL;
7368 unsigned int flags;
7369 tree decl;
7371 switch (OMP_CLAUSE_CODE (c))
7373 case OMP_CLAUSE_PRIVATE:
7374 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
7375 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
7377 flags |= GOVD_PRIVATE_OUTER_REF;
7378 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
7380 else
7381 notice_outer = false;
7382 goto do_add;
7383 case OMP_CLAUSE_SHARED:
7384 flags = GOVD_SHARED | GOVD_EXPLICIT;
7385 goto do_add;
7386 case OMP_CLAUSE_FIRSTPRIVATE:
7387 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7388 check_non_private = "firstprivate";
7389 goto do_add;
7390 case OMP_CLAUSE_LASTPRIVATE:
7391 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
7392 check_non_private = "lastprivate";
7393 decl = OMP_CLAUSE_DECL (c);
7394 if (omp_no_lastprivate (ctx))
7396 notice_outer = false;
7397 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7399 else if (error_operand_p (decl))
7400 goto do_add;
7401 else if (outer_ctx
7402 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
7403 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
7404 && splay_tree_lookup (outer_ctx->variables,
7405 (splay_tree_key) decl) == NULL)
7407 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
7408 if (outer_ctx->outer_context)
7409 omp_notice_variable (outer_ctx->outer_context, decl, true);
7411 else if (outer_ctx
7412 && (outer_ctx->region_type & ORT_TASK) != 0
7413 && outer_ctx->combined_loop
7414 && splay_tree_lookup (outer_ctx->variables,
7415 (splay_tree_key) decl) == NULL)
7417 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7418 if (outer_ctx->outer_context)
7419 omp_notice_variable (outer_ctx->outer_context, decl, true);
7421 else if (outer_ctx
7422 && (outer_ctx->region_type == ORT_WORKSHARE
7423 || outer_ctx->region_type == ORT_ACC)
7424 && outer_ctx->combined_loop
7425 && splay_tree_lookup (outer_ctx->variables,
7426 (splay_tree_key) decl) == NULL
7427 && !omp_check_private (outer_ctx, decl, false))
7429 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7430 if (outer_ctx->outer_context
7431 && (outer_ctx->outer_context->region_type
7432 == ORT_COMBINED_PARALLEL)
7433 && splay_tree_lookup (outer_ctx->outer_context->variables,
7434 (splay_tree_key) decl) == NULL)
7436 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
7437 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
7438 if (octx->outer_context)
7439 omp_notice_variable (octx->outer_context, decl, true);
7441 else if (outer_ctx->outer_context)
7442 omp_notice_variable (outer_ctx->outer_context, decl, true);
7444 goto do_add;
7445 case OMP_CLAUSE_REDUCTION:
7446 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
7447 /* OpenACC permits reductions on private variables. */
7448 if (!(region_type & ORT_ACC))
7449 check_non_private = "reduction";
7450 decl = OMP_CLAUSE_DECL (c);
7451 if (TREE_CODE (decl) == MEM_REF)
7453 tree type = TREE_TYPE (decl);
7454 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
7455 NULL, is_gimple_val, fb_rvalue, false)
7456 == GS_ERROR)
7458 remove = true;
7459 break;
7461 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7462 if (DECL_P (v))
7464 omp_firstprivatize_variable (ctx, v);
7465 omp_notice_variable (ctx, v, true);
7467 decl = TREE_OPERAND (decl, 0);
7468 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
7470 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
7471 NULL, is_gimple_val, fb_rvalue, false)
7472 == GS_ERROR)
7474 remove = true;
7475 break;
7477 v = TREE_OPERAND (decl, 1);
7478 if (DECL_P (v))
7480 omp_firstprivatize_variable (ctx, v);
7481 omp_notice_variable (ctx, v, true);
7483 decl = TREE_OPERAND (decl, 0);
7485 if (TREE_CODE (decl) == ADDR_EXPR
7486 || TREE_CODE (decl) == INDIRECT_REF)
7487 decl = TREE_OPERAND (decl, 0);
7489 goto do_add_decl;
7490 case OMP_CLAUSE_LINEAR:
7491 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
7492 is_gimple_val, fb_rvalue) == GS_ERROR)
7494 remove = true;
7495 break;
7497 else
7499 if (code == OMP_SIMD
7500 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7502 struct gimplify_omp_ctx *octx = outer_ctx;
7503 if (octx
7504 && octx->region_type == ORT_WORKSHARE
7505 && octx->combined_loop
7506 && !octx->distribute)
7508 if (octx->outer_context
7509 && (octx->outer_context->region_type
7510 == ORT_COMBINED_PARALLEL))
7511 octx = octx->outer_context->outer_context;
7512 else
7513 octx = octx->outer_context;
7515 if (octx
7516 && octx->region_type == ORT_WORKSHARE
7517 && octx->combined_loop
7518 && octx->distribute
7519 && !lang_GNU_Fortran ())
7521 error_at (OMP_CLAUSE_LOCATION (c),
7522 "%<linear%> clause for variable other than "
7523 "loop iterator specified on construct "
7524 "combined with %<distribute%>");
7525 remove = true;
7526 break;
7529 /* For combined #pragma omp parallel for simd, need to put
7530 lastprivate and perhaps firstprivate too on the
7531 parallel. Similarly for #pragma omp for simd. */
7532 struct gimplify_omp_ctx *octx = outer_ctx;
7533 decl = NULL_TREE;
7534 if (omp_no_lastprivate (ctx))
7535 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
7538 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7539 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7540 break;
7541 decl = OMP_CLAUSE_DECL (c);
7542 if (error_operand_p (decl))
7544 decl = NULL_TREE;
7545 break;
7547 flags = GOVD_SEEN;
7548 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7549 flags |= GOVD_FIRSTPRIVATE;
7550 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7551 flags |= GOVD_LASTPRIVATE;
7552 if (octx
7553 && octx->region_type == ORT_WORKSHARE
7554 && octx->combined_loop)
7556 if (octx->outer_context
7557 && (octx->outer_context->region_type
7558 == ORT_COMBINED_PARALLEL))
7559 octx = octx->outer_context;
7560 else if (omp_check_private (octx, decl, false))
7561 break;
7563 else if (octx
7564 && (octx->region_type & ORT_TASK) != 0
7565 && octx->combined_loop)
7567 else if (octx
7568 && octx->region_type == ORT_COMBINED_PARALLEL
7569 && ctx->region_type == ORT_WORKSHARE
7570 && octx == outer_ctx)
7571 flags = GOVD_SEEN | GOVD_SHARED;
7572 else if (octx
7573 && octx->region_type == ORT_COMBINED_TEAMS)
7574 flags = GOVD_SEEN | GOVD_SHARED;
7575 else if (octx
7576 && octx->region_type == ORT_COMBINED_TARGET)
7578 flags &= ~GOVD_LASTPRIVATE;
7579 if (flags == GOVD_SEEN)
7580 break;
7582 else
7583 break;
7584 splay_tree_node on
7585 = splay_tree_lookup (octx->variables,
7586 (splay_tree_key) decl);
7587 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
7589 octx = NULL;
7590 break;
7592 omp_add_variable (octx, decl, flags);
7593 if (octx->outer_context == NULL)
7594 break;
7595 octx = octx->outer_context;
7597 while (1);
7598 if (octx
7599 && decl
7600 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7601 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7602 omp_notice_variable (octx, decl, true);
7604 flags = GOVD_LINEAR | GOVD_EXPLICIT;
7605 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7606 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7608 notice_outer = false;
7609 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7611 goto do_add;
7613 case OMP_CLAUSE_MAP:
7614 decl = OMP_CLAUSE_DECL (c);
7615 if (error_operand_p (decl))
7616 remove = true;
7617 switch (code)
7619 case OMP_TARGET:
7620 break;
7621 case OACC_DATA:
7622 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
7623 break;
7624 /* FALLTHRU */
7625 case OMP_TARGET_DATA:
7626 case OMP_TARGET_ENTER_DATA:
7627 case OMP_TARGET_EXIT_DATA:
7628 case OACC_ENTER_DATA:
7629 case OACC_EXIT_DATA:
7630 case OACC_HOST_DATA:
7631 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7632 || (OMP_CLAUSE_MAP_KIND (c)
7633 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7634 /* For target {,enter ,exit }data only the array slice is
7635 mapped, but not the pointer to it. */
7636 remove = true;
7637 break;
7638 default:
7639 break;
7641 if (remove)
7642 break;
7643 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
7645 struct gimplify_omp_ctx *octx;
7646 for (octx = outer_ctx; octx; octx = octx->outer_context)
7648 if (octx->region_type != ORT_ACC_HOST_DATA)
7649 break;
7650 splay_tree_node n2
7651 = splay_tree_lookup (octx->variables,
7652 (splay_tree_key) decl);
7653 if (n2)
7654 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
7655 "declared in enclosing %<host_data%> region",
7656 DECL_NAME (decl));
7659 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7660 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7661 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7662 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7663 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7665 remove = true;
7666 break;
7668 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7669 || (OMP_CLAUSE_MAP_KIND (c)
7670 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7671 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
7673 OMP_CLAUSE_SIZE (c)
7674 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
7675 false);
7676 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
7677 GOVD_FIRSTPRIVATE | GOVD_SEEN);
7679 if (!DECL_P (decl))
7681 tree d = decl, *pd;
7682 if (TREE_CODE (d) == ARRAY_REF)
7684 while (TREE_CODE (d) == ARRAY_REF)
7685 d = TREE_OPERAND (d, 0);
7686 if (TREE_CODE (d) == COMPONENT_REF
7687 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
7688 decl = d;
7690 pd = &OMP_CLAUSE_DECL (c);
7691 if (d == decl
7692 && TREE_CODE (decl) == INDIRECT_REF
7693 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7694 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7695 == REFERENCE_TYPE))
7697 pd = &TREE_OPERAND (decl, 0);
7698 decl = TREE_OPERAND (decl, 0);
7700 if (TREE_CODE (decl) == COMPONENT_REF)
7702 while (TREE_CODE (decl) == COMPONENT_REF)
7703 decl = TREE_OPERAND (decl, 0);
7704 if (TREE_CODE (decl) == INDIRECT_REF
7705 && DECL_P (TREE_OPERAND (decl, 0))
7706 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7707 == REFERENCE_TYPE))
7708 decl = TREE_OPERAND (decl, 0);
7710 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
7711 == GS_ERROR)
7713 remove = true;
7714 break;
7716 if (DECL_P (decl))
7718 if (error_operand_p (decl))
7720 remove = true;
7721 break;
7724 tree stype = TREE_TYPE (decl);
7725 if (TREE_CODE (stype) == REFERENCE_TYPE)
7726 stype = TREE_TYPE (stype);
7727 if (TYPE_SIZE_UNIT (stype) == NULL
7728 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
7730 error_at (OMP_CLAUSE_LOCATION (c),
7731 "mapping field %qE of variable length "
7732 "structure", OMP_CLAUSE_DECL (c));
7733 remove = true;
7734 break;
7737 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7739 /* Error recovery. */
7740 if (prev_list_p == NULL)
7742 remove = true;
7743 break;
7745 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7747 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
7748 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
7750 remove = true;
7751 break;
7756 tree offset;
7757 HOST_WIDE_INT bitsize, bitpos;
7758 machine_mode mode;
7759 int unsignedp, reversep, volatilep = 0;
7760 tree base = OMP_CLAUSE_DECL (c);
7761 while (TREE_CODE (base) == ARRAY_REF)
7762 base = TREE_OPERAND (base, 0);
7763 if (TREE_CODE (base) == INDIRECT_REF)
7764 base = TREE_OPERAND (base, 0);
7765 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7766 &mode, &unsignedp, &reversep,
7767 &volatilep);
7768 tree orig_base = base;
7769 if ((TREE_CODE (base) == INDIRECT_REF
7770 || (TREE_CODE (base) == MEM_REF
7771 && integer_zerop (TREE_OPERAND (base, 1))))
7772 && DECL_P (TREE_OPERAND (base, 0))
7773 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
7774 == REFERENCE_TYPE))
7775 base = TREE_OPERAND (base, 0);
7776 gcc_assert (base == decl
7777 && (offset == NULL_TREE
7778 || TREE_CODE (offset) == INTEGER_CST));
7780 splay_tree_node n
7781 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7782 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
7783 == GOMP_MAP_ALWAYS_POINTER);
7784 if (n == NULL || (n->value & GOVD_MAP) == 0)
7786 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7787 OMP_CLAUSE_MAP);
7788 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
7789 if (orig_base != base)
7790 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
7791 else
7792 OMP_CLAUSE_DECL (l) = decl;
7793 OMP_CLAUSE_SIZE (l) = size_int (1);
7794 if (struct_map_to_clause == NULL)
7795 struct_map_to_clause = new hash_map<tree, tree>;
7796 struct_map_to_clause->put (decl, l);
7797 if (ptr)
7799 enum gomp_map_kind mkind
7800 = code == OMP_TARGET_EXIT_DATA
7801 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
7802 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7803 OMP_CLAUSE_MAP);
7804 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
7805 OMP_CLAUSE_DECL (c2)
7806 = unshare_expr (OMP_CLAUSE_DECL (c));
7807 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
7808 OMP_CLAUSE_SIZE (c2)
7809 = TYPE_SIZE_UNIT (ptr_type_node);
7810 OMP_CLAUSE_CHAIN (l) = c2;
7811 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7813 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
7814 tree c3
7815 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7816 OMP_CLAUSE_MAP);
7817 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
7818 OMP_CLAUSE_DECL (c3)
7819 = unshare_expr (OMP_CLAUSE_DECL (c4));
7820 OMP_CLAUSE_SIZE (c3)
7821 = TYPE_SIZE_UNIT (ptr_type_node);
7822 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
7823 OMP_CLAUSE_CHAIN (c2) = c3;
7825 *prev_list_p = l;
7826 prev_list_p = NULL;
7828 else
7830 OMP_CLAUSE_CHAIN (l) = c;
7831 *list_p = l;
7832 list_p = &OMP_CLAUSE_CHAIN (l);
7834 if (orig_base != base && code == OMP_TARGET)
7836 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7837 OMP_CLAUSE_MAP);
7838 enum gomp_map_kind mkind
7839 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
7840 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
7841 OMP_CLAUSE_DECL (c2) = decl;
7842 OMP_CLAUSE_SIZE (c2) = size_zero_node;
7843 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
7844 OMP_CLAUSE_CHAIN (l) = c2;
7846 flags = GOVD_MAP | GOVD_EXPLICIT;
7847 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
7848 flags |= GOVD_SEEN;
7849 goto do_add_decl;
7851 else
7853 tree *osc = struct_map_to_clause->get (decl);
7854 tree *sc = NULL, *scp = NULL;
7855 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
7856 n->value |= GOVD_SEEN;
7857 offset_int o1, o2;
7858 if (offset)
7859 o1 = wi::to_offset (offset);
7860 else
7861 o1 = 0;
7862 if (bitpos)
7863 o1 = o1 + bitpos / BITS_PER_UNIT;
7864 sc = &OMP_CLAUSE_CHAIN (*osc);
7865 if (*sc != c
7866 && (OMP_CLAUSE_MAP_KIND (*sc)
7867 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7868 sc = &OMP_CLAUSE_CHAIN (*sc);
7869 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
7870 if (ptr && sc == prev_list_p)
7871 break;
7872 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
7873 != COMPONENT_REF
7874 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
7875 != INDIRECT_REF)
7876 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
7877 != ARRAY_REF))
7878 break;
7879 else
7881 tree offset2;
7882 HOST_WIDE_INT bitsize2, bitpos2;
7883 base = OMP_CLAUSE_DECL (*sc);
7884 if (TREE_CODE (base) == ARRAY_REF)
7886 while (TREE_CODE (base) == ARRAY_REF)
7887 base = TREE_OPERAND (base, 0);
7888 if (TREE_CODE (base) != COMPONENT_REF
7889 || (TREE_CODE (TREE_TYPE (base))
7890 != ARRAY_TYPE))
7891 break;
7893 else if (TREE_CODE (base) == INDIRECT_REF
7894 && (TREE_CODE (TREE_OPERAND (base, 0))
7895 == COMPONENT_REF)
7896 && (TREE_CODE (TREE_TYPE
7897 (TREE_OPERAND (base, 0)))
7898 == REFERENCE_TYPE))
7899 base = TREE_OPERAND (base, 0);
7900 base = get_inner_reference (base, &bitsize2,
7901 &bitpos2, &offset2,
7902 &mode, &unsignedp,
7903 &reversep, &volatilep);
7904 if ((TREE_CODE (base) == INDIRECT_REF
7905 || (TREE_CODE (base) == MEM_REF
7906 && integer_zerop (TREE_OPERAND (base,
7907 1))))
7908 && DECL_P (TREE_OPERAND (base, 0))
7909 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
7910 0)))
7911 == REFERENCE_TYPE))
7912 base = TREE_OPERAND (base, 0);
7913 if (base != decl)
7914 break;
7915 if (scp)
7916 continue;
7917 gcc_assert (offset == NULL_TREE
7918 || TREE_CODE (offset) == INTEGER_CST);
7919 tree d1 = OMP_CLAUSE_DECL (*sc);
7920 tree d2 = OMP_CLAUSE_DECL (c);
7921 while (TREE_CODE (d1) == ARRAY_REF)
7922 d1 = TREE_OPERAND (d1, 0);
7923 while (TREE_CODE (d2) == ARRAY_REF)
7924 d2 = TREE_OPERAND (d2, 0);
7925 if (TREE_CODE (d1) == INDIRECT_REF)
7926 d1 = TREE_OPERAND (d1, 0);
7927 if (TREE_CODE (d2) == INDIRECT_REF)
7928 d2 = TREE_OPERAND (d2, 0);
7929 while (TREE_CODE (d1) == COMPONENT_REF)
7930 if (TREE_CODE (d2) == COMPONENT_REF
7931 && TREE_OPERAND (d1, 1)
7932 == TREE_OPERAND (d2, 1))
7934 d1 = TREE_OPERAND (d1, 0);
7935 d2 = TREE_OPERAND (d2, 0);
7937 else
7938 break;
7939 if (d1 == d2)
7941 error_at (OMP_CLAUSE_LOCATION (c),
7942 "%qE appears more than once in map "
7943 "clauses", OMP_CLAUSE_DECL (c));
7944 remove = true;
7945 break;
7947 if (offset2)
7948 o2 = wi::to_offset (offset2);
7949 else
7950 o2 = 0;
7951 if (bitpos2)
7952 o2 = o2 + bitpos2 / BITS_PER_UNIT;
7953 if (wi::ltu_p (o1, o2)
7954 || (wi::eq_p (o1, o2) && bitpos < bitpos2))
7956 if (ptr)
7957 scp = sc;
7958 else
7959 break;
7962 if (remove)
7963 break;
7964 OMP_CLAUSE_SIZE (*osc)
7965 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
7966 size_one_node);
7967 if (ptr)
7969 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7970 OMP_CLAUSE_MAP);
7971 tree cl = NULL_TREE;
7972 enum gomp_map_kind mkind
7973 = code == OMP_TARGET_EXIT_DATA
7974 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
7975 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
7976 OMP_CLAUSE_DECL (c2)
7977 = unshare_expr (OMP_CLAUSE_DECL (c));
7978 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
7979 OMP_CLAUSE_SIZE (c2)
7980 = TYPE_SIZE_UNIT (ptr_type_node);
7981 cl = scp ? *prev_list_p : c2;
7982 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7984 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
7985 tree c3
7986 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7987 OMP_CLAUSE_MAP);
7988 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
7989 OMP_CLAUSE_DECL (c3)
7990 = unshare_expr (OMP_CLAUSE_DECL (c4));
7991 OMP_CLAUSE_SIZE (c3)
7992 = TYPE_SIZE_UNIT (ptr_type_node);
7993 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
7994 if (!scp)
7995 OMP_CLAUSE_CHAIN (c2) = c3;
7996 else
7997 cl = c3;
7999 if (scp)
8000 *scp = c2;
8001 if (sc == prev_list_p)
8003 *sc = cl;
8004 prev_list_p = NULL;
8006 else
8008 *prev_list_p = OMP_CLAUSE_CHAIN (c);
8009 list_p = prev_list_p;
8010 prev_list_p = NULL;
8011 OMP_CLAUSE_CHAIN (c) = *sc;
8012 *sc = cl;
8013 continue;
8016 else if (*sc != c)
8018 *list_p = OMP_CLAUSE_CHAIN (c);
8019 OMP_CLAUSE_CHAIN (c) = *sc;
8020 *sc = c;
8021 continue;
8025 if (!remove
8026 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8027 && OMP_CLAUSE_CHAIN (c)
8028 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
8029 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
8030 == GOMP_MAP_ALWAYS_POINTER))
8031 prev_list_p = list_p;
8032 break;
8034 flags = GOVD_MAP | GOVD_EXPLICIT;
8035 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
8036 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
8037 flags |= GOVD_MAP_ALWAYS_TO;
8038 goto do_add;
8040 case OMP_CLAUSE_DEPEND:
8041 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
8042 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8044 /* Nothing to do. OMP_CLAUSE_DECL will be lowered in
8045 omp-low.c. */
8046 break;
8048 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
8050 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
8051 NULL, is_gimple_val, fb_rvalue);
8052 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
8054 if (error_operand_p (OMP_CLAUSE_DECL (c)))
8056 remove = true;
8057 break;
8059 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
8060 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
8061 is_gimple_val, fb_rvalue) == GS_ERROR)
8063 remove = true;
8064 break;
8066 break;
8068 case OMP_CLAUSE_TO:
8069 case OMP_CLAUSE_FROM:
8070 case OMP_CLAUSE__CACHE_:
8071 decl = OMP_CLAUSE_DECL (c);
8072 if (error_operand_p (decl))
8074 remove = true;
8075 break;
8077 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8078 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
8079 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
8080 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
8081 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
8083 remove = true;
8084 break;
8086 if (!DECL_P (decl))
8088 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
8089 NULL, is_gimple_lvalue, fb_lvalue)
8090 == GS_ERROR)
8092 remove = true;
8093 break;
8095 break;
8097 goto do_notice;
8099 case OMP_CLAUSE_USE_DEVICE_PTR:
8100 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8101 goto do_add;
8102 case OMP_CLAUSE_IS_DEVICE_PTR:
8103 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
8104 goto do_add;
8106 do_add:
8107 decl = OMP_CLAUSE_DECL (c);
8108 do_add_decl:
8109 if (error_operand_p (decl))
8111 remove = true;
8112 break;
8114 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
8116 tree t = omp_member_access_dummy_var (decl);
8117 if (t)
8119 tree v = DECL_VALUE_EXPR (decl);
8120 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
8121 if (outer_ctx)
8122 omp_notice_variable (outer_ctx, t, true);
8125 if (code == OACC_DATA
8126 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8127 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8128 flags |= GOVD_MAP_0LEN_ARRAY;
8129 omp_add_variable (ctx, decl, flags);
8130 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8131 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
8133 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
8134 GOVD_LOCAL | GOVD_SEEN);
8135 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
8136 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
8137 find_decl_expr,
8138 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8139 NULL) == NULL_TREE)
8140 omp_add_variable (ctx,
8141 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
8142 GOVD_LOCAL | GOVD_SEEN);
8143 gimplify_omp_ctxp = ctx;
8144 push_gimplify_context ();
8146 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
8147 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
8149 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
8150 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
8151 pop_gimplify_context
8152 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
8153 push_gimplify_context ();
8154 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
8155 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
8156 pop_gimplify_context
8157 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
8158 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
8159 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
8161 gimplify_omp_ctxp = outer_ctx;
8163 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8164 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
8166 gimplify_omp_ctxp = ctx;
8167 push_gimplify_context ();
8168 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
8170 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8171 NULL, NULL);
8172 TREE_SIDE_EFFECTS (bind) = 1;
8173 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
8174 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
8176 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
8177 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
8178 pop_gimplify_context
8179 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
8180 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
8182 gimplify_omp_ctxp = outer_ctx;
8184 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8185 && OMP_CLAUSE_LINEAR_STMT (c))
8187 gimplify_omp_ctxp = ctx;
8188 push_gimplify_context ();
8189 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8191 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8192 NULL, NULL);
8193 TREE_SIDE_EFFECTS (bind) = 1;
8194 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8195 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8197 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8198 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8199 pop_gimplify_context
8200 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8201 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8203 gimplify_omp_ctxp = outer_ctx;
8205 if (notice_outer)
8206 goto do_notice;
8207 break;
8209 case OMP_CLAUSE_COPYIN:
8210 case OMP_CLAUSE_COPYPRIVATE:
8211 decl = OMP_CLAUSE_DECL (c);
8212 if (error_operand_p (decl))
8214 remove = true;
8215 break;
8217 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8218 && !remove
8219 && !omp_check_private (ctx, decl, true))
8221 remove = true;
8222 if (is_global_var (decl))
8224 if (DECL_THREAD_LOCAL_P (decl))
8225 remove = false;
8226 else if (DECL_HAS_VALUE_EXPR_P (decl))
8228 tree value = get_base_address (DECL_VALUE_EXPR (decl));
8230 if (value
8231 && DECL_P (value)
8232 && DECL_THREAD_LOCAL_P (value))
8233 remove = false;
8236 if (remove)
8237 error_at (OMP_CLAUSE_LOCATION (c),
8238 "copyprivate variable %qE is not threadprivate"
8239 " or private in outer context", DECL_NAME (decl));
8241 do_notice:
8242 if (outer_ctx)
8243 omp_notice_variable (outer_ctx, decl, true);
8244 if (check_non_private
8245 && region_type == ORT_WORKSHARE
8246 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8247 || decl == OMP_CLAUSE_DECL (c)
8248 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
8249 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8250 == ADDR_EXPR
8251 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8252 == POINTER_PLUS_EXPR
8253 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8254 (OMP_CLAUSE_DECL (c), 0), 0))
8255 == ADDR_EXPR)))))
8256 && omp_check_private (ctx, decl, false))
8258 error ("%s variable %qE is private in outer context",
8259 check_non_private, DECL_NAME (decl));
8260 remove = true;
8262 break;
8264 case OMP_CLAUSE_IF:
8265 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
8266 && OMP_CLAUSE_IF_MODIFIER (c) != code)
8268 const char *p[2];
8269 for (int i = 0; i < 2; i++)
8270 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
8272 case OMP_PARALLEL: p[i] = "parallel"; break;
8273 case OMP_TASK: p[i] = "task"; break;
8274 case OMP_TASKLOOP: p[i] = "taskloop"; break;
8275 case OMP_TARGET_DATA: p[i] = "target data"; break;
8276 case OMP_TARGET: p[i] = "target"; break;
8277 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
8278 case OMP_TARGET_ENTER_DATA:
8279 p[i] = "target enter data"; break;
8280 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
8281 default: gcc_unreachable ();
8283 error_at (OMP_CLAUSE_LOCATION (c),
8284 "expected %qs %<if%> clause modifier rather than %qs",
8285 p[0], p[1]);
8286 remove = true;
8288 /* Fall through. */
8290 case OMP_CLAUSE_FINAL:
8291 OMP_CLAUSE_OPERAND (c, 0)
8292 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
8293 /* Fall through. */
8295 case OMP_CLAUSE_SCHEDULE:
8296 case OMP_CLAUSE_NUM_THREADS:
8297 case OMP_CLAUSE_NUM_TEAMS:
8298 case OMP_CLAUSE_THREAD_LIMIT:
8299 case OMP_CLAUSE_DIST_SCHEDULE:
8300 case OMP_CLAUSE_DEVICE:
8301 case OMP_CLAUSE_PRIORITY:
8302 case OMP_CLAUSE_GRAINSIZE:
8303 case OMP_CLAUSE_NUM_TASKS:
8304 case OMP_CLAUSE_HINT:
8305 case OMP_CLAUSE__CILK_FOR_COUNT_:
8306 case OMP_CLAUSE_ASYNC:
8307 case OMP_CLAUSE_WAIT:
8308 case OMP_CLAUSE_NUM_GANGS:
8309 case OMP_CLAUSE_NUM_WORKERS:
8310 case OMP_CLAUSE_VECTOR_LENGTH:
8311 case OMP_CLAUSE_WORKER:
8312 case OMP_CLAUSE_VECTOR:
8313 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8314 is_gimple_val, fb_rvalue) == GS_ERROR)
8315 remove = true;
8316 break;
8318 case OMP_CLAUSE_GANG:
8319 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8320 is_gimple_val, fb_rvalue) == GS_ERROR)
8321 remove = true;
8322 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
8323 is_gimple_val, fb_rvalue) == GS_ERROR)
8324 remove = true;
8325 break;
8327 case OMP_CLAUSE_TILE:
8328 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
8329 list = TREE_CHAIN (list))
8331 if (gimplify_expr (&TREE_VALUE (list), pre_p, NULL,
8332 is_gimple_val, fb_rvalue) == GS_ERROR)
8333 remove = true;
8335 break;
8337 case OMP_CLAUSE_NOWAIT:
8338 case OMP_CLAUSE_ORDERED:
8339 case OMP_CLAUSE_UNTIED:
8340 case OMP_CLAUSE_COLLAPSE:
8341 case OMP_CLAUSE_AUTO:
8342 case OMP_CLAUSE_SEQ:
8343 case OMP_CLAUSE_INDEPENDENT:
8344 case OMP_CLAUSE_MERGEABLE:
8345 case OMP_CLAUSE_PROC_BIND:
8346 case OMP_CLAUSE_SAFELEN:
8347 case OMP_CLAUSE_SIMDLEN:
8348 case OMP_CLAUSE_NOGROUP:
8349 case OMP_CLAUSE_THREADS:
8350 case OMP_CLAUSE_SIMD:
8351 break;
8353 case OMP_CLAUSE_DEFAULTMAP:
8354 ctx->target_map_scalars_firstprivate = false;
8355 break;
8357 case OMP_CLAUSE_ALIGNED:
8358 decl = OMP_CLAUSE_DECL (c);
8359 if (error_operand_p (decl))
8361 remove = true;
8362 break;
8364 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
8365 is_gimple_val, fb_rvalue) == GS_ERROR)
8367 remove = true;
8368 break;
8370 if (!is_global_var (decl)
8371 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8372 omp_add_variable (ctx, decl, GOVD_ALIGNED);
8373 break;
8375 case OMP_CLAUSE_DEFAULT:
8376 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
8377 break;
8379 default:
8380 gcc_unreachable ();
8383 if (code == OACC_DATA
8384 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8385 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8386 remove = true;
8387 if (remove)
8388 *list_p = OMP_CLAUSE_CHAIN (c);
8389 else
8390 list_p = &OMP_CLAUSE_CHAIN (c);
8393 gimplify_omp_ctxp = ctx;
8394 if (struct_map_to_clause)
8395 delete struct_map_to_clause;
8398 /* Return true if DECL is a candidate for shared to firstprivate
8399 optimization. We only consider non-addressable scalars, not
8400 too big, and not references. */
8402 static bool
8403 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
8405 if (TREE_ADDRESSABLE (decl))
8406 return false;
8407 tree type = TREE_TYPE (decl);
8408 if (!is_gimple_reg_type (type)
8409 || TREE_CODE (type) == REFERENCE_TYPE
8410 || TREE_ADDRESSABLE (type))
8411 return false;
8412 /* Don't optimize too large decls, as each thread/task will have
8413 its own. */
8414 HOST_WIDE_INT len = int_size_in_bytes (type);
8415 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
8416 return false;
8417 if (lang_hooks.decls.omp_privatize_by_reference (decl))
8418 return false;
8419 return true;
8422 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8423 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8424 GOVD_WRITTEN in outer contexts. */
8426 static void
8427 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
8429 for (; ctx; ctx = ctx->outer_context)
8431 splay_tree_node n = splay_tree_lookup (ctx->variables,
8432 (splay_tree_key) decl);
8433 if (n == NULL)
8434 continue;
8435 else if (n->value & GOVD_SHARED)
8437 n->value |= GOVD_WRITTEN;
8438 return;
8440 else if (n->value & GOVD_DATA_SHARE_CLASS)
8441 return;
8445 /* Helper callback for walk_gimple_seq to discover possible stores
8446 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8447 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8448 for those. */
8450 static tree
8451 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
8453 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
8455 *walk_subtrees = 0;
8456 if (!wi->is_lhs)
8457 return NULL_TREE;
8459 tree op = *tp;
8462 if (handled_component_p (op))
8463 op = TREE_OPERAND (op, 0);
8464 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
8465 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
8466 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8467 else
8468 break;
8470 while (1);
8471 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
8472 return NULL_TREE;
8474 omp_mark_stores (gimplify_omp_ctxp, op);
8475 return NULL_TREE;
8478 /* Helper callback for walk_gimple_seq to discover possible stores
8479 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8480 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8481 for those. */
8483 static tree
8484 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
8485 bool *handled_ops_p,
8486 struct walk_stmt_info *wi)
8488 gimple *stmt = gsi_stmt (*gsi_p);
8489 switch (gimple_code (stmt))
8491 /* Don't recurse on OpenMP constructs for which
8492 gimplify_adjust_omp_clauses already handled the bodies,
8493 except handle gimple_omp_for_pre_body. */
8494 case GIMPLE_OMP_FOR:
8495 *handled_ops_p = true;
8496 if (gimple_omp_for_pre_body (stmt))
8497 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
8498 omp_find_stores_stmt, omp_find_stores_op, wi);
8499 break;
8500 case GIMPLE_OMP_PARALLEL:
8501 case GIMPLE_OMP_TASK:
8502 case GIMPLE_OMP_SECTIONS:
8503 case GIMPLE_OMP_SINGLE:
8504 case GIMPLE_OMP_TARGET:
8505 case GIMPLE_OMP_TEAMS:
8506 case GIMPLE_OMP_CRITICAL:
8507 *handled_ops_p = true;
8508 break;
8509 default:
8510 break;
8512 return NULL_TREE;
8515 struct gimplify_adjust_omp_clauses_data
8517 tree *list_p;
8518 gimple_seq *pre_p;
8521 /* For all variables that were not actually used within the context,
8522 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
8524 static int
8525 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
8527 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
8528 gimple_seq *pre_p
8529 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
8530 tree decl = (tree) n->key;
8531 unsigned flags = n->value;
8532 enum omp_clause_code code;
8533 tree clause;
8534 bool private_debug;
8536 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
8537 return 0;
8538 if ((flags & GOVD_SEEN) == 0)
8539 return 0;
8540 if (flags & GOVD_DEBUG_PRIVATE)
8542 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
8543 private_debug = true;
8545 else if (flags & GOVD_MAP)
8546 private_debug = false;
8547 else
8548 private_debug
8549 = lang_hooks.decls.omp_private_debug_clause (decl,
8550 !!(flags & GOVD_SHARED));
8551 if (private_debug)
8552 code = OMP_CLAUSE_PRIVATE;
8553 else if (flags & GOVD_MAP)
8555 code = OMP_CLAUSE_MAP;
8556 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8557 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8559 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
8560 return 0;
8563 else if (flags & GOVD_SHARED)
8565 if (is_global_var (decl))
8567 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8568 while (ctx != NULL)
8570 splay_tree_node on
8571 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8572 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
8573 | GOVD_PRIVATE | GOVD_REDUCTION
8574 | GOVD_LINEAR | GOVD_MAP)) != 0)
8575 break;
8576 ctx = ctx->outer_context;
8578 if (ctx == NULL)
8579 return 0;
8581 code = OMP_CLAUSE_SHARED;
8583 else if (flags & GOVD_PRIVATE)
8584 code = OMP_CLAUSE_PRIVATE;
8585 else if (flags & GOVD_FIRSTPRIVATE)
8587 code = OMP_CLAUSE_FIRSTPRIVATE;
8588 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
8589 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8590 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8592 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8593 "%<target%> construct", decl);
8594 return 0;
8597 else if (flags & GOVD_LASTPRIVATE)
8598 code = OMP_CLAUSE_LASTPRIVATE;
8599 else if (flags & GOVD_ALIGNED)
8600 return 0;
8601 else
8602 gcc_unreachable ();
8604 if (((flags & GOVD_LASTPRIVATE)
8605 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
8606 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8607 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8609 tree chain = *list_p;
8610 clause = build_omp_clause (input_location, code);
8611 OMP_CLAUSE_DECL (clause) = decl;
8612 OMP_CLAUSE_CHAIN (clause) = chain;
8613 if (private_debug)
8614 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
8615 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
8616 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
8617 else if (code == OMP_CLAUSE_SHARED
8618 && (flags & GOVD_WRITTEN) == 0
8619 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8620 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
8621 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
8622 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
8623 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
8625 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
8626 OMP_CLAUSE_DECL (nc) = decl;
8627 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
8628 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
8629 OMP_CLAUSE_DECL (clause)
8630 = build_simple_mem_ref_loc (input_location, decl);
8631 OMP_CLAUSE_DECL (clause)
8632 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
8633 build_int_cst (build_pointer_type (char_type_node), 0));
8634 OMP_CLAUSE_SIZE (clause) = size_zero_node;
8635 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8636 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
8637 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
8638 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8639 OMP_CLAUSE_CHAIN (nc) = chain;
8640 OMP_CLAUSE_CHAIN (clause) = nc;
8641 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8642 gimplify_omp_ctxp = ctx->outer_context;
8643 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
8644 pre_p, NULL, is_gimple_val, fb_rvalue);
8645 gimplify_omp_ctxp = ctx;
8647 else if (code == OMP_CLAUSE_MAP)
8649 int kind = (flags & GOVD_MAP_TO_ONLY
8650 ? GOMP_MAP_TO
8651 : GOMP_MAP_TOFROM);
8652 if (flags & GOVD_MAP_FORCE)
8653 kind |= GOMP_MAP_FLAG_FORCE;
8654 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
8655 if (DECL_SIZE (decl)
8656 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8658 tree decl2 = DECL_VALUE_EXPR (decl);
8659 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8660 decl2 = TREE_OPERAND (decl2, 0);
8661 gcc_assert (DECL_P (decl2));
8662 tree mem = build_simple_mem_ref (decl2);
8663 OMP_CLAUSE_DECL (clause) = mem;
8664 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8665 if (gimplify_omp_ctxp->outer_context)
8667 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8668 omp_notice_variable (ctx, decl2, true);
8669 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
8671 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8672 OMP_CLAUSE_MAP);
8673 OMP_CLAUSE_DECL (nc) = decl;
8674 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8675 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
8676 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8677 else
8678 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8679 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8680 OMP_CLAUSE_CHAIN (clause) = nc;
8682 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
8683 && lang_hooks.decls.omp_privatize_by_reference (decl))
8685 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
8686 OMP_CLAUSE_SIZE (clause)
8687 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
8688 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8689 gimplify_omp_ctxp = ctx->outer_context;
8690 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
8691 pre_p, NULL, is_gimple_val, fb_rvalue);
8692 gimplify_omp_ctxp = ctx;
8693 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8694 OMP_CLAUSE_MAP);
8695 OMP_CLAUSE_DECL (nc) = decl;
8696 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8697 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8698 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8699 OMP_CLAUSE_CHAIN (clause) = nc;
8701 else
8702 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
8704 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
8706 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
8707 OMP_CLAUSE_DECL (nc) = decl;
8708 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
8709 OMP_CLAUSE_CHAIN (nc) = chain;
8710 OMP_CLAUSE_CHAIN (clause) = nc;
8711 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8712 gimplify_omp_ctxp = ctx->outer_context;
8713 lang_hooks.decls.omp_finish_clause (nc, pre_p);
8714 gimplify_omp_ctxp = ctx;
8716 *list_p = clause;
8717 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8718 gimplify_omp_ctxp = ctx->outer_context;
8719 lang_hooks.decls.omp_finish_clause (clause, pre_p);
8720 if (gimplify_omp_ctxp)
8721 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
8722 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
8723 && DECL_P (OMP_CLAUSE_SIZE (clause)))
8724 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
8725 true);
8726 gimplify_omp_ctxp = ctx;
8727 return 0;
8730 static void
8731 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
8732 enum tree_code code)
8734 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8735 tree c, decl;
8737 if (body)
8739 struct gimplify_omp_ctx *octx;
8740 for (octx = ctx; octx; octx = octx->outer_context)
8741 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
8742 break;
8743 if (octx)
8745 struct walk_stmt_info wi;
8746 memset (&wi, 0, sizeof (wi));
8747 walk_gimple_seq (body, omp_find_stores_stmt,
8748 omp_find_stores_op, &wi);
8751 while ((c = *list_p) != NULL)
8753 splay_tree_node n;
8754 bool remove = false;
8756 switch (OMP_CLAUSE_CODE (c))
8758 case OMP_CLAUSE_FIRSTPRIVATE:
8759 if ((ctx->region_type & ORT_TARGET)
8760 && (ctx->region_type & ORT_ACC) == 0
8761 && TYPE_ATOMIC (strip_array_types
8762 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
8764 error_at (OMP_CLAUSE_LOCATION (c),
8765 "%<_Atomic%> %qD in %<firstprivate%> clause on "
8766 "%<target%> construct", OMP_CLAUSE_DECL (c));
8767 remove = true;
8768 break;
8770 /* FALLTHRU */
8771 case OMP_CLAUSE_PRIVATE:
8772 case OMP_CLAUSE_SHARED:
8773 case OMP_CLAUSE_LINEAR:
8774 decl = OMP_CLAUSE_DECL (c);
8775 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8776 remove = !(n->value & GOVD_SEEN);
8777 if (! remove)
8779 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
8780 if ((n->value & GOVD_DEBUG_PRIVATE)
8781 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
8783 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
8784 || ((n->value & GOVD_DATA_SHARE_CLASS)
8785 == GOVD_PRIVATE));
8786 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
8787 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
8789 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8790 && (n->value & GOVD_WRITTEN) == 0
8791 && DECL_P (decl)
8792 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8793 OMP_CLAUSE_SHARED_READONLY (c) = 1;
8794 else if (DECL_P (decl)
8795 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8796 && (n->value & GOVD_WRITTEN) != 1)
8797 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8798 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
8799 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8800 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8802 break;
8804 case OMP_CLAUSE_LASTPRIVATE:
8805 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
8806 accurately reflect the presence of a FIRSTPRIVATE clause. */
8807 decl = OMP_CLAUSE_DECL (c);
8808 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8809 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
8810 = (n->value & GOVD_FIRSTPRIVATE) != 0;
8811 if (omp_no_lastprivate (ctx))
8813 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
8814 remove = true;
8815 else
8816 OMP_CLAUSE_CODE (c) = OMP_CLAUSE_PRIVATE;
8818 else if (code == OMP_DISTRIBUTE
8819 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
8821 remove = true;
8822 error_at (OMP_CLAUSE_LOCATION (c),
8823 "same variable used in %<firstprivate%> and "
8824 "%<lastprivate%> clauses on %<distribute%> "
8825 "construct");
8827 if (!remove
8828 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8829 && DECL_P (decl)
8830 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8831 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8832 break;
8834 case OMP_CLAUSE_ALIGNED:
8835 decl = OMP_CLAUSE_DECL (c);
8836 if (!is_global_var (decl))
8838 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8839 remove = n == NULL || !(n->value & GOVD_SEEN);
8840 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8842 struct gimplify_omp_ctx *octx;
8843 if (n != NULL
8844 && (n->value & (GOVD_DATA_SHARE_CLASS
8845 & ~GOVD_FIRSTPRIVATE)))
8846 remove = true;
8847 else
8848 for (octx = ctx->outer_context; octx;
8849 octx = octx->outer_context)
8851 n = splay_tree_lookup (octx->variables,
8852 (splay_tree_key) decl);
8853 if (n == NULL)
8854 continue;
8855 if (n->value & GOVD_LOCAL)
8856 break;
8857 /* We have to avoid assigning a shared variable
8858 to itself when trying to add
8859 __builtin_assume_aligned. */
8860 if (n->value & GOVD_SHARED)
8862 remove = true;
8863 break;
8868 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8870 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8871 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
8872 remove = true;
8874 break;
8876 case OMP_CLAUSE_MAP:
8877 if (code == OMP_TARGET_EXIT_DATA
8878 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
8880 remove = true;
8881 break;
8883 decl = OMP_CLAUSE_DECL (c);
8884 /* Data clauses associated with acc parallel reductions must be
8885 compatible with present_or_copy. Warn and adjust the clause
8886 if that is not the case. */
8887 if (ctx->region_type == ORT_ACC_PARALLEL)
8889 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
8890 n = NULL;
8892 if (DECL_P (t))
8893 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
8895 if (n && (n->value & GOVD_REDUCTION))
8897 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
8899 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
8900 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
8901 && kind != GOMP_MAP_FORCE_PRESENT
8902 && kind != GOMP_MAP_POINTER)
8904 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8905 "incompatible data clause with reduction "
8906 "on %qE; promoting to present_or_copy",
8907 DECL_NAME (t));
8908 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
8912 if (!DECL_P (decl))
8914 if ((ctx->region_type & ORT_TARGET) != 0
8915 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8917 if (TREE_CODE (decl) == INDIRECT_REF
8918 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
8919 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
8920 == REFERENCE_TYPE))
8921 decl = TREE_OPERAND (decl, 0);
8922 if (TREE_CODE (decl) == COMPONENT_REF)
8924 while (TREE_CODE (decl) == COMPONENT_REF)
8925 decl = TREE_OPERAND (decl, 0);
8926 if (DECL_P (decl))
8928 n = splay_tree_lookup (ctx->variables,
8929 (splay_tree_key) decl);
8930 if (!(n->value & GOVD_SEEN))
8931 remove = true;
8935 break;
8937 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8938 if ((ctx->region_type & ORT_TARGET) != 0
8939 && !(n->value & GOVD_SEEN)
8940 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
8941 && !lookup_attribute ("omp declare target link",
8942 DECL_ATTRIBUTES (decl)))
8944 remove = true;
8945 /* For struct element mapping, if struct is never referenced
8946 in target block and none of the mapping has always modifier,
8947 remove all the struct element mappings, which immediately
8948 follow the GOMP_MAP_STRUCT map clause. */
8949 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
8951 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
8952 while (cnt--)
8953 OMP_CLAUSE_CHAIN (c)
8954 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
8957 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
8958 && code == OMP_TARGET_EXIT_DATA)
8959 remove = true;
8960 else if (DECL_SIZE (decl)
8961 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
8962 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
8963 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
8964 && (OMP_CLAUSE_MAP_KIND (c)
8965 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8967 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
8968 for these, TREE_CODE (DECL_SIZE (decl)) will always be
8969 INTEGER_CST. */
8970 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
8972 tree decl2 = DECL_VALUE_EXPR (decl);
8973 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8974 decl2 = TREE_OPERAND (decl2, 0);
8975 gcc_assert (DECL_P (decl2));
8976 tree mem = build_simple_mem_ref (decl2);
8977 OMP_CLAUSE_DECL (c) = mem;
8978 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8979 if (ctx->outer_context)
8981 omp_notice_variable (ctx->outer_context, decl2, true);
8982 omp_notice_variable (ctx->outer_context,
8983 OMP_CLAUSE_SIZE (c), true);
8985 if (((ctx->region_type & ORT_TARGET) != 0
8986 || !ctx->target_firstprivatize_array_bases)
8987 && ((n->value & GOVD_SEEN) == 0
8988 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
8990 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8991 OMP_CLAUSE_MAP);
8992 OMP_CLAUSE_DECL (nc) = decl;
8993 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8994 if (ctx->target_firstprivatize_array_bases)
8995 OMP_CLAUSE_SET_MAP_KIND (nc,
8996 GOMP_MAP_FIRSTPRIVATE_POINTER);
8997 else
8998 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8999 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
9000 OMP_CLAUSE_CHAIN (c) = nc;
9001 c = nc;
9004 else
9006 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9007 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9008 gcc_assert ((n->value & GOVD_SEEN) == 0
9009 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9010 == 0));
9012 break;
9014 case OMP_CLAUSE_TO:
9015 case OMP_CLAUSE_FROM:
9016 case OMP_CLAUSE__CACHE_:
9017 decl = OMP_CLAUSE_DECL (c);
9018 if (!DECL_P (decl))
9019 break;
9020 if (DECL_SIZE (decl)
9021 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
9023 tree decl2 = DECL_VALUE_EXPR (decl);
9024 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
9025 decl2 = TREE_OPERAND (decl2, 0);
9026 gcc_assert (DECL_P (decl2));
9027 tree mem = build_simple_mem_ref (decl2);
9028 OMP_CLAUSE_DECL (c) = mem;
9029 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
9030 if (ctx->outer_context)
9032 omp_notice_variable (ctx->outer_context, decl2, true);
9033 omp_notice_variable (ctx->outer_context,
9034 OMP_CLAUSE_SIZE (c), true);
9037 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
9038 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
9039 break;
9041 case OMP_CLAUSE_REDUCTION:
9042 decl = OMP_CLAUSE_DECL (c);
9043 /* OpenACC reductions need a present_or_copy data clause.
9044 Add one if necessary. Error is the reduction is private. */
9045 if (ctx->region_type == ORT_ACC_PARALLEL)
9047 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
9048 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
9049 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
9050 "reduction on %qE", DECL_NAME (decl));
9051 else if ((n->value & GOVD_MAP) == 0)
9053 tree next = OMP_CLAUSE_CHAIN (c);
9054 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
9055 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
9056 OMP_CLAUSE_DECL (nc) = decl;
9057 OMP_CLAUSE_CHAIN (c) = nc;
9058 lang_hooks.decls.omp_finish_clause (nc, pre_p);
9059 while (1)
9061 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
9062 if (OMP_CLAUSE_CHAIN (nc) == NULL)
9063 break;
9064 nc = OMP_CLAUSE_CHAIN (nc);
9066 OMP_CLAUSE_CHAIN (nc) = next;
9067 n->value |= GOVD_MAP;
9070 if (DECL_P (decl)
9071 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
9072 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
9073 break;
9074 case OMP_CLAUSE_COPYIN:
9075 case OMP_CLAUSE_COPYPRIVATE:
9076 case OMP_CLAUSE_IF:
9077 case OMP_CLAUSE_NUM_THREADS:
9078 case OMP_CLAUSE_NUM_TEAMS:
9079 case OMP_CLAUSE_THREAD_LIMIT:
9080 case OMP_CLAUSE_DIST_SCHEDULE:
9081 case OMP_CLAUSE_DEVICE:
9082 case OMP_CLAUSE_SCHEDULE:
9083 case OMP_CLAUSE_NOWAIT:
9084 case OMP_CLAUSE_ORDERED:
9085 case OMP_CLAUSE_DEFAULT:
9086 case OMP_CLAUSE_UNTIED:
9087 case OMP_CLAUSE_COLLAPSE:
9088 case OMP_CLAUSE_FINAL:
9089 case OMP_CLAUSE_MERGEABLE:
9090 case OMP_CLAUSE_PROC_BIND:
9091 case OMP_CLAUSE_SAFELEN:
9092 case OMP_CLAUSE_SIMDLEN:
9093 case OMP_CLAUSE_DEPEND:
9094 case OMP_CLAUSE_PRIORITY:
9095 case OMP_CLAUSE_GRAINSIZE:
9096 case OMP_CLAUSE_NUM_TASKS:
9097 case OMP_CLAUSE_NOGROUP:
9098 case OMP_CLAUSE_THREADS:
9099 case OMP_CLAUSE_SIMD:
9100 case OMP_CLAUSE_HINT:
9101 case OMP_CLAUSE_DEFAULTMAP:
9102 case OMP_CLAUSE_USE_DEVICE_PTR:
9103 case OMP_CLAUSE_IS_DEVICE_PTR:
9104 case OMP_CLAUSE__CILK_FOR_COUNT_:
9105 case OMP_CLAUSE_ASYNC:
9106 case OMP_CLAUSE_WAIT:
9107 case OMP_CLAUSE_INDEPENDENT:
9108 case OMP_CLAUSE_NUM_GANGS:
9109 case OMP_CLAUSE_NUM_WORKERS:
9110 case OMP_CLAUSE_VECTOR_LENGTH:
9111 case OMP_CLAUSE_GANG:
9112 case OMP_CLAUSE_WORKER:
9113 case OMP_CLAUSE_VECTOR:
9114 case OMP_CLAUSE_AUTO:
9115 case OMP_CLAUSE_SEQ:
9116 break;
9118 case OMP_CLAUSE_TILE:
9119 /* We're not yet making use of the information provided by OpenACC
9120 tile clauses. Discard these here, to simplify later middle end
9121 processing. */
9122 remove = true;
9123 break;
9125 default:
9126 gcc_unreachable ();
9129 if (remove)
9130 *list_p = OMP_CLAUSE_CHAIN (c);
9131 else
9132 list_p = &OMP_CLAUSE_CHAIN (c);
9135 /* Add in any implicit data sharing. */
9136 struct gimplify_adjust_omp_clauses_data data;
9137 data.list_p = list_p;
9138 data.pre_p = pre_p;
9139 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
9141 gimplify_omp_ctxp = ctx->outer_context;
9142 delete_omp_context (ctx);
9145 /* Gimplify OACC_CACHE. */
9147 static void
9148 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
9150 tree expr = *expr_p;
9152 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
9153 OACC_CACHE);
9154 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
9155 OACC_CACHE);
9157 /* TODO: Do something sensible with this information. */
9159 *expr_p = NULL_TREE;
9162 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
9163 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
9164 kind. The entry kind will replace the one in CLAUSE, while the exit
9165 kind will be used in a new omp_clause and returned to the caller. */
9167 static tree
9168 gimplify_oacc_declare_1 (tree clause)
9170 HOST_WIDE_INT kind, new_op;
9171 bool ret = false;
9172 tree c = NULL;
9174 kind = OMP_CLAUSE_MAP_KIND (clause);
9176 switch (kind)
9178 case GOMP_MAP_ALLOC:
9179 case GOMP_MAP_FORCE_ALLOC:
9180 case GOMP_MAP_FORCE_TO:
9181 new_op = GOMP_MAP_DELETE;
9182 ret = true;
9183 break;
9185 case GOMP_MAP_FORCE_FROM:
9186 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9187 new_op = GOMP_MAP_FORCE_FROM;
9188 ret = true;
9189 break;
9191 case GOMP_MAP_FORCE_TOFROM:
9192 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_TO);
9193 new_op = GOMP_MAP_FORCE_FROM;
9194 ret = true;
9195 break;
9197 case GOMP_MAP_FROM:
9198 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9199 new_op = GOMP_MAP_FROM;
9200 ret = true;
9201 break;
9203 case GOMP_MAP_TOFROM:
9204 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
9205 new_op = GOMP_MAP_FROM;
9206 ret = true;
9207 break;
9209 case GOMP_MAP_DEVICE_RESIDENT:
9210 case GOMP_MAP_FORCE_DEVICEPTR:
9211 case GOMP_MAP_FORCE_PRESENT:
9212 case GOMP_MAP_LINK:
9213 case GOMP_MAP_POINTER:
9214 case GOMP_MAP_TO:
9215 break;
9217 default:
9218 gcc_unreachable ();
9219 break;
9222 if (ret)
9224 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
9225 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
9226 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
9229 return c;
9232 /* Gimplify OACC_DECLARE. */
9234 static void
9235 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
9237 tree expr = *expr_p;
9238 gomp_target *stmt;
9239 tree clauses, t;
9241 clauses = OACC_DECLARE_CLAUSES (expr);
9243 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
9245 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
9247 tree decl = OMP_CLAUSE_DECL (t);
9249 if (TREE_CODE (decl) == MEM_REF)
9250 continue;
9252 if (VAR_P (decl)
9253 && !is_global_var (decl)
9254 && DECL_CONTEXT (decl) == current_function_decl)
9256 tree c = gimplify_oacc_declare_1 (t);
9257 if (c)
9259 if (oacc_declare_returns == NULL)
9260 oacc_declare_returns = new hash_map<tree, tree>;
9262 oacc_declare_returns->put (decl, c);
9266 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
9269 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
9270 clauses);
9272 gimplify_seq_add_stmt (pre_p, stmt);
9274 *expr_p = NULL_TREE;
9277 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
9278 gimplification of the body, as well as scanning the body for used
9279 variables. We need to do this scan now, because variable-sized
9280 decls will be decomposed during gimplification. */
9282 static void
9283 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
9285 tree expr = *expr_p;
9286 gimple *g;
9287 gimple_seq body = NULL;
9289 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
9290 OMP_PARALLEL_COMBINED (expr)
9291 ? ORT_COMBINED_PARALLEL
9292 : ORT_PARALLEL, OMP_PARALLEL);
9294 push_gimplify_context ();
9296 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
9297 if (gimple_code (g) == GIMPLE_BIND)
9298 pop_gimplify_context (g);
9299 else
9300 pop_gimplify_context (NULL);
9302 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
9303 OMP_PARALLEL);
9305 g = gimple_build_omp_parallel (body,
9306 OMP_PARALLEL_CLAUSES (expr),
9307 NULL_TREE, NULL_TREE);
9308 if (OMP_PARALLEL_COMBINED (expr))
9309 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
9310 gimplify_seq_add_stmt (pre_p, g);
9311 *expr_p = NULL_TREE;
9314 /* Gimplify the contents of an OMP_TASK statement. This involves
9315 gimplification of the body, as well as scanning the body for used
9316 variables. We need to do this scan now, because variable-sized
9317 decls will be decomposed during gimplification. */
9319 static void
9320 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
9322 tree expr = *expr_p;
9323 gimple *g;
9324 gimple_seq body = NULL;
9326 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
9327 find_omp_clause (OMP_TASK_CLAUSES (expr),
9328 OMP_CLAUSE_UNTIED)
9329 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
9331 push_gimplify_context ();
9333 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
9334 if (gimple_code (g) == GIMPLE_BIND)
9335 pop_gimplify_context (g);
9336 else
9337 pop_gimplify_context (NULL);
9339 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
9340 OMP_TASK);
9342 g = gimple_build_omp_task (body,
9343 OMP_TASK_CLAUSES (expr),
9344 NULL_TREE, NULL_TREE,
9345 NULL_TREE, NULL_TREE, NULL_TREE);
9346 gimplify_seq_add_stmt (pre_p, g);
9347 *expr_p = NULL_TREE;
9350 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9351 with non-NULL OMP_FOR_INIT. */
9353 static tree
9354 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
9356 *walk_subtrees = 0;
9357 switch (TREE_CODE (*tp))
9359 case OMP_FOR:
9360 *walk_subtrees = 1;
9361 /* FALLTHRU */
9362 case OMP_SIMD:
9363 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9364 return *tp;
9365 break;
9366 case BIND_EXPR:
9367 case STATEMENT_LIST:
9368 case OMP_PARALLEL:
9369 *walk_subtrees = 1;
9370 break;
9371 default:
9372 break;
9374 return NULL_TREE;
9377 /* Gimplify the gross structure of an OMP_FOR statement. */
9379 static enum gimplify_status
9380 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
9382 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
9383 enum gimplify_status ret = GS_ALL_DONE;
9384 enum gimplify_status tret;
9385 gomp_for *gfor;
9386 gimple_seq for_body, for_pre_body;
9387 int i;
9388 bitmap has_decl_expr = NULL;
9389 enum omp_region_type ort = ORT_WORKSHARE;
9391 orig_for_stmt = for_stmt = *expr_p;
9393 switch (TREE_CODE (for_stmt))
9395 case OMP_FOR:
9396 case CILK_FOR:
9397 case OMP_DISTRIBUTE:
9398 break;
9399 case OACC_LOOP:
9400 ort = ORT_ACC;
9401 break;
9402 case OMP_TASKLOOP:
9403 if (find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
9404 ort = ORT_UNTIED_TASK;
9405 else
9406 ort = ORT_TASK;
9407 break;
9408 case OMP_SIMD:
9409 case CILK_SIMD:
9410 ort = ORT_SIMD;
9411 break;
9412 default:
9413 gcc_unreachable ();
9416 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9417 clause for the IV. */
9418 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9420 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
9421 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9422 decl = TREE_OPERAND (t, 0);
9423 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
9424 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9425 && OMP_CLAUSE_DECL (c) == decl)
9427 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9428 break;
9432 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9434 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
9435 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
9436 find_combined_omp_for, NULL, NULL);
9437 if (inner_for_stmt == NULL_TREE)
9439 gcc_assert (seen_error ());
9440 *expr_p = NULL_TREE;
9441 return GS_ERROR;
9445 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
9446 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
9447 TREE_CODE (for_stmt));
9449 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
9450 gimplify_omp_ctxp->distribute = true;
9452 /* Handle OMP_FOR_INIT. */
9453 for_pre_body = NULL;
9454 if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
9456 has_decl_expr = BITMAP_ALLOC (NULL);
9457 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
9458 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
9459 == VAR_DECL)
9461 t = OMP_FOR_PRE_BODY (for_stmt);
9462 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9464 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
9466 tree_stmt_iterator si;
9467 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
9468 tsi_next (&si))
9470 t = tsi_stmt (si);
9471 if (TREE_CODE (t) == DECL_EXPR
9472 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
9473 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9477 if (OMP_FOR_PRE_BODY (for_stmt))
9479 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
9480 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9481 else
9483 struct gimplify_omp_ctx ctx;
9484 memset (&ctx, 0, sizeof (ctx));
9485 ctx.region_type = ORT_NONE;
9486 gimplify_omp_ctxp = &ctx;
9487 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9488 gimplify_omp_ctxp = NULL;
9491 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
9493 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9494 for_stmt = inner_for_stmt;
9496 /* For taskloop, need to gimplify the start, end and step before the
9497 taskloop, outside of the taskloop omp context. */
9498 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9500 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9502 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9503 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9505 TREE_OPERAND (t, 1)
9506 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9507 pre_p, NULL, false);
9508 tree c = build_omp_clause (input_location,
9509 OMP_CLAUSE_FIRSTPRIVATE);
9510 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9511 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9512 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9515 /* Handle OMP_FOR_COND. */
9516 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9517 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9519 TREE_OPERAND (t, 1)
9520 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9521 gimple_seq_empty_p (for_pre_body)
9522 ? pre_p : &for_pre_body, NULL,
9523 false);
9524 tree c = build_omp_clause (input_location,
9525 OMP_CLAUSE_FIRSTPRIVATE);
9526 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9527 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9528 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9531 /* Handle OMP_FOR_INCR. */
9532 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9533 if (TREE_CODE (t) == MODIFY_EXPR)
9535 decl = TREE_OPERAND (t, 0);
9536 t = TREE_OPERAND (t, 1);
9537 tree *tp = &TREE_OPERAND (t, 1);
9538 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
9539 tp = &TREE_OPERAND (t, 0);
9541 if (!is_gimple_constant (*tp))
9543 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
9544 ? pre_p : &for_pre_body;
9545 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
9546 tree c = build_omp_clause (input_location,
9547 OMP_CLAUSE_FIRSTPRIVATE);
9548 OMP_CLAUSE_DECL (c) = *tp;
9549 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9550 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9555 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
9556 OMP_TASKLOOP);
9559 if (orig_for_stmt != for_stmt)
9560 gimplify_omp_ctxp->combined_loop = true;
9562 for_body = NULL;
9563 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9564 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
9565 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9566 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
9568 tree c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
9569 bool is_doacross = false;
9570 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
9572 is_doacross = true;
9573 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
9574 (OMP_FOR_INIT (for_stmt))
9575 * 2);
9577 int collapse = 1;
9578 c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
9579 if (c)
9580 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9581 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9583 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9584 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9585 decl = TREE_OPERAND (t, 0);
9586 gcc_assert (DECL_P (decl));
9587 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
9588 || POINTER_TYPE_P (TREE_TYPE (decl)));
9589 if (is_doacross)
9591 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
9592 gimplify_omp_ctxp->loop_iter_var.quick_push
9593 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
9594 else
9595 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9596 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9599 /* Make sure the iteration variable is private. */
9600 tree c = NULL_TREE;
9601 tree c2 = NULL_TREE;
9602 if (orig_for_stmt != for_stmt)
9603 /* Do this only on innermost construct for combined ones. */;
9604 else if (ort == ORT_SIMD)
9606 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9607 (splay_tree_key) decl);
9608 omp_is_private (gimplify_omp_ctxp, decl,
9609 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9610 != 1));
9611 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9612 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9613 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9615 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9616 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9617 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
9618 if ((has_decl_expr
9619 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9620 || omp_no_lastprivate (gimplify_omp_ctxp))
9622 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9623 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9625 struct gimplify_omp_ctx *outer
9626 = gimplify_omp_ctxp->outer_context;
9627 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9629 if (outer->region_type == ORT_WORKSHARE
9630 && outer->combined_loop)
9632 n = splay_tree_lookup (outer->variables,
9633 (splay_tree_key)decl);
9634 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9636 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9637 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9639 else
9641 struct gimplify_omp_ctx *octx = outer->outer_context;
9642 if (octx
9643 && octx->region_type == ORT_COMBINED_PARALLEL
9644 && octx->outer_context
9645 && (octx->outer_context->region_type
9646 == ORT_WORKSHARE)
9647 && octx->outer_context->combined_loop)
9649 octx = octx->outer_context;
9650 n = splay_tree_lookup (octx->variables,
9651 (splay_tree_key)decl);
9652 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9654 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9655 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9662 OMP_CLAUSE_DECL (c) = decl;
9663 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9664 OMP_FOR_CLAUSES (for_stmt) = c;
9665 omp_add_variable (gimplify_omp_ctxp, decl, flags);
9666 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9668 if (outer->region_type == ORT_WORKSHARE
9669 && outer->combined_loop)
9671 if (outer->outer_context
9672 && (outer->outer_context->region_type
9673 == ORT_COMBINED_PARALLEL))
9674 outer = outer->outer_context;
9675 else if (omp_check_private (outer, decl, false))
9676 outer = NULL;
9678 else if (((outer->region_type & ORT_TASK) != 0)
9679 && outer->combined_loop
9680 && !omp_check_private (gimplify_omp_ctxp,
9681 decl, false))
9683 else if (outer->region_type != ORT_COMBINED_PARALLEL)
9685 omp_notice_variable (outer, decl, true);
9686 outer = NULL;
9688 if (outer)
9690 n = splay_tree_lookup (outer->variables,
9691 (splay_tree_key)decl);
9692 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9694 omp_add_variable (outer, decl,
9695 GOVD_LASTPRIVATE | GOVD_SEEN);
9696 if (outer->region_type == ORT_COMBINED_PARALLEL
9697 && outer->outer_context
9698 && (outer->outer_context->region_type
9699 == ORT_WORKSHARE)
9700 && outer->outer_context->combined_loop)
9702 outer = outer->outer_context;
9703 n = splay_tree_lookup (outer->variables,
9704 (splay_tree_key)decl);
9705 if (omp_check_private (outer, decl, false))
9706 outer = NULL;
9707 else if (n == NULL
9708 || ((n->value & GOVD_DATA_SHARE_CLASS)
9709 == 0))
9710 omp_add_variable (outer, decl,
9711 GOVD_LASTPRIVATE
9712 | GOVD_SEEN);
9713 else
9714 outer = NULL;
9716 if (outer && outer->outer_context
9717 && (outer->outer_context->region_type
9718 == ORT_COMBINED_TEAMS))
9720 outer = outer->outer_context;
9721 n = splay_tree_lookup (outer->variables,
9722 (splay_tree_key)decl);
9723 if (n == NULL
9724 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9725 omp_add_variable (outer, decl,
9726 GOVD_SHARED | GOVD_SEEN);
9727 else
9728 outer = NULL;
9730 if (outer && outer->outer_context)
9731 omp_notice_variable (outer->outer_context, decl,
9732 true);
9737 else
9739 bool lastprivate
9740 = (!has_decl_expr
9741 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9742 && !omp_no_lastprivate (gimplify_omp_ctxp);
9743 struct gimplify_omp_ctx *outer
9744 = gimplify_omp_ctxp->outer_context;
9745 if (outer && lastprivate)
9747 if (outer->region_type == ORT_WORKSHARE
9748 && outer->combined_loop)
9750 n = splay_tree_lookup (outer->variables,
9751 (splay_tree_key)decl);
9752 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9754 lastprivate = false;
9755 outer = NULL;
9757 else if (outer->outer_context
9758 && (outer->outer_context->region_type
9759 == ORT_COMBINED_PARALLEL))
9760 outer = outer->outer_context;
9761 else if (omp_check_private (outer, decl, false))
9762 outer = NULL;
9764 else if (((outer->region_type & ORT_TASK) != 0)
9765 && outer->combined_loop
9766 && !omp_check_private (gimplify_omp_ctxp,
9767 decl, false))
9769 else if (outer->region_type != ORT_COMBINED_PARALLEL)
9771 omp_notice_variable (outer, decl, true);
9772 outer = NULL;
9774 if (outer)
9776 n = splay_tree_lookup (outer->variables,
9777 (splay_tree_key)decl);
9778 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9780 omp_add_variable (outer, decl,
9781 GOVD_LASTPRIVATE | GOVD_SEEN);
9782 if (outer->region_type == ORT_COMBINED_PARALLEL
9783 && outer->outer_context
9784 && (outer->outer_context->region_type
9785 == ORT_WORKSHARE)
9786 && outer->outer_context->combined_loop)
9788 outer = outer->outer_context;
9789 n = splay_tree_lookup (outer->variables,
9790 (splay_tree_key)decl);
9791 if (omp_check_private (outer, decl, false))
9792 outer = NULL;
9793 else if (n == NULL
9794 || ((n->value & GOVD_DATA_SHARE_CLASS)
9795 == 0))
9796 omp_add_variable (outer, decl,
9797 GOVD_LASTPRIVATE
9798 | GOVD_SEEN);
9799 else
9800 outer = NULL;
9802 if (outer && outer->outer_context
9803 && (outer->outer_context->region_type
9804 == ORT_COMBINED_TEAMS))
9806 outer = outer->outer_context;
9807 n = splay_tree_lookup (outer->variables,
9808 (splay_tree_key)decl);
9809 if (n == NULL
9810 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9811 omp_add_variable (outer, decl,
9812 GOVD_SHARED | GOVD_SEEN);
9813 else
9814 outer = NULL;
9816 if (outer && outer->outer_context)
9817 omp_notice_variable (outer->outer_context, decl,
9818 true);
9823 c = build_omp_clause (input_location,
9824 lastprivate ? OMP_CLAUSE_LASTPRIVATE
9825 : OMP_CLAUSE_PRIVATE);
9826 OMP_CLAUSE_DECL (c) = decl;
9827 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9828 OMP_FOR_CLAUSES (for_stmt) = c;
9829 omp_add_variable (gimplify_omp_ctxp, decl,
9830 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
9831 | GOVD_EXPLICIT | GOVD_SEEN);
9832 c = NULL_TREE;
9835 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
9836 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9837 else
9838 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
9840 /* If DECL is not a gimple register, create a temporary variable to act
9841 as an iteration counter. This is valid, since DECL cannot be
9842 modified in the body of the loop. Similarly for any iteration vars
9843 in simd with collapse > 1 where the iterator vars must be
9844 lastprivate. */
9845 if (orig_for_stmt != for_stmt)
9846 var = decl;
9847 else if (!is_gimple_reg (decl)
9848 || (ort == ORT_SIMD
9849 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
9851 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9852 /* Make sure omp_add_variable is not called on it prematurely.
9853 We call it ourselves a few lines later. */
9854 gimplify_omp_ctxp = NULL;
9855 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
9856 gimplify_omp_ctxp = ctx;
9857 TREE_OPERAND (t, 0) = var;
9859 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
9861 if (ort == ORT_SIMD
9862 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9864 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9865 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
9866 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
9867 OMP_CLAUSE_DECL (c2) = var;
9868 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
9869 OMP_FOR_CLAUSES (for_stmt) = c2;
9870 omp_add_variable (gimplify_omp_ctxp, var,
9871 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
9872 if (c == NULL_TREE)
9874 c = c2;
9875 c2 = NULL_TREE;
9878 else
9879 omp_add_variable (gimplify_omp_ctxp, var,
9880 GOVD_PRIVATE | GOVD_SEEN);
9882 else
9883 var = decl;
9885 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
9886 is_gimple_val, fb_rvalue, false);
9887 ret = MIN (ret, tret);
9888 if (ret == GS_ERROR)
9889 return ret;
9891 /* Handle OMP_FOR_COND. */
9892 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9893 gcc_assert (COMPARISON_CLASS_P (t));
9894 gcc_assert (TREE_OPERAND (t, 0) == decl);
9896 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
9897 is_gimple_val, fb_rvalue, false);
9898 ret = MIN (ret, tret);
9900 /* Handle OMP_FOR_INCR. */
9901 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9902 switch (TREE_CODE (t))
9904 case PREINCREMENT_EXPR:
9905 case POSTINCREMENT_EXPR:
9907 tree decl = TREE_OPERAND (t, 0);
9908 /* c_omp_for_incr_canonicalize_ptr() should have been
9909 called to massage things appropriately. */
9910 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
9912 if (orig_for_stmt != for_stmt)
9913 break;
9914 t = build_int_cst (TREE_TYPE (decl), 1);
9915 if (c)
9916 OMP_CLAUSE_LINEAR_STEP (c) = t;
9917 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
9918 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
9919 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
9920 break;
9923 case PREDECREMENT_EXPR:
9924 case POSTDECREMENT_EXPR:
9925 /* c_omp_for_incr_canonicalize_ptr() should have been
9926 called to massage things appropriately. */
9927 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
9928 if (orig_for_stmt != for_stmt)
9929 break;
9930 t = build_int_cst (TREE_TYPE (decl), -1);
9931 if (c)
9932 OMP_CLAUSE_LINEAR_STEP (c) = t;
9933 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
9934 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
9935 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
9936 break;
9938 case MODIFY_EXPR:
9939 gcc_assert (TREE_OPERAND (t, 0) == decl);
9940 TREE_OPERAND (t, 0) = var;
9942 t = TREE_OPERAND (t, 1);
9943 switch (TREE_CODE (t))
9945 case PLUS_EXPR:
9946 if (TREE_OPERAND (t, 1) == decl)
9948 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
9949 TREE_OPERAND (t, 0) = var;
9950 break;
9953 /* Fallthru. */
9954 case MINUS_EXPR:
9955 case POINTER_PLUS_EXPR:
9956 gcc_assert (TREE_OPERAND (t, 0) == decl);
9957 TREE_OPERAND (t, 0) = var;
9958 break;
9959 default:
9960 gcc_unreachable ();
9963 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
9964 is_gimple_val, fb_rvalue, false);
9965 ret = MIN (ret, tret);
9966 if (c)
9968 tree step = TREE_OPERAND (t, 1);
9969 tree stept = TREE_TYPE (decl);
9970 if (POINTER_TYPE_P (stept))
9971 stept = sizetype;
9972 step = fold_convert (stept, step);
9973 if (TREE_CODE (t) == MINUS_EXPR)
9974 step = fold_build1 (NEGATE_EXPR, stept, step);
9975 OMP_CLAUSE_LINEAR_STEP (c) = step;
9976 if (step != TREE_OPERAND (t, 1))
9978 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
9979 &for_pre_body, NULL,
9980 is_gimple_val, fb_rvalue, false);
9981 ret = MIN (ret, tret);
9984 break;
9986 default:
9987 gcc_unreachable ();
9990 if (c2)
9992 gcc_assert (c);
9993 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
9996 if ((var != decl || collapse > 1) && orig_for_stmt == for_stmt)
9998 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
9999 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10000 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
10001 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
10002 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
10003 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
10004 && OMP_CLAUSE_DECL (c) == decl)
10006 if (is_doacross && (collapse == 1 || i >= collapse))
10007 t = var;
10008 else
10010 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10011 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
10012 gcc_assert (TREE_OPERAND (t, 0) == var);
10013 t = TREE_OPERAND (t, 1);
10014 gcc_assert (TREE_CODE (t) == PLUS_EXPR
10015 || TREE_CODE (t) == MINUS_EXPR
10016 || TREE_CODE (t) == POINTER_PLUS_EXPR);
10017 gcc_assert (TREE_OPERAND (t, 0) == var);
10018 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
10019 is_doacross ? var : decl,
10020 TREE_OPERAND (t, 1));
10022 gimple_seq *seq;
10023 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10024 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
10025 else
10026 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
10027 gimplify_assign (decl, t, seq);
10032 BITMAP_FREE (has_decl_expr);
10034 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10036 push_gimplify_context ();
10037 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
10039 OMP_FOR_BODY (orig_for_stmt)
10040 = build3 (BIND_EXPR, void_type_node, NULL,
10041 OMP_FOR_BODY (orig_for_stmt), NULL);
10042 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
10046 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
10047 &for_body);
10049 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10051 if (gimple_code (g) == GIMPLE_BIND)
10052 pop_gimplify_context (g);
10053 else
10054 pop_gimplify_context (NULL);
10057 if (orig_for_stmt != for_stmt)
10058 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10060 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10061 decl = TREE_OPERAND (t, 0);
10062 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
10063 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10064 gimplify_omp_ctxp = ctx->outer_context;
10065 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
10066 gimplify_omp_ctxp = ctx;
10067 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
10068 TREE_OPERAND (t, 0) = var;
10069 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10070 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
10071 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
10074 gimplify_adjust_omp_clauses (pre_p, for_body,
10075 &OMP_FOR_CLAUSES (orig_for_stmt),
10076 TREE_CODE (orig_for_stmt));
10078 int kind;
10079 switch (TREE_CODE (orig_for_stmt))
10081 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
10082 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
10083 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
10084 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
10085 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
10086 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
10087 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
10088 default:
10089 gcc_unreachable ();
10091 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
10092 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
10093 for_pre_body);
10094 if (orig_for_stmt != for_stmt)
10095 gimple_omp_for_set_combined_p (gfor, true);
10096 if (gimplify_omp_ctxp
10097 && (gimplify_omp_ctxp->combined_loop
10098 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
10099 && gimplify_omp_ctxp->outer_context
10100 && gimplify_omp_ctxp->outer_context->combined_loop)))
10102 gimple_omp_for_set_combined_into_p (gfor, true);
10103 if (gimplify_omp_ctxp->combined_loop)
10104 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
10105 else
10106 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
10109 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
10111 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
10112 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
10113 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
10114 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
10115 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
10116 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
10117 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
10118 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
10121 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
10122 constructs with GIMPLE_OMP_TASK sandwiched in between them.
10123 The outer taskloop stands for computing the number of iterations,
10124 counts for collapsed loops and holding taskloop specific clauses.
10125 The task construct stands for the effect of data sharing on the
10126 explicit task it creates and the inner taskloop stands for expansion
10127 of the static loop inside of the explicit task construct. */
10128 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
10130 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
10131 tree task_clauses = NULL_TREE;
10132 tree c = *gfor_clauses_ptr;
10133 tree *gtask_clauses_ptr = &task_clauses;
10134 tree outer_for_clauses = NULL_TREE;
10135 tree *gforo_clauses_ptr = &outer_for_clauses;
10136 for (; c; c = OMP_CLAUSE_CHAIN (c))
10137 switch (OMP_CLAUSE_CODE (c))
10139 /* These clauses are allowed on task, move them there. */
10140 case OMP_CLAUSE_SHARED:
10141 case OMP_CLAUSE_FIRSTPRIVATE:
10142 case OMP_CLAUSE_DEFAULT:
10143 case OMP_CLAUSE_IF:
10144 case OMP_CLAUSE_UNTIED:
10145 case OMP_CLAUSE_FINAL:
10146 case OMP_CLAUSE_MERGEABLE:
10147 case OMP_CLAUSE_PRIORITY:
10148 *gtask_clauses_ptr = c;
10149 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10150 break;
10151 case OMP_CLAUSE_PRIVATE:
10152 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
10154 /* We want private on outer for and firstprivate
10155 on task. */
10156 *gtask_clauses_ptr
10157 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10158 OMP_CLAUSE_FIRSTPRIVATE);
10159 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10160 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10161 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10162 *gforo_clauses_ptr = c;
10163 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10165 else
10167 *gtask_clauses_ptr = c;
10168 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10170 break;
10171 /* These clauses go into outer taskloop clauses. */
10172 case OMP_CLAUSE_GRAINSIZE:
10173 case OMP_CLAUSE_NUM_TASKS:
10174 case OMP_CLAUSE_NOGROUP:
10175 *gforo_clauses_ptr = c;
10176 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10177 break;
10178 /* Taskloop clause we duplicate on both taskloops. */
10179 case OMP_CLAUSE_COLLAPSE:
10180 *gfor_clauses_ptr = c;
10181 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10182 *gforo_clauses_ptr = copy_node (c);
10183 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10184 break;
10185 /* For lastprivate, keep the clause on inner taskloop, and add
10186 a shared clause on task. If the same decl is also firstprivate,
10187 add also firstprivate clause on the inner taskloop. */
10188 case OMP_CLAUSE_LASTPRIVATE:
10189 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
10191 /* For taskloop C++ lastprivate IVs, we want:
10192 1) private on outer taskloop
10193 2) firstprivate and shared on task
10194 3) lastprivate on inner taskloop */
10195 *gtask_clauses_ptr
10196 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10197 OMP_CLAUSE_FIRSTPRIVATE);
10198 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10199 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10200 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10201 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
10202 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10203 OMP_CLAUSE_PRIVATE);
10204 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
10205 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
10206 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
10207 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10209 *gfor_clauses_ptr = c;
10210 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10211 *gtask_clauses_ptr
10212 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
10213 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10214 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
10215 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
10216 gtask_clauses_ptr
10217 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10218 break;
10219 default:
10220 gcc_unreachable ();
10222 *gfor_clauses_ptr = NULL_TREE;
10223 *gtask_clauses_ptr = NULL_TREE;
10224 *gforo_clauses_ptr = NULL_TREE;
10225 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
10226 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
10227 NULL_TREE, NULL_TREE, NULL_TREE);
10228 gimple_omp_task_set_taskloop_p (g, true);
10229 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
10230 gomp_for *gforo
10231 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
10232 gimple_omp_for_collapse (gfor),
10233 gimple_omp_for_pre_body (gfor));
10234 gimple_omp_for_set_pre_body (gfor, NULL);
10235 gimple_omp_for_set_combined_p (gforo, true);
10236 gimple_omp_for_set_combined_into_p (gfor, true);
10237 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
10239 t = unshare_expr (gimple_omp_for_index (gfor, i));
10240 gimple_omp_for_set_index (gforo, i, t);
10241 t = unshare_expr (gimple_omp_for_initial (gfor, i));
10242 gimple_omp_for_set_initial (gforo, i, t);
10243 gimple_omp_for_set_cond (gforo, i,
10244 gimple_omp_for_cond (gfor, i));
10245 t = unshare_expr (gimple_omp_for_final (gfor, i));
10246 gimple_omp_for_set_final (gforo, i, t);
10247 t = unshare_expr (gimple_omp_for_incr (gfor, i));
10248 gimple_omp_for_set_incr (gforo, i, t);
10250 gimplify_seq_add_stmt (pre_p, gforo);
10252 else
10253 gimplify_seq_add_stmt (pre_p, gfor);
10254 if (ret != GS_ALL_DONE)
10255 return GS_ERROR;
10256 *expr_p = NULL_TREE;
10257 return GS_ALL_DONE;
10260 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10261 of OMP_TARGET's body. */
10263 static tree
10264 find_omp_teams (tree *tp, int *walk_subtrees, void *)
10266 *walk_subtrees = 0;
10267 switch (TREE_CODE (*tp))
10269 case OMP_TEAMS:
10270 return *tp;
10271 case BIND_EXPR:
10272 case STATEMENT_LIST:
10273 *walk_subtrees = 1;
10274 break;
10275 default:
10276 break;
10278 return NULL_TREE;
10281 /* Helper function of optimize_target_teams, determine if the expression
10282 can be computed safely before the target construct on the host. */
10284 static tree
10285 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
10287 splay_tree_node n;
10289 if (TYPE_P (*tp))
10291 *walk_subtrees = 0;
10292 return NULL_TREE;
10294 switch (TREE_CODE (*tp))
10296 case VAR_DECL:
10297 case PARM_DECL:
10298 case RESULT_DECL:
10299 *walk_subtrees = 0;
10300 if (error_operand_p (*tp)
10301 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
10302 || DECL_HAS_VALUE_EXPR_P (*tp)
10303 || DECL_THREAD_LOCAL_P (*tp)
10304 || TREE_SIDE_EFFECTS (*tp)
10305 || TREE_THIS_VOLATILE (*tp))
10306 return *tp;
10307 if (is_global_var (*tp)
10308 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
10309 || lookup_attribute ("omp declare target link",
10310 DECL_ATTRIBUTES (*tp))))
10311 return *tp;
10312 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10313 (splay_tree_key) *tp);
10314 if (n == NULL)
10316 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
10317 return NULL_TREE;
10318 return *tp;
10320 else if (n->value & GOVD_LOCAL)
10321 return *tp;
10322 else if (n->value & GOVD_FIRSTPRIVATE)
10323 return NULL_TREE;
10324 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10325 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10326 return NULL_TREE;
10327 return *tp;
10328 case INTEGER_CST:
10329 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10330 return *tp;
10331 return NULL_TREE;
10332 case TARGET_EXPR:
10333 if (TARGET_EXPR_INITIAL (*tp)
10334 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
10335 return *tp;
10336 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
10337 walk_subtrees, NULL);
10338 /* Allow some reasonable subset of integral arithmetics. */
10339 case PLUS_EXPR:
10340 case MINUS_EXPR:
10341 case MULT_EXPR:
10342 case TRUNC_DIV_EXPR:
10343 case CEIL_DIV_EXPR:
10344 case FLOOR_DIV_EXPR:
10345 case ROUND_DIV_EXPR:
10346 case TRUNC_MOD_EXPR:
10347 case CEIL_MOD_EXPR:
10348 case FLOOR_MOD_EXPR:
10349 case ROUND_MOD_EXPR:
10350 case RDIV_EXPR:
10351 case EXACT_DIV_EXPR:
10352 case MIN_EXPR:
10353 case MAX_EXPR:
10354 case LSHIFT_EXPR:
10355 case RSHIFT_EXPR:
10356 case BIT_IOR_EXPR:
10357 case BIT_XOR_EXPR:
10358 case BIT_AND_EXPR:
10359 case NEGATE_EXPR:
10360 case ABS_EXPR:
10361 case BIT_NOT_EXPR:
10362 case NON_LVALUE_EXPR:
10363 CASE_CONVERT:
10364 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10365 return *tp;
10366 return NULL_TREE;
10367 /* And disallow anything else, except for comparisons. */
10368 default:
10369 if (COMPARISON_CLASS_P (*tp))
10370 return NULL_TREE;
10371 return *tp;
10375 /* Try to determine if the num_teams and/or thread_limit expressions
10376 can have their values determined already before entering the
10377 target construct.
10378 INTEGER_CSTs trivially are,
10379 integral decls that are firstprivate (explicitly or implicitly)
10380 or explicitly map(always, to:) or map(always, tofrom:) on the target
10381 region too, and expressions involving simple arithmetics on those
10382 too, function calls are not ok, dereferencing something neither etc.
10383 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10384 EXPR based on what we find:
10385 0 stands for clause not specified at all, use implementation default
10386 -1 stands for value that can't be determined easily before entering
10387 the target construct.
10388 If teams construct is not present at all, use 1 for num_teams
10389 and 0 for thread_limit (only one team is involved, and the thread
10390 limit is implementation defined. */
10392 static void
10393 optimize_target_teams (tree target, gimple_seq *pre_p)
10395 tree body = OMP_BODY (target);
10396 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
10397 tree num_teams = integer_zero_node;
10398 tree thread_limit = integer_zero_node;
10399 location_t num_teams_loc = EXPR_LOCATION (target);
10400 location_t thread_limit_loc = EXPR_LOCATION (target);
10401 tree c, *p, expr;
10402 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
10404 if (teams == NULL_TREE)
10405 num_teams = integer_one_node;
10406 else
10407 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
10409 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
10411 p = &num_teams;
10412 num_teams_loc = OMP_CLAUSE_LOCATION (c);
10414 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
10416 p = &thread_limit;
10417 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
10419 else
10420 continue;
10421 expr = OMP_CLAUSE_OPERAND (c, 0);
10422 if (TREE_CODE (expr) == INTEGER_CST)
10424 *p = expr;
10425 continue;
10427 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
10429 *p = integer_minus_one_node;
10430 continue;
10432 *p = expr;
10433 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
10434 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
10435 == GS_ERROR)
10437 gimplify_omp_ctxp = target_ctx;
10438 *p = integer_minus_one_node;
10439 continue;
10441 gimplify_omp_ctxp = target_ctx;
10442 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
10443 OMP_CLAUSE_OPERAND (c, 0) = *p;
10445 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10446 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
10447 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10448 OMP_TARGET_CLAUSES (target) = c;
10449 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10450 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
10451 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10452 OMP_TARGET_CLAUSES (target) = c;
10455 /* Gimplify the gross structure of several OMP constructs. */
10457 static void
10458 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
10460 tree expr = *expr_p;
10461 gimple *stmt;
10462 gimple_seq body = NULL;
10463 enum omp_region_type ort;
10465 switch (TREE_CODE (expr))
10467 case OMP_SECTIONS:
10468 case OMP_SINGLE:
10469 ort = ORT_WORKSHARE;
10470 break;
10471 case OMP_TARGET:
10472 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
10473 break;
10474 case OACC_KERNELS:
10475 ort = ORT_ACC_KERNELS;
10476 break;
10477 case OACC_PARALLEL:
10478 ort = ORT_ACC_PARALLEL;
10479 break;
10480 case OACC_DATA:
10481 ort = ORT_ACC_DATA;
10482 break;
10483 case OMP_TARGET_DATA:
10484 ort = ORT_TARGET_DATA;
10485 break;
10486 case OMP_TEAMS:
10487 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
10488 break;
10489 case OACC_HOST_DATA:
10490 ort = ORT_ACC_HOST_DATA;
10491 break;
10492 default:
10493 gcc_unreachable ();
10495 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
10496 TREE_CODE (expr));
10497 if (TREE_CODE (expr) == OMP_TARGET)
10498 optimize_target_teams (expr, pre_p);
10499 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
10501 push_gimplify_context ();
10502 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
10503 if (gimple_code (g) == GIMPLE_BIND)
10504 pop_gimplify_context (g);
10505 else
10506 pop_gimplify_context (NULL);
10507 if ((ort & ORT_TARGET_DATA) != 0)
10509 enum built_in_function end_ix;
10510 switch (TREE_CODE (expr))
10512 case OACC_DATA:
10513 case OACC_HOST_DATA:
10514 end_ix = BUILT_IN_GOACC_DATA_END;
10515 break;
10516 case OMP_TARGET_DATA:
10517 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
10518 break;
10519 default:
10520 gcc_unreachable ();
10522 tree fn = builtin_decl_explicit (end_ix);
10523 g = gimple_build_call (fn, 0);
10524 gimple_seq cleanup = NULL;
10525 gimple_seq_add_stmt (&cleanup, g);
10526 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10527 body = NULL;
10528 gimple_seq_add_stmt (&body, g);
10531 else
10532 gimplify_and_add (OMP_BODY (expr), &body);
10533 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
10534 TREE_CODE (expr));
10536 switch (TREE_CODE (expr))
10538 case OACC_DATA:
10539 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
10540 OMP_CLAUSES (expr));
10541 break;
10542 case OACC_KERNELS:
10543 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
10544 OMP_CLAUSES (expr));
10545 break;
10546 case OACC_HOST_DATA:
10547 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
10548 OMP_CLAUSES (expr));
10549 break;
10550 case OACC_PARALLEL:
10551 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
10552 OMP_CLAUSES (expr));
10553 break;
10554 case OMP_SECTIONS:
10555 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
10556 break;
10557 case OMP_SINGLE:
10558 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
10559 break;
10560 case OMP_TARGET:
10561 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
10562 OMP_CLAUSES (expr));
10563 break;
10564 case OMP_TARGET_DATA:
10565 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
10566 OMP_CLAUSES (expr));
10567 break;
10568 case OMP_TEAMS:
10569 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
10570 break;
10571 default:
10572 gcc_unreachable ();
10575 gimplify_seq_add_stmt (pre_p, stmt);
10576 *expr_p = NULL_TREE;
10579 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10580 target update constructs. */
10582 static void
10583 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
10585 tree expr = *expr_p;
10586 int kind;
10587 gomp_target *stmt;
10588 enum omp_region_type ort = ORT_WORKSHARE;
10590 switch (TREE_CODE (expr))
10592 case OACC_ENTER_DATA:
10593 case OACC_EXIT_DATA:
10594 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
10595 ort = ORT_ACC;
10596 break;
10597 case OACC_UPDATE:
10598 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
10599 ort = ORT_ACC;
10600 break;
10601 case OMP_TARGET_UPDATE:
10602 kind = GF_OMP_TARGET_KIND_UPDATE;
10603 break;
10604 case OMP_TARGET_ENTER_DATA:
10605 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
10606 break;
10607 case OMP_TARGET_EXIT_DATA:
10608 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
10609 break;
10610 default:
10611 gcc_unreachable ();
10613 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
10614 ort, TREE_CODE (expr));
10615 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
10616 TREE_CODE (expr));
10617 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
10619 gimplify_seq_add_stmt (pre_p, stmt);
10620 *expr_p = NULL_TREE;
10623 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
10624 stabilized the lhs of the atomic operation as *ADDR. Return true if
10625 EXPR is this stabilized form. */
10627 static bool
10628 goa_lhs_expr_p (tree expr, tree addr)
10630 /* Also include casts to other type variants. The C front end is fond
10631 of adding these for e.g. volatile variables. This is like
10632 STRIP_TYPE_NOPS but includes the main variant lookup. */
10633 STRIP_USELESS_TYPE_CONVERSION (expr);
10635 if (TREE_CODE (expr) == INDIRECT_REF)
10637 expr = TREE_OPERAND (expr, 0);
10638 while (expr != addr
10639 && (CONVERT_EXPR_P (expr)
10640 || TREE_CODE (expr) == NON_LVALUE_EXPR)
10641 && TREE_CODE (expr) == TREE_CODE (addr)
10642 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
10644 expr = TREE_OPERAND (expr, 0);
10645 addr = TREE_OPERAND (addr, 0);
10647 if (expr == addr)
10648 return true;
10649 return (TREE_CODE (addr) == ADDR_EXPR
10650 && TREE_CODE (expr) == ADDR_EXPR
10651 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
10653 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
10654 return true;
10655 return false;
10658 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
10659 expression does not involve the lhs, evaluate it into a temporary.
10660 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
10661 or -1 if an error was encountered. */
10663 static int
10664 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
10665 tree lhs_var)
10667 tree expr = *expr_p;
10668 int saw_lhs;
10670 if (goa_lhs_expr_p (expr, lhs_addr))
10672 *expr_p = lhs_var;
10673 return 1;
10675 if (is_gimple_val (expr))
10676 return 0;
10678 saw_lhs = 0;
10679 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
10681 case tcc_binary:
10682 case tcc_comparison:
10683 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
10684 lhs_var);
10685 /* FALLTHRU */
10686 case tcc_unary:
10687 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
10688 lhs_var);
10689 break;
10690 case tcc_expression:
10691 switch (TREE_CODE (expr))
10693 case TRUTH_ANDIF_EXPR:
10694 case TRUTH_ORIF_EXPR:
10695 case TRUTH_AND_EXPR:
10696 case TRUTH_OR_EXPR:
10697 case TRUTH_XOR_EXPR:
10698 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
10699 lhs_addr, lhs_var);
10700 /* FALLTHRU */
10701 case TRUTH_NOT_EXPR:
10702 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
10703 lhs_addr, lhs_var);
10704 break;
10705 case COMPOUND_EXPR:
10706 /* Break out any preevaluations from cp_build_modify_expr. */
10707 for (; TREE_CODE (expr) == COMPOUND_EXPR;
10708 expr = TREE_OPERAND (expr, 1))
10709 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
10710 *expr_p = expr;
10711 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
10712 default:
10713 break;
10715 break;
10716 default:
10717 break;
10720 if (saw_lhs == 0)
10722 enum gimplify_status gs;
10723 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
10724 if (gs != GS_ALL_DONE)
10725 saw_lhs = -1;
10728 return saw_lhs;
10731 /* Gimplify an OMP_ATOMIC statement. */
10733 static enum gimplify_status
10734 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
10736 tree addr = TREE_OPERAND (*expr_p, 0);
10737 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
10738 ? NULL : TREE_OPERAND (*expr_p, 1);
10739 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
10740 tree tmp_load;
10741 gomp_atomic_load *loadstmt;
10742 gomp_atomic_store *storestmt;
10744 tmp_load = create_tmp_reg (type);
10745 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
10746 return GS_ERROR;
10748 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
10749 != GS_ALL_DONE)
10750 return GS_ERROR;
10752 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
10753 gimplify_seq_add_stmt (pre_p, loadstmt);
10754 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
10755 != GS_ALL_DONE)
10756 return GS_ERROR;
10758 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
10759 rhs = tmp_load;
10760 storestmt = gimple_build_omp_atomic_store (rhs);
10761 gimplify_seq_add_stmt (pre_p, storestmt);
10762 if (OMP_ATOMIC_SEQ_CST (*expr_p))
10764 gimple_omp_atomic_set_seq_cst (loadstmt);
10765 gimple_omp_atomic_set_seq_cst (storestmt);
10767 switch (TREE_CODE (*expr_p))
10769 case OMP_ATOMIC_READ:
10770 case OMP_ATOMIC_CAPTURE_OLD:
10771 *expr_p = tmp_load;
10772 gimple_omp_atomic_set_need_value (loadstmt);
10773 break;
10774 case OMP_ATOMIC_CAPTURE_NEW:
10775 *expr_p = rhs;
10776 gimple_omp_atomic_set_need_value (storestmt);
10777 break;
10778 default:
10779 *expr_p = NULL;
10780 break;
10783 return GS_ALL_DONE;
10786 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
10787 body, and adding some EH bits. */
10789 static enum gimplify_status
10790 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
10792 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
10793 gimple *body_stmt;
10794 gtransaction *trans_stmt;
10795 gimple_seq body = NULL;
10796 int subcode = 0;
10798 /* Wrap the transaction body in a BIND_EXPR so we have a context
10799 where to put decls for OMP. */
10800 if (TREE_CODE (tbody) != BIND_EXPR)
10802 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
10803 TREE_SIDE_EFFECTS (bind) = 1;
10804 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
10805 TRANSACTION_EXPR_BODY (expr) = bind;
10808 push_gimplify_context ();
10809 temp = voidify_wrapper_expr (*expr_p, NULL);
10811 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
10812 pop_gimplify_context (body_stmt);
10814 trans_stmt = gimple_build_transaction (body);
10815 if (TRANSACTION_EXPR_OUTER (expr))
10816 subcode = GTMA_IS_OUTER;
10817 else if (TRANSACTION_EXPR_RELAXED (expr))
10818 subcode = GTMA_IS_RELAXED;
10819 gimple_transaction_set_subcode (trans_stmt, subcode);
10821 gimplify_seq_add_stmt (pre_p, trans_stmt);
10823 if (temp)
10825 *expr_p = temp;
10826 return GS_OK;
10829 *expr_p = NULL_TREE;
10830 return GS_ALL_DONE;
10833 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
10834 is the OMP_BODY of the original EXPR (which has already been
10835 gimplified so it's not present in the EXPR).
10837 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
10839 static gimple *
10840 gimplify_omp_ordered (tree expr, gimple_seq body)
10842 tree c, decls;
10843 int failures = 0;
10844 unsigned int i;
10845 tree source_c = NULL_TREE;
10846 tree sink_c = NULL_TREE;
10848 if (gimplify_omp_ctxp)
10850 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
10851 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
10852 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
10853 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
10854 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
10856 error_at (OMP_CLAUSE_LOCATION (c),
10857 "%<ordered%> construct with %<depend%> clause must be "
10858 "closely nested inside a loop with %<ordered%> clause "
10859 "with a parameter");
10860 failures++;
10862 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
10863 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
10865 bool fail = false;
10866 for (decls = OMP_CLAUSE_DECL (c), i = 0;
10867 decls && TREE_CODE (decls) == TREE_LIST;
10868 decls = TREE_CHAIN (decls), ++i)
10869 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
10870 continue;
10871 else if (TREE_VALUE (decls)
10872 != gimplify_omp_ctxp->loop_iter_var[2 * i])
10874 error_at (OMP_CLAUSE_LOCATION (c),
10875 "variable %qE is not an iteration "
10876 "of outermost loop %d, expected %qE",
10877 TREE_VALUE (decls), i + 1,
10878 gimplify_omp_ctxp->loop_iter_var[2 * i]);
10879 fail = true;
10880 failures++;
10882 else
10883 TREE_VALUE (decls)
10884 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
10885 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
10887 error_at (OMP_CLAUSE_LOCATION (c),
10888 "number of variables in %<depend(sink)%> "
10889 "clause does not match number of "
10890 "iteration variables");
10891 failures++;
10893 sink_c = c;
10895 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
10896 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
10898 if (source_c)
10900 error_at (OMP_CLAUSE_LOCATION (c),
10901 "more than one %<depend(source)%> clause on an "
10902 "%<ordered%> construct");
10903 failures++;
10905 else
10906 source_c = c;
10909 if (source_c && sink_c)
10911 error_at (OMP_CLAUSE_LOCATION (source_c),
10912 "%<depend(source)%> clause specified together with "
10913 "%<depend(sink:)%> clauses on the same construct");
10914 failures++;
10917 if (failures)
10918 return gimple_build_nop ();
10919 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
10922 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
10923 expression produces a value to be used as an operand inside a GIMPLE
10924 statement, the value will be stored back in *EXPR_P. This value will
10925 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
10926 an SSA_NAME. The corresponding sequence of GIMPLE statements is
10927 emitted in PRE_P and POST_P.
10929 Additionally, this process may overwrite parts of the input
10930 expression during gimplification. Ideally, it should be
10931 possible to do non-destructive gimplification.
10933 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
10934 the expression needs to evaluate to a value to be used as
10935 an operand in a GIMPLE statement, this value will be stored in
10936 *EXPR_P on exit. This happens when the caller specifies one
10937 of fb_lvalue or fb_rvalue fallback flags.
10939 PRE_P will contain the sequence of GIMPLE statements corresponding
10940 to the evaluation of EXPR and all the side-effects that must
10941 be executed before the main expression. On exit, the last
10942 statement of PRE_P is the core statement being gimplified. For
10943 instance, when gimplifying 'if (++a)' the last statement in
10944 PRE_P will be 'if (t.1)' where t.1 is the result of
10945 pre-incrementing 'a'.
10947 POST_P will contain the sequence of GIMPLE statements corresponding
10948 to the evaluation of all the side-effects that must be executed
10949 after the main expression. If this is NULL, the post
10950 side-effects are stored at the end of PRE_P.
10952 The reason why the output is split in two is to handle post
10953 side-effects explicitly. In some cases, an expression may have
10954 inner and outer post side-effects which need to be emitted in
10955 an order different from the one given by the recursive
10956 traversal. For instance, for the expression (*p--)++ the post
10957 side-effects of '--' must actually occur *after* the post
10958 side-effects of '++'. However, gimplification will first visit
10959 the inner expression, so if a separate POST sequence was not
10960 used, the resulting sequence would be:
10962 1 t.1 = *p
10963 2 p = p - 1
10964 3 t.2 = t.1 + 1
10965 4 *p = t.2
10967 However, the post-decrement operation in line #2 must not be
10968 evaluated until after the store to *p at line #4, so the
10969 correct sequence should be:
10971 1 t.1 = *p
10972 2 t.2 = t.1 + 1
10973 3 *p = t.2
10974 4 p = p - 1
10976 So, by specifying a separate post queue, it is possible
10977 to emit the post side-effects in the correct order.
10978 If POST_P is NULL, an internal queue will be used. Before
10979 returning to the caller, the sequence POST_P is appended to
10980 the main output sequence PRE_P.
10982 GIMPLE_TEST_F points to a function that takes a tree T and
10983 returns nonzero if T is in the GIMPLE form requested by the
10984 caller. The GIMPLE predicates are in gimple.c.
10986 FALLBACK tells the function what sort of a temporary we want if
10987 gimplification cannot produce an expression that complies with
10988 GIMPLE_TEST_F.
10990 fb_none means that no temporary should be generated
10991 fb_rvalue means that an rvalue is OK to generate
10992 fb_lvalue means that an lvalue is OK to generate
10993 fb_either means that either is OK, but an lvalue is preferable.
10994 fb_mayfail means that gimplification may fail (in which case
10995 GS_ERROR will be returned)
10997 The return value is either GS_ERROR or GS_ALL_DONE, since this
10998 function iterates until EXPR is completely gimplified or an error
10999 occurs. */
11001 enum gimplify_status
11002 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
11003 bool (*gimple_test_f) (tree), fallback_t fallback)
11005 tree tmp;
11006 gimple_seq internal_pre = NULL;
11007 gimple_seq internal_post = NULL;
11008 tree save_expr;
11009 bool is_statement;
11010 location_t saved_location;
11011 enum gimplify_status ret;
11012 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
11013 tree label;
11015 save_expr = *expr_p;
11016 if (save_expr == NULL_TREE)
11017 return GS_ALL_DONE;
11019 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
11020 is_statement = gimple_test_f == is_gimple_stmt;
11021 if (is_statement)
11022 gcc_assert (pre_p);
11024 /* Consistency checks. */
11025 if (gimple_test_f == is_gimple_reg)
11026 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
11027 else if (gimple_test_f == is_gimple_val
11028 || gimple_test_f == is_gimple_call_addr
11029 || gimple_test_f == is_gimple_condexpr
11030 || gimple_test_f == is_gimple_mem_rhs
11031 || gimple_test_f == is_gimple_mem_rhs_or_call
11032 || gimple_test_f == is_gimple_reg_rhs
11033 || gimple_test_f == is_gimple_reg_rhs_or_call
11034 || gimple_test_f == is_gimple_asm_val
11035 || gimple_test_f == is_gimple_mem_ref_addr)
11036 gcc_assert (fallback & fb_rvalue);
11037 else if (gimple_test_f == is_gimple_min_lval
11038 || gimple_test_f == is_gimple_lvalue)
11039 gcc_assert (fallback & fb_lvalue);
11040 else if (gimple_test_f == is_gimple_addressable)
11041 gcc_assert (fallback & fb_either);
11042 else if (gimple_test_f == is_gimple_stmt)
11043 gcc_assert (fallback == fb_none);
11044 else
11046 /* We should have recognized the GIMPLE_TEST_F predicate to
11047 know what kind of fallback to use in case a temporary is
11048 needed to hold the value or address of *EXPR_P. */
11049 gcc_unreachable ();
11052 /* We used to check the predicate here and return immediately if it
11053 succeeds. This is wrong; the design is for gimplification to be
11054 idempotent, and for the predicates to only test for valid forms, not
11055 whether they are fully simplified. */
11056 if (pre_p == NULL)
11057 pre_p = &internal_pre;
11059 if (post_p == NULL)
11060 post_p = &internal_post;
11062 /* Remember the last statements added to PRE_P and POST_P. Every
11063 new statement added by the gimplification helpers needs to be
11064 annotated with location information. To centralize the
11065 responsibility, we remember the last statement that had been
11066 added to both queues before gimplifying *EXPR_P. If
11067 gimplification produces new statements in PRE_P and POST_P, those
11068 statements will be annotated with the same location information
11069 as *EXPR_P. */
11070 pre_last_gsi = gsi_last (*pre_p);
11071 post_last_gsi = gsi_last (*post_p);
11073 saved_location = input_location;
11074 if (save_expr != error_mark_node
11075 && EXPR_HAS_LOCATION (*expr_p))
11076 input_location = EXPR_LOCATION (*expr_p);
11078 /* Loop over the specific gimplifiers until the toplevel node
11079 remains the same. */
11082 /* Strip away as many useless type conversions as possible
11083 at the toplevel. */
11084 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
11086 /* Remember the expr. */
11087 save_expr = *expr_p;
11089 /* Die, die, die, my darling. */
11090 if (save_expr == error_mark_node
11091 || (TREE_TYPE (save_expr)
11092 && TREE_TYPE (save_expr) == error_mark_node))
11094 ret = GS_ERROR;
11095 break;
11098 /* Do any language-specific gimplification. */
11099 ret = ((enum gimplify_status)
11100 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
11101 if (ret == GS_OK)
11103 if (*expr_p == NULL_TREE)
11104 break;
11105 if (*expr_p != save_expr)
11106 continue;
11108 else if (ret != GS_UNHANDLED)
11109 break;
11111 /* Make sure that all the cases set 'ret' appropriately. */
11112 ret = GS_UNHANDLED;
11113 switch (TREE_CODE (*expr_p))
11115 /* First deal with the special cases. */
11117 case POSTINCREMENT_EXPR:
11118 case POSTDECREMENT_EXPR:
11119 case PREINCREMENT_EXPR:
11120 case PREDECREMENT_EXPR:
11121 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
11122 fallback != fb_none,
11123 TREE_TYPE (*expr_p));
11124 break;
11126 case VIEW_CONVERT_EXPR:
11127 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
11128 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
11130 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11131 post_p, is_gimple_val, fb_rvalue);
11132 recalculate_side_effects (*expr_p);
11133 break;
11135 /* Fallthru. */
11137 case ARRAY_REF:
11138 case ARRAY_RANGE_REF:
11139 case REALPART_EXPR:
11140 case IMAGPART_EXPR:
11141 case COMPONENT_REF:
11142 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
11143 fallback ? fallback : fb_rvalue);
11144 break;
11146 case COND_EXPR:
11147 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
11149 /* C99 code may assign to an array in a structure value of a
11150 conditional expression, and this has undefined behavior
11151 only on execution, so create a temporary if an lvalue is
11152 required. */
11153 if (fallback == fb_lvalue)
11155 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11156 mark_addressable (*expr_p);
11157 ret = GS_OK;
11159 break;
11161 case CALL_EXPR:
11162 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
11164 /* C99 code may assign to an array in a structure returned
11165 from a function, and this has undefined behavior only on
11166 execution, so create a temporary if an lvalue is
11167 required. */
11168 if (fallback == fb_lvalue)
11170 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11171 mark_addressable (*expr_p);
11172 ret = GS_OK;
11174 break;
11176 case TREE_LIST:
11177 gcc_unreachable ();
11179 case COMPOUND_EXPR:
11180 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
11181 break;
11183 case COMPOUND_LITERAL_EXPR:
11184 ret = gimplify_compound_literal_expr (expr_p, pre_p,
11185 gimple_test_f, fallback);
11186 break;
11188 case MODIFY_EXPR:
11189 case INIT_EXPR:
11190 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
11191 fallback != fb_none);
11192 break;
11194 case TRUTH_ANDIF_EXPR:
11195 case TRUTH_ORIF_EXPR:
11197 /* Preserve the original type of the expression and the
11198 source location of the outer expression. */
11199 tree org_type = TREE_TYPE (*expr_p);
11200 *expr_p = gimple_boolify (*expr_p);
11201 *expr_p = build3_loc (input_location, COND_EXPR,
11202 org_type, *expr_p,
11203 fold_convert_loc
11204 (input_location,
11205 org_type, boolean_true_node),
11206 fold_convert_loc
11207 (input_location,
11208 org_type, boolean_false_node));
11209 ret = GS_OK;
11210 break;
11213 case TRUTH_NOT_EXPR:
11215 tree type = TREE_TYPE (*expr_p);
11216 /* The parsers are careful to generate TRUTH_NOT_EXPR
11217 only with operands that are always zero or one.
11218 We do not fold here but handle the only interesting case
11219 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
11220 *expr_p = gimple_boolify (*expr_p);
11221 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
11222 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
11223 TREE_TYPE (*expr_p),
11224 TREE_OPERAND (*expr_p, 0));
11225 else
11226 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
11227 TREE_TYPE (*expr_p),
11228 TREE_OPERAND (*expr_p, 0),
11229 build_int_cst (TREE_TYPE (*expr_p), 1));
11230 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
11231 *expr_p = fold_convert_loc (input_location, type, *expr_p);
11232 ret = GS_OK;
11233 break;
11236 case ADDR_EXPR:
11237 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
11238 break;
11240 case ANNOTATE_EXPR:
11242 tree cond = TREE_OPERAND (*expr_p, 0);
11243 tree kind = TREE_OPERAND (*expr_p, 1);
11244 tree type = TREE_TYPE (cond);
11245 if (!INTEGRAL_TYPE_P (type))
11247 *expr_p = cond;
11248 ret = GS_OK;
11249 break;
11251 tree tmp = create_tmp_var (type);
11252 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
11253 gcall *call
11254 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
11255 gimple_call_set_lhs (call, tmp);
11256 gimplify_seq_add_stmt (pre_p, call);
11257 *expr_p = tmp;
11258 ret = GS_ALL_DONE;
11259 break;
11262 case VA_ARG_EXPR:
11263 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
11264 break;
11266 CASE_CONVERT:
11267 if (IS_EMPTY_STMT (*expr_p))
11269 ret = GS_ALL_DONE;
11270 break;
11273 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
11274 || fallback == fb_none)
11276 /* Just strip a conversion to void (or in void context) and
11277 try again. */
11278 *expr_p = TREE_OPERAND (*expr_p, 0);
11279 ret = GS_OK;
11280 break;
11283 ret = gimplify_conversion (expr_p);
11284 if (ret == GS_ERROR)
11285 break;
11286 if (*expr_p != save_expr)
11287 break;
11288 /* FALLTHRU */
11290 case FIX_TRUNC_EXPR:
11291 /* unary_expr: ... | '(' cast ')' val | ... */
11292 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11293 is_gimple_val, fb_rvalue);
11294 recalculate_side_effects (*expr_p);
11295 break;
11297 case INDIRECT_REF:
11299 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
11300 bool notrap = TREE_THIS_NOTRAP (*expr_p);
11301 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
11303 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
11304 if (*expr_p != save_expr)
11306 ret = GS_OK;
11307 break;
11310 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11311 is_gimple_reg, fb_rvalue);
11312 if (ret == GS_ERROR)
11313 break;
11315 recalculate_side_effects (*expr_p);
11316 *expr_p = fold_build2_loc (input_location, MEM_REF,
11317 TREE_TYPE (*expr_p),
11318 TREE_OPERAND (*expr_p, 0),
11319 build_int_cst (saved_ptr_type, 0));
11320 TREE_THIS_VOLATILE (*expr_p) = volatilep;
11321 TREE_THIS_NOTRAP (*expr_p) = notrap;
11322 ret = GS_OK;
11323 break;
11326 /* We arrive here through the various re-gimplifcation paths. */
11327 case MEM_REF:
11328 /* First try re-folding the whole thing. */
11329 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
11330 TREE_OPERAND (*expr_p, 0),
11331 TREE_OPERAND (*expr_p, 1));
11332 if (tmp)
11334 REF_REVERSE_STORAGE_ORDER (tmp)
11335 = REF_REVERSE_STORAGE_ORDER (*expr_p);
11336 *expr_p = tmp;
11337 recalculate_side_effects (*expr_p);
11338 ret = GS_OK;
11339 break;
11341 /* Avoid re-gimplifying the address operand if it is already
11342 in suitable form. Re-gimplifying would mark the address
11343 operand addressable. Always gimplify when not in SSA form
11344 as we still may have to gimplify decls with value-exprs. */
11345 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
11346 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
11348 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11349 is_gimple_mem_ref_addr, fb_rvalue);
11350 if (ret == GS_ERROR)
11351 break;
11353 recalculate_side_effects (*expr_p);
11354 ret = GS_ALL_DONE;
11355 break;
11357 /* Constants need not be gimplified. */
11358 case INTEGER_CST:
11359 case REAL_CST:
11360 case FIXED_CST:
11361 case STRING_CST:
11362 case COMPLEX_CST:
11363 case VECTOR_CST:
11364 /* Drop the overflow flag on constants, we do not want
11365 that in the GIMPLE IL. */
11366 if (TREE_OVERFLOW_P (*expr_p))
11367 *expr_p = drop_tree_overflow (*expr_p);
11368 ret = GS_ALL_DONE;
11369 break;
11371 case CONST_DECL:
11372 /* If we require an lvalue, such as for ADDR_EXPR, retain the
11373 CONST_DECL node. Otherwise the decl is replaceable by its
11374 value. */
11375 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
11376 if (fallback & fb_lvalue)
11377 ret = GS_ALL_DONE;
11378 else
11380 *expr_p = DECL_INITIAL (*expr_p);
11381 ret = GS_OK;
11383 break;
11385 case DECL_EXPR:
11386 ret = gimplify_decl_expr (expr_p, pre_p);
11387 break;
11389 case BIND_EXPR:
11390 ret = gimplify_bind_expr (expr_p, pre_p);
11391 break;
11393 case LOOP_EXPR:
11394 ret = gimplify_loop_expr (expr_p, pre_p);
11395 break;
11397 case SWITCH_EXPR:
11398 ret = gimplify_switch_expr (expr_p, pre_p);
11399 break;
11401 case EXIT_EXPR:
11402 ret = gimplify_exit_expr (expr_p);
11403 break;
11405 case GOTO_EXPR:
11406 /* If the target is not LABEL, then it is a computed jump
11407 and the target needs to be gimplified. */
11408 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
11410 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
11411 NULL, is_gimple_val, fb_rvalue);
11412 if (ret == GS_ERROR)
11413 break;
11415 gimplify_seq_add_stmt (pre_p,
11416 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
11417 ret = GS_ALL_DONE;
11418 break;
11420 case PREDICT_EXPR:
11421 gimplify_seq_add_stmt (pre_p,
11422 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
11423 PREDICT_EXPR_OUTCOME (*expr_p)));
11424 ret = GS_ALL_DONE;
11425 break;
11427 case LABEL_EXPR:
11428 ret = gimplify_label_expr (expr_p, pre_p);
11429 label = LABEL_EXPR_LABEL (*expr_p);
11430 gcc_assert (decl_function_context (label) == current_function_decl);
11432 /* If the label is used in a goto statement, or address of the label
11433 is taken, we need to unpoison all variables that were seen so far.
11434 Doing so would prevent us from reporting a false positives. */
11435 if (asan_sanitize_use_after_scope ()
11436 && asan_used_labels != NULL
11437 && asan_used_labels->contains (label))
11438 asan_poison_variables (asan_poisoned_variables, false, pre_p);
11439 break;
11441 case CASE_LABEL_EXPR:
11442 ret = gimplify_case_label_expr (expr_p, pre_p);
11444 if (gimplify_ctxp->live_switch_vars)
11445 asan_poison_variables (gimplify_ctxp->live_switch_vars, false,
11446 pre_p);
11447 break;
11449 case RETURN_EXPR:
11450 ret = gimplify_return_expr (*expr_p, pre_p);
11451 break;
11453 case CONSTRUCTOR:
11454 /* Don't reduce this in place; let gimplify_init_constructor work its
11455 magic. Buf if we're just elaborating this for side effects, just
11456 gimplify any element that has side-effects. */
11457 if (fallback == fb_none)
11459 unsigned HOST_WIDE_INT ix;
11460 tree val;
11461 tree temp = NULL_TREE;
11462 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
11463 if (TREE_SIDE_EFFECTS (val))
11464 append_to_statement_list (val, &temp);
11466 *expr_p = temp;
11467 ret = temp ? GS_OK : GS_ALL_DONE;
11469 /* C99 code may assign to an array in a constructed
11470 structure or union, and this has undefined behavior only
11471 on execution, so create a temporary if an lvalue is
11472 required. */
11473 else if (fallback == fb_lvalue)
11475 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11476 mark_addressable (*expr_p);
11477 ret = GS_OK;
11479 else
11480 ret = GS_ALL_DONE;
11481 break;
11483 /* The following are special cases that are not handled by the
11484 original GIMPLE grammar. */
11486 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11487 eliminated. */
11488 case SAVE_EXPR:
11489 ret = gimplify_save_expr (expr_p, pre_p, post_p);
11490 break;
11492 case BIT_FIELD_REF:
11493 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11494 post_p, is_gimple_lvalue, fb_either);
11495 recalculate_side_effects (*expr_p);
11496 break;
11498 case TARGET_MEM_REF:
11500 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
11502 if (TMR_BASE (*expr_p))
11503 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
11504 post_p, is_gimple_mem_ref_addr, fb_either);
11505 if (TMR_INDEX (*expr_p))
11506 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
11507 post_p, is_gimple_val, fb_rvalue);
11508 if (TMR_INDEX2 (*expr_p))
11509 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
11510 post_p, is_gimple_val, fb_rvalue);
11511 /* TMR_STEP and TMR_OFFSET are always integer constants. */
11512 ret = MIN (r0, r1);
11514 break;
11516 case NON_LVALUE_EXPR:
11517 /* This should have been stripped above. */
11518 gcc_unreachable ();
11520 case ASM_EXPR:
11521 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
11522 break;
11524 case TRY_FINALLY_EXPR:
11525 case TRY_CATCH_EXPR:
11527 gimple_seq eval, cleanup;
11528 gtry *try_;
11530 /* Calls to destructors are generated automatically in FINALLY/CATCH
11531 block. They should have location as UNKNOWN_LOCATION. However,
11532 gimplify_call_expr will reset these call stmts to input_location
11533 if it finds stmt's location is unknown. To prevent resetting for
11534 destructors, we set the input_location to unknown.
11535 Note that this only affects the destructor calls in FINALLY/CATCH
11536 block, and will automatically reset to its original value by the
11537 end of gimplify_expr. */
11538 input_location = UNKNOWN_LOCATION;
11539 eval = cleanup = NULL;
11540 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
11541 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
11542 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
11543 if (gimple_seq_empty_p (cleanup))
11545 gimple_seq_add_seq (pre_p, eval);
11546 ret = GS_ALL_DONE;
11547 break;
11549 try_ = gimple_build_try (eval, cleanup,
11550 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
11551 ? GIMPLE_TRY_FINALLY
11552 : GIMPLE_TRY_CATCH);
11553 if (EXPR_HAS_LOCATION (save_expr))
11554 gimple_set_location (try_, EXPR_LOCATION (save_expr));
11555 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
11556 gimple_set_location (try_, saved_location);
11557 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
11558 gimple_try_set_catch_is_cleanup (try_,
11559 TRY_CATCH_IS_CLEANUP (*expr_p));
11560 gimplify_seq_add_stmt (pre_p, try_);
11561 ret = GS_ALL_DONE;
11562 break;
11565 case CLEANUP_POINT_EXPR:
11566 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
11567 break;
11569 case TARGET_EXPR:
11570 ret = gimplify_target_expr (expr_p, pre_p, post_p);
11571 break;
11573 case CATCH_EXPR:
11575 gimple *c;
11576 gimple_seq handler = NULL;
11577 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
11578 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
11579 gimplify_seq_add_stmt (pre_p, c);
11580 ret = GS_ALL_DONE;
11581 break;
11584 case EH_FILTER_EXPR:
11586 gimple *ehf;
11587 gimple_seq failure = NULL;
11589 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
11590 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
11591 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
11592 gimplify_seq_add_stmt (pre_p, ehf);
11593 ret = GS_ALL_DONE;
11594 break;
11597 case OBJ_TYPE_REF:
11599 enum gimplify_status r0, r1;
11600 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
11601 post_p, is_gimple_val, fb_rvalue);
11602 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
11603 post_p, is_gimple_val, fb_rvalue);
11604 TREE_SIDE_EFFECTS (*expr_p) = 0;
11605 ret = MIN (r0, r1);
11607 break;
11609 case LABEL_DECL:
11610 /* We get here when taking the address of a label. We mark
11611 the label as "forced"; meaning it can never be removed and
11612 it is a potential target for any computed goto. */
11613 FORCED_LABEL (*expr_p) = 1;
11614 ret = GS_ALL_DONE;
11615 break;
11617 case STATEMENT_LIST:
11618 ret = gimplify_statement_list (expr_p, pre_p);
11619 break;
11621 case WITH_SIZE_EXPR:
11623 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11624 post_p == &internal_post ? NULL : post_p,
11625 gimple_test_f, fallback);
11626 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
11627 is_gimple_val, fb_rvalue);
11628 ret = GS_ALL_DONE;
11630 break;
11632 case VAR_DECL:
11633 case PARM_DECL:
11634 ret = gimplify_var_or_parm_decl (expr_p);
11635 break;
11637 case RESULT_DECL:
11638 /* When within an OMP context, notice uses of variables. */
11639 if (gimplify_omp_ctxp)
11640 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
11641 ret = GS_ALL_DONE;
11642 break;
11644 case SSA_NAME:
11645 /* Allow callbacks into the gimplifier during optimization. */
11646 ret = GS_ALL_DONE;
11647 break;
11649 case OMP_PARALLEL:
11650 gimplify_omp_parallel (expr_p, pre_p);
11651 ret = GS_ALL_DONE;
11652 break;
11654 case OMP_TASK:
11655 gimplify_omp_task (expr_p, pre_p);
11656 ret = GS_ALL_DONE;
11657 break;
11659 case OMP_FOR:
11660 case OMP_SIMD:
11661 case CILK_SIMD:
11662 case CILK_FOR:
11663 case OMP_DISTRIBUTE:
11664 case OMP_TASKLOOP:
11665 case OACC_LOOP:
11666 ret = gimplify_omp_for (expr_p, pre_p);
11667 break;
11669 case OACC_CACHE:
11670 gimplify_oacc_cache (expr_p, pre_p);
11671 ret = GS_ALL_DONE;
11672 break;
11674 case OACC_DECLARE:
11675 gimplify_oacc_declare (expr_p, pre_p);
11676 ret = GS_ALL_DONE;
11677 break;
11679 case OACC_HOST_DATA:
11680 case OACC_DATA:
11681 case OACC_KERNELS:
11682 case OACC_PARALLEL:
11683 case OMP_SECTIONS:
11684 case OMP_SINGLE:
11685 case OMP_TARGET:
11686 case OMP_TARGET_DATA:
11687 case OMP_TEAMS:
11688 gimplify_omp_workshare (expr_p, pre_p);
11689 ret = GS_ALL_DONE;
11690 break;
11692 case OACC_ENTER_DATA:
11693 case OACC_EXIT_DATA:
11694 case OACC_UPDATE:
11695 case OMP_TARGET_UPDATE:
11696 case OMP_TARGET_ENTER_DATA:
11697 case OMP_TARGET_EXIT_DATA:
11698 gimplify_omp_target_update (expr_p, pre_p);
11699 ret = GS_ALL_DONE;
11700 break;
11702 case OMP_SECTION:
11703 case OMP_MASTER:
11704 case OMP_TASKGROUP:
11705 case OMP_ORDERED:
11706 case OMP_CRITICAL:
11708 gimple_seq body = NULL;
11709 gimple *g;
11711 gimplify_and_add (OMP_BODY (*expr_p), &body);
11712 switch (TREE_CODE (*expr_p))
11714 case OMP_SECTION:
11715 g = gimple_build_omp_section (body);
11716 break;
11717 case OMP_MASTER:
11718 g = gimple_build_omp_master (body);
11719 break;
11720 case OMP_TASKGROUP:
11722 gimple_seq cleanup = NULL;
11723 tree fn
11724 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
11725 g = gimple_build_call (fn, 0);
11726 gimple_seq_add_stmt (&cleanup, g);
11727 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
11728 body = NULL;
11729 gimple_seq_add_stmt (&body, g);
11730 g = gimple_build_omp_taskgroup (body);
11732 break;
11733 case OMP_ORDERED:
11734 g = gimplify_omp_ordered (*expr_p, body);
11735 break;
11736 case OMP_CRITICAL:
11737 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
11738 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
11739 gimplify_adjust_omp_clauses (pre_p, body,
11740 &OMP_CRITICAL_CLAUSES (*expr_p),
11741 OMP_CRITICAL);
11742 g = gimple_build_omp_critical (body,
11743 OMP_CRITICAL_NAME (*expr_p),
11744 OMP_CRITICAL_CLAUSES (*expr_p));
11745 break;
11746 default:
11747 gcc_unreachable ();
11749 gimplify_seq_add_stmt (pre_p, g);
11750 ret = GS_ALL_DONE;
11751 break;
11754 case OMP_ATOMIC:
11755 case OMP_ATOMIC_READ:
11756 case OMP_ATOMIC_CAPTURE_OLD:
11757 case OMP_ATOMIC_CAPTURE_NEW:
11758 ret = gimplify_omp_atomic (expr_p, pre_p);
11759 break;
11761 case TRANSACTION_EXPR:
11762 ret = gimplify_transaction (expr_p, pre_p);
11763 break;
11765 case TRUTH_AND_EXPR:
11766 case TRUTH_OR_EXPR:
11767 case TRUTH_XOR_EXPR:
11769 tree orig_type = TREE_TYPE (*expr_p);
11770 tree new_type, xop0, xop1;
11771 *expr_p = gimple_boolify (*expr_p);
11772 new_type = TREE_TYPE (*expr_p);
11773 if (!useless_type_conversion_p (orig_type, new_type))
11775 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
11776 ret = GS_OK;
11777 break;
11780 /* Boolified binary truth expressions are semantically equivalent
11781 to bitwise binary expressions. Canonicalize them to the
11782 bitwise variant. */
11783 switch (TREE_CODE (*expr_p))
11785 case TRUTH_AND_EXPR:
11786 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
11787 break;
11788 case TRUTH_OR_EXPR:
11789 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
11790 break;
11791 case TRUTH_XOR_EXPR:
11792 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
11793 break;
11794 default:
11795 break;
11797 /* Now make sure that operands have compatible type to
11798 expression's new_type. */
11799 xop0 = TREE_OPERAND (*expr_p, 0);
11800 xop1 = TREE_OPERAND (*expr_p, 1);
11801 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
11802 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
11803 new_type,
11804 xop0);
11805 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
11806 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
11807 new_type,
11808 xop1);
11809 /* Continue classified as tcc_binary. */
11810 goto expr_2;
11813 case VEC_COND_EXPR:
11815 enum gimplify_status r0, r1, r2;
11817 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11818 post_p, is_gimple_condexpr, fb_rvalue);
11819 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
11820 post_p, is_gimple_val, fb_rvalue);
11821 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
11822 post_p, is_gimple_val, fb_rvalue);
11824 ret = MIN (MIN (r0, r1), r2);
11825 recalculate_side_effects (*expr_p);
11827 break;
11829 case FMA_EXPR:
11830 case VEC_PERM_EXPR:
11831 /* Classified as tcc_expression. */
11832 goto expr_3;
11834 case BIT_INSERT_EXPR:
11835 /* Argument 3 is a constant. */
11836 goto expr_2;
11838 case POINTER_PLUS_EXPR:
11840 enum gimplify_status r0, r1;
11841 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11842 post_p, is_gimple_val, fb_rvalue);
11843 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
11844 post_p, is_gimple_val, fb_rvalue);
11845 recalculate_side_effects (*expr_p);
11846 ret = MIN (r0, r1);
11847 break;
11850 case CILK_SYNC_STMT:
11852 if (!fn_contains_cilk_spawn_p (cfun))
11854 error_at (EXPR_LOCATION (*expr_p),
11855 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
11856 ret = GS_ERROR;
11858 else
11860 gimplify_cilk_sync (expr_p, pre_p);
11861 ret = GS_ALL_DONE;
11863 break;
11866 default:
11867 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
11869 case tcc_comparison:
11870 /* Handle comparison of objects of non scalar mode aggregates
11871 with a call to memcmp. It would be nice to only have to do
11872 this for variable-sized objects, but then we'd have to allow
11873 the same nest of reference nodes we allow for MODIFY_EXPR and
11874 that's too complex.
11876 Compare scalar mode aggregates as scalar mode values. Using
11877 memcmp for them would be very inefficient at best, and is
11878 plain wrong if bitfields are involved. */
11880 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
11882 /* Vector comparisons need no boolification. */
11883 if (TREE_CODE (type) == VECTOR_TYPE)
11884 goto expr_2;
11885 else if (!AGGREGATE_TYPE_P (type))
11887 tree org_type = TREE_TYPE (*expr_p);
11888 *expr_p = gimple_boolify (*expr_p);
11889 if (!useless_type_conversion_p (org_type,
11890 TREE_TYPE (*expr_p)))
11892 *expr_p = fold_convert_loc (input_location,
11893 org_type, *expr_p);
11894 ret = GS_OK;
11896 else
11897 goto expr_2;
11899 else if (TYPE_MODE (type) != BLKmode)
11900 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
11901 else
11902 ret = gimplify_variable_sized_compare (expr_p);
11904 break;
11907 /* If *EXPR_P does not need to be special-cased, handle it
11908 according to its class. */
11909 case tcc_unary:
11910 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11911 post_p, is_gimple_val, fb_rvalue);
11912 break;
11914 case tcc_binary:
11915 expr_2:
11917 enum gimplify_status r0, r1;
11919 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11920 post_p, is_gimple_val, fb_rvalue);
11921 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
11922 post_p, is_gimple_val, fb_rvalue);
11924 ret = MIN (r0, r1);
11925 break;
11928 expr_3:
11930 enum gimplify_status r0, r1, r2;
11932 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11933 post_p, is_gimple_val, fb_rvalue);
11934 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
11935 post_p, is_gimple_val, fb_rvalue);
11936 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
11937 post_p, is_gimple_val, fb_rvalue);
11939 ret = MIN (MIN (r0, r1), r2);
11940 break;
11943 case tcc_declaration:
11944 case tcc_constant:
11945 ret = GS_ALL_DONE;
11946 goto dont_recalculate;
11948 default:
11949 gcc_unreachable ();
11952 recalculate_side_effects (*expr_p);
11954 dont_recalculate:
11955 break;
11958 gcc_assert (*expr_p || ret != GS_OK);
11960 while (ret == GS_OK);
11962 /* If we encountered an error_mark somewhere nested inside, either
11963 stub out the statement or propagate the error back out. */
11964 if (ret == GS_ERROR)
11966 if (is_statement)
11967 *expr_p = NULL;
11968 goto out;
11971 /* This was only valid as a return value from the langhook, which
11972 we handled. Make sure it doesn't escape from any other context. */
11973 gcc_assert (ret != GS_UNHANDLED);
11975 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
11977 /* We aren't looking for a value, and we don't have a valid
11978 statement. If it doesn't have side-effects, throw it away. */
11979 if (!TREE_SIDE_EFFECTS (*expr_p))
11980 *expr_p = NULL;
11981 else if (!TREE_THIS_VOLATILE (*expr_p))
11983 /* This is probably a _REF that contains something nested that
11984 has side effects. Recurse through the operands to find it. */
11985 enum tree_code code = TREE_CODE (*expr_p);
11987 switch (code)
11989 case COMPONENT_REF:
11990 case REALPART_EXPR:
11991 case IMAGPART_EXPR:
11992 case VIEW_CONVERT_EXPR:
11993 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11994 gimple_test_f, fallback);
11995 break;
11997 case ARRAY_REF:
11998 case ARRAY_RANGE_REF:
11999 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
12000 gimple_test_f, fallback);
12001 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
12002 gimple_test_f, fallback);
12003 break;
12005 default:
12006 /* Anything else with side-effects must be converted to
12007 a valid statement before we get here. */
12008 gcc_unreachable ();
12011 *expr_p = NULL;
12013 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
12014 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
12016 /* Historically, the compiler has treated a bare reference
12017 to a non-BLKmode volatile lvalue as forcing a load. */
12018 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
12020 /* Normally, we do not want to create a temporary for a
12021 TREE_ADDRESSABLE type because such a type should not be
12022 copied by bitwise-assignment. However, we make an
12023 exception here, as all we are doing here is ensuring that
12024 we read the bytes that make up the type. We use
12025 create_tmp_var_raw because create_tmp_var will abort when
12026 given a TREE_ADDRESSABLE type. */
12027 tree tmp = create_tmp_var_raw (type, "vol");
12028 gimple_add_tmp_var (tmp);
12029 gimplify_assign (tmp, *expr_p, pre_p);
12030 *expr_p = NULL;
12032 else
12033 /* We can't do anything useful with a volatile reference to
12034 an incomplete type, so just throw it away. Likewise for
12035 a BLKmode type, since any implicit inner load should
12036 already have been turned into an explicit one by the
12037 gimplification process. */
12038 *expr_p = NULL;
12041 /* If we are gimplifying at the statement level, we're done. Tack
12042 everything together and return. */
12043 if (fallback == fb_none || is_statement)
12045 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
12046 it out for GC to reclaim it. */
12047 *expr_p = NULL_TREE;
12049 if (!gimple_seq_empty_p (internal_pre)
12050 || !gimple_seq_empty_p (internal_post))
12052 gimplify_seq_add_seq (&internal_pre, internal_post);
12053 gimplify_seq_add_seq (pre_p, internal_pre);
12056 /* The result of gimplifying *EXPR_P is going to be the last few
12057 statements in *PRE_P and *POST_P. Add location information
12058 to all the statements that were added by the gimplification
12059 helpers. */
12060 if (!gimple_seq_empty_p (*pre_p))
12061 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
12063 if (!gimple_seq_empty_p (*post_p))
12064 annotate_all_with_location_after (*post_p, post_last_gsi,
12065 input_location);
12067 goto out;
12070 #ifdef ENABLE_GIMPLE_CHECKING
12071 if (*expr_p)
12073 enum tree_code code = TREE_CODE (*expr_p);
12074 /* These expressions should already be in gimple IR form. */
12075 gcc_assert (code != MODIFY_EXPR
12076 && code != ASM_EXPR
12077 && code != BIND_EXPR
12078 && code != CATCH_EXPR
12079 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
12080 && code != EH_FILTER_EXPR
12081 && code != GOTO_EXPR
12082 && code != LABEL_EXPR
12083 && code != LOOP_EXPR
12084 && code != SWITCH_EXPR
12085 && code != TRY_FINALLY_EXPR
12086 && code != OACC_PARALLEL
12087 && code != OACC_KERNELS
12088 && code != OACC_DATA
12089 && code != OACC_HOST_DATA
12090 && code != OACC_DECLARE
12091 && code != OACC_UPDATE
12092 && code != OACC_ENTER_DATA
12093 && code != OACC_EXIT_DATA
12094 && code != OACC_CACHE
12095 && code != OMP_CRITICAL
12096 && code != OMP_FOR
12097 && code != OACC_LOOP
12098 && code != OMP_MASTER
12099 && code != OMP_TASKGROUP
12100 && code != OMP_ORDERED
12101 && code != OMP_PARALLEL
12102 && code != OMP_SECTIONS
12103 && code != OMP_SECTION
12104 && code != OMP_SINGLE);
12106 #endif
12108 /* Otherwise we're gimplifying a subexpression, so the resulting
12109 value is interesting. If it's a valid operand that matches
12110 GIMPLE_TEST_F, we're done. Unless we are handling some
12111 post-effects internally; if that's the case, we need to copy into
12112 a temporary before adding the post-effects to POST_P. */
12113 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
12114 goto out;
12116 /* Otherwise, we need to create a new temporary for the gimplified
12117 expression. */
12119 /* We can't return an lvalue if we have an internal postqueue. The
12120 object the lvalue refers to would (probably) be modified by the
12121 postqueue; we need to copy the value out first, which means an
12122 rvalue. */
12123 if ((fallback & fb_lvalue)
12124 && gimple_seq_empty_p (internal_post)
12125 && is_gimple_addressable (*expr_p))
12127 /* An lvalue will do. Take the address of the expression, store it
12128 in a temporary, and replace the expression with an INDIRECT_REF of
12129 that temporary. */
12130 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
12131 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
12132 *expr_p = build_simple_mem_ref (tmp);
12134 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
12136 /* An rvalue will do. Assign the gimplified expression into a
12137 new temporary TMP and replace the original expression with
12138 TMP. First, make sure that the expression has a type so that
12139 it can be assigned into a temporary. */
12140 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
12141 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
12143 else
12145 #ifdef ENABLE_GIMPLE_CHECKING
12146 if (!(fallback & fb_mayfail))
12148 fprintf (stderr, "gimplification failed:\n");
12149 print_generic_expr (stderr, *expr_p, 0);
12150 debug_tree (*expr_p);
12151 internal_error ("gimplification failed");
12153 #endif
12154 gcc_assert (fallback & fb_mayfail);
12156 /* If this is an asm statement, and the user asked for the
12157 impossible, don't die. Fail and let gimplify_asm_expr
12158 issue an error. */
12159 ret = GS_ERROR;
12160 goto out;
12163 /* Make sure the temporary matches our predicate. */
12164 gcc_assert ((*gimple_test_f) (*expr_p));
12166 if (!gimple_seq_empty_p (internal_post))
12168 annotate_all_with_location (internal_post, input_location);
12169 gimplify_seq_add_seq (pre_p, internal_post);
12172 out:
12173 input_location = saved_location;
12174 return ret;
12177 /* Like gimplify_expr but make sure the gimplified result is not itself
12178 a SSA name (but a decl if it were). Temporaries required by
12179 evaluating *EXPR_P may be still SSA names. */
12181 static enum gimplify_status
12182 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
12183 bool (*gimple_test_f) (tree), fallback_t fallback,
12184 bool allow_ssa)
12186 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
12187 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
12188 gimple_test_f, fallback);
12189 if (! allow_ssa
12190 && TREE_CODE (*expr_p) == SSA_NAME)
12192 tree name = *expr_p;
12193 if (was_ssa_name_p)
12194 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
12195 else
12197 /* Avoid the extra copy if possible. */
12198 *expr_p = create_tmp_reg (TREE_TYPE (name));
12199 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
12200 release_ssa_name (name);
12203 return ret;
12206 /* Look through TYPE for variable-sized objects and gimplify each such
12207 size that we find. Add to LIST_P any statements generated. */
12209 void
12210 gimplify_type_sizes (tree type, gimple_seq *list_p)
12212 tree field, t;
12214 if (type == NULL || type == error_mark_node)
12215 return;
12217 /* We first do the main variant, then copy into any other variants. */
12218 type = TYPE_MAIN_VARIANT (type);
12220 /* Avoid infinite recursion. */
12221 if (TYPE_SIZES_GIMPLIFIED (type))
12222 return;
12224 TYPE_SIZES_GIMPLIFIED (type) = 1;
12226 switch (TREE_CODE (type))
12228 case INTEGER_TYPE:
12229 case ENUMERAL_TYPE:
12230 case BOOLEAN_TYPE:
12231 case REAL_TYPE:
12232 case FIXED_POINT_TYPE:
12233 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
12234 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
12236 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12238 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
12239 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
12241 break;
12243 case ARRAY_TYPE:
12244 /* These types may not have declarations, so handle them here. */
12245 gimplify_type_sizes (TREE_TYPE (type), list_p);
12246 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
12247 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12248 with assigned stack slots, for -O1+ -g they should be tracked
12249 by VTA. */
12250 if (!(TYPE_NAME (type)
12251 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12252 && DECL_IGNORED_P (TYPE_NAME (type)))
12253 && TYPE_DOMAIN (type)
12254 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
12256 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
12257 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12258 DECL_IGNORED_P (t) = 0;
12259 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12260 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12261 DECL_IGNORED_P (t) = 0;
12263 break;
12265 case RECORD_TYPE:
12266 case UNION_TYPE:
12267 case QUAL_UNION_TYPE:
12268 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12269 if (TREE_CODE (field) == FIELD_DECL)
12271 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
12272 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
12273 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
12274 gimplify_type_sizes (TREE_TYPE (field), list_p);
12276 break;
12278 case POINTER_TYPE:
12279 case REFERENCE_TYPE:
12280 /* We used to recurse on the pointed-to type here, which turned out to
12281 be incorrect because its definition might refer to variables not
12282 yet initialized at this point if a forward declaration is involved.
12284 It was actually useful for anonymous pointed-to types to ensure
12285 that the sizes evaluation dominates every possible later use of the
12286 values. Restricting to such types here would be safe since there
12287 is no possible forward declaration around, but would introduce an
12288 undesirable middle-end semantic to anonymity. We then defer to
12289 front-ends the responsibility of ensuring that the sizes are
12290 evaluated both early and late enough, e.g. by attaching artificial
12291 type declarations to the tree. */
12292 break;
12294 default:
12295 break;
12298 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
12299 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
12301 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12303 TYPE_SIZE (t) = TYPE_SIZE (type);
12304 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
12305 TYPE_SIZES_GIMPLIFIED (t) = 1;
12309 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12310 a size or position, has had all of its SAVE_EXPRs evaluated.
12311 We add any required statements to *STMT_P. */
12313 void
12314 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
12316 tree expr = *expr_p;
12318 /* We don't do anything if the value isn't there, is constant, or contains
12319 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
12320 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
12321 will want to replace it with a new variable, but that will cause problems
12322 if this type is from outside the function. It's OK to have that here. */
12323 if (is_gimple_sizepos (expr))
12324 return;
12326 *expr_p = unshare_expr (expr);
12328 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12329 if the def vanishes. */
12330 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
12333 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12334 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
12335 is true, also gimplify the parameters. */
12337 gbind *
12338 gimplify_body (tree fndecl, bool do_parms)
12340 location_t saved_location = input_location;
12341 gimple_seq parm_stmts, seq;
12342 gimple *outer_stmt;
12343 gbind *outer_bind;
12344 struct cgraph_node *cgn;
12346 timevar_push (TV_TREE_GIMPLIFY);
12348 init_tree_ssa (cfun);
12350 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12351 gimplification. */
12352 default_rtl_profile ();
12354 gcc_assert (gimplify_ctxp == NULL);
12355 push_gimplify_context (true);
12357 if (flag_openacc || flag_openmp)
12359 gcc_assert (gimplify_omp_ctxp == NULL);
12360 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
12361 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
12364 /* Unshare most shared trees in the body and in that of any nested functions.
12365 It would seem we don't have to do this for nested functions because
12366 they are supposed to be output and then the outer function gimplified
12367 first, but the g++ front end doesn't always do it that way. */
12368 unshare_body (fndecl);
12369 unvisit_body (fndecl);
12371 cgn = cgraph_node::get (fndecl);
12372 if (cgn && cgn->origin)
12373 nonlocal_vlas = new hash_set<tree>;
12375 /* Make sure input_location isn't set to something weird. */
12376 input_location = DECL_SOURCE_LOCATION (fndecl);
12378 /* Resolve callee-copies. This has to be done before processing
12379 the body so that DECL_VALUE_EXPR gets processed correctly. */
12380 parm_stmts = do_parms ? gimplify_parameters () : NULL;
12382 /* Gimplify the function's body. */
12383 seq = NULL;
12384 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
12385 outer_stmt = gimple_seq_first_stmt (seq);
12386 if (!outer_stmt)
12388 outer_stmt = gimple_build_nop ();
12389 gimplify_seq_add_stmt (&seq, outer_stmt);
12392 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
12393 not the case, wrap everything in a GIMPLE_BIND to make it so. */
12394 if (gimple_code (outer_stmt) == GIMPLE_BIND
12395 && gimple_seq_first (seq) == gimple_seq_last (seq))
12396 outer_bind = as_a <gbind *> (outer_stmt);
12397 else
12398 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
12400 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12402 /* If we had callee-copies statements, insert them at the beginning
12403 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
12404 if (!gimple_seq_empty_p (parm_stmts))
12406 tree parm;
12408 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
12409 gimple_bind_set_body (outer_bind, parm_stmts);
12411 for (parm = DECL_ARGUMENTS (current_function_decl);
12412 parm; parm = DECL_CHAIN (parm))
12413 if (DECL_HAS_VALUE_EXPR_P (parm))
12415 DECL_HAS_VALUE_EXPR_P (parm) = 0;
12416 DECL_IGNORED_P (parm) = 0;
12420 if (nonlocal_vlas)
12422 if (nonlocal_vla_vars)
12424 /* tree-nested.c may later on call declare_vars (..., true);
12425 which relies on BLOCK_VARS chain to be the tail of the
12426 gimple_bind_vars chain. Ensure we don't violate that
12427 assumption. */
12428 if (gimple_bind_block (outer_bind)
12429 == DECL_INITIAL (current_function_decl))
12430 declare_vars (nonlocal_vla_vars, outer_bind, true);
12431 else
12432 BLOCK_VARS (DECL_INITIAL (current_function_decl))
12433 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
12434 nonlocal_vla_vars);
12435 nonlocal_vla_vars = NULL_TREE;
12437 delete nonlocal_vlas;
12438 nonlocal_vlas = NULL;
12441 if ((flag_openacc || flag_openmp || flag_openmp_simd)
12442 && gimplify_omp_ctxp)
12444 delete_omp_context (gimplify_omp_ctxp);
12445 gimplify_omp_ctxp = NULL;
12448 pop_gimplify_context (outer_bind);
12449 gcc_assert (gimplify_ctxp == NULL);
12451 if (flag_checking && !seen_error ())
12452 verify_gimple_in_seq (gimple_bind_body (outer_bind));
12454 timevar_pop (TV_TREE_GIMPLIFY);
12455 input_location = saved_location;
12457 return outer_bind;
12460 typedef char *char_p; /* For DEF_VEC_P. */
12462 /* Return whether we should exclude FNDECL from instrumentation. */
12464 static bool
12465 flag_instrument_functions_exclude_p (tree fndecl)
12467 vec<char_p> *v;
12469 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
12470 if (v && v->length () > 0)
12472 const char *name;
12473 int i;
12474 char *s;
12476 name = lang_hooks.decl_printable_name (fndecl, 0);
12477 FOR_EACH_VEC_ELT (*v, i, s)
12478 if (strstr (name, s) != NULL)
12479 return true;
12482 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
12483 if (v && v->length () > 0)
12485 const char *name;
12486 int i;
12487 char *s;
12489 name = DECL_SOURCE_FILE (fndecl);
12490 FOR_EACH_VEC_ELT (*v, i, s)
12491 if (strstr (name, s) != NULL)
12492 return true;
12495 return false;
12498 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
12499 node for the function we want to gimplify.
12501 Return the sequence of GIMPLE statements corresponding to the body
12502 of FNDECL. */
12504 void
12505 gimplify_function_tree (tree fndecl)
12507 tree parm, ret;
12508 gimple_seq seq;
12509 gbind *bind;
12511 gcc_assert (!gimple_body (fndecl));
12513 if (DECL_STRUCT_FUNCTION (fndecl))
12514 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
12515 else
12516 push_struct_function (fndecl);
12518 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12519 if necessary. */
12520 cfun->curr_properties |= PROP_gimple_lva;
12522 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
12524 /* Preliminarily mark non-addressed complex variables as eligible
12525 for promotion to gimple registers. We'll transform their uses
12526 as we find them. */
12527 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
12528 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
12529 && !TREE_THIS_VOLATILE (parm)
12530 && !needs_to_live_in_memory (parm))
12531 DECL_GIMPLE_REG_P (parm) = 1;
12534 ret = DECL_RESULT (fndecl);
12535 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
12536 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
12537 && !needs_to_live_in_memory (ret))
12538 DECL_GIMPLE_REG_P (ret) = 1;
12540 asan_poisoned_variables = new hash_set<tree> ();
12541 bind = gimplify_body (fndecl, true);
12542 delete asan_poisoned_variables;
12543 asan_poisoned_variables = NULL;
12545 /* The tree body of the function is no longer needed, replace it
12546 with the new GIMPLE body. */
12547 seq = NULL;
12548 gimple_seq_add_stmt (&seq, bind);
12549 gimple_set_body (fndecl, seq);
12551 /* If we're instrumenting function entry/exit, then prepend the call to
12552 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12553 catch the exit hook. */
12554 /* ??? Add some way to ignore exceptions for this TFE. */
12555 if (flag_instrument_function_entry_exit
12556 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
12557 && !flag_instrument_functions_exclude_p (fndecl))
12559 tree x;
12560 gbind *new_bind;
12561 gimple *tf;
12562 gimple_seq cleanup = NULL, body = NULL;
12563 tree tmp_var;
12564 gcall *call;
12566 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12567 call = gimple_build_call (x, 1, integer_zero_node);
12568 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12569 gimple_call_set_lhs (call, tmp_var);
12570 gimplify_seq_add_stmt (&cleanup, call);
12571 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
12572 call = gimple_build_call (x, 2,
12573 build_fold_addr_expr (current_function_decl),
12574 tmp_var);
12575 gimplify_seq_add_stmt (&cleanup, call);
12576 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
12578 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12579 call = gimple_build_call (x, 1, integer_zero_node);
12580 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12581 gimple_call_set_lhs (call, tmp_var);
12582 gimplify_seq_add_stmt (&body, call);
12583 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
12584 call = gimple_build_call (x, 2,
12585 build_fold_addr_expr (current_function_decl),
12586 tmp_var);
12587 gimplify_seq_add_stmt (&body, call);
12588 gimplify_seq_add_stmt (&body, tf);
12589 new_bind = gimple_build_bind (NULL, body, NULL);
12591 /* Replace the current function body with the body
12592 wrapped in the try/finally TF. */
12593 seq = NULL;
12594 gimple_seq_add_stmt (&seq, new_bind);
12595 gimple_set_body (fndecl, seq);
12596 bind = new_bind;
12599 if ((flag_sanitize & SANITIZE_THREAD) != 0
12600 && !lookup_attribute ("no_sanitize_thread", DECL_ATTRIBUTES (fndecl)))
12602 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
12603 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
12604 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
12605 /* Replace the current function body with the body
12606 wrapped in the try/finally TF. */
12607 seq = NULL;
12608 gimple_seq_add_stmt (&seq, new_bind);
12609 gimple_set_body (fndecl, seq);
12612 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12613 cfun->curr_properties |= PROP_gimple_any;
12615 pop_cfun ();
12617 dump_function (TDI_generic, fndecl);
12620 /* Return a dummy expression of type TYPE in order to keep going after an
12621 error. */
12623 static tree
12624 dummy_object (tree type)
12626 tree t = build_int_cst (build_pointer_type (type), 0);
12627 return build2 (MEM_REF, type, t, t);
12630 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
12631 builtin function, but a very special sort of operator. */
12633 enum gimplify_status
12634 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
12635 gimple_seq *post_p ATTRIBUTE_UNUSED)
12637 tree promoted_type, have_va_type;
12638 tree valist = TREE_OPERAND (*expr_p, 0);
12639 tree type = TREE_TYPE (*expr_p);
12640 tree t, tag, aptag;
12641 location_t loc = EXPR_LOCATION (*expr_p);
12643 /* Verify that valist is of the proper type. */
12644 have_va_type = TREE_TYPE (valist);
12645 if (have_va_type == error_mark_node)
12646 return GS_ERROR;
12647 have_va_type = targetm.canonical_va_list_type (have_va_type);
12648 if (have_va_type == NULL_TREE
12649 && TREE_CODE (valist) == ADDR_EXPR)
12650 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
12651 have_va_type
12652 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
12653 gcc_assert (have_va_type != NULL_TREE);
12655 /* Generate a diagnostic for requesting data of a type that cannot
12656 be passed through `...' due to type promotion at the call site. */
12657 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
12658 != type)
12660 static bool gave_help;
12661 bool warned;
12662 /* Use the expansion point to handle cases such as passing bool (defined
12663 in a system header) through `...'. */
12664 source_location xloc
12665 = expansion_point_location_if_in_system_header (loc);
12667 /* Unfortunately, this is merely undefined, rather than a constraint
12668 violation, so we cannot make this an error. If this call is never
12669 executed, the program is still strictly conforming. */
12670 warned = warning_at (xloc, 0,
12671 "%qT is promoted to %qT when passed through %<...%>",
12672 type, promoted_type);
12673 if (!gave_help && warned)
12675 gave_help = true;
12676 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
12677 promoted_type, type);
12680 /* We can, however, treat "undefined" any way we please.
12681 Call abort to encourage the user to fix the program. */
12682 if (warned)
12683 inform (xloc, "if this code is reached, the program will abort");
12684 /* Before the abort, allow the evaluation of the va_list
12685 expression to exit or longjmp. */
12686 gimplify_and_add (valist, pre_p);
12687 t = build_call_expr_loc (loc,
12688 builtin_decl_implicit (BUILT_IN_TRAP), 0);
12689 gimplify_and_add (t, pre_p);
12691 /* This is dead code, but go ahead and finish so that the
12692 mode of the result comes out right. */
12693 *expr_p = dummy_object (type);
12694 return GS_ALL_DONE;
12697 tag = build_int_cst (build_pointer_type (type), 0);
12698 aptag = build_int_cst (TREE_TYPE (valist), 0);
12700 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
12701 valist, tag, aptag);
12703 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
12704 needs to be expanded. */
12705 cfun->curr_properties &= ~PROP_gimple_lva;
12707 return GS_OK;
12710 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
12712 DST/SRC are the destination and source respectively. You can pass
12713 ungimplified trees in DST or SRC, in which case they will be
12714 converted to a gimple operand if necessary.
12716 This function returns the newly created GIMPLE_ASSIGN tuple. */
12718 gimple *
12719 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
12721 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12722 gimplify_and_add (t, seq_p);
12723 ggc_free (t);
12724 return gimple_seq_last_stmt (*seq_p);
12727 inline hashval_t
12728 gimplify_hasher::hash (const elt_t *p)
12730 tree t = p->val;
12731 return iterative_hash_expr (t, 0);
12734 inline bool
12735 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
12737 tree t1 = p1->val;
12738 tree t2 = p2->val;
12739 enum tree_code code = TREE_CODE (t1);
12741 if (TREE_CODE (t2) != code
12742 || TREE_TYPE (t1) != TREE_TYPE (t2))
12743 return false;
12745 if (!operand_equal_p (t1, t2, 0))
12746 return false;
12748 /* Only allow them to compare equal if they also hash equal; otherwise
12749 results are nondeterminate, and we fail bootstrap comparison. */
12750 gcc_checking_assert (hash (p1) == hash (p2));
12752 return true;