pa64-hpux.h (FINI_SECTION_ASM_OP): Define to null string.
[official-gcc.git] / gcc / gimplify.c
blob1531582be115c92c17a9dfacf9d16b526eab469d
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"
63 enum gimplify_omp_var_data
65 GOVD_SEEN = 1,
66 GOVD_EXPLICIT = 2,
67 GOVD_SHARED = 4,
68 GOVD_PRIVATE = 8,
69 GOVD_FIRSTPRIVATE = 16,
70 GOVD_LASTPRIVATE = 32,
71 GOVD_REDUCTION = 64,
72 GOVD_LOCAL = 128,
73 GOVD_MAP = 256,
74 GOVD_DEBUG_PRIVATE = 512,
75 GOVD_PRIVATE_OUTER_REF = 1024,
76 GOVD_LINEAR = 2048,
77 GOVD_ALIGNED = 4096,
79 /* Flag for GOVD_MAP: don't copy back. */
80 GOVD_MAP_TO_ONLY = 8192,
82 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
83 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
85 GOVD_MAP_0LEN_ARRAY = 32768,
87 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
88 GOVD_MAP_ALWAYS_TO = 65536,
90 /* Flag for shared vars that are or might be stored to in the region. */
91 GOVD_WRITTEN = 131072,
93 /* Flag for GOVD_MAP, if it is a forced mapping. */
94 GOVD_MAP_FORCE = 262144,
96 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
97 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
98 | GOVD_LOCAL)
102 enum omp_region_type
104 ORT_WORKSHARE = 0x00,
105 ORT_SIMD = 0x01,
107 ORT_PARALLEL = 0x02,
108 ORT_COMBINED_PARALLEL = 0x03,
110 ORT_TASK = 0x04,
111 ORT_UNTIED_TASK = 0x05,
113 ORT_TEAMS = 0x08,
114 ORT_COMBINED_TEAMS = 0x09,
116 /* Data region. */
117 ORT_TARGET_DATA = 0x10,
119 /* Data region with offloading. */
120 ORT_TARGET = 0x20,
121 ORT_COMBINED_TARGET = 0x21,
123 /* OpenACC variants. */
124 ORT_ACC = 0x40, /* A generic OpenACC region. */
125 ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
126 ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
127 ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 0x80, /* Kernels construct. */
128 ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 0x80, /* Host data. */
130 /* Dummy OpenMP region, used to disable expansion of
131 DECL_VALUE_EXPRs in taskloop pre body. */
132 ORT_NONE = 0x100
135 /* Gimplify hashtable helper. */
137 struct gimplify_hasher : free_ptr_hash <elt_t>
139 static inline hashval_t hash (const elt_t *);
140 static inline bool equal (const elt_t *, const elt_t *);
143 struct gimplify_ctx
145 struct gimplify_ctx *prev_context;
147 vec<gbind *> bind_expr_stack;
148 tree temps;
149 gimple_seq conditional_cleanups;
150 tree exit_label;
151 tree return_temp;
153 vec<tree> case_labels;
154 /* The formal temporary table. Should this be persistent? */
155 hash_table<gimplify_hasher> *temp_htab;
157 int conditions;
158 unsigned into_ssa : 1;
159 unsigned allow_rhs_cond_expr : 1;
160 unsigned in_cleanup_point_expr : 1;
161 unsigned keep_stack : 1;
162 unsigned save_stack : 1;
163 unsigned in_switch_expr : 1;
166 struct gimplify_omp_ctx
168 struct gimplify_omp_ctx *outer_context;
169 splay_tree variables;
170 hash_set<tree> *privatized_types;
171 /* Iteration variables in an OMP_FOR. */
172 vec<tree> loop_iter_var;
173 location_t location;
174 enum omp_clause_default_kind default_kind;
175 enum omp_region_type region_type;
176 bool combined_loop;
177 bool distribute;
178 bool target_map_scalars_firstprivate;
179 bool target_map_pointers_as_0len_arrays;
180 bool target_firstprivatize_array_bases;
183 static struct gimplify_ctx *gimplify_ctxp;
184 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
186 /* Forward declaration. */
187 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
188 static hash_map<tree, tree> *oacc_declare_returns;
189 static enum gimplify_status gimplify_expr (tree *, gimple_seq *, gimple_seq *,
190 bool (*) (tree), fallback_t, bool);
192 /* Shorter alias name for the above function for use in gimplify.c
193 only. */
195 static inline void
196 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
198 gimple_seq_add_stmt_without_update (seq_p, gs);
201 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
202 NULL, a new sequence is allocated. This function is
203 similar to gimple_seq_add_seq, but does not scan the operands.
204 During gimplification, we need to manipulate statement sequences
205 before the def/use vectors have been constructed. */
207 static void
208 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
210 gimple_stmt_iterator si;
212 if (src == NULL)
213 return;
215 si = gsi_last (*dst_p);
216 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
220 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
221 and popping gimplify contexts. */
223 static struct gimplify_ctx *ctx_pool = NULL;
225 /* Return a gimplify context struct from the pool. */
227 static inline struct gimplify_ctx *
228 ctx_alloc (void)
230 struct gimplify_ctx * c = ctx_pool;
232 if (c)
233 ctx_pool = c->prev_context;
234 else
235 c = XNEW (struct gimplify_ctx);
237 memset (c, '\0', sizeof (*c));
238 return c;
241 /* Put gimplify context C back into the pool. */
243 static inline void
244 ctx_free (struct gimplify_ctx *c)
246 c->prev_context = ctx_pool;
247 ctx_pool = c;
250 /* Free allocated ctx stack memory. */
252 void
253 free_gimplify_stack (void)
255 struct gimplify_ctx *c;
257 while ((c = ctx_pool))
259 ctx_pool = c->prev_context;
260 free (c);
265 /* Set up a context for the gimplifier. */
267 void
268 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
270 struct gimplify_ctx *c = ctx_alloc ();
272 c->prev_context = gimplify_ctxp;
273 gimplify_ctxp = c;
274 gimplify_ctxp->into_ssa = in_ssa;
275 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
278 /* Tear down a context for the gimplifier. If BODY is non-null, then
279 put the temporaries into the outer BIND_EXPR. Otherwise, put them
280 in the local_decls.
282 BODY is not a sequence, but the first tuple in a sequence. */
284 void
285 pop_gimplify_context (gimple *body)
287 struct gimplify_ctx *c = gimplify_ctxp;
289 gcc_assert (c
290 && (!c->bind_expr_stack.exists ()
291 || c->bind_expr_stack.is_empty ()));
292 c->bind_expr_stack.release ();
293 gimplify_ctxp = c->prev_context;
295 if (body)
296 declare_vars (c->temps, body, false);
297 else
298 record_vars (c->temps);
300 delete c->temp_htab;
301 c->temp_htab = NULL;
302 ctx_free (c);
305 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
307 static void
308 gimple_push_bind_expr (gbind *bind_stmt)
310 gimplify_ctxp->bind_expr_stack.reserve (8);
311 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
314 /* Pop the first element off the stack of bindings. */
316 static void
317 gimple_pop_bind_expr (void)
319 gimplify_ctxp->bind_expr_stack.pop ();
322 /* Return the first element of the stack of bindings. */
324 gbind *
325 gimple_current_bind_expr (void)
327 return gimplify_ctxp->bind_expr_stack.last ();
330 /* Return the stack of bindings created during gimplification. */
332 vec<gbind *>
333 gimple_bind_expr_stack (void)
335 return gimplify_ctxp->bind_expr_stack;
338 /* Return true iff there is a COND_EXPR between us and the innermost
339 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
341 static bool
342 gimple_conditional_context (void)
344 return gimplify_ctxp->conditions > 0;
347 /* Note that we've entered a COND_EXPR. */
349 static void
350 gimple_push_condition (void)
352 #ifdef ENABLE_GIMPLE_CHECKING
353 if (gimplify_ctxp->conditions == 0)
354 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
355 #endif
356 ++(gimplify_ctxp->conditions);
359 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
360 now, add any conditional cleanups we've seen to the prequeue. */
362 static void
363 gimple_pop_condition (gimple_seq *pre_p)
365 int conds = --(gimplify_ctxp->conditions);
367 gcc_assert (conds >= 0);
368 if (conds == 0)
370 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
371 gimplify_ctxp->conditional_cleanups = NULL;
375 /* A stable comparison routine for use with splay trees and DECLs. */
377 static int
378 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
380 tree a = (tree) xa;
381 tree b = (tree) xb;
383 return DECL_UID (a) - DECL_UID (b);
386 /* Create a new omp construct that deals with variable remapping. */
388 static struct gimplify_omp_ctx *
389 new_omp_context (enum omp_region_type region_type)
391 struct gimplify_omp_ctx *c;
393 c = XCNEW (struct gimplify_omp_ctx);
394 c->outer_context = gimplify_omp_ctxp;
395 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
396 c->privatized_types = new hash_set<tree>;
397 c->location = input_location;
398 c->region_type = region_type;
399 if ((region_type & ORT_TASK) == 0)
400 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
401 else
402 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
404 return c;
407 /* Destroy an omp construct that deals with variable remapping. */
409 static void
410 delete_omp_context (struct gimplify_omp_ctx *c)
412 splay_tree_delete (c->variables);
413 delete c->privatized_types;
414 c->loop_iter_var.release ();
415 XDELETE (c);
418 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
419 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
421 /* Both gimplify the statement T and append it to *SEQ_P. This function
422 behaves exactly as gimplify_stmt, but you don't have to pass T as a
423 reference. */
425 void
426 gimplify_and_add (tree t, gimple_seq *seq_p)
428 gimplify_stmt (&t, seq_p);
431 /* Gimplify statement T into sequence *SEQ_P, and return the first
432 tuple in the sequence of generated tuples for this statement.
433 Return NULL if gimplifying T produced no tuples. */
435 static gimple *
436 gimplify_and_return_first (tree t, gimple_seq *seq_p)
438 gimple_stmt_iterator last = gsi_last (*seq_p);
440 gimplify_and_add (t, seq_p);
442 if (!gsi_end_p (last))
444 gsi_next (&last);
445 return gsi_stmt (last);
447 else
448 return gimple_seq_first_stmt (*seq_p);
451 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
452 LHS, or for a call argument. */
454 static bool
455 is_gimple_mem_rhs (tree t)
457 /* If we're dealing with a renamable type, either source or dest must be
458 a renamed variable. */
459 if (is_gimple_reg_type (TREE_TYPE (t)))
460 return is_gimple_val (t);
461 else
462 return is_gimple_val (t) || is_gimple_lvalue (t);
465 /* Return true if T is a CALL_EXPR or an expression that can be
466 assigned to a temporary. Note that this predicate should only be
467 used during gimplification. See the rationale for this in
468 gimplify_modify_expr. */
470 static bool
471 is_gimple_reg_rhs_or_call (tree t)
473 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
474 || TREE_CODE (t) == CALL_EXPR);
477 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
478 this predicate should only be used during gimplification. See the
479 rationale for this in gimplify_modify_expr. */
481 static bool
482 is_gimple_mem_rhs_or_call (tree t)
484 /* If we're dealing with a renamable type, either source or dest must be
485 a renamed variable. */
486 if (is_gimple_reg_type (TREE_TYPE (t)))
487 return is_gimple_val (t);
488 else
489 return (is_gimple_val (t) || is_gimple_lvalue (t)
490 || TREE_CODE (t) == CALL_EXPR);
493 /* Create a temporary with a name derived from VAL. Subroutine of
494 lookup_tmp_var; nobody else should call this function. */
496 static inline tree
497 create_tmp_from_val (tree val)
499 /* Drop all qualifiers and address-space information from the value type. */
500 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
501 tree var = create_tmp_var (type, get_name (val));
502 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
503 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
504 DECL_GIMPLE_REG_P (var) = 1;
505 return var;
508 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
509 an existing expression temporary. */
511 static tree
512 lookup_tmp_var (tree val, bool is_formal)
514 tree ret;
516 /* If not optimizing, never really reuse a temporary. local-alloc
517 won't allocate any variable that is used in more than one basic
518 block, which means it will go into memory, causing much extra
519 work in reload and final and poorer code generation, outweighing
520 the extra memory allocation here. */
521 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
522 ret = create_tmp_from_val (val);
523 else
525 elt_t elt, *elt_p;
526 elt_t **slot;
528 elt.val = val;
529 if (!gimplify_ctxp->temp_htab)
530 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
531 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
532 if (*slot == NULL)
534 elt_p = XNEW (elt_t);
535 elt_p->val = val;
536 elt_p->temp = ret = create_tmp_from_val (val);
537 *slot = elt_p;
539 else
541 elt_p = *slot;
542 ret = elt_p->temp;
546 return ret;
549 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
551 static tree
552 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
553 bool is_formal, bool allow_ssa)
555 tree t, mod;
557 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
558 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
559 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
560 fb_rvalue);
562 if (allow_ssa
563 && gimplify_ctxp->into_ssa
564 && is_gimple_reg_type (TREE_TYPE (val)))
566 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
567 if (! gimple_in_ssa_p (cfun))
569 const char *name = get_name (val);
570 if (name)
571 SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
574 else
575 t = lookup_tmp_var (val, is_formal);
577 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
579 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
581 /* gimplify_modify_expr might want to reduce this further. */
582 gimplify_and_add (mod, pre_p);
583 ggc_free (mod);
585 return t;
588 /* Return a formal temporary variable initialized with VAL. PRE_P is as
589 in gimplify_expr. Only use this function if:
591 1) The value of the unfactored expression represented by VAL will not
592 change between the initialization and use of the temporary, and
593 2) The temporary will not be otherwise modified.
595 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
596 and #2 means it is inappropriate for && temps.
598 For other cases, use get_initialized_tmp_var instead. */
600 tree
601 get_formal_tmp_var (tree val, gimple_seq *pre_p)
603 return internal_get_tmp_var (val, pre_p, NULL, true, true);
606 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
607 are as in gimplify_expr. */
609 tree
610 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
611 bool allow_ssa)
613 return internal_get_tmp_var (val, pre_p, post_p, false, allow_ssa);
616 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
617 generate debug info for them; otherwise don't. */
619 void
620 declare_vars (tree vars, gimple *gs, bool debug_info)
622 tree last = vars;
623 if (last)
625 tree temps, block;
627 gbind *scope = as_a <gbind *> (gs);
629 temps = nreverse (last);
631 block = gimple_bind_block (scope);
632 gcc_assert (!block || TREE_CODE (block) == BLOCK);
633 if (!block || !debug_info)
635 DECL_CHAIN (last) = gimple_bind_vars (scope);
636 gimple_bind_set_vars (scope, temps);
638 else
640 /* We need to attach the nodes both to the BIND_EXPR and to its
641 associated BLOCK for debugging purposes. The key point here
642 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
643 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
644 if (BLOCK_VARS (block))
645 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
646 else
648 gimple_bind_set_vars (scope,
649 chainon (gimple_bind_vars (scope), temps));
650 BLOCK_VARS (block) = temps;
656 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
657 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
658 no such upper bound can be obtained. */
660 static void
661 force_constant_size (tree var)
663 /* The only attempt we make is by querying the maximum size of objects
664 of the variable's type. */
666 HOST_WIDE_INT max_size;
668 gcc_assert (VAR_P (var));
670 max_size = max_int_size_in_bytes (TREE_TYPE (var));
672 gcc_assert (max_size >= 0);
674 DECL_SIZE_UNIT (var)
675 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
676 DECL_SIZE (var)
677 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
680 /* Push the temporary variable TMP into the current binding. */
682 void
683 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
685 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
687 /* Later processing assumes that the object size is constant, which might
688 not be true at this point. Force the use of a constant upper bound in
689 this case. */
690 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
691 force_constant_size (tmp);
693 DECL_CONTEXT (tmp) = fn->decl;
694 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
696 record_vars_into (tmp, fn->decl);
699 /* Push the temporary variable TMP into the current binding. */
701 void
702 gimple_add_tmp_var (tree tmp)
704 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
706 /* Later processing assumes that the object size is constant, which might
707 not be true at this point. Force the use of a constant upper bound in
708 this case. */
709 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
710 force_constant_size (tmp);
712 DECL_CONTEXT (tmp) = current_function_decl;
713 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
715 if (gimplify_ctxp)
717 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
718 gimplify_ctxp->temps = tmp;
720 /* Mark temporaries local within the nearest enclosing parallel. */
721 if (gimplify_omp_ctxp)
723 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
724 while (ctx
725 && (ctx->region_type == ORT_WORKSHARE
726 || ctx->region_type == ORT_SIMD
727 || ctx->region_type == ORT_ACC))
728 ctx = ctx->outer_context;
729 if (ctx)
730 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
733 else if (cfun)
734 record_vars (tmp);
735 else
737 gimple_seq body_seq;
739 /* This case is for nested functions. We need to expose the locals
740 they create. */
741 body_seq = gimple_body (current_function_decl);
742 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
748 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
749 nodes that are referenced more than once in GENERIC functions. This is
750 necessary because gimplification (translation into GIMPLE) is performed
751 by modifying tree nodes in-place, so gimplication of a shared node in a
752 first context could generate an invalid GIMPLE form in a second context.
754 This is achieved with a simple mark/copy/unmark algorithm that walks the
755 GENERIC representation top-down, marks nodes with TREE_VISITED the first
756 time it encounters them, duplicates them if they already have TREE_VISITED
757 set, and finally removes the TREE_VISITED marks it has set.
759 The algorithm works only at the function level, i.e. it generates a GENERIC
760 representation of a function with no nodes shared within the function when
761 passed a GENERIC function (except for nodes that are allowed to be shared).
763 At the global level, it is also necessary to unshare tree nodes that are
764 referenced in more than one function, for the same aforementioned reason.
765 This requires some cooperation from the front-end. There are 2 strategies:
767 1. Manual unsharing. The front-end needs to call unshare_expr on every
768 expression that might end up being shared across functions.
770 2. Deep unsharing. This is an extension of regular unsharing. Instead
771 of calling unshare_expr on expressions that might be shared across
772 functions, the front-end pre-marks them with TREE_VISITED. This will
773 ensure that they are unshared on the first reference within functions
774 when the regular unsharing algorithm runs. The counterpart is that
775 this algorithm must look deeper than for manual unsharing, which is
776 specified by LANG_HOOKS_DEEP_UNSHARING.
778 If there are only few specific cases of node sharing across functions, it is
779 probably easier for a front-end to unshare the expressions manually. On the
780 contrary, if the expressions generated at the global level are as widespread
781 as expressions generated within functions, deep unsharing is very likely the
782 way to go. */
784 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
785 These nodes model computations that must be done once. If we were to
786 unshare something like SAVE_EXPR(i++), the gimplification process would
787 create wrong code. However, if DATA is non-null, it must hold a pointer
788 set that is used to unshare the subtrees of these nodes. */
790 static tree
791 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
793 tree t = *tp;
794 enum tree_code code = TREE_CODE (t);
796 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
797 copy their subtrees if we can make sure to do it only once. */
798 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
800 if (data && !((hash_set<tree> *)data)->add (t))
802 else
803 *walk_subtrees = 0;
806 /* Stop at types, decls, constants like copy_tree_r. */
807 else if (TREE_CODE_CLASS (code) == tcc_type
808 || TREE_CODE_CLASS (code) == tcc_declaration
809 || TREE_CODE_CLASS (code) == tcc_constant
810 /* We can't do anything sensible with a BLOCK used as an
811 expression, but we also can't just die when we see it
812 because of non-expression uses. So we avert our eyes
813 and cross our fingers. Silly Java. */
814 || code == BLOCK)
815 *walk_subtrees = 0;
817 /* Cope with the statement expression extension. */
818 else if (code == STATEMENT_LIST)
821 /* Leave the bulk of the work to copy_tree_r itself. */
822 else
823 copy_tree_r (tp, walk_subtrees, NULL);
825 return NULL_TREE;
828 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
829 If *TP has been visited already, then *TP is deeply copied by calling
830 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
832 static tree
833 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
835 tree t = *tp;
836 enum tree_code code = TREE_CODE (t);
838 /* Skip types, decls, and constants. But we do want to look at their
839 types and the bounds of types. Mark them as visited so we properly
840 unmark their subtrees on the unmark pass. If we've already seen them,
841 don't look down further. */
842 if (TREE_CODE_CLASS (code) == tcc_type
843 || TREE_CODE_CLASS (code) == tcc_declaration
844 || TREE_CODE_CLASS (code) == tcc_constant)
846 if (TREE_VISITED (t))
847 *walk_subtrees = 0;
848 else
849 TREE_VISITED (t) = 1;
852 /* If this node has been visited already, unshare it and don't look
853 any deeper. */
854 else if (TREE_VISITED (t))
856 walk_tree (tp, mostly_copy_tree_r, data, NULL);
857 *walk_subtrees = 0;
860 /* Otherwise, mark the node as visited and keep looking. */
861 else
862 TREE_VISITED (t) = 1;
864 return NULL_TREE;
867 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
868 copy_if_shared_r callback unmodified. */
870 static inline void
871 copy_if_shared (tree *tp, void *data)
873 walk_tree (tp, copy_if_shared_r, data, NULL);
876 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
877 any nested functions. */
879 static void
880 unshare_body (tree fndecl)
882 struct cgraph_node *cgn = cgraph_node::get (fndecl);
883 /* If the language requires deep unsharing, we need a pointer set to make
884 sure we don't repeatedly unshare subtrees of unshareable nodes. */
885 hash_set<tree> *visited
886 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
888 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
889 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
890 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
892 delete visited;
894 if (cgn)
895 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
896 unshare_body (cgn->decl);
899 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
900 Subtrees are walked until the first unvisited node is encountered. */
902 static tree
903 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
905 tree t = *tp;
907 /* If this node has been visited, unmark it and keep looking. */
908 if (TREE_VISITED (t))
909 TREE_VISITED (t) = 0;
911 /* Otherwise, don't look any deeper. */
912 else
913 *walk_subtrees = 0;
915 return NULL_TREE;
918 /* Unmark the visited trees rooted at *TP. */
920 static inline void
921 unmark_visited (tree *tp)
923 walk_tree (tp, unmark_visited_r, NULL, NULL);
926 /* Likewise, but mark all trees as not visited. */
928 static void
929 unvisit_body (tree fndecl)
931 struct cgraph_node *cgn = cgraph_node::get (fndecl);
933 unmark_visited (&DECL_SAVED_TREE (fndecl));
934 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
935 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
937 if (cgn)
938 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
939 unvisit_body (cgn->decl);
942 /* Unconditionally make an unshared copy of EXPR. This is used when using
943 stored expressions which span multiple functions, such as BINFO_VTABLE,
944 as the normal unsharing process can't tell that they're shared. */
946 tree
947 unshare_expr (tree expr)
949 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
950 return expr;
953 /* Worker for unshare_expr_without_location. */
955 static tree
956 prune_expr_location (tree *tp, int *walk_subtrees, void *)
958 if (EXPR_P (*tp))
959 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
960 else
961 *walk_subtrees = 0;
962 return NULL_TREE;
965 /* Similar to unshare_expr but also prune all expression locations
966 from EXPR. */
968 tree
969 unshare_expr_without_location (tree expr)
971 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
972 if (EXPR_P (expr))
973 walk_tree (&expr, prune_expr_location, NULL, NULL);
974 return expr;
977 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
978 contain statements and have a value. Assign its value to a temporary
979 and give it void_type_node. Return the temporary, or NULL_TREE if
980 WRAPPER was already void. */
982 tree
983 voidify_wrapper_expr (tree wrapper, tree temp)
985 tree type = TREE_TYPE (wrapper);
986 if (type && !VOID_TYPE_P (type))
988 tree *p;
990 /* Set p to point to the body of the wrapper. Loop until we find
991 something that isn't a wrapper. */
992 for (p = &wrapper; p && *p; )
994 switch (TREE_CODE (*p))
996 case BIND_EXPR:
997 TREE_SIDE_EFFECTS (*p) = 1;
998 TREE_TYPE (*p) = void_type_node;
999 /* For a BIND_EXPR, the body is operand 1. */
1000 p = &BIND_EXPR_BODY (*p);
1001 break;
1003 case CLEANUP_POINT_EXPR:
1004 case TRY_FINALLY_EXPR:
1005 case TRY_CATCH_EXPR:
1006 TREE_SIDE_EFFECTS (*p) = 1;
1007 TREE_TYPE (*p) = void_type_node;
1008 p = &TREE_OPERAND (*p, 0);
1009 break;
1011 case STATEMENT_LIST:
1013 tree_stmt_iterator i = tsi_last (*p);
1014 TREE_SIDE_EFFECTS (*p) = 1;
1015 TREE_TYPE (*p) = void_type_node;
1016 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1018 break;
1020 case COMPOUND_EXPR:
1021 /* Advance to the last statement. Set all container types to
1022 void. */
1023 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1025 TREE_SIDE_EFFECTS (*p) = 1;
1026 TREE_TYPE (*p) = void_type_node;
1028 break;
1030 case TRANSACTION_EXPR:
1031 TREE_SIDE_EFFECTS (*p) = 1;
1032 TREE_TYPE (*p) = void_type_node;
1033 p = &TRANSACTION_EXPR_BODY (*p);
1034 break;
1036 default:
1037 /* Assume that any tree upon which voidify_wrapper_expr is
1038 directly called is a wrapper, and that its body is op0. */
1039 if (p == &wrapper)
1041 TREE_SIDE_EFFECTS (*p) = 1;
1042 TREE_TYPE (*p) = void_type_node;
1043 p = &TREE_OPERAND (*p, 0);
1044 break;
1046 goto out;
1050 out:
1051 if (p == NULL || IS_EMPTY_STMT (*p))
1052 temp = NULL_TREE;
1053 else if (temp)
1055 /* The wrapper is on the RHS of an assignment that we're pushing
1056 down. */
1057 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1058 || TREE_CODE (temp) == MODIFY_EXPR);
1059 TREE_OPERAND (temp, 1) = *p;
1060 *p = temp;
1062 else
1064 temp = create_tmp_var (type, "retval");
1065 *p = build2 (INIT_EXPR, type, temp, *p);
1068 return temp;
1071 return NULL_TREE;
1074 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1075 a temporary through which they communicate. */
1077 static void
1078 build_stack_save_restore (gcall **save, gcall **restore)
1080 tree tmp_var;
1082 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1083 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1084 gimple_call_set_lhs (*save, tmp_var);
1086 *restore
1087 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1088 1, tmp_var);
1091 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1093 static enum gimplify_status
1094 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1096 tree bind_expr = *expr_p;
1097 bool old_keep_stack = gimplify_ctxp->keep_stack;
1098 bool old_save_stack = gimplify_ctxp->save_stack;
1099 tree t;
1100 gbind *bind_stmt;
1101 gimple_seq body, cleanup;
1102 gcall *stack_save;
1103 location_t start_locus = 0, end_locus = 0;
1104 tree ret_clauses = NULL;
1106 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1108 /* Mark variables seen in this bind expr. */
1109 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1111 if (VAR_P (t))
1113 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1115 /* Mark variable as local. */
1116 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1117 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1118 || splay_tree_lookup (ctx->variables,
1119 (splay_tree_key) t) == NULL))
1121 if (ctx->region_type == ORT_SIMD
1122 && TREE_ADDRESSABLE (t)
1123 && !TREE_STATIC (t))
1124 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1125 else
1126 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1129 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1131 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1132 cfun->has_local_explicit_reg_vars = true;
1135 /* Preliminarily mark non-addressed complex variables as eligible
1136 for promotion to gimple registers. We'll transform their uses
1137 as we find them. */
1138 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1139 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1140 && !TREE_THIS_VOLATILE (t)
1141 && (VAR_P (t) && !DECL_HARD_REGISTER (t))
1142 && !needs_to_live_in_memory (t))
1143 DECL_GIMPLE_REG_P (t) = 1;
1146 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1147 BIND_EXPR_BLOCK (bind_expr));
1148 gimple_push_bind_expr (bind_stmt);
1150 gimplify_ctxp->keep_stack = false;
1151 gimplify_ctxp->save_stack = false;
1153 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1154 body = NULL;
1155 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1156 gimple_bind_set_body (bind_stmt, body);
1158 /* Source location wise, the cleanup code (stack_restore and clobbers)
1159 belongs to the end of the block, so propagate what we have. The
1160 stack_save operation belongs to the beginning of block, which we can
1161 infer from the bind_expr directly if the block has no explicit
1162 assignment. */
1163 if (BIND_EXPR_BLOCK (bind_expr))
1165 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1166 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1168 if (start_locus == 0)
1169 start_locus = EXPR_LOCATION (bind_expr);
1171 cleanup = NULL;
1172 stack_save = NULL;
1174 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1175 the stack space allocated to the VLAs. */
1176 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1178 gcall *stack_restore;
1180 /* Save stack on entry and restore it on exit. Add a try_finally
1181 block to achieve this. */
1182 build_stack_save_restore (&stack_save, &stack_restore);
1184 gimple_set_location (stack_save, start_locus);
1185 gimple_set_location (stack_restore, end_locus);
1187 gimplify_seq_add_stmt (&cleanup, stack_restore);
1190 /* Add clobbers for all variables that go out of scope. */
1191 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1193 if (VAR_P (t)
1194 && !is_global_var (t)
1195 && DECL_CONTEXT (t) == current_function_decl)
1197 if (!DECL_HARD_REGISTER (t)
1198 && !TREE_THIS_VOLATILE (t)
1199 && !DECL_HAS_VALUE_EXPR_P (t)
1200 /* Only care for variables that have to be in memory. Others
1201 will be rewritten into SSA names, hence moved to the
1202 top-level. */
1203 && !is_gimple_reg (t)
1204 && flag_stack_reuse != SR_NONE)
1206 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1207 gimple *clobber_stmt;
1208 TREE_THIS_VOLATILE (clobber) = 1;
1209 clobber_stmt = gimple_build_assign (t, clobber);
1210 gimple_set_location (clobber_stmt, end_locus);
1211 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1214 if (flag_openacc && oacc_declare_returns != NULL)
1216 tree *c = oacc_declare_returns->get (t);
1217 if (c != NULL)
1219 if (ret_clauses)
1220 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1222 ret_clauses = *c;
1224 oacc_declare_returns->remove (t);
1226 if (oacc_declare_returns->elements () == 0)
1228 delete oacc_declare_returns;
1229 oacc_declare_returns = NULL;
1236 if (ret_clauses)
1238 gomp_target *stmt;
1239 gimple_stmt_iterator si = gsi_start (cleanup);
1241 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1242 ret_clauses);
1243 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1246 if (cleanup)
1248 gtry *gs;
1249 gimple_seq new_body;
1251 new_body = NULL;
1252 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1253 GIMPLE_TRY_FINALLY);
1255 if (stack_save)
1256 gimplify_seq_add_stmt (&new_body, stack_save);
1257 gimplify_seq_add_stmt (&new_body, gs);
1258 gimple_bind_set_body (bind_stmt, new_body);
1261 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1262 if (!gimplify_ctxp->keep_stack)
1263 gimplify_ctxp->keep_stack = old_keep_stack;
1264 gimplify_ctxp->save_stack = old_save_stack;
1266 gimple_pop_bind_expr ();
1268 gimplify_seq_add_stmt (pre_p, bind_stmt);
1270 if (temp)
1272 *expr_p = temp;
1273 return GS_OK;
1276 *expr_p = NULL_TREE;
1277 return GS_ALL_DONE;
1280 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1281 GIMPLE value, it is assigned to a new temporary and the statement is
1282 re-written to return the temporary.
1284 PRE_P points to the sequence where side effects that must happen before
1285 STMT should be stored. */
1287 static enum gimplify_status
1288 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1290 greturn *ret;
1291 tree ret_expr = TREE_OPERAND (stmt, 0);
1292 tree result_decl, result;
1294 if (ret_expr == error_mark_node)
1295 return GS_ERROR;
1297 /* Implicit _Cilk_sync must be inserted right before any return statement
1298 if there is a _Cilk_spawn in the function. If the user has provided a
1299 _Cilk_sync, the optimizer should remove this duplicate one. */
1300 if (fn_contains_cilk_spawn_p (cfun))
1302 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1303 gimplify_and_add (impl_sync, pre_p);
1306 if (!ret_expr
1307 || TREE_CODE (ret_expr) == RESULT_DECL
1308 || ret_expr == error_mark_node)
1310 greturn *ret = gimple_build_return (ret_expr);
1311 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1312 gimplify_seq_add_stmt (pre_p, ret);
1313 return GS_ALL_DONE;
1316 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1317 result_decl = NULL_TREE;
1318 else
1320 result_decl = TREE_OPERAND (ret_expr, 0);
1322 /* See through a return by reference. */
1323 if (TREE_CODE (result_decl) == INDIRECT_REF)
1324 result_decl = TREE_OPERAND (result_decl, 0);
1326 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1327 || TREE_CODE (ret_expr) == INIT_EXPR)
1328 && TREE_CODE (result_decl) == RESULT_DECL);
1331 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1332 Recall that aggregate_value_p is FALSE for any aggregate type that is
1333 returned in registers. If we're returning values in registers, then
1334 we don't want to extend the lifetime of the RESULT_DECL, particularly
1335 across another call. In addition, for those aggregates for which
1336 hard_function_value generates a PARALLEL, we'll die during normal
1337 expansion of structure assignments; there's special code in expand_return
1338 to handle this case that does not exist in expand_expr. */
1339 if (!result_decl)
1340 result = NULL_TREE;
1341 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1343 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1345 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1346 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1347 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1348 should be effectively allocated by the caller, i.e. all calls to
1349 this function must be subject to the Return Slot Optimization. */
1350 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1351 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1353 result = result_decl;
1355 else if (gimplify_ctxp->return_temp)
1356 result = gimplify_ctxp->return_temp;
1357 else
1359 result = create_tmp_reg (TREE_TYPE (result_decl));
1361 /* ??? With complex control flow (usually involving abnormal edges),
1362 we can wind up warning about an uninitialized value for this. Due
1363 to how this variable is constructed and initialized, this is never
1364 true. Give up and never warn. */
1365 TREE_NO_WARNING (result) = 1;
1367 gimplify_ctxp->return_temp = result;
1370 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1371 Then gimplify the whole thing. */
1372 if (result != result_decl)
1373 TREE_OPERAND (ret_expr, 0) = result;
1375 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1377 ret = gimple_build_return (result);
1378 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1379 gimplify_seq_add_stmt (pre_p, ret);
1381 return GS_ALL_DONE;
1384 /* Gimplify a variable-length array DECL. */
1386 static void
1387 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1389 /* This is a variable-sized decl. Simplify its size and mark it
1390 for deferred expansion. */
1391 tree t, addr, ptr_type;
1393 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1394 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1396 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1397 if (DECL_HAS_VALUE_EXPR_P (decl))
1398 return;
1400 /* All occurrences of this decl in final gimplified code will be
1401 replaced by indirection. Setting DECL_VALUE_EXPR does two
1402 things: First, it lets the rest of the gimplifier know what
1403 replacement to use. Second, it lets the debug info know
1404 where to find the value. */
1405 ptr_type = build_pointer_type (TREE_TYPE (decl));
1406 addr = create_tmp_var (ptr_type, get_name (decl));
1407 DECL_IGNORED_P (addr) = 0;
1408 t = build_fold_indirect_ref (addr);
1409 TREE_THIS_NOTRAP (t) = 1;
1410 SET_DECL_VALUE_EXPR (decl, t);
1411 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1413 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1414 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1415 size_int (DECL_ALIGN (decl)));
1416 /* The call has been built for a variable-sized object. */
1417 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1418 t = fold_convert (ptr_type, t);
1419 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1421 gimplify_and_add (t, seq_p);
1424 /* A helper function to be called via walk_tree. Mark all labels under *TP
1425 as being forced. To be called for DECL_INITIAL of static variables. */
1427 static tree
1428 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1430 if (TYPE_P (*tp))
1431 *walk_subtrees = 0;
1432 if (TREE_CODE (*tp) == LABEL_DECL)
1434 FORCED_LABEL (*tp) = 1;
1435 cfun->has_forced_label_in_static = 1;
1438 return NULL_TREE;
1441 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1442 and initialization explicit. */
1444 static enum gimplify_status
1445 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1447 tree stmt = *stmt_p;
1448 tree decl = DECL_EXPR_DECL (stmt);
1450 *stmt_p = NULL_TREE;
1452 if (TREE_TYPE (decl) == error_mark_node)
1453 return GS_ERROR;
1455 if ((TREE_CODE (decl) == TYPE_DECL
1456 || VAR_P (decl))
1457 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1459 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1460 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
1461 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl)), seq_p);
1464 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1465 in case its size expressions contain problematic nodes like CALL_EXPR. */
1466 if (TREE_CODE (decl) == TYPE_DECL
1467 && DECL_ORIGINAL_TYPE (decl)
1468 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1470 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1471 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl)) == REFERENCE_TYPE)
1472 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl)), seq_p);
1475 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
1477 tree init = DECL_INITIAL (decl);
1479 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1480 || (!TREE_STATIC (decl)
1481 && flag_stack_check == GENERIC_STACK_CHECK
1482 && compare_tree_int (DECL_SIZE_UNIT (decl),
1483 STACK_CHECK_MAX_VAR_SIZE) > 0))
1484 gimplify_vla_decl (decl, seq_p);
1486 /* Some front ends do not explicitly declare all anonymous
1487 artificial variables. We compensate here by declaring the
1488 variables, though it would be better if the front ends would
1489 explicitly declare them. */
1490 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1491 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1492 gimple_add_tmp_var (decl);
1494 if (init && init != error_mark_node)
1496 if (!TREE_STATIC (decl))
1498 DECL_INITIAL (decl) = NULL_TREE;
1499 init = build2 (INIT_EXPR, void_type_node, decl, init);
1500 gimplify_and_add (init, seq_p);
1501 ggc_free (init);
1503 else
1504 /* We must still examine initializers for static variables
1505 as they may contain a label address. */
1506 walk_tree (&init, force_labels_r, NULL, NULL);
1510 return GS_ALL_DONE;
1513 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1514 and replacing the LOOP_EXPR with goto, but if the loop contains an
1515 EXIT_EXPR, we need to append a label for it to jump to. */
1517 static enum gimplify_status
1518 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1520 tree saved_label = gimplify_ctxp->exit_label;
1521 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1523 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1525 gimplify_ctxp->exit_label = NULL_TREE;
1527 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1529 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1531 if (gimplify_ctxp->exit_label)
1532 gimplify_seq_add_stmt (pre_p,
1533 gimple_build_label (gimplify_ctxp->exit_label));
1535 gimplify_ctxp->exit_label = saved_label;
1537 *expr_p = NULL;
1538 return GS_ALL_DONE;
1541 /* Gimplify a statement list onto a sequence. These may be created either
1542 by an enlightened front-end, or by shortcut_cond_expr. */
1544 static enum gimplify_status
1545 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1547 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1549 tree_stmt_iterator i = tsi_start (*expr_p);
1551 while (!tsi_end_p (i))
1553 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1554 tsi_delink (&i);
1557 if (temp)
1559 *expr_p = temp;
1560 return GS_OK;
1563 return GS_ALL_DONE;
1566 /* Callback for walk_gimple_seq. */
1568 static tree
1569 warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1570 struct walk_stmt_info *wi)
1572 gimple *stmt = gsi_stmt (*gsi_p);
1574 *handled_ops_p = true;
1575 switch (gimple_code (stmt))
1577 case GIMPLE_TRY:
1578 /* A compiler-generated cleanup or a user-written try block.
1579 If it's empty, don't dive into it--that would result in
1580 worse location info. */
1581 if (gimple_try_eval (stmt) == NULL)
1583 wi->info = stmt;
1584 return integer_zero_node;
1586 /* Fall through. */
1587 case GIMPLE_BIND:
1588 case GIMPLE_CATCH:
1589 case GIMPLE_EH_FILTER:
1590 case GIMPLE_TRANSACTION:
1591 /* Walk the sub-statements. */
1592 *handled_ops_p = false;
1593 break;
1594 default:
1595 /* Save the first "real" statement (not a decl/lexical scope/...). */
1596 wi->info = stmt;
1597 return integer_zero_node;
1599 return NULL_TREE;
1602 /* Possibly warn about unreachable statements between switch's controlling
1603 expression and the first case. SEQ is the body of a switch expression. */
1605 static void
1606 maybe_warn_switch_unreachable (gimple_seq seq)
1608 if (!warn_switch_unreachable
1609 /* This warning doesn't play well with Fortran when optimizations
1610 are on. */
1611 || lang_GNU_Fortran ()
1612 || seq == NULL)
1613 return;
1615 struct walk_stmt_info wi;
1616 memset (&wi, 0, sizeof (wi));
1617 walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
1618 gimple *stmt = (gimple *) wi.info;
1620 if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
1622 if (gimple_code (stmt) == GIMPLE_GOTO
1623 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
1624 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
1625 /* Don't warn for compiler-generated gotos. These occur
1626 in Duff's devices, for example. */;
1627 else
1628 warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
1629 "statement will never be executed");
1634 /* A label entry that pairs label and a location. */
1635 struct label_entry
1637 tree label;
1638 location_t loc;
1641 /* Find LABEL in vector of label entries VEC. */
1643 static struct label_entry *
1644 find_label_entry (const auto_vec<struct label_entry> *vec, tree label)
1646 unsigned int i;
1647 struct label_entry *l;
1649 FOR_EACH_VEC_ELT (*vec, i, l)
1650 if (l->label == label)
1651 return l;
1652 return NULL;
1655 /* Return true if LABEL, a LABEL_DECL, represents a case label
1656 in a vector of labels CASES. */
1658 static bool
1659 case_label_p (const vec<tree> *cases, tree label)
1661 unsigned int i;
1662 tree l;
1664 FOR_EACH_VEC_ELT (*cases, i, l)
1665 if (CASE_LABEL (l) == label)
1666 return true;
1667 return false;
1670 /* Find the last statement in a scope STMT. */
1672 static gimple *
1673 last_stmt_in_scope (gimple *stmt)
1675 if (!stmt)
1676 return NULL;
1678 switch (gimple_code (stmt))
1680 case GIMPLE_BIND:
1682 gbind *bind = as_a <gbind *> (stmt);
1683 stmt = gimple_seq_last_stmt (gimple_bind_body (bind));
1684 return last_stmt_in_scope (stmt);
1687 case GIMPLE_TRY:
1689 gtry *try_stmt = as_a <gtry *> (stmt);
1690 stmt = gimple_seq_last_stmt (gimple_try_eval (try_stmt));
1691 gimple *last_eval = last_stmt_in_scope (stmt);
1692 if (gimple_stmt_may_fallthru (last_eval)
1693 && (last_eval == NULL
1694 || !gimple_call_internal_p (last_eval, IFN_FALLTHROUGH))
1695 && gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
1697 stmt = gimple_seq_last_stmt (gimple_try_cleanup (try_stmt));
1698 return last_stmt_in_scope (stmt);
1700 else
1701 return last_eval;
1704 default:
1705 return stmt;
1709 /* Collect interesting labels in LABELS and return the statement preceding
1710 another case label, or a user-defined label. */
1712 static gimple *
1713 collect_fallthrough_labels (gimple_stmt_iterator *gsi_p,
1714 auto_vec <struct label_entry> *labels)
1716 gimple *prev = NULL;
1720 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_BIND
1721 || gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_TRY)
1723 /* Nested scope. Only look at the last statement of
1724 the innermost scope. */
1725 location_t bind_loc = gimple_location (gsi_stmt (*gsi_p));
1726 gimple *last = last_stmt_in_scope (gsi_stmt (*gsi_p));
1727 if (last)
1729 prev = last;
1730 /* It might be a label without a location. Use the
1731 location of the scope then. */
1732 if (!gimple_has_location (prev))
1733 gimple_set_location (prev, bind_loc);
1735 gsi_next (gsi_p);
1736 continue;
1739 /* Ifs are tricky. */
1740 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_COND)
1742 gcond *cond_stmt = as_a <gcond *> (gsi_stmt (*gsi_p));
1743 tree false_lab = gimple_cond_false_label (cond_stmt);
1744 location_t if_loc = gimple_location (cond_stmt);
1746 /* If we have e.g.
1747 if (i > 1) goto <D.2259>; else goto D;
1748 we can't do much with the else-branch. */
1749 if (!DECL_ARTIFICIAL (false_lab))
1750 break;
1752 /* Go on until the false label, then one step back. */
1753 for (; !gsi_end_p (*gsi_p); gsi_next (gsi_p))
1755 gimple *stmt = gsi_stmt (*gsi_p);
1756 if (gimple_code (stmt) == GIMPLE_LABEL
1757 && gimple_label_label (as_a <glabel *> (stmt)) == false_lab)
1758 break;
1761 /* Not found? Oops. */
1762 if (gsi_end_p (*gsi_p))
1763 break;
1765 struct label_entry l = { false_lab, if_loc };
1766 labels->safe_push (l);
1768 /* Go to the last statement of the then branch. */
1769 gsi_prev (gsi_p);
1771 /* if (i != 0) goto <D.1759>; else goto <D.1760>;
1772 <D.1759>:
1773 <stmt>;
1774 goto <D.1761>;
1775 <D.1760>:
1777 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_GOTO
1778 && !gimple_has_location (gsi_stmt (*gsi_p)))
1780 /* Look at the statement before, it might be
1781 attribute fallthrough, in which case don't warn. */
1782 gsi_prev (gsi_p);
1783 bool fallthru_before_dest
1784 = gimple_call_internal_p (gsi_stmt (*gsi_p), IFN_FALLTHROUGH);
1785 gsi_next (gsi_p);
1786 tree goto_dest = gimple_goto_dest (gsi_stmt (*gsi_p));
1787 if (!fallthru_before_dest)
1789 struct label_entry l = { goto_dest, if_loc };
1790 labels->safe_push (l);
1793 /* And move back. */
1794 gsi_next (gsi_p);
1797 /* Remember the last statement. Skip labels that are of no interest
1798 to us. */
1799 if (gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
1801 tree label = gimple_label_label (as_a <glabel *> (gsi_stmt (*gsi_p)));
1802 if (find_label_entry (labels, label))
1803 prev = gsi_stmt (*gsi_p);
1805 else
1806 prev = gsi_stmt (*gsi_p);
1807 gsi_next (gsi_p);
1809 while (!gsi_end_p (*gsi_p)
1810 /* Stop if we find a case or a user-defined label. */
1811 && (gimple_code (gsi_stmt (*gsi_p)) != GIMPLE_LABEL
1812 || !gimple_has_location (gsi_stmt (*gsi_p))));
1814 return prev;
1817 /* Return true if the switch fallthough warning should occur. LABEL is
1818 the label statement that we're falling through to. */
1820 static bool
1821 should_warn_for_implicit_fallthrough (gimple_stmt_iterator *gsi_p, tree label)
1823 gimple_stmt_iterator gsi = *gsi_p;
1825 /* Don't warn if the label is marked with a "falls through" comment. */
1826 if (FALLTHROUGH_LABEL_P (label))
1827 return false;
1829 /* Don't warn for a non-case label followed by a statement:
1830 case 0:
1831 foo ();
1832 label:
1833 bar ();
1834 as these are likely intentional. */
1835 if (!case_label_p (&gimplify_ctxp->case_labels, label))
1837 gsi_next (&gsi);
1838 if (gsi_end_p (gsi) || gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
1839 return false;
1842 /* Don't warn for terminated branches, i.e. when the subsequent case labels
1843 immediately breaks. */
1844 gsi = *gsi_p;
1846 /* Skip all immediately following labels. */
1847 while (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1848 gsi_next (&gsi);
1850 /* { ... something; default:; } */
1851 if (gsi_end_p (gsi)
1852 /* { ... something; default: break; } or
1853 { ... something; default: goto L; } */
1854 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
1855 /* { ... something; default: return; } */
1856 || gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
1857 return false;
1859 return true;
1862 /* Callback for walk_gimple_seq. */
1864 static tree
1865 warn_implicit_fallthrough_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1866 struct walk_stmt_info *)
1868 gimple *stmt = gsi_stmt (*gsi_p);
1870 *handled_ops_p = true;
1871 switch (gimple_code (stmt))
1873 case GIMPLE_TRY:
1874 case GIMPLE_BIND:
1875 case GIMPLE_CATCH:
1876 case GIMPLE_EH_FILTER:
1877 case GIMPLE_TRANSACTION:
1878 /* Walk the sub-statements. */
1879 *handled_ops_p = false;
1880 break;
1882 /* Find a sequence of form:
1884 GIMPLE_LABEL
1885 [...]
1886 <may fallthru stmt>
1887 GIMPLE_LABEL
1889 and possibly warn. */
1890 case GIMPLE_LABEL:
1892 /* Found a label. Skip all immediately following labels. */
1893 while (!gsi_end_p (*gsi_p)
1894 && gimple_code (gsi_stmt (*gsi_p)) == GIMPLE_LABEL)
1895 gsi_next (gsi_p);
1897 /* There might be no more statements. */
1898 if (gsi_end_p (*gsi_p))
1899 return integer_zero_node;
1901 /* Vector of labels that fall through. */
1902 auto_vec <struct label_entry> labels;
1903 gimple *prev = collect_fallthrough_labels (gsi_p, &labels);
1905 /* There might be no more statements. */
1906 if (gsi_end_p (*gsi_p))
1907 return integer_zero_node;
1909 gimple *next = gsi_stmt (*gsi_p);
1910 tree label;
1911 /* If what follows is a label, then we may have a fallthrough. */
1912 if (gimple_code (next) == GIMPLE_LABEL
1913 && gimple_has_location (next)
1914 && (label = gimple_label_label (as_a <glabel *> (next)))
1915 && prev != NULL)
1917 struct label_entry *l;
1918 bool warned_p = false;
1919 if (!should_warn_for_implicit_fallthrough (gsi_p, label))
1920 /* Quiet. */;
1921 else if (gimple_code (prev) == GIMPLE_LABEL
1922 && (label = gimple_label_label (as_a <glabel *> (prev)))
1923 && (l = find_label_entry (&labels, label)))
1924 warned_p = warning_at (l->loc, OPT_Wimplicit_fallthrough_,
1925 "this statement may fall through");
1926 else if (!gimple_call_internal_p (prev, IFN_FALLTHROUGH)
1927 /* Try to be clever and don't warn when the statement
1928 can't actually fall through. */
1929 && gimple_stmt_may_fallthru (prev)
1930 && gimple_has_location (prev))
1931 warned_p = warning_at (gimple_location (prev),
1932 OPT_Wimplicit_fallthrough_,
1933 "this statement may fall through");
1934 if (warned_p)
1935 inform (gimple_location (next), "here");
1937 /* Mark this label as processed so as to prevent multiple
1938 warnings in nested switches. */
1939 FALLTHROUGH_LABEL_P (label) = true;
1941 /* So that next warn_implicit_fallthrough_r will start looking for
1942 a new sequence starting with this label. */
1943 gsi_prev (gsi_p);
1946 break;
1947 default:
1948 break;
1950 return NULL_TREE;
1953 /* Warn when a switch case falls through. */
1955 static void
1956 maybe_warn_implicit_fallthrough (gimple_seq seq)
1958 if (!warn_implicit_fallthrough)
1959 return;
1961 /* This warning is meant for C/C++/ObjC/ObjC++ only. */
1962 if (!(lang_GNU_C ()
1963 || lang_GNU_CXX ()
1964 || lang_GNU_OBJC ()))
1965 return;
1967 struct walk_stmt_info wi;
1968 memset (&wi, 0, sizeof (wi));
1969 walk_gimple_seq (seq, warn_implicit_fallthrough_r, NULL, &wi);
1972 /* Callback for walk_gimple_seq. */
1974 static tree
1975 expand_FALLTHROUGH_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
1976 struct walk_stmt_info *)
1978 gimple *stmt = gsi_stmt (*gsi_p);
1980 *handled_ops_p = true;
1981 switch (gimple_code (stmt))
1983 case GIMPLE_TRY:
1984 case GIMPLE_BIND:
1985 case GIMPLE_CATCH:
1986 case GIMPLE_EH_FILTER:
1987 case GIMPLE_TRANSACTION:
1988 /* Walk the sub-statements. */
1989 *handled_ops_p = false;
1990 break;
1991 case GIMPLE_CALL:
1992 if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH))
1994 gsi_remove (gsi_p, true);
1995 if (gsi_end_p (*gsi_p))
1996 return integer_zero_node;
1998 bool found = false;
1999 location_t loc = gimple_location (stmt);
2001 gimple_stmt_iterator gsi2 = *gsi_p;
2002 stmt = gsi_stmt (gsi2);
2003 if (gimple_code (stmt) == GIMPLE_GOTO && !gimple_has_location (stmt))
2005 /* Go on until the artificial label. */
2006 tree goto_dest = gimple_goto_dest (stmt);
2007 for (; !gsi_end_p (gsi2); gsi_next (&gsi2))
2009 if (gimple_code (gsi_stmt (gsi2)) == GIMPLE_LABEL
2010 && gimple_label_label (as_a <glabel *> (gsi_stmt (gsi2)))
2011 == goto_dest)
2012 break;
2015 /* Not found? Stop. */
2016 if (gsi_end_p (gsi2))
2017 break;
2019 /* Look one past it. */
2020 gsi_next (&gsi2);
2023 /* We're looking for a case label or default label here. */
2024 while (!gsi_end_p (gsi2))
2026 stmt = gsi_stmt (gsi2);
2027 if (gimple_code (stmt) == GIMPLE_LABEL)
2029 tree label = gimple_label_label (as_a <glabel *> (stmt));
2030 if (gimple_has_location (stmt) && DECL_ARTIFICIAL (label))
2032 found = true;
2033 break;
2036 else
2037 /* Something other than a label. That's not expected. */
2038 break;
2039 gsi_next (&gsi2);
2041 if (!found)
2042 warning_at (loc, 0, "attribute %<fallthrough%> not preceding "
2043 "a case label or default label");
2045 break;
2046 default:
2047 break;
2049 return NULL_TREE;
2052 /* Expand all FALLTHROUGH () calls in SEQ. */
2054 static void
2055 expand_FALLTHROUGH (gimple_seq *seq_p)
2057 struct walk_stmt_info wi;
2058 memset (&wi, 0, sizeof (wi));
2059 walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi);
2063 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
2064 branch to. */
2066 static enum gimplify_status
2067 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
2069 tree switch_expr = *expr_p;
2070 gimple_seq switch_body_seq = NULL;
2071 enum gimplify_status ret;
2072 tree index_type = TREE_TYPE (switch_expr);
2073 if (index_type == NULL_TREE)
2074 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
2076 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
2077 fb_rvalue);
2078 if (ret == GS_ERROR || ret == GS_UNHANDLED)
2079 return ret;
2081 if (SWITCH_BODY (switch_expr))
2083 vec<tree> labels;
2084 vec<tree> saved_labels;
2085 tree default_case = NULL_TREE;
2086 gswitch *switch_stmt;
2088 /* If someone can be bothered to fill in the labels, they can
2089 be bothered to null out the body too. */
2090 gcc_assert (!SWITCH_LABELS (switch_expr));
2092 /* Save old labels, get new ones from body, then restore the old
2093 labels. Save all the things from the switch body to append after. */
2094 saved_labels = gimplify_ctxp->case_labels;
2095 gimplify_ctxp->case_labels.create (8);
2096 bool old_in_switch_expr = gimplify_ctxp->in_switch_expr;
2097 gimplify_ctxp->in_switch_expr = true;
2099 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
2101 gimplify_ctxp->in_switch_expr = old_in_switch_expr;
2102 maybe_warn_switch_unreachable (switch_body_seq);
2103 maybe_warn_implicit_fallthrough (switch_body_seq);
2104 /* Only do this for the outermost GIMPLE_SWITCH. */
2105 if (!gimplify_ctxp->in_switch_expr)
2106 expand_FALLTHROUGH (&switch_body_seq);
2108 labels = gimplify_ctxp->case_labels;
2109 gimplify_ctxp->case_labels = saved_labels;
2111 preprocess_case_label_vec_for_gimple (labels, index_type,
2112 &default_case);
2114 if (!default_case)
2116 glabel *new_default;
2118 default_case
2119 = build_case_label (NULL_TREE, NULL_TREE,
2120 create_artificial_label (UNKNOWN_LOCATION));
2121 new_default = gimple_build_label (CASE_LABEL (default_case));
2122 gimplify_seq_add_stmt (&switch_body_seq, new_default);
2125 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
2126 default_case, labels);
2127 gimplify_seq_add_stmt (pre_p, switch_stmt);
2128 gimplify_seq_add_seq (pre_p, switch_body_seq);
2129 labels.release ();
2131 else
2132 gcc_assert (SWITCH_LABELS (switch_expr));
2134 return GS_ALL_DONE;
2137 /* Gimplify the LABEL_EXPR pointed to by EXPR_P. */
2139 static enum gimplify_status
2140 gimplify_label_expr (tree *expr_p, gimple_seq *pre_p)
2142 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
2143 == current_function_decl);
2145 glabel *label_stmt = gimple_build_label (LABEL_EXPR_LABEL (*expr_p));
2146 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2147 gimplify_seq_add_stmt (pre_p, label_stmt);
2149 return GS_ALL_DONE;
2152 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
2154 static enum gimplify_status
2155 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
2157 struct gimplify_ctx *ctxp;
2158 glabel *label_stmt;
2160 /* Invalid programs can play Duff's Device type games with, for example,
2161 #pragma omp parallel. At least in the C front end, we don't
2162 detect such invalid branches until after gimplification, in the
2163 diagnose_omp_blocks pass. */
2164 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
2165 if (ctxp->case_labels.exists ())
2166 break;
2168 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
2169 gimple_set_location (label_stmt, EXPR_LOCATION (*expr_p));
2170 ctxp->case_labels.safe_push (*expr_p);
2171 gimplify_seq_add_stmt (pre_p, label_stmt);
2173 return GS_ALL_DONE;
2176 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
2177 if necessary. */
2179 tree
2180 build_and_jump (tree *label_p)
2182 if (label_p == NULL)
2183 /* If there's nowhere to jump, just fall through. */
2184 return NULL_TREE;
2186 if (*label_p == NULL_TREE)
2188 tree label = create_artificial_label (UNKNOWN_LOCATION);
2189 *label_p = label;
2192 return build1 (GOTO_EXPR, void_type_node, *label_p);
2195 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
2196 This also involves building a label to jump to and communicating it to
2197 gimplify_loop_expr through gimplify_ctxp->exit_label. */
2199 static enum gimplify_status
2200 gimplify_exit_expr (tree *expr_p)
2202 tree cond = TREE_OPERAND (*expr_p, 0);
2203 tree expr;
2205 expr = build_and_jump (&gimplify_ctxp->exit_label);
2206 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
2207 *expr_p = expr;
2209 return GS_OK;
2212 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
2213 different from its canonical type, wrap the whole thing inside a
2214 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
2215 type.
2217 The canonical type of a COMPONENT_REF is the type of the field being
2218 referenced--unless the field is a bit-field which can be read directly
2219 in a smaller mode, in which case the canonical type is the
2220 sign-appropriate type corresponding to that mode. */
2222 static void
2223 canonicalize_component_ref (tree *expr_p)
2225 tree expr = *expr_p;
2226 tree type;
2228 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
2230 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
2231 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
2232 else
2233 type = TREE_TYPE (TREE_OPERAND (expr, 1));
2235 /* One could argue that all the stuff below is not necessary for
2236 the non-bitfield case and declare it a FE error if type
2237 adjustment would be needed. */
2238 if (TREE_TYPE (expr) != type)
2240 #ifdef ENABLE_TYPES_CHECKING
2241 tree old_type = TREE_TYPE (expr);
2242 #endif
2243 int type_quals;
2245 /* We need to preserve qualifiers and propagate them from
2246 operand 0. */
2247 type_quals = TYPE_QUALS (type)
2248 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
2249 if (TYPE_QUALS (type) != type_quals)
2250 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
2252 /* Set the type of the COMPONENT_REF to the underlying type. */
2253 TREE_TYPE (expr) = type;
2255 #ifdef ENABLE_TYPES_CHECKING
2256 /* It is now a FE error, if the conversion from the canonical
2257 type to the original expression type is not useless. */
2258 gcc_assert (useless_type_conversion_p (old_type, type));
2259 #endif
2263 /* If a NOP conversion is changing a pointer to array of foo to a pointer
2264 to foo, embed that change in the ADDR_EXPR by converting
2265 T array[U];
2266 (T *)&array
2268 &array[L]
2269 where L is the lower bound. For simplicity, only do this for constant
2270 lower bound.
2271 The constraint is that the type of &array[L] is trivially convertible
2272 to T *. */
2274 static void
2275 canonicalize_addr_expr (tree *expr_p)
2277 tree expr = *expr_p;
2278 tree addr_expr = TREE_OPERAND (expr, 0);
2279 tree datype, ddatype, pddatype;
2281 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
2282 if (!POINTER_TYPE_P (TREE_TYPE (expr))
2283 || TREE_CODE (addr_expr) != ADDR_EXPR)
2284 return;
2286 /* The addr_expr type should be a pointer to an array. */
2287 datype = TREE_TYPE (TREE_TYPE (addr_expr));
2288 if (TREE_CODE (datype) != ARRAY_TYPE)
2289 return;
2291 /* The pointer to element type shall be trivially convertible to
2292 the expression pointer type. */
2293 ddatype = TREE_TYPE (datype);
2294 pddatype = build_pointer_type (ddatype);
2295 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
2296 pddatype))
2297 return;
2299 /* The lower bound and element sizes must be constant. */
2300 if (!TYPE_SIZE_UNIT (ddatype)
2301 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2302 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2303 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2304 return;
2306 /* All checks succeeded. Build a new node to merge the cast. */
2307 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2308 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2309 NULL_TREE, NULL_TREE);
2310 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2312 /* We can have stripped a required restrict qualifier above. */
2313 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2314 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2317 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2318 underneath as appropriate. */
2320 static enum gimplify_status
2321 gimplify_conversion (tree *expr_p)
2323 location_t loc = EXPR_LOCATION (*expr_p);
2324 gcc_assert (CONVERT_EXPR_P (*expr_p));
2326 /* Then strip away all but the outermost conversion. */
2327 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2329 /* And remove the outermost conversion if it's useless. */
2330 if (tree_ssa_useless_type_conversion (*expr_p))
2331 *expr_p = TREE_OPERAND (*expr_p, 0);
2333 /* If we still have a conversion at the toplevel,
2334 then canonicalize some constructs. */
2335 if (CONVERT_EXPR_P (*expr_p))
2337 tree sub = TREE_OPERAND (*expr_p, 0);
2339 /* If a NOP conversion is changing the type of a COMPONENT_REF
2340 expression, then canonicalize its type now in order to expose more
2341 redundant conversions. */
2342 if (TREE_CODE (sub) == COMPONENT_REF)
2343 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2345 /* If a NOP conversion is changing a pointer to array of foo
2346 to a pointer to foo, embed that change in the ADDR_EXPR. */
2347 else if (TREE_CODE (sub) == ADDR_EXPR)
2348 canonicalize_addr_expr (expr_p);
2351 /* If we have a conversion to a non-register type force the
2352 use of a VIEW_CONVERT_EXPR instead. */
2353 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2354 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2355 TREE_OPERAND (*expr_p, 0));
2357 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
2358 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
2359 TREE_SET_CODE (*expr_p, NOP_EXPR);
2361 return GS_OK;
2364 /* Nonlocal VLAs seen in the current function. */
2365 static hash_set<tree> *nonlocal_vlas;
2367 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
2368 static tree nonlocal_vla_vars;
2370 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2371 DECL_VALUE_EXPR, and it's worth re-examining things. */
2373 static enum gimplify_status
2374 gimplify_var_or_parm_decl (tree *expr_p)
2376 tree decl = *expr_p;
2378 /* ??? If this is a local variable, and it has not been seen in any
2379 outer BIND_EXPR, then it's probably the result of a duplicate
2380 declaration, for which we've already issued an error. It would
2381 be really nice if the front end wouldn't leak these at all.
2382 Currently the only known culprit is C++ destructors, as seen
2383 in g++.old-deja/g++.jason/binding.C. */
2384 if (VAR_P (decl)
2385 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2386 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2387 && decl_function_context (decl) == current_function_decl)
2389 gcc_assert (seen_error ());
2390 return GS_ERROR;
2393 /* When within an OMP context, notice uses of variables. */
2394 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2395 return GS_ALL_DONE;
2397 /* If the decl is an alias for another expression, substitute it now. */
2398 if (DECL_HAS_VALUE_EXPR_P (decl))
2400 tree value_expr = DECL_VALUE_EXPR (decl);
2402 /* For referenced nonlocal VLAs add a decl for debugging purposes
2403 to the current function. */
2404 if (VAR_P (decl)
2405 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2406 && nonlocal_vlas != NULL
2407 && TREE_CODE (value_expr) == INDIRECT_REF
2408 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2409 && decl_function_context (decl) != current_function_decl)
2411 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2412 while (ctx
2413 && (ctx->region_type == ORT_WORKSHARE
2414 || ctx->region_type == ORT_SIMD
2415 || ctx->region_type == ORT_ACC))
2416 ctx = ctx->outer_context;
2417 if (!ctx && !nonlocal_vlas->add (decl))
2419 tree copy = copy_node (decl);
2421 lang_hooks.dup_lang_specific_decl (copy);
2422 SET_DECL_RTL (copy, 0);
2423 TREE_USED (copy) = 1;
2424 DECL_CHAIN (copy) = nonlocal_vla_vars;
2425 nonlocal_vla_vars = copy;
2426 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2427 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2431 *expr_p = unshare_expr (value_expr);
2432 return GS_OK;
2435 return GS_ALL_DONE;
2438 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
2440 static void
2441 recalculate_side_effects (tree t)
2443 enum tree_code code = TREE_CODE (t);
2444 int len = TREE_OPERAND_LENGTH (t);
2445 int i;
2447 switch (TREE_CODE_CLASS (code))
2449 case tcc_expression:
2450 switch (code)
2452 case INIT_EXPR:
2453 case MODIFY_EXPR:
2454 case VA_ARG_EXPR:
2455 case PREDECREMENT_EXPR:
2456 case PREINCREMENT_EXPR:
2457 case POSTDECREMENT_EXPR:
2458 case POSTINCREMENT_EXPR:
2459 /* All of these have side-effects, no matter what their
2460 operands are. */
2461 return;
2463 default:
2464 break;
2466 /* Fall through. */
2468 case tcc_comparison: /* a comparison expression */
2469 case tcc_unary: /* a unary arithmetic expression */
2470 case tcc_binary: /* a binary arithmetic expression */
2471 case tcc_reference: /* a reference */
2472 case tcc_vl_exp: /* a function call */
2473 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2474 for (i = 0; i < len; ++i)
2476 tree op = TREE_OPERAND (t, i);
2477 if (op && TREE_SIDE_EFFECTS (op))
2478 TREE_SIDE_EFFECTS (t) = 1;
2480 break;
2482 case tcc_constant:
2483 /* No side-effects. */
2484 return;
2486 default:
2487 gcc_unreachable ();
2491 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2492 node *EXPR_P.
2494 compound_lval
2495 : min_lval '[' val ']'
2496 | min_lval '.' ID
2497 | compound_lval '[' val ']'
2498 | compound_lval '.' ID
2500 This is not part of the original SIMPLE definition, which separates
2501 array and member references, but it seems reasonable to handle them
2502 together. Also, this way we don't run into problems with union
2503 aliasing; gcc requires that for accesses through a union to alias, the
2504 union reference must be explicit, which was not always the case when we
2505 were splitting up array and member refs.
2507 PRE_P points to the sequence where side effects that must happen before
2508 *EXPR_P should be stored.
2510 POST_P points to the sequence where side effects that must happen after
2511 *EXPR_P should be stored. */
2513 static enum gimplify_status
2514 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2515 fallback_t fallback)
2517 tree *p;
2518 enum gimplify_status ret = GS_ALL_DONE, tret;
2519 int i;
2520 location_t loc = EXPR_LOCATION (*expr_p);
2521 tree expr = *expr_p;
2523 /* Create a stack of the subexpressions so later we can walk them in
2524 order from inner to outer. */
2525 auto_vec<tree, 10> expr_stack;
2527 /* We can handle anything that get_inner_reference can deal with. */
2528 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2530 restart:
2531 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2532 if (TREE_CODE (*p) == INDIRECT_REF)
2533 *p = fold_indirect_ref_loc (loc, *p);
2535 if (handled_component_p (*p))
2537 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2538 additional COMPONENT_REFs. */
2539 else if ((VAR_P (*p) || TREE_CODE (*p) == PARM_DECL)
2540 && gimplify_var_or_parm_decl (p) == GS_OK)
2541 goto restart;
2542 else
2543 break;
2545 expr_stack.safe_push (*p);
2548 gcc_assert (expr_stack.length ());
2550 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2551 walked through and P points to the innermost expression.
2553 Java requires that we elaborated nodes in source order. That
2554 means we must gimplify the inner expression followed by each of
2555 the indices, in order. But we can't gimplify the inner
2556 expression until we deal with any variable bounds, sizes, or
2557 positions in order to deal with PLACEHOLDER_EXPRs.
2559 So we do this in three steps. First we deal with the annotations
2560 for any variables in the components, then we gimplify the base,
2561 then we gimplify any indices, from left to right. */
2562 for (i = expr_stack.length () - 1; i >= 0; i--)
2564 tree t = expr_stack[i];
2566 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2568 /* Gimplify the low bound and element type size and put them into
2569 the ARRAY_REF. If these values are set, they have already been
2570 gimplified. */
2571 if (TREE_OPERAND (t, 2) == NULL_TREE)
2573 tree low = unshare_expr (array_ref_low_bound (t));
2574 if (!is_gimple_min_invariant (low))
2576 TREE_OPERAND (t, 2) = low;
2577 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2578 post_p, is_gimple_reg,
2579 fb_rvalue);
2580 ret = MIN (ret, tret);
2583 else
2585 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2586 is_gimple_reg, fb_rvalue);
2587 ret = MIN (ret, tret);
2590 if (TREE_OPERAND (t, 3) == NULL_TREE)
2592 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2593 tree elmt_size = unshare_expr (array_ref_element_size (t));
2594 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2596 /* Divide the element size by the alignment of the element
2597 type (above). */
2598 elmt_size
2599 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2601 if (!is_gimple_min_invariant (elmt_size))
2603 TREE_OPERAND (t, 3) = elmt_size;
2604 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2605 post_p, is_gimple_reg,
2606 fb_rvalue);
2607 ret = MIN (ret, tret);
2610 else
2612 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2613 is_gimple_reg, fb_rvalue);
2614 ret = MIN (ret, tret);
2617 else if (TREE_CODE (t) == COMPONENT_REF)
2619 /* Set the field offset into T and gimplify it. */
2620 if (TREE_OPERAND (t, 2) == NULL_TREE)
2622 tree offset = unshare_expr (component_ref_field_offset (t));
2623 tree field = TREE_OPERAND (t, 1);
2624 tree factor
2625 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2627 /* Divide the offset by its alignment. */
2628 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2630 if (!is_gimple_min_invariant (offset))
2632 TREE_OPERAND (t, 2) = offset;
2633 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2634 post_p, is_gimple_reg,
2635 fb_rvalue);
2636 ret = MIN (ret, tret);
2639 else
2641 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2642 is_gimple_reg, fb_rvalue);
2643 ret = MIN (ret, tret);
2648 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2649 so as to match the min_lval predicate. Failure to do so may result
2650 in the creation of large aggregate temporaries. */
2651 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2652 fallback | fb_lvalue);
2653 ret = MIN (ret, tret);
2655 /* And finally, the indices and operands of ARRAY_REF. During this
2656 loop we also remove any useless conversions. */
2657 for (; expr_stack.length () > 0; )
2659 tree t = expr_stack.pop ();
2661 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2663 /* Gimplify the dimension. */
2664 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2666 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2667 is_gimple_val, fb_rvalue);
2668 ret = MIN (ret, tret);
2672 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2674 /* The innermost expression P may have originally had
2675 TREE_SIDE_EFFECTS set which would have caused all the outer
2676 expressions in *EXPR_P leading to P to also have had
2677 TREE_SIDE_EFFECTS set. */
2678 recalculate_side_effects (t);
2681 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2682 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2684 canonicalize_component_ref (expr_p);
2687 expr_stack.release ();
2689 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2691 return ret;
2694 /* Gimplify the self modifying expression pointed to by EXPR_P
2695 (++, --, +=, -=).
2697 PRE_P points to the list where side effects that must happen before
2698 *EXPR_P should be stored.
2700 POST_P points to the list where side effects that must happen after
2701 *EXPR_P should be stored.
2703 WANT_VALUE is nonzero iff we want to use the value of this expression
2704 in another expression.
2706 ARITH_TYPE is the type the computation should be performed in. */
2708 enum gimplify_status
2709 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2710 bool want_value, tree arith_type)
2712 enum tree_code code;
2713 tree lhs, lvalue, rhs, t1;
2714 gimple_seq post = NULL, *orig_post_p = post_p;
2715 bool postfix;
2716 enum tree_code arith_code;
2717 enum gimplify_status ret;
2718 location_t loc = EXPR_LOCATION (*expr_p);
2720 code = TREE_CODE (*expr_p);
2722 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2723 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2725 /* Prefix or postfix? */
2726 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2727 /* Faster to treat as prefix if result is not used. */
2728 postfix = want_value;
2729 else
2730 postfix = false;
2732 /* For postfix, make sure the inner expression's post side effects
2733 are executed after side effects from this expression. */
2734 if (postfix)
2735 post_p = &post;
2737 /* Add or subtract? */
2738 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2739 arith_code = PLUS_EXPR;
2740 else
2741 arith_code = MINUS_EXPR;
2743 /* Gimplify the LHS into a GIMPLE lvalue. */
2744 lvalue = TREE_OPERAND (*expr_p, 0);
2745 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2746 if (ret == GS_ERROR)
2747 return ret;
2749 /* Extract the operands to the arithmetic operation. */
2750 lhs = lvalue;
2751 rhs = TREE_OPERAND (*expr_p, 1);
2753 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2754 that as the result value and in the postqueue operation. */
2755 if (postfix)
2757 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2758 if (ret == GS_ERROR)
2759 return ret;
2761 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2764 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2765 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2767 rhs = convert_to_ptrofftype_loc (loc, rhs);
2768 if (arith_code == MINUS_EXPR)
2769 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2770 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2772 else
2773 t1 = fold_convert (TREE_TYPE (*expr_p),
2774 fold_build2 (arith_code, arith_type,
2775 fold_convert (arith_type, lhs),
2776 fold_convert (arith_type, rhs)));
2778 if (postfix)
2780 gimplify_assign (lvalue, t1, pre_p);
2781 gimplify_seq_add_seq (orig_post_p, post);
2782 *expr_p = lhs;
2783 return GS_ALL_DONE;
2785 else
2787 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2788 return GS_OK;
2792 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2794 static void
2795 maybe_with_size_expr (tree *expr_p)
2797 tree expr = *expr_p;
2798 tree type = TREE_TYPE (expr);
2799 tree size;
2801 /* If we've already wrapped this or the type is error_mark_node, we can't do
2802 anything. */
2803 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2804 || type == error_mark_node)
2805 return;
2807 /* If the size isn't known or is a constant, we have nothing to do. */
2808 size = TYPE_SIZE_UNIT (type);
2809 if (!size || TREE_CODE (size) == INTEGER_CST)
2810 return;
2812 /* Otherwise, make a WITH_SIZE_EXPR. */
2813 size = unshare_expr (size);
2814 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2815 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2818 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2819 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2820 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
2821 gimplified to an SSA name. */
2823 enum gimplify_status
2824 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
2825 bool allow_ssa)
2827 bool (*test) (tree);
2828 fallback_t fb;
2830 /* In general, we allow lvalues for function arguments to avoid
2831 extra overhead of copying large aggregates out of even larger
2832 aggregates into temporaries only to copy the temporaries to
2833 the argument list. Make optimizers happy by pulling out to
2834 temporaries those types that fit in registers. */
2835 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2836 test = is_gimple_val, fb = fb_rvalue;
2837 else
2839 test = is_gimple_lvalue, fb = fb_either;
2840 /* Also strip a TARGET_EXPR that would force an extra copy. */
2841 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2843 tree init = TARGET_EXPR_INITIAL (*arg_p);
2844 if (init
2845 && !VOID_TYPE_P (TREE_TYPE (init)))
2846 *arg_p = init;
2850 /* If this is a variable sized type, we must remember the size. */
2851 maybe_with_size_expr (arg_p);
2853 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2854 /* Make sure arguments have the same location as the function call
2855 itself. */
2856 protected_set_expr_location (*arg_p, call_location);
2858 /* There is a sequence point before a function call. Side effects in
2859 the argument list must occur before the actual call. So, when
2860 gimplifying arguments, force gimplify_expr to use an internal
2861 post queue which is then appended to the end of PRE_P. */
2862 return gimplify_expr (arg_p, pre_p, NULL, test, fb, allow_ssa);
2865 /* Don't fold inside offloading or taskreg regions: it can break code by
2866 adding decl references that weren't in the source. We'll do it during
2867 omplower pass instead. */
2869 static bool
2870 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2872 struct gimplify_omp_ctx *ctx;
2873 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2874 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
2875 return false;
2876 return fold_stmt (gsi);
2879 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2880 WANT_VALUE is true if the result of the call is desired. */
2882 static enum gimplify_status
2883 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2885 tree fndecl, parms, p, fnptrtype;
2886 enum gimplify_status ret;
2887 int i, nargs;
2888 gcall *call;
2889 bool builtin_va_start_p = false;
2890 location_t loc = EXPR_LOCATION (*expr_p);
2892 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2894 /* For reliable diagnostics during inlining, it is necessary that
2895 every call_expr be annotated with file and line. */
2896 if (! EXPR_HAS_LOCATION (*expr_p))
2897 SET_EXPR_LOCATION (*expr_p, input_location);
2899 /* Gimplify internal functions created in the FEs. */
2900 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2902 if (want_value)
2903 return GS_ALL_DONE;
2905 nargs = call_expr_nargs (*expr_p);
2906 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2907 auto_vec<tree> vargs (nargs);
2909 for (i = 0; i < nargs; i++)
2911 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2912 EXPR_LOCATION (*expr_p));
2913 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2915 gimple *call = gimple_build_call_internal_vec (ifn, vargs);
2916 gimplify_seq_add_stmt (pre_p, call);
2917 return GS_ALL_DONE;
2920 /* This may be a call to a builtin function.
2922 Builtin function calls may be transformed into different
2923 (and more efficient) builtin function calls under certain
2924 circumstances. Unfortunately, gimplification can muck things
2925 up enough that the builtin expanders are not aware that certain
2926 transformations are still valid.
2928 So we attempt transformation/gimplification of the call before
2929 we gimplify the CALL_EXPR. At this time we do not manage to
2930 transform all calls in the same manner as the expanders do, but
2931 we do transform most of them. */
2932 fndecl = get_callee_fndecl (*expr_p);
2933 if (fndecl
2934 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2935 switch (DECL_FUNCTION_CODE (fndecl))
2937 case BUILT_IN_ALLOCA:
2938 case BUILT_IN_ALLOCA_WITH_ALIGN:
2939 /* If the call has been built for a variable-sized object, then we
2940 want to restore the stack level when the enclosing BIND_EXPR is
2941 exited to reclaim the allocated space; otherwise, we precisely
2942 need to do the opposite and preserve the latest stack level. */
2943 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
2944 gimplify_ctxp->save_stack = true;
2945 else
2946 gimplify_ctxp->keep_stack = true;
2947 break;
2949 case BUILT_IN_VA_START:
2951 builtin_va_start_p = TRUE;
2952 if (call_expr_nargs (*expr_p) < 2)
2954 error ("too few arguments to function %<va_start%>");
2955 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2956 return GS_OK;
2959 if (fold_builtin_next_arg (*expr_p, true))
2961 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2962 return GS_OK;
2964 break;
2967 default:
2970 if (fndecl && DECL_BUILT_IN (fndecl))
2972 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2973 if (new_tree && new_tree != *expr_p)
2975 /* There was a transformation of this call which computes the
2976 same value, but in a more efficient way. Return and try
2977 again. */
2978 *expr_p = new_tree;
2979 return GS_OK;
2983 /* Remember the original function pointer type. */
2984 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2986 /* There is a sequence point before the call, so any side effects in
2987 the calling expression must occur before the actual call. Force
2988 gimplify_expr to use an internal post queue. */
2989 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2990 is_gimple_call_addr, fb_rvalue);
2992 nargs = call_expr_nargs (*expr_p);
2994 /* Get argument types for verification. */
2995 fndecl = get_callee_fndecl (*expr_p);
2996 parms = NULL_TREE;
2997 if (fndecl)
2998 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2999 else
3000 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
3002 if (fndecl && DECL_ARGUMENTS (fndecl))
3003 p = DECL_ARGUMENTS (fndecl);
3004 else if (parms)
3005 p = parms;
3006 else
3007 p = NULL_TREE;
3008 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
3011 /* If the last argument is __builtin_va_arg_pack () and it is not
3012 passed as a named argument, decrease the number of CALL_EXPR
3013 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
3014 if (!p
3015 && i < nargs
3016 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
3018 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
3019 tree last_arg_fndecl = get_callee_fndecl (last_arg);
3021 if (last_arg_fndecl
3022 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
3023 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
3024 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
3026 tree call = *expr_p;
3028 --nargs;
3029 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
3030 CALL_EXPR_FN (call),
3031 nargs, CALL_EXPR_ARGP (call));
3033 /* Copy all CALL_EXPR flags, location and block, except
3034 CALL_EXPR_VA_ARG_PACK flag. */
3035 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
3036 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
3037 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
3038 = CALL_EXPR_RETURN_SLOT_OPT (call);
3039 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
3040 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
3042 /* Set CALL_EXPR_VA_ARG_PACK. */
3043 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
3047 /* If the call returns twice then after building the CFG the call
3048 argument computations will no longer dominate the call because
3049 we add an abnormal incoming edge to the call. So do not use SSA
3050 vars there. */
3051 bool returns_twice = call_expr_flags (*expr_p) & ECF_RETURNS_TWICE;
3053 /* Gimplify the function arguments. */
3054 if (nargs > 0)
3056 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
3057 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
3058 PUSH_ARGS_REVERSED ? i-- : i++)
3060 enum gimplify_status t;
3062 /* Avoid gimplifying the second argument to va_start, which needs to
3063 be the plain PARM_DECL. */
3064 if ((i != 1) || !builtin_va_start_p)
3066 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
3067 EXPR_LOCATION (*expr_p), ! returns_twice);
3069 if (t == GS_ERROR)
3070 ret = GS_ERROR;
3075 /* Gimplify the static chain. */
3076 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
3078 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
3079 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
3080 else
3082 enum gimplify_status t;
3083 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
3084 EXPR_LOCATION (*expr_p), ! returns_twice);
3085 if (t == GS_ERROR)
3086 ret = GS_ERROR;
3090 /* Verify the function result. */
3091 if (want_value && fndecl
3092 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
3094 error_at (loc, "using result of function returning %<void%>");
3095 ret = GS_ERROR;
3098 /* Try this again in case gimplification exposed something. */
3099 if (ret != GS_ERROR)
3101 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
3103 if (new_tree && new_tree != *expr_p)
3105 /* There was a transformation of this call which computes the
3106 same value, but in a more efficient way. Return and try
3107 again. */
3108 *expr_p = new_tree;
3109 return GS_OK;
3112 else
3114 *expr_p = error_mark_node;
3115 return GS_ERROR;
3118 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
3119 decl. This allows us to eliminate redundant or useless
3120 calls to "const" functions. */
3121 if (TREE_CODE (*expr_p) == CALL_EXPR)
3123 int flags = call_expr_flags (*expr_p);
3124 if (flags & (ECF_CONST | ECF_PURE)
3125 /* An infinite loop is considered a side effect. */
3126 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
3127 TREE_SIDE_EFFECTS (*expr_p) = 0;
3130 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
3131 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
3132 form and delegate the creation of a GIMPLE_CALL to
3133 gimplify_modify_expr. This is always possible because when
3134 WANT_VALUE is true, the caller wants the result of this call into
3135 a temporary, which means that we will emit an INIT_EXPR in
3136 internal_get_tmp_var which will then be handled by
3137 gimplify_modify_expr. */
3138 if (!want_value)
3140 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
3141 have to do is replicate it as a GIMPLE_CALL tuple. */
3142 gimple_stmt_iterator gsi;
3143 call = gimple_build_call_from_tree (*expr_p);
3144 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
3145 notice_special_calls (call);
3146 gimplify_seq_add_stmt (pre_p, call);
3147 gsi = gsi_last (*pre_p);
3148 maybe_fold_stmt (&gsi);
3149 *expr_p = NULL_TREE;
3151 else
3152 /* Remember the original function type. */
3153 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
3154 CALL_EXPR_FN (*expr_p));
3156 return ret;
3159 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
3160 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
3162 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
3163 condition is true or false, respectively. If null, we should generate
3164 our own to skip over the evaluation of this specific expression.
3166 LOCUS is the source location of the COND_EXPR.
3168 This function is the tree equivalent of do_jump.
3170 shortcut_cond_r should only be called by shortcut_cond_expr. */
3172 static tree
3173 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
3174 location_t locus)
3176 tree local_label = NULL_TREE;
3177 tree t, expr = NULL;
3179 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
3180 retain the shortcut semantics. Just insert the gotos here;
3181 shortcut_cond_expr will append the real blocks later. */
3182 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3184 location_t new_locus;
3186 /* Turn if (a && b) into
3188 if (a); else goto no;
3189 if (b) goto yes; else goto no;
3190 (no:) */
3192 if (false_label_p == NULL)
3193 false_label_p = &local_label;
3195 /* Keep the original source location on the first 'if'. */
3196 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
3197 append_to_statement_list (t, &expr);
3199 /* Set the source location of the && on the second 'if'. */
3200 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
3201 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3202 new_locus);
3203 append_to_statement_list (t, &expr);
3205 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3207 location_t new_locus;
3209 /* Turn if (a || b) into
3211 if (a) goto yes;
3212 if (b) goto yes; else goto no;
3213 (yes:) */
3215 if (true_label_p == NULL)
3216 true_label_p = &local_label;
3218 /* Keep the original source location on the first 'if'. */
3219 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
3220 append_to_statement_list (t, &expr);
3222 /* Set the source location of the || on the second 'if'. */
3223 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
3224 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
3225 new_locus);
3226 append_to_statement_list (t, &expr);
3228 else if (TREE_CODE (pred) == COND_EXPR
3229 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
3230 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
3232 location_t new_locus;
3234 /* As long as we're messing with gotos, turn if (a ? b : c) into
3235 if (a)
3236 if (b) goto yes; else goto no;
3237 else
3238 if (c) goto yes; else goto no;
3240 Don't do this if one of the arms has void type, which can happen
3241 in C++ when the arm is throw. */
3243 /* Keep the original source location on the first 'if'. Set the source
3244 location of the ? on the second 'if'. */
3245 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
3246 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
3247 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
3248 false_label_p, locus),
3249 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
3250 false_label_p, new_locus));
3252 else
3254 expr = build3 (COND_EXPR, void_type_node, pred,
3255 build_and_jump (true_label_p),
3256 build_and_jump (false_label_p));
3257 SET_EXPR_LOCATION (expr, locus);
3260 if (local_label)
3262 t = build1 (LABEL_EXPR, void_type_node, local_label);
3263 append_to_statement_list (t, &expr);
3266 return expr;
3269 /* Given a conditional expression EXPR with short-circuit boolean
3270 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
3271 predicate apart into the equivalent sequence of conditionals. */
3273 static tree
3274 shortcut_cond_expr (tree expr)
3276 tree pred = TREE_OPERAND (expr, 0);
3277 tree then_ = TREE_OPERAND (expr, 1);
3278 tree else_ = TREE_OPERAND (expr, 2);
3279 tree true_label, false_label, end_label, t;
3280 tree *true_label_p;
3281 tree *false_label_p;
3282 bool emit_end, emit_false, jump_over_else;
3283 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
3284 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
3286 /* First do simple transformations. */
3287 if (!else_se)
3289 /* If there is no 'else', turn
3290 if (a && b) then c
3291 into
3292 if (a) if (b) then c. */
3293 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
3295 /* Keep the original source location on the first 'if'. */
3296 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3297 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3298 /* Set the source location of the && on the second 'if'. */
3299 if (EXPR_HAS_LOCATION (pred))
3300 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
3301 then_ = shortcut_cond_expr (expr);
3302 then_se = then_ && TREE_SIDE_EFFECTS (then_);
3303 pred = TREE_OPERAND (pred, 0);
3304 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
3305 SET_EXPR_LOCATION (expr, locus);
3309 if (!then_se)
3311 /* If there is no 'then', turn
3312 if (a || b); else d
3313 into
3314 if (a); else if (b); else d. */
3315 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
3317 /* Keep the original source location on the first 'if'. */
3318 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
3319 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
3320 /* Set the source location of the || on the second 'if'. */
3321 if (EXPR_HAS_LOCATION (pred))
3322 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
3323 else_ = shortcut_cond_expr (expr);
3324 else_se = else_ && TREE_SIDE_EFFECTS (else_);
3325 pred = TREE_OPERAND (pred, 0);
3326 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
3327 SET_EXPR_LOCATION (expr, locus);
3331 /* If we're done, great. */
3332 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
3333 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
3334 return expr;
3336 /* Otherwise we need to mess with gotos. Change
3337 if (a) c; else d;
3339 if (a); else goto no;
3340 c; goto end;
3341 no: d; end:
3342 and recursively gimplify the condition. */
3344 true_label = false_label = end_label = NULL_TREE;
3346 /* If our arms just jump somewhere, hijack those labels so we don't
3347 generate jumps to jumps. */
3349 if (then_
3350 && TREE_CODE (then_) == GOTO_EXPR
3351 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
3353 true_label = GOTO_DESTINATION (then_);
3354 then_ = NULL;
3355 then_se = false;
3358 if (else_
3359 && TREE_CODE (else_) == GOTO_EXPR
3360 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
3362 false_label = GOTO_DESTINATION (else_);
3363 else_ = NULL;
3364 else_se = false;
3367 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
3368 if (true_label)
3369 true_label_p = &true_label;
3370 else
3371 true_label_p = NULL;
3373 /* The 'else' branch also needs a label if it contains interesting code. */
3374 if (false_label || else_se)
3375 false_label_p = &false_label;
3376 else
3377 false_label_p = NULL;
3379 /* If there was nothing else in our arms, just forward the label(s). */
3380 if (!then_se && !else_se)
3381 return shortcut_cond_r (pred, true_label_p, false_label_p,
3382 EXPR_LOC_OR_LOC (expr, input_location));
3384 /* If our last subexpression already has a terminal label, reuse it. */
3385 if (else_se)
3386 t = expr_last (else_);
3387 else if (then_se)
3388 t = expr_last (then_);
3389 else
3390 t = NULL;
3391 if (t && TREE_CODE (t) == LABEL_EXPR)
3392 end_label = LABEL_EXPR_LABEL (t);
3394 /* If we don't care about jumping to the 'else' branch, jump to the end
3395 if the condition is false. */
3396 if (!false_label_p)
3397 false_label_p = &end_label;
3399 /* We only want to emit these labels if we aren't hijacking them. */
3400 emit_end = (end_label == NULL_TREE);
3401 emit_false = (false_label == NULL_TREE);
3403 /* We only emit the jump over the else clause if we have to--if the
3404 then clause may fall through. Otherwise we can wind up with a
3405 useless jump and a useless label at the end of gimplified code,
3406 which will cause us to think that this conditional as a whole
3407 falls through even if it doesn't. If we then inline a function
3408 which ends with such a condition, that can cause us to issue an
3409 inappropriate warning about control reaching the end of a
3410 non-void function. */
3411 jump_over_else = block_may_fallthru (then_);
3413 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3414 EXPR_LOC_OR_LOC (expr, input_location));
3416 expr = NULL;
3417 append_to_statement_list (pred, &expr);
3419 append_to_statement_list (then_, &expr);
3420 if (else_se)
3422 if (jump_over_else)
3424 tree last = expr_last (expr);
3425 t = build_and_jump (&end_label);
3426 if (EXPR_HAS_LOCATION (last))
3427 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
3428 append_to_statement_list (t, &expr);
3430 if (emit_false)
3432 t = build1 (LABEL_EXPR, void_type_node, false_label);
3433 append_to_statement_list (t, &expr);
3435 append_to_statement_list (else_, &expr);
3437 if (emit_end && end_label)
3439 t = build1 (LABEL_EXPR, void_type_node, end_label);
3440 append_to_statement_list (t, &expr);
3443 return expr;
3446 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3448 tree
3449 gimple_boolify (tree expr)
3451 tree type = TREE_TYPE (expr);
3452 location_t loc = EXPR_LOCATION (expr);
3454 if (TREE_CODE (expr) == NE_EXPR
3455 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3456 && integer_zerop (TREE_OPERAND (expr, 1)))
3458 tree call = TREE_OPERAND (expr, 0);
3459 tree fn = get_callee_fndecl (call);
3461 /* For __builtin_expect ((long) (x), y) recurse into x as well
3462 if x is truth_value_p. */
3463 if (fn
3464 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3465 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3466 && call_expr_nargs (call) == 2)
3468 tree arg = CALL_EXPR_ARG (call, 0);
3469 if (arg)
3471 if (TREE_CODE (arg) == NOP_EXPR
3472 && TREE_TYPE (arg) == TREE_TYPE (call))
3473 arg = TREE_OPERAND (arg, 0);
3474 if (truth_value_p (TREE_CODE (arg)))
3476 arg = gimple_boolify (arg);
3477 CALL_EXPR_ARG (call, 0)
3478 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3484 switch (TREE_CODE (expr))
3486 case TRUTH_AND_EXPR:
3487 case TRUTH_OR_EXPR:
3488 case TRUTH_XOR_EXPR:
3489 case TRUTH_ANDIF_EXPR:
3490 case TRUTH_ORIF_EXPR:
3491 /* Also boolify the arguments of truth exprs. */
3492 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3493 /* FALLTHRU */
3495 case TRUTH_NOT_EXPR:
3496 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3498 /* These expressions always produce boolean results. */
3499 if (TREE_CODE (type) != BOOLEAN_TYPE)
3500 TREE_TYPE (expr) = boolean_type_node;
3501 return expr;
3503 case ANNOTATE_EXPR:
3504 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
3506 case annot_expr_ivdep_kind:
3507 case annot_expr_no_vector_kind:
3508 case annot_expr_vector_kind:
3509 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3510 if (TREE_CODE (type) != BOOLEAN_TYPE)
3511 TREE_TYPE (expr) = boolean_type_node;
3512 return expr;
3513 default:
3514 gcc_unreachable ();
3517 default:
3518 if (COMPARISON_CLASS_P (expr))
3520 /* There expressions always prduce boolean results. */
3521 if (TREE_CODE (type) != BOOLEAN_TYPE)
3522 TREE_TYPE (expr) = boolean_type_node;
3523 return expr;
3525 /* Other expressions that get here must have boolean values, but
3526 might need to be converted to the appropriate mode. */
3527 if (TREE_CODE (type) == BOOLEAN_TYPE)
3528 return expr;
3529 return fold_convert_loc (loc, boolean_type_node, expr);
3533 /* Given a conditional expression *EXPR_P without side effects, gimplify
3534 its operands. New statements are inserted to PRE_P. */
3536 static enum gimplify_status
3537 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3539 tree expr = *expr_p, cond;
3540 enum gimplify_status ret, tret;
3541 enum tree_code code;
3543 cond = gimple_boolify (COND_EXPR_COND (expr));
3545 /* We need to handle && and || specially, as their gimplification
3546 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3547 code = TREE_CODE (cond);
3548 if (code == TRUTH_ANDIF_EXPR)
3549 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3550 else if (code == TRUTH_ORIF_EXPR)
3551 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3552 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3553 COND_EXPR_COND (*expr_p) = cond;
3555 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3556 is_gimple_val, fb_rvalue);
3557 ret = MIN (ret, tret);
3558 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3559 is_gimple_val, fb_rvalue);
3561 return MIN (ret, tret);
3564 /* Return true if evaluating EXPR could trap.
3565 EXPR is GENERIC, while tree_could_trap_p can be called
3566 only on GIMPLE. */
3568 static bool
3569 generic_expr_could_trap_p (tree expr)
3571 unsigned i, n;
3573 if (!expr || is_gimple_val (expr))
3574 return false;
3576 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3577 return true;
3579 n = TREE_OPERAND_LENGTH (expr);
3580 for (i = 0; i < n; i++)
3581 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3582 return true;
3584 return false;
3587 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3588 into
3590 if (p) if (p)
3591 t1 = a; a;
3592 else or else
3593 t1 = b; b;
3596 The second form is used when *EXPR_P is of type void.
3598 PRE_P points to the list where side effects that must happen before
3599 *EXPR_P should be stored. */
3601 static enum gimplify_status
3602 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3604 tree expr = *expr_p;
3605 tree type = TREE_TYPE (expr);
3606 location_t loc = EXPR_LOCATION (expr);
3607 tree tmp, arm1, arm2;
3608 enum gimplify_status ret;
3609 tree label_true, label_false, label_cont;
3610 bool have_then_clause_p, have_else_clause_p;
3611 gcond *cond_stmt;
3612 enum tree_code pred_code;
3613 gimple_seq seq = NULL;
3615 /* If this COND_EXPR has a value, copy the values into a temporary within
3616 the arms. */
3617 if (!VOID_TYPE_P (type))
3619 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3620 tree result;
3622 /* If either an rvalue is ok or we do not require an lvalue, create the
3623 temporary. But we cannot do that if the type is addressable. */
3624 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3625 && !TREE_ADDRESSABLE (type))
3627 if (gimplify_ctxp->allow_rhs_cond_expr
3628 /* If either branch has side effects or could trap, it can't be
3629 evaluated unconditionally. */
3630 && !TREE_SIDE_EFFECTS (then_)
3631 && !generic_expr_could_trap_p (then_)
3632 && !TREE_SIDE_EFFECTS (else_)
3633 && !generic_expr_could_trap_p (else_))
3634 return gimplify_pure_cond_expr (expr_p, pre_p);
3636 tmp = create_tmp_var (type, "iftmp");
3637 result = tmp;
3640 /* Otherwise, only create and copy references to the values. */
3641 else
3643 type = build_pointer_type (type);
3645 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3646 then_ = build_fold_addr_expr_loc (loc, then_);
3648 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3649 else_ = build_fold_addr_expr_loc (loc, else_);
3651 expr
3652 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3654 tmp = create_tmp_var (type, "iftmp");
3655 result = build_simple_mem_ref_loc (loc, tmp);
3658 /* Build the new then clause, `tmp = then_;'. But don't build the
3659 assignment if the value is void; in C++ it can be if it's a throw. */
3660 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3661 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3663 /* Similarly, build the new else clause, `tmp = else_;'. */
3664 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3665 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3667 TREE_TYPE (expr) = void_type_node;
3668 recalculate_side_effects (expr);
3670 /* Move the COND_EXPR to the prequeue. */
3671 gimplify_stmt (&expr, pre_p);
3673 *expr_p = result;
3674 return GS_ALL_DONE;
3677 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3678 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3679 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3680 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3682 /* Make sure the condition has BOOLEAN_TYPE. */
3683 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3685 /* Break apart && and || conditions. */
3686 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3687 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3689 expr = shortcut_cond_expr (expr);
3691 if (expr != *expr_p)
3693 *expr_p = expr;
3695 /* We can't rely on gimplify_expr to re-gimplify the expanded
3696 form properly, as cleanups might cause the target labels to be
3697 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3698 set up a conditional context. */
3699 gimple_push_condition ();
3700 gimplify_stmt (expr_p, &seq);
3701 gimple_pop_condition (pre_p);
3702 gimple_seq_add_seq (pre_p, seq);
3704 return GS_ALL_DONE;
3708 /* Now do the normal gimplification. */
3710 /* Gimplify condition. */
3711 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3712 fb_rvalue);
3713 if (ret == GS_ERROR)
3714 return GS_ERROR;
3715 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3717 gimple_push_condition ();
3719 have_then_clause_p = have_else_clause_p = false;
3720 if (TREE_OPERAND (expr, 1) != NULL
3721 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3722 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3723 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3724 == current_function_decl)
3725 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3726 have different locations, otherwise we end up with incorrect
3727 location information on the branches. */
3728 && (optimize
3729 || !EXPR_HAS_LOCATION (expr)
3730 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3731 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3733 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3734 have_then_clause_p = true;
3736 else
3737 label_true = create_artificial_label (UNKNOWN_LOCATION);
3738 if (TREE_OPERAND (expr, 2) != NULL
3739 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3740 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3741 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3742 == current_function_decl)
3743 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3744 have different locations, otherwise we end up with incorrect
3745 location information on the branches. */
3746 && (optimize
3747 || !EXPR_HAS_LOCATION (expr)
3748 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3749 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3751 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3752 have_else_clause_p = true;
3754 else
3755 label_false = create_artificial_label (UNKNOWN_LOCATION);
3757 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3758 &arm2);
3759 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3760 label_false);
3761 gimple_set_no_warning (cond_stmt, TREE_NO_WARNING (COND_EXPR_COND (expr)));
3762 gimplify_seq_add_stmt (&seq, cond_stmt);
3763 gimple_stmt_iterator gsi = gsi_last (seq);
3764 maybe_fold_stmt (&gsi);
3766 label_cont = NULL_TREE;
3767 if (!have_then_clause_p)
3769 /* For if (...) {} else { code; } put label_true after
3770 the else block. */
3771 if (TREE_OPERAND (expr, 1) == NULL_TREE
3772 && !have_else_clause_p
3773 && TREE_OPERAND (expr, 2) != NULL_TREE)
3774 label_cont = label_true;
3775 else
3777 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3778 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3779 /* For if (...) { code; } else {} or
3780 if (...) { code; } else goto label; or
3781 if (...) { code; return; } else { ... }
3782 label_cont isn't needed. */
3783 if (!have_else_clause_p
3784 && TREE_OPERAND (expr, 2) != NULL_TREE
3785 && gimple_seq_may_fallthru (seq))
3787 gimple *g;
3788 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3790 g = gimple_build_goto (label_cont);
3792 /* GIMPLE_COND's are very low level; they have embedded
3793 gotos. This particular embedded goto should not be marked
3794 with the location of the original COND_EXPR, as it would
3795 correspond to the COND_EXPR's condition, not the ELSE or the
3796 THEN arms. To avoid marking it with the wrong location, flag
3797 it as "no location". */
3798 gimple_set_do_not_emit_location (g);
3800 gimplify_seq_add_stmt (&seq, g);
3804 if (!have_else_clause_p)
3806 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3807 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3809 if (label_cont)
3810 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3812 gimple_pop_condition (pre_p);
3813 gimple_seq_add_seq (pre_p, seq);
3815 if (ret == GS_ERROR)
3816 ; /* Do nothing. */
3817 else if (have_then_clause_p || have_else_clause_p)
3818 ret = GS_ALL_DONE;
3819 else
3821 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3822 expr = TREE_OPERAND (expr, 0);
3823 gimplify_stmt (&expr, pre_p);
3826 *expr_p = NULL;
3827 return ret;
3830 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3831 to be marked addressable.
3833 We cannot rely on such an expression being directly markable if a temporary
3834 has been created by the gimplification. In this case, we create another
3835 temporary and initialize it with a copy, which will become a store after we
3836 mark it addressable. This can happen if the front-end passed us something
3837 that it could not mark addressable yet, like a Fortran pass-by-reference
3838 parameter (int) floatvar. */
3840 static void
3841 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3843 while (handled_component_p (*expr_p))
3844 expr_p = &TREE_OPERAND (*expr_p, 0);
3845 if (is_gimple_reg (*expr_p))
3847 /* Do not allow an SSA name as the temporary. */
3848 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL, false);
3849 DECL_GIMPLE_REG_P (var) = 0;
3850 *expr_p = var;
3854 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3855 a call to __builtin_memcpy. */
3857 static enum gimplify_status
3858 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3859 gimple_seq *seq_p)
3861 tree t, to, to_ptr, from, from_ptr;
3862 gcall *gs;
3863 location_t loc = EXPR_LOCATION (*expr_p);
3865 to = TREE_OPERAND (*expr_p, 0);
3866 from = TREE_OPERAND (*expr_p, 1);
3868 /* Mark the RHS addressable. Beware that it may not be possible to do so
3869 directly if a temporary has been created by the gimplification. */
3870 prepare_gimple_addressable (&from, seq_p);
3872 mark_addressable (from);
3873 from_ptr = build_fold_addr_expr_loc (loc, from);
3874 gimplify_arg (&from_ptr, seq_p, loc);
3876 mark_addressable (to);
3877 to_ptr = build_fold_addr_expr_loc (loc, to);
3878 gimplify_arg (&to_ptr, seq_p, loc);
3880 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3882 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3884 if (want_value)
3886 /* tmp = memcpy() */
3887 t = create_tmp_var (TREE_TYPE (to_ptr));
3888 gimple_call_set_lhs (gs, t);
3889 gimplify_seq_add_stmt (seq_p, gs);
3891 *expr_p = build_simple_mem_ref (t);
3892 return GS_ALL_DONE;
3895 gimplify_seq_add_stmt (seq_p, gs);
3896 *expr_p = NULL;
3897 return GS_ALL_DONE;
3900 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3901 a call to __builtin_memset. In this case we know that the RHS is
3902 a CONSTRUCTOR with an empty element list. */
3904 static enum gimplify_status
3905 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3906 gimple_seq *seq_p)
3908 tree t, from, to, to_ptr;
3909 gcall *gs;
3910 location_t loc = EXPR_LOCATION (*expr_p);
3912 /* Assert our assumptions, to abort instead of producing wrong code
3913 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3914 not be immediately exposed. */
3915 from = TREE_OPERAND (*expr_p, 1);
3916 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3917 from = TREE_OPERAND (from, 0);
3919 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3920 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3922 /* Now proceed. */
3923 to = TREE_OPERAND (*expr_p, 0);
3925 to_ptr = build_fold_addr_expr_loc (loc, to);
3926 gimplify_arg (&to_ptr, seq_p, loc);
3927 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3929 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3931 if (want_value)
3933 /* tmp = memset() */
3934 t = create_tmp_var (TREE_TYPE (to_ptr));
3935 gimple_call_set_lhs (gs, t);
3936 gimplify_seq_add_stmt (seq_p, gs);
3938 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3939 return GS_ALL_DONE;
3942 gimplify_seq_add_stmt (seq_p, gs);
3943 *expr_p = NULL;
3944 return GS_ALL_DONE;
3947 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3948 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3949 assignment. Return non-null if we detect a potential overlap. */
3951 struct gimplify_init_ctor_preeval_data
3953 /* The base decl of the lhs object. May be NULL, in which case we
3954 have to assume the lhs is indirect. */
3955 tree lhs_base_decl;
3957 /* The alias set of the lhs object. */
3958 alias_set_type lhs_alias_set;
3961 static tree
3962 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3964 struct gimplify_init_ctor_preeval_data *data
3965 = (struct gimplify_init_ctor_preeval_data *) xdata;
3966 tree t = *tp;
3968 /* If we find the base object, obviously we have overlap. */
3969 if (data->lhs_base_decl == t)
3970 return t;
3972 /* If the constructor component is indirect, determine if we have a
3973 potential overlap with the lhs. The only bits of information we
3974 have to go on at this point are addressability and alias sets. */
3975 if ((INDIRECT_REF_P (t)
3976 || TREE_CODE (t) == MEM_REF)
3977 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3978 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3979 return t;
3981 /* If the constructor component is a call, determine if it can hide a
3982 potential overlap with the lhs through an INDIRECT_REF like above.
3983 ??? Ugh - this is completely broken. In fact this whole analysis
3984 doesn't look conservative. */
3985 if (TREE_CODE (t) == CALL_EXPR)
3987 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3989 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3990 if (POINTER_TYPE_P (TREE_VALUE (type))
3991 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3992 && alias_sets_conflict_p (data->lhs_alias_set,
3993 get_alias_set
3994 (TREE_TYPE (TREE_VALUE (type)))))
3995 return t;
3998 if (IS_TYPE_OR_DECL_P (t))
3999 *walk_subtrees = 0;
4000 return NULL;
4003 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
4004 force values that overlap with the lhs (as described by *DATA)
4005 into temporaries. */
4007 static void
4008 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4009 struct gimplify_init_ctor_preeval_data *data)
4011 enum gimplify_status one;
4013 /* If the value is constant, then there's nothing to pre-evaluate. */
4014 if (TREE_CONSTANT (*expr_p))
4016 /* Ensure it does not have side effects, it might contain a reference to
4017 the object we're initializing. */
4018 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
4019 return;
4022 /* If the type has non-trivial constructors, we can't pre-evaluate. */
4023 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
4024 return;
4026 /* Recurse for nested constructors. */
4027 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
4029 unsigned HOST_WIDE_INT ix;
4030 constructor_elt *ce;
4031 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
4033 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
4034 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
4036 return;
4039 /* If this is a variable sized type, we must remember the size. */
4040 maybe_with_size_expr (expr_p);
4042 /* Gimplify the constructor element to something appropriate for the rhs
4043 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
4044 the gimplifier will consider this a store to memory. Doing this
4045 gimplification now means that we won't have to deal with complicated
4046 language-specific trees, nor trees like SAVE_EXPR that can induce
4047 exponential search behavior. */
4048 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
4049 if (one == GS_ERROR)
4051 *expr_p = NULL;
4052 return;
4055 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
4056 with the lhs, since "a = { .x=a }" doesn't make sense. This will
4057 always be true for all scalars, since is_gimple_mem_rhs insists on a
4058 temporary variable for them. */
4059 if (DECL_P (*expr_p))
4060 return;
4062 /* If this is of variable size, we have no choice but to assume it doesn't
4063 overlap since we can't make a temporary for it. */
4064 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
4065 return;
4067 /* Otherwise, we must search for overlap ... */
4068 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
4069 return;
4071 /* ... and if found, force the value into a temporary. */
4072 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4075 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
4076 a RANGE_EXPR in a CONSTRUCTOR for an array.
4078 var = lower;
4079 loop_entry:
4080 object[var] = value;
4081 if (var == upper)
4082 goto loop_exit;
4083 var = var + 1;
4084 goto loop_entry;
4085 loop_exit:
4087 We increment var _after_ the loop exit check because we might otherwise
4088 fail if upper == TYPE_MAX_VALUE (type for upper).
4090 Note that we never have to deal with SAVE_EXPRs here, because this has
4091 already been taken care of for us, in gimplify_init_ctor_preeval(). */
4093 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
4094 gimple_seq *, bool);
4096 static void
4097 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
4098 tree value, tree array_elt_type,
4099 gimple_seq *pre_p, bool cleared)
4101 tree loop_entry_label, loop_exit_label, fall_thru_label;
4102 tree var, var_type, cref, tmp;
4104 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
4105 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
4106 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
4108 /* Create and initialize the index variable. */
4109 var_type = TREE_TYPE (upper);
4110 var = create_tmp_var (var_type);
4111 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
4113 /* Add the loop entry label. */
4114 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
4116 /* Build the reference. */
4117 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4118 var, NULL_TREE, NULL_TREE);
4120 /* If we are a constructor, just call gimplify_init_ctor_eval to do
4121 the store. Otherwise just assign value to the reference. */
4123 if (TREE_CODE (value) == CONSTRUCTOR)
4124 /* NB we might have to call ourself recursively through
4125 gimplify_init_ctor_eval if the value is a constructor. */
4126 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4127 pre_p, cleared);
4128 else
4129 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
4131 /* We exit the loop when the index var is equal to the upper bound. */
4132 gimplify_seq_add_stmt (pre_p,
4133 gimple_build_cond (EQ_EXPR, var, upper,
4134 loop_exit_label, fall_thru_label));
4136 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
4138 /* Otherwise, increment the index var... */
4139 tmp = build2 (PLUS_EXPR, var_type, var,
4140 fold_convert (var_type, integer_one_node));
4141 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
4143 /* ...and jump back to the loop entry. */
4144 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
4146 /* Add the loop exit label. */
4147 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
4150 /* Return true if FDECL is accessing a field that is zero sized. */
4152 static bool
4153 zero_sized_field_decl (const_tree fdecl)
4155 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
4156 && integer_zerop (DECL_SIZE (fdecl)))
4157 return true;
4158 return false;
4161 /* Return true if TYPE is zero sized. */
4163 static bool
4164 zero_sized_type (const_tree type)
4166 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
4167 && integer_zerop (TYPE_SIZE (type)))
4168 return true;
4169 return false;
4172 /* A subroutine of gimplify_init_constructor. Generate individual
4173 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
4174 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
4175 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
4176 zeroed first. */
4178 static void
4179 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
4180 gimple_seq *pre_p, bool cleared)
4182 tree array_elt_type = NULL;
4183 unsigned HOST_WIDE_INT ix;
4184 tree purpose, value;
4186 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
4187 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
4189 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
4191 tree cref;
4193 /* NULL values are created above for gimplification errors. */
4194 if (value == NULL)
4195 continue;
4197 if (cleared && initializer_zerop (value))
4198 continue;
4200 /* ??? Here's to hoping the front end fills in all of the indices,
4201 so we don't have to figure out what's missing ourselves. */
4202 gcc_assert (purpose);
4204 /* Skip zero-sized fields, unless value has side-effects. This can
4205 happen with calls to functions returning a zero-sized type, which
4206 we shouldn't discard. As a number of downstream passes don't
4207 expect sets of zero-sized fields, we rely on the gimplification of
4208 the MODIFY_EXPR we make below to drop the assignment statement. */
4209 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
4210 continue;
4212 /* If we have a RANGE_EXPR, we have to build a loop to assign the
4213 whole range. */
4214 if (TREE_CODE (purpose) == RANGE_EXPR)
4216 tree lower = TREE_OPERAND (purpose, 0);
4217 tree upper = TREE_OPERAND (purpose, 1);
4219 /* If the lower bound is equal to upper, just treat it as if
4220 upper was the index. */
4221 if (simple_cst_equal (lower, upper))
4222 purpose = upper;
4223 else
4225 gimplify_init_ctor_eval_range (object, lower, upper, value,
4226 array_elt_type, pre_p, cleared);
4227 continue;
4231 if (array_elt_type)
4233 /* Do not use bitsizetype for ARRAY_REF indices. */
4234 if (TYPE_DOMAIN (TREE_TYPE (object)))
4235 purpose
4236 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
4237 purpose);
4238 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
4239 purpose, NULL_TREE, NULL_TREE);
4241 else
4243 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
4244 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
4245 unshare_expr (object), purpose, NULL_TREE);
4248 if (TREE_CODE (value) == CONSTRUCTOR
4249 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
4250 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
4251 pre_p, cleared);
4252 else
4254 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
4255 gimplify_and_add (init, pre_p);
4256 ggc_free (init);
4261 /* Return the appropriate RHS predicate for this LHS. */
4263 gimple_predicate
4264 rhs_predicate_for (tree lhs)
4266 if (is_gimple_reg (lhs))
4267 return is_gimple_reg_rhs_or_call;
4268 else
4269 return is_gimple_mem_rhs_or_call;
4272 /* Return the initial guess for an appropriate RHS predicate for this LHS,
4273 before the LHS has been gimplified. */
4275 static gimple_predicate
4276 initial_rhs_predicate_for (tree lhs)
4278 if (is_gimple_reg_type (TREE_TYPE (lhs)))
4279 return is_gimple_reg_rhs_or_call;
4280 else
4281 return is_gimple_mem_rhs_or_call;
4284 /* Gimplify a C99 compound literal expression. This just means adding
4285 the DECL_EXPR before the current statement and using its anonymous
4286 decl instead. */
4288 static enum gimplify_status
4289 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
4290 bool (*gimple_test_f) (tree),
4291 fallback_t fallback)
4293 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
4294 tree decl = DECL_EXPR_DECL (decl_s);
4295 tree init = DECL_INITIAL (decl);
4296 /* Mark the decl as addressable if the compound literal
4297 expression is addressable now, otherwise it is marked too late
4298 after we gimplify the initialization expression. */
4299 if (TREE_ADDRESSABLE (*expr_p))
4300 TREE_ADDRESSABLE (decl) = 1;
4301 /* Otherwise, if we don't need an lvalue and have a literal directly
4302 substitute it. Check if it matches the gimple predicate, as
4303 otherwise we'd generate a new temporary, and we can as well just
4304 use the decl we already have. */
4305 else if (!TREE_ADDRESSABLE (decl)
4306 && init
4307 && (fallback & fb_lvalue) == 0
4308 && gimple_test_f (init))
4310 *expr_p = init;
4311 return GS_OK;
4314 /* Preliminarily mark non-addressed complex variables as eligible
4315 for promotion to gimple registers. We'll transform their uses
4316 as we find them. */
4317 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
4318 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
4319 && !TREE_THIS_VOLATILE (decl)
4320 && !needs_to_live_in_memory (decl))
4321 DECL_GIMPLE_REG_P (decl) = 1;
4323 /* If the decl is not addressable, then it is being used in some
4324 expression or on the right hand side of a statement, and it can
4325 be put into a readonly data section. */
4326 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
4327 TREE_READONLY (decl) = 1;
4329 /* This decl isn't mentioned in the enclosing block, so add it to the
4330 list of temps. FIXME it seems a bit of a kludge to say that
4331 anonymous artificial vars aren't pushed, but everything else is. */
4332 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
4333 gimple_add_tmp_var (decl);
4335 gimplify_and_add (decl_s, pre_p);
4336 *expr_p = decl;
4337 return GS_OK;
4340 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
4341 return a new CONSTRUCTOR if something changed. */
4343 static tree
4344 optimize_compound_literals_in_ctor (tree orig_ctor)
4346 tree ctor = orig_ctor;
4347 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4348 unsigned int idx, num = vec_safe_length (elts);
4350 for (idx = 0; idx < num; idx++)
4352 tree value = (*elts)[idx].value;
4353 tree newval = value;
4354 if (TREE_CODE (value) == CONSTRUCTOR)
4355 newval = optimize_compound_literals_in_ctor (value);
4356 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
4358 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
4359 tree decl = DECL_EXPR_DECL (decl_s);
4360 tree init = DECL_INITIAL (decl);
4362 if (!TREE_ADDRESSABLE (value)
4363 && !TREE_ADDRESSABLE (decl)
4364 && init
4365 && TREE_CODE (init) == CONSTRUCTOR)
4366 newval = optimize_compound_literals_in_ctor (init);
4368 if (newval == value)
4369 continue;
4371 if (ctor == orig_ctor)
4373 ctor = copy_node (orig_ctor);
4374 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
4375 elts = CONSTRUCTOR_ELTS (ctor);
4377 (*elts)[idx].value = newval;
4379 return ctor;
4382 /* A subroutine of gimplify_modify_expr. Break out elements of a
4383 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
4385 Note that we still need to clear any elements that don't have explicit
4386 initializers, so if not all elements are initialized we keep the
4387 original MODIFY_EXPR, we just remove all of the constructor elements.
4389 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
4390 GS_ERROR if we would have to create a temporary when gimplifying
4391 this constructor. Otherwise, return GS_OK.
4393 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
4395 static enum gimplify_status
4396 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4397 bool want_value, bool notify_temp_creation)
4399 tree object, ctor, type;
4400 enum gimplify_status ret;
4401 vec<constructor_elt, va_gc> *elts;
4403 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
4405 if (!notify_temp_creation)
4407 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4408 is_gimple_lvalue, fb_lvalue);
4409 if (ret == GS_ERROR)
4410 return ret;
4413 object = TREE_OPERAND (*expr_p, 0);
4414 ctor = TREE_OPERAND (*expr_p, 1) =
4415 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
4416 type = TREE_TYPE (ctor);
4417 elts = CONSTRUCTOR_ELTS (ctor);
4418 ret = GS_ALL_DONE;
4420 switch (TREE_CODE (type))
4422 case RECORD_TYPE:
4423 case UNION_TYPE:
4424 case QUAL_UNION_TYPE:
4425 case ARRAY_TYPE:
4427 struct gimplify_init_ctor_preeval_data preeval_data;
4428 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
4429 bool cleared, complete_p, valid_const_initializer;
4431 /* Aggregate types must lower constructors to initialization of
4432 individual elements. The exception is that a CONSTRUCTOR node
4433 with no elements indicates zero-initialization of the whole. */
4434 if (vec_safe_is_empty (elts))
4436 if (notify_temp_creation)
4437 return GS_OK;
4438 break;
4441 /* Fetch information about the constructor to direct later processing.
4442 We might want to make static versions of it in various cases, and
4443 can only do so if it known to be a valid constant initializer. */
4444 valid_const_initializer
4445 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4446 &num_ctor_elements, &complete_p);
4448 /* If a const aggregate variable is being initialized, then it
4449 should never be a lose to promote the variable to be static. */
4450 if (valid_const_initializer
4451 && num_nonzero_elements > 1
4452 && TREE_READONLY (object)
4453 && VAR_P (object)
4454 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4456 if (notify_temp_creation)
4457 return GS_ERROR;
4458 DECL_INITIAL (object) = ctor;
4459 TREE_STATIC (object) = 1;
4460 if (!DECL_NAME (object))
4461 DECL_NAME (object) = create_tmp_var_name ("C");
4462 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4464 /* ??? C++ doesn't automatically append a .<number> to the
4465 assembler name, and even when it does, it looks at FE private
4466 data structures to figure out what that number should be,
4467 which are not set for this variable. I suppose this is
4468 important for local statics for inline functions, which aren't
4469 "local" in the object file sense. So in order to get a unique
4470 TU-local symbol, we must invoke the lhd version now. */
4471 lhd_set_decl_assembler_name (object);
4473 *expr_p = NULL_TREE;
4474 break;
4477 /* If there are "lots" of initialized elements, even discounting
4478 those that are not address constants (and thus *must* be
4479 computed at runtime), then partition the constructor into
4480 constant and non-constant parts. Block copy the constant
4481 parts in, then generate code for the non-constant parts. */
4482 /* TODO. There's code in cp/typeck.c to do this. */
4484 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4485 /* store_constructor will ignore the clearing of variable-sized
4486 objects. Initializers for such objects must explicitly set
4487 every field that needs to be set. */
4488 cleared = false;
4489 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
4490 /* If the constructor isn't complete, clear the whole object
4491 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
4493 ??? This ought not to be needed. For any element not present
4494 in the initializer, we should simply set them to zero. Except
4495 we'd need to *find* the elements that are not present, and that
4496 requires trickery to avoid quadratic compile-time behavior in
4497 large cases or excessive memory use in small cases. */
4498 cleared = true;
4499 else if (num_ctor_elements - num_nonzero_elements
4500 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4501 && num_nonzero_elements < num_ctor_elements / 4)
4502 /* If there are "lots" of zeros, it's more efficient to clear
4503 the memory and then set the nonzero elements. */
4504 cleared = true;
4505 else
4506 cleared = false;
4508 /* If there are "lots" of initialized elements, and all of them
4509 are valid address constants, then the entire initializer can
4510 be dropped to memory, and then memcpy'd out. Don't do this
4511 for sparse arrays, though, as it's more efficient to follow
4512 the standard CONSTRUCTOR behavior of memset followed by
4513 individual element initialization. Also don't do this for small
4514 all-zero initializers (which aren't big enough to merit
4515 clearing), and don't try to make bitwise copies of
4516 TREE_ADDRESSABLE types.
4518 We cannot apply such transformation when compiling chkp static
4519 initializer because creation of initializer image in the memory
4520 will require static initialization of bounds for it. It should
4521 result in another gimplification of similar initializer and we
4522 may fall into infinite loop. */
4523 if (valid_const_initializer
4524 && !(cleared || num_nonzero_elements == 0)
4525 && !TREE_ADDRESSABLE (type)
4526 && (!current_function_decl
4527 || !lookup_attribute ("chkp ctor",
4528 DECL_ATTRIBUTES (current_function_decl))))
4530 HOST_WIDE_INT size = int_size_in_bytes (type);
4531 unsigned int align;
4533 /* ??? We can still get unbounded array types, at least
4534 from the C++ front end. This seems wrong, but attempt
4535 to work around it for now. */
4536 if (size < 0)
4538 size = int_size_in_bytes (TREE_TYPE (object));
4539 if (size >= 0)
4540 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4543 /* Find the maximum alignment we can assume for the object. */
4544 /* ??? Make use of DECL_OFFSET_ALIGN. */
4545 if (DECL_P (object))
4546 align = DECL_ALIGN (object);
4547 else
4548 align = TYPE_ALIGN (type);
4550 /* Do a block move either if the size is so small as to make
4551 each individual move a sub-unit move on average, or if it
4552 is so large as to make individual moves inefficient. */
4553 if (size > 0
4554 && num_nonzero_elements > 1
4555 && (size < num_nonzero_elements
4556 || !can_move_by_pieces (size, align)))
4558 if (notify_temp_creation)
4559 return GS_ERROR;
4561 walk_tree (&ctor, force_labels_r, NULL, NULL);
4562 ctor = tree_output_constant_def (ctor);
4563 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4564 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4565 TREE_OPERAND (*expr_p, 1) = ctor;
4567 /* This is no longer an assignment of a CONSTRUCTOR, but
4568 we still may have processing to do on the LHS. So
4569 pretend we didn't do anything here to let that happen. */
4570 return GS_UNHANDLED;
4574 /* If the target is volatile, we have non-zero elements and more than
4575 one field to assign, initialize the target from a temporary. */
4576 if (TREE_THIS_VOLATILE (object)
4577 && !TREE_ADDRESSABLE (type)
4578 && num_nonzero_elements > 0
4579 && vec_safe_length (elts) > 1)
4581 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4582 TREE_OPERAND (*expr_p, 0) = temp;
4583 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4584 *expr_p,
4585 build2 (MODIFY_EXPR, void_type_node,
4586 object, temp));
4587 return GS_OK;
4590 if (notify_temp_creation)
4591 return GS_OK;
4593 /* If there are nonzero elements and if needed, pre-evaluate to capture
4594 elements overlapping with the lhs into temporaries. We must do this
4595 before clearing to fetch the values before they are zeroed-out. */
4596 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4598 preeval_data.lhs_base_decl = get_base_address (object);
4599 if (!DECL_P (preeval_data.lhs_base_decl))
4600 preeval_data.lhs_base_decl = NULL;
4601 preeval_data.lhs_alias_set = get_alias_set (object);
4603 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4604 pre_p, post_p, &preeval_data);
4607 bool ctor_has_side_effects_p
4608 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4610 if (cleared)
4612 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4613 Note that we still have to gimplify, in order to handle the
4614 case of variable sized types. Avoid shared tree structures. */
4615 CONSTRUCTOR_ELTS (ctor) = NULL;
4616 TREE_SIDE_EFFECTS (ctor) = 0;
4617 object = unshare_expr (object);
4618 gimplify_stmt (expr_p, pre_p);
4621 /* If we have not block cleared the object, or if there are nonzero
4622 elements in the constructor, or if the constructor has side effects,
4623 add assignments to the individual scalar fields of the object. */
4624 if (!cleared
4625 || num_nonzero_elements > 0
4626 || ctor_has_side_effects_p)
4627 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4629 *expr_p = NULL_TREE;
4631 break;
4633 case COMPLEX_TYPE:
4635 tree r, i;
4637 if (notify_temp_creation)
4638 return GS_OK;
4640 /* Extract the real and imaginary parts out of the ctor. */
4641 gcc_assert (elts->length () == 2);
4642 r = (*elts)[0].value;
4643 i = (*elts)[1].value;
4644 if (r == NULL || i == NULL)
4646 tree zero = build_zero_cst (TREE_TYPE (type));
4647 if (r == NULL)
4648 r = zero;
4649 if (i == NULL)
4650 i = zero;
4653 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4654 represent creation of a complex value. */
4655 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4657 ctor = build_complex (type, r, i);
4658 TREE_OPERAND (*expr_p, 1) = ctor;
4660 else
4662 ctor = build2 (COMPLEX_EXPR, type, r, i);
4663 TREE_OPERAND (*expr_p, 1) = ctor;
4664 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4665 pre_p,
4666 post_p,
4667 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4668 fb_rvalue);
4671 break;
4673 case VECTOR_TYPE:
4675 unsigned HOST_WIDE_INT ix;
4676 constructor_elt *ce;
4678 if (notify_temp_creation)
4679 return GS_OK;
4681 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4682 if (TREE_CONSTANT (ctor))
4684 bool constant_p = true;
4685 tree value;
4687 /* Even when ctor is constant, it might contain non-*_CST
4688 elements, such as addresses or trapping values like
4689 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4690 in VECTOR_CST nodes. */
4691 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4692 if (!CONSTANT_CLASS_P (value))
4694 constant_p = false;
4695 break;
4698 if (constant_p)
4700 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4701 break;
4704 TREE_CONSTANT (ctor) = 0;
4707 /* Vector types use CONSTRUCTOR all the way through gimple
4708 compilation as a general initializer. */
4709 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4711 enum gimplify_status tret;
4712 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4713 fb_rvalue);
4714 if (tret == GS_ERROR)
4715 ret = GS_ERROR;
4716 else if (TREE_STATIC (ctor)
4717 && !initializer_constant_valid_p (ce->value,
4718 TREE_TYPE (ce->value)))
4719 TREE_STATIC (ctor) = 0;
4721 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4722 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4724 break;
4726 default:
4727 /* So how did we get a CONSTRUCTOR for a scalar type? */
4728 gcc_unreachable ();
4731 if (ret == GS_ERROR)
4732 return GS_ERROR;
4733 /* If we have gimplified both sides of the initializer but have
4734 not emitted an assignment, do so now. */
4735 if (*expr_p)
4737 tree lhs = TREE_OPERAND (*expr_p, 0);
4738 tree rhs = TREE_OPERAND (*expr_p, 1);
4739 gassign *init = gimple_build_assign (lhs, rhs);
4740 gimplify_seq_add_stmt (pre_p, init);
4742 if (want_value)
4744 *expr_p = object;
4745 return GS_OK;
4747 else
4749 *expr_p = NULL;
4750 return GS_ALL_DONE;
4754 /* Given a pointer value OP0, return a simplified version of an
4755 indirection through OP0, or NULL_TREE if no simplification is
4756 possible. This may only be applied to a rhs of an expression.
4757 Note that the resulting type may be different from the type pointed
4758 to in the sense that it is still compatible from the langhooks
4759 point of view. */
4761 static tree
4762 gimple_fold_indirect_ref_rhs (tree t)
4764 return gimple_fold_indirect_ref (t);
4767 /* Subroutine of gimplify_modify_expr to do simplifications of
4768 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4769 something changes. */
4771 static enum gimplify_status
4772 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4773 gimple_seq *pre_p, gimple_seq *post_p,
4774 bool want_value)
4776 enum gimplify_status ret = GS_UNHANDLED;
4777 bool changed;
4781 changed = false;
4782 switch (TREE_CODE (*from_p))
4784 case VAR_DECL:
4785 /* If we're assigning from a read-only variable initialized with
4786 a constructor, do the direct assignment from the constructor,
4787 but only if neither source nor target are volatile since this
4788 latter assignment might end up being done on a per-field basis. */
4789 if (DECL_INITIAL (*from_p)
4790 && TREE_READONLY (*from_p)
4791 && !TREE_THIS_VOLATILE (*from_p)
4792 && !TREE_THIS_VOLATILE (*to_p)
4793 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4795 tree old_from = *from_p;
4796 enum gimplify_status subret;
4798 /* Move the constructor into the RHS. */
4799 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4801 /* Let's see if gimplify_init_constructor will need to put
4802 it in memory. */
4803 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4804 false, true);
4805 if (subret == GS_ERROR)
4807 /* If so, revert the change. */
4808 *from_p = old_from;
4810 else
4812 ret = GS_OK;
4813 changed = true;
4816 break;
4817 case INDIRECT_REF:
4819 /* If we have code like
4821 *(const A*)(A*)&x
4823 where the type of "x" is a (possibly cv-qualified variant
4824 of "A"), treat the entire expression as identical to "x".
4825 This kind of code arises in C++ when an object is bound
4826 to a const reference, and if "x" is a TARGET_EXPR we want
4827 to take advantage of the optimization below. */
4828 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4829 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4830 if (t)
4832 if (TREE_THIS_VOLATILE (t) != volatile_p)
4834 if (DECL_P (t))
4835 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4836 build_fold_addr_expr (t));
4837 if (REFERENCE_CLASS_P (t))
4838 TREE_THIS_VOLATILE (t) = volatile_p;
4840 *from_p = t;
4841 ret = GS_OK;
4842 changed = true;
4844 break;
4847 case TARGET_EXPR:
4849 /* If we are initializing something from a TARGET_EXPR, strip the
4850 TARGET_EXPR and initialize it directly, if possible. This can't
4851 be done if the initializer is void, since that implies that the
4852 temporary is set in some non-trivial way.
4854 ??? What about code that pulls out the temp and uses it
4855 elsewhere? I think that such code never uses the TARGET_EXPR as
4856 an initializer. If I'm wrong, we'll die because the temp won't
4857 have any RTL. In that case, I guess we'll need to replace
4858 references somehow. */
4859 tree init = TARGET_EXPR_INITIAL (*from_p);
4861 if (init
4862 && !VOID_TYPE_P (TREE_TYPE (init)))
4864 *from_p = init;
4865 ret = GS_OK;
4866 changed = true;
4869 break;
4871 case COMPOUND_EXPR:
4872 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4873 caught. */
4874 gimplify_compound_expr (from_p, pre_p, true);
4875 ret = GS_OK;
4876 changed = true;
4877 break;
4879 case CONSTRUCTOR:
4880 /* If we already made some changes, let the front end have a
4881 crack at this before we break it down. */
4882 if (ret != GS_UNHANDLED)
4883 break;
4884 /* If we're initializing from a CONSTRUCTOR, break this into
4885 individual MODIFY_EXPRs. */
4886 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4887 false);
4889 case COND_EXPR:
4890 /* If we're assigning to a non-register type, push the assignment
4891 down into the branches. This is mandatory for ADDRESSABLE types,
4892 since we cannot generate temporaries for such, but it saves a
4893 copy in other cases as well. */
4894 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4896 /* This code should mirror the code in gimplify_cond_expr. */
4897 enum tree_code code = TREE_CODE (*expr_p);
4898 tree cond = *from_p;
4899 tree result = *to_p;
4901 ret = gimplify_expr (&result, pre_p, post_p,
4902 is_gimple_lvalue, fb_lvalue);
4903 if (ret != GS_ERROR)
4904 ret = GS_OK;
4906 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4907 TREE_OPERAND (cond, 1)
4908 = build2 (code, void_type_node, result,
4909 TREE_OPERAND (cond, 1));
4910 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4911 TREE_OPERAND (cond, 2)
4912 = build2 (code, void_type_node, unshare_expr (result),
4913 TREE_OPERAND (cond, 2));
4915 TREE_TYPE (cond) = void_type_node;
4916 recalculate_side_effects (cond);
4918 if (want_value)
4920 gimplify_and_add (cond, pre_p);
4921 *expr_p = unshare_expr (result);
4923 else
4924 *expr_p = cond;
4925 return ret;
4927 break;
4929 case CALL_EXPR:
4930 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4931 return slot so that we don't generate a temporary. */
4932 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4933 && aggregate_value_p (*from_p, *from_p))
4935 bool use_target;
4937 if (!(rhs_predicate_for (*to_p))(*from_p))
4938 /* If we need a temporary, *to_p isn't accurate. */
4939 use_target = false;
4940 /* It's OK to use the return slot directly unless it's an NRV. */
4941 else if (TREE_CODE (*to_p) == RESULT_DECL
4942 && DECL_NAME (*to_p) == NULL_TREE
4943 && needs_to_live_in_memory (*to_p))
4944 use_target = true;
4945 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4946 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4947 /* Don't force regs into memory. */
4948 use_target = false;
4949 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4950 /* It's OK to use the target directly if it's being
4951 initialized. */
4952 use_target = true;
4953 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
4954 != INTEGER_CST)
4955 /* Always use the target and thus RSO for variable-sized types.
4956 GIMPLE cannot deal with a variable-sized assignment
4957 embedded in a call statement. */
4958 use_target = true;
4959 else if (TREE_CODE (*to_p) != SSA_NAME
4960 && (!is_gimple_variable (*to_p)
4961 || needs_to_live_in_memory (*to_p)))
4962 /* Don't use the original target if it's already addressable;
4963 if its address escapes, and the called function uses the
4964 NRV optimization, a conforming program could see *to_p
4965 change before the called function returns; see c++/19317.
4966 When optimizing, the return_slot pass marks more functions
4967 as safe after we have escape info. */
4968 use_target = false;
4969 else
4970 use_target = true;
4972 if (use_target)
4974 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4975 mark_addressable (*to_p);
4978 break;
4980 case WITH_SIZE_EXPR:
4981 /* Likewise for calls that return an aggregate of non-constant size,
4982 since we would not be able to generate a temporary at all. */
4983 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4985 *from_p = TREE_OPERAND (*from_p, 0);
4986 /* We don't change ret in this case because the
4987 WITH_SIZE_EXPR might have been added in
4988 gimplify_modify_expr, so returning GS_OK would lead to an
4989 infinite loop. */
4990 changed = true;
4992 break;
4994 /* If we're initializing from a container, push the initialization
4995 inside it. */
4996 case CLEANUP_POINT_EXPR:
4997 case BIND_EXPR:
4998 case STATEMENT_LIST:
5000 tree wrap = *from_p;
5001 tree t;
5003 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
5004 fb_lvalue);
5005 if (ret != GS_ERROR)
5006 ret = GS_OK;
5008 t = voidify_wrapper_expr (wrap, *expr_p);
5009 gcc_assert (t == *expr_p);
5011 if (want_value)
5013 gimplify_and_add (wrap, pre_p);
5014 *expr_p = unshare_expr (*to_p);
5016 else
5017 *expr_p = wrap;
5018 return GS_OK;
5021 case COMPOUND_LITERAL_EXPR:
5023 tree complit = TREE_OPERAND (*expr_p, 1);
5024 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
5025 tree decl = DECL_EXPR_DECL (decl_s);
5026 tree init = DECL_INITIAL (decl);
5028 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
5029 into struct T x = { 0, 1, 2 } if the address of the
5030 compound literal has never been taken. */
5031 if (!TREE_ADDRESSABLE (complit)
5032 && !TREE_ADDRESSABLE (decl)
5033 && init)
5035 *expr_p = copy_node (*expr_p);
5036 TREE_OPERAND (*expr_p, 1) = init;
5037 return GS_OK;
5041 default:
5042 break;
5045 while (changed);
5047 return ret;
5051 /* Return true if T looks like a valid GIMPLE statement. */
5053 static bool
5054 is_gimple_stmt (tree t)
5056 const enum tree_code code = TREE_CODE (t);
5058 switch (code)
5060 case NOP_EXPR:
5061 /* The only valid NOP_EXPR is the empty statement. */
5062 return IS_EMPTY_STMT (t);
5064 case BIND_EXPR:
5065 case COND_EXPR:
5066 /* These are only valid if they're void. */
5067 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
5069 case SWITCH_EXPR:
5070 case GOTO_EXPR:
5071 case RETURN_EXPR:
5072 case LABEL_EXPR:
5073 case CASE_LABEL_EXPR:
5074 case TRY_CATCH_EXPR:
5075 case TRY_FINALLY_EXPR:
5076 case EH_FILTER_EXPR:
5077 case CATCH_EXPR:
5078 case ASM_EXPR:
5079 case STATEMENT_LIST:
5080 case OACC_PARALLEL:
5081 case OACC_KERNELS:
5082 case OACC_DATA:
5083 case OACC_HOST_DATA:
5084 case OACC_DECLARE:
5085 case OACC_UPDATE:
5086 case OACC_ENTER_DATA:
5087 case OACC_EXIT_DATA:
5088 case OACC_CACHE:
5089 case OMP_PARALLEL:
5090 case OMP_FOR:
5091 case OMP_SIMD:
5092 case CILK_SIMD:
5093 case OMP_DISTRIBUTE:
5094 case OACC_LOOP:
5095 case OMP_SECTIONS:
5096 case OMP_SECTION:
5097 case OMP_SINGLE:
5098 case OMP_MASTER:
5099 case OMP_TASKGROUP:
5100 case OMP_ORDERED:
5101 case OMP_CRITICAL:
5102 case OMP_TASK:
5103 case OMP_TARGET:
5104 case OMP_TARGET_DATA:
5105 case OMP_TARGET_UPDATE:
5106 case OMP_TARGET_ENTER_DATA:
5107 case OMP_TARGET_EXIT_DATA:
5108 case OMP_TASKLOOP:
5109 case OMP_TEAMS:
5110 /* These are always void. */
5111 return true;
5113 case CALL_EXPR:
5114 case MODIFY_EXPR:
5115 case PREDICT_EXPR:
5116 /* These are valid regardless of their type. */
5117 return true;
5119 default:
5120 return false;
5125 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
5126 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
5127 DECL_GIMPLE_REG_P set.
5129 IMPORTANT NOTE: This promotion is performed by introducing a load of the
5130 other, unmodified part of the complex object just before the total store.
5131 As a consequence, if the object is still uninitialized, an undefined value
5132 will be loaded into a register, which may result in a spurious exception
5133 if the register is floating-point and the value happens to be a signaling
5134 NaN for example. Then the fully-fledged complex operations lowering pass
5135 followed by a DCE pass are necessary in order to fix things up. */
5137 static enum gimplify_status
5138 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
5139 bool want_value)
5141 enum tree_code code, ocode;
5142 tree lhs, rhs, new_rhs, other, realpart, imagpart;
5144 lhs = TREE_OPERAND (*expr_p, 0);
5145 rhs = TREE_OPERAND (*expr_p, 1);
5146 code = TREE_CODE (lhs);
5147 lhs = TREE_OPERAND (lhs, 0);
5149 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
5150 other = build1 (ocode, TREE_TYPE (rhs), lhs);
5151 TREE_NO_WARNING (other) = 1;
5152 other = get_formal_tmp_var (other, pre_p);
5154 realpart = code == REALPART_EXPR ? rhs : other;
5155 imagpart = code == REALPART_EXPR ? other : rhs;
5157 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
5158 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
5159 else
5160 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
5162 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
5163 *expr_p = (want_value) ? rhs : NULL_TREE;
5165 return GS_ALL_DONE;
5168 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
5170 modify_expr
5171 : varname '=' rhs
5172 | '*' ID '=' rhs
5174 PRE_P points to the list where side effects that must happen before
5175 *EXPR_P should be stored.
5177 POST_P points to the list where side effects that must happen after
5178 *EXPR_P should be stored.
5180 WANT_VALUE is nonzero iff we want to use the value of this expression
5181 in another expression. */
5183 static enum gimplify_status
5184 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
5185 bool want_value)
5187 tree *from_p = &TREE_OPERAND (*expr_p, 1);
5188 tree *to_p = &TREE_OPERAND (*expr_p, 0);
5189 enum gimplify_status ret = GS_UNHANDLED;
5190 gimple *assign;
5191 location_t loc = EXPR_LOCATION (*expr_p);
5192 gimple_stmt_iterator gsi;
5194 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
5195 || TREE_CODE (*expr_p) == INIT_EXPR);
5197 /* Trying to simplify a clobber using normal logic doesn't work,
5198 so handle it here. */
5199 if (TREE_CLOBBER_P (*from_p))
5201 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5202 if (ret == GS_ERROR)
5203 return ret;
5204 gcc_assert (!want_value
5205 && (VAR_P (*to_p) || TREE_CODE (*to_p) == MEM_REF));
5206 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
5207 *expr_p = NULL;
5208 return GS_ALL_DONE;
5211 /* Insert pointer conversions required by the middle-end that are not
5212 required by the frontend. This fixes middle-end type checking for
5213 for example gcc.dg/redecl-6.c. */
5214 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
5216 STRIP_USELESS_TYPE_CONVERSION (*from_p);
5217 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
5218 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
5221 /* See if any simplifications can be done based on what the RHS is. */
5222 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5223 want_value);
5224 if (ret != GS_UNHANDLED)
5225 return ret;
5227 /* For zero sized types only gimplify the left hand side and right hand
5228 side as statements and throw away the assignment. Do this after
5229 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
5230 types properly. */
5231 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
5233 gimplify_stmt (from_p, pre_p);
5234 gimplify_stmt (to_p, pre_p);
5235 *expr_p = NULL_TREE;
5236 return GS_ALL_DONE;
5239 /* If the value being copied is of variable width, compute the length
5240 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
5241 before gimplifying any of the operands so that we can resolve any
5242 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
5243 the size of the expression to be copied, not of the destination, so
5244 that is what we must do here. */
5245 maybe_with_size_expr (from_p);
5247 /* As a special case, we have to temporarily allow for assignments
5248 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
5249 a toplevel statement, when gimplifying the GENERIC expression
5250 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
5251 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
5253 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
5254 prevent gimplify_expr from trying to create a new temporary for
5255 foo's LHS, we tell it that it should only gimplify until it
5256 reaches the CALL_EXPR. On return from gimplify_expr, the newly
5257 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
5258 and all we need to do here is set 'a' to be its LHS. */
5260 /* Gimplify the RHS first for C++17 and bug 71104. */
5261 gimple_predicate initial_pred = initial_rhs_predicate_for (*to_p);
5262 ret = gimplify_expr (from_p, pre_p, post_p, initial_pred, fb_rvalue);
5263 if (ret == GS_ERROR)
5264 return ret;
5266 /* Then gimplify the LHS. */
5267 /* If we gimplified the RHS to a CALL_EXPR and that call may return
5268 twice we have to make sure to gimplify into non-SSA as otherwise
5269 the abnormal edge added later will make those defs not dominate
5270 their uses.
5271 ??? Technically this applies only to the registers used in the
5272 resulting non-register *TO_P. */
5273 bool saved_into_ssa = gimplify_ctxp->into_ssa;
5274 if (saved_into_ssa
5275 && TREE_CODE (*from_p) == CALL_EXPR
5276 && call_expr_flags (*from_p) & ECF_RETURNS_TWICE)
5277 gimplify_ctxp->into_ssa = false;
5278 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
5279 gimplify_ctxp->into_ssa = saved_into_ssa;
5280 if (ret == GS_ERROR)
5281 return ret;
5283 /* Now that the LHS is gimplified, re-gimplify the RHS if our initial
5284 guess for the predicate was wrong. */
5285 gimple_predicate final_pred = rhs_predicate_for (*to_p);
5286 if (final_pred != initial_pred)
5288 ret = gimplify_expr (from_p, pre_p, post_p, final_pred, fb_rvalue);
5289 if (ret == GS_ERROR)
5290 return ret;
5293 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
5294 size as argument to the call. */
5295 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5297 tree call = TREE_OPERAND (*from_p, 0);
5298 tree vlasize = TREE_OPERAND (*from_p, 1);
5300 if (TREE_CODE (call) == CALL_EXPR
5301 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
5303 int nargs = call_expr_nargs (call);
5304 tree type = TREE_TYPE (call);
5305 tree ap = CALL_EXPR_ARG (call, 0);
5306 tree tag = CALL_EXPR_ARG (call, 1);
5307 tree aptag = CALL_EXPR_ARG (call, 2);
5308 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
5309 IFN_VA_ARG, type,
5310 nargs + 1, ap, tag,
5311 aptag, vlasize);
5312 TREE_OPERAND (*from_p, 0) = newcall;
5316 /* Now see if the above changed *from_p to something we handle specially. */
5317 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
5318 want_value);
5319 if (ret != GS_UNHANDLED)
5320 return ret;
5322 /* If we've got a variable sized assignment between two lvalues (i.e. does
5323 not involve a call), then we can make things a bit more straightforward
5324 by converting the assignment to memcpy or memset. */
5325 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
5327 tree from = TREE_OPERAND (*from_p, 0);
5328 tree size = TREE_OPERAND (*from_p, 1);
5330 if (TREE_CODE (from) == CONSTRUCTOR)
5331 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
5333 if (is_gimple_addressable (from))
5335 *from_p = from;
5336 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
5337 pre_p);
5341 /* Transform partial stores to non-addressable complex variables into
5342 total stores. This allows us to use real instead of virtual operands
5343 for these variables, which improves optimization. */
5344 if ((TREE_CODE (*to_p) == REALPART_EXPR
5345 || TREE_CODE (*to_p) == IMAGPART_EXPR)
5346 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
5347 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
5349 /* Try to alleviate the effects of the gimplification creating artificial
5350 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
5351 make sure not to create DECL_DEBUG_EXPR links across functions. */
5352 if (!gimplify_ctxp->into_ssa
5353 && VAR_P (*from_p)
5354 && DECL_IGNORED_P (*from_p)
5355 && DECL_P (*to_p)
5356 && !DECL_IGNORED_P (*to_p)
5357 && decl_function_context (*to_p) == current_function_decl)
5359 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
5360 DECL_NAME (*from_p)
5361 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
5362 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
5363 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
5366 if (want_value && TREE_THIS_VOLATILE (*to_p))
5367 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
5369 if (TREE_CODE (*from_p) == CALL_EXPR)
5371 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
5372 instead of a GIMPLE_ASSIGN. */
5373 gcall *call_stmt;
5374 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
5376 /* Gimplify internal functions created in the FEs. */
5377 int nargs = call_expr_nargs (*from_p), i;
5378 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
5379 auto_vec<tree> vargs (nargs);
5381 for (i = 0; i < nargs; i++)
5383 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
5384 EXPR_LOCATION (*from_p));
5385 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
5387 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
5388 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
5390 else
5392 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
5393 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
5394 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
5395 tree fndecl = get_callee_fndecl (*from_p);
5396 if (fndecl
5397 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
5398 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
5399 && call_expr_nargs (*from_p) == 3)
5400 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
5401 CALL_EXPR_ARG (*from_p, 0),
5402 CALL_EXPR_ARG (*from_p, 1),
5403 CALL_EXPR_ARG (*from_p, 2));
5404 else
5406 call_stmt = gimple_build_call_from_tree (*from_p);
5407 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
5410 notice_special_calls (call_stmt);
5411 if (!gimple_call_noreturn_p (call_stmt) || !should_remove_lhs_p (*to_p))
5412 gimple_call_set_lhs (call_stmt, *to_p);
5413 else if (TREE_CODE (*to_p) == SSA_NAME)
5414 /* The above is somewhat premature, avoid ICEing later for a
5415 SSA name w/o a definition. We may have uses in the GIMPLE IL.
5416 ??? This doesn't make it a default-def. */
5417 SSA_NAME_DEF_STMT (*to_p) = gimple_build_nop ();
5418 assign = call_stmt;
5420 else
5422 assign = gimple_build_assign (*to_p, *from_p);
5423 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
5424 if (COMPARISON_CLASS_P (*from_p))
5425 gimple_set_no_warning (assign, TREE_NO_WARNING (*from_p));
5428 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
5430 /* We should have got an SSA name from the start. */
5431 gcc_assert (TREE_CODE (*to_p) == SSA_NAME
5432 || ! gimple_in_ssa_p (cfun));
5435 gimplify_seq_add_stmt (pre_p, assign);
5436 gsi = gsi_last (*pre_p);
5437 maybe_fold_stmt (&gsi);
5439 if (want_value)
5441 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
5442 return GS_OK;
5444 else
5445 *expr_p = NULL;
5447 return GS_ALL_DONE;
5450 /* Gimplify a comparison between two variable-sized objects. Do this
5451 with a call to BUILT_IN_MEMCMP. */
5453 static enum gimplify_status
5454 gimplify_variable_sized_compare (tree *expr_p)
5456 location_t loc = EXPR_LOCATION (*expr_p);
5457 tree op0 = TREE_OPERAND (*expr_p, 0);
5458 tree op1 = TREE_OPERAND (*expr_p, 1);
5459 tree t, arg, dest, src, expr;
5461 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5462 arg = unshare_expr (arg);
5463 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5464 src = build_fold_addr_expr_loc (loc, op1);
5465 dest = build_fold_addr_expr_loc (loc, op0);
5466 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5467 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5469 expr
5470 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5471 SET_EXPR_LOCATION (expr, loc);
5472 *expr_p = expr;
5474 return GS_OK;
5477 /* Gimplify a comparison between two aggregate objects of integral scalar
5478 mode as a comparison between the bitwise equivalent scalar values. */
5480 static enum gimplify_status
5481 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5483 location_t loc = EXPR_LOCATION (*expr_p);
5484 tree op0 = TREE_OPERAND (*expr_p, 0);
5485 tree op1 = TREE_OPERAND (*expr_p, 1);
5487 tree type = TREE_TYPE (op0);
5488 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5490 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5491 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5493 *expr_p
5494 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5496 return GS_OK;
5499 /* Gimplify an expression sequence. This function gimplifies each
5500 expression and rewrites the original expression with the last
5501 expression of the sequence in GIMPLE form.
5503 PRE_P points to the list where the side effects for all the
5504 expressions in the sequence will be emitted.
5506 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5508 static enum gimplify_status
5509 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5511 tree t = *expr_p;
5515 tree *sub_p = &TREE_OPERAND (t, 0);
5517 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5518 gimplify_compound_expr (sub_p, pre_p, false);
5519 else
5520 gimplify_stmt (sub_p, pre_p);
5522 t = TREE_OPERAND (t, 1);
5524 while (TREE_CODE (t) == COMPOUND_EXPR);
5526 *expr_p = t;
5527 if (want_value)
5528 return GS_OK;
5529 else
5531 gimplify_stmt (expr_p, pre_p);
5532 return GS_ALL_DONE;
5536 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5537 gimplify. After gimplification, EXPR_P will point to a new temporary
5538 that holds the original value of the SAVE_EXPR node.
5540 PRE_P points to the list where side effects that must happen before
5541 *EXPR_P should be stored. */
5543 static enum gimplify_status
5544 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5546 enum gimplify_status ret = GS_ALL_DONE;
5547 tree val;
5549 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5550 val = TREE_OPERAND (*expr_p, 0);
5552 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5553 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5555 /* The operand may be a void-valued expression such as SAVE_EXPRs
5556 generated by the Java frontend for class initialization. It is
5557 being executed only for its side-effects. */
5558 if (TREE_TYPE (val) == void_type_node)
5560 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5561 is_gimple_stmt, fb_none);
5562 val = NULL;
5564 else
5565 /* The temporary may not be an SSA name as later abnormal and EH
5566 control flow may invalidate use/def domination. */
5567 val = get_initialized_tmp_var (val, pre_p, post_p, false);
5569 TREE_OPERAND (*expr_p, 0) = val;
5570 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5573 *expr_p = val;
5575 return ret;
5578 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5580 unary_expr
5581 : ...
5582 | '&' varname
5585 PRE_P points to the list where side effects that must happen before
5586 *EXPR_P should be stored.
5588 POST_P points to the list where side effects that must happen after
5589 *EXPR_P should be stored. */
5591 static enum gimplify_status
5592 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5594 tree expr = *expr_p;
5595 tree op0 = TREE_OPERAND (expr, 0);
5596 enum gimplify_status ret;
5597 location_t loc = EXPR_LOCATION (*expr_p);
5599 switch (TREE_CODE (op0))
5601 case INDIRECT_REF:
5602 do_indirect_ref:
5603 /* Check if we are dealing with an expression of the form '&*ptr'.
5604 While the front end folds away '&*ptr' into 'ptr', these
5605 expressions may be generated internally by the compiler (e.g.,
5606 builtins like __builtin_va_end). */
5607 /* Caution: the silent array decomposition semantics we allow for
5608 ADDR_EXPR means we can't always discard the pair. */
5609 /* Gimplification of the ADDR_EXPR operand may drop
5610 cv-qualification conversions, so make sure we add them if
5611 needed. */
5613 tree op00 = TREE_OPERAND (op0, 0);
5614 tree t_expr = TREE_TYPE (expr);
5615 tree t_op00 = TREE_TYPE (op00);
5617 if (!useless_type_conversion_p (t_expr, t_op00))
5618 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5619 *expr_p = op00;
5620 ret = GS_OK;
5622 break;
5624 case VIEW_CONVERT_EXPR:
5625 /* Take the address of our operand and then convert it to the type of
5626 this ADDR_EXPR.
5628 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5629 all clear. The impact of this transformation is even less clear. */
5631 /* If the operand is a useless conversion, look through it. Doing so
5632 guarantees that the ADDR_EXPR and its operand will remain of the
5633 same type. */
5634 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5635 op0 = TREE_OPERAND (op0, 0);
5637 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5638 build_fold_addr_expr_loc (loc,
5639 TREE_OPERAND (op0, 0)));
5640 ret = GS_OK;
5641 break;
5643 case MEM_REF:
5644 if (integer_zerop (TREE_OPERAND (op0, 1)))
5645 goto do_indirect_ref;
5647 /* fall through */
5649 default:
5650 /* If we see a call to a declared builtin or see its address
5651 being taken (we can unify those cases here) then we can mark
5652 the builtin for implicit generation by GCC. */
5653 if (TREE_CODE (op0) == FUNCTION_DECL
5654 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
5655 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
5656 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
5658 /* We use fb_either here because the C frontend sometimes takes
5659 the address of a call that returns a struct; see
5660 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5661 the implied temporary explicit. */
5663 /* Make the operand addressable. */
5664 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5665 is_gimple_addressable, fb_either);
5666 if (ret == GS_ERROR)
5667 break;
5669 /* Then mark it. Beware that it may not be possible to do so directly
5670 if a temporary has been created by the gimplification. */
5671 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5673 op0 = TREE_OPERAND (expr, 0);
5675 /* For various reasons, the gimplification of the expression
5676 may have made a new INDIRECT_REF. */
5677 if (TREE_CODE (op0) == INDIRECT_REF)
5678 goto do_indirect_ref;
5680 mark_addressable (TREE_OPERAND (expr, 0));
5682 /* The FEs may end up building ADDR_EXPRs early on a decl with
5683 an incomplete type. Re-build ADDR_EXPRs in canonical form
5684 here. */
5685 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5686 *expr_p = build_fold_addr_expr (op0);
5688 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5689 recompute_tree_invariant_for_addr_expr (*expr_p);
5691 /* If we re-built the ADDR_EXPR add a conversion to the original type
5692 if required. */
5693 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5694 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5696 break;
5699 return ret;
5702 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5703 value; output operands should be a gimple lvalue. */
5705 static enum gimplify_status
5706 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5708 tree expr;
5709 int noutputs;
5710 const char **oconstraints;
5711 int i;
5712 tree link;
5713 const char *constraint;
5714 bool allows_mem, allows_reg, is_inout;
5715 enum gimplify_status ret, tret;
5716 gasm *stmt;
5717 vec<tree, va_gc> *inputs;
5718 vec<tree, va_gc> *outputs;
5719 vec<tree, va_gc> *clobbers;
5720 vec<tree, va_gc> *labels;
5721 tree link_next;
5723 expr = *expr_p;
5724 noutputs = list_length (ASM_OUTPUTS (expr));
5725 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5727 inputs = NULL;
5728 outputs = NULL;
5729 clobbers = NULL;
5730 labels = NULL;
5732 ret = GS_ALL_DONE;
5733 link_next = NULL_TREE;
5734 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5736 bool ok;
5737 size_t constraint_len;
5739 link_next = TREE_CHAIN (link);
5741 oconstraints[i]
5742 = constraint
5743 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5744 constraint_len = strlen (constraint);
5745 if (constraint_len == 0)
5746 continue;
5748 ok = parse_output_constraint (&constraint, i, 0, 0,
5749 &allows_mem, &allows_reg, &is_inout);
5750 if (!ok)
5752 ret = GS_ERROR;
5753 is_inout = false;
5756 if (!allows_reg && allows_mem)
5757 mark_addressable (TREE_VALUE (link));
5759 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5760 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5761 fb_lvalue | fb_mayfail);
5762 if (tret == GS_ERROR)
5764 error ("invalid lvalue in asm output %d", i);
5765 ret = tret;
5768 /* If the constraint does not allow memory make sure we gimplify
5769 it to a register if it is not already but its base is. This
5770 happens for complex and vector components. */
5771 if (!allows_mem)
5773 tree op = TREE_VALUE (link);
5774 if (! is_gimple_val (op)
5775 && is_gimple_reg_type (TREE_TYPE (op))
5776 && is_gimple_reg (get_base_address (op)))
5778 tree tem = create_tmp_reg (TREE_TYPE (op));
5779 tree ass;
5780 if (is_inout)
5782 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem),
5783 tem, unshare_expr (op));
5784 gimplify_and_add (ass, pre_p);
5786 ass = build2 (MODIFY_EXPR, TREE_TYPE (tem), op, tem);
5787 gimplify_and_add (ass, post_p);
5789 TREE_VALUE (link) = tem;
5790 tret = GS_OK;
5794 vec_safe_push (outputs, link);
5795 TREE_CHAIN (link) = NULL_TREE;
5797 if (is_inout)
5799 /* An input/output operand. To give the optimizers more
5800 flexibility, split it into separate input and output
5801 operands. */
5802 tree input;
5803 /* Buffer big enough to format a 32-bit UINT_MAX into. */
5804 char buf[11];
5806 /* Turn the in/out constraint into an output constraint. */
5807 char *p = xstrdup (constraint);
5808 p[0] = '=';
5809 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5811 /* And add a matching input constraint. */
5812 if (allows_reg)
5814 sprintf (buf, "%u", i);
5816 /* If there are multiple alternatives in the constraint,
5817 handle each of them individually. Those that allow register
5818 will be replaced with operand number, the others will stay
5819 unchanged. */
5820 if (strchr (p, ',') != NULL)
5822 size_t len = 0, buflen = strlen (buf);
5823 char *beg, *end, *str, *dst;
5825 for (beg = p + 1;;)
5827 end = strchr (beg, ',');
5828 if (end == NULL)
5829 end = strchr (beg, '\0');
5830 if ((size_t) (end - beg) < buflen)
5831 len += buflen + 1;
5832 else
5833 len += end - beg + 1;
5834 if (*end)
5835 beg = end + 1;
5836 else
5837 break;
5840 str = (char *) alloca (len);
5841 for (beg = p + 1, dst = str;;)
5843 const char *tem;
5844 bool mem_p, reg_p, inout_p;
5846 end = strchr (beg, ',');
5847 if (end)
5848 *end = '\0';
5849 beg[-1] = '=';
5850 tem = beg - 1;
5851 parse_output_constraint (&tem, i, 0, 0,
5852 &mem_p, &reg_p, &inout_p);
5853 if (dst != str)
5854 *dst++ = ',';
5855 if (reg_p)
5857 memcpy (dst, buf, buflen);
5858 dst += buflen;
5860 else
5862 if (end)
5863 len = end - beg;
5864 else
5865 len = strlen (beg);
5866 memcpy (dst, beg, len);
5867 dst += len;
5869 if (end)
5870 beg = end + 1;
5871 else
5872 break;
5874 *dst = '\0';
5875 input = build_string (dst - str, str);
5877 else
5878 input = build_string (strlen (buf), buf);
5880 else
5881 input = build_string (constraint_len - 1, constraint + 1);
5883 free (p);
5885 input = build_tree_list (build_tree_list (NULL_TREE, input),
5886 unshare_expr (TREE_VALUE (link)));
5887 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5891 link_next = NULL_TREE;
5892 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5894 link_next = TREE_CHAIN (link);
5895 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5896 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5897 oconstraints, &allows_mem, &allows_reg);
5899 /* If we can't make copies, we can only accept memory. */
5900 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5902 if (allows_mem)
5903 allows_reg = 0;
5904 else
5906 error ("impossible constraint in %<asm%>");
5907 error ("non-memory input %d must stay in memory", i);
5908 return GS_ERROR;
5912 /* If the operand is a memory input, it should be an lvalue. */
5913 if (!allows_reg && allows_mem)
5915 tree inputv = TREE_VALUE (link);
5916 STRIP_NOPS (inputv);
5917 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5918 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5919 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5920 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
5921 || TREE_CODE (inputv) == MODIFY_EXPR)
5922 TREE_VALUE (link) = error_mark_node;
5923 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5924 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5925 if (tret != GS_ERROR)
5927 /* Unlike output operands, memory inputs are not guaranteed
5928 to be lvalues by the FE, and while the expressions are
5929 marked addressable there, if it is e.g. a statement
5930 expression, temporaries in it might not end up being
5931 addressable. They might be already used in the IL and thus
5932 it is too late to make them addressable now though. */
5933 tree x = TREE_VALUE (link);
5934 while (handled_component_p (x))
5935 x = TREE_OPERAND (x, 0);
5936 if (TREE_CODE (x) == MEM_REF
5937 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
5938 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
5939 if ((VAR_P (x)
5940 || TREE_CODE (x) == PARM_DECL
5941 || TREE_CODE (x) == RESULT_DECL)
5942 && !TREE_ADDRESSABLE (x)
5943 && is_gimple_reg (x))
5945 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
5946 input_location), 0,
5947 "memory input %d is not directly addressable",
5949 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
5952 mark_addressable (TREE_VALUE (link));
5953 if (tret == GS_ERROR)
5955 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
5956 "memory input %d is not directly addressable", i);
5957 ret = tret;
5960 else
5962 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5963 is_gimple_asm_val, fb_rvalue);
5964 if (tret == GS_ERROR)
5965 ret = tret;
5968 TREE_CHAIN (link) = NULL_TREE;
5969 vec_safe_push (inputs, link);
5972 link_next = NULL_TREE;
5973 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5975 link_next = TREE_CHAIN (link);
5976 TREE_CHAIN (link) = NULL_TREE;
5977 vec_safe_push (clobbers, link);
5980 link_next = NULL_TREE;
5981 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5983 link_next = TREE_CHAIN (link);
5984 TREE_CHAIN (link) = NULL_TREE;
5985 vec_safe_push (labels, link);
5988 /* Do not add ASMs with errors to the gimple IL stream. */
5989 if (ret != GS_ERROR)
5991 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5992 inputs, outputs, clobbers, labels);
5994 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
5995 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5997 gimplify_seq_add_stmt (pre_p, stmt);
6000 return ret;
6003 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
6004 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
6005 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
6006 return to this function.
6008 FIXME should we complexify the prequeue handling instead? Or use flags
6009 for all the cleanups and let the optimizer tighten them up? The current
6010 code seems pretty fragile; it will break on a cleanup within any
6011 non-conditional nesting. But any such nesting would be broken, anyway;
6012 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
6013 and continues out of it. We can do that at the RTL level, though, so
6014 having an optimizer to tighten up try/finally regions would be a Good
6015 Thing. */
6017 static enum gimplify_status
6018 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
6020 gimple_stmt_iterator iter;
6021 gimple_seq body_sequence = NULL;
6023 tree temp = voidify_wrapper_expr (*expr_p, NULL);
6025 /* We only care about the number of conditions between the innermost
6026 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
6027 any cleanups collected outside the CLEANUP_POINT_EXPR. */
6028 int old_conds = gimplify_ctxp->conditions;
6029 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
6030 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
6031 gimplify_ctxp->conditions = 0;
6032 gimplify_ctxp->conditional_cleanups = NULL;
6033 gimplify_ctxp->in_cleanup_point_expr = true;
6035 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
6037 gimplify_ctxp->conditions = old_conds;
6038 gimplify_ctxp->conditional_cleanups = old_cleanups;
6039 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
6041 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
6043 gimple *wce = gsi_stmt (iter);
6045 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
6047 if (gsi_one_before_end_p (iter))
6049 /* Note that gsi_insert_seq_before and gsi_remove do not
6050 scan operands, unlike some other sequence mutators. */
6051 if (!gimple_wce_cleanup_eh_only (wce))
6052 gsi_insert_seq_before_without_update (&iter,
6053 gimple_wce_cleanup (wce),
6054 GSI_SAME_STMT);
6055 gsi_remove (&iter, true);
6056 break;
6058 else
6060 gtry *gtry;
6061 gimple_seq seq;
6062 enum gimple_try_flags kind;
6064 if (gimple_wce_cleanup_eh_only (wce))
6065 kind = GIMPLE_TRY_CATCH;
6066 else
6067 kind = GIMPLE_TRY_FINALLY;
6068 seq = gsi_split_seq_after (iter);
6070 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
6071 /* Do not use gsi_replace here, as it may scan operands.
6072 We want to do a simple structural modification only. */
6073 gsi_set_stmt (&iter, gtry);
6074 iter = gsi_start (gtry->eval);
6077 else
6078 gsi_next (&iter);
6081 gimplify_seq_add_seq (pre_p, body_sequence);
6082 if (temp)
6084 *expr_p = temp;
6085 return GS_OK;
6087 else
6089 *expr_p = NULL;
6090 return GS_ALL_DONE;
6094 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
6095 is the cleanup action required. EH_ONLY is true if the cleanup should
6096 only be executed if an exception is thrown, not on normal exit. */
6098 static void
6099 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
6101 gimple *wce;
6102 gimple_seq cleanup_stmts = NULL;
6104 /* Errors can result in improperly nested cleanups. Which results in
6105 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
6106 if (seen_error ())
6107 return;
6109 if (gimple_conditional_context ())
6111 /* If we're in a conditional context, this is more complex. We only
6112 want to run the cleanup if we actually ran the initialization that
6113 necessitates it, but we want to run it after the end of the
6114 conditional context. So we wrap the try/finally around the
6115 condition and use a flag to determine whether or not to actually
6116 run the destructor. Thus
6118 test ? f(A()) : 0
6120 becomes (approximately)
6122 flag = 0;
6123 try {
6124 if (test) { A::A(temp); flag = 1; val = f(temp); }
6125 else { val = 0; }
6126 } finally {
6127 if (flag) A::~A(temp);
6131 tree flag = create_tmp_var (boolean_type_node, "cleanup");
6132 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
6133 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
6135 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
6136 gimplify_stmt (&cleanup, &cleanup_stmts);
6137 wce = gimple_build_wce (cleanup_stmts);
6139 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
6140 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
6141 gimplify_seq_add_stmt (pre_p, ftrue);
6143 /* Because of this manipulation, and the EH edges that jump
6144 threading cannot redirect, the temporary (VAR) will appear
6145 to be used uninitialized. Don't warn. */
6146 TREE_NO_WARNING (var) = 1;
6148 else
6150 gimplify_stmt (&cleanup, &cleanup_stmts);
6151 wce = gimple_build_wce (cleanup_stmts);
6152 gimple_wce_set_cleanup_eh_only (wce, eh_only);
6153 gimplify_seq_add_stmt (pre_p, wce);
6157 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
6159 static enum gimplify_status
6160 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
6162 tree targ = *expr_p;
6163 tree temp = TARGET_EXPR_SLOT (targ);
6164 tree init = TARGET_EXPR_INITIAL (targ);
6165 enum gimplify_status ret;
6167 if (init)
6169 tree cleanup = NULL_TREE;
6171 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
6172 to the temps list. Handle also variable length TARGET_EXPRs. */
6173 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
6175 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
6176 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
6177 gimplify_vla_decl (temp, pre_p);
6179 else
6180 gimple_add_tmp_var (temp);
6182 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
6183 expression is supposed to initialize the slot. */
6184 if (VOID_TYPE_P (TREE_TYPE (init)))
6185 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6186 else
6188 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
6189 init = init_expr;
6190 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
6191 init = NULL;
6192 ggc_free (init_expr);
6194 if (ret == GS_ERROR)
6196 /* PR c++/28266 Make sure this is expanded only once. */
6197 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6198 return GS_ERROR;
6200 if (init)
6201 gimplify_and_add (init, pre_p);
6203 /* If needed, push the cleanup for the temp. */
6204 if (TARGET_EXPR_CLEANUP (targ))
6206 if (CLEANUP_EH_ONLY (targ))
6207 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
6208 CLEANUP_EH_ONLY (targ), pre_p);
6209 else
6210 cleanup = TARGET_EXPR_CLEANUP (targ);
6213 /* Add a clobber for the temporary going out of scope, like
6214 gimplify_bind_expr. */
6215 if (gimplify_ctxp->in_cleanup_point_expr
6216 && needs_to_live_in_memory (temp)
6217 && flag_stack_reuse == SR_ALL)
6219 tree clobber = build_constructor (TREE_TYPE (temp),
6220 NULL);
6221 TREE_THIS_VOLATILE (clobber) = true;
6222 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
6223 if (cleanup)
6224 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
6225 clobber);
6226 else
6227 cleanup = clobber;
6230 if (cleanup)
6231 gimple_push_cleanup (temp, cleanup, false, pre_p);
6233 /* Only expand this once. */
6234 TREE_OPERAND (targ, 3) = init;
6235 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
6237 else
6238 /* We should have expanded this before. */
6239 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
6241 *expr_p = temp;
6242 return GS_OK;
6245 /* Gimplification of expression trees. */
6247 /* Gimplify an expression which appears at statement context. The
6248 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
6249 NULL, a new sequence is allocated.
6251 Return true if we actually added a statement to the queue. */
6253 bool
6254 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
6256 gimple_seq_node last;
6258 last = gimple_seq_last (*seq_p);
6259 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
6260 return last != gimple_seq_last (*seq_p);
6263 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
6264 to CTX. If entries already exist, force them to be some flavor of private.
6265 If there is no enclosing parallel, do nothing. */
6267 void
6268 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
6270 splay_tree_node n;
6272 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
6273 return;
6277 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6278 if (n != NULL)
6280 if (n->value & GOVD_SHARED)
6281 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
6282 else if (n->value & GOVD_MAP)
6283 n->value |= GOVD_MAP_TO_ONLY;
6284 else
6285 return;
6287 else if ((ctx->region_type & ORT_TARGET) != 0)
6289 if (ctx->target_map_scalars_firstprivate)
6290 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6291 else
6292 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
6294 else if (ctx->region_type != ORT_WORKSHARE
6295 && ctx->region_type != ORT_SIMD
6296 && ctx->region_type != ORT_ACC
6297 && !(ctx->region_type & ORT_TARGET_DATA))
6298 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
6300 ctx = ctx->outer_context;
6302 while (ctx);
6305 /* Similarly for each of the type sizes of TYPE. */
6307 static void
6308 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
6310 if (type == NULL || type == error_mark_node)
6311 return;
6312 type = TYPE_MAIN_VARIANT (type);
6314 if (ctx->privatized_types->add (type))
6315 return;
6317 switch (TREE_CODE (type))
6319 case INTEGER_TYPE:
6320 case ENUMERAL_TYPE:
6321 case BOOLEAN_TYPE:
6322 case REAL_TYPE:
6323 case FIXED_POINT_TYPE:
6324 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
6325 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
6326 break;
6328 case ARRAY_TYPE:
6329 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6330 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
6331 break;
6333 case RECORD_TYPE:
6334 case UNION_TYPE:
6335 case QUAL_UNION_TYPE:
6337 tree field;
6338 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6339 if (TREE_CODE (field) == FIELD_DECL)
6341 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
6342 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
6345 break;
6347 case POINTER_TYPE:
6348 case REFERENCE_TYPE:
6349 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
6350 break;
6352 default:
6353 break;
6356 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
6357 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
6358 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
6361 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
6363 static void
6364 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
6366 splay_tree_node n;
6367 unsigned int nflags;
6368 tree t;
6370 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
6371 return;
6373 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
6374 there are constructors involved somewhere. */
6375 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
6376 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
6377 flags |= GOVD_SEEN;
6379 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6380 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6382 /* We shouldn't be re-adding the decl with the same data
6383 sharing class. */
6384 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
6385 nflags = n->value | flags;
6386 /* The only combination of data sharing classes we should see is
6387 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
6388 reduction variables to be used in data sharing clauses. */
6389 gcc_assert ((ctx->region_type & ORT_ACC) != 0
6390 || ((nflags & GOVD_DATA_SHARE_CLASS)
6391 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
6392 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
6393 n->value = nflags;
6394 return;
6397 /* When adding a variable-sized variable, we have to handle all sorts
6398 of additional bits of data: the pointer replacement variable, and
6399 the parameters of the type. */
6400 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6402 /* Add the pointer replacement variable as PRIVATE if the variable
6403 replacement is private, else FIRSTPRIVATE since we'll need the
6404 address of the original variable either for SHARED, or for the
6405 copy into or out of the context. */
6406 if (!(flags & GOVD_LOCAL))
6408 if (flags & GOVD_MAP)
6409 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
6410 else if (flags & GOVD_PRIVATE)
6411 nflags = GOVD_PRIVATE;
6412 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6413 && (flags & GOVD_FIRSTPRIVATE))
6414 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
6415 else
6416 nflags = GOVD_FIRSTPRIVATE;
6417 nflags |= flags & GOVD_SEEN;
6418 t = DECL_VALUE_EXPR (decl);
6419 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6420 t = TREE_OPERAND (t, 0);
6421 gcc_assert (DECL_P (t));
6422 omp_add_variable (ctx, t, nflags);
6425 /* Add all of the variable and type parameters (which should have
6426 been gimplified to a formal temporary) as FIRSTPRIVATE. */
6427 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
6428 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
6429 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6431 /* The variable-sized variable itself is never SHARED, only some form
6432 of PRIVATE. The sharing would take place via the pointer variable
6433 which we remapped above. */
6434 if (flags & GOVD_SHARED)
6435 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
6436 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
6438 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
6439 alloca statement we generate for the variable, so make sure it
6440 is available. This isn't automatically needed for the SHARED
6441 case, since we won't be allocating local storage then.
6442 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
6443 in this case omp_notice_variable will be called later
6444 on when it is gimplified. */
6445 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
6446 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
6447 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
6449 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
6450 && lang_hooks.decls.omp_privatize_by_reference (decl))
6452 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
6454 /* Similar to the direct variable sized case above, we'll need the
6455 size of references being privatized. */
6456 if ((flags & GOVD_SHARED) == 0)
6458 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6459 if (DECL_P (t))
6460 omp_notice_variable (ctx, t, true);
6464 if (n != NULL)
6465 n->value |= flags;
6466 else
6467 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
6469 /* For reductions clauses in OpenACC loop directives, by default create a
6470 copy clause on the enclosing parallel construct for carrying back the
6471 results. */
6472 if (ctx->region_type == ORT_ACC && (flags & GOVD_REDUCTION))
6474 struct gimplify_omp_ctx *outer_ctx = ctx->outer_context;
6475 while (outer_ctx)
6477 n = splay_tree_lookup (outer_ctx->variables, (splay_tree_key)decl);
6478 if (n != NULL)
6480 /* Ignore local variables and explicitly declared clauses. */
6481 if (n->value & (GOVD_LOCAL | GOVD_EXPLICIT))
6482 break;
6483 else if (outer_ctx->region_type == ORT_ACC_KERNELS)
6485 /* According to the OpenACC spec, such a reduction variable
6486 should already have a copy map on a kernels construct,
6487 verify that here. */
6488 gcc_assert (!(n->value & GOVD_FIRSTPRIVATE)
6489 && (n->value & GOVD_MAP));
6491 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6493 /* Remove firstprivate and make it a copy map. */
6494 n->value &= ~GOVD_FIRSTPRIVATE;
6495 n->value |= GOVD_MAP;
6498 else if (outer_ctx->region_type == ORT_ACC_PARALLEL)
6500 splay_tree_insert (outer_ctx->variables, (splay_tree_key)decl,
6501 GOVD_MAP | GOVD_SEEN);
6502 break;
6504 outer_ctx = outer_ctx->outer_context;
6509 /* Notice a threadprivate variable DECL used in OMP context CTX.
6510 This just prints out diagnostics about threadprivate variable uses
6511 in untied tasks. If DECL2 is non-NULL, prevent this warning
6512 on that variable. */
6514 static bool
6515 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
6516 tree decl2)
6518 splay_tree_node n;
6519 struct gimplify_omp_ctx *octx;
6521 for (octx = ctx; octx; octx = octx->outer_context)
6522 if ((octx->region_type & ORT_TARGET) != 0)
6524 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
6525 if (n == NULL)
6527 error ("threadprivate variable %qE used in target region",
6528 DECL_NAME (decl));
6529 error_at (octx->location, "enclosing target region");
6530 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
6532 if (decl2)
6533 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
6536 if (ctx->region_type != ORT_UNTIED_TASK)
6537 return false;
6538 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6539 if (n == NULL)
6541 error ("threadprivate variable %qE used in untied task",
6542 DECL_NAME (decl));
6543 error_at (ctx->location, "enclosing task");
6544 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
6546 if (decl2)
6547 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
6548 return false;
6551 /* Return true if global var DECL is device resident. */
6553 static bool
6554 device_resident_p (tree decl)
6556 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
6558 if (!attr)
6559 return false;
6561 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
6563 tree c = TREE_VALUE (t);
6564 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
6565 return true;
6568 return false;
6571 /* Determine outer default flags for DECL mentioned in an OMP region
6572 but not declared in an enclosing clause.
6574 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
6575 remapped firstprivate instead of shared. To some extent this is
6576 addressed in omp_firstprivatize_type_sizes, but not
6577 effectively. */
6579 static unsigned
6580 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
6581 bool in_code, unsigned flags)
6583 enum omp_clause_default_kind default_kind = ctx->default_kind;
6584 enum omp_clause_default_kind kind;
6586 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
6587 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6588 default_kind = kind;
6590 switch (default_kind)
6592 case OMP_CLAUSE_DEFAULT_NONE:
6594 const char *rtype;
6596 if (ctx->region_type & ORT_PARALLEL)
6597 rtype = "parallel";
6598 else if (ctx->region_type & ORT_TASK)
6599 rtype = "task";
6600 else if (ctx->region_type & ORT_TEAMS)
6601 rtype = "teams";
6602 else
6603 gcc_unreachable ();
6605 error ("%qE not specified in enclosing %s",
6606 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
6607 error_at (ctx->location, "enclosing %s", rtype);
6609 /* FALLTHRU */
6610 case OMP_CLAUSE_DEFAULT_SHARED:
6611 flags |= GOVD_SHARED;
6612 break;
6613 case OMP_CLAUSE_DEFAULT_PRIVATE:
6614 flags |= GOVD_PRIVATE;
6615 break;
6616 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
6617 flags |= GOVD_FIRSTPRIVATE;
6618 break;
6619 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6620 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
6621 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
6622 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
6624 omp_notice_variable (octx, decl, in_code);
6625 for (; octx; octx = octx->outer_context)
6627 splay_tree_node n2;
6629 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
6630 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
6631 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
6632 continue;
6633 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
6635 flags |= GOVD_FIRSTPRIVATE;
6636 goto found_outer;
6638 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
6640 flags |= GOVD_SHARED;
6641 goto found_outer;
6646 if (TREE_CODE (decl) == PARM_DECL
6647 || (!is_global_var (decl)
6648 && DECL_CONTEXT (decl) == current_function_decl))
6649 flags |= GOVD_FIRSTPRIVATE;
6650 else
6651 flags |= GOVD_SHARED;
6652 found_outer:
6653 break;
6655 default:
6656 gcc_unreachable ();
6659 return flags;
6663 /* Determine outer default flags for DECL mentioned in an OACC region
6664 but not declared in an enclosing clause. */
6666 static unsigned
6667 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
6669 const char *rkind;
6670 bool on_device = false;
6671 tree type = TREE_TYPE (decl);
6673 if (lang_hooks.decls.omp_privatize_by_reference (decl))
6674 type = TREE_TYPE (type);
6676 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
6677 && is_global_var (decl)
6678 && device_resident_p (decl))
6680 on_device = true;
6681 flags |= GOVD_MAP_TO_ONLY;
6684 switch (ctx->region_type)
6686 default:
6687 gcc_unreachable ();
6689 case ORT_ACC_KERNELS:
6690 /* Scalars are default 'copy' under kernels, non-scalars are default
6691 'present_or_copy'. */
6692 flags |= GOVD_MAP;
6693 if (!AGGREGATE_TYPE_P (type))
6694 flags |= GOVD_MAP_FORCE;
6696 rkind = "kernels";
6697 break;
6699 case ORT_ACC_PARALLEL:
6701 if (on_device || AGGREGATE_TYPE_P (type))
6702 /* Aggregates default to 'present_or_copy'. */
6703 flags |= GOVD_MAP;
6704 else
6705 /* Scalars default to 'firstprivate'. */
6706 flags |= GOVD_FIRSTPRIVATE;
6707 rkind = "parallel";
6709 break;
6712 if (DECL_ARTIFICIAL (decl))
6713 ; /* We can get compiler-generated decls, and should not complain
6714 about them. */
6715 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
6717 error ("%qE not specified in enclosing OpenACC %qs construct",
6718 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
6719 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
6721 else
6722 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
6724 return flags;
6727 /* Record the fact that DECL was used within the OMP context CTX.
6728 IN_CODE is true when real code uses DECL, and false when we should
6729 merely emit default(none) errors. Return true if DECL is going to
6730 be remapped and thus DECL shouldn't be gimplified into its
6731 DECL_VALUE_EXPR (if any). */
6733 static bool
6734 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
6736 splay_tree_node n;
6737 unsigned flags = in_code ? GOVD_SEEN : 0;
6738 bool ret = false, shared;
6740 if (error_operand_p (decl))
6741 return false;
6743 if (ctx->region_type == ORT_NONE)
6744 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
6746 if (is_global_var (decl))
6748 /* Threadprivate variables are predetermined. */
6749 if (DECL_THREAD_LOCAL_P (decl))
6750 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
6752 if (DECL_HAS_VALUE_EXPR_P (decl))
6754 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6756 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
6757 return omp_notice_threadprivate_variable (ctx, decl, value);
6760 if (gimplify_omp_ctxp->outer_context == NULL
6761 && VAR_P (decl)
6762 && get_oacc_fn_attrib (current_function_decl))
6764 location_t loc = DECL_SOURCE_LOCATION (decl);
6766 if (lookup_attribute ("omp declare target link",
6767 DECL_ATTRIBUTES (decl)))
6769 error_at (loc,
6770 "%qE with %<link%> clause used in %<routine%> function",
6771 DECL_NAME (decl));
6772 return false;
6774 else if (!lookup_attribute ("omp declare target",
6775 DECL_ATTRIBUTES (decl)))
6777 error_at (loc,
6778 "%qE requires a %<declare%> directive for use "
6779 "in a %<routine%> function", DECL_NAME (decl));
6780 return false;
6785 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6786 if ((ctx->region_type & ORT_TARGET) != 0)
6788 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
6789 if (n == NULL)
6791 unsigned nflags = flags;
6792 if (ctx->target_map_pointers_as_0len_arrays
6793 || ctx->target_map_scalars_firstprivate)
6795 bool is_declare_target = false;
6796 bool is_scalar = false;
6797 if (is_global_var (decl)
6798 && varpool_node::get_create (decl)->offloadable)
6800 struct gimplify_omp_ctx *octx;
6801 for (octx = ctx->outer_context;
6802 octx; octx = octx->outer_context)
6804 n = splay_tree_lookup (octx->variables,
6805 (splay_tree_key)decl);
6806 if (n
6807 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
6808 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6809 break;
6811 is_declare_target = octx == NULL;
6813 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
6815 tree type = TREE_TYPE (decl);
6816 if (TREE_CODE (type) == REFERENCE_TYPE)
6817 type = TREE_TYPE (type);
6818 if (TREE_CODE (type) == COMPLEX_TYPE)
6819 type = TREE_TYPE (type);
6820 if (INTEGRAL_TYPE_P (type)
6821 || SCALAR_FLOAT_TYPE_P (type)
6822 || TREE_CODE (type) == POINTER_TYPE)
6823 is_scalar = true;
6825 if (is_declare_target)
6827 else if (ctx->target_map_pointers_as_0len_arrays
6828 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
6829 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
6830 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
6831 == POINTER_TYPE)))
6832 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
6833 else if (is_scalar)
6834 nflags |= GOVD_FIRSTPRIVATE;
6837 struct gimplify_omp_ctx *octx = ctx->outer_context;
6838 if ((ctx->region_type & ORT_ACC) && octx)
6840 /* Look in outer OpenACC contexts, to see if there's a
6841 data attribute for this variable. */
6842 omp_notice_variable (octx, decl, in_code);
6844 for (; octx; octx = octx->outer_context)
6846 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
6847 break;
6848 splay_tree_node n2
6849 = splay_tree_lookup (octx->variables,
6850 (splay_tree_key) decl);
6851 if (n2)
6853 if (octx->region_type == ORT_ACC_HOST_DATA)
6854 error ("variable %qE declared in enclosing "
6855 "%<host_data%> region", DECL_NAME (decl));
6856 nflags |= GOVD_MAP;
6857 if (octx->region_type == ORT_ACC_DATA
6858 && (n2->value & GOVD_MAP_0LEN_ARRAY))
6859 nflags |= GOVD_MAP_0LEN_ARRAY;
6860 goto found_outer;
6866 tree type = TREE_TYPE (decl);
6868 if (nflags == flags
6869 && gimplify_omp_ctxp->target_firstprivatize_array_bases
6870 && lang_hooks.decls.omp_privatize_by_reference (decl))
6871 type = TREE_TYPE (type);
6872 if (nflags == flags
6873 && !lang_hooks.types.omp_mappable_type (type))
6875 error ("%qD referenced in target region does not have "
6876 "a mappable type", decl);
6877 nflags |= GOVD_MAP | GOVD_EXPLICIT;
6879 else if (nflags == flags)
6881 if ((ctx->region_type & ORT_ACC) != 0)
6882 nflags = oacc_default_clause (ctx, decl, flags);
6883 else
6884 nflags |= GOVD_MAP;
6887 found_outer:
6888 omp_add_variable (ctx, decl, nflags);
6890 else
6892 /* If nothing changed, there's nothing left to do. */
6893 if ((n->value & flags) == flags)
6894 return ret;
6895 flags |= n->value;
6896 n->value = flags;
6898 goto do_outer;
6901 if (n == NULL)
6903 if (ctx->region_type == ORT_WORKSHARE
6904 || ctx->region_type == ORT_SIMD
6905 || ctx->region_type == ORT_ACC
6906 || (ctx->region_type & ORT_TARGET_DATA) != 0)
6907 goto do_outer;
6909 flags = omp_default_clause (ctx, decl, in_code, flags);
6911 if ((flags & GOVD_PRIVATE)
6912 && lang_hooks.decls.omp_private_outer_ref (decl))
6913 flags |= GOVD_PRIVATE_OUTER_REF;
6915 omp_add_variable (ctx, decl, flags);
6917 shared = (flags & GOVD_SHARED) != 0;
6918 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6919 goto do_outer;
6922 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6923 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6924 && DECL_SIZE (decl))
6926 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6928 splay_tree_node n2;
6929 tree t = DECL_VALUE_EXPR (decl);
6930 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6931 t = TREE_OPERAND (t, 0);
6932 gcc_assert (DECL_P (t));
6933 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6934 n2->value |= GOVD_SEEN;
6936 else if (lang_hooks.decls.omp_privatize_by_reference (decl)
6937 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
6938 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
6939 != INTEGER_CST))
6941 splay_tree_node n2;
6942 tree t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
6943 gcc_assert (DECL_P (t));
6944 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6945 if (n2)
6946 n2->value |= GOVD_SEEN;
6950 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6951 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6953 /* If nothing changed, there's nothing left to do. */
6954 if ((n->value & flags) == flags)
6955 return ret;
6956 flags |= n->value;
6957 n->value = flags;
6959 do_outer:
6960 /* If the variable is private in the current context, then we don't
6961 need to propagate anything to an outer context. */
6962 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6963 return ret;
6964 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6965 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6966 return ret;
6967 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6968 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6969 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6970 return ret;
6971 if (ctx->outer_context
6972 && omp_notice_variable (ctx->outer_context, decl, in_code))
6973 return true;
6974 return ret;
6977 /* Verify that DECL is private within CTX. If there's specific information
6978 to the contrary in the innermost scope, generate an error. */
6980 static bool
6981 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
6983 splay_tree_node n;
6985 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6986 if (n != NULL)
6988 if (n->value & GOVD_SHARED)
6990 if (ctx == gimplify_omp_ctxp)
6992 if (simd)
6993 error ("iteration variable %qE is predetermined linear",
6994 DECL_NAME (decl));
6995 else
6996 error ("iteration variable %qE should be private",
6997 DECL_NAME (decl));
6998 n->value = GOVD_PRIVATE;
6999 return true;
7001 else
7002 return false;
7004 else if ((n->value & GOVD_EXPLICIT) != 0
7005 && (ctx == gimplify_omp_ctxp
7006 || (ctx->region_type == ORT_COMBINED_PARALLEL
7007 && gimplify_omp_ctxp->outer_context == ctx)))
7009 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
7010 error ("iteration variable %qE should not be firstprivate",
7011 DECL_NAME (decl));
7012 else if ((n->value & GOVD_REDUCTION) != 0)
7013 error ("iteration variable %qE should not be reduction",
7014 DECL_NAME (decl));
7015 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
7016 error ("iteration variable %qE should not be linear",
7017 DECL_NAME (decl));
7018 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
7019 error ("iteration variable %qE should not be lastprivate",
7020 DECL_NAME (decl));
7021 else if (simd && (n->value & GOVD_PRIVATE) != 0)
7022 error ("iteration variable %qE should not be private",
7023 DECL_NAME (decl));
7024 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
7025 error ("iteration variable %qE is predetermined linear",
7026 DECL_NAME (decl));
7028 return (ctx == gimplify_omp_ctxp
7029 || (ctx->region_type == ORT_COMBINED_PARALLEL
7030 && gimplify_omp_ctxp->outer_context == ctx));
7033 if (ctx->region_type != ORT_WORKSHARE
7034 && ctx->region_type != ORT_SIMD
7035 && ctx->region_type != ORT_ACC)
7036 return false;
7037 else if (ctx->outer_context)
7038 return omp_is_private (ctx->outer_context, decl, simd);
7039 return false;
7042 /* Return true if DECL is private within a parallel region
7043 that binds to the current construct's context or in parallel
7044 region's REDUCTION clause. */
7046 static bool
7047 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
7049 splay_tree_node n;
7053 ctx = ctx->outer_context;
7054 if (ctx == NULL)
7056 if (is_global_var (decl))
7057 return false;
7059 /* References might be private, but might be shared too,
7060 when checking for copyprivate, assume they might be
7061 private, otherwise assume they might be shared. */
7062 if (copyprivate)
7063 return true;
7065 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7066 return false;
7068 /* Treat C++ privatized non-static data members outside
7069 of the privatization the same. */
7070 if (omp_member_access_dummy_var (decl))
7071 return false;
7073 return true;
7076 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7078 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
7079 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
7080 continue;
7082 if (n != NULL)
7084 if ((n->value & GOVD_LOCAL) != 0
7085 && omp_member_access_dummy_var (decl))
7086 return false;
7087 return (n->value & GOVD_SHARED) == 0;
7090 while (ctx->region_type == ORT_WORKSHARE
7091 || ctx->region_type == ORT_SIMD
7092 || ctx->region_type == ORT_ACC);
7093 return false;
7096 /* Return true if the CTX is combined with distribute and thus
7097 lastprivate can't be supported. */
7099 static bool
7100 omp_no_lastprivate (struct gimplify_omp_ctx *ctx)
7104 if (ctx->outer_context == NULL)
7105 return false;
7106 ctx = ctx->outer_context;
7107 switch (ctx->region_type)
7109 case ORT_WORKSHARE:
7110 if (!ctx->combined_loop)
7111 return false;
7112 if (ctx->distribute)
7113 return lang_GNU_Fortran ();
7114 break;
7115 case ORT_COMBINED_PARALLEL:
7116 break;
7117 case ORT_COMBINED_TEAMS:
7118 return lang_GNU_Fortran ();
7119 default:
7120 return false;
7123 while (1);
7126 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
7128 static tree
7129 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
7131 tree t = *tp;
7133 /* If this node has been visited, unmark it and keep looking. */
7134 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
7135 return t;
7137 if (IS_TYPE_OR_DECL_P (t))
7138 *walk_subtrees = 0;
7139 return NULL_TREE;
7142 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
7143 and previous omp contexts. */
7145 static void
7146 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
7147 enum omp_region_type region_type,
7148 enum tree_code code)
7150 struct gimplify_omp_ctx *ctx, *outer_ctx;
7151 tree c;
7152 hash_map<tree, tree> *struct_map_to_clause = NULL;
7153 tree *prev_list_p = NULL;
7155 ctx = new_omp_context (region_type);
7156 outer_ctx = ctx->outer_context;
7157 if (code == OMP_TARGET && !lang_GNU_Fortran ())
7159 ctx->target_map_pointers_as_0len_arrays = true;
7160 /* FIXME: For Fortran we want to set this too, when
7161 the Fortran FE is updated to OpenMP 4.5. */
7162 ctx->target_map_scalars_firstprivate = true;
7164 if (!lang_GNU_Fortran ())
7165 switch (code)
7167 case OMP_TARGET:
7168 case OMP_TARGET_DATA:
7169 case OMP_TARGET_ENTER_DATA:
7170 case OMP_TARGET_EXIT_DATA:
7171 case OACC_HOST_DATA:
7172 ctx->target_firstprivatize_array_bases = true;
7173 default:
7174 break;
7177 while ((c = *list_p) != NULL)
7179 bool remove = false;
7180 bool notice_outer = true;
7181 const char *check_non_private = NULL;
7182 unsigned int flags;
7183 tree decl;
7185 switch (OMP_CLAUSE_CODE (c))
7187 case OMP_CLAUSE_PRIVATE:
7188 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
7189 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
7191 flags |= GOVD_PRIVATE_OUTER_REF;
7192 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
7194 else
7195 notice_outer = false;
7196 goto do_add;
7197 case OMP_CLAUSE_SHARED:
7198 flags = GOVD_SHARED | GOVD_EXPLICIT;
7199 goto do_add;
7200 case OMP_CLAUSE_FIRSTPRIVATE:
7201 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7202 check_non_private = "firstprivate";
7203 goto do_add;
7204 case OMP_CLAUSE_LASTPRIVATE:
7205 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
7206 check_non_private = "lastprivate";
7207 decl = OMP_CLAUSE_DECL (c);
7208 if (omp_no_lastprivate (ctx))
7210 notice_outer = false;
7211 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7213 else if (error_operand_p (decl))
7214 goto do_add;
7215 else if (outer_ctx
7216 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
7217 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
7218 && splay_tree_lookup (outer_ctx->variables,
7219 (splay_tree_key) decl) == NULL)
7221 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
7222 if (outer_ctx->outer_context)
7223 omp_notice_variable (outer_ctx->outer_context, decl, true);
7225 else if (outer_ctx
7226 && (outer_ctx->region_type & ORT_TASK) != 0
7227 && outer_ctx->combined_loop
7228 && splay_tree_lookup (outer_ctx->variables,
7229 (splay_tree_key) decl) == NULL)
7231 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7232 if (outer_ctx->outer_context)
7233 omp_notice_variable (outer_ctx->outer_context, decl, true);
7235 else if (outer_ctx
7236 && (outer_ctx->region_type == ORT_WORKSHARE
7237 || outer_ctx->region_type == ORT_ACC)
7238 && outer_ctx->combined_loop
7239 && splay_tree_lookup (outer_ctx->variables,
7240 (splay_tree_key) decl) == NULL
7241 && !omp_check_private (outer_ctx, decl, false))
7243 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
7244 if (outer_ctx->outer_context
7245 && (outer_ctx->outer_context->region_type
7246 == ORT_COMBINED_PARALLEL)
7247 && splay_tree_lookup (outer_ctx->outer_context->variables,
7248 (splay_tree_key) decl) == NULL)
7250 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
7251 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
7252 if (octx->outer_context)
7253 omp_notice_variable (octx->outer_context, decl, true);
7255 else if (outer_ctx->outer_context)
7256 omp_notice_variable (outer_ctx->outer_context, decl, true);
7258 goto do_add;
7259 case OMP_CLAUSE_REDUCTION:
7260 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
7261 /* OpenACC permits reductions on private variables. */
7262 if (!(region_type & ORT_ACC))
7263 check_non_private = "reduction";
7264 decl = OMP_CLAUSE_DECL (c);
7265 if (TREE_CODE (decl) == MEM_REF)
7267 tree type = TREE_TYPE (decl);
7268 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
7269 NULL, is_gimple_val, fb_rvalue, false)
7270 == GS_ERROR)
7272 remove = true;
7273 break;
7275 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7276 if (DECL_P (v))
7278 omp_firstprivatize_variable (ctx, v);
7279 omp_notice_variable (ctx, v, true);
7281 decl = TREE_OPERAND (decl, 0);
7282 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
7284 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
7285 NULL, is_gimple_val, fb_rvalue, false)
7286 == GS_ERROR)
7288 remove = true;
7289 break;
7291 v = TREE_OPERAND (decl, 1);
7292 if (DECL_P (v))
7294 omp_firstprivatize_variable (ctx, v);
7295 omp_notice_variable (ctx, v, true);
7297 decl = TREE_OPERAND (decl, 0);
7299 if (TREE_CODE (decl) == ADDR_EXPR
7300 || TREE_CODE (decl) == INDIRECT_REF)
7301 decl = TREE_OPERAND (decl, 0);
7303 goto do_add_decl;
7304 case OMP_CLAUSE_LINEAR:
7305 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
7306 is_gimple_val, fb_rvalue) == GS_ERROR)
7308 remove = true;
7309 break;
7311 else
7313 if (code == OMP_SIMD
7314 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7316 struct gimplify_omp_ctx *octx = outer_ctx;
7317 if (octx
7318 && octx->region_type == ORT_WORKSHARE
7319 && octx->combined_loop
7320 && !octx->distribute)
7322 if (octx->outer_context
7323 && (octx->outer_context->region_type
7324 == ORT_COMBINED_PARALLEL))
7325 octx = octx->outer_context->outer_context;
7326 else
7327 octx = octx->outer_context;
7329 if (octx
7330 && octx->region_type == ORT_WORKSHARE
7331 && octx->combined_loop
7332 && octx->distribute
7333 && !lang_GNU_Fortran ())
7335 error_at (OMP_CLAUSE_LOCATION (c),
7336 "%<linear%> clause for variable other than "
7337 "loop iterator specified on construct "
7338 "combined with %<distribute%>");
7339 remove = true;
7340 break;
7343 /* For combined #pragma omp parallel for simd, need to put
7344 lastprivate and perhaps firstprivate too on the
7345 parallel. Similarly for #pragma omp for simd. */
7346 struct gimplify_omp_ctx *octx = outer_ctx;
7347 decl = NULL_TREE;
7348 if (omp_no_lastprivate (ctx))
7349 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
7352 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7353 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7354 break;
7355 decl = OMP_CLAUSE_DECL (c);
7356 if (error_operand_p (decl))
7358 decl = NULL_TREE;
7359 break;
7361 flags = GOVD_SEEN;
7362 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
7363 flags |= GOVD_FIRSTPRIVATE;
7364 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7365 flags |= GOVD_LASTPRIVATE;
7366 if (octx
7367 && octx->region_type == ORT_WORKSHARE
7368 && octx->combined_loop)
7370 if (octx->outer_context
7371 && (octx->outer_context->region_type
7372 == ORT_COMBINED_PARALLEL))
7373 octx = octx->outer_context;
7374 else if (omp_check_private (octx, decl, false))
7375 break;
7377 else if (octx
7378 && (octx->region_type & ORT_TASK) != 0
7379 && octx->combined_loop)
7381 else if (octx
7382 && octx->region_type == ORT_COMBINED_PARALLEL
7383 && ctx->region_type == ORT_WORKSHARE
7384 && octx == outer_ctx)
7385 flags = GOVD_SEEN | GOVD_SHARED;
7386 else if (octx
7387 && octx->region_type == ORT_COMBINED_TEAMS)
7388 flags = GOVD_SEEN | GOVD_SHARED;
7389 else if (octx
7390 && octx->region_type == ORT_COMBINED_TARGET)
7392 flags &= ~GOVD_LASTPRIVATE;
7393 if (flags == GOVD_SEEN)
7394 break;
7396 else
7397 break;
7398 splay_tree_node on
7399 = splay_tree_lookup (octx->variables,
7400 (splay_tree_key) decl);
7401 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
7403 octx = NULL;
7404 break;
7406 omp_add_variable (octx, decl, flags);
7407 if (octx->outer_context == NULL)
7408 break;
7409 octx = octx->outer_context;
7411 while (1);
7412 if (octx
7413 && decl
7414 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7415 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7416 omp_notice_variable (octx, decl, true);
7418 flags = GOVD_LINEAR | GOVD_EXPLICIT;
7419 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
7420 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7422 notice_outer = false;
7423 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7425 goto do_add;
7427 case OMP_CLAUSE_MAP:
7428 decl = OMP_CLAUSE_DECL (c);
7429 if (error_operand_p (decl))
7430 remove = true;
7431 switch (code)
7433 case OMP_TARGET:
7434 break;
7435 case OACC_DATA:
7436 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
7437 break;
7438 /* FALLTHRU */
7439 case OMP_TARGET_DATA:
7440 case OMP_TARGET_ENTER_DATA:
7441 case OMP_TARGET_EXIT_DATA:
7442 case OACC_ENTER_DATA:
7443 case OACC_EXIT_DATA:
7444 case OACC_HOST_DATA:
7445 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7446 || (OMP_CLAUSE_MAP_KIND (c)
7447 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7448 /* For target {,enter ,exit }data only the array slice is
7449 mapped, but not the pointer to it. */
7450 remove = true;
7451 break;
7452 default:
7453 break;
7455 if (remove)
7456 break;
7457 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
7459 struct gimplify_omp_ctx *octx;
7460 for (octx = outer_ctx; octx; octx = octx->outer_context)
7462 if (octx->region_type != ORT_ACC_HOST_DATA)
7463 break;
7464 splay_tree_node n2
7465 = splay_tree_lookup (octx->variables,
7466 (splay_tree_key) decl);
7467 if (n2)
7468 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
7469 "declared in enclosing %<host_data%> region",
7470 DECL_NAME (decl));
7473 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7474 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7475 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7476 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7477 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7479 remove = true;
7480 break;
7482 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
7483 || (OMP_CLAUSE_MAP_KIND (c)
7484 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7485 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
7487 OMP_CLAUSE_SIZE (c)
7488 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL,
7489 false);
7490 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
7491 GOVD_FIRSTPRIVATE | GOVD_SEEN);
7493 if (!DECL_P (decl))
7495 tree d = decl, *pd;
7496 if (TREE_CODE (d) == ARRAY_REF)
7498 while (TREE_CODE (d) == ARRAY_REF)
7499 d = TREE_OPERAND (d, 0);
7500 if (TREE_CODE (d) == COMPONENT_REF
7501 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
7502 decl = d;
7504 pd = &OMP_CLAUSE_DECL (c);
7505 if (d == decl
7506 && TREE_CODE (decl) == INDIRECT_REF
7507 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7508 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7509 == REFERENCE_TYPE))
7511 pd = &TREE_OPERAND (decl, 0);
7512 decl = TREE_OPERAND (decl, 0);
7514 if (TREE_CODE (decl) == COMPONENT_REF)
7516 while (TREE_CODE (decl) == COMPONENT_REF)
7517 decl = TREE_OPERAND (decl, 0);
7518 if (TREE_CODE (decl) == INDIRECT_REF
7519 && DECL_P (TREE_OPERAND (decl, 0))
7520 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7521 == REFERENCE_TYPE))
7522 decl = TREE_OPERAND (decl, 0);
7524 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
7525 == GS_ERROR)
7527 remove = true;
7528 break;
7530 if (DECL_P (decl))
7532 if (error_operand_p (decl))
7534 remove = true;
7535 break;
7538 tree stype = TREE_TYPE (decl);
7539 if (TREE_CODE (stype) == REFERENCE_TYPE)
7540 stype = TREE_TYPE (stype);
7541 if (TYPE_SIZE_UNIT (stype) == NULL
7542 || TREE_CODE (TYPE_SIZE_UNIT (stype)) != INTEGER_CST)
7544 error_at (OMP_CLAUSE_LOCATION (c),
7545 "mapping field %qE of variable length "
7546 "structure", OMP_CLAUSE_DECL (c));
7547 remove = true;
7548 break;
7551 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7553 /* Error recovery. */
7554 if (prev_list_p == NULL)
7556 remove = true;
7557 break;
7559 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7561 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
7562 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
7564 remove = true;
7565 break;
7570 tree offset;
7571 HOST_WIDE_INT bitsize, bitpos;
7572 machine_mode mode;
7573 int unsignedp, reversep, volatilep = 0;
7574 tree base = OMP_CLAUSE_DECL (c);
7575 while (TREE_CODE (base) == ARRAY_REF)
7576 base = TREE_OPERAND (base, 0);
7577 if (TREE_CODE (base) == INDIRECT_REF)
7578 base = TREE_OPERAND (base, 0);
7579 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7580 &mode, &unsignedp, &reversep,
7581 &volatilep);
7582 tree orig_base = base;
7583 if ((TREE_CODE (base) == INDIRECT_REF
7584 || (TREE_CODE (base) == MEM_REF
7585 && integer_zerop (TREE_OPERAND (base, 1))))
7586 && DECL_P (TREE_OPERAND (base, 0))
7587 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base, 0)))
7588 == REFERENCE_TYPE))
7589 base = TREE_OPERAND (base, 0);
7590 gcc_assert (base == decl
7591 && (offset == NULL_TREE
7592 || TREE_CODE (offset) == INTEGER_CST));
7594 splay_tree_node n
7595 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
7596 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
7597 == GOMP_MAP_ALWAYS_POINTER);
7598 if (n == NULL || (n->value & GOVD_MAP) == 0)
7600 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7601 OMP_CLAUSE_MAP);
7602 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
7603 if (orig_base != base)
7604 OMP_CLAUSE_DECL (l) = unshare_expr (orig_base);
7605 else
7606 OMP_CLAUSE_DECL (l) = decl;
7607 OMP_CLAUSE_SIZE (l) = size_int (1);
7608 if (struct_map_to_clause == NULL)
7609 struct_map_to_clause = new hash_map<tree, tree>;
7610 struct_map_to_clause->put (decl, l);
7611 if (ptr)
7613 enum gomp_map_kind mkind
7614 = code == OMP_TARGET_EXIT_DATA
7615 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
7616 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7617 OMP_CLAUSE_MAP);
7618 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
7619 OMP_CLAUSE_DECL (c2)
7620 = unshare_expr (OMP_CLAUSE_DECL (c));
7621 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
7622 OMP_CLAUSE_SIZE (c2)
7623 = TYPE_SIZE_UNIT (ptr_type_node);
7624 OMP_CLAUSE_CHAIN (l) = c2;
7625 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7627 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
7628 tree c3
7629 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7630 OMP_CLAUSE_MAP);
7631 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
7632 OMP_CLAUSE_DECL (c3)
7633 = unshare_expr (OMP_CLAUSE_DECL (c4));
7634 OMP_CLAUSE_SIZE (c3)
7635 = TYPE_SIZE_UNIT (ptr_type_node);
7636 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
7637 OMP_CLAUSE_CHAIN (c2) = c3;
7639 *prev_list_p = l;
7640 prev_list_p = NULL;
7642 else
7644 OMP_CLAUSE_CHAIN (l) = c;
7645 *list_p = l;
7646 list_p = &OMP_CLAUSE_CHAIN (l);
7648 if (orig_base != base && code == OMP_TARGET)
7650 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7651 OMP_CLAUSE_MAP);
7652 enum gomp_map_kind mkind
7653 = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
7654 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
7655 OMP_CLAUSE_DECL (c2) = decl;
7656 OMP_CLAUSE_SIZE (c2) = size_zero_node;
7657 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (l);
7658 OMP_CLAUSE_CHAIN (l) = c2;
7660 flags = GOVD_MAP | GOVD_EXPLICIT;
7661 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
7662 flags |= GOVD_SEEN;
7663 goto do_add_decl;
7665 else
7667 tree *osc = struct_map_to_clause->get (decl);
7668 tree *sc = NULL, *scp = NULL;
7669 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
7670 n->value |= GOVD_SEEN;
7671 offset_int o1, o2;
7672 if (offset)
7673 o1 = wi::to_offset (offset);
7674 else
7675 o1 = 0;
7676 if (bitpos)
7677 o1 = o1 + bitpos / BITS_PER_UNIT;
7678 sc = &OMP_CLAUSE_CHAIN (*osc);
7679 if (*sc != c
7680 && (OMP_CLAUSE_MAP_KIND (*sc)
7681 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7682 sc = &OMP_CLAUSE_CHAIN (*sc);
7683 for (; *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
7684 if (ptr && sc == prev_list_p)
7685 break;
7686 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
7687 != COMPONENT_REF
7688 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
7689 != INDIRECT_REF)
7690 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
7691 != ARRAY_REF))
7692 break;
7693 else
7695 tree offset2;
7696 HOST_WIDE_INT bitsize2, bitpos2;
7697 base = OMP_CLAUSE_DECL (*sc);
7698 if (TREE_CODE (base) == ARRAY_REF)
7700 while (TREE_CODE (base) == ARRAY_REF)
7701 base = TREE_OPERAND (base, 0);
7702 if (TREE_CODE (base) != COMPONENT_REF
7703 || (TREE_CODE (TREE_TYPE (base))
7704 != ARRAY_TYPE))
7705 break;
7707 else if (TREE_CODE (base) == INDIRECT_REF
7708 && (TREE_CODE (TREE_OPERAND (base, 0))
7709 == COMPONENT_REF)
7710 && (TREE_CODE (TREE_TYPE
7711 (TREE_OPERAND (base, 0)))
7712 == REFERENCE_TYPE))
7713 base = TREE_OPERAND (base, 0);
7714 base = get_inner_reference (base, &bitsize2,
7715 &bitpos2, &offset2,
7716 &mode, &unsignedp,
7717 &reversep, &volatilep);
7718 if ((TREE_CODE (base) == INDIRECT_REF
7719 || (TREE_CODE (base) == MEM_REF
7720 && integer_zerop (TREE_OPERAND (base,
7721 1))))
7722 && DECL_P (TREE_OPERAND (base, 0))
7723 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (base,
7724 0)))
7725 == REFERENCE_TYPE))
7726 base = TREE_OPERAND (base, 0);
7727 if (base != decl)
7728 break;
7729 if (scp)
7730 continue;
7731 gcc_assert (offset == NULL_TREE
7732 || TREE_CODE (offset) == INTEGER_CST);
7733 tree d1 = OMP_CLAUSE_DECL (*sc);
7734 tree d2 = OMP_CLAUSE_DECL (c);
7735 while (TREE_CODE (d1) == ARRAY_REF)
7736 d1 = TREE_OPERAND (d1, 0);
7737 while (TREE_CODE (d2) == ARRAY_REF)
7738 d2 = TREE_OPERAND (d2, 0);
7739 if (TREE_CODE (d1) == INDIRECT_REF)
7740 d1 = TREE_OPERAND (d1, 0);
7741 if (TREE_CODE (d2) == INDIRECT_REF)
7742 d2 = TREE_OPERAND (d2, 0);
7743 while (TREE_CODE (d1) == COMPONENT_REF)
7744 if (TREE_CODE (d2) == COMPONENT_REF
7745 && TREE_OPERAND (d1, 1)
7746 == TREE_OPERAND (d2, 1))
7748 d1 = TREE_OPERAND (d1, 0);
7749 d2 = TREE_OPERAND (d2, 0);
7751 else
7752 break;
7753 if (d1 == d2)
7755 error_at (OMP_CLAUSE_LOCATION (c),
7756 "%qE appears more than once in map "
7757 "clauses", OMP_CLAUSE_DECL (c));
7758 remove = true;
7759 break;
7761 if (offset2)
7762 o2 = wi::to_offset (offset2);
7763 else
7764 o2 = 0;
7765 if (bitpos2)
7766 o2 = o2 + bitpos2 / BITS_PER_UNIT;
7767 if (wi::ltu_p (o1, o2)
7768 || (wi::eq_p (o1, o2) && bitpos < bitpos2))
7770 if (ptr)
7771 scp = sc;
7772 else
7773 break;
7776 if (remove)
7777 break;
7778 OMP_CLAUSE_SIZE (*osc)
7779 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
7780 size_one_node);
7781 if (ptr)
7783 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7784 OMP_CLAUSE_MAP);
7785 tree cl = NULL_TREE;
7786 enum gomp_map_kind mkind
7787 = code == OMP_TARGET_EXIT_DATA
7788 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
7789 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
7790 OMP_CLAUSE_DECL (c2)
7791 = unshare_expr (OMP_CLAUSE_DECL (c));
7792 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
7793 OMP_CLAUSE_SIZE (c2)
7794 = TYPE_SIZE_UNIT (ptr_type_node);
7795 cl = scp ? *prev_list_p : c2;
7796 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7798 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
7799 tree c3
7800 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7801 OMP_CLAUSE_MAP);
7802 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
7803 OMP_CLAUSE_DECL (c3)
7804 = unshare_expr (OMP_CLAUSE_DECL (c4));
7805 OMP_CLAUSE_SIZE (c3)
7806 = TYPE_SIZE_UNIT (ptr_type_node);
7807 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
7808 if (!scp)
7809 OMP_CLAUSE_CHAIN (c2) = c3;
7810 else
7811 cl = c3;
7813 if (scp)
7814 *scp = c2;
7815 if (sc == prev_list_p)
7817 *sc = cl;
7818 prev_list_p = NULL;
7820 else
7822 *prev_list_p = OMP_CLAUSE_CHAIN (c);
7823 list_p = prev_list_p;
7824 prev_list_p = NULL;
7825 OMP_CLAUSE_CHAIN (c) = *sc;
7826 *sc = cl;
7827 continue;
7830 else if (*sc != c)
7832 *list_p = OMP_CLAUSE_CHAIN (c);
7833 OMP_CLAUSE_CHAIN (c) = *sc;
7834 *sc = c;
7835 continue;
7839 if (!remove
7840 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
7841 && OMP_CLAUSE_CHAIN (c)
7842 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
7843 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
7844 == GOMP_MAP_ALWAYS_POINTER))
7845 prev_list_p = list_p;
7846 break;
7848 flags = GOVD_MAP | GOVD_EXPLICIT;
7849 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
7850 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
7851 flags |= GOVD_MAP_ALWAYS_TO;
7852 goto do_add;
7854 case OMP_CLAUSE_DEPEND:
7855 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
7856 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
7858 /* Nothing to do. OMP_CLAUSE_DECL will be lowered in
7859 omp-low.c. */
7860 break;
7862 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
7864 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
7865 NULL, is_gimple_val, fb_rvalue);
7866 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
7868 if (error_operand_p (OMP_CLAUSE_DECL (c)))
7870 remove = true;
7871 break;
7873 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
7874 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
7875 is_gimple_val, fb_rvalue) == GS_ERROR)
7877 remove = true;
7878 break;
7880 break;
7882 case OMP_CLAUSE_TO:
7883 case OMP_CLAUSE_FROM:
7884 case OMP_CLAUSE__CACHE_:
7885 decl = OMP_CLAUSE_DECL (c);
7886 if (error_operand_p (decl))
7888 remove = true;
7889 break;
7891 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7892 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7893 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7894 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7895 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7897 remove = true;
7898 break;
7900 if (!DECL_P (decl))
7902 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
7903 NULL, is_gimple_lvalue, fb_lvalue)
7904 == GS_ERROR)
7906 remove = true;
7907 break;
7909 break;
7911 goto do_notice;
7913 case OMP_CLAUSE_USE_DEVICE_PTR:
7914 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7915 goto do_add;
7916 case OMP_CLAUSE_IS_DEVICE_PTR:
7917 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7918 goto do_add;
7920 do_add:
7921 decl = OMP_CLAUSE_DECL (c);
7922 do_add_decl:
7923 if (error_operand_p (decl))
7925 remove = true;
7926 break;
7928 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
7930 tree t = omp_member_access_dummy_var (decl);
7931 if (t)
7933 tree v = DECL_VALUE_EXPR (decl);
7934 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
7935 if (outer_ctx)
7936 omp_notice_variable (outer_ctx, t, true);
7939 if (code == OACC_DATA
7940 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7941 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7942 flags |= GOVD_MAP_0LEN_ARRAY;
7943 omp_add_variable (ctx, decl, flags);
7944 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
7945 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
7947 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
7948 GOVD_LOCAL | GOVD_SEEN);
7949 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
7950 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
7951 find_decl_expr,
7952 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
7953 NULL) == NULL_TREE)
7954 omp_add_variable (ctx,
7955 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
7956 GOVD_LOCAL | GOVD_SEEN);
7957 gimplify_omp_ctxp = ctx;
7958 push_gimplify_context ();
7960 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
7961 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
7963 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
7964 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
7965 pop_gimplify_context
7966 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
7967 push_gimplify_context ();
7968 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
7969 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
7970 pop_gimplify_context
7971 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
7972 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
7973 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
7975 gimplify_omp_ctxp = outer_ctx;
7977 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7978 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
7980 gimplify_omp_ctxp = ctx;
7981 push_gimplify_context ();
7982 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
7984 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
7985 NULL, NULL);
7986 TREE_SIDE_EFFECTS (bind) = 1;
7987 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
7988 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
7990 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
7991 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
7992 pop_gimplify_context
7993 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
7994 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
7996 gimplify_omp_ctxp = outer_ctx;
7998 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7999 && OMP_CLAUSE_LINEAR_STMT (c))
8001 gimplify_omp_ctxp = ctx;
8002 push_gimplify_context ();
8003 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
8005 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
8006 NULL, NULL);
8007 TREE_SIDE_EFFECTS (bind) = 1;
8008 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
8009 OMP_CLAUSE_LINEAR_STMT (c) = bind;
8011 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
8012 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
8013 pop_gimplify_context
8014 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
8015 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
8017 gimplify_omp_ctxp = outer_ctx;
8019 if (notice_outer)
8020 goto do_notice;
8021 break;
8023 case OMP_CLAUSE_COPYIN:
8024 case OMP_CLAUSE_COPYPRIVATE:
8025 decl = OMP_CLAUSE_DECL (c);
8026 if (error_operand_p (decl))
8028 remove = true;
8029 break;
8031 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
8032 && !remove
8033 && !omp_check_private (ctx, decl, true))
8035 remove = true;
8036 if (is_global_var (decl))
8038 if (DECL_THREAD_LOCAL_P (decl))
8039 remove = false;
8040 else if (DECL_HAS_VALUE_EXPR_P (decl))
8042 tree value = get_base_address (DECL_VALUE_EXPR (decl));
8044 if (value
8045 && DECL_P (value)
8046 && DECL_THREAD_LOCAL_P (value))
8047 remove = false;
8050 if (remove)
8051 error_at (OMP_CLAUSE_LOCATION (c),
8052 "copyprivate variable %qE is not threadprivate"
8053 " or private in outer context", DECL_NAME (decl));
8055 do_notice:
8056 if (outer_ctx)
8057 omp_notice_variable (outer_ctx, decl, true);
8058 if (check_non_private
8059 && region_type == ORT_WORKSHARE
8060 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
8061 || decl == OMP_CLAUSE_DECL (c)
8062 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
8063 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8064 == ADDR_EXPR
8065 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
8066 == POINTER_PLUS_EXPR
8067 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
8068 (OMP_CLAUSE_DECL (c), 0), 0))
8069 == ADDR_EXPR)))))
8070 && omp_check_private (ctx, decl, false))
8072 error ("%s variable %qE is private in outer context",
8073 check_non_private, DECL_NAME (decl));
8074 remove = true;
8076 break;
8078 case OMP_CLAUSE_IF:
8079 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
8080 && OMP_CLAUSE_IF_MODIFIER (c) != code)
8082 const char *p[2];
8083 for (int i = 0; i < 2; i++)
8084 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
8086 case OMP_PARALLEL: p[i] = "parallel"; break;
8087 case OMP_TASK: p[i] = "task"; break;
8088 case OMP_TASKLOOP: p[i] = "taskloop"; break;
8089 case OMP_TARGET_DATA: p[i] = "target data"; break;
8090 case OMP_TARGET: p[i] = "target"; break;
8091 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
8092 case OMP_TARGET_ENTER_DATA:
8093 p[i] = "target enter data"; break;
8094 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
8095 default: gcc_unreachable ();
8097 error_at (OMP_CLAUSE_LOCATION (c),
8098 "expected %qs %<if%> clause modifier rather than %qs",
8099 p[0], p[1]);
8100 remove = true;
8102 /* Fall through. */
8104 case OMP_CLAUSE_FINAL:
8105 OMP_CLAUSE_OPERAND (c, 0)
8106 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
8107 /* Fall through. */
8109 case OMP_CLAUSE_SCHEDULE:
8110 case OMP_CLAUSE_NUM_THREADS:
8111 case OMP_CLAUSE_NUM_TEAMS:
8112 case OMP_CLAUSE_THREAD_LIMIT:
8113 case OMP_CLAUSE_DIST_SCHEDULE:
8114 case OMP_CLAUSE_DEVICE:
8115 case OMP_CLAUSE_PRIORITY:
8116 case OMP_CLAUSE_GRAINSIZE:
8117 case OMP_CLAUSE_NUM_TASKS:
8118 case OMP_CLAUSE_HINT:
8119 case OMP_CLAUSE__CILK_FOR_COUNT_:
8120 case OMP_CLAUSE_ASYNC:
8121 case OMP_CLAUSE_WAIT:
8122 case OMP_CLAUSE_NUM_GANGS:
8123 case OMP_CLAUSE_NUM_WORKERS:
8124 case OMP_CLAUSE_VECTOR_LENGTH:
8125 case OMP_CLAUSE_WORKER:
8126 case OMP_CLAUSE_VECTOR:
8127 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8128 is_gimple_val, fb_rvalue) == GS_ERROR)
8129 remove = true;
8130 break;
8132 case OMP_CLAUSE_GANG:
8133 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
8134 is_gimple_val, fb_rvalue) == GS_ERROR)
8135 remove = true;
8136 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
8137 is_gimple_val, fb_rvalue) == GS_ERROR)
8138 remove = true;
8139 break;
8141 case OMP_CLAUSE_TILE:
8142 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
8143 list = TREE_CHAIN (list))
8145 if (gimplify_expr (&TREE_VALUE (list), pre_p, NULL,
8146 is_gimple_val, fb_rvalue) == GS_ERROR)
8147 remove = true;
8149 break;
8151 case OMP_CLAUSE_NOWAIT:
8152 case OMP_CLAUSE_ORDERED:
8153 case OMP_CLAUSE_UNTIED:
8154 case OMP_CLAUSE_COLLAPSE:
8155 case OMP_CLAUSE_AUTO:
8156 case OMP_CLAUSE_SEQ:
8157 case OMP_CLAUSE_INDEPENDENT:
8158 case OMP_CLAUSE_MERGEABLE:
8159 case OMP_CLAUSE_PROC_BIND:
8160 case OMP_CLAUSE_SAFELEN:
8161 case OMP_CLAUSE_SIMDLEN:
8162 case OMP_CLAUSE_NOGROUP:
8163 case OMP_CLAUSE_THREADS:
8164 case OMP_CLAUSE_SIMD:
8165 break;
8167 case OMP_CLAUSE_DEFAULTMAP:
8168 ctx->target_map_scalars_firstprivate = false;
8169 break;
8171 case OMP_CLAUSE_ALIGNED:
8172 decl = OMP_CLAUSE_DECL (c);
8173 if (error_operand_p (decl))
8175 remove = true;
8176 break;
8178 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
8179 is_gimple_val, fb_rvalue) == GS_ERROR)
8181 remove = true;
8182 break;
8184 if (!is_global_var (decl)
8185 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8186 omp_add_variable (ctx, decl, GOVD_ALIGNED);
8187 break;
8189 case OMP_CLAUSE_DEFAULT:
8190 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
8191 break;
8193 default:
8194 gcc_unreachable ();
8197 if (code == OACC_DATA
8198 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8199 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8200 remove = true;
8201 if (remove)
8202 *list_p = OMP_CLAUSE_CHAIN (c);
8203 else
8204 list_p = &OMP_CLAUSE_CHAIN (c);
8207 gimplify_omp_ctxp = ctx;
8208 if (struct_map_to_clause)
8209 delete struct_map_to_clause;
8212 /* Return true if DECL is a candidate for shared to firstprivate
8213 optimization. We only consider non-addressable scalars, not
8214 too big, and not references. */
8216 static bool
8217 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
8219 if (TREE_ADDRESSABLE (decl))
8220 return false;
8221 tree type = TREE_TYPE (decl);
8222 if (!is_gimple_reg_type (type)
8223 || TREE_CODE (type) == REFERENCE_TYPE
8224 || TREE_ADDRESSABLE (type))
8225 return false;
8226 /* Don't optimize too large decls, as each thread/task will have
8227 its own. */
8228 HOST_WIDE_INT len = int_size_in_bytes (type);
8229 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
8230 return false;
8231 if (lang_hooks.decls.omp_privatize_by_reference (decl))
8232 return false;
8233 return true;
8236 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
8237 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
8238 GOVD_WRITTEN in outer contexts. */
8240 static void
8241 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
8243 for (; ctx; ctx = ctx->outer_context)
8245 splay_tree_node n = splay_tree_lookup (ctx->variables,
8246 (splay_tree_key) decl);
8247 if (n == NULL)
8248 continue;
8249 else if (n->value & GOVD_SHARED)
8251 n->value |= GOVD_WRITTEN;
8252 return;
8254 else if (n->value & GOVD_DATA_SHARE_CLASS)
8255 return;
8259 /* Helper callback for walk_gimple_seq to discover possible stores
8260 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8261 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8262 for those. */
8264 static tree
8265 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
8267 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
8269 *walk_subtrees = 0;
8270 if (!wi->is_lhs)
8271 return NULL_TREE;
8273 tree op = *tp;
8276 if (handled_component_p (op))
8277 op = TREE_OPERAND (op, 0);
8278 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
8279 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
8280 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8281 else
8282 break;
8284 while (1);
8285 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
8286 return NULL_TREE;
8288 omp_mark_stores (gimplify_omp_ctxp, op);
8289 return NULL_TREE;
8292 /* Helper callback for walk_gimple_seq to discover possible stores
8293 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
8294 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
8295 for those. */
8297 static tree
8298 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
8299 bool *handled_ops_p,
8300 struct walk_stmt_info *wi)
8302 gimple *stmt = gsi_stmt (*gsi_p);
8303 switch (gimple_code (stmt))
8305 /* Don't recurse on OpenMP constructs for which
8306 gimplify_adjust_omp_clauses already handled the bodies,
8307 except handle gimple_omp_for_pre_body. */
8308 case GIMPLE_OMP_FOR:
8309 *handled_ops_p = true;
8310 if (gimple_omp_for_pre_body (stmt))
8311 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
8312 omp_find_stores_stmt, omp_find_stores_op, wi);
8313 break;
8314 case GIMPLE_OMP_PARALLEL:
8315 case GIMPLE_OMP_TASK:
8316 case GIMPLE_OMP_SECTIONS:
8317 case GIMPLE_OMP_SINGLE:
8318 case GIMPLE_OMP_TARGET:
8319 case GIMPLE_OMP_TEAMS:
8320 case GIMPLE_OMP_CRITICAL:
8321 *handled_ops_p = true;
8322 break;
8323 default:
8324 break;
8326 return NULL_TREE;
8329 struct gimplify_adjust_omp_clauses_data
8331 tree *list_p;
8332 gimple_seq *pre_p;
8335 /* For all variables that were not actually used within the context,
8336 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
8338 static int
8339 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
8341 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
8342 gimple_seq *pre_p
8343 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
8344 tree decl = (tree) n->key;
8345 unsigned flags = n->value;
8346 enum omp_clause_code code;
8347 tree clause;
8348 bool private_debug;
8350 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
8351 return 0;
8352 if ((flags & GOVD_SEEN) == 0)
8353 return 0;
8354 if (flags & GOVD_DEBUG_PRIVATE)
8356 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
8357 private_debug = true;
8359 else if (flags & GOVD_MAP)
8360 private_debug = false;
8361 else
8362 private_debug
8363 = lang_hooks.decls.omp_private_debug_clause (decl,
8364 !!(flags & GOVD_SHARED));
8365 if (private_debug)
8366 code = OMP_CLAUSE_PRIVATE;
8367 else if (flags & GOVD_MAP)
8369 code = OMP_CLAUSE_MAP;
8370 if ((gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8371 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8373 error ("%<_Atomic%> %qD in implicit %<map%> clause", decl);
8374 return 0;
8377 else if (flags & GOVD_SHARED)
8379 if (is_global_var (decl))
8381 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8382 while (ctx != NULL)
8384 splay_tree_node on
8385 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8386 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
8387 | GOVD_PRIVATE | GOVD_REDUCTION
8388 | GOVD_LINEAR | GOVD_MAP)) != 0)
8389 break;
8390 ctx = ctx->outer_context;
8392 if (ctx == NULL)
8393 return 0;
8395 code = OMP_CLAUSE_SHARED;
8397 else if (flags & GOVD_PRIVATE)
8398 code = OMP_CLAUSE_PRIVATE;
8399 else if (flags & GOVD_FIRSTPRIVATE)
8401 code = OMP_CLAUSE_FIRSTPRIVATE;
8402 if ((gimplify_omp_ctxp->region_type & ORT_TARGET)
8403 && (gimplify_omp_ctxp->region_type & ORT_ACC) == 0
8404 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (decl))))
8406 error ("%<_Atomic%> %qD in implicit %<firstprivate%> clause on "
8407 "%<target%> construct", decl);
8408 return 0;
8411 else if (flags & GOVD_LASTPRIVATE)
8412 code = OMP_CLAUSE_LASTPRIVATE;
8413 else if (flags & GOVD_ALIGNED)
8414 return 0;
8415 else
8416 gcc_unreachable ();
8418 if (((flags & GOVD_LASTPRIVATE)
8419 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
8420 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8421 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8423 tree chain = *list_p;
8424 clause = build_omp_clause (input_location, code);
8425 OMP_CLAUSE_DECL (clause) = decl;
8426 OMP_CLAUSE_CHAIN (clause) = chain;
8427 if (private_debug)
8428 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
8429 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
8430 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
8431 else if (code == OMP_CLAUSE_SHARED
8432 && (flags & GOVD_WRITTEN) == 0
8433 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8434 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
8435 else if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_EXPLICIT) == 0)
8436 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause) = 1;
8437 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
8439 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
8440 OMP_CLAUSE_DECL (nc) = decl;
8441 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
8442 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
8443 OMP_CLAUSE_DECL (clause)
8444 = build_simple_mem_ref_loc (input_location, decl);
8445 OMP_CLAUSE_DECL (clause)
8446 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
8447 build_int_cst (build_pointer_type (char_type_node), 0));
8448 OMP_CLAUSE_SIZE (clause) = size_zero_node;
8449 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8450 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
8451 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
8452 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8453 OMP_CLAUSE_CHAIN (nc) = chain;
8454 OMP_CLAUSE_CHAIN (clause) = nc;
8455 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8456 gimplify_omp_ctxp = ctx->outer_context;
8457 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
8458 pre_p, NULL, is_gimple_val, fb_rvalue);
8459 gimplify_omp_ctxp = ctx;
8461 else if (code == OMP_CLAUSE_MAP)
8463 int kind = (flags & GOVD_MAP_TO_ONLY
8464 ? GOMP_MAP_TO
8465 : GOMP_MAP_TOFROM);
8466 if (flags & GOVD_MAP_FORCE)
8467 kind |= GOMP_MAP_FLAG_FORCE;
8468 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
8469 if (DECL_SIZE (decl)
8470 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8472 tree decl2 = DECL_VALUE_EXPR (decl);
8473 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8474 decl2 = TREE_OPERAND (decl2, 0);
8475 gcc_assert (DECL_P (decl2));
8476 tree mem = build_simple_mem_ref (decl2);
8477 OMP_CLAUSE_DECL (clause) = mem;
8478 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8479 if (gimplify_omp_ctxp->outer_context)
8481 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
8482 omp_notice_variable (ctx, decl2, true);
8483 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
8485 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8486 OMP_CLAUSE_MAP);
8487 OMP_CLAUSE_DECL (nc) = decl;
8488 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8489 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
8490 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
8491 else
8492 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8493 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8494 OMP_CLAUSE_CHAIN (clause) = nc;
8496 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
8497 && lang_hooks.decls.omp_privatize_by_reference (decl))
8499 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
8500 OMP_CLAUSE_SIZE (clause)
8501 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
8502 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8503 gimplify_omp_ctxp = ctx->outer_context;
8504 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
8505 pre_p, NULL, is_gimple_val, fb_rvalue);
8506 gimplify_omp_ctxp = ctx;
8507 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
8508 OMP_CLAUSE_MAP);
8509 OMP_CLAUSE_DECL (nc) = decl;
8510 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8511 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8512 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
8513 OMP_CLAUSE_CHAIN (clause) = nc;
8515 else
8516 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
8518 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
8520 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
8521 OMP_CLAUSE_DECL (nc) = decl;
8522 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
8523 OMP_CLAUSE_CHAIN (nc) = chain;
8524 OMP_CLAUSE_CHAIN (clause) = nc;
8525 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8526 gimplify_omp_ctxp = ctx->outer_context;
8527 lang_hooks.decls.omp_finish_clause (nc, pre_p);
8528 gimplify_omp_ctxp = ctx;
8530 *list_p = clause;
8531 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8532 gimplify_omp_ctxp = ctx->outer_context;
8533 lang_hooks.decls.omp_finish_clause (clause, pre_p);
8534 if (gimplify_omp_ctxp)
8535 for (; clause != chain; clause = OMP_CLAUSE_CHAIN (clause))
8536 if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_MAP
8537 && DECL_P (OMP_CLAUSE_SIZE (clause)))
8538 omp_notice_variable (gimplify_omp_ctxp, OMP_CLAUSE_SIZE (clause),
8539 true);
8540 gimplify_omp_ctxp = ctx;
8541 return 0;
8544 static void
8545 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
8546 enum tree_code code)
8548 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8549 tree c, decl;
8551 if (body)
8553 struct gimplify_omp_ctx *octx;
8554 for (octx = ctx; octx; octx = octx->outer_context)
8555 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
8556 break;
8557 if (octx)
8559 struct walk_stmt_info wi;
8560 memset (&wi, 0, sizeof (wi));
8561 walk_gimple_seq (body, omp_find_stores_stmt,
8562 omp_find_stores_op, &wi);
8565 while ((c = *list_p) != NULL)
8567 splay_tree_node n;
8568 bool remove = false;
8570 switch (OMP_CLAUSE_CODE (c))
8572 case OMP_CLAUSE_FIRSTPRIVATE:
8573 if ((ctx->region_type & ORT_TARGET)
8574 && (ctx->region_type & ORT_ACC) == 0
8575 && TYPE_ATOMIC (strip_array_types
8576 (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
8578 error_at (OMP_CLAUSE_LOCATION (c),
8579 "%<_Atomic%> %qD in %<firstprivate%> clause on "
8580 "%<target%> construct", OMP_CLAUSE_DECL (c));
8581 remove = true;
8582 break;
8584 /* FALLTHRU */
8585 case OMP_CLAUSE_PRIVATE:
8586 case OMP_CLAUSE_SHARED:
8587 case OMP_CLAUSE_LINEAR:
8588 decl = OMP_CLAUSE_DECL (c);
8589 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8590 remove = !(n->value & GOVD_SEEN);
8591 if (! remove)
8593 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
8594 if ((n->value & GOVD_DEBUG_PRIVATE)
8595 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
8597 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
8598 || ((n->value & GOVD_DATA_SHARE_CLASS)
8599 == GOVD_PRIVATE));
8600 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
8601 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
8603 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8604 && (n->value & GOVD_WRITTEN) == 0
8605 && DECL_P (decl)
8606 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8607 OMP_CLAUSE_SHARED_READONLY (c) = 1;
8608 else if (DECL_P (decl)
8609 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8610 && (n->value & GOVD_WRITTEN) != 1)
8611 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8612 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
8613 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8614 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8616 break;
8618 case OMP_CLAUSE_LASTPRIVATE:
8619 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
8620 accurately reflect the presence of a FIRSTPRIVATE clause. */
8621 decl = OMP_CLAUSE_DECL (c);
8622 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8623 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
8624 = (n->value & GOVD_FIRSTPRIVATE) != 0;
8625 if (omp_no_lastprivate (ctx))
8627 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
8628 remove = true;
8629 else
8630 OMP_CLAUSE_CODE (c) = OMP_CLAUSE_PRIVATE;
8632 else if (code == OMP_DISTRIBUTE
8633 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
8635 remove = true;
8636 error_at (OMP_CLAUSE_LOCATION (c),
8637 "same variable used in %<firstprivate%> and "
8638 "%<lastprivate%> clauses on %<distribute%> "
8639 "construct");
8641 if (!remove
8642 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8643 && DECL_P (decl)
8644 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8645 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8646 break;
8648 case OMP_CLAUSE_ALIGNED:
8649 decl = OMP_CLAUSE_DECL (c);
8650 if (!is_global_var (decl))
8652 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8653 remove = n == NULL || !(n->value & GOVD_SEEN);
8654 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
8656 struct gimplify_omp_ctx *octx;
8657 if (n != NULL
8658 && (n->value & (GOVD_DATA_SHARE_CLASS
8659 & ~GOVD_FIRSTPRIVATE)))
8660 remove = true;
8661 else
8662 for (octx = ctx->outer_context; octx;
8663 octx = octx->outer_context)
8665 n = splay_tree_lookup (octx->variables,
8666 (splay_tree_key) decl);
8667 if (n == NULL)
8668 continue;
8669 if (n->value & GOVD_LOCAL)
8670 break;
8671 /* We have to avoid assigning a shared variable
8672 to itself when trying to add
8673 __builtin_assume_aligned. */
8674 if (n->value & GOVD_SHARED)
8676 remove = true;
8677 break;
8682 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8684 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8685 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
8686 remove = true;
8688 break;
8690 case OMP_CLAUSE_MAP:
8691 if (code == OMP_TARGET_EXIT_DATA
8692 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
8694 remove = true;
8695 break;
8697 decl = OMP_CLAUSE_DECL (c);
8698 /* Data clauses associated with acc parallel reductions must be
8699 compatible with present_or_copy. Warn and adjust the clause
8700 if that is not the case. */
8701 if (ctx->region_type == ORT_ACC_PARALLEL)
8703 tree t = DECL_P (decl) ? decl : TREE_OPERAND (decl, 0);
8704 n = NULL;
8706 if (DECL_P (t))
8707 n = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
8709 if (n && (n->value & GOVD_REDUCTION))
8711 enum gomp_map_kind kind = OMP_CLAUSE_MAP_KIND (c);
8713 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
8714 if ((kind & GOMP_MAP_TOFROM) != GOMP_MAP_TOFROM
8715 && kind != GOMP_MAP_FORCE_PRESENT
8716 && kind != GOMP_MAP_POINTER)
8718 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8719 "incompatible data clause with reduction "
8720 "on %qE; promoting to present_or_copy",
8721 DECL_NAME (t));
8722 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
8726 if (!DECL_P (decl))
8728 if ((ctx->region_type & ORT_TARGET) != 0
8729 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8731 if (TREE_CODE (decl) == INDIRECT_REF
8732 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
8733 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
8734 == REFERENCE_TYPE))
8735 decl = TREE_OPERAND (decl, 0);
8736 if (TREE_CODE (decl) == COMPONENT_REF)
8738 while (TREE_CODE (decl) == COMPONENT_REF)
8739 decl = TREE_OPERAND (decl, 0);
8740 if (DECL_P (decl))
8742 n = splay_tree_lookup (ctx->variables,
8743 (splay_tree_key) decl);
8744 if (!(n->value & GOVD_SEEN))
8745 remove = true;
8749 break;
8751 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8752 if ((ctx->region_type & ORT_TARGET) != 0
8753 && !(n->value & GOVD_SEEN)
8754 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
8755 && !lookup_attribute ("omp declare target link",
8756 DECL_ATTRIBUTES (decl)))
8758 remove = true;
8759 /* For struct element mapping, if struct is never referenced
8760 in target block and none of the mapping has always modifier,
8761 remove all the struct element mappings, which immediately
8762 follow the GOMP_MAP_STRUCT map clause. */
8763 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
8765 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
8766 while (cnt--)
8767 OMP_CLAUSE_CHAIN (c)
8768 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
8771 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
8772 && code == OMP_TARGET_EXIT_DATA)
8773 remove = true;
8774 else if (DECL_SIZE (decl)
8775 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
8776 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
8777 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
8778 && (OMP_CLAUSE_MAP_KIND (c)
8779 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8781 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
8782 for these, TREE_CODE (DECL_SIZE (decl)) will always be
8783 INTEGER_CST. */
8784 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
8786 tree decl2 = DECL_VALUE_EXPR (decl);
8787 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8788 decl2 = TREE_OPERAND (decl2, 0);
8789 gcc_assert (DECL_P (decl2));
8790 tree mem = build_simple_mem_ref (decl2);
8791 OMP_CLAUSE_DECL (c) = mem;
8792 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8793 if (ctx->outer_context)
8795 omp_notice_variable (ctx->outer_context, decl2, true);
8796 omp_notice_variable (ctx->outer_context,
8797 OMP_CLAUSE_SIZE (c), true);
8799 if (((ctx->region_type & ORT_TARGET) != 0
8800 || !ctx->target_firstprivatize_array_bases)
8801 && ((n->value & GOVD_SEEN) == 0
8802 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
8804 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8805 OMP_CLAUSE_MAP);
8806 OMP_CLAUSE_DECL (nc) = decl;
8807 OMP_CLAUSE_SIZE (nc) = size_zero_node;
8808 if (ctx->target_firstprivatize_array_bases)
8809 OMP_CLAUSE_SET_MAP_KIND (nc,
8810 GOMP_MAP_FIRSTPRIVATE_POINTER);
8811 else
8812 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
8813 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
8814 OMP_CLAUSE_CHAIN (c) = nc;
8815 c = nc;
8818 else
8820 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8821 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
8822 gcc_assert ((n->value & GOVD_SEEN) == 0
8823 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
8824 == 0));
8826 break;
8828 case OMP_CLAUSE_TO:
8829 case OMP_CLAUSE_FROM:
8830 case OMP_CLAUSE__CACHE_:
8831 decl = OMP_CLAUSE_DECL (c);
8832 if (!DECL_P (decl))
8833 break;
8834 if (DECL_SIZE (decl)
8835 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8837 tree decl2 = DECL_VALUE_EXPR (decl);
8838 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8839 decl2 = TREE_OPERAND (decl2, 0);
8840 gcc_assert (DECL_P (decl2));
8841 tree mem = build_simple_mem_ref (decl2);
8842 OMP_CLAUSE_DECL (c) = mem;
8843 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8844 if (ctx->outer_context)
8846 omp_notice_variable (ctx->outer_context, decl2, true);
8847 omp_notice_variable (ctx->outer_context,
8848 OMP_CLAUSE_SIZE (c), true);
8851 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8852 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
8853 break;
8855 case OMP_CLAUSE_REDUCTION:
8856 decl = OMP_CLAUSE_DECL (c);
8857 /* OpenACC reductions need a present_or_copy data clause.
8858 Add one if necessary. Error is the reduction is private. */
8859 if (ctx->region_type == ORT_ACC_PARALLEL)
8861 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
8862 if (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
8863 error_at (OMP_CLAUSE_LOCATION (c), "invalid private "
8864 "reduction on %qE", DECL_NAME (decl));
8865 else if ((n->value & GOVD_MAP) == 0)
8867 tree next = OMP_CLAUSE_CHAIN (c);
8868 tree nc = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_MAP);
8869 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_TOFROM);
8870 OMP_CLAUSE_DECL (nc) = decl;
8871 OMP_CLAUSE_CHAIN (c) = nc;
8872 lang_hooks.decls.omp_finish_clause (nc, pre_p);
8873 while (1)
8875 OMP_CLAUSE_MAP_IN_REDUCTION (nc) = 1;
8876 if (OMP_CLAUSE_CHAIN (nc) == NULL)
8877 break;
8878 nc = OMP_CLAUSE_CHAIN (nc);
8880 OMP_CLAUSE_CHAIN (nc) = next;
8881 n->value |= GOVD_MAP;
8884 if (DECL_P (decl)
8885 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8886 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8887 break;
8888 case OMP_CLAUSE_COPYIN:
8889 case OMP_CLAUSE_COPYPRIVATE:
8890 case OMP_CLAUSE_IF:
8891 case OMP_CLAUSE_NUM_THREADS:
8892 case OMP_CLAUSE_NUM_TEAMS:
8893 case OMP_CLAUSE_THREAD_LIMIT:
8894 case OMP_CLAUSE_DIST_SCHEDULE:
8895 case OMP_CLAUSE_DEVICE:
8896 case OMP_CLAUSE_SCHEDULE:
8897 case OMP_CLAUSE_NOWAIT:
8898 case OMP_CLAUSE_ORDERED:
8899 case OMP_CLAUSE_DEFAULT:
8900 case OMP_CLAUSE_UNTIED:
8901 case OMP_CLAUSE_COLLAPSE:
8902 case OMP_CLAUSE_FINAL:
8903 case OMP_CLAUSE_MERGEABLE:
8904 case OMP_CLAUSE_PROC_BIND:
8905 case OMP_CLAUSE_SAFELEN:
8906 case OMP_CLAUSE_SIMDLEN:
8907 case OMP_CLAUSE_DEPEND:
8908 case OMP_CLAUSE_PRIORITY:
8909 case OMP_CLAUSE_GRAINSIZE:
8910 case OMP_CLAUSE_NUM_TASKS:
8911 case OMP_CLAUSE_NOGROUP:
8912 case OMP_CLAUSE_THREADS:
8913 case OMP_CLAUSE_SIMD:
8914 case OMP_CLAUSE_HINT:
8915 case OMP_CLAUSE_DEFAULTMAP:
8916 case OMP_CLAUSE_USE_DEVICE_PTR:
8917 case OMP_CLAUSE_IS_DEVICE_PTR:
8918 case OMP_CLAUSE__CILK_FOR_COUNT_:
8919 case OMP_CLAUSE_ASYNC:
8920 case OMP_CLAUSE_WAIT:
8921 case OMP_CLAUSE_INDEPENDENT:
8922 case OMP_CLAUSE_NUM_GANGS:
8923 case OMP_CLAUSE_NUM_WORKERS:
8924 case OMP_CLAUSE_VECTOR_LENGTH:
8925 case OMP_CLAUSE_GANG:
8926 case OMP_CLAUSE_WORKER:
8927 case OMP_CLAUSE_VECTOR:
8928 case OMP_CLAUSE_AUTO:
8929 case OMP_CLAUSE_SEQ:
8930 break;
8932 case OMP_CLAUSE_TILE:
8933 /* We're not yet making use of the information provided by OpenACC
8934 tile clauses. Discard these here, to simplify later middle end
8935 processing. */
8936 remove = true;
8937 break;
8939 default:
8940 gcc_unreachable ();
8943 if (remove)
8944 *list_p = OMP_CLAUSE_CHAIN (c);
8945 else
8946 list_p = &OMP_CLAUSE_CHAIN (c);
8949 /* Add in any implicit data sharing. */
8950 struct gimplify_adjust_omp_clauses_data data;
8951 data.list_p = list_p;
8952 data.pre_p = pre_p;
8953 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
8955 gimplify_omp_ctxp = ctx->outer_context;
8956 delete_omp_context (ctx);
8959 /* Gimplify OACC_CACHE. */
8961 static void
8962 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
8964 tree expr = *expr_p;
8966 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
8967 OACC_CACHE);
8968 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
8969 OACC_CACHE);
8971 /* TODO: Do something sensible with this information. */
8973 *expr_p = NULL_TREE;
8976 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
8977 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
8978 kind. The entry kind will replace the one in CLAUSE, while the exit
8979 kind will be used in a new omp_clause and returned to the caller. */
8981 static tree
8982 gimplify_oacc_declare_1 (tree clause)
8984 HOST_WIDE_INT kind, new_op;
8985 bool ret = false;
8986 tree c = NULL;
8988 kind = OMP_CLAUSE_MAP_KIND (clause);
8990 switch (kind)
8992 case GOMP_MAP_ALLOC:
8993 case GOMP_MAP_FORCE_ALLOC:
8994 case GOMP_MAP_FORCE_TO:
8995 new_op = GOMP_MAP_DELETE;
8996 ret = true;
8997 break;
8999 case GOMP_MAP_FORCE_FROM:
9000 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9001 new_op = GOMP_MAP_FORCE_FROM;
9002 ret = true;
9003 break;
9005 case GOMP_MAP_FORCE_TOFROM:
9006 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_TO);
9007 new_op = GOMP_MAP_FORCE_FROM;
9008 ret = true;
9009 break;
9011 case GOMP_MAP_FROM:
9012 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
9013 new_op = GOMP_MAP_FROM;
9014 ret = true;
9015 break;
9017 case GOMP_MAP_TOFROM:
9018 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
9019 new_op = GOMP_MAP_FROM;
9020 ret = true;
9021 break;
9023 case GOMP_MAP_DEVICE_RESIDENT:
9024 case GOMP_MAP_FORCE_DEVICEPTR:
9025 case GOMP_MAP_FORCE_PRESENT:
9026 case GOMP_MAP_LINK:
9027 case GOMP_MAP_POINTER:
9028 case GOMP_MAP_TO:
9029 break;
9031 default:
9032 gcc_unreachable ();
9033 break;
9036 if (ret)
9038 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
9039 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
9040 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
9043 return c;
9046 /* Gimplify OACC_DECLARE. */
9048 static void
9049 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
9051 tree expr = *expr_p;
9052 gomp_target *stmt;
9053 tree clauses, t;
9055 clauses = OACC_DECLARE_CLAUSES (expr);
9057 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
9059 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
9061 tree decl = OMP_CLAUSE_DECL (t);
9063 if (TREE_CODE (decl) == MEM_REF)
9064 continue;
9066 if (VAR_P (decl)
9067 && !is_global_var (decl)
9068 && DECL_CONTEXT (decl) == current_function_decl)
9070 tree c = gimplify_oacc_declare_1 (t);
9071 if (c)
9073 if (oacc_declare_returns == NULL)
9074 oacc_declare_returns = new hash_map<tree, tree>;
9076 oacc_declare_returns->put (decl, c);
9080 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
9083 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
9084 clauses);
9086 gimplify_seq_add_stmt (pre_p, stmt);
9088 *expr_p = NULL_TREE;
9091 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
9092 gimplification of the body, as well as scanning the body for used
9093 variables. We need to do this scan now, because variable-sized
9094 decls will be decomposed during gimplification. */
9096 static void
9097 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
9099 tree expr = *expr_p;
9100 gimple *g;
9101 gimple_seq body = NULL;
9103 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
9104 OMP_PARALLEL_COMBINED (expr)
9105 ? ORT_COMBINED_PARALLEL
9106 : ORT_PARALLEL, OMP_PARALLEL);
9108 push_gimplify_context ();
9110 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
9111 if (gimple_code (g) == GIMPLE_BIND)
9112 pop_gimplify_context (g);
9113 else
9114 pop_gimplify_context (NULL);
9116 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
9117 OMP_PARALLEL);
9119 g = gimple_build_omp_parallel (body,
9120 OMP_PARALLEL_CLAUSES (expr),
9121 NULL_TREE, NULL_TREE);
9122 if (OMP_PARALLEL_COMBINED (expr))
9123 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
9124 gimplify_seq_add_stmt (pre_p, g);
9125 *expr_p = NULL_TREE;
9128 /* Gimplify the contents of an OMP_TASK statement. This involves
9129 gimplification of the body, as well as scanning the body for used
9130 variables. We need to do this scan now, because variable-sized
9131 decls will be decomposed during gimplification. */
9133 static void
9134 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
9136 tree expr = *expr_p;
9137 gimple *g;
9138 gimple_seq body = NULL;
9140 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
9141 find_omp_clause (OMP_TASK_CLAUSES (expr),
9142 OMP_CLAUSE_UNTIED)
9143 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
9145 push_gimplify_context ();
9147 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
9148 if (gimple_code (g) == GIMPLE_BIND)
9149 pop_gimplify_context (g);
9150 else
9151 pop_gimplify_context (NULL);
9153 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
9154 OMP_TASK);
9156 g = gimple_build_omp_task (body,
9157 OMP_TASK_CLAUSES (expr),
9158 NULL_TREE, NULL_TREE,
9159 NULL_TREE, NULL_TREE, NULL_TREE);
9160 gimplify_seq_add_stmt (pre_p, g);
9161 *expr_p = NULL_TREE;
9164 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
9165 with non-NULL OMP_FOR_INIT. */
9167 static tree
9168 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
9170 *walk_subtrees = 0;
9171 switch (TREE_CODE (*tp))
9173 case OMP_FOR:
9174 *walk_subtrees = 1;
9175 /* FALLTHRU */
9176 case OMP_SIMD:
9177 if (OMP_FOR_INIT (*tp) != NULL_TREE)
9178 return *tp;
9179 break;
9180 case BIND_EXPR:
9181 case STATEMENT_LIST:
9182 case OMP_PARALLEL:
9183 *walk_subtrees = 1;
9184 break;
9185 default:
9186 break;
9188 return NULL_TREE;
9191 /* Gimplify the gross structure of an OMP_FOR statement. */
9193 static enum gimplify_status
9194 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
9196 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
9197 enum gimplify_status ret = GS_ALL_DONE;
9198 enum gimplify_status tret;
9199 gomp_for *gfor;
9200 gimple_seq for_body, for_pre_body;
9201 int i;
9202 bitmap has_decl_expr = NULL;
9203 enum omp_region_type ort = ORT_WORKSHARE;
9205 orig_for_stmt = for_stmt = *expr_p;
9207 switch (TREE_CODE (for_stmt))
9209 case OMP_FOR:
9210 case CILK_FOR:
9211 case OMP_DISTRIBUTE:
9212 break;
9213 case OACC_LOOP:
9214 ort = ORT_ACC;
9215 break;
9216 case OMP_TASKLOOP:
9217 if (find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
9218 ort = ORT_UNTIED_TASK;
9219 else
9220 ort = ORT_TASK;
9221 break;
9222 case OMP_SIMD:
9223 case CILK_SIMD:
9224 ort = ORT_SIMD;
9225 break;
9226 default:
9227 gcc_unreachable ();
9230 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
9231 clause for the IV. */
9232 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9234 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
9235 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9236 decl = TREE_OPERAND (t, 0);
9237 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
9238 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9239 && OMP_CLAUSE_DECL (c) == decl)
9241 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9242 break;
9246 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9248 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
9249 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
9250 find_combined_omp_for, NULL, NULL);
9251 if (inner_for_stmt == NULL_TREE)
9253 gcc_assert (seen_error ());
9254 *expr_p = NULL_TREE;
9255 return GS_ERROR;
9259 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
9260 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
9261 TREE_CODE (for_stmt));
9263 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
9264 gimplify_omp_ctxp->distribute = true;
9266 /* Handle OMP_FOR_INIT. */
9267 for_pre_body = NULL;
9268 if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
9270 has_decl_expr = BITMAP_ALLOC (NULL);
9271 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
9272 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
9273 == VAR_DECL)
9275 t = OMP_FOR_PRE_BODY (for_stmt);
9276 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9278 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
9280 tree_stmt_iterator si;
9281 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
9282 tsi_next (&si))
9284 t = tsi_stmt (si);
9285 if (TREE_CODE (t) == DECL_EXPR
9286 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
9287 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
9291 if (OMP_FOR_PRE_BODY (for_stmt))
9293 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
9294 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9295 else
9297 struct gimplify_omp_ctx ctx;
9298 memset (&ctx, 0, sizeof (ctx));
9299 ctx.region_type = ORT_NONE;
9300 gimplify_omp_ctxp = &ctx;
9301 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
9302 gimplify_omp_ctxp = NULL;
9305 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
9307 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
9308 for_stmt = inner_for_stmt;
9310 /* For taskloop, need to gimplify the start, end and step before the
9311 taskloop, outside of the taskloop omp context. */
9312 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9314 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9316 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9317 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9319 TREE_OPERAND (t, 1)
9320 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9321 pre_p, NULL, false);
9322 tree c = build_omp_clause (input_location,
9323 OMP_CLAUSE_FIRSTPRIVATE);
9324 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9325 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9326 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9329 /* Handle OMP_FOR_COND. */
9330 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9331 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
9333 TREE_OPERAND (t, 1)
9334 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
9335 gimple_seq_empty_p (for_pre_body)
9336 ? pre_p : &for_pre_body, NULL,
9337 false);
9338 tree c = build_omp_clause (input_location,
9339 OMP_CLAUSE_FIRSTPRIVATE);
9340 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
9341 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9342 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9345 /* Handle OMP_FOR_INCR. */
9346 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9347 if (TREE_CODE (t) == MODIFY_EXPR)
9349 decl = TREE_OPERAND (t, 0);
9350 t = TREE_OPERAND (t, 1);
9351 tree *tp = &TREE_OPERAND (t, 1);
9352 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
9353 tp = &TREE_OPERAND (t, 0);
9355 if (!is_gimple_constant (*tp))
9357 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
9358 ? pre_p : &for_pre_body;
9359 *tp = get_initialized_tmp_var (*tp, seq, NULL, false);
9360 tree c = build_omp_clause (input_location,
9361 OMP_CLAUSE_FIRSTPRIVATE);
9362 OMP_CLAUSE_DECL (c) = *tp;
9363 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
9364 OMP_FOR_CLAUSES (orig_for_stmt) = c;
9369 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
9370 OMP_TASKLOOP);
9373 if (orig_for_stmt != for_stmt)
9374 gimplify_omp_ctxp->combined_loop = true;
9376 for_body = NULL;
9377 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9378 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
9379 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9380 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
9382 tree c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
9383 bool is_doacross = false;
9384 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
9386 is_doacross = true;
9387 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
9388 (OMP_FOR_INIT (for_stmt))
9389 * 2);
9391 int collapse = 1;
9392 c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
9393 if (c)
9394 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9395 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9397 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9398 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9399 decl = TREE_OPERAND (t, 0);
9400 gcc_assert (DECL_P (decl));
9401 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
9402 || POINTER_TYPE_P (TREE_TYPE (decl)));
9403 if (is_doacross)
9405 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
9406 gimplify_omp_ctxp->loop_iter_var.quick_push
9407 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
9408 else
9409 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9410 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
9413 /* Make sure the iteration variable is private. */
9414 tree c = NULL_TREE;
9415 tree c2 = NULL_TREE;
9416 if (orig_for_stmt != for_stmt)
9417 /* Do this only on innermost construct for combined ones. */;
9418 else if (ort == ORT_SIMD)
9420 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9421 (splay_tree_key) decl);
9422 omp_is_private (gimplify_omp_ctxp, decl,
9423 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
9424 != 1));
9425 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
9426 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9427 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9429 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9430 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
9431 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
9432 if ((has_decl_expr
9433 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9434 || omp_no_lastprivate (gimplify_omp_ctxp))
9436 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9437 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9439 struct gimplify_omp_ctx *outer
9440 = gimplify_omp_ctxp->outer_context;
9441 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9443 if (outer->region_type == ORT_WORKSHARE
9444 && outer->combined_loop)
9446 n = splay_tree_lookup (outer->variables,
9447 (splay_tree_key)decl);
9448 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9450 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9451 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9453 else
9455 struct gimplify_omp_ctx *octx = outer->outer_context;
9456 if (octx
9457 && octx->region_type == ORT_COMBINED_PARALLEL
9458 && octx->outer_context
9459 && (octx->outer_context->region_type
9460 == ORT_WORKSHARE)
9461 && octx->outer_context->combined_loop)
9463 octx = octx->outer_context;
9464 n = splay_tree_lookup (octx->variables,
9465 (splay_tree_key)decl);
9466 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9468 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
9469 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
9476 OMP_CLAUSE_DECL (c) = decl;
9477 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9478 OMP_FOR_CLAUSES (for_stmt) = c;
9479 omp_add_variable (gimplify_omp_ctxp, decl, flags);
9480 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
9482 if (outer->region_type == ORT_WORKSHARE
9483 && outer->combined_loop)
9485 if (outer->outer_context
9486 && (outer->outer_context->region_type
9487 == ORT_COMBINED_PARALLEL))
9488 outer = outer->outer_context;
9489 else if (omp_check_private (outer, decl, false))
9490 outer = NULL;
9492 else if (((outer->region_type & ORT_TASK) != 0)
9493 && outer->combined_loop
9494 && !omp_check_private (gimplify_omp_ctxp,
9495 decl, false))
9497 else if (outer->region_type != ORT_COMBINED_PARALLEL)
9499 omp_notice_variable (outer, decl, true);
9500 outer = NULL;
9502 if (outer)
9504 n = splay_tree_lookup (outer->variables,
9505 (splay_tree_key)decl);
9506 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9508 omp_add_variable (outer, decl,
9509 GOVD_LASTPRIVATE | GOVD_SEEN);
9510 if (outer->region_type == ORT_COMBINED_PARALLEL
9511 && outer->outer_context
9512 && (outer->outer_context->region_type
9513 == ORT_WORKSHARE)
9514 && outer->outer_context->combined_loop)
9516 outer = outer->outer_context;
9517 n = splay_tree_lookup (outer->variables,
9518 (splay_tree_key)decl);
9519 if (omp_check_private (outer, decl, false))
9520 outer = NULL;
9521 else if (n == NULL
9522 || ((n->value & GOVD_DATA_SHARE_CLASS)
9523 == 0))
9524 omp_add_variable (outer, decl,
9525 GOVD_LASTPRIVATE
9526 | GOVD_SEEN);
9527 else
9528 outer = NULL;
9530 if (outer && outer->outer_context
9531 && (outer->outer_context->region_type
9532 == ORT_COMBINED_TEAMS))
9534 outer = outer->outer_context;
9535 n = splay_tree_lookup (outer->variables,
9536 (splay_tree_key)decl);
9537 if (n == NULL
9538 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9539 omp_add_variable (outer, decl,
9540 GOVD_SHARED | GOVD_SEEN);
9541 else
9542 outer = NULL;
9544 if (outer && outer->outer_context)
9545 omp_notice_variable (outer->outer_context, decl,
9546 true);
9551 else
9553 bool lastprivate
9554 = (!has_decl_expr
9555 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
9556 && !omp_no_lastprivate (gimplify_omp_ctxp);
9557 struct gimplify_omp_ctx *outer
9558 = gimplify_omp_ctxp->outer_context;
9559 if (outer && lastprivate)
9561 if (outer->region_type == ORT_WORKSHARE
9562 && outer->combined_loop)
9564 n = splay_tree_lookup (outer->variables,
9565 (splay_tree_key)decl);
9566 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
9568 lastprivate = false;
9569 outer = NULL;
9571 else if (outer->outer_context
9572 && (outer->outer_context->region_type
9573 == ORT_COMBINED_PARALLEL))
9574 outer = outer->outer_context;
9575 else if (omp_check_private (outer, decl, false))
9576 outer = NULL;
9578 else if (((outer->region_type & ORT_TASK) != 0)
9579 && outer->combined_loop
9580 && !omp_check_private (gimplify_omp_ctxp,
9581 decl, false))
9583 else if (outer->region_type != ORT_COMBINED_PARALLEL)
9585 omp_notice_variable (outer, decl, true);
9586 outer = NULL;
9588 if (outer)
9590 n = splay_tree_lookup (outer->variables,
9591 (splay_tree_key)decl);
9592 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9594 omp_add_variable (outer, decl,
9595 GOVD_LASTPRIVATE | GOVD_SEEN);
9596 if (outer->region_type == ORT_COMBINED_PARALLEL
9597 && outer->outer_context
9598 && (outer->outer_context->region_type
9599 == ORT_WORKSHARE)
9600 && outer->outer_context->combined_loop)
9602 outer = outer->outer_context;
9603 n = splay_tree_lookup (outer->variables,
9604 (splay_tree_key)decl);
9605 if (omp_check_private (outer, decl, false))
9606 outer = NULL;
9607 else if (n == NULL
9608 || ((n->value & GOVD_DATA_SHARE_CLASS)
9609 == 0))
9610 omp_add_variable (outer, decl,
9611 GOVD_LASTPRIVATE
9612 | GOVD_SEEN);
9613 else
9614 outer = NULL;
9616 if (outer && outer->outer_context
9617 && (outer->outer_context->region_type
9618 == ORT_COMBINED_TEAMS))
9620 outer = outer->outer_context;
9621 n = splay_tree_lookup (outer->variables,
9622 (splay_tree_key)decl);
9623 if (n == NULL
9624 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
9625 omp_add_variable (outer, decl,
9626 GOVD_SHARED | GOVD_SEEN);
9627 else
9628 outer = NULL;
9630 if (outer && outer->outer_context)
9631 omp_notice_variable (outer->outer_context, decl,
9632 true);
9637 c = build_omp_clause (input_location,
9638 lastprivate ? OMP_CLAUSE_LASTPRIVATE
9639 : OMP_CLAUSE_PRIVATE);
9640 OMP_CLAUSE_DECL (c) = decl;
9641 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
9642 OMP_FOR_CLAUSES (for_stmt) = c;
9643 omp_add_variable (gimplify_omp_ctxp, decl,
9644 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
9645 | GOVD_EXPLICIT | GOVD_SEEN);
9646 c = NULL_TREE;
9649 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
9650 omp_notice_variable (gimplify_omp_ctxp, decl, true);
9651 else
9652 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
9654 /* If DECL is not a gimple register, create a temporary variable to act
9655 as an iteration counter. This is valid, since DECL cannot be
9656 modified in the body of the loop. Similarly for any iteration vars
9657 in simd with collapse > 1 where the iterator vars must be
9658 lastprivate. */
9659 if (orig_for_stmt != for_stmt)
9660 var = decl;
9661 else if (!is_gimple_reg (decl)
9662 || (ort == ORT_SIMD
9663 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
9665 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9666 /* Make sure omp_add_variable is not called on it prematurely.
9667 We call it ourselves a few lines later. */
9668 gimplify_omp_ctxp = NULL;
9669 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
9670 gimplify_omp_ctxp = ctx;
9671 TREE_OPERAND (t, 0) = var;
9673 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
9675 if (ort == ORT_SIMD
9676 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
9678 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
9679 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
9680 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
9681 OMP_CLAUSE_DECL (c2) = var;
9682 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
9683 OMP_FOR_CLAUSES (for_stmt) = c2;
9684 omp_add_variable (gimplify_omp_ctxp, var,
9685 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
9686 if (c == NULL_TREE)
9688 c = c2;
9689 c2 = NULL_TREE;
9692 else
9693 omp_add_variable (gimplify_omp_ctxp, var,
9694 GOVD_PRIVATE | GOVD_SEEN);
9696 else
9697 var = decl;
9699 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
9700 is_gimple_val, fb_rvalue, false);
9701 ret = MIN (ret, tret);
9702 if (ret == GS_ERROR)
9703 return ret;
9705 /* Handle OMP_FOR_COND. */
9706 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9707 gcc_assert (COMPARISON_CLASS_P (t));
9708 gcc_assert (TREE_OPERAND (t, 0) == decl);
9710 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
9711 is_gimple_val, fb_rvalue, false);
9712 ret = MIN (ret, tret);
9714 /* Handle OMP_FOR_INCR. */
9715 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9716 switch (TREE_CODE (t))
9718 case PREINCREMENT_EXPR:
9719 case POSTINCREMENT_EXPR:
9721 tree decl = TREE_OPERAND (t, 0);
9722 /* c_omp_for_incr_canonicalize_ptr() should have been
9723 called to massage things appropriately. */
9724 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
9726 if (orig_for_stmt != for_stmt)
9727 break;
9728 t = build_int_cst (TREE_TYPE (decl), 1);
9729 if (c)
9730 OMP_CLAUSE_LINEAR_STEP (c) = t;
9731 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
9732 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
9733 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
9734 break;
9737 case PREDECREMENT_EXPR:
9738 case POSTDECREMENT_EXPR:
9739 /* c_omp_for_incr_canonicalize_ptr() should have been
9740 called to massage things appropriately. */
9741 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
9742 if (orig_for_stmt != for_stmt)
9743 break;
9744 t = build_int_cst (TREE_TYPE (decl), -1);
9745 if (c)
9746 OMP_CLAUSE_LINEAR_STEP (c) = t;
9747 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
9748 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
9749 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
9750 break;
9752 case MODIFY_EXPR:
9753 gcc_assert (TREE_OPERAND (t, 0) == decl);
9754 TREE_OPERAND (t, 0) = var;
9756 t = TREE_OPERAND (t, 1);
9757 switch (TREE_CODE (t))
9759 case PLUS_EXPR:
9760 if (TREE_OPERAND (t, 1) == decl)
9762 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
9763 TREE_OPERAND (t, 0) = var;
9764 break;
9767 /* Fallthru. */
9768 case MINUS_EXPR:
9769 case POINTER_PLUS_EXPR:
9770 gcc_assert (TREE_OPERAND (t, 0) == decl);
9771 TREE_OPERAND (t, 0) = var;
9772 break;
9773 default:
9774 gcc_unreachable ();
9777 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
9778 is_gimple_val, fb_rvalue, false);
9779 ret = MIN (ret, tret);
9780 if (c)
9782 tree step = TREE_OPERAND (t, 1);
9783 tree stept = TREE_TYPE (decl);
9784 if (POINTER_TYPE_P (stept))
9785 stept = sizetype;
9786 step = fold_convert (stept, step);
9787 if (TREE_CODE (t) == MINUS_EXPR)
9788 step = fold_build1 (NEGATE_EXPR, stept, step);
9789 OMP_CLAUSE_LINEAR_STEP (c) = step;
9790 if (step != TREE_OPERAND (t, 1))
9792 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
9793 &for_pre_body, NULL,
9794 is_gimple_val, fb_rvalue, false);
9795 ret = MIN (ret, tret);
9798 break;
9800 default:
9801 gcc_unreachable ();
9804 if (c2)
9806 gcc_assert (c);
9807 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
9810 if ((var != decl || collapse > 1) && orig_for_stmt == for_stmt)
9812 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
9813 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9814 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
9815 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
9816 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
9817 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
9818 && OMP_CLAUSE_DECL (c) == decl)
9820 if (is_doacross && (collapse == 1 || i >= collapse))
9821 t = var;
9822 else
9824 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9825 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
9826 gcc_assert (TREE_OPERAND (t, 0) == var);
9827 t = TREE_OPERAND (t, 1);
9828 gcc_assert (TREE_CODE (t) == PLUS_EXPR
9829 || TREE_CODE (t) == MINUS_EXPR
9830 || TREE_CODE (t) == POINTER_PLUS_EXPR);
9831 gcc_assert (TREE_OPERAND (t, 0) == var);
9832 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
9833 is_doacross ? var : decl,
9834 TREE_OPERAND (t, 1));
9836 gimple_seq *seq;
9837 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
9838 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
9839 else
9840 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
9841 gimplify_assign (decl, t, seq);
9846 BITMAP_FREE (has_decl_expr);
9848 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9850 push_gimplify_context ();
9851 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
9853 OMP_FOR_BODY (orig_for_stmt)
9854 = build3 (BIND_EXPR, void_type_node, NULL,
9855 OMP_FOR_BODY (orig_for_stmt), NULL);
9856 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
9860 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
9861 &for_body);
9863 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9865 if (gimple_code (g) == GIMPLE_BIND)
9866 pop_gimplify_context (g);
9867 else
9868 pop_gimplify_context (NULL);
9871 if (orig_for_stmt != for_stmt)
9872 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9874 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9875 decl = TREE_OPERAND (t, 0);
9876 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9877 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9878 gimplify_omp_ctxp = ctx->outer_context;
9879 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
9880 gimplify_omp_ctxp = ctx;
9881 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
9882 TREE_OPERAND (t, 0) = var;
9883 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9884 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
9885 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
9888 gimplify_adjust_omp_clauses (pre_p, for_body,
9889 &OMP_FOR_CLAUSES (orig_for_stmt),
9890 TREE_CODE (orig_for_stmt));
9892 int kind;
9893 switch (TREE_CODE (orig_for_stmt))
9895 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
9896 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
9897 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
9898 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
9899 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
9900 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
9901 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
9902 default:
9903 gcc_unreachable ();
9905 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
9906 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
9907 for_pre_body);
9908 if (orig_for_stmt != for_stmt)
9909 gimple_omp_for_set_combined_p (gfor, true);
9910 if (gimplify_omp_ctxp
9911 && (gimplify_omp_ctxp->combined_loop
9912 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
9913 && gimplify_omp_ctxp->outer_context
9914 && gimplify_omp_ctxp->outer_context->combined_loop)))
9916 gimple_omp_for_set_combined_into_p (gfor, true);
9917 if (gimplify_omp_ctxp->combined_loop)
9918 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
9919 else
9920 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
9923 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9925 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9926 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
9927 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
9928 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9929 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
9930 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
9931 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9932 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
9935 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
9936 constructs with GIMPLE_OMP_TASK sandwiched in between them.
9937 The outer taskloop stands for computing the number of iterations,
9938 counts for collapsed loops and holding taskloop specific clauses.
9939 The task construct stands for the effect of data sharing on the
9940 explicit task it creates and the inner taskloop stands for expansion
9941 of the static loop inside of the explicit task construct. */
9942 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9944 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
9945 tree task_clauses = NULL_TREE;
9946 tree c = *gfor_clauses_ptr;
9947 tree *gtask_clauses_ptr = &task_clauses;
9948 tree outer_for_clauses = NULL_TREE;
9949 tree *gforo_clauses_ptr = &outer_for_clauses;
9950 for (; c; c = OMP_CLAUSE_CHAIN (c))
9951 switch (OMP_CLAUSE_CODE (c))
9953 /* These clauses are allowed on task, move them there. */
9954 case OMP_CLAUSE_SHARED:
9955 case OMP_CLAUSE_FIRSTPRIVATE:
9956 case OMP_CLAUSE_DEFAULT:
9957 case OMP_CLAUSE_IF:
9958 case OMP_CLAUSE_UNTIED:
9959 case OMP_CLAUSE_FINAL:
9960 case OMP_CLAUSE_MERGEABLE:
9961 case OMP_CLAUSE_PRIORITY:
9962 *gtask_clauses_ptr = c;
9963 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9964 break;
9965 case OMP_CLAUSE_PRIVATE:
9966 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
9968 /* We want private on outer for and firstprivate
9969 on task. */
9970 *gtask_clauses_ptr
9971 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9972 OMP_CLAUSE_FIRSTPRIVATE);
9973 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
9974 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
9975 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
9976 *gforo_clauses_ptr = c;
9977 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9979 else
9981 *gtask_clauses_ptr = c;
9982 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9984 break;
9985 /* These clauses go into outer taskloop clauses. */
9986 case OMP_CLAUSE_GRAINSIZE:
9987 case OMP_CLAUSE_NUM_TASKS:
9988 case OMP_CLAUSE_NOGROUP:
9989 *gforo_clauses_ptr = c;
9990 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9991 break;
9992 /* Taskloop clause we duplicate on both taskloops. */
9993 case OMP_CLAUSE_COLLAPSE:
9994 *gfor_clauses_ptr = c;
9995 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9996 *gforo_clauses_ptr = copy_node (c);
9997 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
9998 break;
9999 /* For lastprivate, keep the clause on inner taskloop, and add
10000 a shared clause on task. If the same decl is also firstprivate,
10001 add also firstprivate clause on the inner taskloop. */
10002 case OMP_CLAUSE_LASTPRIVATE:
10003 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
10005 /* For taskloop C++ lastprivate IVs, we want:
10006 1) private on outer taskloop
10007 2) firstprivate and shared on task
10008 3) lastprivate on inner taskloop */
10009 *gtask_clauses_ptr
10010 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10011 OMP_CLAUSE_FIRSTPRIVATE);
10012 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10013 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
10014 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10015 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
10016 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
10017 OMP_CLAUSE_PRIVATE);
10018 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
10019 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
10020 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
10021 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
10023 *gfor_clauses_ptr = c;
10024 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
10025 *gtask_clauses_ptr
10026 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
10027 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
10028 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
10029 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
10030 gtask_clauses_ptr
10031 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
10032 break;
10033 default:
10034 gcc_unreachable ();
10036 *gfor_clauses_ptr = NULL_TREE;
10037 *gtask_clauses_ptr = NULL_TREE;
10038 *gforo_clauses_ptr = NULL_TREE;
10039 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
10040 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
10041 NULL_TREE, NULL_TREE, NULL_TREE);
10042 gimple_omp_task_set_taskloop_p (g, true);
10043 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
10044 gomp_for *gforo
10045 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
10046 gimple_omp_for_collapse (gfor),
10047 gimple_omp_for_pre_body (gfor));
10048 gimple_omp_for_set_pre_body (gfor, NULL);
10049 gimple_omp_for_set_combined_p (gforo, true);
10050 gimple_omp_for_set_combined_into_p (gfor, true);
10051 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
10053 t = unshare_expr (gimple_omp_for_index (gfor, i));
10054 gimple_omp_for_set_index (gforo, i, t);
10055 t = unshare_expr (gimple_omp_for_initial (gfor, i));
10056 gimple_omp_for_set_initial (gforo, i, t);
10057 gimple_omp_for_set_cond (gforo, i,
10058 gimple_omp_for_cond (gfor, i));
10059 t = unshare_expr (gimple_omp_for_final (gfor, i));
10060 gimple_omp_for_set_final (gforo, i, t);
10061 t = unshare_expr (gimple_omp_for_incr (gfor, i));
10062 gimple_omp_for_set_incr (gforo, i, t);
10064 gimplify_seq_add_stmt (pre_p, gforo);
10066 else
10067 gimplify_seq_add_stmt (pre_p, gfor);
10068 if (ret != GS_ALL_DONE)
10069 return GS_ERROR;
10070 *expr_p = NULL_TREE;
10071 return GS_ALL_DONE;
10074 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
10075 of OMP_TARGET's body. */
10077 static tree
10078 find_omp_teams (tree *tp, int *walk_subtrees, void *)
10080 *walk_subtrees = 0;
10081 switch (TREE_CODE (*tp))
10083 case OMP_TEAMS:
10084 return *tp;
10085 case BIND_EXPR:
10086 case STATEMENT_LIST:
10087 *walk_subtrees = 1;
10088 break;
10089 default:
10090 break;
10092 return NULL_TREE;
10095 /* Helper function of optimize_target_teams, determine if the expression
10096 can be computed safely before the target construct on the host. */
10098 static tree
10099 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
10101 splay_tree_node n;
10103 if (TYPE_P (*tp))
10105 *walk_subtrees = 0;
10106 return NULL_TREE;
10108 switch (TREE_CODE (*tp))
10110 case VAR_DECL:
10111 case PARM_DECL:
10112 case RESULT_DECL:
10113 *walk_subtrees = 0;
10114 if (error_operand_p (*tp)
10115 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
10116 || DECL_HAS_VALUE_EXPR_P (*tp)
10117 || DECL_THREAD_LOCAL_P (*tp)
10118 || TREE_SIDE_EFFECTS (*tp)
10119 || TREE_THIS_VOLATILE (*tp))
10120 return *tp;
10121 if (is_global_var (*tp)
10122 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
10123 || lookup_attribute ("omp declare target link",
10124 DECL_ATTRIBUTES (*tp))))
10125 return *tp;
10126 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
10127 (splay_tree_key) *tp);
10128 if (n == NULL)
10130 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
10131 return NULL_TREE;
10132 return *tp;
10134 else if (n->value & GOVD_LOCAL)
10135 return *tp;
10136 else if (n->value & GOVD_FIRSTPRIVATE)
10137 return NULL_TREE;
10138 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10139 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
10140 return NULL_TREE;
10141 return *tp;
10142 case INTEGER_CST:
10143 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10144 return *tp;
10145 return NULL_TREE;
10146 case TARGET_EXPR:
10147 if (TARGET_EXPR_INITIAL (*tp)
10148 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
10149 return *tp;
10150 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
10151 walk_subtrees, NULL);
10152 /* Allow some reasonable subset of integral arithmetics. */
10153 case PLUS_EXPR:
10154 case MINUS_EXPR:
10155 case MULT_EXPR:
10156 case TRUNC_DIV_EXPR:
10157 case CEIL_DIV_EXPR:
10158 case FLOOR_DIV_EXPR:
10159 case ROUND_DIV_EXPR:
10160 case TRUNC_MOD_EXPR:
10161 case CEIL_MOD_EXPR:
10162 case FLOOR_MOD_EXPR:
10163 case ROUND_MOD_EXPR:
10164 case RDIV_EXPR:
10165 case EXACT_DIV_EXPR:
10166 case MIN_EXPR:
10167 case MAX_EXPR:
10168 case LSHIFT_EXPR:
10169 case RSHIFT_EXPR:
10170 case BIT_IOR_EXPR:
10171 case BIT_XOR_EXPR:
10172 case BIT_AND_EXPR:
10173 case NEGATE_EXPR:
10174 case ABS_EXPR:
10175 case BIT_NOT_EXPR:
10176 case NON_LVALUE_EXPR:
10177 CASE_CONVERT:
10178 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
10179 return *tp;
10180 return NULL_TREE;
10181 /* And disallow anything else, except for comparisons. */
10182 default:
10183 if (COMPARISON_CLASS_P (*tp))
10184 return NULL_TREE;
10185 return *tp;
10189 /* Try to determine if the num_teams and/or thread_limit expressions
10190 can have their values determined already before entering the
10191 target construct.
10192 INTEGER_CSTs trivially are,
10193 integral decls that are firstprivate (explicitly or implicitly)
10194 or explicitly map(always, to:) or map(always, tofrom:) on the target
10195 region too, and expressions involving simple arithmetics on those
10196 too, function calls are not ok, dereferencing something neither etc.
10197 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
10198 EXPR based on what we find:
10199 0 stands for clause not specified at all, use implementation default
10200 -1 stands for value that can't be determined easily before entering
10201 the target construct.
10202 If teams construct is not present at all, use 1 for num_teams
10203 and 0 for thread_limit (only one team is involved, and the thread
10204 limit is implementation defined. */
10206 static void
10207 optimize_target_teams (tree target, gimple_seq *pre_p)
10209 tree body = OMP_BODY (target);
10210 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
10211 tree num_teams = integer_zero_node;
10212 tree thread_limit = integer_zero_node;
10213 location_t num_teams_loc = EXPR_LOCATION (target);
10214 location_t thread_limit_loc = EXPR_LOCATION (target);
10215 tree c, *p, expr;
10216 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
10218 if (teams == NULL_TREE)
10219 num_teams = integer_one_node;
10220 else
10221 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
10223 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
10225 p = &num_teams;
10226 num_teams_loc = OMP_CLAUSE_LOCATION (c);
10228 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
10230 p = &thread_limit;
10231 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
10233 else
10234 continue;
10235 expr = OMP_CLAUSE_OPERAND (c, 0);
10236 if (TREE_CODE (expr) == INTEGER_CST)
10238 *p = expr;
10239 continue;
10241 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
10243 *p = integer_minus_one_node;
10244 continue;
10246 *p = expr;
10247 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
10248 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue, false)
10249 == GS_ERROR)
10251 gimplify_omp_ctxp = target_ctx;
10252 *p = integer_minus_one_node;
10253 continue;
10255 gimplify_omp_ctxp = target_ctx;
10256 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
10257 OMP_CLAUSE_OPERAND (c, 0) = *p;
10259 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10260 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
10261 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10262 OMP_TARGET_CLAUSES (target) = c;
10263 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10264 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
10265 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
10266 OMP_TARGET_CLAUSES (target) = c;
10269 /* Gimplify the gross structure of several OMP constructs. */
10271 static void
10272 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
10274 tree expr = *expr_p;
10275 gimple *stmt;
10276 gimple_seq body = NULL;
10277 enum omp_region_type ort;
10279 switch (TREE_CODE (expr))
10281 case OMP_SECTIONS:
10282 case OMP_SINGLE:
10283 ort = ORT_WORKSHARE;
10284 break;
10285 case OMP_TARGET:
10286 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
10287 break;
10288 case OACC_KERNELS:
10289 ort = ORT_ACC_KERNELS;
10290 break;
10291 case OACC_PARALLEL:
10292 ort = ORT_ACC_PARALLEL;
10293 break;
10294 case OACC_DATA:
10295 ort = ORT_ACC_DATA;
10296 break;
10297 case OMP_TARGET_DATA:
10298 ort = ORT_TARGET_DATA;
10299 break;
10300 case OMP_TEAMS:
10301 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
10302 break;
10303 case OACC_HOST_DATA:
10304 ort = ORT_ACC_HOST_DATA;
10305 break;
10306 default:
10307 gcc_unreachable ();
10309 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
10310 TREE_CODE (expr));
10311 if (TREE_CODE (expr) == OMP_TARGET)
10312 optimize_target_teams (expr, pre_p);
10313 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
10315 push_gimplify_context ();
10316 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
10317 if (gimple_code (g) == GIMPLE_BIND)
10318 pop_gimplify_context (g);
10319 else
10320 pop_gimplify_context (NULL);
10321 if ((ort & ORT_TARGET_DATA) != 0)
10323 enum built_in_function end_ix;
10324 switch (TREE_CODE (expr))
10326 case OACC_DATA:
10327 case OACC_HOST_DATA:
10328 end_ix = BUILT_IN_GOACC_DATA_END;
10329 break;
10330 case OMP_TARGET_DATA:
10331 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
10332 break;
10333 default:
10334 gcc_unreachable ();
10336 tree fn = builtin_decl_explicit (end_ix);
10337 g = gimple_build_call (fn, 0);
10338 gimple_seq cleanup = NULL;
10339 gimple_seq_add_stmt (&cleanup, g);
10340 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10341 body = NULL;
10342 gimple_seq_add_stmt (&body, g);
10345 else
10346 gimplify_and_add (OMP_BODY (expr), &body);
10347 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
10348 TREE_CODE (expr));
10350 switch (TREE_CODE (expr))
10352 case OACC_DATA:
10353 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
10354 OMP_CLAUSES (expr));
10355 break;
10356 case OACC_KERNELS:
10357 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
10358 OMP_CLAUSES (expr));
10359 break;
10360 case OACC_HOST_DATA:
10361 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
10362 OMP_CLAUSES (expr));
10363 break;
10364 case OACC_PARALLEL:
10365 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
10366 OMP_CLAUSES (expr));
10367 break;
10368 case OMP_SECTIONS:
10369 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
10370 break;
10371 case OMP_SINGLE:
10372 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
10373 break;
10374 case OMP_TARGET:
10375 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
10376 OMP_CLAUSES (expr));
10377 break;
10378 case OMP_TARGET_DATA:
10379 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
10380 OMP_CLAUSES (expr));
10381 break;
10382 case OMP_TEAMS:
10383 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
10384 break;
10385 default:
10386 gcc_unreachable ();
10389 gimplify_seq_add_stmt (pre_p, stmt);
10390 *expr_p = NULL_TREE;
10393 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
10394 target update constructs. */
10396 static void
10397 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
10399 tree expr = *expr_p;
10400 int kind;
10401 gomp_target *stmt;
10402 enum omp_region_type ort = ORT_WORKSHARE;
10404 switch (TREE_CODE (expr))
10406 case OACC_ENTER_DATA:
10407 case OACC_EXIT_DATA:
10408 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
10409 ort = ORT_ACC;
10410 break;
10411 case OACC_UPDATE:
10412 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
10413 ort = ORT_ACC;
10414 break;
10415 case OMP_TARGET_UPDATE:
10416 kind = GF_OMP_TARGET_KIND_UPDATE;
10417 break;
10418 case OMP_TARGET_ENTER_DATA:
10419 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
10420 break;
10421 case OMP_TARGET_EXIT_DATA:
10422 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
10423 break;
10424 default:
10425 gcc_unreachable ();
10427 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
10428 ort, TREE_CODE (expr));
10429 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
10430 TREE_CODE (expr));
10431 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
10433 gimplify_seq_add_stmt (pre_p, stmt);
10434 *expr_p = NULL_TREE;
10437 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
10438 stabilized the lhs of the atomic operation as *ADDR. Return true if
10439 EXPR is this stabilized form. */
10441 static bool
10442 goa_lhs_expr_p (tree expr, tree addr)
10444 /* Also include casts to other type variants. The C front end is fond
10445 of adding these for e.g. volatile variables. This is like
10446 STRIP_TYPE_NOPS but includes the main variant lookup. */
10447 STRIP_USELESS_TYPE_CONVERSION (expr);
10449 if (TREE_CODE (expr) == INDIRECT_REF)
10451 expr = TREE_OPERAND (expr, 0);
10452 while (expr != addr
10453 && (CONVERT_EXPR_P (expr)
10454 || TREE_CODE (expr) == NON_LVALUE_EXPR)
10455 && TREE_CODE (expr) == TREE_CODE (addr)
10456 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
10458 expr = TREE_OPERAND (expr, 0);
10459 addr = TREE_OPERAND (addr, 0);
10461 if (expr == addr)
10462 return true;
10463 return (TREE_CODE (addr) == ADDR_EXPR
10464 && TREE_CODE (expr) == ADDR_EXPR
10465 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
10467 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
10468 return true;
10469 return false;
10472 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
10473 expression does not involve the lhs, evaluate it into a temporary.
10474 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
10475 or -1 if an error was encountered. */
10477 static int
10478 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
10479 tree lhs_var)
10481 tree expr = *expr_p;
10482 int saw_lhs;
10484 if (goa_lhs_expr_p (expr, lhs_addr))
10486 *expr_p = lhs_var;
10487 return 1;
10489 if (is_gimple_val (expr))
10490 return 0;
10492 saw_lhs = 0;
10493 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
10495 case tcc_binary:
10496 case tcc_comparison:
10497 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
10498 lhs_var);
10499 /* FALLTHRU */
10500 case tcc_unary:
10501 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
10502 lhs_var);
10503 break;
10504 case tcc_expression:
10505 switch (TREE_CODE (expr))
10507 case TRUTH_ANDIF_EXPR:
10508 case TRUTH_ORIF_EXPR:
10509 case TRUTH_AND_EXPR:
10510 case TRUTH_OR_EXPR:
10511 case TRUTH_XOR_EXPR:
10512 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
10513 lhs_addr, lhs_var);
10514 /* FALLTHRU */
10515 case TRUTH_NOT_EXPR:
10516 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
10517 lhs_addr, lhs_var);
10518 break;
10519 case COMPOUND_EXPR:
10520 /* Break out any preevaluations from cp_build_modify_expr. */
10521 for (; TREE_CODE (expr) == COMPOUND_EXPR;
10522 expr = TREE_OPERAND (expr, 1))
10523 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
10524 *expr_p = expr;
10525 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
10526 default:
10527 break;
10529 break;
10530 default:
10531 break;
10534 if (saw_lhs == 0)
10536 enum gimplify_status gs;
10537 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
10538 if (gs != GS_ALL_DONE)
10539 saw_lhs = -1;
10542 return saw_lhs;
10545 /* Gimplify an OMP_ATOMIC statement. */
10547 static enum gimplify_status
10548 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
10550 tree addr = TREE_OPERAND (*expr_p, 0);
10551 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
10552 ? NULL : TREE_OPERAND (*expr_p, 1);
10553 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
10554 tree tmp_load;
10555 gomp_atomic_load *loadstmt;
10556 gomp_atomic_store *storestmt;
10558 tmp_load = create_tmp_reg (type);
10559 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
10560 return GS_ERROR;
10562 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
10563 != GS_ALL_DONE)
10564 return GS_ERROR;
10566 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
10567 gimplify_seq_add_stmt (pre_p, loadstmt);
10568 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
10569 != GS_ALL_DONE)
10570 return GS_ERROR;
10572 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
10573 rhs = tmp_load;
10574 storestmt = gimple_build_omp_atomic_store (rhs);
10575 gimplify_seq_add_stmt (pre_p, storestmt);
10576 if (OMP_ATOMIC_SEQ_CST (*expr_p))
10578 gimple_omp_atomic_set_seq_cst (loadstmt);
10579 gimple_omp_atomic_set_seq_cst (storestmt);
10581 switch (TREE_CODE (*expr_p))
10583 case OMP_ATOMIC_READ:
10584 case OMP_ATOMIC_CAPTURE_OLD:
10585 *expr_p = tmp_load;
10586 gimple_omp_atomic_set_need_value (loadstmt);
10587 break;
10588 case OMP_ATOMIC_CAPTURE_NEW:
10589 *expr_p = rhs;
10590 gimple_omp_atomic_set_need_value (storestmt);
10591 break;
10592 default:
10593 *expr_p = NULL;
10594 break;
10597 return GS_ALL_DONE;
10600 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
10601 body, and adding some EH bits. */
10603 static enum gimplify_status
10604 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
10606 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
10607 gimple *body_stmt;
10608 gtransaction *trans_stmt;
10609 gimple_seq body = NULL;
10610 int subcode = 0;
10612 /* Wrap the transaction body in a BIND_EXPR so we have a context
10613 where to put decls for OMP. */
10614 if (TREE_CODE (tbody) != BIND_EXPR)
10616 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
10617 TREE_SIDE_EFFECTS (bind) = 1;
10618 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
10619 TRANSACTION_EXPR_BODY (expr) = bind;
10622 push_gimplify_context ();
10623 temp = voidify_wrapper_expr (*expr_p, NULL);
10625 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
10626 pop_gimplify_context (body_stmt);
10628 trans_stmt = gimple_build_transaction (body);
10629 if (TRANSACTION_EXPR_OUTER (expr))
10630 subcode = GTMA_IS_OUTER;
10631 else if (TRANSACTION_EXPR_RELAXED (expr))
10632 subcode = GTMA_IS_RELAXED;
10633 gimple_transaction_set_subcode (trans_stmt, subcode);
10635 gimplify_seq_add_stmt (pre_p, trans_stmt);
10637 if (temp)
10639 *expr_p = temp;
10640 return GS_OK;
10643 *expr_p = NULL_TREE;
10644 return GS_ALL_DONE;
10647 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
10648 is the OMP_BODY of the original EXPR (which has already been
10649 gimplified so it's not present in the EXPR).
10651 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
10653 static gimple *
10654 gimplify_omp_ordered (tree expr, gimple_seq body)
10656 tree c, decls;
10657 int failures = 0;
10658 unsigned int i;
10659 tree source_c = NULL_TREE;
10660 tree sink_c = NULL_TREE;
10662 if (gimplify_omp_ctxp)
10664 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
10665 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
10666 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
10667 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
10668 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
10670 error_at (OMP_CLAUSE_LOCATION (c),
10671 "%<ordered%> construct with %<depend%> clause must be "
10672 "closely nested inside a loop with %<ordered%> clause "
10673 "with a parameter");
10674 failures++;
10676 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
10677 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
10679 bool fail = false;
10680 for (decls = OMP_CLAUSE_DECL (c), i = 0;
10681 decls && TREE_CODE (decls) == TREE_LIST;
10682 decls = TREE_CHAIN (decls), ++i)
10683 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
10684 continue;
10685 else if (TREE_VALUE (decls)
10686 != gimplify_omp_ctxp->loop_iter_var[2 * i])
10688 error_at (OMP_CLAUSE_LOCATION (c),
10689 "variable %qE is not an iteration "
10690 "of outermost loop %d, expected %qE",
10691 TREE_VALUE (decls), i + 1,
10692 gimplify_omp_ctxp->loop_iter_var[2 * i]);
10693 fail = true;
10694 failures++;
10696 else
10697 TREE_VALUE (decls)
10698 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
10699 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
10701 error_at (OMP_CLAUSE_LOCATION (c),
10702 "number of variables in %<depend(sink)%> "
10703 "clause does not match number of "
10704 "iteration variables");
10705 failures++;
10707 sink_c = c;
10709 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
10710 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
10712 if (source_c)
10714 error_at (OMP_CLAUSE_LOCATION (c),
10715 "more than one %<depend(source)%> clause on an "
10716 "%<ordered%> construct");
10717 failures++;
10719 else
10720 source_c = c;
10723 if (source_c && sink_c)
10725 error_at (OMP_CLAUSE_LOCATION (source_c),
10726 "%<depend(source)%> clause specified together with "
10727 "%<depend(sink:)%> clauses on the same construct");
10728 failures++;
10731 if (failures)
10732 return gimple_build_nop ();
10733 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
10736 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
10737 expression produces a value to be used as an operand inside a GIMPLE
10738 statement, the value will be stored back in *EXPR_P. This value will
10739 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
10740 an SSA_NAME. The corresponding sequence of GIMPLE statements is
10741 emitted in PRE_P and POST_P.
10743 Additionally, this process may overwrite parts of the input
10744 expression during gimplification. Ideally, it should be
10745 possible to do non-destructive gimplification.
10747 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
10748 the expression needs to evaluate to a value to be used as
10749 an operand in a GIMPLE statement, this value will be stored in
10750 *EXPR_P on exit. This happens when the caller specifies one
10751 of fb_lvalue or fb_rvalue fallback flags.
10753 PRE_P will contain the sequence of GIMPLE statements corresponding
10754 to the evaluation of EXPR and all the side-effects that must
10755 be executed before the main expression. On exit, the last
10756 statement of PRE_P is the core statement being gimplified. For
10757 instance, when gimplifying 'if (++a)' the last statement in
10758 PRE_P will be 'if (t.1)' where t.1 is the result of
10759 pre-incrementing 'a'.
10761 POST_P will contain the sequence of GIMPLE statements corresponding
10762 to the evaluation of all the side-effects that must be executed
10763 after the main expression. If this is NULL, the post
10764 side-effects are stored at the end of PRE_P.
10766 The reason why the output is split in two is to handle post
10767 side-effects explicitly. In some cases, an expression may have
10768 inner and outer post side-effects which need to be emitted in
10769 an order different from the one given by the recursive
10770 traversal. For instance, for the expression (*p--)++ the post
10771 side-effects of '--' must actually occur *after* the post
10772 side-effects of '++'. However, gimplification will first visit
10773 the inner expression, so if a separate POST sequence was not
10774 used, the resulting sequence would be:
10776 1 t.1 = *p
10777 2 p = p - 1
10778 3 t.2 = t.1 + 1
10779 4 *p = t.2
10781 However, the post-decrement operation in line #2 must not be
10782 evaluated until after the store to *p at line #4, so the
10783 correct sequence should be:
10785 1 t.1 = *p
10786 2 t.2 = t.1 + 1
10787 3 *p = t.2
10788 4 p = p - 1
10790 So, by specifying a separate post queue, it is possible
10791 to emit the post side-effects in the correct order.
10792 If POST_P is NULL, an internal queue will be used. Before
10793 returning to the caller, the sequence POST_P is appended to
10794 the main output sequence PRE_P.
10796 GIMPLE_TEST_F points to a function that takes a tree T and
10797 returns nonzero if T is in the GIMPLE form requested by the
10798 caller. The GIMPLE predicates are in gimple.c.
10800 FALLBACK tells the function what sort of a temporary we want if
10801 gimplification cannot produce an expression that complies with
10802 GIMPLE_TEST_F.
10804 fb_none means that no temporary should be generated
10805 fb_rvalue means that an rvalue is OK to generate
10806 fb_lvalue means that an lvalue is OK to generate
10807 fb_either means that either is OK, but an lvalue is preferable.
10808 fb_mayfail means that gimplification may fail (in which case
10809 GS_ERROR will be returned)
10811 The return value is either GS_ERROR or GS_ALL_DONE, since this
10812 function iterates until EXPR is completely gimplified or an error
10813 occurs. */
10815 enum gimplify_status
10816 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
10817 bool (*gimple_test_f) (tree), fallback_t fallback)
10819 tree tmp;
10820 gimple_seq internal_pre = NULL;
10821 gimple_seq internal_post = NULL;
10822 tree save_expr;
10823 bool is_statement;
10824 location_t saved_location;
10825 enum gimplify_status ret;
10826 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
10828 save_expr = *expr_p;
10829 if (save_expr == NULL_TREE)
10830 return GS_ALL_DONE;
10832 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
10833 is_statement = gimple_test_f == is_gimple_stmt;
10834 if (is_statement)
10835 gcc_assert (pre_p);
10837 /* Consistency checks. */
10838 if (gimple_test_f == is_gimple_reg)
10839 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
10840 else if (gimple_test_f == is_gimple_val
10841 || gimple_test_f == is_gimple_call_addr
10842 || gimple_test_f == is_gimple_condexpr
10843 || gimple_test_f == is_gimple_mem_rhs
10844 || gimple_test_f == is_gimple_mem_rhs_or_call
10845 || gimple_test_f == is_gimple_reg_rhs
10846 || gimple_test_f == is_gimple_reg_rhs_or_call
10847 || gimple_test_f == is_gimple_asm_val
10848 || gimple_test_f == is_gimple_mem_ref_addr)
10849 gcc_assert (fallback & fb_rvalue);
10850 else if (gimple_test_f == is_gimple_min_lval
10851 || gimple_test_f == is_gimple_lvalue)
10852 gcc_assert (fallback & fb_lvalue);
10853 else if (gimple_test_f == is_gimple_addressable)
10854 gcc_assert (fallback & fb_either);
10855 else if (gimple_test_f == is_gimple_stmt)
10856 gcc_assert (fallback == fb_none);
10857 else
10859 /* We should have recognized the GIMPLE_TEST_F predicate to
10860 know what kind of fallback to use in case a temporary is
10861 needed to hold the value or address of *EXPR_P. */
10862 gcc_unreachable ();
10865 /* We used to check the predicate here and return immediately if it
10866 succeeds. This is wrong; the design is for gimplification to be
10867 idempotent, and for the predicates to only test for valid forms, not
10868 whether they are fully simplified. */
10869 if (pre_p == NULL)
10870 pre_p = &internal_pre;
10872 if (post_p == NULL)
10873 post_p = &internal_post;
10875 /* Remember the last statements added to PRE_P and POST_P. Every
10876 new statement added by the gimplification helpers needs to be
10877 annotated with location information. To centralize the
10878 responsibility, we remember the last statement that had been
10879 added to both queues before gimplifying *EXPR_P. If
10880 gimplification produces new statements in PRE_P and POST_P, those
10881 statements will be annotated with the same location information
10882 as *EXPR_P. */
10883 pre_last_gsi = gsi_last (*pre_p);
10884 post_last_gsi = gsi_last (*post_p);
10886 saved_location = input_location;
10887 if (save_expr != error_mark_node
10888 && EXPR_HAS_LOCATION (*expr_p))
10889 input_location = EXPR_LOCATION (*expr_p);
10891 /* Loop over the specific gimplifiers until the toplevel node
10892 remains the same. */
10895 /* Strip away as many useless type conversions as possible
10896 at the toplevel. */
10897 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
10899 /* Remember the expr. */
10900 save_expr = *expr_p;
10902 /* Die, die, die, my darling. */
10903 if (save_expr == error_mark_node
10904 || (TREE_TYPE (save_expr)
10905 && TREE_TYPE (save_expr) == error_mark_node))
10907 ret = GS_ERROR;
10908 break;
10911 /* Do any language-specific gimplification. */
10912 ret = ((enum gimplify_status)
10913 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
10914 if (ret == GS_OK)
10916 if (*expr_p == NULL_TREE)
10917 break;
10918 if (*expr_p != save_expr)
10919 continue;
10921 else if (ret != GS_UNHANDLED)
10922 break;
10924 /* Make sure that all the cases set 'ret' appropriately. */
10925 ret = GS_UNHANDLED;
10926 switch (TREE_CODE (*expr_p))
10928 /* First deal with the special cases. */
10930 case POSTINCREMENT_EXPR:
10931 case POSTDECREMENT_EXPR:
10932 case PREINCREMENT_EXPR:
10933 case PREDECREMENT_EXPR:
10934 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
10935 fallback != fb_none,
10936 TREE_TYPE (*expr_p));
10937 break;
10939 case VIEW_CONVERT_EXPR:
10940 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
10941 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
10943 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10944 post_p, is_gimple_val, fb_rvalue);
10945 recalculate_side_effects (*expr_p);
10946 break;
10948 /* Fallthru. */
10950 case ARRAY_REF:
10951 case ARRAY_RANGE_REF:
10952 case REALPART_EXPR:
10953 case IMAGPART_EXPR:
10954 case COMPONENT_REF:
10955 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
10956 fallback ? fallback : fb_rvalue);
10957 break;
10959 case COND_EXPR:
10960 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
10962 /* C99 code may assign to an array in a structure value of a
10963 conditional expression, and this has undefined behavior
10964 only on execution, so create a temporary if an lvalue is
10965 required. */
10966 if (fallback == fb_lvalue)
10968 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
10969 mark_addressable (*expr_p);
10970 ret = GS_OK;
10972 break;
10974 case CALL_EXPR:
10975 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
10977 /* C99 code may assign to an array in a structure returned
10978 from a function, and this has undefined behavior only on
10979 execution, so create a temporary if an lvalue is
10980 required. */
10981 if (fallback == fb_lvalue)
10983 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
10984 mark_addressable (*expr_p);
10985 ret = GS_OK;
10987 break;
10989 case TREE_LIST:
10990 gcc_unreachable ();
10992 case COMPOUND_EXPR:
10993 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
10994 break;
10996 case COMPOUND_LITERAL_EXPR:
10997 ret = gimplify_compound_literal_expr (expr_p, pre_p,
10998 gimple_test_f, fallback);
10999 break;
11001 case MODIFY_EXPR:
11002 case INIT_EXPR:
11003 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
11004 fallback != fb_none);
11005 break;
11007 case TRUTH_ANDIF_EXPR:
11008 case TRUTH_ORIF_EXPR:
11010 /* Preserve the original type of the expression and the
11011 source location of the outer expression. */
11012 tree org_type = TREE_TYPE (*expr_p);
11013 *expr_p = gimple_boolify (*expr_p);
11014 *expr_p = build3_loc (input_location, COND_EXPR,
11015 org_type, *expr_p,
11016 fold_convert_loc
11017 (input_location,
11018 org_type, boolean_true_node),
11019 fold_convert_loc
11020 (input_location,
11021 org_type, boolean_false_node));
11022 ret = GS_OK;
11023 break;
11026 case TRUTH_NOT_EXPR:
11028 tree type = TREE_TYPE (*expr_p);
11029 /* The parsers are careful to generate TRUTH_NOT_EXPR
11030 only with operands that are always zero or one.
11031 We do not fold here but handle the only interesting case
11032 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
11033 *expr_p = gimple_boolify (*expr_p);
11034 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
11035 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
11036 TREE_TYPE (*expr_p),
11037 TREE_OPERAND (*expr_p, 0));
11038 else
11039 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
11040 TREE_TYPE (*expr_p),
11041 TREE_OPERAND (*expr_p, 0),
11042 build_int_cst (TREE_TYPE (*expr_p), 1));
11043 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
11044 *expr_p = fold_convert_loc (input_location, type, *expr_p);
11045 ret = GS_OK;
11046 break;
11049 case ADDR_EXPR:
11050 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
11051 break;
11053 case ANNOTATE_EXPR:
11055 tree cond = TREE_OPERAND (*expr_p, 0);
11056 tree kind = TREE_OPERAND (*expr_p, 1);
11057 tree type = TREE_TYPE (cond);
11058 if (!INTEGRAL_TYPE_P (type))
11060 *expr_p = cond;
11061 ret = GS_OK;
11062 break;
11064 tree tmp = create_tmp_var (type);
11065 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
11066 gcall *call
11067 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
11068 gimple_call_set_lhs (call, tmp);
11069 gimplify_seq_add_stmt (pre_p, call);
11070 *expr_p = tmp;
11071 ret = GS_ALL_DONE;
11072 break;
11075 case VA_ARG_EXPR:
11076 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
11077 break;
11079 CASE_CONVERT:
11080 if (IS_EMPTY_STMT (*expr_p))
11082 ret = GS_ALL_DONE;
11083 break;
11086 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
11087 || fallback == fb_none)
11089 /* Just strip a conversion to void (or in void context) and
11090 try again. */
11091 *expr_p = TREE_OPERAND (*expr_p, 0);
11092 ret = GS_OK;
11093 break;
11096 ret = gimplify_conversion (expr_p);
11097 if (ret == GS_ERROR)
11098 break;
11099 if (*expr_p != save_expr)
11100 break;
11101 /* FALLTHRU */
11103 case FIX_TRUNC_EXPR:
11104 /* unary_expr: ... | '(' cast ')' val | ... */
11105 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11106 is_gimple_val, fb_rvalue);
11107 recalculate_side_effects (*expr_p);
11108 break;
11110 case INDIRECT_REF:
11112 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
11113 bool notrap = TREE_THIS_NOTRAP (*expr_p);
11114 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
11116 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
11117 if (*expr_p != save_expr)
11119 ret = GS_OK;
11120 break;
11123 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11124 is_gimple_reg, fb_rvalue);
11125 if (ret == GS_ERROR)
11126 break;
11128 recalculate_side_effects (*expr_p);
11129 *expr_p = fold_build2_loc (input_location, MEM_REF,
11130 TREE_TYPE (*expr_p),
11131 TREE_OPERAND (*expr_p, 0),
11132 build_int_cst (saved_ptr_type, 0));
11133 TREE_THIS_VOLATILE (*expr_p) = volatilep;
11134 TREE_THIS_NOTRAP (*expr_p) = notrap;
11135 ret = GS_OK;
11136 break;
11139 /* We arrive here through the various re-gimplifcation paths. */
11140 case MEM_REF:
11141 /* First try re-folding the whole thing. */
11142 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
11143 TREE_OPERAND (*expr_p, 0),
11144 TREE_OPERAND (*expr_p, 1));
11145 if (tmp)
11147 REF_REVERSE_STORAGE_ORDER (tmp)
11148 = REF_REVERSE_STORAGE_ORDER (*expr_p);
11149 *expr_p = tmp;
11150 recalculate_side_effects (*expr_p);
11151 ret = GS_OK;
11152 break;
11154 /* Avoid re-gimplifying the address operand if it is already
11155 in suitable form. Re-gimplifying would mark the address
11156 operand addressable. Always gimplify when not in SSA form
11157 as we still may have to gimplify decls with value-exprs. */
11158 if (!gimplify_ctxp || !gimple_in_ssa_p (cfun)
11159 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
11161 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11162 is_gimple_mem_ref_addr, fb_rvalue);
11163 if (ret == GS_ERROR)
11164 break;
11166 recalculate_side_effects (*expr_p);
11167 ret = GS_ALL_DONE;
11168 break;
11170 /* Constants need not be gimplified. */
11171 case INTEGER_CST:
11172 case REAL_CST:
11173 case FIXED_CST:
11174 case STRING_CST:
11175 case COMPLEX_CST:
11176 case VECTOR_CST:
11177 /* Drop the overflow flag on constants, we do not want
11178 that in the GIMPLE IL. */
11179 if (TREE_OVERFLOW_P (*expr_p))
11180 *expr_p = drop_tree_overflow (*expr_p);
11181 ret = GS_ALL_DONE;
11182 break;
11184 case CONST_DECL:
11185 /* If we require an lvalue, such as for ADDR_EXPR, retain the
11186 CONST_DECL node. Otherwise the decl is replaceable by its
11187 value. */
11188 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
11189 if (fallback & fb_lvalue)
11190 ret = GS_ALL_DONE;
11191 else
11193 *expr_p = DECL_INITIAL (*expr_p);
11194 ret = GS_OK;
11196 break;
11198 case DECL_EXPR:
11199 ret = gimplify_decl_expr (expr_p, pre_p);
11200 break;
11202 case BIND_EXPR:
11203 ret = gimplify_bind_expr (expr_p, pre_p);
11204 break;
11206 case LOOP_EXPR:
11207 ret = gimplify_loop_expr (expr_p, pre_p);
11208 break;
11210 case SWITCH_EXPR:
11211 ret = gimplify_switch_expr (expr_p, pre_p);
11212 break;
11214 case EXIT_EXPR:
11215 ret = gimplify_exit_expr (expr_p);
11216 break;
11218 case GOTO_EXPR:
11219 /* If the target is not LABEL, then it is a computed jump
11220 and the target needs to be gimplified. */
11221 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
11223 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
11224 NULL, is_gimple_val, fb_rvalue);
11225 if (ret == GS_ERROR)
11226 break;
11228 gimplify_seq_add_stmt (pre_p,
11229 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
11230 ret = GS_ALL_DONE;
11231 break;
11233 case PREDICT_EXPR:
11234 gimplify_seq_add_stmt (pre_p,
11235 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
11236 PREDICT_EXPR_OUTCOME (*expr_p)));
11237 ret = GS_ALL_DONE;
11238 break;
11240 case LABEL_EXPR:
11241 ret = gimplify_label_expr (expr_p, pre_p);
11242 break;
11244 case CASE_LABEL_EXPR:
11245 ret = gimplify_case_label_expr (expr_p, pre_p);
11246 break;
11248 case RETURN_EXPR:
11249 ret = gimplify_return_expr (*expr_p, pre_p);
11250 break;
11252 case CONSTRUCTOR:
11253 /* Don't reduce this in place; let gimplify_init_constructor work its
11254 magic. Buf if we're just elaborating this for side effects, just
11255 gimplify any element that has side-effects. */
11256 if (fallback == fb_none)
11258 unsigned HOST_WIDE_INT ix;
11259 tree val;
11260 tree temp = NULL_TREE;
11261 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
11262 if (TREE_SIDE_EFFECTS (val))
11263 append_to_statement_list (val, &temp);
11265 *expr_p = temp;
11266 ret = temp ? GS_OK : GS_ALL_DONE;
11268 /* C99 code may assign to an array in a constructed
11269 structure or union, and this has undefined behavior only
11270 on execution, so create a temporary if an lvalue is
11271 required. */
11272 else if (fallback == fb_lvalue)
11274 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p, false);
11275 mark_addressable (*expr_p);
11276 ret = GS_OK;
11278 else
11279 ret = GS_ALL_DONE;
11280 break;
11282 /* The following are special cases that are not handled by the
11283 original GIMPLE grammar. */
11285 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
11286 eliminated. */
11287 case SAVE_EXPR:
11288 ret = gimplify_save_expr (expr_p, pre_p, post_p);
11289 break;
11291 case BIT_FIELD_REF:
11292 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11293 post_p, is_gimple_lvalue, fb_either);
11294 recalculate_side_effects (*expr_p);
11295 break;
11297 case TARGET_MEM_REF:
11299 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
11301 if (TMR_BASE (*expr_p))
11302 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
11303 post_p, is_gimple_mem_ref_addr, fb_either);
11304 if (TMR_INDEX (*expr_p))
11305 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
11306 post_p, is_gimple_val, fb_rvalue);
11307 if (TMR_INDEX2 (*expr_p))
11308 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
11309 post_p, is_gimple_val, fb_rvalue);
11310 /* TMR_STEP and TMR_OFFSET are always integer constants. */
11311 ret = MIN (r0, r1);
11313 break;
11315 case NON_LVALUE_EXPR:
11316 /* This should have been stripped above. */
11317 gcc_unreachable ();
11319 case ASM_EXPR:
11320 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
11321 break;
11323 case TRY_FINALLY_EXPR:
11324 case TRY_CATCH_EXPR:
11326 gimple_seq eval, cleanup;
11327 gtry *try_;
11329 /* Calls to destructors are generated automatically in FINALLY/CATCH
11330 block. They should have location as UNKNOWN_LOCATION. However,
11331 gimplify_call_expr will reset these call stmts to input_location
11332 if it finds stmt's location is unknown. To prevent resetting for
11333 destructors, we set the input_location to unknown.
11334 Note that this only affects the destructor calls in FINALLY/CATCH
11335 block, and will automatically reset to its original value by the
11336 end of gimplify_expr. */
11337 input_location = UNKNOWN_LOCATION;
11338 eval = cleanup = NULL;
11339 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
11340 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
11341 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
11342 if (gimple_seq_empty_p (cleanup))
11344 gimple_seq_add_seq (pre_p, eval);
11345 ret = GS_ALL_DONE;
11346 break;
11348 try_ = gimple_build_try (eval, cleanup,
11349 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
11350 ? GIMPLE_TRY_FINALLY
11351 : GIMPLE_TRY_CATCH);
11352 if (EXPR_HAS_LOCATION (save_expr))
11353 gimple_set_location (try_, EXPR_LOCATION (save_expr));
11354 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
11355 gimple_set_location (try_, saved_location);
11356 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
11357 gimple_try_set_catch_is_cleanup (try_,
11358 TRY_CATCH_IS_CLEANUP (*expr_p));
11359 gimplify_seq_add_stmt (pre_p, try_);
11360 ret = GS_ALL_DONE;
11361 break;
11364 case CLEANUP_POINT_EXPR:
11365 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
11366 break;
11368 case TARGET_EXPR:
11369 ret = gimplify_target_expr (expr_p, pre_p, post_p);
11370 break;
11372 case CATCH_EXPR:
11374 gimple *c;
11375 gimple_seq handler = NULL;
11376 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
11377 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
11378 gimplify_seq_add_stmt (pre_p, c);
11379 ret = GS_ALL_DONE;
11380 break;
11383 case EH_FILTER_EXPR:
11385 gimple *ehf;
11386 gimple_seq failure = NULL;
11388 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
11389 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
11390 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
11391 gimplify_seq_add_stmt (pre_p, ehf);
11392 ret = GS_ALL_DONE;
11393 break;
11396 case OBJ_TYPE_REF:
11398 enum gimplify_status r0, r1;
11399 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
11400 post_p, is_gimple_val, fb_rvalue);
11401 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
11402 post_p, is_gimple_val, fb_rvalue);
11403 TREE_SIDE_EFFECTS (*expr_p) = 0;
11404 ret = MIN (r0, r1);
11406 break;
11408 case LABEL_DECL:
11409 /* We get here when taking the address of a label. We mark
11410 the label as "forced"; meaning it can never be removed and
11411 it is a potential target for any computed goto. */
11412 FORCED_LABEL (*expr_p) = 1;
11413 ret = GS_ALL_DONE;
11414 break;
11416 case STATEMENT_LIST:
11417 ret = gimplify_statement_list (expr_p, pre_p);
11418 break;
11420 case WITH_SIZE_EXPR:
11422 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11423 post_p == &internal_post ? NULL : post_p,
11424 gimple_test_f, fallback);
11425 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
11426 is_gimple_val, fb_rvalue);
11427 ret = GS_ALL_DONE;
11429 break;
11431 case VAR_DECL:
11432 case PARM_DECL:
11433 ret = gimplify_var_or_parm_decl (expr_p);
11434 break;
11436 case RESULT_DECL:
11437 /* When within an OMP context, notice uses of variables. */
11438 if (gimplify_omp_ctxp)
11439 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
11440 ret = GS_ALL_DONE;
11441 break;
11443 case SSA_NAME:
11444 /* Allow callbacks into the gimplifier during optimization. */
11445 ret = GS_ALL_DONE;
11446 break;
11448 case OMP_PARALLEL:
11449 gimplify_omp_parallel (expr_p, pre_p);
11450 ret = GS_ALL_DONE;
11451 break;
11453 case OMP_TASK:
11454 gimplify_omp_task (expr_p, pre_p);
11455 ret = GS_ALL_DONE;
11456 break;
11458 case OMP_FOR:
11459 case OMP_SIMD:
11460 case CILK_SIMD:
11461 case CILK_FOR:
11462 case OMP_DISTRIBUTE:
11463 case OMP_TASKLOOP:
11464 case OACC_LOOP:
11465 ret = gimplify_omp_for (expr_p, pre_p);
11466 break;
11468 case OACC_CACHE:
11469 gimplify_oacc_cache (expr_p, pre_p);
11470 ret = GS_ALL_DONE;
11471 break;
11473 case OACC_DECLARE:
11474 gimplify_oacc_declare (expr_p, pre_p);
11475 ret = GS_ALL_DONE;
11476 break;
11478 case OACC_HOST_DATA:
11479 case OACC_DATA:
11480 case OACC_KERNELS:
11481 case OACC_PARALLEL:
11482 case OMP_SECTIONS:
11483 case OMP_SINGLE:
11484 case OMP_TARGET:
11485 case OMP_TARGET_DATA:
11486 case OMP_TEAMS:
11487 gimplify_omp_workshare (expr_p, pre_p);
11488 ret = GS_ALL_DONE;
11489 break;
11491 case OACC_ENTER_DATA:
11492 case OACC_EXIT_DATA:
11493 case OACC_UPDATE:
11494 case OMP_TARGET_UPDATE:
11495 case OMP_TARGET_ENTER_DATA:
11496 case OMP_TARGET_EXIT_DATA:
11497 gimplify_omp_target_update (expr_p, pre_p);
11498 ret = GS_ALL_DONE;
11499 break;
11501 case OMP_SECTION:
11502 case OMP_MASTER:
11503 case OMP_TASKGROUP:
11504 case OMP_ORDERED:
11505 case OMP_CRITICAL:
11507 gimple_seq body = NULL;
11508 gimple *g;
11510 gimplify_and_add (OMP_BODY (*expr_p), &body);
11511 switch (TREE_CODE (*expr_p))
11513 case OMP_SECTION:
11514 g = gimple_build_omp_section (body);
11515 break;
11516 case OMP_MASTER:
11517 g = gimple_build_omp_master (body);
11518 break;
11519 case OMP_TASKGROUP:
11521 gimple_seq cleanup = NULL;
11522 tree fn
11523 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
11524 g = gimple_build_call (fn, 0);
11525 gimple_seq_add_stmt (&cleanup, g);
11526 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
11527 body = NULL;
11528 gimple_seq_add_stmt (&body, g);
11529 g = gimple_build_omp_taskgroup (body);
11531 break;
11532 case OMP_ORDERED:
11533 g = gimplify_omp_ordered (*expr_p, body);
11534 break;
11535 case OMP_CRITICAL:
11536 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
11537 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
11538 gimplify_adjust_omp_clauses (pre_p, body,
11539 &OMP_CRITICAL_CLAUSES (*expr_p),
11540 OMP_CRITICAL);
11541 g = gimple_build_omp_critical (body,
11542 OMP_CRITICAL_NAME (*expr_p),
11543 OMP_CRITICAL_CLAUSES (*expr_p));
11544 break;
11545 default:
11546 gcc_unreachable ();
11548 gimplify_seq_add_stmt (pre_p, g);
11549 ret = GS_ALL_DONE;
11550 break;
11553 case OMP_ATOMIC:
11554 case OMP_ATOMIC_READ:
11555 case OMP_ATOMIC_CAPTURE_OLD:
11556 case OMP_ATOMIC_CAPTURE_NEW:
11557 ret = gimplify_omp_atomic (expr_p, pre_p);
11558 break;
11560 case TRANSACTION_EXPR:
11561 ret = gimplify_transaction (expr_p, pre_p);
11562 break;
11564 case TRUTH_AND_EXPR:
11565 case TRUTH_OR_EXPR:
11566 case TRUTH_XOR_EXPR:
11568 tree orig_type = TREE_TYPE (*expr_p);
11569 tree new_type, xop0, xop1;
11570 *expr_p = gimple_boolify (*expr_p);
11571 new_type = TREE_TYPE (*expr_p);
11572 if (!useless_type_conversion_p (orig_type, new_type))
11574 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
11575 ret = GS_OK;
11576 break;
11579 /* Boolified binary truth expressions are semantically equivalent
11580 to bitwise binary expressions. Canonicalize them to the
11581 bitwise variant. */
11582 switch (TREE_CODE (*expr_p))
11584 case TRUTH_AND_EXPR:
11585 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
11586 break;
11587 case TRUTH_OR_EXPR:
11588 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
11589 break;
11590 case TRUTH_XOR_EXPR:
11591 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
11592 break;
11593 default:
11594 break;
11596 /* Now make sure that operands have compatible type to
11597 expression's new_type. */
11598 xop0 = TREE_OPERAND (*expr_p, 0);
11599 xop1 = TREE_OPERAND (*expr_p, 1);
11600 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
11601 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
11602 new_type,
11603 xop0);
11604 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
11605 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
11606 new_type,
11607 xop1);
11608 /* Continue classified as tcc_binary. */
11609 goto expr_2;
11612 case VEC_COND_EXPR:
11614 enum gimplify_status r0, r1, r2;
11616 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11617 post_p, is_gimple_condexpr, fb_rvalue);
11618 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
11619 post_p, is_gimple_val, fb_rvalue);
11620 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
11621 post_p, is_gimple_val, fb_rvalue);
11623 ret = MIN (MIN (r0, r1), r2);
11624 recalculate_side_effects (*expr_p);
11626 break;
11628 case FMA_EXPR:
11629 case VEC_PERM_EXPR:
11630 /* Classified as tcc_expression. */
11631 goto expr_3;
11633 case BIT_INSERT_EXPR:
11634 /* Argument 3 is a constant. */
11635 goto expr_2;
11637 case POINTER_PLUS_EXPR:
11639 enum gimplify_status r0, r1;
11640 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11641 post_p, is_gimple_val, fb_rvalue);
11642 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
11643 post_p, is_gimple_val, fb_rvalue);
11644 recalculate_side_effects (*expr_p);
11645 ret = MIN (r0, r1);
11646 break;
11649 case CILK_SYNC_STMT:
11651 if (!fn_contains_cilk_spawn_p (cfun))
11653 error_at (EXPR_LOCATION (*expr_p),
11654 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
11655 ret = GS_ERROR;
11657 else
11659 gimplify_cilk_sync (expr_p, pre_p);
11660 ret = GS_ALL_DONE;
11662 break;
11665 default:
11666 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
11668 case tcc_comparison:
11669 /* Handle comparison of objects of non scalar mode aggregates
11670 with a call to memcmp. It would be nice to only have to do
11671 this for variable-sized objects, but then we'd have to allow
11672 the same nest of reference nodes we allow for MODIFY_EXPR and
11673 that's too complex.
11675 Compare scalar mode aggregates as scalar mode values. Using
11676 memcmp for them would be very inefficient at best, and is
11677 plain wrong if bitfields are involved. */
11679 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
11681 /* Vector comparisons need no boolification. */
11682 if (TREE_CODE (type) == VECTOR_TYPE)
11683 goto expr_2;
11684 else if (!AGGREGATE_TYPE_P (type))
11686 tree org_type = TREE_TYPE (*expr_p);
11687 *expr_p = gimple_boolify (*expr_p);
11688 if (!useless_type_conversion_p (org_type,
11689 TREE_TYPE (*expr_p)))
11691 *expr_p = fold_convert_loc (input_location,
11692 org_type, *expr_p);
11693 ret = GS_OK;
11695 else
11696 goto expr_2;
11698 else if (TYPE_MODE (type) != BLKmode)
11699 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
11700 else
11701 ret = gimplify_variable_sized_compare (expr_p);
11703 break;
11706 /* If *EXPR_P does not need to be special-cased, handle it
11707 according to its class. */
11708 case tcc_unary:
11709 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11710 post_p, is_gimple_val, fb_rvalue);
11711 break;
11713 case tcc_binary:
11714 expr_2:
11716 enum gimplify_status r0, r1;
11718 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11719 post_p, is_gimple_val, fb_rvalue);
11720 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
11721 post_p, is_gimple_val, fb_rvalue);
11723 ret = MIN (r0, r1);
11724 break;
11727 expr_3:
11729 enum gimplify_status r0, r1, r2;
11731 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
11732 post_p, is_gimple_val, fb_rvalue);
11733 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
11734 post_p, is_gimple_val, fb_rvalue);
11735 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
11736 post_p, is_gimple_val, fb_rvalue);
11738 ret = MIN (MIN (r0, r1), r2);
11739 break;
11742 case tcc_declaration:
11743 case tcc_constant:
11744 ret = GS_ALL_DONE;
11745 goto dont_recalculate;
11747 default:
11748 gcc_unreachable ();
11751 recalculate_side_effects (*expr_p);
11753 dont_recalculate:
11754 break;
11757 gcc_assert (*expr_p || ret != GS_OK);
11759 while (ret == GS_OK);
11761 /* If we encountered an error_mark somewhere nested inside, either
11762 stub out the statement or propagate the error back out. */
11763 if (ret == GS_ERROR)
11765 if (is_statement)
11766 *expr_p = NULL;
11767 goto out;
11770 /* This was only valid as a return value from the langhook, which
11771 we handled. Make sure it doesn't escape from any other context. */
11772 gcc_assert (ret != GS_UNHANDLED);
11774 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
11776 /* We aren't looking for a value, and we don't have a valid
11777 statement. If it doesn't have side-effects, throw it away. */
11778 if (!TREE_SIDE_EFFECTS (*expr_p))
11779 *expr_p = NULL;
11780 else if (!TREE_THIS_VOLATILE (*expr_p))
11782 /* This is probably a _REF that contains something nested that
11783 has side effects. Recurse through the operands to find it. */
11784 enum tree_code code = TREE_CODE (*expr_p);
11786 switch (code)
11788 case COMPONENT_REF:
11789 case REALPART_EXPR:
11790 case IMAGPART_EXPR:
11791 case VIEW_CONVERT_EXPR:
11792 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11793 gimple_test_f, fallback);
11794 break;
11796 case ARRAY_REF:
11797 case ARRAY_RANGE_REF:
11798 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
11799 gimple_test_f, fallback);
11800 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
11801 gimple_test_f, fallback);
11802 break;
11804 default:
11805 /* Anything else with side-effects must be converted to
11806 a valid statement before we get here. */
11807 gcc_unreachable ();
11810 *expr_p = NULL;
11812 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
11813 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
11815 /* Historically, the compiler has treated a bare reference
11816 to a non-BLKmode volatile lvalue as forcing a load. */
11817 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
11819 /* Normally, we do not want to create a temporary for a
11820 TREE_ADDRESSABLE type because such a type should not be
11821 copied by bitwise-assignment. However, we make an
11822 exception here, as all we are doing here is ensuring that
11823 we read the bytes that make up the type. We use
11824 create_tmp_var_raw because create_tmp_var will abort when
11825 given a TREE_ADDRESSABLE type. */
11826 tree tmp = create_tmp_var_raw (type, "vol");
11827 gimple_add_tmp_var (tmp);
11828 gimplify_assign (tmp, *expr_p, pre_p);
11829 *expr_p = NULL;
11831 else
11832 /* We can't do anything useful with a volatile reference to
11833 an incomplete type, so just throw it away. Likewise for
11834 a BLKmode type, since any implicit inner load should
11835 already have been turned into an explicit one by the
11836 gimplification process. */
11837 *expr_p = NULL;
11840 /* If we are gimplifying at the statement level, we're done. Tack
11841 everything together and return. */
11842 if (fallback == fb_none || is_statement)
11844 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
11845 it out for GC to reclaim it. */
11846 *expr_p = NULL_TREE;
11848 if (!gimple_seq_empty_p (internal_pre)
11849 || !gimple_seq_empty_p (internal_post))
11851 gimplify_seq_add_seq (&internal_pre, internal_post);
11852 gimplify_seq_add_seq (pre_p, internal_pre);
11855 /* The result of gimplifying *EXPR_P is going to be the last few
11856 statements in *PRE_P and *POST_P. Add location information
11857 to all the statements that were added by the gimplification
11858 helpers. */
11859 if (!gimple_seq_empty_p (*pre_p))
11860 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
11862 if (!gimple_seq_empty_p (*post_p))
11863 annotate_all_with_location_after (*post_p, post_last_gsi,
11864 input_location);
11866 goto out;
11869 #ifdef ENABLE_GIMPLE_CHECKING
11870 if (*expr_p)
11872 enum tree_code code = TREE_CODE (*expr_p);
11873 /* These expressions should already be in gimple IR form. */
11874 gcc_assert (code != MODIFY_EXPR
11875 && code != ASM_EXPR
11876 && code != BIND_EXPR
11877 && code != CATCH_EXPR
11878 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
11879 && code != EH_FILTER_EXPR
11880 && code != GOTO_EXPR
11881 && code != LABEL_EXPR
11882 && code != LOOP_EXPR
11883 && code != SWITCH_EXPR
11884 && code != TRY_FINALLY_EXPR
11885 && code != OACC_PARALLEL
11886 && code != OACC_KERNELS
11887 && code != OACC_DATA
11888 && code != OACC_HOST_DATA
11889 && code != OACC_DECLARE
11890 && code != OACC_UPDATE
11891 && code != OACC_ENTER_DATA
11892 && code != OACC_EXIT_DATA
11893 && code != OACC_CACHE
11894 && code != OMP_CRITICAL
11895 && code != OMP_FOR
11896 && code != OACC_LOOP
11897 && code != OMP_MASTER
11898 && code != OMP_TASKGROUP
11899 && code != OMP_ORDERED
11900 && code != OMP_PARALLEL
11901 && code != OMP_SECTIONS
11902 && code != OMP_SECTION
11903 && code != OMP_SINGLE);
11905 #endif
11907 /* Otherwise we're gimplifying a subexpression, so the resulting
11908 value is interesting. If it's a valid operand that matches
11909 GIMPLE_TEST_F, we're done. Unless we are handling some
11910 post-effects internally; if that's the case, we need to copy into
11911 a temporary before adding the post-effects to POST_P. */
11912 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
11913 goto out;
11915 /* Otherwise, we need to create a new temporary for the gimplified
11916 expression. */
11918 /* We can't return an lvalue if we have an internal postqueue. The
11919 object the lvalue refers to would (probably) be modified by the
11920 postqueue; we need to copy the value out first, which means an
11921 rvalue. */
11922 if ((fallback & fb_lvalue)
11923 && gimple_seq_empty_p (internal_post)
11924 && is_gimple_addressable (*expr_p))
11926 /* An lvalue will do. Take the address of the expression, store it
11927 in a temporary, and replace the expression with an INDIRECT_REF of
11928 that temporary. */
11929 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
11930 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
11931 *expr_p = build_simple_mem_ref (tmp);
11933 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
11935 /* An rvalue will do. Assign the gimplified expression into a
11936 new temporary TMP and replace the original expression with
11937 TMP. First, make sure that the expression has a type so that
11938 it can be assigned into a temporary. */
11939 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
11940 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
11942 else
11944 #ifdef ENABLE_GIMPLE_CHECKING
11945 if (!(fallback & fb_mayfail))
11947 fprintf (stderr, "gimplification failed:\n");
11948 print_generic_expr (stderr, *expr_p, 0);
11949 debug_tree (*expr_p);
11950 internal_error ("gimplification failed");
11952 #endif
11953 gcc_assert (fallback & fb_mayfail);
11955 /* If this is an asm statement, and the user asked for the
11956 impossible, don't die. Fail and let gimplify_asm_expr
11957 issue an error. */
11958 ret = GS_ERROR;
11959 goto out;
11962 /* Make sure the temporary matches our predicate. */
11963 gcc_assert ((*gimple_test_f) (*expr_p));
11965 if (!gimple_seq_empty_p (internal_post))
11967 annotate_all_with_location (internal_post, input_location);
11968 gimplify_seq_add_seq (pre_p, internal_post);
11971 out:
11972 input_location = saved_location;
11973 return ret;
11976 /* Like gimplify_expr but make sure the gimplified result is not itself
11977 a SSA name (but a decl if it were). Temporaries required by
11978 evaluating *EXPR_P may be still SSA names. */
11980 static enum gimplify_status
11981 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
11982 bool (*gimple_test_f) (tree), fallback_t fallback,
11983 bool allow_ssa)
11985 bool was_ssa_name_p = TREE_CODE (*expr_p) == SSA_NAME;
11986 enum gimplify_status ret = gimplify_expr (expr_p, pre_p, post_p,
11987 gimple_test_f, fallback);
11988 if (! allow_ssa
11989 && TREE_CODE (*expr_p) == SSA_NAME)
11991 tree name = *expr_p;
11992 if (was_ssa_name_p)
11993 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, NULL, false);
11994 else
11996 /* Avoid the extra copy if possible. */
11997 *expr_p = create_tmp_reg (TREE_TYPE (name));
11998 gimple_set_lhs (SSA_NAME_DEF_STMT (name), *expr_p);
11999 release_ssa_name (name);
12002 return ret;
12005 /* Look through TYPE for variable-sized objects and gimplify each such
12006 size that we find. Add to LIST_P any statements generated. */
12008 void
12009 gimplify_type_sizes (tree type, gimple_seq *list_p)
12011 tree field, t;
12013 if (type == NULL || type == error_mark_node)
12014 return;
12016 /* We first do the main variant, then copy into any other variants. */
12017 type = TYPE_MAIN_VARIANT (type);
12019 /* Avoid infinite recursion. */
12020 if (TYPE_SIZES_GIMPLIFIED (type))
12021 return;
12023 TYPE_SIZES_GIMPLIFIED (type) = 1;
12025 switch (TREE_CODE (type))
12027 case INTEGER_TYPE:
12028 case ENUMERAL_TYPE:
12029 case BOOLEAN_TYPE:
12030 case REAL_TYPE:
12031 case FIXED_POINT_TYPE:
12032 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
12033 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
12035 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12037 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
12038 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
12040 break;
12042 case ARRAY_TYPE:
12043 /* These types may not have declarations, so handle them here. */
12044 gimplify_type_sizes (TREE_TYPE (type), list_p);
12045 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
12046 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
12047 with assigned stack slots, for -O1+ -g they should be tracked
12048 by VTA. */
12049 if (!(TYPE_NAME (type)
12050 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12051 && DECL_IGNORED_P (TYPE_NAME (type)))
12052 && TYPE_DOMAIN (type)
12053 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
12055 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
12056 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12057 DECL_IGNORED_P (t) = 0;
12058 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12059 if (t && VAR_P (t) && DECL_ARTIFICIAL (t))
12060 DECL_IGNORED_P (t) = 0;
12062 break;
12064 case RECORD_TYPE:
12065 case UNION_TYPE:
12066 case QUAL_UNION_TYPE:
12067 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12068 if (TREE_CODE (field) == FIELD_DECL)
12070 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
12071 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
12072 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
12073 gimplify_type_sizes (TREE_TYPE (field), list_p);
12075 break;
12077 case POINTER_TYPE:
12078 case REFERENCE_TYPE:
12079 /* We used to recurse on the pointed-to type here, which turned out to
12080 be incorrect because its definition might refer to variables not
12081 yet initialized at this point if a forward declaration is involved.
12083 It was actually useful for anonymous pointed-to types to ensure
12084 that the sizes evaluation dominates every possible later use of the
12085 values. Restricting to such types here would be safe since there
12086 is no possible forward declaration around, but would introduce an
12087 undesirable middle-end semantic to anonymity. We then defer to
12088 front-ends the responsibility of ensuring that the sizes are
12089 evaluated both early and late enough, e.g. by attaching artificial
12090 type declarations to the tree. */
12091 break;
12093 default:
12094 break;
12097 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
12098 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
12100 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12102 TYPE_SIZE (t) = TYPE_SIZE (type);
12103 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
12104 TYPE_SIZES_GIMPLIFIED (t) = 1;
12108 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
12109 a size or position, has had all of its SAVE_EXPRs evaluated.
12110 We add any required statements to *STMT_P. */
12112 void
12113 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
12115 tree expr = *expr_p;
12117 /* We don't do anything if the value isn't there, is constant, or contains
12118 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
12119 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
12120 will want to replace it with a new variable, but that will cause problems
12121 if this type is from outside the function. It's OK to have that here. */
12122 if (is_gimple_sizepos (expr))
12123 return;
12125 *expr_p = unshare_expr (expr);
12127 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
12128 if the def vanishes. */
12129 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue, false);
12132 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
12133 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
12134 is true, also gimplify the parameters. */
12136 gbind *
12137 gimplify_body (tree fndecl, bool do_parms)
12139 location_t saved_location = input_location;
12140 gimple_seq parm_stmts, seq;
12141 gimple *outer_stmt;
12142 gbind *outer_bind;
12143 struct cgraph_node *cgn;
12145 timevar_push (TV_TREE_GIMPLIFY);
12147 init_tree_ssa (cfun);
12149 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
12150 gimplification. */
12151 default_rtl_profile ();
12153 gcc_assert (gimplify_ctxp == NULL);
12154 push_gimplify_context (true);
12156 if (flag_openacc || flag_openmp)
12158 gcc_assert (gimplify_omp_ctxp == NULL);
12159 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
12160 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
12163 /* Unshare most shared trees in the body and in that of any nested functions.
12164 It would seem we don't have to do this for nested functions because
12165 they are supposed to be output and then the outer function gimplified
12166 first, but the g++ front end doesn't always do it that way. */
12167 unshare_body (fndecl);
12168 unvisit_body (fndecl);
12170 cgn = cgraph_node::get (fndecl);
12171 if (cgn && cgn->origin)
12172 nonlocal_vlas = new hash_set<tree>;
12174 /* Make sure input_location isn't set to something weird. */
12175 input_location = DECL_SOURCE_LOCATION (fndecl);
12177 /* Resolve callee-copies. This has to be done before processing
12178 the body so that DECL_VALUE_EXPR gets processed correctly. */
12179 parm_stmts = do_parms ? gimplify_parameters () : NULL;
12181 /* Gimplify the function's body. */
12182 seq = NULL;
12183 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
12184 outer_stmt = gimple_seq_first_stmt (seq);
12185 if (!outer_stmt)
12187 outer_stmt = gimple_build_nop ();
12188 gimplify_seq_add_stmt (&seq, outer_stmt);
12191 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
12192 not the case, wrap everything in a GIMPLE_BIND to make it so. */
12193 if (gimple_code (outer_stmt) == GIMPLE_BIND
12194 && gimple_seq_first (seq) == gimple_seq_last (seq))
12195 outer_bind = as_a <gbind *> (outer_stmt);
12196 else
12197 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
12199 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12201 /* If we had callee-copies statements, insert them at the beginning
12202 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
12203 if (!gimple_seq_empty_p (parm_stmts))
12205 tree parm;
12207 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
12208 gimple_bind_set_body (outer_bind, parm_stmts);
12210 for (parm = DECL_ARGUMENTS (current_function_decl);
12211 parm; parm = DECL_CHAIN (parm))
12212 if (DECL_HAS_VALUE_EXPR_P (parm))
12214 DECL_HAS_VALUE_EXPR_P (parm) = 0;
12215 DECL_IGNORED_P (parm) = 0;
12219 if (nonlocal_vlas)
12221 if (nonlocal_vla_vars)
12223 /* tree-nested.c may later on call declare_vars (..., true);
12224 which relies on BLOCK_VARS chain to be the tail of the
12225 gimple_bind_vars chain. Ensure we don't violate that
12226 assumption. */
12227 if (gimple_bind_block (outer_bind)
12228 == DECL_INITIAL (current_function_decl))
12229 declare_vars (nonlocal_vla_vars, outer_bind, true);
12230 else
12231 BLOCK_VARS (DECL_INITIAL (current_function_decl))
12232 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
12233 nonlocal_vla_vars);
12234 nonlocal_vla_vars = NULL_TREE;
12236 delete nonlocal_vlas;
12237 nonlocal_vlas = NULL;
12240 if ((flag_openacc || flag_openmp || flag_openmp_simd)
12241 && gimplify_omp_ctxp)
12243 delete_omp_context (gimplify_omp_ctxp);
12244 gimplify_omp_ctxp = NULL;
12247 pop_gimplify_context (outer_bind);
12248 gcc_assert (gimplify_ctxp == NULL);
12250 if (flag_checking && !seen_error ())
12251 verify_gimple_in_seq (gimple_bind_body (outer_bind));
12253 timevar_pop (TV_TREE_GIMPLIFY);
12254 input_location = saved_location;
12256 return outer_bind;
12259 typedef char *char_p; /* For DEF_VEC_P. */
12261 /* Return whether we should exclude FNDECL from instrumentation. */
12263 static bool
12264 flag_instrument_functions_exclude_p (tree fndecl)
12266 vec<char_p> *v;
12268 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
12269 if (v && v->length () > 0)
12271 const char *name;
12272 int i;
12273 char *s;
12275 name = lang_hooks.decl_printable_name (fndecl, 0);
12276 FOR_EACH_VEC_ELT (*v, i, s)
12277 if (strstr (name, s) != NULL)
12278 return true;
12281 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
12282 if (v && v->length () > 0)
12284 const char *name;
12285 int i;
12286 char *s;
12288 name = DECL_SOURCE_FILE (fndecl);
12289 FOR_EACH_VEC_ELT (*v, i, s)
12290 if (strstr (name, s) != NULL)
12291 return true;
12294 return false;
12297 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
12298 node for the function we want to gimplify.
12300 Return the sequence of GIMPLE statements corresponding to the body
12301 of FNDECL. */
12303 void
12304 gimplify_function_tree (tree fndecl)
12306 tree parm, ret;
12307 gimple_seq seq;
12308 gbind *bind;
12310 gcc_assert (!gimple_body (fndecl));
12312 if (DECL_STRUCT_FUNCTION (fndecl))
12313 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
12314 else
12315 push_struct_function (fndecl);
12317 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
12318 if necessary. */
12319 cfun->curr_properties |= PROP_gimple_lva;
12321 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
12323 /* Preliminarily mark non-addressed complex variables as eligible
12324 for promotion to gimple registers. We'll transform their uses
12325 as we find them. */
12326 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
12327 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
12328 && !TREE_THIS_VOLATILE (parm)
12329 && !needs_to_live_in_memory (parm))
12330 DECL_GIMPLE_REG_P (parm) = 1;
12333 ret = DECL_RESULT (fndecl);
12334 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
12335 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
12336 && !needs_to_live_in_memory (ret))
12337 DECL_GIMPLE_REG_P (ret) = 1;
12339 bind = gimplify_body (fndecl, true);
12341 /* The tree body of the function is no longer needed, replace it
12342 with the new GIMPLE body. */
12343 seq = NULL;
12344 gimple_seq_add_stmt (&seq, bind);
12345 gimple_set_body (fndecl, seq);
12347 /* If we're instrumenting function entry/exit, then prepend the call to
12348 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
12349 catch the exit hook. */
12350 /* ??? Add some way to ignore exceptions for this TFE. */
12351 if (flag_instrument_function_entry_exit
12352 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
12353 && !flag_instrument_functions_exclude_p (fndecl))
12355 tree x;
12356 gbind *new_bind;
12357 gimple *tf;
12358 gimple_seq cleanup = NULL, body = NULL;
12359 tree tmp_var;
12360 gcall *call;
12362 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12363 call = gimple_build_call (x, 1, integer_zero_node);
12364 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12365 gimple_call_set_lhs (call, tmp_var);
12366 gimplify_seq_add_stmt (&cleanup, call);
12367 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
12368 call = gimple_build_call (x, 2,
12369 build_fold_addr_expr (current_function_decl),
12370 tmp_var);
12371 gimplify_seq_add_stmt (&cleanup, call);
12372 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
12374 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
12375 call = gimple_build_call (x, 1, integer_zero_node);
12376 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
12377 gimple_call_set_lhs (call, tmp_var);
12378 gimplify_seq_add_stmt (&body, call);
12379 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
12380 call = gimple_build_call (x, 2,
12381 build_fold_addr_expr (current_function_decl),
12382 tmp_var);
12383 gimplify_seq_add_stmt (&body, call);
12384 gimplify_seq_add_stmt (&body, tf);
12385 new_bind = gimple_build_bind (NULL, body, NULL);
12387 /* Replace the current function body with the body
12388 wrapped in the try/finally TF. */
12389 seq = NULL;
12390 gimple_seq_add_stmt (&seq, new_bind);
12391 gimple_set_body (fndecl, seq);
12392 bind = new_bind;
12395 if ((flag_sanitize & SANITIZE_THREAD) != 0
12396 && !lookup_attribute ("no_sanitize_thread", DECL_ATTRIBUTES (fndecl)))
12398 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
12399 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
12400 gbind *new_bind = gimple_build_bind (NULL, tf, NULL);
12401 /* Replace the current function body with the body
12402 wrapped in the try/finally TF. */
12403 seq = NULL;
12404 gimple_seq_add_stmt (&seq, new_bind);
12405 gimple_set_body (fndecl, seq);
12408 DECL_SAVED_TREE (fndecl) = NULL_TREE;
12409 cfun->curr_properties |= PROP_gimple_any;
12411 pop_cfun ();
12413 dump_function (TDI_generic, fndecl);
12416 /* Return a dummy expression of type TYPE in order to keep going after an
12417 error. */
12419 static tree
12420 dummy_object (tree type)
12422 tree t = build_int_cst (build_pointer_type (type), 0);
12423 return build2 (MEM_REF, type, t, t);
12426 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
12427 builtin function, but a very special sort of operator. */
12429 enum gimplify_status
12430 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
12431 gimple_seq *post_p ATTRIBUTE_UNUSED)
12433 tree promoted_type, have_va_type;
12434 tree valist = TREE_OPERAND (*expr_p, 0);
12435 tree type = TREE_TYPE (*expr_p);
12436 tree t, tag, aptag;
12437 location_t loc = EXPR_LOCATION (*expr_p);
12439 /* Verify that valist is of the proper type. */
12440 have_va_type = TREE_TYPE (valist);
12441 if (have_va_type == error_mark_node)
12442 return GS_ERROR;
12443 have_va_type = targetm.canonical_va_list_type (have_va_type);
12444 if (have_va_type == NULL_TREE
12445 && TREE_CODE (valist) == ADDR_EXPR)
12446 /* Handle 'Case 1: Not an array type' from c-common.c/build_va_arg. */
12447 have_va_type
12448 = targetm.canonical_va_list_type (TREE_TYPE (TREE_TYPE (valist)));
12449 gcc_assert (have_va_type != NULL_TREE);
12451 /* Generate a diagnostic for requesting data of a type that cannot
12452 be passed through `...' due to type promotion at the call site. */
12453 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
12454 != type)
12456 static bool gave_help;
12457 bool warned;
12458 /* Use the expansion point to handle cases such as passing bool (defined
12459 in a system header) through `...'. */
12460 source_location xloc
12461 = expansion_point_location_if_in_system_header (loc);
12463 /* Unfortunately, this is merely undefined, rather than a constraint
12464 violation, so we cannot make this an error. If this call is never
12465 executed, the program is still strictly conforming. */
12466 warned = warning_at (xloc, 0,
12467 "%qT is promoted to %qT when passed through %<...%>",
12468 type, promoted_type);
12469 if (!gave_help && warned)
12471 gave_help = true;
12472 inform (xloc, "(so you should pass %qT not %qT to %<va_arg%>)",
12473 promoted_type, type);
12476 /* We can, however, treat "undefined" any way we please.
12477 Call abort to encourage the user to fix the program. */
12478 if (warned)
12479 inform (xloc, "if this code is reached, the program will abort");
12480 /* Before the abort, allow the evaluation of the va_list
12481 expression to exit or longjmp. */
12482 gimplify_and_add (valist, pre_p);
12483 t = build_call_expr_loc (loc,
12484 builtin_decl_implicit (BUILT_IN_TRAP), 0);
12485 gimplify_and_add (t, pre_p);
12487 /* This is dead code, but go ahead and finish so that the
12488 mode of the result comes out right. */
12489 *expr_p = dummy_object (type);
12490 return GS_ALL_DONE;
12493 tag = build_int_cst (build_pointer_type (type), 0);
12494 aptag = build_int_cst (TREE_TYPE (valist), 0);
12496 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
12497 valist, tag, aptag);
12499 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
12500 needs to be expanded. */
12501 cfun->curr_properties &= ~PROP_gimple_lva;
12503 return GS_OK;
12506 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
12508 DST/SRC are the destination and source respectively. You can pass
12509 ungimplified trees in DST or SRC, in which case they will be
12510 converted to a gimple operand if necessary.
12512 This function returns the newly created GIMPLE_ASSIGN tuple. */
12514 gimple *
12515 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
12517 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
12518 gimplify_and_add (t, seq_p);
12519 ggc_free (t);
12520 return gimple_seq_last_stmt (*seq_p);
12523 inline hashval_t
12524 gimplify_hasher::hash (const elt_t *p)
12526 tree t = p->val;
12527 return iterative_hash_expr (t, 0);
12530 inline bool
12531 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
12533 tree t1 = p1->val;
12534 tree t2 = p2->val;
12535 enum tree_code code = TREE_CODE (t1);
12537 if (TREE_CODE (t2) != code
12538 || TREE_TYPE (t1) != TREE_TYPE (t2))
12539 return false;
12541 if (!operand_equal_p (t1, t2, 0))
12542 return false;
12544 /* Only allow them to compare equal if they also hash equal; otherwise
12545 results are nondeterminate, and we fail bootstrap comparison. */
12546 gcc_checking_assert (hash (p1) == hash (p2));
12548 return true;