* gfortran.dg/debug/pr46756.f: Remove XFAIL for AIX.
[official-gcc.git] / gcc / gimplify.c
blob03203c0f155b681205608229767e0ad9601638ad
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2015 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"
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 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
88 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
89 | GOVD_LOCAL)
93 enum omp_region_type
95 ORT_WORKSHARE = 0,
96 ORT_SIMD = 1,
97 ORT_PARALLEL = 2,
98 ORT_COMBINED_PARALLEL = 3,
99 ORT_TASK = 4,
100 ORT_UNTIED_TASK = 5,
101 ORT_TEAMS = 8,
102 ORT_COMBINED_TEAMS = 9,
103 /* Data region. */
104 ORT_TARGET_DATA = 16,
105 /* Data region with offloading. */
106 ORT_TARGET = 32,
107 ORT_COMBINED_TARGET = 33,
108 /* Dummy OpenMP region, used to disable expansion of
109 DECL_VALUE_EXPRs in taskloop pre body. */
110 ORT_NONE = 64
113 /* Gimplify hashtable helper. */
115 struct gimplify_hasher : free_ptr_hash <elt_t>
117 static inline hashval_t hash (const elt_t *);
118 static inline bool equal (const elt_t *, const elt_t *);
121 struct gimplify_ctx
123 struct gimplify_ctx *prev_context;
125 vec<gbind *> bind_expr_stack;
126 tree temps;
127 gimple_seq conditional_cleanups;
128 tree exit_label;
129 tree return_temp;
131 vec<tree> case_labels;
132 /* The formal temporary table. Should this be persistent? */
133 hash_table<gimplify_hasher> *temp_htab;
135 int conditions;
136 bool save_stack;
137 bool into_ssa;
138 bool allow_rhs_cond_expr;
139 bool in_cleanup_point_expr;
142 struct gimplify_omp_ctx
144 struct gimplify_omp_ctx *outer_context;
145 splay_tree variables;
146 hash_set<tree> *privatized_types;
147 /* Iteration variables in an OMP_FOR. */
148 vec<tree> loop_iter_var;
149 location_t location;
150 enum omp_clause_default_kind default_kind;
151 enum omp_region_type region_type;
152 bool combined_loop;
153 bool distribute;
154 bool target_map_scalars_firstprivate;
155 bool target_map_pointers_as_0len_arrays;
156 bool target_firstprivatize_array_bases;
159 static struct gimplify_ctx *gimplify_ctxp;
160 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
162 /* Forward declaration. */
163 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
165 /* Shorter alias name for the above function for use in gimplify.c
166 only. */
168 static inline void
169 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
171 gimple_seq_add_stmt_without_update (seq_p, gs);
174 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
175 NULL, a new sequence is allocated. This function is
176 similar to gimple_seq_add_seq, but does not scan the operands.
177 During gimplification, we need to manipulate statement sequences
178 before the def/use vectors have been constructed. */
180 static void
181 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
183 gimple_stmt_iterator si;
185 if (src == NULL)
186 return;
188 si = gsi_last (*dst_p);
189 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
193 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
194 and popping gimplify contexts. */
196 static struct gimplify_ctx *ctx_pool = NULL;
198 /* Return a gimplify context struct from the pool. */
200 static inline struct gimplify_ctx *
201 ctx_alloc (void)
203 struct gimplify_ctx * c = ctx_pool;
205 if (c)
206 ctx_pool = c->prev_context;
207 else
208 c = XNEW (struct gimplify_ctx);
210 memset (c, '\0', sizeof (*c));
211 return c;
214 /* Put gimplify context C back into the pool. */
216 static inline void
217 ctx_free (struct gimplify_ctx *c)
219 c->prev_context = ctx_pool;
220 ctx_pool = c;
223 /* Free allocated ctx stack memory. */
225 void
226 free_gimplify_stack (void)
228 struct gimplify_ctx *c;
230 while ((c = ctx_pool))
232 ctx_pool = c->prev_context;
233 free (c);
238 /* Set up a context for the gimplifier. */
240 void
241 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
243 struct gimplify_ctx *c = ctx_alloc ();
245 c->prev_context = gimplify_ctxp;
246 gimplify_ctxp = c;
247 gimplify_ctxp->into_ssa = in_ssa;
248 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
251 /* Tear down a context for the gimplifier. If BODY is non-null, then
252 put the temporaries into the outer BIND_EXPR. Otherwise, put them
253 in the local_decls.
255 BODY is not a sequence, but the first tuple in a sequence. */
257 void
258 pop_gimplify_context (gimple *body)
260 struct gimplify_ctx *c = gimplify_ctxp;
262 gcc_assert (c
263 && (!c->bind_expr_stack.exists ()
264 || c->bind_expr_stack.is_empty ()));
265 c->bind_expr_stack.release ();
266 gimplify_ctxp = c->prev_context;
268 if (body)
269 declare_vars (c->temps, body, false);
270 else
271 record_vars (c->temps);
273 delete c->temp_htab;
274 c->temp_htab = NULL;
275 ctx_free (c);
278 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
280 static void
281 gimple_push_bind_expr (gbind *bind_stmt)
283 gimplify_ctxp->bind_expr_stack.reserve (8);
284 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
287 /* Pop the first element off the stack of bindings. */
289 static void
290 gimple_pop_bind_expr (void)
292 gimplify_ctxp->bind_expr_stack.pop ();
295 /* Return the first element of the stack of bindings. */
297 gbind *
298 gimple_current_bind_expr (void)
300 return gimplify_ctxp->bind_expr_stack.last ();
303 /* Return the stack of bindings created during gimplification. */
305 vec<gbind *>
306 gimple_bind_expr_stack (void)
308 return gimplify_ctxp->bind_expr_stack;
311 /* Return true iff there is a COND_EXPR between us and the innermost
312 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
314 static bool
315 gimple_conditional_context (void)
317 return gimplify_ctxp->conditions > 0;
320 /* Note that we've entered a COND_EXPR. */
322 static void
323 gimple_push_condition (void)
325 #ifdef ENABLE_GIMPLE_CHECKING
326 if (gimplify_ctxp->conditions == 0)
327 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
328 #endif
329 ++(gimplify_ctxp->conditions);
332 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
333 now, add any conditional cleanups we've seen to the prequeue. */
335 static void
336 gimple_pop_condition (gimple_seq *pre_p)
338 int conds = --(gimplify_ctxp->conditions);
340 gcc_assert (conds >= 0);
341 if (conds == 0)
343 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
344 gimplify_ctxp->conditional_cleanups = NULL;
348 /* A stable comparison routine for use with splay trees and DECLs. */
350 static int
351 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
353 tree a = (tree) xa;
354 tree b = (tree) xb;
356 return DECL_UID (a) - DECL_UID (b);
359 /* Create a new omp construct that deals with variable remapping. */
361 static struct gimplify_omp_ctx *
362 new_omp_context (enum omp_region_type region_type)
364 struct gimplify_omp_ctx *c;
366 c = XCNEW (struct gimplify_omp_ctx);
367 c->outer_context = gimplify_omp_ctxp;
368 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
369 c->privatized_types = new hash_set<tree>;
370 c->location = input_location;
371 c->region_type = region_type;
372 if ((region_type & ORT_TASK) == 0)
373 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
374 else
375 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
377 return c;
380 /* Destroy an omp construct that deals with variable remapping. */
382 static void
383 delete_omp_context (struct gimplify_omp_ctx *c)
385 splay_tree_delete (c->variables);
386 delete c->privatized_types;
387 c->loop_iter_var.release ();
388 XDELETE (c);
391 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
392 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
394 /* Both gimplify the statement T and append it to *SEQ_P. This function
395 behaves exactly as gimplify_stmt, but you don't have to pass T as a
396 reference. */
398 void
399 gimplify_and_add (tree t, gimple_seq *seq_p)
401 gimplify_stmt (&t, seq_p);
404 /* Gimplify statement T into sequence *SEQ_P, and return the first
405 tuple in the sequence of generated tuples for this statement.
406 Return NULL if gimplifying T produced no tuples. */
408 static gimple *
409 gimplify_and_return_first (tree t, gimple_seq *seq_p)
411 gimple_stmt_iterator last = gsi_last (*seq_p);
413 gimplify_and_add (t, seq_p);
415 if (!gsi_end_p (last))
417 gsi_next (&last);
418 return gsi_stmt (last);
420 else
421 return gimple_seq_first_stmt (*seq_p);
424 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
425 LHS, or for a call argument. */
427 static bool
428 is_gimple_mem_rhs (tree t)
430 /* If we're dealing with a renamable type, either source or dest must be
431 a renamed variable. */
432 if (is_gimple_reg_type (TREE_TYPE (t)))
433 return is_gimple_val (t);
434 else
435 return is_gimple_val (t) || is_gimple_lvalue (t);
438 /* Return true if T is a CALL_EXPR or an expression that can be
439 assigned to a temporary. Note that this predicate should only be
440 used during gimplification. See the rationale for this in
441 gimplify_modify_expr. */
443 static bool
444 is_gimple_reg_rhs_or_call (tree t)
446 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
447 || TREE_CODE (t) == CALL_EXPR);
450 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
451 this predicate should only be used during gimplification. See the
452 rationale for this in gimplify_modify_expr. */
454 static bool
455 is_gimple_mem_rhs_or_call (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)
463 || TREE_CODE (t) == CALL_EXPR);
466 /* Create a temporary with a name derived from VAL. Subroutine of
467 lookup_tmp_var; nobody else should call this function. */
469 static inline tree
470 create_tmp_from_val (tree val)
472 /* Drop all qualifiers and address-space information from the value type. */
473 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
474 tree var = create_tmp_var (type, get_name (val));
475 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
476 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
477 DECL_GIMPLE_REG_P (var) = 1;
478 return var;
481 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
482 an existing expression temporary. */
484 static tree
485 lookup_tmp_var (tree val, bool is_formal)
487 tree ret;
489 /* If not optimizing, never really reuse a temporary. local-alloc
490 won't allocate any variable that is used in more than one basic
491 block, which means it will go into memory, causing much extra
492 work in reload and final and poorer code generation, outweighing
493 the extra memory allocation here. */
494 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
495 ret = create_tmp_from_val (val);
496 else
498 elt_t elt, *elt_p;
499 elt_t **slot;
501 elt.val = val;
502 if (!gimplify_ctxp->temp_htab)
503 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
504 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
505 if (*slot == NULL)
507 elt_p = XNEW (elt_t);
508 elt_p->val = val;
509 elt_p->temp = ret = create_tmp_from_val (val);
510 *slot = elt_p;
512 else
514 elt_p = *slot;
515 ret = elt_p->temp;
519 return ret;
522 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
524 static tree
525 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
526 bool is_formal)
528 tree t, mod;
530 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
531 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
532 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
533 fb_rvalue);
535 if (gimplify_ctxp->into_ssa
536 && is_gimple_reg_type (TREE_TYPE (val)))
537 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
538 else
539 t = lookup_tmp_var (val, is_formal);
541 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
543 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
545 /* gimplify_modify_expr might want to reduce this further. */
546 gimplify_and_add (mod, pre_p);
547 ggc_free (mod);
549 return t;
552 /* Return a formal temporary variable initialized with VAL. PRE_P is as
553 in gimplify_expr. Only use this function if:
555 1) The value of the unfactored expression represented by VAL will not
556 change between the initialization and use of the temporary, and
557 2) The temporary will not be otherwise modified.
559 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
560 and #2 means it is inappropriate for && temps.
562 For other cases, use get_initialized_tmp_var instead. */
564 tree
565 get_formal_tmp_var (tree val, gimple_seq *pre_p)
567 return internal_get_tmp_var (val, pre_p, NULL, true);
570 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
571 are as in gimplify_expr. */
573 tree
574 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
576 return internal_get_tmp_var (val, pre_p, post_p, false);
579 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
580 generate debug info for them; otherwise don't. */
582 void
583 declare_vars (tree vars, gimple *gs, bool debug_info)
585 tree last = vars;
586 if (last)
588 tree temps, block;
590 gbind *scope = as_a <gbind *> (gs);
592 temps = nreverse (last);
594 block = gimple_bind_block (scope);
595 gcc_assert (!block || TREE_CODE (block) == BLOCK);
596 if (!block || !debug_info)
598 DECL_CHAIN (last) = gimple_bind_vars (scope);
599 gimple_bind_set_vars (scope, temps);
601 else
603 /* We need to attach the nodes both to the BIND_EXPR and to its
604 associated BLOCK for debugging purposes. The key point here
605 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
606 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
607 if (BLOCK_VARS (block))
608 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
609 else
611 gimple_bind_set_vars (scope,
612 chainon (gimple_bind_vars (scope), temps));
613 BLOCK_VARS (block) = temps;
619 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
620 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
621 no such upper bound can be obtained. */
623 static void
624 force_constant_size (tree var)
626 /* The only attempt we make is by querying the maximum size of objects
627 of the variable's type. */
629 HOST_WIDE_INT max_size;
631 gcc_assert (TREE_CODE (var) == VAR_DECL);
633 max_size = max_int_size_in_bytes (TREE_TYPE (var));
635 gcc_assert (max_size >= 0);
637 DECL_SIZE_UNIT (var)
638 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
639 DECL_SIZE (var)
640 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
643 /* Push the temporary variable TMP into the current binding. */
645 void
646 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
648 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
650 /* Later processing assumes that the object size is constant, which might
651 not be true at this point. Force the use of a constant upper bound in
652 this case. */
653 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
654 force_constant_size (tmp);
656 DECL_CONTEXT (tmp) = fn->decl;
657 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
659 record_vars_into (tmp, fn->decl);
662 /* Push the temporary variable TMP into the current binding. */
664 void
665 gimple_add_tmp_var (tree tmp)
667 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
669 /* Later processing assumes that the object size is constant, which might
670 not be true at this point. Force the use of a constant upper bound in
671 this case. */
672 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
673 force_constant_size (tmp);
675 DECL_CONTEXT (tmp) = current_function_decl;
676 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
678 if (gimplify_ctxp)
680 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
681 gimplify_ctxp->temps = tmp;
683 /* Mark temporaries local within the nearest enclosing parallel. */
684 if (gimplify_omp_ctxp)
686 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
687 while (ctx
688 && (ctx->region_type == ORT_WORKSHARE
689 || ctx->region_type == ORT_SIMD))
690 ctx = ctx->outer_context;
691 if (ctx)
692 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
695 else if (cfun)
696 record_vars (tmp);
697 else
699 gimple_seq body_seq;
701 /* This case is for nested functions. We need to expose the locals
702 they create. */
703 body_seq = gimple_body (current_function_decl);
704 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
710 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
711 nodes that are referenced more than once in GENERIC functions. This is
712 necessary because gimplification (translation into GIMPLE) is performed
713 by modifying tree nodes in-place, so gimplication of a shared node in a
714 first context could generate an invalid GIMPLE form in a second context.
716 This is achieved with a simple mark/copy/unmark algorithm that walks the
717 GENERIC representation top-down, marks nodes with TREE_VISITED the first
718 time it encounters them, duplicates them if they already have TREE_VISITED
719 set, and finally removes the TREE_VISITED marks it has set.
721 The algorithm works only at the function level, i.e. it generates a GENERIC
722 representation of a function with no nodes shared within the function when
723 passed a GENERIC function (except for nodes that are allowed to be shared).
725 At the global level, it is also necessary to unshare tree nodes that are
726 referenced in more than one function, for the same aforementioned reason.
727 This requires some cooperation from the front-end. There are 2 strategies:
729 1. Manual unsharing. The front-end needs to call unshare_expr on every
730 expression that might end up being shared across functions.
732 2. Deep unsharing. This is an extension of regular unsharing. Instead
733 of calling unshare_expr on expressions that might be shared across
734 functions, the front-end pre-marks them with TREE_VISITED. This will
735 ensure that they are unshared on the first reference within functions
736 when the regular unsharing algorithm runs. The counterpart is that
737 this algorithm must look deeper than for manual unsharing, which is
738 specified by LANG_HOOKS_DEEP_UNSHARING.
740 If there are only few specific cases of node sharing across functions, it is
741 probably easier for a front-end to unshare the expressions manually. On the
742 contrary, if the expressions generated at the global level are as widespread
743 as expressions generated within functions, deep unsharing is very likely the
744 way to go. */
746 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
747 These nodes model computations that must be done once. If we were to
748 unshare something like SAVE_EXPR(i++), the gimplification process would
749 create wrong code. However, if DATA is non-null, it must hold a pointer
750 set that is used to unshare the subtrees of these nodes. */
752 static tree
753 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
755 tree t = *tp;
756 enum tree_code code = TREE_CODE (t);
758 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
759 copy their subtrees if we can make sure to do it only once. */
760 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
762 if (data && !((hash_set<tree> *)data)->add (t))
764 else
765 *walk_subtrees = 0;
768 /* Stop at types, decls, constants like copy_tree_r. */
769 else if (TREE_CODE_CLASS (code) == tcc_type
770 || TREE_CODE_CLASS (code) == tcc_declaration
771 || TREE_CODE_CLASS (code) == tcc_constant
772 /* We can't do anything sensible with a BLOCK used as an
773 expression, but we also can't just die when we see it
774 because of non-expression uses. So we avert our eyes
775 and cross our fingers. Silly Java. */
776 || code == BLOCK)
777 *walk_subtrees = 0;
779 /* Cope with the statement expression extension. */
780 else if (code == STATEMENT_LIST)
783 /* Leave the bulk of the work to copy_tree_r itself. */
784 else
785 copy_tree_r (tp, walk_subtrees, NULL);
787 return NULL_TREE;
790 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
791 If *TP has been visited already, then *TP is deeply copied by calling
792 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
794 static tree
795 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
797 tree t = *tp;
798 enum tree_code code = TREE_CODE (t);
800 /* Skip types, decls, and constants. But we do want to look at their
801 types and the bounds of types. Mark them as visited so we properly
802 unmark their subtrees on the unmark pass. If we've already seen them,
803 don't look down further. */
804 if (TREE_CODE_CLASS (code) == tcc_type
805 || TREE_CODE_CLASS (code) == tcc_declaration
806 || TREE_CODE_CLASS (code) == tcc_constant)
808 if (TREE_VISITED (t))
809 *walk_subtrees = 0;
810 else
811 TREE_VISITED (t) = 1;
814 /* If this node has been visited already, unshare it and don't look
815 any deeper. */
816 else if (TREE_VISITED (t))
818 walk_tree (tp, mostly_copy_tree_r, data, NULL);
819 *walk_subtrees = 0;
822 /* Otherwise, mark the node as visited and keep looking. */
823 else
824 TREE_VISITED (t) = 1;
826 return NULL_TREE;
829 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
830 copy_if_shared_r callback unmodified. */
832 static inline void
833 copy_if_shared (tree *tp, void *data)
835 walk_tree (tp, copy_if_shared_r, data, NULL);
838 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
839 any nested functions. */
841 static void
842 unshare_body (tree fndecl)
844 struct cgraph_node *cgn = cgraph_node::get (fndecl);
845 /* If the language requires deep unsharing, we need a pointer set to make
846 sure we don't repeatedly unshare subtrees of unshareable nodes. */
847 hash_set<tree> *visited
848 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
850 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
851 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
852 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
854 delete visited;
856 if (cgn)
857 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
858 unshare_body (cgn->decl);
861 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
862 Subtrees are walked until the first unvisited node is encountered. */
864 static tree
865 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
867 tree t = *tp;
869 /* If this node has been visited, unmark it and keep looking. */
870 if (TREE_VISITED (t))
871 TREE_VISITED (t) = 0;
873 /* Otherwise, don't look any deeper. */
874 else
875 *walk_subtrees = 0;
877 return NULL_TREE;
880 /* Unmark the visited trees rooted at *TP. */
882 static inline void
883 unmark_visited (tree *tp)
885 walk_tree (tp, unmark_visited_r, NULL, NULL);
888 /* Likewise, but mark all trees as not visited. */
890 static void
891 unvisit_body (tree fndecl)
893 struct cgraph_node *cgn = cgraph_node::get (fndecl);
895 unmark_visited (&DECL_SAVED_TREE (fndecl));
896 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
897 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
899 if (cgn)
900 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
901 unvisit_body (cgn->decl);
904 /* Unconditionally make an unshared copy of EXPR. This is used when using
905 stored expressions which span multiple functions, such as BINFO_VTABLE,
906 as the normal unsharing process can't tell that they're shared. */
908 tree
909 unshare_expr (tree expr)
911 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
912 return expr;
915 /* Worker for unshare_expr_without_location. */
917 static tree
918 prune_expr_location (tree *tp, int *walk_subtrees, void *)
920 if (EXPR_P (*tp))
921 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
922 else
923 *walk_subtrees = 0;
924 return NULL_TREE;
927 /* Similar to unshare_expr but also prune all expression locations
928 from EXPR. */
930 tree
931 unshare_expr_without_location (tree expr)
933 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
934 if (EXPR_P (expr))
935 walk_tree (&expr, prune_expr_location, NULL, NULL);
936 return expr;
939 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
940 contain statements and have a value. Assign its value to a temporary
941 and give it void_type_node. Return the temporary, or NULL_TREE if
942 WRAPPER was already void. */
944 tree
945 voidify_wrapper_expr (tree wrapper, tree temp)
947 tree type = TREE_TYPE (wrapper);
948 if (type && !VOID_TYPE_P (type))
950 tree *p;
952 /* Set p to point to the body of the wrapper. Loop until we find
953 something that isn't a wrapper. */
954 for (p = &wrapper; p && *p; )
956 switch (TREE_CODE (*p))
958 case BIND_EXPR:
959 TREE_SIDE_EFFECTS (*p) = 1;
960 TREE_TYPE (*p) = void_type_node;
961 /* For a BIND_EXPR, the body is operand 1. */
962 p = &BIND_EXPR_BODY (*p);
963 break;
965 case CLEANUP_POINT_EXPR:
966 case TRY_FINALLY_EXPR:
967 case TRY_CATCH_EXPR:
968 TREE_SIDE_EFFECTS (*p) = 1;
969 TREE_TYPE (*p) = void_type_node;
970 p = &TREE_OPERAND (*p, 0);
971 break;
973 case STATEMENT_LIST:
975 tree_stmt_iterator i = tsi_last (*p);
976 TREE_SIDE_EFFECTS (*p) = 1;
977 TREE_TYPE (*p) = void_type_node;
978 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
980 break;
982 case COMPOUND_EXPR:
983 /* Advance to the last statement. Set all container types to
984 void. */
985 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
987 TREE_SIDE_EFFECTS (*p) = 1;
988 TREE_TYPE (*p) = void_type_node;
990 break;
992 case TRANSACTION_EXPR:
993 TREE_SIDE_EFFECTS (*p) = 1;
994 TREE_TYPE (*p) = void_type_node;
995 p = &TRANSACTION_EXPR_BODY (*p);
996 break;
998 default:
999 /* Assume that any tree upon which voidify_wrapper_expr is
1000 directly called is a wrapper, and that its body is op0. */
1001 if (p == &wrapper)
1003 TREE_SIDE_EFFECTS (*p) = 1;
1004 TREE_TYPE (*p) = void_type_node;
1005 p = &TREE_OPERAND (*p, 0);
1006 break;
1008 goto out;
1012 out:
1013 if (p == NULL || IS_EMPTY_STMT (*p))
1014 temp = NULL_TREE;
1015 else if (temp)
1017 /* The wrapper is on the RHS of an assignment that we're pushing
1018 down. */
1019 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1020 || TREE_CODE (temp) == MODIFY_EXPR);
1021 TREE_OPERAND (temp, 1) = *p;
1022 *p = temp;
1024 else
1026 temp = create_tmp_var (type, "retval");
1027 *p = build2 (INIT_EXPR, type, temp, *p);
1030 return temp;
1033 return NULL_TREE;
1036 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1037 a temporary through which they communicate. */
1039 static void
1040 build_stack_save_restore (gcall **save, gcall **restore)
1042 tree tmp_var;
1044 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1045 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1046 gimple_call_set_lhs (*save, tmp_var);
1048 *restore
1049 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1050 1, tmp_var);
1053 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1055 static enum gimplify_status
1056 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1058 tree bind_expr = *expr_p;
1059 bool old_save_stack = gimplify_ctxp->save_stack;
1060 tree t;
1061 gbind *bind_stmt;
1062 gimple_seq body, cleanup;
1063 gcall *stack_save;
1064 location_t start_locus = 0, end_locus = 0;
1066 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1068 /* Mark variables seen in this bind expr. */
1069 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1071 if (TREE_CODE (t) == VAR_DECL)
1073 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1075 /* Mark variable as local. */
1076 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1077 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1078 || splay_tree_lookup (ctx->variables,
1079 (splay_tree_key) t) == NULL))
1081 if (ctx->region_type == ORT_SIMD
1082 && TREE_ADDRESSABLE (t)
1083 && !TREE_STATIC (t))
1084 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1085 else
1086 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1089 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1091 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1092 cfun->has_local_explicit_reg_vars = true;
1095 /* Preliminarily mark non-addressed complex variables as eligible
1096 for promotion to gimple registers. We'll transform their uses
1097 as we find them. */
1098 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1099 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1100 && !TREE_THIS_VOLATILE (t)
1101 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1102 && !needs_to_live_in_memory (t))
1103 DECL_GIMPLE_REG_P (t) = 1;
1106 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1107 BIND_EXPR_BLOCK (bind_expr));
1108 gimple_push_bind_expr (bind_stmt);
1110 gimplify_ctxp->save_stack = false;
1112 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1113 body = NULL;
1114 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1115 gimple_bind_set_body (bind_stmt, body);
1117 /* Source location wise, the cleanup code (stack_restore and clobbers)
1118 belongs to the end of the block, so propagate what we have. The
1119 stack_save operation belongs to the beginning of block, which we can
1120 infer from the bind_expr directly if the block has no explicit
1121 assignment. */
1122 if (BIND_EXPR_BLOCK (bind_expr))
1124 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1125 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1127 if (start_locus == 0)
1128 start_locus = EXPR_LOCATION (bind_expr);
1130 cleanup = NULL;
1131 stack_save = NULL;
1132 if (gimplify_ctxp->save_stack)
1134 gcall *stack_restore;
1136 /* Save stack on entry and restore it on exit. Add a try_finally
1137 block to achieve this. */
1138 build_stack_save_restore (&stack_save, &stack_restore);
1140 gimple_set_location (stack_save, start_locus);
1141 gimple_set_location (stack_restore, end_locus);
1143 gimplify_seq_add_stmt (&cleanup, stack_restore);
1146 /* Add clobbers for all variables that go out of scope. */
1147 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1149 if (TREE_CODE (t) == VAR_DECL
1150 && !is_global_var (t)
1151 && DECL_CONTEXT (t) == current_function_decl
1152 && !DECL_HARD_REGISTER (t)
1153 && !TREE_THIS_VOLATILE (t)
1154 && !DECL_HAS_VALUE_EXPR_P (t)
1155 /* Only care for variables that have to be in memory. Others
1156 will be rewritten into SSA names, hence moved to the top-level. */
1157 && !is_gimple_reg (t)
1158 && flag_stack_reuse != SR_NONE)
1160 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1161 gimple *clobber_stmt;
1162 TREE_THIS_VOLATILE (clobber) = 1;
1163 clobber_stmt = gimple_build_assign (t, clobber);
1164 gimple_set_location (clobber_stmt, end_locus);
1165 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1169 if (cleanup)
1171 gtry *gs;
1172 gimple_seq new_body;
1174 new_body = NULL;
1175 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1176 GIMPLE_TRY_FINALLY);
1178 if (stack_save)
1179 gimplify_seq_add_stmt (&new_body, stack_save);
1180 gimplify_seq_add_stmt (&new_body, gs);
1181 gimple_bind_set_body (bind_stmt, new_body);
1184 gimplify_ctxp->save_stack = old_save_stack;
1185 gimple_pop_bind_expr ();
1187 gimplify_seq_add_stmt (pre_p, bind_stmt);
1189 if (temp)
1191 *expr_p = temp;
1192 return GS_OK;
1195 *expr_p = NULL_TREE;
1196 return GS_ALL_DONE;
1199 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1200 GIMPLE value, it is assigned to a new temporary and the statement is
1201 re-written to return the temporary.
1203 PRE_P points to the sequence where side effects that must happen before
1204 STMT should be stored. */
1206 static enum gimplify_status
1207 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1209 greturn *ret;
1210 tree ret_expr = TREE_OPERAND (stmt, 0);
1211 tree result_decl, result;
1213 if (ret_expr == error_mark_node)
1214 return GS_ERROR;
1216 /* Implicit _Cilk_sync must be inserted right before any return statement
1217 if there is a _Cilk_spawn in the function. If the user has provided a
1218 _Cilk_sync, the optimizer should remove this duplicate one. */
1219 if (fn_contains_cilk_spawn_p (cfun))
1221 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1222 gimplify_and_add (impl_sync, pre_p);
1225 if (!ret_expr
1226 || TREE_CODE (ret_expr) == RESULT_DECL
1227 || ret_expr == error_mark_node)
1229 greturn *ret = gimple_build_return (ret_expr);
1230 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1231 gimplify_seq_add_stmt (pre_p, ret);
1232 return GS_ALL_DONE;
1235 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1236 result_decl = NULL_TREE;
1237 else
1239 result_decl = TREE_OPERAND (ret_expr, 0);
1241 /* See through a return by reference. */
1242 if (TREE_CODE (result_decl) == INDIRECT_REF)
1243 result_decl = TREE_OPERAND (result_decl, 0);
1245 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1246 || TREE_CODE (ret_expr) == INIT_EXPR)
1247 && TREE_CODE (result_decl) == RESULT_DECL);
1250 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1251 Recall that aggregate_value_p is FALSE for any aggregate type that is
1252 returned in registers. If we're returning values in registers, then
1253 we don't want to extend the lifetime of the RESULT_DECL, particularly
1254 across another call. In addition, for those aggregates for which
1255 hard_function_value generates a PARALLEL, we'll die during normal
1256 expansion of structure assignments; there's special code in expand_return
1257 to handle this case that does not exist in expand_expr. */
1258 if (!result_decl)
1259 result = NULL_TREE;
1260 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1262 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1264 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1265 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1266 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1267 should be effectively allocated by the caller, i.e. all calls to
1268 this function must be subject to the Return Slot Optimization. */
1269 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1270 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1272 result = result_decl;
1274 else if (gimplify_ctxp->return_temp)
1275 result = gimplify_ctxp->return_temp;
1276 else
1278 result = create_tmp_reg (TREE_TYPE (result_decl));
1280 /* ??? With complex control flow (usually involving abnormal edges),
1281 we can wind up warning about an uninitialized value for this. Due
1282 to how this variable is constructed and initialized, this is never
1283 true. Give up and never warn. */
1284 TREE_NO_WARNING (result) = 1;
1286 gimplify_ctxp->return_temp = result;
1289 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1290 Then gimplify the whole thing. */
1291 if (result != result_decl)
1292 TREE_OPERAND (ret_expr, 0) = result;
1294 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1296 ret = gimple_build_return (result);
1297 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1298 gimplify_seq_add_stmt (pre_p, ret);
1300 return GS_ALL_DONE;
1303 /* Gimplify a variable-length array DECL. */
1305 static void
1306 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1308 /* This is a variable-sized decl. Simplify its size and mark it
1309 for deferred expansion. */
1310 tree t, addr, ptr_type;
1312 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1313 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1315 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1316 if (DECL_HAS_VALUE_EXPR_P (decl))
1317 return;
1319 /* All occurrences of this decl in final gimplified code will be
1320 replaced by indirection. Setting DECL_VALUE_EXPR does two
1321 things: First, it lets the rest of the gimplifier know what
1322 replacement to use. Second, it lets the debug info know
1323 where to find the value. */
1324 ptr_type = build_pointer_type (TREE_TYPE (decl));
1325 addr = create_tmp_var (ptr_type, get_name (decl));
1326 DECL_IGNORED_P (addr) = 0;
1327 t = build_fold_indirect_ref (addr);
1328 TREE_THIS_NOTRAP (t) = 1;
1329 SET_DECL_VALUE_EXPR (decl, t);
1330 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1332 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1333 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1334 size_int (DECL_ALIGN (decl)));
1335 /* The call has been built for a variable-sized object. */
1336 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1337 t = fold_convert (ptr_type, t);
1338 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1340 gimplify_and_add (t, seq_p);
1342 /* Indicate that we need to restore the stack level when the
1343 enclosing BIND_EXPR is exited. */
1344 gimplify_ctxp->save_stack = true;
1347 /* A helper function to be called via walk_tree. Mark all labels under *TP
1348 as being forced. To be called for DECL_INITIAL of static variables. */
1350 static tree
1351 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1353 if (TYPE_P (*tp))
1354 *walk_subtrees = 0;
1355 if (TREE_CODE (*tp) == LABEL_DECL)
1356 FORCED_LABEL (*tp) = 1;
1358 return NULL_TREE;
1361 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1362 and initialization explicit. */
1364 static enum gimplify_status
1365 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1367 tree stmt = *stmt_p;
1368 tree decl = DECL_EXPR_DECL (stmt);
1370 *stmt_p = NULL_TREE;
1372 if (TREE_TYPE (decl) == error_mark_node)
1373 return GS_ERROR;
1375 if ((TREE_CODE (decl) == TYPE_DECL
1376 || TREE_CODE (decl) == VAR_DECL)
1377 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1378 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1380 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1381 in case its size expressions contain problematic nodes like CALL_EXPR. */
1382 if (TREE_CODE (decl) == TYPE_DECL
1383 && DECL_ORIGINAL_TYPE (decl)
1384 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1385 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1387 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1389 tree init = DECL_INITIAL (decl);
1391 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1392 || (!TREE_STATIC (decl)
1393 && flag_stack_check == GENERIC_STACK_CHECK
1394 && compare_tree_int (DECL_SIZE_UNIT (decl),
1395 STACK_CHECK_MAX_VAR_SIZE) > 0))
1396 gimplify_vla_decl (decl, seq_p);
1398 /* Some front ends do not explicitly declare all anonymous
1399 artificial variables. We compensate here by declaring the
1400 variables, though it would be better if the front ends would
1401 explicitly declare them. */
1402 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1403 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1404 gimple_add_tmp_var (decl);
1406 if (init && init != error_mark_node)
1408 if (!TREE_STATIC (decl))
1410 DECL_INITIAL (decl) = NULL_TREE;
1411 init = build2 (INIT_EXPR, void_type_node, decl, init);
1412 gimplify_and_add (init, seq_p);
1413 ggc_free (init);
1415 else
1416 /* We must still examine initializers for static variables
1417 as they may contain a label address. */
1418 walk_tree (&init, force_labels_r, NULL, NULL);
1422 return GS_ALL_DONE;
1425 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1426 and replacing the LOOP_EXPR with goto, but if the loop contains an
1427 EXIT_EXPR, we need to append a label for it to jump to. */
1429 static enum gimplify_status
1430 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1432 tree saved_label = gimplify_ctxp->exit_label;
1433 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1435 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1437 gimplify_ctxp->exit_label = NULL_TREE;
1439 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1441 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1443 if (gimplify_ctxp->exit_label)
1444 gimplify_seq_add_stmt (pre_p,
1445 gimple_build_label (gimplify_ctxp->exit_label));
1447 gimplify_ctxp->exit_label = saved_label;
1449 *expr_p = NULL;
1450 return GS_ALL_DONE;
1453 /* Gimplify a statement list onto a sequence. These may be created either
1454 by an enlightened front-end, or by shortcut_cond_expr. */
1456 static enum gimplify_status
1457 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1459 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1461 tree_stmt_iterator i = tsi_start (*expr_p);
1463 while (!tsi_end_p (i))
1465 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1466 tsi_delink (&i);
1469 if (temp)
1471 *expr_p = temp;
1472 return GS_OK;
1475 return GS_ALL_DONE;
1479 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1480 branch to. */
1482 static enum gimplify_status
1483 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1485 tree switch_expr = *expr_p;
1486 gimple_seq switch_body_seq = NULL;
1487 enum gimplify_status ret;
1488 tree index_type = TREE_TYPE (switch_expr);
1489 if (index_type == NULL_TREE)
1490 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1492 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1493 fb_rvalue);
1494 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1495 return ret;
1497 if (SWITCH_BODY (switch_expr))
1499 vec<tree> labels;
1500 vec<tree> saved_labels;
1501 tree default_case = NULL_TREE;
1502 gswitch *switch_stmt;
1504 /* If someone can be bothered to fill in the labels, they can
1505 be bothered to null out the body too. */
1506 gcc_assert (!SWITCH_LABELS (switch_expr));
1508 /* Save old labels, get new ones from body, then restore the old
1509 labels. Save all the things from the switch body to append after. */
1510 saved_labels = gimplify_ctxp->case_labels;
1511 gimplify_ctxp->case_labels.create (8);
1513 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1514 labels = gimplify_ctxp->case_labels;
1515 gimplify_ctxp->case_labels = saved_labels;
1517 preprocess_case_label_vec_for_gimple (labels, index_type,
1518 &default_case);
1520 if (!default_case)
1522 glabel *new_default;
1524 default_case
1525 = build_case_label (NULL_TREE, NULL_TREE,
1526 create_artificial_label (UNKNOWN_LOCATION));
1527 new_default = gimple_build_label (CASE_LABEL (default_case));
1528 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1531 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
1532 default_case, labels);
1533 gimplify_seq_add_stmt (pre_p, switch_stmt);
1534 gimplify_seq_add_seq (pre_p, switch_body_seq);
1535 labels.release ();
1537 else
1538 gcc_assert (SWITCH_LABELS (switch_expr));
1540 return GS_ALL_DONE;
1543 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1545 static enum gimplify_status
1546 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1548 struct gimplify_ctx *ctxp;
1549 glabel *label_stmt;
1551 /* Invalid programs can play Duff's Device type games with, for example,
1552 #pragma omp parallel. At least in the C front end, we don't
1553 detect such invalid branches until after gimplification, in the
1554 diagnose_omp_blocks pass. */
1555 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1556 if (ctxp->case_labels.exists ())
1557 break;
1559 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
1560 ctxp->case_labels.safe_push (*expr_p);
1561 gimplify_seq_add_stmt (pre_p, label_stmt);
1563 return GS_ALL_DONE;
1566 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1567 if necessary. */
1569 tree
1570 build_and_jump (tree *label_p)
1572 if (label_p == NULL)
1573 /* If there's nowhere to jump, just fall through. */
1574 return NULL_TREE;
1576 if (*label_p == NULL_TREE)
1578 tree label = create_artificial_label (UNKNOWN_LOCATION);
1579 *label_p = label;
1582 return build1 (GOTO_EXPR, void_type_node, *label_p);
1585 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1586 This also involves building a label to jump to and communicating it to
1587 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1589 static enum gimplify_status
1590 gimplify_exit_expr (tree *expr_p)
1592 tree cond = TREE_OPERAND (*expr_p, 0);
1593 tree expr;
1595 expr = build_and_jump (&gimplify_ctxp->exit_label);
1596 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1597 *expr_p = expr;
1599 return GS_OK;
1602 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1603 different from its canonical type, wrap the whole thing inside a
1604 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1605 type.
1607 The canonical type of a COMPONENT_REF is the type of the field being
1608 referenced--unless the field is a bit-field which can be read directly
1609 in a smaller mode, in which case the canonical type is the
1610 sign-appropriate type corresponding to that mode. */
1612 static void
1613 canonicalize_component_ref (tree *expr_p)
1615 tree expr = *expr_p;
1616 tree type;
1618 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1620 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1621 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1622 else
1623 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1625 /* One could argue that all the stuff below is not necessary for
1626 the non-bitfield case and declare it a FE error if type
1627 adjustment would be needed. */
1628 if (TREE_TYPE (expr) != type)
1630 #ifdef ENABLE_TYPES_CHECKING
1631 tree old_type = TREE_TYPE (expr);
1632 #endif
1633 int type_quals;
1635 /* We need to preserve qualifiers and propagate them from
1636 operand 0. */
1637 type_quals = TYPE_QUALS (type)
1638 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1639 if (TYPE_QUALS (type) != type_quals)
1640 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1642 /* Set the type of the COMPONENT_REF to the underlying type. */
1643 TREE_TYPE (expr) = type;
1645 #ifdef ENABLE_TYPES_CHECKING
1646 /* It is now a FE error, if the conversion from the canonical
1647 type to the original expression type is not useless. */
1648 gcc_assert (useless_type_conversion_p (old_type, type));
1649 #endif
1653 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1654 to foo, embed that change in the ADDR_EXPR by converting
1655 T array[U];
1656 (T *)&array
1658 &array[L]
1659 where L is the lower bound. For simplicity, only do this for constant
1660 lower bound.
1661 The constraint is that the type of &array[L] is trivially convertible
1662 to T *. */
1664 static void
1665 canonicalize_addr_expr (tree *expr_p)
1667 tree expr = *expr_p;
1668 tree addr_expr = TREE_OPERAND (expr, 0);
1669 tree datype, ddatype, pddatype;
1671 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1672 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1673 || TREE_CODE (addr_expr) != ADDR_EXPR)
1674 return;
1676 /* The addr_expr type should be a pointer to an array. */
1677 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1678 if (TREE_CODE (datype) != ARRAY_TYPE)
1679 return;
1681 /* The pointer to element type shall be trivially convertible to
1682 the expression pointer type. */
1683 ddatype = TREE_TYPE (datype);
1684 pddatype = build_pointer_type (ddatype);
1685 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1686 pddatype))
1687 return;
1689 /* The lower bound and element sizes must be constant. */
1690 if (!TYPE_SIZE_UNIT (ddatype)
1691 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1692 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1693 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1694 return;
1696 /* All checks succeeded. Build a new node to merge the cast. */
1697 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1698 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1699 NULL_TREE, NULL_TREE);
1700 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1702 /* We can have stripped a required restrict qualifier above. */
1703 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1704 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1707 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1708 underneath as appropriate. */
1710 static enum gimplify_status
1711 gimplify_conversion (tree *expr_p)
1713 location_t loc = EXPR_LOCATION (*expr_p);
1714 gcc_assert (CONVERT_EXPR_P (*expr_p));
1716 /* Then strip away all but the outermost conversion. */
1717 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1719 /* And remove the outermost conversion if it's useless. */
1720 if (tree_ssa_useless_type_conversion (*expr_p))
1721 *expr_p = TREE_OPERAND (*expr_p, 0);
1723 /* If we still have a conversion at the toplevel,
1724 then canonicalize some constructs. */
1725 if (CONVERT_EXPR_P (*expr_p))
1727 tree sub = TREE_OPERAND (*expr_p, 0);
1729 /* If a NOP conversion is changing the type of a COMPONENT_REF
1730 expression, then canonicalize its type now in order to expose more
1731 redundant conversions. */
1732 if (TREE_CODE (sub) == COMPONENT_REF)
1733 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1735 /* If a NOP conversion is changing a pointer to array of foo
1736 to a pointer to foo, embed that change in the ADDR_EXPR. */
1737 else if (TREE_CODE (sub) == ADDR_EXPR)
1738 canonicalize_addr_expr (expr_p);
1741 /* If we have a conversion to a non-register type force the
1742 use of a VIEW_CONVERT_EXPR instead. */
1743 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1744 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1745 TREE_OPERAND (*expr_p, 0));
1747 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
1748 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
1749 TREE_SET_CODE (*expr_p, NOP_EXPR);
1751 return GS_OK;
1754 /* Nonlocal VLAs seen in the current function. */
1755 static hash_set<tree> *nonlocal_vlas;
1757 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1758 static tree nonlocal_vla_vars;
1760 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1761 DECL_VALUE_EXPR, and it's worth re-examining things. */
1763 static enum gimplify_status
1764 gimplify_var_or_parm_decl (tree *expr_p)
1766 tree decl = *expr_p;
1768 /* ??? If this is a local variable, and it has not been seen in any
1769 outer BIND_EXPR, then it's probably the result of a duplicate
1770 declaration, for which we've already issued an error. It would
1771 be really nice if the front end wouldn't leak these at all.
1772 Currently the only known culprit is C++ destructors, as seen
1773 in g++.old-deja/g++.jason/binding.C. */
1774 if (TREE_CODE (decl) == VAR_DECL
1775 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1776 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1777 && decl_function_context (decl) == current_function_decl)
1779 gcc_assert (seen_error ());
1780 return GS_ERROR;
1783 /* When within an OMP context, notice uses of variables. */
1784 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1785 return GS_ALL_DONE;
1787 /* If the decl is an alias for another expression, substitute it now. */
1788 if (DECL_HAS_VALUE_EXPR_P (decl))
1790 tree value_expr = DECL_VALUE_EXPR (decl);
1792 /* For referenced nonlocal VLAs add a decl for debugging purposes
1793 to the current function. */
1794 if (TREE_CODE (decl) == VAR_DECL
1795 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1796 && nonlocal_vlas != NULL
1797 && TREE_CODE (value_expr) == INDIRECT_REF
1798 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1799 && decl_function_context (decl) != current_function_decl)
1801 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1802 while (ctx
1803 && (ctx->region_type == ORT_WORKSHARE
1804 || ctx->region_type == ORT_SIMD))
1805 ctx = ctx->outer_context;
1806 if (!ctx && !nonlocal_vlas->add (decl))
1808 tree copy = copy_node (decl);
1810 lang_hooks.dup_lang_specific_decl (copy);
1811 SET_DECL_RTL (copy, 0);
1812 TREE_USED (copy) = 1;
1813 DECL_CHAIN (copy) = nonlocal_vla_vars;
1814 nonlocal_vla_vars = copy;
1815 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1816 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1820 *expr_p = unshare_expr (value_expr);
1821 return GS_OK;
1824 return GS_ALL_DONE;
1827 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1829 static void
1830 recalculate_side_effects (tree t)
1832 enum tree_code code = TREE_CODE (t);
1833 int len = TREE_OPERAND_LENGTH (t);
1834 int i;
1836 switch (TREE_CODE_CLASS (code))
1838 case tcc_expression:
1839 switch (code)
1841 case INIT_EXPR:
1842 case MODIFY_EXPR:
1843 case VA_ARG_EXPR:
1844 case PREDECREMENT_EXPR:
1845 case PREINCREMENT_EXPR:
1846 case POSTDECREMENT_EXPR:
1847 case POSTINCREMENT_EXPR:
1848 /* All of these have side-effects, no matter what their
1849 operands are. */
1850 return;
1852 default:
1853 break;
1855 /* Fall through. */
1857 case tcc_comparison: /* a comparison expression */
1858 case tcc_unary: /* a unary arithmetic expression */
1859 case tcc_binary: /* a binary arithmetic expression */
1860 case tcc_reference: /* a reference */
1861 case tcc_vl_exp: /* a function call */
1862 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1863 for (i = 0; i < len; ++i)
1865 tree op = TREE_OPERAND (t, i);
1866 if (op && TREE_SIDE_EFFECTS (op))
1867 TREE_SIDE_EFFECTS (t) = 1;
1869 break;
1871 case tcc_constant:
1872 /* No side-effects. */
1873 return;
1875 default:
1876 gcc_unreachable ();
1880 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1881 node *EXPR_P.
1883 compound_lval
1884 : min_lval '[' val ']'
1885 | min_lval '.' ID
1886 | compound_lval '[' val ']'
1887 | compound_lval '.' ID
1889 This is not part of the original SIMPLE definition, which separates
1890 array and member references, but it seems reasonable to handle them
1891 together. Also, this way we don't run into problems with union
1892 aliasing; gcc requires that for accesses through a union to alias, the
1893 union reference must be explicit, which was not always the case when we
1894 were splitting up array and member refs.
1896 PRE_P points to the sequence where side effects that must happen before
1897 *EXPR_P should be stored.
1899 POST_P points to the sequence where side effects that must happen after
1900 *EXPR_P should be stored. */
1902 static enum gimplify_status
1903 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1904 fallback_t fallback)
1906 tree *p;
1907 enum gimplify_status ret = GS_ALL_DONE, tret;
1908 int i;
1909 location_t loc = EXPR_LOCATION (*expr_p);
1910 tree expr = *expr_p;
1912 /* Create a stack of the subexpressions so later we can walk them in
1913 order from inner to outer. */
1914 auto_vec<tree, 10> expr_stack;
1916 /* We can handle anything that get_inner_reference can deal with. */
1917 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1919 restart:
1920 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1921 if (TREE_CODE (*p) == INDIRECT_REF)
1922 *p = fold_indirect_ref_loc (loc, *p);
1924 if (handled_component_p (*p))
1926 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1927 additional COMPONENT_REFs. */
1928 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1929 && gimplify_var_or_parm_decl (p) == GS_OK)
1930 goto restart;
1931 else
1932 break;
1934 expr_stack.safe_push (*p);
1937 gcc_assert (expr_stack.length ());
1939 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1940 walked through and P points to the innermost expression.
1942 Java requires that we elaborated nodes in source order. That
1943 means we must gimplify the inner expression followed by each of
1944 the indices, in order. But we can't gimplify the inner
1945 expression until we deal with any variable bounds, sizes, or
1946 positions in order to deal with PLACEHOLDER_EXPRs.
1948 So we do this in three steps. First we deal with the annotations
1949 for any variables in the components, then we gimplify the base,
1950 then we gimplify any indices, from left to right. */
1951 for (i = expr_stack.length () - 1; i >= 0; i--)
1953 tree t = expr_stack[i];
1955 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1957 /* Gimplify the low bound and element type size and put them into
1958 the ARRAY_REF. If these values are set, they have already been
1959 gimplified. */
1960 if (TREE_OPERAND (t, 2) == NULL_TREE)
1962 tree low = unshare_expr (array_ref_low_bound (t));
1963 if (!is_gimple_min_invariant (low))
1965 TREE_OPERAND (t, 2) = low;
1966 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1967 post_p, is_gimple_reg,
1968 fb_rvalue);
1969 ret = MIN (ret, tret);
1972 else
1974 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1975 is_gimple_reg, fb_rvalue);
1976 ret = MIN (ret, tret);
1979 if (TREE_OPERAND (t, 3) == NULL_TREE)
1981 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1982 tree elmt_size = unshare_expr (array_ref_element_size (t));
1983 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1985 /* Divide the element size by the alignment of the element
1986 type (above). */
1987 elmt_size
1988 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1990 if (!is_gimple_min_invariant (elmt_size))
1992 TREE_OPERAND (t, 3) = elmt_size;
1993 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1994 post_p, is_gimple_reg,
1995 fb_rvalue);
1996 ret = MIN (ret, tret);
1999 else
2001 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2002 is_gimple_reg, fb_rvalue);
2003 ret = MIN (ret, tret);
2006 else if (TREE_CODE (t) == COMPONENT_REF)
2008 /* Set the field offset into T and gimplify it. */
2009 if (TREE_OPERAND (t, 2) == NULL_TREE)
2011 tree offset = unshare_expr (component_ref_field_offset (t));
2012 tree field = TREE_OPERAND (t, 1);
2013 tree factor
2014 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2016 /* Divide the offset by its alignment. */
2017 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2019 if (!is_gimple_min_invariant (offset))
2021 TREE_OPERAND (t, 2) = offset;
2022 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2023 post_p, is_gimple_reg,
2024 fb_rvalue);
2025 ret = MIN (ret, tret);
2028 else
2030 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2031 is_gimple_reg, fb_rvalue);
2032 ret = MIN (ret, tret);
2037 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2038 so as to match the min_lval predicate. Failure to do so may result
2039 in the creation of large aggregate temporaries. */
2040 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2041 fallback | fb_lvalue);
2042 ret = MIN (ret, tret);
2044 /* And finally, the indices and operands of ARRAY_REF. During this
2045 loop we also remove any useless conversions. */
2046 for (; expr_stack.length () > 0; )
2048 tree t = expr_stack.pop ();
2050 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2052 /* Gimplify the dimension. */
2053 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2055 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2056 is_gimple_val, fb_rvalue);
2057 ret = MIN (ret, tret);
2061 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2063 /* The innermost expression P may have originally had
2064 TREE_SIDE_EFFECTS set which would have caused all the outer
2065 expressions in *EXPR_P leading to P to also have had
2066 TREE_SIDE_EFFECTS set. */
2067 recalculate_side_effects (t);
2070 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2071 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2073 canonicalize_component_ref (expr_p);
2076 expr_stack.release ();
2078 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2080 return ret;
2083 /* Gimplify the self modifying expression pointed to by EXPR_P
2084 (++, --, +=, -=).
2086 PRE_P points to the list where side effects that must happen before
2087 *EXPR_P should be stored.
2089 POST_P points to the list where side effects that must happen after
2090 *EXPR_P should be stored.
2092 WANT_VALUE is nonzero iff we want to use the value of this expression
2093 in another expression.
2095 ARITH_TYPE is the type the computation should be performed in. */
2097 enum gimplify_status
2098 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2099 bool want_value, tree arith_type)
2101 enum tree_code code;
2102 tree lhs, lvalue, rhs, t1;
2103 gimple_seq post = NULL, *orig_post_p = post_p;
2104 bool postfix;
2105 enum tree_code arith_code;
2106 enum gimplify_status ret;
2107 location_t loc = EXPR_LOCATION (*expr_p);
2109 code = TREE_CODE (*expr_p);
2111 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2112 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2114 /* Prefix or postfix? */
2115 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2116 /* Faster to treat as prefix if result is not used. */
2117 postfix = want_value;
2118 else
2119 postfix = false;
2121 /* For postfix, make sure the inner expression's post side effects
2122 are executed after side effects from this expression. */
2123 if (postfix)
2124 post_p = &post;
2126 /* Add or subtract? */
2127 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2128 arith_code = PLUS_EXPR;
2129 else
2130 arith_code = MINUS_EXPR;
2132 /* Gimplify the LHS into a GIMPLE lvalue. */
2133 lvalue = TREE_OPERAND (*expr_p, 0);
2134 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2135 if (ret == GS_ERROR)
2136 return ret;
2138 /* Extract the operands to the arithmetic operation. */
2139 lhs = lvalue;
2140 rhs = TREE_OPERAND (*expr_p, 1);
2142 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2143 that as the result value and in the postqueue operation. */
2144 if (postfix)
2146 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2147 if (ret == GS_ERROR)
2148 return ret;
2150 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2153 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2154 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2156 rhs = convert_to_ptrofftype_loc (loc, rhs);
2157 if (arith_code == MINUS_EXPR)
2158 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2159 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2161 else
2162 t1 = fold_convert (TREE_TYPE (*expr_p),
2163 fold_build2 (arith_code, arith_type,
2164 fold_convert (arith_type, lhs),
2165 fold_convert (arith_type, rhs)));
2167 if (postfix)
2169 gimplify_assign (lvalue, t1, pre_p);
2170 gimplify_seq_add_seq (orig_post_p, post);
2171 *expr_p = lhs;
2172 return GS_ALL_DONE;
2174 else
2176 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2177 return GS_OK;
2181 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2183 static void
2184 maybe_with_size_expr (tree *expr_p)
2186 tree expr = *expr_p;
2187 tree type = TREE_TYPE (expr);
2188 tree size;
2190 /* If we've already wrapped this or the type is error_mark_node, we can't do
2191 anything. */
2192 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2193 || type == error_mark_node)
2194 return;
2196 /* If the size isn't known or is a constant, we have nothing to do. */
2197 size = TYPE_SIZE_UNIT (type);
2198 if (!size || TREE_CODE (size) == INTEGER_CST)
2199 return;
2201 /* Otherwise, make a WITH_SIZE_EXPR. */
2202 size = unshare_expr (size);
2203 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2204 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2207 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2208 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2209 the CALL_EXPR. */
2211 enum gimplify_status
2212 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2214 bool (*test) (tree);
2215 fallback_t fb;
2217 /* In general, we allow lvalues for function arguments to avoid
2218 extra overhead of copying large aggregates out of even larger
2219 aggregates into temporaries only to copy the temporaries to
2220 the argument list. Make optimizers happy by pulling out to
2221 temporaries those types that fit in registers. */
2222 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2223 test = is_gimple_val, fb = fb_rvalue;
2224 else
2226 test = is_gimple_lvalue, fb = fb_either;
2227 /* Also strip a TARGET_EXPR that would force an extra copy. */
2228 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2230 tree init = TARGET_EXPR_INITIAL (*arg_p);
2231 if (init
2232 && !VOID_TYPE_P (TREE_TYPE (init)))
2233 *arg_p = init;
2237 /* If this is a variable sized type, we must remember the size. */
2238 maybe_with_size_expr (arg_p);
2240 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2241 /* Make sure arguments have the same location as the function call
2242 itself. */
2243 protected_set_expr_location (*arg_p, call_location);
2245 /* There is a sequence point before a function call. Side effects in
2246 the argument list must occur before the actual call. So, when
2247 gimplifying arguments, force gimplify_expr to use an internal
2248 post queue which is then appended to the end of PRE_P. */
2249 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2252 /* Don't fold inside offloading or taskreg regions: it can break code by
2253 adding decl references that weren't in the source. We'll do it during
2254 omplower pass instead. */
2256 static bool
2257 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2259 struct gimplify_omp_ctx *ctx;
2260 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2261 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
2262 return false;
2263 return fold_stmt (gsi);
2266 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2267 WANT_VALUE is true if the result of the call is desired. */
2269 static enum gimplify_status
2270 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2272 tree fndecl, parms, p, fnptrtype;
2273 enum gimplify_status ret;
2274 int i, nargs;
2275 gcall *call;
2276 bool builtin_va_start_p = false;
2277 location_t loc = EXPR_LOCATION (*expr_p);
2279 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2281 /* For reliable diagnostics during inlining, it is necessary that
2282 every call_expr be annotated with file and line. */
2283 if (! EXPR_HAS_LOCATION (*expr_p))
2284 SET_EXPR_LOCATION (*expr_p, input_location);
2286 /* Gimplify internal functions created in the FEs. */
2287 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2289 if (want_value)
2290 return GS_ALL_DONE;
2292 nargs = call_expr_nargs (*expr_p);
2293 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2294 auto_vec<tree> vargs (nargs);
2296 for (i = 0; i < nargs; i++)
2298 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2299 EXPR_LOCATION (*expr_p));
2300 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2302 gimple *call = gimple_build_call_internal_vec (ifn, vargs);
2303 gimplify_seq_add_stmt (pre_p, call);
2304 return GS_ALL_DONE;
2307 /* This may be a call to a builtin function.
2309 Builtin function calls may be transformed into different
2310 (and more efficient) builtin function calls under certain
2311 circumstances. Unfortunately, gimplification can muck things
2312 up enough that the builtin expanders are not aware that certain
2313 transformations are still valid.
2315 So we attempt transformation/gimplification of the call before
2316 we gimplify the CALL_EXPR. At this time we do not manage to
2317 transform all calls in the same manner as the expanders do, but
2318 we do transform most of them. */
2319 fndecl = get_callee_fndecl (*expr_p);
2320 if (fndecl
2321 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2322 switch (DECL_FUNCTION_CODE (fndecl))
2324 case BUILT_IN_VA_START:
2326 builtin_va_start_p = TRUE;
2327 if (call_expr_nargs (*expr_p) < 2)
2329 error ("too few arguments to function %<va_start%>");
2330 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2331 return GS_OK;
2334 if (fold_builtin_next_arg (*expr_p, true))
2336 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2337 return GS_OK;
2339 break;
2341 case BUILT_IN_LINE:
2343 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2344 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2345 return GS_OK;
2347 case BUILT_IN_FILE:
2349 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2350 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2351 return GS_OK;
2353 case BUILT_IN_FUNCTION:
2355 const char *function;
2356 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2357 *expr_p = build_string_literal (strlen (function) + 1, function);
2358 return GS_OK;
2360 default:
2363 if (fndecl && DECL_BUILT_IN (fndecl))
2365 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2366 if (new_tree && new_tree != *expr_p)
2368 /* There was a transformation of this call which computes the
2369 same value, but in a more efficient way. Return and try
2370 again. */
2371 *expr_p = new_tree;
2372 return GS_OK;
2376 /* Remember the original function pointer type. */
2377 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2379 /* There is a sequence point before the call, so any side effects in
2380 the calling expression must occur before the actual call. Force
2381 gimplify_expr to use an internal post queue. */
2382 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2383 is_gimple_call_addr, fb_rvalue);
2385 nargs = call_expr_nargs (*expr_p);
2387 /* Get argument types for verification. */
2388 fndecl = get_callee_fndecl (*expr_p);
2389 parms = NULL_TREE;
2390 if (fndecl)
2391 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2392 else
2393 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2395 if (fndecl && DECL_ARGUMENTS (fndecl))
2396 p = DECL_ARGUMENTS (fndecl);
2397 else if (parms)
2398 p = parms;
2399 else
2400 p = NULL_TREE;
2401 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2404 /* If the last argument is __builtin_va_arg_pack () and it is not
2405 passed as a named argument, decrease the number of CALL_EXPR
2406 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2407 if (!p
2408 && i < nargs
2409 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2411 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2412 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2414 if (last_arg_fndecl
2415 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2416 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2417 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2419 tree call = *expr_p;
2421 --nargs;
2422 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2423 CALL_EXPR_FN (call),
2424 nargs, CALL_EXPR_ARGP (call));
2426 /* Copy all CALL_EXPR flags, location and block, except
2427 CALL_EXPR_VA_ARG_PACK flag. */
2428 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2429 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2430 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2431 = CALL_EXPR_RETURN_SLOT_OPT (call);
2432 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2433 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2435 /* Set CALL_EXPR_VA_ARG_PACK. */
2436 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2440 /* Gimplify the function arguments. */
2441 if (nargs > 0)
2443 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2444 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2445 PUSH_ARGS_REVERSED ? i-- : i++)
2447 enum gimplify_status t;
2449 /* Avoid gimplifying the second argument to va_start, which needs to
2450 be the plain PARM_DECL. */
2451 if ((i != 1) || !builtin_va_start_p)
2453 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2454 EXPR_LOCATION (*expr_p));
2456 if (t == GS_ERROR)
2457 ret = GS_ERROR;
2462 /* Gimplify the static chain. */
2463 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
2465 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
2466 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
2467 else
2469 enum gimplify_status t;
2470 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
2471 EXPR_LOCATION (*expr_p));
2472 if (t == GS_ERROR)
2473 ret = GS_ERROR;
2477 /* Verify the function result. */
2478 if (want_value && fndecl
2479 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2481 error_at (loc, "using result of function returning %<void%>");
2482 ret = GS_ERROR;
2485 /* Try this again in case gimplification exposed something. */
2486 if (ret != GS_ERROR)
2488 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2490 if (new_tree && new_tree != *expr_p)
2492 /* There was a transformation of this call which computes the
2493 same value, but in a more efficient way. Return and try
2494 again. */
2495 *expr_p = new_tree;
2496 return GS_OK;
2499 else
2501 *expr_p = error_mark_node;
2502 return GS_ERROR;
2505 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2506 decl. This allows us to eliminate redundant or useless
2507 calls to "const" functions. */
2508 if (TREE_CODE (*expr_p) == CALL_EXPR)
2510 int flags = call_expr_flags (*expr_p);
2511 if (flags & (ECF_CONST | ECF_PURE)
2512 /* An infinite loop is considered a side effect. */
2513 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2514 TREE_SIDE_EFFECTS (*expr_p) = 0;
2517 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2518 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2519 form and delegate the creation of a GIMPLE_CALL to
2520 gimplify_modify_expr. This is always possible because when
2521 WANT_VALUE is true, the caller wants the result of this call into
2522 a temporary, which means that we will emit an INIT_EXPR in
2523 internal_get_tmp_var which will then be handled by
2524 gimplify_modify_expr. */
2525 if (!want_value)
2527 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2528 have to do is replicate it as a GIMPLE_CALL tuple. */
2529 gimple_stmt_iterator gsi;
2530 call = gimple_build_call_from_tree (*expr_p);
2531 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2532 notice_special_calls (call);
2533 gimplify_seq_add_stmt (pre_p, call);
2534 gsi = gsi_last (*pre_p);
2535 maybe_fold_stmt (&gsi);
2536 *expr_p = NULL_TREE;
2538 else
2539 /* Remember the original function type. */
2540 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2541 CALL_EXPR_FN (*expr_p));
2543 return ret;
2546 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2547 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2549 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2550 condition is true or false, respectively. If null, we should generate
2551 our own to skip over the evaluation of this specific expression.
2553 LOCUS is the source location of the COND_EXPR.
2555 This function is the tree equivalent of do_jump.
2557 shortcut_cond_r should only be called by shortcut_cond_expr. */
2559 static tree
2560 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2561 location_t locus)
2563 tree local_label = NULL_TREE;
2564 tree t, expr = NULL;
2566 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2567 retain the shortcut semantics. Just insert the gotos here;
2568 shortcut_cond_expr will append the real blocks later. */
2569 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2571 location_t new_locus;
2573 /* Turn if (a && b) into
2575 if (a); else goto no;
2576 if (b) goto yes; else goto no;
2577 (no:) */
2579 if (false_label_p == NULL)
2580 false_label_p = &local_label;
2582 /* Keep the original source location on the first 'if'. */
2583 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2584 append_to_statement_list (t, &expr);
2586 /* Set the source location of the && on the second 'if'. */
2587 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2588 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2589 new_locus);
2590 append_to_statement_list (t, &expr);
2592 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2594 location_t new_locus;
2596 /* Turn if (a || b) into
2598 if (a) goto yes;
2599 if (b) goto yes; else goto no;
2600 (yes:) */
2602 if (true_label_p == NULL)
2603 true_label_p = &local_label;
2605 /* Keep the original source location on the first 'if'. */
2606 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2607 append_to_statement_list (t, &expr);
2609 /* Set the source location of the || on the second 'if'. */
2610 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2611 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2612 new_locus);
2613 append_to_statement_list (t, &expr);
2615 else if (TREE_CODE (pred) == COND_EXPR
2616 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2617 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2619 location_t new_locus;
2621 /* As long as we're messing with gotos, turn if (a ? b : c) into
2622 if (a)
2623 if (b) goto yes; else goto no;
2624 else
2625 if (c) goto yes; else goto no;
2627 Don't do this if one of the arms has void type, which can happen
2628 in C++ when the arm is throw. */
2630 /* Keep the original source location on the first 'if'. Set the source
2631 location of the ? on the second 'if'. */
2632 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2633 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2634 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2635 false_label_p, locus),
2636 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2637 false_label_p, new_locus));
2639 else
2641 expr = build3 (COND_EXPR, void_type_node, pred,
2642 build_and_jump (true_label_p),
2643 build_and_jump (false_label_p));
2644 SET_EXPR_LOCATION (expr, locus);
2647 if (local_label)
2649 t = build1 (LABEL_EXPR, void_type_node, local_label);
2650 append_to_statement_list (t, &expr);
2653 return expr;
2656 /* Given a conditional expression EXPR with short-circuit boolean
2657 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2658 predicate apart into the equivalent sequence of conditionals. */
2660 static tree
2661 shortcut_cond_expr (tree expr)
2663 tree pred = TREE_OPERAND (expr, 0);
2664 tree then_ = TREE_OPERAND (expr, 1);
2665 tree else_ = TREE_OPERAND (expr, 2);
2666 tree true_label, false_label, end_label, t;
2667 tree *true_label_p;
2668 tree *false_label_p;
2669 bool emit_end, emit_false, jump_over_else;
2670 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2671 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2673 /* First do simple transformations. */
2674 if (!else_se)
2676 /* If there is no 'else', turn
2677 if (a && b) then c
2678 into
2679 if (a) if (b) then c. */
2680 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2682 /* Keep the original source location on the first 'if'. */
2683 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2684 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2685 /* Set the source location of the && on the second 'if'. */
2686 if (EXPR_HAS_LOCATION (pred))
2687 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2688 then_ = shortcut_cond_expr (expr);
2689 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2690 pred = TREE_OPERAND (pred, 0);
2691 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2692 SET_EXPR_LOCATION (expr, locus);
2696 if (!then_se)
2698 /* If there is no 'then', turn
2699 if (a || b); else d
2700 into
2701 if (a); else if (b); else d. */
2702 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2704 /* Keep the original source location on the first 'if'. */
2705 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2706 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2707 /* Set the source location of the || on the second 'if'. */
2708 if (EXPR_HAS_LOCATION (pred))
2709 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2710 else_ = shortcut_cond_expr (expr);
2711 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2712 pred = TREE_OPERAND (pred, 0);
2713 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2714 SET_EXPR_LOCATION (expr, locus);
2718 /* If we're done, great. */
2719 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2720 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2721 return expr;
2723 /* Otherwise we need to mess with gotos. Change
2724 if (a) c; else d;
2726 if (a); else goto no;
2727 c; goto end;
2728 no: d; end:
2729 and recursively gimplify the condition. */
2731 true_label = false_label = end_label = NULL_TREE;
2733 /* If our arms just jump somewhere, hijack those labels so we don't
2734 generate jumps to jumps. */
2736 if (then_
2737 && TREE_CODE (then_) == GOTO_EXPR
2738 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2740 true_label = GOTO_DESTINATION (then_);
2741 then_ = NULL;
2742 then_se = false;
2745 if (else_
2746 && TREE_CODE (else_) == GOTO_EXPR
2747 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2749 false_label = GOTO_DESTINATION (else_);
2750 else_ = NULL;
2751 else_se = false;
2754 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2755 if (true_label)
2756 true_label_p = &true_label;
2757 else
2758 true_label_p = NULL;
2760 /* The 'else' branch also needs a label if it contains interesting code. */
2761 if (false_label || else_se)
2762 false_label_p = &false_label;
2763 else
2764 false_label_p = NULL;
2766 /* If there was nothing else in our arms, just forward the label(s). */
2767 if (!then_se && !else_se)
2768 return shortcut_cond_r (pred, true_label_p, false_label_p,
2769 EXPR_LOC_OR_LOC (expr, input_location));
2771 /* If our last subexpression already has a terminal label, reuse it. */
2772 if (else_se)
2773 t = expr_last (else_);
2774 else if (then_se)
2775 t = expr_last (then_);
2776 else
2777 t = NULL;
2778 if (t && TREE_CODE (t) == LABEL_EXPR)
2779 end_label = LABEL_EXPR_LABEL (t);
2781 /* If we don't care about jumping to the 'else' branch, jump to the end
2782 if the condition is false. */
2783 if (!false_label_p)
2784 false_label_p = &end_label;
2786 /* We only want to emit these labels if we aren't hijacking them. */
2787 emit_end = (end_label == NULL_TREE);
2788 emit_false = (false_label == NULL_TREE);
2790 /* We only emit the jump over the else clause if we have to--if the
2791 then clause may fall through. Otherwise we can wind up with a
2792 useless jump and a useless label at the end of gimplified code,
2793 which will cause us to think that this conditional as a whole
2794 falls through even if it doesn't. If we then inline a function
2795 which ends with such a condition, that can cause us to issue an
2796 inappropriate warning about control reaching the end of a
2797 non-void function. */
2798 jump_over_else = block_may_fallthru (then_);
2800 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2801 EXPR_LOC_OR_LOC (expr, input_location));
2803 expr = NULL;
2804 append_to_statement_list (pred, &expr);
2806 append_to_statement_list (then_, &expr);
2807 if (else_se)
2809 if (jump_over_else)
2811 tree last = expr_last (expr);
2812 t = build_and_jump (&end_label);
2813 if (EXPR_HAS_LOCATION (last))
2814 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2815 append_to_statement_list (t, &expr);
2817 if (emit_false)
2819 t = build1 (LABEL_EXPR, void_type_node, false_label);
2820 append_to_statement_list (t, &expr);
2822 append_to_statement_list (else_, &expr);
2824 if (emit_end && end_label)
2826 t = build1 (LABEL_EXPR, void_type_node, end_label);
2827 append_to_statement_list (t, &expr);
2830 return expr;
2833 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2835 tree
2836 gimple_boolify (tree expr)
2838 tree type = TREE_TYPE (expr);
2839 location_t loc = EXPR_LOCATION (expr);
2841 if (TREE_CODE (expr) == NE_EXPR
2842 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2843 && integer_zerop (TREE_OPERAND (expr, 1)))
2845 tree call = TREE_OPERAND (expr, 0);
2846 tree fn = get_callee_fndecl (call);
2848 /* For __builtin_expect ((long) (x), y) recurse into x as well
2849 if x is truth_value_p. */
2850 if (fn
2851 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2852 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2853 && call_expr_nargs (call) == 2)
2855 tree arg = CALL_EXPR_ARG (call, 0);
2856 if (arg)
2858 if (TREE_CODE (arg) == NOP_EXPR
2859 && TREE_TYPE (arg) == TREE_TYPE (call))
2860 arg = TREE_OPERAND (arg, 0);
2861 if (truth_value_p (TREE_CODE (arg)))
2863 arg = gimple_boolify (arg);
2864 CALL_EXPR_ARG (call, 0)
2865 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2871 switch (TREE_CODE (expr))
2873 case TRUTH_AND_EXPR:
2874 case TRUTH_OR_EXPR:
2875 case TRUTH_XOR_EXPR:
2876 case TRUTH_ANDIF_EXPR:
2877 case TRUTH_ORIF_EXPR:
2878 /* Also boolify the arguments of truth exprs. */
2879 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2880 /* FALLTHRU */
2882 case TRUTH_NOT_EXPR:
2883 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2885 /* These expressions always produce boolean results. */
2886 if (TREE_CODE (type) != BOOLEAN_TYPE)
2887 TREE_TYPE (expr) = boolean_type_node;
2888 return expr;
2890 case ANNOTATE_EXPR:
2891 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2893 case annot_expr_ivdep_kind:
2894 case annot_expr_no_vector_kind:
2895 case annot_expr_vector_kind:
2896 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2897 if (TREE_CODE (type) != BOOLEAN_TYPE)
2898 TREE_TYPE (expr) = boolean_type_node;
2899 return expr;
2900 default:
2901 gcc_unreachable ();
2904 default:
2905 if (COMPARISON_CLASS_P (expr))
2907 /* There expressions always prduce boolean results. */
2908 if (TREE_CODE (type) != BOOLEAN_TYPE)
2909 TREE_TYPE (expr) = boolean_type_node;
2910 return expr;
2912 /* Other expressions that get here must have boolean values, but
2913 might need to be converted to the appropriate mode. */
2914 if (TREE_CODE (type) == BOOLEAN_TYPE)
2915 return expr;
2916 return fold_convert_loc (loc, boolean_type_node, expr);
2920 /* Given a conditional expression *EXPR_P without side effects, gimplify
2921 its operands. New statements are inserted to PRE_P. */
2923 static enum gimplify_status
2924 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2926 tree expr = *expr_p, cond;
2927 enum gimplify_status ret, tret;
2928 enum tree_code code;
2930 cond = gimple_boolify (COND_EXPR_COND (expr));
2932 /* We need to handle && and || specially, as their gimplification
2933 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2934 code = TREE_CODE (cond);
2935 if (code == TRUTH_ANDIF_EXPR)
2936 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2937 else if (code == TRUTH_ORIF_EXPR)
2938 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2939 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2940 COND_EXPR_COND (*expr_p) = cond;
2942 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2943 is_gimple_val, fb_rvalue);
2944 ret = MIN (ret, tret);
2945 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2946 is_gimple_val, fb_rvalue);
2948 return MIN (ret, tret);
2951 /* Return true if evaluating EXPR could trap.
2952 EXPR is GENERIC, while tree_could_trap_p can be called
2953 only on GIMPLE. */
2955 static bool
2956 generic_expr_could_trap_p (tree expr)
2958 unsigned i, n;
2960 if (!expr || is_gimple_val (expr))
2961 return false;
2963 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2964 return true;
2966 n = TREE_OPERAND_LENGTH (expr);
2967 for (i = 0; i < n; i++)
2968 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2969 return true;
2971 return false;
2974 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2975 into
2977 if (p) if (p)
2978 t1 = a; a;
2979 else or else
2980 t1 = b; b;
2983 The second form is used when *EXPR_P is of type void.
2985 PRE_P points to the list where side effects that must happen before
2986 *EXPR_P should be stored. */
2988 static enum gimplify_status
2989 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2991 tree expr = *expr_p;
2992 tree type = TREE_TYPE (expr);
2993 location_t loc = EXPR_LOCATION (expr);
2994 tree tmp, arm1, arm2;
2995 enum gimplify_status ret;
2996 tree label_true, label_false, label_cont;
2997 bool have_then_clause_p, have_else_clause_p;
2998 gcond *cond_stmt;
2999 enum tree_code pred_code;
3000 gimple_seq seq = NULL;
3002 /* If this COND_EXPR has a value, copy the values into a temporary within
3003 the arms. */
3004 if (!VOID_TYPE_P (type))
3006 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3007 tree result;
3009 /* If either an rvalue is ok or we do not require an lvalue, create the
3010 temporary. But we cannot do that if the type is addressable. */
3011 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3012 && !TREE_ADDRESSABLE (type))
3014 if (gimplify_ctxp->allow_rhs_cond_expr
3015 /* If either branch has side effects or could trap, it can't be
3016 evaluated unconditionally. */
3017 && !TREE_SIDE_EFFECTS (then_)
3018 && !generic_expr_could_trap_p (then_)
3019 && !TREE_SIDE_EFFECTS (else_)
3020 && !generic_expr_could_trap_p (else_))
3021 return gimplify_pure_cond_expr (expr_p, pre_p);
3023 tmp = create_tmp_var (type, "iftmp");
3024 result = tmp;
3027 /* Otherwise, only create and copy references to the values. */
3028 else
3030 type = build_pointer_type (type);
3032 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3033 then_ = build_fold_addr_expr_loc (loc, then_);
3035 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3036 else_ = build_fold_addr_expr_loc (loc, else_);
3038 expr
3039 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3041 tmp = create_tmp_var (type, "iftmp");
3042 result = build_simple_mem_ref_loc (loc, tmp);
3045 /* Build the new then clause, `tmp = then_;'. But don't build the
3046 assignment if the value is void; in C++ it can be if it's a throw. */
3047 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3048 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3050 /* Similarly, build the new else clause, `tmp = else_;'. */
3051 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3052 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3054 TREE_TYPE (expr) = void_type_node;
3055 recalculate_side_effects (expr);
3057 /* Move the COND_EXPR to the prequeue. */
3058 gimplify_stmt (&expr, pre_p);
3060 *expr_p = result;
3061 return GS_ALL_DONE;
3064 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3065 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3066 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3067 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3069 /* Make sure the condition has BOOLEAN_TYPE. */
3070 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3072 /* Break apart && and || conditions. */
3073 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3074 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3076 expr = shortcut_cond_expr (expr);
3078 if (expr != *expr_p)
3080 *expr_p = expr;
3082 /* We can't rely on gimplify_expr to re-gimplify the expanded
3083 form properly, as cleanups might cause the target labels to be
3084 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3085 set up a conditional context. */
3086 gimple_push_condition ();
3087 gimplify_stmt (expr_p, &seq);
3088 gimple_pop_condition (pre_p);
3089 gimple_seq_add_seq (pre_p, seq);
3091 return GS_ALL_DONE;
3095 /* Now do the normal gimplification. */
3097 /* Gimplify condition. */
3098 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3099 fb_rvalue);
3100 if (ret == GS_ERROR)
3101 return GS_ERROR;
3102 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3104 gimple_push_condition ();
3106 have_then_clause_p = have_else_clause_p = false;
3107 if (TREE_OPERAND (expr, 1) != NULL
3108 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3109 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3110 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3111 == current_function_decl)
3112 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3113 have different locations, otherwise we end up with incorrect
3114 location information on the branches. */
3115 && (optimize
3116 || !EXPR_HAS_LOCATION (expr)
3117 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3118 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3120 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3121 have_then_clause_p = true;
3123 else
3124 label_true = create_artificial_label (UNKNOWN_LOCATION);
3125 if (TREE_OPERAND (expr, 2) != NULL
3126 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3127 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3128 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3129 == current_function_decl)
3130 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3131 have different locations, otherwise we end up with incorrect
3132 location information on the branches. */
3133 && (optimize
3134 || !EXPR_HAS_LOCATION (expr)
3135 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3136 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3138 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3139 have_else_clause_p = true;
3141 else
3142 label_false = create_artificial_label (UNKNOWN_LOCATION);
3144 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3145 &arm2);
3146 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3147 label_false);
3148 gimplify_seq_add_stmt (&seq, cond_stmt);
3149 gimple_stmt_iterator gsi = gsi_last (seq);
3150 maybe_fold_stmt (&gsi);
3152 label_cont = NULL_TREE;
3153 if (!have_then_clause_p)
3155 /* For if (...) {} else { code; } put label_true after
3156 the else block. */
3157 if (TREE_OPERAND (expr, 1) == NULL_TREE
3158 && !have_else_clause_p
3159 && TREE_OPERAND (expr, 2) != NULL_TREE)
3160 label_cont = label_true;
3161 else
3163 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3164 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3165 /* For if (...) { code; } else {} or
3166 if (...) { code; } else goto label; or
3167 if (...) { code; return; } else { ... }
3168 label_cont isn't needed. */
3169 if (!have_else_clause_p
3170 && TREE_OPERAND (expr, 2) != NULL_TREE
3171 && gimple_seq_may_fallthru (seq))
3173 gimple *g;
3174 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3176 g = gimple_build_goto (label_cont);
3178 /* GIMPLE_COND's are very low level; they have embedded
3179 gotos. This particular embedded goto should not be marked
3180 with the location of the original COND_EXPR, as it would
3181 correspond to the COND_EXPR's condition, not the ELSE or the
3182 THEN arms. To avoid marking it with the wrong location, flag
3183 it as "no location". */
3184 gimple_set_do_not_emit_location (g);
3186 gimplify_seq_add_stmt (&seq, g);
3190 if (!have_else_clause_p)
3192 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3193 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3195 if (label_cont)
3196 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3198 gimple_pop_condition (pre_p);
3199 gimple_seq_add_seq (pre_p, seq);
3201 if (ret == GS_ERROR)
3202 ; /* Do nothing. */
3203 else if (have_then_clause_p || have_else_clause_p)
3204 ret = GS_ALL_DONE;
3205 else
3207 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3208 expr = TREE_OPERAND (expr, 0);
3209 gimplify_stmt (&expr, pre_p);
3212 *expr_p = NULL;
3213 return ret;
3216 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3217 to be marked addressable.
3219 We cannot rely on such an expression being directly markable if a temporary
3220 has been created by the gimplification. In this case, we create another
3221 temporary and initialize it with a copy, which will become a store after we
3222 mark it addressable. This can happen if the front-end passed us something
3223 that it could not mark addressable yet, like a Fortran pass-by-reference
3224 parameter (int) floatvar. */
3226 static void
3227 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3229 while (handled_component_p (*expr_p))
3230 expr_p = &TREE_OPERAND (*expr_p, 0);
3231 if (is_gimple_reg (*expr_p))
3233 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3234 DECL_GIMPLE_REG_P (var) = 0;
3235 *expr_p = var;
3239 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3240 a call to __builtin_memcpy. */
3242 static enum gimplify_status
3243 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3244 gimple_seq *seq_p)
3246 tree t, to, to_ptr, from, from_ptr;
3247 gcall *gs;
3248 location_t loc = EXPR_LOCATION (*expr_p);
3250 to = TREE_OPERAND (*expr_p, 0);
3251 from = TREE_OPERAND (*expr_p, 1);
3253 /* Mark the RHS addressable. Beware that it may not be possible to do so
3254 directly if a temporary has been created by the gimplification. */
3255 prepare_gimple_addressable (&from, seq_p);
3257 mark_addressable (from);
3258 from_ptr = build_fold_addr_expr_loc (loc, from);
3259 gimplify_arg (&from_ptr, seq_p, loc);
3261 mark_addressable (to);
3262 to_ptr = build_fold_addr_expr_loc (loc, to);
3263 gimplify_arg (&to_ptr, seq_p, loc);
3265 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3267 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3269 if (want_value)
3271 /* tmp = memcpy() */
3272 t = create_tmp_var (TREE_TYPE (to_ptr));
3273 gimple_call_set_lhs (gs, t);
3274 gimplify_seq_add_stmt (seq_p, gs);
3276 *expr_p = build_simple_mem_ref (t);
3277 return GS_ALL_DONE;
3280 gimplify_seq_add_stmt (seq_p, gs);
3281 *expr_p = NULL;
3282 return GS_ALL_DONE;
3285 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3286 a call to __builtin_memset. In this case we know that the RHS is
3287 a CONSTRUCTOR with an empty element list. */
3289 static enum gimplify_status
3290 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3291 gimple_seq *seq_p)
3293 tree t, from, to, to_ptr;
3294 gcall *gs;
3295 location_t loc = EXPR_LOCATION (*expr_p);
3297 /* Assert our assumptions, to abort instead of producing wrong code
3298 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3299 not be immediately exposed. */
3300 from = TREE_OPERAND (*expr_p, 1);
3301 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3302 from = TREE_OPERAND (from, 0);
3304 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3305 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3307 /* Now proceed. */
3308 to = TREE_OPERAND (*expr_p, 0);
3310 to_ptr = build_fold_addr_expr_loc (loc, to);
3311 gimplify_arg (&to_ptr, seq_p, loc);
3312 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3314 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3316 if (want_value)
3318 /* tmp = memset() */
3319 t = create_tmp_var (TREE_TYPE (to_ptr));
3320 gimple_call_set_lhs (gs, t);
3321 gimplify_seq_add_stmt (seq_p, gs);
3323 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3324 return GS_ALL_DONE;
3327 gimplify_seq_add_stmt (seq_p, gs);
3328 *expr_p = NULL;
3329 return GS_ALL_DONE;
3332 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3333 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3334 assignment. Return non-null if we detect a potential overlap. */
3336 struct gimplify_init_ctor_preeval_data
3338 /* The base decl of the lhs object. May be NULL, in which case we
3339 have to assume the lhs is indirect. */
3340 tree lhs_base_decl;
3342 /* The alias set of the lhs object. */
3343 alias_set_type lhs_alias_set;
3346 static tree
3347 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3349 struct gimplify_init_ctor_preeval_data *data
3350 = (struct gimplify_init_ctor_preeval_data *) xdata;
3351 tree t = *tp;
3353 /* If we find the base object, obviously we have overlap. */
3354 if (data->lhs_base_decl == t)
3355 return t;
3357 /* If the constructor component is indirect, determine if we have a
3358 potential overlap with the lhs. The only bits of information we
3359 have to go on at this point are addressability and alias sets. */
3360 if ((INDIRECT_REF_P (t)
3361 || TREE_CODE (t) == MEM_REF)
3362 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3363 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3364 return t;
3366 /* If the constructor component is a call, determine if it can hide a
3367 potential overlap with the lhs through an INDIRECT_REF like above.
3368 ??? Ugh - this is completely broken. In fact this whole analysis
3369 doesn't look conservative. */
3370 if (TREE_CODE (t) == CALL_EXPR)
3372 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3374 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3375 if (POINTER_TYPE_P (TREE_VALUE (type))
3376 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3377 && alias_sets_conflict_p (data->lhs_alias_set,
3378 get_alias_set
3379 (TREE_TYPE (TREE_VALUE (type)))))
3380 return t;
3383 if (IS_TYPE_OR_DECL_P (t))
3384 *walk_subtrees = 0;
3385 return NULL;
3388 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3389 force values that overlap with the lhs (as described by *DATA)
3390 into temporaries. */
3392 static void
3393 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3394 struct gimplify_init_ctor_preeval_data *data)
3396 enum gimplify_status one;
3398 /* If the value is constant, then there's nothing to pre-evaluate. */
3399 if (TREE_CONSTANT (*expr_p))
3401 /* Ensure it does not have side effects, it might contain a reference to
3402 the object we're initializing. */
3403 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3404 return;
3407 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3408 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3409 return;
3411 /* Recurse for nested constructors. */
3412 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3414 unsigned HOST_WIDE_INT ix;
3415 constructor_elt *ce;
3416 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3418 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3419 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3421 return;
3424 /* If this is a variable sized type, we must remember the size. */
3425 maybe_with_size_expr (expr_p);
3427 /* Gimplify the constructor element to something appropriate for the rhs
3428 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3429 the gimplifier will consider this a store to memory. Doing this
3430 gimplification now means that we won't have to deal with complicated
3431 language-specific trees, nor trees like SAVE_EXPR that can induce
3432 exponential search behavior. */
3433 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3434 if (one == GS_ERROR)
3436 *expr_p = NULL;
3437 return;
3440 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3441 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3442 always be true for all scalars, since is_gimple_mem_rhs insists on a
3443 temporary variable for them. */
3444 if (DECL_P (*expr_p))
3445 return;
3447 /* If this is of variable size, we have no choice but to assume it doesn't
3448 overlap since we can't make a temporary for it. */
3449 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3450 return;
3452 /* Otherwise, we must search for overlap ... */
3453 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3454 return;
3456 /* ... and if found, force the value into a temporary. */
3457 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3460 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3461 a RANGE_EXPR in a CONSTRUCTOR for an array.
3463 var = lower;
3464 loop_entry:
3465 object[var] = value;
3466 if (var == upper)
3467 goto loop_exit;
3468 var = var + 1;
3469 goto loop_entry;
3470 loop_exit:
3472 We increment var _after_ the loop exit check because we might otherwise
3473 fail if upper == TYPE_MAX_VALUE (type for upper).
3475 Note that we never have to deal with SAVE_EXPRs here, because this has
3476 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3478 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3479 gimple_seq *, bool);
3481 static void
3482 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3483 tree value, tree array_elt_type,
3484 gimple_seq *pre_p, bool cleared)
3486 tree loop_entry_label, loop_exit_label, fall_thru_label;
3487 tree var, var_type, cref, tmp;
3489 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3490 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3491 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3493 /* Create and initialize the index variable. */
3494 var_type = TREE_TYPE (upper);
3495 var = create_tmp_var (var_type);
3496 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3498 /* Add the loop entry label. */
3499 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3501 /* Build the reference. */
3502 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3503 var, NULL_TREE, NULL_TREE);
3505 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3506 the store. Otherwise just assign value to the reference. */
3508 if (TREE_CODE (value) == CONSTRUCTOR)
3509 /* NB we might have to call ourself recursively through
3510 gimplify_init_ctor_eval if the value is a constructor. */
3511 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3512 pre_p, cleared);
3513 else
3514 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3516 /* We exit the loop when the index var is equal to the upper bound. */
3517 gimplify_seq_add_stmt (pre_p,
3518 gimple_build_cond (EQ_EXPR, var, upper,
3519 loop_exit_label, fall_thru_label));
3521 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3523 /* Otherwise, increment the index var... */
3524 tmp = build2 (PLUS_EXPR, var_type, var,
3525 fold_convert (var_type, integer_one_node));
3526 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3528 /* ...and jump back to the loop entry. */
3529 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3531 /* Add the loop exit label. */
3532 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3535 /* Return true if FDECL is accessing a field that is zero sized. */
3537 static bool
3538 zero_sized_field_decl (const_tree fdecl)
3540 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3541 && integer_zerop (DECL_SIZE (fdecl)))
3542 return true;
3543 return false;
3546 /* Return true if TYPE is zero sized. */
3548 static bool
3549 zero_sized_type (const_tree type)
3551 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3552 && integer_zerop (TYPE_SIZE (type)))
3553 return true;
3554 return false;
3557 /* A subroutine of gimplify_init_constructor. Generate individual
3558 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3559 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3560 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3561 zeroed first. */
3563 static void
3564 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3565 gimple_seq *pre_p, bool cleared)
3567 tree array_elt_type = NULL;
3568 unsigned HOST_WIDE_INT ix;
3569 tree purpose, value;
3571 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3572 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3574 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3576 tree cref;
3578 /* NULL values are created above for gimplification errors. */
3579 if (value == NULL)
3580 continue;
3582 if (cleared && initializer_zerop (value))
3583 continue;
3585 /* ??? Here's to hoping the front end fills in all of the indices,
3586 so we don't have to figure out what's missing ourselves. */
3587 gcc_assert (purpose);
3589 /* Skip zero-sized fields, unless value has side-effects. This can
3590 happen with calls to functions returning a zero-sized type, which
3591 we shouldn't discard. As a number of downstream passes don't
3592 expect sets of zero-sized fields, we rely on the gimplification of
3593 the MODIFY_EXPR we make below to drop the assignment statement. */
3594 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3595 continue;
3597 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3598 whole range. */
3599 if (TREE_CODE (purpose) == RANGE_EXPR)
3601 tree lower = TREE_OPERAND (purpose, 0);
3602 tree upper = TREE_OPERAND (purpose, 1);
3604 /* If the lower bound is equal to upper, just treat it as if
3605 upper was the index. */
3606 if (simple_cst_equal (lower, upper))
3607 purpose = upper;
3608 else
3610 gimplify_init_ctor_eval_range (object, lower, upper, value,
3611 array_elt_type, pre_p, cleared);
3612 continue;
3616 if (array_elt_type)
3618 /* Do not use bitsizetype for ARRAY_REF indices. */
3619 if (TYPE_DOMAIN (TREE_TYPE (object)))
3620 purpose
3621 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3622 purpose);
3623 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3624 purpose, NULL_TREE, NULL_TREE);
3626 else
3628 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3629 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3630 unshare_expr (object), purpose, NULL_TREE);
3633 if (TREE_CODE (value) == CONSTRUCTOR
3634 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3635 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3636 pre_p, cleared);
3637 else
3639 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3640 gimplify_and_add (init, pre_p);
3641 ggc_free (init);
3646 /* Return the appropriate RHS predicate for this LHS. */
3648 gimple_predicate
3649 rhs_predicate_for (tree lhs)
3651 if (is_gimple_reg (lhs))
3652 return is_gimple_reg_rhs_or_call;
3653 else
3654 return is_gimple_mem_rhs_or_call;
3657 /* Gimplify a C99 compound literal expression. This just means adding
3658 the DECL_EXPR before the current statement and using its anonymous
3659 decl instead. */
3661 static enum gimplify_status
3662 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3663 bool (*gimple_test_f) (tree),
3664 fallback_t fallback)
3666 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3667 tree decl = DECL_EXPR_DECL (decl_s);
3668 tree init = DECL_INITIAL (decl);
3669 /* Mark the decl as addressable if the compound literal
3670 expression is addressable now, otherwise it is marked too late
3671 after we gimplify the initialization expression. */
3672 if (TREE_ADDRESSABLE (*expr_p))
3673 TREE_ADDRESSABLE (decl) = 1;
3674 /* Otherwise, if we don't need an lvalue and have a literal directly
3675 substitute it. Check if it matches the gimple predicate, as
3676 otherwise we'd generate a new temporary, and we can as well just
3677 use the decl we already have. */
3678 else if (!TREE_ADDRESSABLE (decl)
3679 && init
3680 && (fallback & fb_lvalue) == 0
3681 && gimple_test_f (init))
3683 *expr_p = init;
3684 return GS_OK;
3687 /* Preliminarily mark non-addressed complex variables as eligible
3688 for promotion to gimple registers. We'll transform their uses
3689 as we find them. */
3690 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3691 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3692 && !TREE_THIS_VOLATILE (decl)
3693 && !needs_to_live_in_memory (decl))
3694 DECL_GIMPLE_REG_P (decl) = 1;
3696 /* If the decl is not addressable, then it is being used in some
3697 expression or on the right hand side of a statement, and it can
3698 be put into a readonly data section. */
3699 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3700 TREE_READONLY (decl) = 1;
3702 /* This decl isn't mentioned in the enclosing block, so add it to the
3703 list of temps. FIXME it seems a bit of a kludge to say that
3704 anonymous artificial vars aren't pushed, but everything else is. */
3705 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3706 gimple_add_tmp_var (decl);
3708 gimplify_and_add (decl_s, pre_p);
3709 *expr_p = decl;
3710 return GS_OK;
3713 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3714 return a new CONSTRUCTOR if something changed. */
3716 static tree
3717 optimize_compound_literals_in_ctor (tree orig_ctor)
3719 tree ctor = orig_ctor;
3720 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3721 unsigned int idx, num = vec_safe_length (elts);
3723 for (idx = 0; idx < num; idx++)
3725 tree value = (*elts)[idx].value;
3726 tree newval = value;
3727 if (TREE_CODE (value) == CONSTRUCTOR)
3728 newval = optimize_compound_literals_in_ctor (value);
3729 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3731 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3732 tree decl = DECL_EXPR_DECL (decl_s);
3733 tree init = DECL_INITIAL (decl);
3735 if (!TREE_ADDRESSABLE (value)
3736 && !TREE_ADDRESSABLE (decl)
3737 && init
3738 && TREE_CODE (init) == CONSTRUCTOR)
3739 newval = optimize_compound_literals_in_ctor (init);
3741 if (newval == value)
3742 continue;
3744 if (ctor == orig_ctor)
3746 ctor = copy_node (orig_ctor);
3747 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3748 elts = CONSTRUCTOR_ELTS (ctor);
3750 (*elts)[idx].value = newval;
3752 return ctor;
3755 /* A subroutine of gimplify_modify_expr. Break out elements of a
3756 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3758 Note that we still need to clear any elements that don't have explicit
3759 initializers, so if not all elements are initialized we keep the
3760 original MODIFY_EXPR, we just remove all of the constructor elements.
3762 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3763 GS_ERROR if we would have to create a temporary when gimplifying
3764 this constructor. Otherwise, return GS_OK.
3766 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3768 static enum gimplify_status
3769 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3770 bool want_value, bool notify_temp_creation)
3772 tree object, ctor, type;
3773 enum gimplify_status ret;
3774 vec<constructor_elt, va_gc> *elts;
3776 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3778 if (!notify_temp_creation)
3780 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3781 is_gimple_lvalue, fb_lvalue);
3782 if (ret == GS_ERROR)
3783 return ret;
3786 object = TREE_OPERAND (*expr_p, 0);
3787 ctor = TREE_OPERAND (*expr_p, 1) =
3788 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3789 type = TREE_TYPE (ctor);
3790 elts = CONSTRUCTOR_ELTS (ctor);
3791 ret = GS_ALL_DONE;
3793 switch (TREE_CODE (type))
3795 case RECORD_TYPE:
3796 case UNION_TYPE:
3797 case QUAL_UNION_TYPE:
3798 case ARRAY_TYPE:
3800 struct gimplify_init_ctor_preeval_data preeval_data;
3801 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3802 bool cleared, complete_p, valid_const_initializer;
3804 /* Aggregate types must lower constructors to initialization of
3805 individual elements. The exception is that a CONSTRUCTOR node
3806 with no elements indicates zero-initialization of the whole. */
3807 if (vec_safe_is_empty (elts))
3809 if (notify_temp_creation)
3810 return GS_OK;
3811 break;
3814 /* Fetch information about the constructor to direct later processing.
3815 We might want to make static versions of it in various cases, and
3816 can only do so if it known to be a valid constant initializer. */
3817 valid_const_initializer
3818 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3819 &num_ctor_elements, &complete_p);
3821 /* If a const aggregate variable is being initialized, then it
3822 should never be a lose to promote the variable to be static. */
3823 if (valid_const_initializer
3824 && num_nonzero_elements > 1
3825 && TREE_READONLY (object)
3826 && TREE_CODE (object) == VAR_DECL
3827 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3829 if (notify_temp_creation)
3830 return GS_ERROR;
3831 DECL_INITIAL (object) = ctor;
3832 TREE_STATIC (object) = 1;
3833 if (!DECL_NAME (object))
3834 DECL_NAME (object) = create_tmp_var_name ("C");
3835 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3837 /* ??? C++ doesn't automatically append a .<number> to the
3838 assembler name, and even when it does, it looks at FE private
3839 data structures to figure out what that number should be,
3840 which are not set for this variable. I suppose this is
3841 important for local statics for inline functions, which aren't
3842 "local" in the object file sense. So in order to get a unique
3843 TU-local symbol, we must invoke the lhd version now. */
3844 lhd_set_decl_assembler_name (object);
3846 *expr_p = NULL_TREE;
3847 break;
3850 /* If there are "lots" of initialized elements, even discounting
3851 those that are not address constants (and thus *must* be
3852 computed at runtime), then partition the constructor into
3853 constant and non-constant parts. Block copy the constant
3854 parts in, then generate code for the non-constant parts. */
3855 /* TODO. There's code in cp/typeck.c to do this. */
3857 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3858 /* store_constructor will ignore the clearing of variable-sized
3859 objects. Initializers for such objects must explicitly set
3860 every field that needs to be set. */
3861 cleared = false;
3862 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3863 /* If the constructor isn't complete, clear the whole object
3864 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3866 ??? This ought not to be needed. For any element not present
3867 in the initializer, we should simply set them to zero. Except
3868 we'd need to *find* the elements that are not present, and that
3869 requires trickery to avoid quadratic compile-time behavior in
3870 large cases or excessive memory use in small cases. */
3871 cleared = true;
3872 else if (num_ctor_elements - num_nonzero_elements
3873 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3874 && num_nonzero_elements < num_ctor_elements / 4)
3875 /* If there are "lots" of zeros, it's more efficient to clear
3876 the memory and then set the nonzero elements. */
3877 cleared = true;
3878 else
3879 cleared = false;
3881 /* If there are "lots" of initialized elements, and all of them
3882 are valid address constants, then the entire initializer can
3883 be dropped to memory, and then memcpy'd out. Don't do this
3884 for sparse arrays, though, as it's more efficient to follow
3885 the standard CONSTRUCTOR behavior of memset followed by
3886 individual element initialization. Also don't do this for small
3887 all-zero initializers (which aren't big enough to merit
3888 clearing), and don't try to make bitwise copies of
3889 TREE_ADDRESSABLE types.
3891 We cannot apply such transformation when compiling chkp static
3892 initializer because creation of initializer image in the memory
3893 will require static initialization of bounds for it. It should
3894 result in another gimplification of similar initializer and we
3895 may fall into infinite loop. */
3896 if (valid_const_initializer
3897 && !(cleared || num_nonzero_elements == 0)
3898 && !TREE_ADDRESSABLE (type)
3899 && (!current_function_decl
3900 || !lookup_attribute ("chkp ctor",
3901 DECL_ATTRIBUTES (current_function_decl))))
3903 HOST_WIDE_INT size = int_size_in_bytes (type);
3904 unsigned int align;
3906 /* ??? We can still get unbounded array types, at least
3907 from the C++ front end. This seems wrong, but attempt
3908 to work around it for now. */
3909 if (size < 0)
3911 size = int_size_in_bytes (TREE_TYPE (object));
3912 if (size >= 0)
3913 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3916 /* Find the maximum alignment we can assume for the object. */
3917 /* ??? Make use of DECL_OFFSET_ALIGN. */
3918 if (DECL_P (object))
3919 align = DECL_ALIGN (object);
3920 else
3921 align = TYPE_ALIGN (type);
3923 /* Do a block move either if the size is so small as to make
3924 each individual move a sub-unit move on average, or if it
3925 is so large as to make individual moves inefficient. */
3926 if (size > 0
3927 && num_nonzero_elements > 1
3928 && (size < num_nonzero_elements
3929 || !can_move_by_pieces (size, align)))
3931 if (notify_temp_creation)
3932 return GS_ERROR;
3934 walk_tree (&ctor, force_labels_r, NULL, NULL);
3935 ctor = tree_output_constant_def (ctor);
3936 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3937 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3938 TREE_OPERAND (*expr_p, 1) = ctor;
3940 /* This is no longer an assignment of a CONSTRUCTOR, but
3941 we still may have processing to do on the LHS. So
3942 pretend we didn't do anything here to let that happen. */
3943 return GS_UNHANDLED;
3947 /* If the target is volatile, we have non-zero elements and more than
3948 one field to assign, initialize the target from a temporary. */
3949 if (TREE_THIS_VOLATILE (object)
3950 && !TREE_ADDRESSABLE (type)
3951 && num_nonzero_elements > 0
3952 && vec_safe_length (elts) > 1)
3954 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
3955 TREE_OPERAND (*expr_p, 0) = temp;
3956 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3957 *expr_p,
3958 build2 (MODIFY_EXPR, void_type_node,
3959 object, temp));
3960 return GS_OK;
3963 if (notify_temp_creation)
3964 return GS_OK;
3966 /* If there are nonzero elements and if needed, pre-evaluate to capture
3967 elements overlapping with the lhs into temporaries. We must do this
3968 before clearing to fetch the values before they are zeroed-out. */
3969 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3971 preeval_data.lhs_base_decl = get_base_address (object);
3972 if (!DECL_P (preeval_data.lhs_base_decl))
3973 preeval_data.lhs_base_decl = NULL;
3974 preeval_data.lhs_alias_set = get_alias_set (object);
3976 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3977 pre_p, post_p, &preeval_data);
3980 bool ctor_has_side_effects_p
3981 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
3983 if (cleared)
3985 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3986 Note that we still have to gimplify, in order to handle the
3987 case of variable sized types. Avoid shared tree structures. */
3988 CONSTRUCTOR_ELTS (ctor) = NULL;
3989 TREE_SIDE_EFFECTS (ctor) = 0;
3990 object = unshare_expr (object);
3991 gimplify_stmt (expr_p, pre_p);
3994 /* If we have not block cleared the object, or if there are nonzero
3995 elements in the constructor, or if the constructor has side effects,
3996 add assignments to the individual scalar fields of the object. */
3997 if (!cleared
3998 || num_nonzero_elements > 0
3999 || ctor_has_side_effects_p)
4000 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4002 *expr_p = NULL_TREE;
4004 break;
4006 case COMPLEX_TYPE:
4008 tree r, i;
4010 if (notify_temp_creation)
4011 return GS_OK;
4013 /* Extract the real and imaginary parts out of the ctor. */
4014 gcc_assert (elts->length () == 2);
4015 r = (*elts)[0].value;
4016 i = (*elts)[1].value;
4017 if (r == NULL || i == NULL)
4019 tree zero = build_zero_cst (TREE_TYPE (type));
4020 if (r == NULL)
4021 r = zero;
4022 if (i == NULL)
4023 i = zero;
4026 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4027 represent creation of a complex value. */
4028 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4030 ctor = build_complex (type, r, i);
4031 TREE_OPERAND (*expr_p, 1) = ctor;
4033 else
4035 ctor = build2 (COMPLEX_EXPR, type, r, i);
4036 TREE_OPERAND (*expr_p, 1) = ctor;
4037 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4038 pre_p,
4039 post_p,
4040 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4041 fb_rvalue);
4044 break;
4046 case VECTOR_TYPE:
4048 unsigned HOST_WIDE_INT ix;
4049 constructor_elt *ce;
4051 if (notify_temp_creation)
4052 return GS_OK;
4054 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4055 if (TREE_CONSTANT (ctor))
4057 bool constant_p = true;
4058 tree value;
4060 /* Even when ctor is constant, it might contain non-*_CST
4061 elements, such as addresses or trapping values like
4062 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4063 in VECTOR_CST nodes. */
4064 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4065 if (!CONSTANT_CLASS_P (value))
4067 constant_p = false;
4068 break;
4071 if (constant_p)
4073 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4074 break;
4077 TREE_CONSTANT (ctor) = 0;
4080 /* Vector types use CONSTRUCTOR all the way through gimple
4081 compilation as a general initializer. */
4082 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4084 enum gimplify_status tret;
4085 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4086 fb_rvalue);
4087 if (tret == GS_ERROR)
4088 ret = GS_ERROR;
4090 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4091 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4093 break;
4095 default:
4096 /* So how did we get a CONSTRUCTOR for a scalar type? */
4097 gcc_unreachable ();
4100 if (ret == GS_ERROR)
4101 return GS_ERROR;
4102 else if (want_value)
4104 *expr_p = object;
4105 return GS_OK;
4107 else
4109 /* If we have gimplified both sides of the initializer but have
4110 not emitted an assignment, do so now. */
4111 if (*expr_p)
4113 tree lhs = TREE_OPERAND (*expr_p, 0);
4114 tree rhs = TREE_OPERAND (*expr_p, 1);
4115 gassign *init = gimple_build_assign (lhs, rhs);
4116 gimplify_seq_add_stmt (pre_p, init);
4117 *expr_p = NULL;
4120 return GS_ALL_DONE;
4124 /* Given a pointer value OP0, return a simplified version of an
4125 indirection through OP0, or NULL_TREE if no simplification is
4126 possible. This may only be applied to a rhs of an expression.
4127 Note that the resulting type may be different from the type pointed
4128 to in the sense that it is still compatible from the langhooks
4129 point of view. */
4131 static tree
4132 gimple_fold_indirect_ref_rhs (tree t)
4134 return gimple_fold_indirect_ref (t);
4137 /* Subroutine of gimplify_modify_expr to do simplifications of
4138 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4139 something changes. */
4141 static enum gimplify_status
4142 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4143 gimple_seq *pre_p, gimple_seq *post_p,
4144 bool want_value)
4146 enum gimplify_status ret = GS_UNHANDLED;
4147 bool changed;
4151 changed = false;
4152 switch (TREE_CODE (*from_p))
4154 case VAR_DECL:
4155 /* If we're assigning from a read-only variable initialized with
4156 a constructor, do the direct assignment from the constructor,
4157 but only if neither source nor target are volatile since this
4158 latter assignment might end up being done on a per-field basis. */
4159 if (DECL_INITIAL (*from_p)
4160 && TREE_READONLY (*from_p)
4161 && !TREE_THIS_VOLATILE (*from_p)
4162 && !TREE_THIS_VOLATILE (*to_p)
4163 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4165 tree old_from = *from_p;
4166 enum gimplify_status subret;
4168 /* Move the constructor into the RHS. */
4169 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4171 /* Let's see if gimplify_init_constructor will need to put
4172 it in memory. */
4173 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4174 false, true);
4175 if (subret == GS_ERROR)
4177 /* If so, revert the change. */
4178 *from_p = old_from;
4180 else
4182 ret = GS_OK;
4183 changed = true;
4186 break;
4187 case INDIRECT_REF:
4189 /* If we have code like
4191 *(const A*)(A*)&x
4193 where the type of "x" is a (possibly cv-qualified variant
4194 of "A"), treat the entire expression as identical to "x".
4195 This kind of code arises in C++ when an object is bound
4196 to a const reference, and if "x" is a TARGET_EXPR we want
4197 to take advantage of the optimization below. */
4198 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4199 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4200 if (t)
4202 if (TREE_THIS_VOLATILE (t) != volatile_p)
4204 if (DECL_P (t))
4205 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4206 build_fold_addr_expr (t));
4207 if (REFERENCE_CLASS_P (t))
4208 TREE_THIS_VOLATILE (t) = volatile_p;
4210 *from_p = t;
4211 ret = GS_OK;
4212 changed = true;
4214 break;
4217 case TARGET_EXPR:
4219 /* If we are initializing something from a TARGET_EXPR, strip the
4220 TARGET_EXPR and initialize it directly, if possible. This can't
4221 be done if the initializer is void, since that implies that the
4222 temporary is set in some non-trivial way.
4224 ??? What about code that pulls out the temp and uses it
4225 elsewhere? I think that such code never uses the TARGET_EXPR as
4226 an initializer. If I'm wrong, we'll die because the temp won't
4227 have any RTL. In that case, I guess we'll need to replace
4228 references somehow. */
4229 tree init = TARGET_EXPR_INITIAL (*from_p);
4231 if (init
4232 && !VOID_TYPE_P (TREE_TYPE (init)))
4234 *from_p = init;
4235 ret = GS_OK;
4236 changed = true;
4239 break;
4241 case COMPOUND_EXPR:
4242 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4243 caught. */
4244 gimplify_compound_expr (from_p, pre_p, true);
4245 ret = GS_OK;
4246 changed = true;
4247 break;
4249 case CONSTRUCTOR:
4250 /* If we already made some changes, let the front end have a
4251 crack at this before we break it down. */
4252 if (ret != GS_UNHANDLED)
4253 break;
4254 /* If we're initializing from a CONSTRUCTOR, break this into
4255 individual MODIFY_EXPRs. */
4256 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4257 false);
4259 case COND_EXPR:
4260 /* If we're assigning to a non-register type, push the assignment
4261 down into the branches. This is mandatory for ADDRESSABLE types,
4262 since we cannot generate temporaries for such, but it saves a
4263 copy in other cases as well. */
4264 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4266 /* This code should mirror the code in gimplify_cond_expr. */
4267 enum tree_code code = TREE_CODE (*expr_p);
4268 tree cond = *from_p;
4269 tree result = *to_p;
4271 ret = gimplify_expr (&result, pre_p, post_p,
4272 is_gimple_lvalue, fb_lvalue);
4273 if (ret != GS_ERROR)
4274 ret = GS_OK;
4276 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4277 TREE_OPERAND (cond, 1)
4278 = build2 (code, void_type_node, result,
4279 TREE_OPERAND (cond, 1));
4280 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4281 TREE_OPERAND (cond, 2)
4282 = build2 (code, void_type_node, unshare_expr (result),
4283 TREE_OPERAND (cond, 2));
4285 TREE_TYPE (cond) = void_type_node;
4286 recalculate_side_effects (cond);
4288 if (want_value)
4290 gimplify_and_add (cond, pre_p);
4291 *expr_p = unshare_expr (result);
4293 else
4294 *expr_p = cond;
4295 return ret;
4297 break;
4299 case CALL_EXPR:
4300 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4301 return slot so that we don't generate a temporary. */
4302 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4303 && aggregate_value_p (*from_p, *from_p))
4305 bool use_target;
4307 if (!(rhs_predicate_for (*to_p))(*from_p))
4308 /* If we need a temporary, *to_p isn't accurate. */
4309 use_target = false;
4310 /* It's OK to use the return slot directly unless it's an NRV. */
4311 else if (TREE_CODE (*to_p) == RESULT_DECL
4312 && DECL_NAME (*to_p) == NULL_TREE
4313 && needs_to_live_in_memory (*to_p))
4314 use_target = true;
4315 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4316 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4317 /* Don't force regs into memory. */
4318 use_target = false;
4319 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4320 /* It's OK to use the target directly if it's being
4321 initialized. */
4322 use_target = true;
4323 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
4324 != INTEGER_CST)
4325 /* Always use the target and thus RSO for variable-sized types.
4326 GIMPLE cannot deal with a variable-sized assignment
4327 embedded in a call statement. */
4328 use_target = true;
4329 else if (TREE_CODE (*to_p) != SSA_NAME
4330 && (!is_gimple_variable (*to_p)
4331 || needs_to_live_in_memory (*to_p)))
4332 /* Don't use the original target if it's already addressable;
4333 if its address escapes, and the called function uses the
4334 NRV optimization, a conforming program could see *to_p
4335 change before the called function returns; see c++/19317.
4336 When optimizing, the return_slot pass marks more functions
4337 as safe after we have escape info. */
4338 use_target = false;
4339 else
4340 use_target = true;
4342 if (use_target)
4344 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4345 mark_addressable (*to_p);
4348 break;
4350 case WITH_SIZE_EXPR:
4351 /* Likewise for calls that return an aggregate of non-constant size,
4352 since we would not be able to generate a temporary at all. */
4353 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4355 *from_p = TREE_OPERAND (*from_p, 0);
4356 /* We don't change ret in this case because the
4357 WITH_SIZE_EXPR might have been added in
4358 gimplify_modify_expr, so returning GS_OK would lead to an
4359 infinite loop. */
4360 changed = true;
4362 break;
4364 /* If we're initializing from a container, push the initialization
4365 inside it. */
4366 case CLEANUP_POINT_EXPR:
4367 case BIND_EXPR:
4368 case STATEMENT_LIST:
4370 tree wrap = *from_p;
4371 tree t;
4373 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4374 fb_lvalue);
4375 if (ret != GS_ERROR)
4376 ret = GS_OK;
4378 t = voidify_wrapper_expr (wrap, *expr_p);
4379 gcc_assert (t == *expr_p);
4381 if (want_value)
4383 gimplify_and_add (wrap, pre_p);
4384 *expr_p = unshare_expr (*to_p);
4386 else
4387 *expr_p = wrap;
4388 return GS_OK;
4391 case COMPOUND_LITERAL_EXPR:
4393 tree complit = TREE_OPERAND (*expr_p, 1);
4394 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4395 tree decl = DECL_EXPR_DECL (decl_s);
4396 tree init = DECL_INITIAL (decl);
4398 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4399 into struct T x = { 0, 1, 2 } if the address of the
4400 compound literal has never been taken. */
4401 if (!TREE_ADDRESSABLE (complit)
4402 && !TREE_ADDRESSABLE (decl)
4403 && init)
4405 *expr_p = copy_node (*expr_p);
4406 TREE_OPERAND (*expr_p, 1) = init;
4407 return GS_OK;
4411 default:
4412 break;
4415 while (changed);
4417 return ret;
4421 /* Return true if T looks like a valid GIMPLE statement. */
4423 static bool
4424 is_gimple_stmt (tree t)
4426 const enum tree_code code = TREE_CODE (t);
4428 switch (code)
4430 case NOP_EXPR:
4431 /* The only valid NOP_EXPR is the empty statement. */
4432 return IS_EMPTY_STMT (t);
4434 case BIND_EXPR:
4435 case COND_EXPR:
4436 /* These are only valid if they're void. */
4437 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4439 case SWITCH_EXPR:
4440 case GOTO_EXPR:
4441 case RETURN_EXPR:
4442 case LABEL_EXPR:
4443 case CASE_LABEL_EXPR:
4444 case TRY_CATCH_EXPR:
4445 case TRY_FINALLY_EXPR:
4446 case EH_FILTER_EXPR:
4447 case CATCH_EXPR:
4448 case ASM_EXPR:
4449 case STATEMENT_LIST:
4450 case OACC_PARALLEL:
4451 case OACC_KERNELS:
4452 case OACC_DATA:
4453 case OACC_HOST_DATA:
4454 case OACC_DECLARE:
4455 case OACC_UPDATE:
4456 case OACC_ENTER_DATA:
4457 case OACC_EXIT_DATA:
4458 case OACC_CACHE:
4459 case OMP_PARALLEL:
4460 case OMP_FOR:
4461 case OMP_SIMD:
4462 case CILK_SIMD:
4463 case OMP_DISTRIBUTE:
4464 case OACC_LOOP:
4465 case OMP_SECTIONS:
4466 case OMP_SECTION:
4467 case OMP_SINGLE:
4468 case OMP_MASTER:
4469 case OMP_TASKGROUP:
4470 case OMP_ORDERED:
4471 case OMP_CRITICAL:
4472 case OMP_TASK:
4473 case OMP_TARGET:
4474 case OMP_TARGET_DATA:
4475 case OMP_TARGET_UPDATE:
4476 case OMP_TARGET_ENTER_DATA:
4477 case OMP_TARGET_EXIT_DATA:
4478 case OMP_TASKLOOP:
4479 case OMP_TEAMS:
4480 /* These are always void. */
4481 return true;
4483 case CALL_EXPR:
4484 case MODIFY_EXPR:
4485 case PREDICT_EXPR:
4486 /* These are valid regardless of their type. */
4487 return true;
4489 default:
4490 return false;
4495 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4496 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4497 DECL_GIMPLE_REG_P set.
4499 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4500 other, unmodified part of the complex object just before the total store.
4501 As a consequence, if the object is still uninitialized, an undefined value
4502 will be loaded into a register, which may result in a spurious exception
4503 if the register is floating-point and the value happens to be a signaling
4504 NaN for example. Then the fully-fledged complex operations lowering pass
4505 followed by a DCE pass are necessary in order to fix things up. */
4507 static enum gimplify_status
4508 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4509 bool want_value)
4511 enum tree_code code, ocode;
4512 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4514 lhs = TREE_OPERAND (*expr_p, 0);
4515 rhs = TREE_OPERAND (*expr_p, 1);
4516 code = TREE_CODE (lhs);
4517 lhs = TREE_OPERAND (lhs, 0);
4519 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4520 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4521 TREE_NO_WARNING (other) = 1;
4522 other = get_formal_tmp_var (other, pre_p);
4524 realpart = code == REALPART_EXPR ? rhs : other;
4525 imagpart = code == REALPART_EXPR ? other : rhs;
4527 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4528 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4529 else
4530 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4532 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4533 *expr_p = (want_value) ? rhs : NULL_TREE;
4535 return GS_ALL_DONE;
4538 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4540 modify_expr
4541 : varname '=' rhs
4542 | '*' ID '=' rhs
4544 PRE_P points to the list where side effects that must happen before
4545 *EXPR_P should be stored.
4547 POST_P points to the list where side effects that must happen after
4548 *EXPR_P should be stored.
4550 WANT_VALUE is nonzero iff we want to use the value of this expression
4551 in another expression. */
4553 static enum gimplify_status
4554 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4555 bool want_value)
4557 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4558 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4559 enum gimplify_status ret = GS_UNHANDLED;
4560 gimple *assign;
4561 location_t loc = EXPR_LOCATION (*expr_p);
4562 gimple_stmt_iterator gsi;
4564 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4565 || TREE_CODE (*expr_p) == INIT_EXPR);
4567 /* Trying to simplify a clobber using normal logic doesn't work,
4568 so handle it here. */
4569 if (TREE_CLOBBER_P (*from_p))
4571 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4572 if (ret == GS_ERROR)
4573 return ret;
4574 gcc_assert (!want_value
4575 && (TREE_CODE (*to_p) == VAR_DECL
4576 || TREE_CODE (*to_p) == MEM_REF));
4577 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4578 *expr_p = NULL;
4579 return GS_ALL_DONE;
4582 /* Insert pointer conversions required by the middle-end that are not
4583 required by the frontend. This fixes middle-end type checking for
4584 for example gcc.dg/redecl-6.c. */
4585 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4587 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4588 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4589 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4592 /* See if any simplifications can be done based on what the RHS is. */
4593 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4594 want_value);
4595 if (ret != GS_UNHANDLED)
4596 return ret;
4598 /* For zero sized types only gimplify the left hand side and right hand
4599 side as statements and throw away the assignment. Do this after
4600 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4601 types properly. */
4602 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4604 gimplify_stmt (from_p, pre_p);
4605 gimplify_stmt (to_p, pre_p);
4606 *expr_p = NULL_TREE;
4607 return GS_ALL_DONE;
4610 /* If the value being copied is of variable width, compute the length
4611 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4612 before gimplifying any of the operands so that we can resolve any
4613 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4614 the size of the expression to be copied, not of the destination, so
4615 that is what we must do here. */
4616 maybe_with_size_expr (from_p);
4618 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4619 if (ret == GS_ERROR)
4620 return ret;
4622 /* As a special case, we have to temporarily allow for assignments
4623 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4624 a toplevel statement, when gimplifying the GENERIC expression
4625 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4626 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4628 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4629 prevent gimplify_expr from trying to create a new temporary for
4630 foo's LHS, we tell it that it should only gimplify until it
4631 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4632 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4633 and all we need to do here is set 'a' to be its LHS. */
4634 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4635 fb_rvalue);
4636 if (ret == GS_ERROR)
4637 return ret;
4639 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
4640 size as argument to the call. */
4641 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4643 tree call = TREE_OPERAND (*from_p, 0);
4644 tree vlasize = TREE_OPERAND (*from_p, 1);
4646 if (TREE_CODE (call) == CALL_EXPR
4647 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
4649 int nargs = call_expr_nargs (call);
4650 tree type = TREE_TYPE (call);
4651 tree ap = CALL_EXPR_ARG (call, 0);
4652 tree tag = CALL_EXPR_ARG (call, 1);
4653 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
4654 IFN_VA_ARG, type,
4655 nargs + 1, ap, tag,
4656 vlasize);
4657 tree *call_p = &(TREE_OPERAND (*from_p, 0));
4658 *call_p = newcall;
4662 /* Now see if the above changed *from_p to something we handle specially. */
4663 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4664 want_value);
4665 if (ret != GS_UNHANDLED)
4666 return ret;
4668 /* If we've got a variable sized assignment between two lvalues (i.e. does
4669 not involve a call), then we can make things a bit more straightforward
4670 by converting the assignment to memcpy or memset. */
4671 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4673 tree from = TREE_OPERAND (*from_p, 0);
4674 tree size = TREE_OPERAND (*from_p, 1);
4676 if (TREE_CODE (from) == CONSTRUCTOR)
4677 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4679 if (is_gimple_addressable (from))
4681 *from_p = from;
4682 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4683 pre_p);
4687 /* Transform partial stores to non-addressable complex variables into
4688 total stores. This allows us to use real instead of virtual operands
4689 for these variables, which improves optimization. */
4690 if ((TREE_CODE (*to_p) == REALPART_EXPR
4691 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4692 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4693 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4695 /* Try to alleviate the effects of the gimplification creating artificial
4696 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
4697 make sure not to create DECL_DEBUG_EXPR links across functions. */
4698 if (!gimplify_ctxp->into_ssa
4699 && TREE_CODE (*from_p) == VAR_DECL
4700 && DECL_IGNORED_P (*from_p)
4701 && DECL_P (*to_p)
4702 && !DECL_IGNORED_P (*to_p)
4703 && decl_function_context (*to_p) == current_function_decl)
4705 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4706 DECL_NAME (*from_p)
4707 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4708 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4709 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4712 if (want_value && TREE_THIS_VOLATILE (*to_p))
4713 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4715 if (TREE_CODE (*from_p) == CALL_EXPR)
4717 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4718 instead of a GIMPLE_ASSIGN. */
4719 gcall *call_stmt;
4720 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
4722 /* Gimplify internal functions created in the FEs. */
4723 int nargs = call_expr_nargs (*from_p), i;
4724 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
4725 auto_vec<tree> vargs (nargs);
4727 for (i = 0; i < nargs; i++)
4729 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
4730 EXPR_LOCATION (*from_p));
4731 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
4733 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
4734 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
4736 else
4738 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4739 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4740 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4741 tree fndecl = get_callee_fndecl (*from_p);
4742 if (fndecl
4743 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4744 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4745 && call_expr_nargs (*from_p) == 3)
4746 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4747 CALL_EXPR_ARG (*from_p, 0),
4748 CALL_EXPR_ARG (*from_p, 1),
4749 CALL_EXPR_ARG (*from_p, 2));
4750 else
4752 call_stmt = gimple_build_call_from_tree (*from_p);
4753 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
4756 notice_special_calls (call_stmt);
4757 if (!gimple_call_noreturn_p (call_stmt))
4758 gimple_call_set_lhs (call_stmt, *to_p);
4759 assign = call_stmt;
4761 else
4763 assign = gimple_build_assign (*to_p, *from_p);
4764 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4767 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4769 /* We should have got an SSA name from the start. */
4770 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4773 gimplify_seq_add_stmt (pre_p, assign);
4774 gsi = gsi_last (*pre_p);
4775 maybe_fold_stmt (&gsi);
4777 if (want_value)
4779 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4780 return GS_OK;
4782 else
4783 *expr_p = NULL;
4785 return GS_ALL_DONE;
4788 /* Gimplify a comparison between two variable-sized objects. Do this
4789 with a call to BUILT_IN_MEMCMP. */
4791 static enum gimplify_status
4792 gimplify_variable_sized_compare (tree *expr_p)
4794 location_t loc = EXPR_LOCATION (*expr_p);
4795 tree op0 = TREE_OPERAND (*expr_p, 0);
4796 tree op1 = TREE_OPERAND (*expr_p, 1);
4797 tree t, arg, dest, src, expr;
4799 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4800 arg = unshare_expr (arg);
4801 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4802 src = build_fold_addr_expr_loc (loc, op1);
4803 dest = build_fold_addr_expr_loc (loc, op0);
4804 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4805 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4807 expr
4808 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4809 SET_EXPR_LOCATION (expr, loc);
4810 *expr_p = expr;
4812 return GS_OK;
4815 /* Gimplify a comparison between two aggregate objects of integral scalar
4816 mode as a comparison between the bitwise equivalent scalar values. */
4818 static enum gimplify_status
4819 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4821 location_t loc = EXPR_LOCATION (*expr_p);
4822 tree op0 = TREE_OPERAND (*expr_p, 0);
4823 tree op1 = TREE_OPERAND (*expr_p, 1);
4825 tree type = TREE_TYPE (op0);
4826 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4828 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4829 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4831 *expr_p
4832 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4834 return GS_OK;
4837 /* Gimplify an expression sequence. This function gimplifies each
4838 expression and rewrites the original expression with the last
4839 expression of the sequence in GIMPLE form.
4841 PRE_P points to the list where the side effects for all the
4842 expressions in the sequence will be emitted.
4844 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4846 static enum gimplify_status
4847 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4849 tree t = *expr_p;
4853 tree *sub_p = &TREE_OPERAND (t, 0);
4855 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4856 gimplify_compound_expr (sub_p, pre_p, false);
4857 else
4858 gimplify_stmt (sub_p, pre_p);
4860 t = TREE_OPERAND (t, 1);
4862 while (TREE_CODE (t) == COMPOUND_EXPR);
4864 *expr_p = t;
4865 if (want_value)
4866 return GS_OK;
4867 else
4869 gimplify_stmt (expr_p, pre_p);
4870 return GS_ALL_DONE;
4874 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4875 gimplify. After gimplification, EXPR_P will point to a new temporary
4876 that holds the original value of the SAVE_EXPR node.
4878 PRE_P points to the list where side effects that must happen before
4879 *EXPR_P should be stored. */
4881 static enum gimplify_status
4882 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4884 enum gimplify_status ret = GS_ALL_DONE;
4885 tree val;
4887 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4888 val = TREE_OPERAND (*expr_p, 0);
4890 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4891 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4893 /* The operand may be a void-valued expression such as SAVE_EXPRs
4894 generated by the Java frontend for class initialization. It is
4895 being executed only for its side-effects. */
4896 if (TREE_TYPE (val) == void_type_node)
4898 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4899 is_gimple_stmt, fb_none);
4900 val = NULL;
4902 else
4903 val = get_initialized_tmp_var (val, pre_p, post_p);
4905 TREE_OPERAND (*expr_p, 0) = val;
4906 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4909 *expr_p = val;
4911 return ret;
4914 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4916 unary_expr
4917 : ...
4918 | '&' varname
4921 PRE_P points to the list where side effects that must happen before
4922 *EXPR_P should be stored.
4924 POST_P points to the list where side effects that must happen after
4925 *EXPR_P should be stored. */
4927 static enum gimplify_status
4928 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4930 tree expr = *expr_p;
4931 tree op0 = TREE_OPERAND (expr, 0);
4932 enum gimplify_status ret;
4933 location_t loc = EXPR_LOCATION (*expr_p);
4935 switch (TREE_CODE (op0))
4937 case INDIRECT_REF:
4938 do_indirect_ref:
4939 /* Check if we are dealing with an expression of the form '&*ptr'.
4940 While the front end folds away '&*ptr' into 'ptr', these
4941 expressions may be generated internally by the compiler (e.g.,
4942 builtins like __builtin_va_end). */
4943 /* Caution: the silent array decomposition semantics we allow for
4944 ADDR_EXPR means we can't always discard the pair. */
4945 /* Gimplification of the ADDR_EXPR operand may drop
4946 cv-qualification conversions, so make sure we add them if
4947 needed. */
4949 tree op00 = TREE_OPERAND (op0, 0);
4950 tree t_expr = TREE_TYPE (expr);
4951 tree t_op00 = TREE_TYPE (op00);
4953 if (!useless_type_conversion_p (t_expr, t_op00))
4954 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4955 *expr_p = op00;
4956 ret = GS_OK;
4958 break;
4960 case VIEW_CONVERT_EXPR:
4961 /* Take the address of our operand and then convert it to the type of
4962 this ADDR_EXPR.
4964 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4965 all clear. The impact of this transformation is even less clear. */
4967 /* If the operand is a useless conversion, look through it. Doing so
4968 guarantees that the ADDR_EXPR and its operand will remain of the
4969 same type. */
4970 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4971 op0 = TREE_OPERAND (op0, 0);
4973 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4974 build_fold_addr_expr_loc (loc,
4975 TREE_OPERAND (op0, 0)));
4976 ret = GS_OK;
4977 break;
4979 case MEM_REF:
4980 if (integer_zerop (TREE_OPERAND (op0, 1)))
4981 goto do_indirect_ref;
4983 /* ... fall through ... */
4985 default:
4986 /* If we see a call to a declared builtin or see its address
4987 being taken (we can unify those cases here) then we can mark
4988 the builtin for implicit generation by GCC. */
4989 if (TREE_CODE (op0) == FUNCTION_DECL
4990 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
4991 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
4992 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
4994 /* We use fb_either here because the C frontend sometimes takes
4995 the address of a call that returns a struct; see
4996 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4997 the implied temporary explicit. */
4999 /* Make the operand addressable. */
5000 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5001 is_gimple_addressable, fb_either);
5002 if (ret == GS_ERROR)
5003 break;
5005 /* Then mark it. Beware that it may not be possible to do so directly
5006 if a temporary has been created by the gimplification. */
5007 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5009 op0 = TREE_OPERAND (expr, 0);
5011 /* For various reasons, the gimplification of the expression
5012 may have made a new INDIRECT_REF. */
5013 if (TREE_CODE (op0) == INDIRECT_REF)
5014 goto do_indirect_ref;
5016 mark_addressable (TREE_OPERAND (expr, 0));
5018 /* The FEs may end up building ADDR_EXPRs early on a decl with
5019 an incomplete type. Re-build ADDR_EXPRs in canonical form
5020 here. */
5021 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5022 *expr_p = build_fold_addr_expr (op0);
5024 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5025 recompute_tree_invariant_for_addr_expr (*expr_p);
5027 /* If we re-built the ADDR_EXPR add a conversion to the original type
5028 if required. */
5029 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5030 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5032 break;
5035 return ret;
5038 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5039 value; output operands should be a gimple lvalue. */
5041 static enum gimplify_status
5042 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5044 tree expr;
5045 int noutputs;
5046 const char **oconstraints;
5047 int i;
5048 tree link;
5049 const char *constraint;
5050 bool allows_mem, allows_reg, is_inout;
5051 enum gimplify_status ret, tret;
5052 gasm *stmt;
5053 vec<tree, va_gc> *inputs;
5054 vec<tree, va_gc> *outputs;
5055 vec<tree, va_gc> *clobbers;
5056 vec<tree, va_gc> *labels;
5057 tree link_next;
5059 expr = *expr_p;
5060 noutputs = list_length (ASM_OUTPUTS (expr));
5061 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5063 inputs = NULL;
5064 outputs = NULL;
5065 clobbers = NULL;
5066 labels = NULL;
5068 ret = GS_ALL_DONE;
5069 link_next = NULL_TREE;
5070 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5072 bool ok;
5073 size_t constraint_len;
5075 link_next = TREE_CHAIN (link);
5077 oconstraints[i]
5078 = constraint
5079 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5080 constraint_len = strlen (constraint);
5081 if (constraint_len == 0)
5082 continue;
5084 ok = parse_output_constraint (&constraint, i, 0, 0,
5085 &allows_mem, &allows_reg, &is_inout);
5086 if (!ok)
5088 ret = GS_ERROR;
5089 is_inout = false;
5092 if (!allows_reg && allows_mem)
5093 mark_addressable (TREE_VALUE (link));
5095 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5096 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5097 fb_lvalue | fb_mayfail);
5098 if (tret == GS_ERROR)
5100 error ("invalid lvalue in asm output %d", i);
5101 ret = tret;
5104 vec_safe_push (outputs, link);
5105 TREE_CHAIN (link) = NULL_TREE;
5107 if (is_inout)
5109 /* An input/output operand. To give the optimizers more
5110 flexibility, split it into separate input and output
5111 operands. */
5112 tree input;
5113 char buf[10];
5115 /* Turn the in/out constraint into an output constraint. */
5116 char *p = xstrdup (constraint);
5117 p[0] = '=';
5118 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5120 /* And add a matching input constraint. */
5121 if (allows_reg)
5123 sprintf (buf, "%d", i);
5125 /* If there are multiple alternatives in the constraint,
5126 handle each of them individually. Those that allow register
5127 will be replaced with operand number, the others will stay
5128 unchanged. */
5129 if (strchr (p, ',') != NULL)
5131 size_t len = 0, buflen = strlen (buf);
5132 char *beg, *end, *str, *dst;
5134 for (beg = p + 1;;)
5136 end = strchr (beg, ',');
5137 if (end == NULL)
5138 end = strchr (beg, '\0');
5139 if ((size_t) (end - beg) < buflen)
5140 len += buflen + 1;
5141 else
5142 len += end - beg + 1;
5143 if (*end)
5144 beg = end + 1;
5145 else
5146 break;
5149 str = (char *) alloca (len);
5150 for (beg = p + 1, dst = str;;)
5152 const char *tem;
5153 bool mem_p, reg_p, inout_p;
5155 end = strchr (beg, ',');
5156 if (end)
5157 *end = '\0';
5158 beg[-1] = '=';
5159 tem = beg - 1;
5160 parse_output_constraint (&tem, i, 0, 0,
5161 &mem_p, &reg_p, &inout_p);
5162 if (dst != str)
5163 *dst++ = ',';
5164 if (reg_p)
5166 memcpy (dst, buf, buflen);
5167 dst += buflen;
5169 else
5171 if (end)
5172 len = end - beg;
5173 else
5174 len = strlen (beg);
5175 memcpy (dst, beg, len);
5176 dst += len;
5178 if (end)
5179 beg = end + 1;
5180 else
5181 break;
5183 *dst = '\0';
5184 input = build_string (dst - str, str);
5186 else
5187 input = build_string (strlen (buf), buf);
5189 else
5190 input = build_string (constraint_len - 1, constraint + 1);
5192 free (p);
5194 input = build_tree_list (build_tree_list (NULL_TREE, input),
5195 unshare_expr (TREE_VALUE (link)));
5196 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5200 link_next = NULL_TREE;
5201 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5203 link_next = TREE_CHAIN (link);
5204 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5205 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5206 oconstraints, &allows_mem, &allows_reg);
5208 /* If we can't make copies, we can only accept memory. */
5209 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5211 if (allows_mem)
5212 allows_reg = 0;
5213 else
5215 error ("impossible constraint in %<asm%>");
5216 error ("non-memory input %d must stay in memory", i);
5217 return GS_ERROR;
5221 /* If the operand is a memory input, it should be an lvalue. */
5222 if (!allows_reg && allows_mem)
5224 tree inputv = TREE_VALUE (link);
5225 STRIP_NOPS (inputv);
5226 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5227 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5228 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5229 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
5230 || TREE_CODE (inputv) == MODIFY_EXPR)
5231 TREE_VALUE (link) = error_mark_node;
5232 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5233 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5234 mark_addressable (TREE_VALUE (link));
5235 if (tret == GS_ERROR)
5237 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5238 input_location = EXPR_LOCATION (TREE_VALUE (link));
5239 error ("memory input %d is not directly addressable", i);
5240 ret = tret;
5243 else
5245 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5246 is_gimple_asm_val, fb_rvalue);
5247 if (tret == GS_ERROR)
5248 ret = tret;
5251 TREE_CHAIN (link) = NULL_TREE;
5252 vec_safe_push (inputs, link);
5255 link_next = NULL_TREE;
5256 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5258 link_next = TREE_CHAIN (link);
5259 TREE_CHAIN (link) = NULL_TREE;
5260 vec_safe_push (clobbers, link);
5263 link_next = NULL_TREE;
5264 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5266 link_next = TREE_CHAIN (link);
5267 TREE_CHAIN (link) = NULL_TREE;
5268 vec_safe_push (labels, link);
5271 /* Do not add ASMs with errors to the gimple IL stream. */
5272 if (ret != GS_ERROR)
5274 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5275 inputs, outputs, clobbers, labels);
5277 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
5278 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5280 gimplify_seq_add_stmt (pre_p, stmt);
5283 return ret;
5286 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5287 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5288 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5289 return to this function.
5291 FIXME should we complexify the prequeue handling instead? Or use flags
5292 for all the cleanups and let the optimizer tighten them up? The current
5293 code seems pretty fragile; it will break on a cleanup within any
5294 non-conditional nesting. But any such nesting would be broken, anyway;
5295 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5296 and continues out of it. We can do that at the RTL level, though, so
5297 having an optimizer to tighten up try/finally regions would be a Good
5298 Thing. */
5300 static enum gimplify_status
5301 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5303 gimple_stmt_iterator iter;
5304 gimple_seq body_sequence = NULL;
5306 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5308 /* We only care about the number of conditions between the innermost
5309 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5310 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5311 int old_conds = gimplify_ctxp->conditions;
5312 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5313 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5314 gimplify_ctxp->conditions = 0;
5315 gimplify_ctxp->conditional_cleanups = NULL;
5316 gimplify_ctxp->in_cleanup_point_expr = true;
5318 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5320 gimplify_ctxp->conditions = old_conds;
5321 gimplify_ctxp->conditional_cleanups = old_cleanups;
5322 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5324 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5326 gimple *wce = gsi_stmt (iter);
5328 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5330 if (gsi_one_before_end_p (iter))
5332 /* Note that gsi_insert_seq_before and gsi_remove do not
5333 scan operands, unlike some other sequence mutators. */
5334 if (!gimple_wce_cleanup_eh_only (wce))
5335 gsi_insert_seq_before_without_update (&iter,
5336 gimple_wce_cleanup (wce),
5337 GSI_SAME_STMT);
5338 gsi_remove (&iter, true);
5339 break;
5341 else
5343 gtry *gtry;
5344 gimple_seq seq;
5345 enum gimple_try_flags kind;
5347 if (gimple_wce_cleanup_eh_only (wce))
5348 kind = GIMPLE_TRY_CATCH;
5349 else
5350 kind = GIMPLE_TRY_FINALLY;
5351 seq = gsi_split_seq_after (iter);
5353 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5354 /* Do not use gsi_replace here, as it may scan operands.
5355 We want to do a simple structural modification only. */
5356 gsi_set_stmt (&iter, gtry);
5357 iter = gsi_start (gtry->eval);
5360 else
5361 gsi_next (&iter);
5364 gimplify_seq_add_seq (pre_p, body_sequence);
5365 if (temp)
5367 *expr_p = temp;
5368 return GS_OK;
5370 else
5372 *expr_p = NULL;
5373 return GS_ALL_DONE;
5377 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5378 is the cleanup action required. EH_ONLY is true if the cleanup should
5379 only be executed if an exception is thrown, not on normal exit. */
5381 static void
5382 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5384 gimple *wce;
5385 gimple_seq cleanup_stmts = NULL;
5387 /* Errors can result in improperly nested cleanups. Which results in
5388 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5389 if (seen_error ())
5390 return;
5392 if (gimple_conditional_context ())
5394 /* If we're in a conditional context, this is more complex. We only
5395 want to run the cleanup if we actually ran the initialization that
5396 necessitates it, but we want to run it after the end of the
5397 conditional context. So we wrap the try/finally around the
5398 condition and use a flag to determine whether or not to actually
5399 run the destructor. Thus
5401 test ? f(A()) : 0
5403 becomes (approximately)
5405 flag = 0;
5406 try {
5407 if (test) { A::A(temp); flag = 1; val = f(temp); }
5408 else { val = 0; }
5409 } finally {
5410 if (flag) A::~A(temp);
5414 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5415 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
5416 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
5418 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5419 gimplify_stmt (&cleanup, &cleanup_stmts);
5420 wce = gimple_build_wce (cleanup_stmts);
5422 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5423 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5424 gimplify_seq_add_stmt (pre_p, ftrue);
5426 /* Because of this manipulation, and the EH edges that jump
5427 threading cannot redirect, the temporary (VAR) will appear
5428 to be used uninitialized. Don't warn. */
5429 TREE_NO_WARNING (var) = 1;
5431 else
5433 gimplify_stmt (&cleanup, &cleanup_stmts);
5434 wce = gimple_build_wce (cleanup_stmts);
5435 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5436 gimplify_seq_add_stmt (pre_p, wce);
5440 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5442 static enum gimplify_status
5443 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5445 tree targ = *expr_p;
5446 tree temp = TARGET_EXPR_SLOT (targ);
5447 tree init = TARGET_EXPR_INITIAL (targ);
5448 enum gimplify_status ret;
5450 if (init)
5452 tree cleanup = NULL_TREE;
5454 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5455 to the temps list. Handle also variable length TARGET_EXPRs. */
5456 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5458 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5459 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5460 gimplify_vla_decl (temp, pre_p);
5462 else
5463 gimple_add_tmp_var (temp);
5465 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5466 expression is supposed to initialize the slot. */
5467 if (VOID_TYPE_P (TREE_TYPE (init)))
5468 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5469 else
5471 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5472 init = init_expr;
5473 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5474 init = NULL;
5475 ggc_free (init_expr);
5477 if (ret == GS_ERROR)
5479 /* PR c++/28266 Make sure this is expanded only once. */
5480 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5481 return GS_ERROR;
5483 if (init)
5484 gimplify_and_add (init, pre_p);
5486 /* If needed, push the cleanup for the temp. */
5487 if (TARGET_EXPR_CLEANUP (targ))
5489 if (CLEANUP_EH_ONLY (targ))
5490 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5491 CLEANUP_EH_ONLY (targ), pre_p);
5492 else
5493 cleanup = TARGET_EXPR_CLEANUP (targ);
5496 /* Add a clobber for the temporary going out of scope, like
5497 gimplify_bind_expr. */
5498 if (gimplify_ctxp->in_cleanup_point_expr
5499 && needs_to_live_in_memory (temp)
5500 && flag_stack_reuse == SR_ALL)
5502 tree clobber = build_constructor (TREE_TYPE (temp),
5503 NULL);
5504 TREE_THIS_VOLATILE (clobber) = true;
5505 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5506 if (cleanup)
5507 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5508 clobber);
5509 else
5510 cleanup = clobber;
5513 if (cleanup)
5514 gimple_push_cleanup (temp, cleanup, false, pre_p);
5516 /* Only expand this once. */
5517 TREE_OPERAND (targ, 3) = init;
5518 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5520 else
5521 /* We should have expanded this before. */
5522 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5524 *expr_p = temp;
5525 return GS_OK;
5528 /* Gimplification of expression trees. */
5530 /* Gimplify an expression which appears at statement context. The
5531 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5532 NULL, a new sequence is allocated.
5534 Return true if we actually added a statement to the queue. */
5536 bool
5537 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5539 gimple_seq_node last;
5541 last = gimple_seq_last (*seq_p);
5542 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5543 return last != gimple_seq_last (*seq_p);
5546 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5547 to CTX. If entries already exist, force them to be some flavor of private.
5548 If there is no enclosing parallel, do nothing. */
5550 void
5551 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5553 splay_tree_node n;
5555 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
5556 return;
5560 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5561 if (n != NULL)
5563 if (n->value & GOVD_SHARED)
5564 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5565 else if (n->value & GOVD_MAP)
5566 n->value |= GOVD_MAP_TO_ONLY;
5567 else
5568 return;
5570 else if ((ctx->region_type & ORT_TARGET) != 0)
5572 if (ctx->target_map_scalars_firstprivate)
5573 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5574 else
5575 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5577 else if (ctx->region_type != ORT_WORKSHARE
5578 && ctx->region_type != ORT_SIMD
5579 && ctx->region_type != ORT_TARGET_DATA)
5580 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5582 ctx = ctx->outer_context;
5584 while (ctx);
5587 /* Similarly for each of the type sizes of TYPE. */
5589 static void
5590 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5592 if (type == NULL || type == error_mark_node)
5593 return;
5594 type = TYPE_MAIN_VARIANT (type);
5596 if (ctx->privatized_types->add (type))
5597 return;
5599 switch (TREE_CODE (type))
5601 case INTEGER_TYPE:
5602 case ENUMERAL_TYPE:
5603 case BOOLEAN_TYPE:
5604 case REAL_TYPE:
5605 case FIXED_POINT_TYPE:
5606 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5607 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5608 break;
5610 case ARRAY_TYPE:
5611 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5612 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5613 break;
5615 case RECORD_TYPE:
5616 case UNION_TYPE:
5617 case QUAL_UNION_TYPE:
5619 tree field;
5620 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5621 if (TREE_CODE (field) == FIELD_DECL)
5623 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5624 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5627 break;
5629 case POINTER_TYPE:
5630 case REFERENCE_TYPE:
5631 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5632 break;
5634 default:
5635 break;
5638 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5639 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5640 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5643 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
5645 static void
5646 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5648 splay_tree_node n;
5649 unsigned int nflags;
5650 tree t;
5652 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
5653 return;
5655 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5656 there are constructors involved somewhere. */
5657 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5658 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5659 flags |= GOVD_SEEN;
5661 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5662 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
5664 /* We shouldn't be re-adding the decl with the same data
5665 sharing class. */
5666 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5667 /* The only combination of data sharing classes we should see is
5668 FIRSTPRIVATE and LASTPRIVATE. */
5669 nflags = n->value | flags;
5670 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5671 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5672 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5673 n->value = nflags;
5674 return;
5677 /* When adding a variable-sized variable, we have to handle all sorts
5678 of additional bits of data: the pointer replacement variable, and
5679 the parameters of the type. */
5680 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5682 /* Add the pointer replacement variable as PRIVATE if the variable
5683 replacement is private, else FIRSTPRIVATE since we'll need the
5684 address of the original variable either for SHARED, or for the
5685 copy into or out of the context. */
5686 if (!(flags & GOVD_LOCAL))
5688 if (flags & GOVD_MAP)
5689 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5690 else if (flags & GOVD_PRIVATE)
5691 nflags = GOVD_PRIVATE;
5692 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
5693 && (flags & GOVD_FIRSTPRIVATE))
5694 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
5695 else
5696 nflags = GOVD_FIRSTPRIVATE;
5697 nflags |= flags & GOVD_SEEN;
5698 t = DECL_VALUE_EXPR (decl);
5699 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5700 t = TREE_OPERAND (t, 0);
5701 gcc_assert (DECL_P (t));
5702 omp_add_variable (ctx, t, nflags);
5705 /* Add all of the variable and type parameters (which should have
5706 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5707 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5708 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5709 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5711 /* The variable-sized variable itself is never SHARED, only some form
5712 of PRIVATE. The sharing would take place via the pointer variable
5713 which we remapped above. */
5714 if (flags & GOVD_SHARED)
5715 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5716 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5718 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5719 alloca statement we generate for the variable, so make sure it
5720 is available. This isn't automatically needed for the SHARED
5721 case, since we won't be allocating local storage then.
5722 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5723 in this case omp_notice_variable will be called later
5724 on when it is gimplified. */
5725 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5726 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5727 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5729 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5730 && lang_hooks.decls.omp_privatize_by_reference (decl))
5732 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5734 /* Similar to the direct variable sized case above, we'll need the
5735 size of references being privatized. */
5736 if ((flags & GOVD_SHARED) == 0)
5738 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5739 if (DECL_P (t))
5740 omp_notice_variable (ctx, t, true);
5744 if (n != NULL)
5745 n->value |= flags;
5746 else
5747 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5750 /* Notice a threadprivate variable DECL used in OMP context CTX.
5751 This just prints out diagnostics about threadprivate variable uses
5752 in untied tasks. If DECL2 is non-NULL, prevent this warning
5753 on that variable. */
5755 static bool
5756 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5757 tree decl2)
5759 splay_tree_node n;
5760 struct gimplify_omp_ctx *octx;
5762 for (octx = ctx; octx; octx = octx->outer_context)
5763 if ((octx->region_type & ORT_TARGET) != 0)
5765 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5766 if (n == NULL)
5768 error ("threadprivate variable %qE used in target region",
5769 DECL_NAME (decl));
5770 error_at (octx->location, "enclosing target region");
5771 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5773 if (decl2)
5774 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5777 if (ctx->region_type != ORT_UNTIED_TASK)
5778 return false;
5779 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5780 if (n == NULL)
5782 error ("threadprivate variable %qE used in untied task",
5783 DECL_NAME (decl));
5784 error_at (ctx->location, "enclosing task");
5785 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5787 if (decl2)
5788 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5789 return false;
5792 /* Determine outer default flags for DECL mentioned in an OMP region
5793 but not declared in an enclosing clause.
5795 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5796 remapped firstprivate instead of shared. To some extent this is
5797 addressed in omp_firstprivatize_type_sizes, but not
5798 effectively. */
5800 static unsigned
5801 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
5802 bool in_code, unsigned flags)
5804 enum omp_clause_default_kind default_kind = ctx->default_kind;
5805 enum omp_clause_default_kind kind;
5807 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5808 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5809 default_kind = kind;
5811 switch (default_kind)
5813 case OMP_CLAUSE_DEFAULT_NONE:
5815 const char *rtype;
5817 if (ctx->region_type & ORT_PARALLEL)
5818 rtype = "parallel";
5819 else if (ctx->region_type & ORT_TASK)
5820 rtype = "task";
5821 else if (ctx->region_type & ORT_TEAMS)
5822 rtype = "teams";
5823 else
5824 gcc_unreachable ();
5826 error ("%qE not specified in enclosing %s",
5827 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
5828 error_at (ctx->location, "enclosing %s", rtype);
5830 /* FALLTHRU */
5831 case OMP_CLAUSE_DEFAULT_SHARED:
5832 flags |= GOVD_SHARED;
5833 break;
5834 case OMP_CLAUSE_DEFAULT_PRIVATE:
5835 flags |= GOVD_PRIVATE;
5836 break;
5837 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5838 flags |= GOVD_FIRSTPRIVATE;
5839 break;
5840 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5841 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5842 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5843 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
5845 omp_notice_variable (octx, decl, in_code);
5846 for (; octx; octx = octx->outer_context)
5848 splay_tree_node n2;
5850 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5851 continue;
5852 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5853 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5855 flags |= GOVD_FIRSTPRIVATE;
5856 goto found_outer;
5858 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5860 flags |= GOVD_SHARED;
5861 goto found_outer;
5866 if (TREE_CODE (decl) == PARM_DECL
5867 || (!is_global_var (decl)
5868 && DECL_CONTEXT (decl) == current_function_decl))
5869 flags |= GOVD_FIRSTPRIVATE;
5870 else
5871 flags |= GOVD_SHARED;
5872 found_outer:
5873 break;
5875 default:
5876 gcc_unreachable ();
5879 return flags;
5882 /* Record the fact that DECL was used within the OMP context CTX.
5883 IN_CODE is true when real code uses DECL, and false when we should
5884 merely emit default(none) errors. Return true if DECL is going to
5885 be remapped and thus DECL shouldn't be gimplified into its
5886 DECL_VALUE_EXPR (if any). */
5888 static bool
5889 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5891 splay_tree_node n;
5892 unsigned flags = in_code ? GOVD_SEEN : 0;
5893 bool ret = false, shared;
5895 if (error_operand_p (decl))
5896 return false;
5898 if (ctx->region_type == ORT_NONE)
5899 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
5901 /* Threadprivate variables are predetermined. */
5902 if (is_global_var (decl))
5904 if (DECL_THREAD_LOCAL_P (decl))
5905 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5907 if (DECL_HAS_VALUE_EXPR_P (decl))
5909 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5911 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5912 return omp_notice_threadprivate_variable (ctx, decl, value);
5916 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5917 if ((ctx->region_type & ORT_TARGET) != 0)
5919 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5920 if (n == NULL)
5922 unsigned nflags = flags;
5923 if (ctx->target_map_pointers_as_0len_arrays
5924 || ctx->target_map_scalars_firstprivate)
5926 bool is_declare_target = false;
5927 bool is_scalar = false;
5928 if (is_global_var (decl)
5929 && varpool_node::get_create (decl)->offloadable)
5931 struct gimplify_omp_ctx *octx;
5932 for (octx = ctx->outer_context;
5933 octx; octx = octx->outer_context)
5935 n = splay_tree_lookup (octx->variables,
5936 (splay_tree_key)decl);
5937 if (n
5938 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
5939 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
5940 break;
5942 is_declare_target = octx == NULL;
5944 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
5946 tree type = TREE_TYPE (decl);
5947 if (TREE_CODE (type) == REFERENCE_TYPE)
5948 type = TREE_TYPE (type);
5949 if (TREE_CODE (type) == COMPLEX_TYPE)
5950 type = TREE_TYPE (type);
5951 if (INTEGRAL_TYPE_P (type)
5952 || SCALAR_FLOAT_TYPE_P (type)
5953 || TREE_CODE (type) == POINTER_TYPE)
5954 is_scalar = true;
5956 if (is_declare_target)
5958 else if (ctx->target_map_pointers_as_0len_arrays
5959 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
5960 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
5961 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
5962 == POINTER_TYPE)))
5963 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
5964 else if (is_scalar)
5965 nflags |= GOVD_FIRSTPRIVATE;
5967 if (nflags == flags
5968 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5970 error ("%qD referenced in target region does not have "
5971 "a mappable type", decl);
5972 nflags |= GOVD_MAP | GOVD_EXPLICIT;
5974 else if (nflags == flags)
5975 nflags |= GOVD_MAP;
5976 omp_add_variable (ctx, decl, nflags);
5978 else
5980 /* If nothing changed, there's nothing left to do. */
5981 if ((n->value & flags) == flags)
5982 return ret;
5983 n->value |= flags;
5985 goto do_outer;
5988 if (n == NULL)
5990 if (ctx->region_type == ORT_WORKSHARE
5991 || ctx->region_type == ORT_SIMD
5992 || ctx->region_type == ORT_TARGET_DATA)
5993 goto do_outer;
5995 flags = omp_default_clause (ctx, decl, in_code, flags);
5997 if ((flags & GOVD_PRIVATE)
5998 && lang_hooks.decls.omp_private_outer_ref (decl))
5999 flags |= GOVD_PRIVATE_OUTER_REF;
6001 omp_add_variable (ctx, decl, flags);
6003 shared = (flags & GOVD_SHARED) != 0;
6004 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6005 goto do_outer;
6008 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6009 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6010 && DECL_SIZE (decl)
6011 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6013 splay_tree_node n2;
6014 tree t = DECL_VALUE_EXPR (decl);
6015 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6016 t = TREE_OPERAND (t, 0);
6017 gcc_assert (DECL_P (t));
6018 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6019 n2->value |= GOVD_SEEN;
6022 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6023 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6025 /* If nothing changed, there's nothing left to do. */
6026 if ((n->value & flags) == flags)
6027 return ret;
6028 flags |= n->value;
6029 n->value = flags;
6031 do_outer:
6032 /* If the variable is private in the current context, then we don't
6033 need to propagate anything to an outer context. */
6034 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6035 return ret;
6036 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6037 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6038 return ret;
6039 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6040 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6041 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6042 return ret;
6043 if (ctx->outer_context
6044 && omp_notice_variable (ctx->outer_context, decl, in_code))
6045 return true;
6046 return ret;
6049 /* Verify that DECL is private within CTX. If there's specific information
6050 to the contrary in the innermost scope, generate an error. */
6052 static bool
6053 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
6055 splay_tree_node n;
6057 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6058 if (n != NULL)
6060 if (n->value & GOVD_SHARED)
6062 if (ctx == gimplify_omp_ctxp)
6064 if (simd)
6065 error ("iteration variable %qE is predetermined linear",
6066 DECL_NAME (decl));
6067 else
6068 error ("iteration variable %qE should be private",
6069 DECL_NAME (decl));
6070 n->value = GOVD_PRIVATE;
6071 return true;
6073 else
6074 return false;
6076 else if ((n->value & GOVD_EXPLICIT) != 0
6077 && (ctx == gimplify_omp_ctxp
6078 || (ctx->region_type == ORT_COMBINED_PARALLEL
6079 && gimplify_omp_ctxp->outer_context == ctx)))
6081 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6082 error ("iteration variable %qE should not be firstprivate",
6083 DECL_NAME (decl));
6084 else if ((n->value & GOVD_REDUCTION) != 0)
6085 error ("iteration variable %qE should not be reduction",
6086 DECL_NAME (decl));
6087 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
6088 error ("iteration variable %qE should not be lastprivate",
6089 DECL_NAME (decl));
6090 else if (simd && (n->value & GOVD_PRIVATE) != 0)
6091 error ("iteration variable %qE should not be private",
6092 DECL_NAME (decl));
6093 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
6094 error ("iteration variable %qE is predetermined linear",
6095 DECL_NAME (decl));
6097 return (ctx == gimplify_omp_ctxp
6098 || (ctx->region_type == ORT_COMBINED_PARALLEL
6099 && gimplify_omp_ctxp->outer_context == ctx));
6102 if (ctx->region_type != ORT_WORKSHARE
6103 && ctx->region_type != ORT_SIMD)
6104 return false;
6105 else if (ctx->outer_context)
6106 return omp_is_private (ctx->outer_context, decl, simd);
6107 return false;
6110 /* Return true if DECL is private within a parallel region
6111 that binds to the current construct's context or in parallel
6112 region's REDUCTION clause. */
6114 static bool
6115 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
6117 splay_tree_node n;
6121 ctx = ctx->outer_context;
6122 if (ctx == NULL)
6124 if (is_global_var (decl))
6125 return false;
6127 /* References might be private, but might be shared too,
6128 when checking for copyprivate, assume they might be
6129 private, otherwise assume they might be shared. */
6130 if (copyprivate)
6131 return true;
6133 if (lang_hooks.decls.omp_privatize_by_reference (decl))
6134 return false;
6136 /* Treat C++ privatized non-static data members outside
6137 of the privatization the same. */
6138 if (omp_member_access_dummy_var (decl))
6139 return false;
6141 return true;
6144 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
6145 continue;
6147 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6148 if (n != NULL)
6150 if ((n->value & GOVD_LOCAL) != 0
6151 && omp_member_access_dummy_var (decl))
6152 return false;
6153 return (n->value & GOVD_SHARED) == 0;
6156 while (ctx->region_type == ORT_WORKSHARE
6157 || ctx->region_type == ORT_SIMD);
6158 return false;
6161 /* Return true if the CTX is combined with distribute and thus
6162 lastprivate can't be supported. */
6164 static bool
6165 omp_no_lastprivate (struct gimplify_omp_ctx *ctx)
6169 if (ctx->outer_context == NULL)
6170 return false;
6171 ctx = ctx->outer_context;
6172 switch (ctx->region_type)
6174 case ORT_WORKSHARE:
6175 if (!ctx->combined_loop)
6176 return false;
6177 if (ctx->distribute)
6178 return true;
6179 break;
6180 case ORT_COMBINED_PARALLEL:
6181 break;
6182 case ORT_COMBINED_TEAMS:
6183 return true;
6184 default:
6185 return false;
6188 while (1);
6191 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
6193 static tree
6194 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
6196 tree t = *tp;
6198 /* If this node has been visited, unmark it and keep looking. */
6199 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
6200 return t;
6202 if (IS_TYPE_OR_DECL_P (t))
6203 *walk_subtrees = 0;
6204 return NULL_TREE;
6207 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
6208 and previous omp contexts. */
6210 static void
6211 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6212 enum omp_region_type region_type,
6213 enum tree_code code)
6215 struct gimplify_omp_ctx *ctx, *outer_ctx;
6216 tree c;
6217 hash_map<tree, tree> *struct_map_to_clause = NULL;
6218 tree *orig_list_p = list_p;
6220 ctx = new_omp_context (region_type);
6221 outer_ctx = ctx->outer_context;
6222 if (code == OMP_TARGET && !lang_GNU_Fortran ())
6224 ctx->target_map_pointers_as_0len_arrays = true;
6225 /* FIXME: For Fortran we want to set this too, when
6226 the Fortran FE is updated to OpenMP 4.5. */
6227 ctx->target_map_scalars_firstprivate = true;
6229 if (!lang_GNU_Fortran ())
6230 switch (code)
6232 case OMP_TARGET:
6233 case OMP_TARGET_DATA:
6234 case OMP_TARGET_ENTER_DATA:
6235 case OMP_TARGET_EXIT_DATA:
6236 ctx->target_firstprivatize_array_bases = true;
6237 default:
6238 break;
6241 while ((c = *list_p) != NULL)
6243 bool remove = false;
6244 bool notice_outer = true;
6245 const char *check_non_private = NULL;
6246 unsigned int flags;
6247 tree decl;
6249 switch (OMP_CLAUSE_CODE (c))
6251 case OMP_CLAUSE_PRIVATE:
6252 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6253 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6255 flags |= GOVD_PRIVATE_OUTER_REF;
6256 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6258 else
6259 notice_outer = false;
6260 goto do_add;
6261 case OMP_CLAUSE_SHARED:
6262 flags = GOVD_SHARED | GOVD_EXPLICIT;
6263 goto do_add;
6264 case OMP_CLAUSE_FIRSTPRIVATE:
6265 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6266 check_non_private = "firstprivate";
6267 goto do_add;
6268 case OMP_CLAUSE_LASTPRIVATE:
6269 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6270 check_non_private = "lastprivate";
6271 decl = OMP_CLAUSE_DECL (c);
6272 if (omp_no_lastprivate (ctx))
6274 notice_outer = false;
6275 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
6277 else if (error_operand_p (decl))
6278 goto do_add;
6279 else if (outer_ctx
6280 && outer_ctx->region_type == ORT_COMBINED_PARALLEL
6281 && splay_tree_lookup (outer_ctx->variables,
6282 (splay_tree_key) decl) == NULL)
6283 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
6284 else if (outer_ctx
6285 && (outer_ctx->region_type & ORT_TASK) != 0
6286 && outer_ctx->combined_loop
6287 && splay_tree_lookup (outer_ctx->variables,
6288 (splay_tree_key) decl) == NULL)
6289 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
6290 else if (outer_ctx
6291 && outer_ctx->region_type == ORT_WORKSHARE
6292 && outer_ctx->combined_loop
6293 && splay_tree_lookup (outer_ctx->variables,
6294 (splay_tree_key) decl) == NULL
6295 && !omp_check_private (outer_ctx, decl, false))
6297 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
6298 if (outer_ctx->outer_context
6299 && (outer_ctx->outer_context->region_type
6300 == ORT_COMBINED_PARALLEL)
6301 && splay_tree_lookup (outer_ctx->outer_context->variables,
6302 (splay_tree_key) decl) == NULL)
6303 omp_add_variable (outer_ctx->outer_context, decl,
6304 GOVD_SHARED | GOVD_SEEN);
6306 goto do_add;
6307 case OMP_CLAUSE_REDUCTION:
6308 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6309 check_non_private = "reduction";
6310 decl = OMP_CLAUSE_DECL (c);
6311 if (TREE_CODE (decl) == MEM_REF)
6313 tree type = TREE_TYPE (decl);
6314 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
6315 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6317 remove = true;
6318 break;
6320 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
6321 if (DECL_P (v))
6323 omp_firstprivatize_variable (ctx, v);
6324 omp_notice_variable (ctx, v, true);
6326 decl = TREE_OPERAND (decl, 0);
6327 if (TREE_CODE (decl) == ADDR_EXPR
6328 || TREE_CODE (decl) == INDIRECT_REF)
6329 decl = TREE_OPERAND (decl, 0);
6331 goto do_add_decl;
6332 case OMP_CLAUSE_LINEAR:
6333 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6334 is_gimple_val, fb_rvalue) == GS_ERROR)
6336 remove = true;
6337 break;
6339 else
6341 if (code == OMP_SIMD
6342 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6344 struct gimplify_omp_ctx *octx = outer_ctx;
6345 if (octx
6346 && octx->region_type == ORT_WORKSHARE
6347 && octx->combined_loop
6348 && !octx->distribute)
6350 if (octx->outer_context
6351 && (octx->outer_context->region_type
6352 == ORT_COMBINED_PARALLEL))
6353 octx = octx->outer_context->outer_context;
6354 else
6355 octx = octx->outer_context;
6357 if (octx
6358 && octx->region_type == ORT_WORKSHARE
6359 && octx->combined_loop
6360 && octx->distribute
6361 && !lang_GNU_Fortran ())
6363 error_at (OMP_CLAUSE_LOCATION (c),
6364 "%<linear%> clause for variable other than "
6365 "loop iterator specified on construct "
6366 "combined with %<distribute%>");
6367 remove = true;
6368 break;
6371 /* For combined #pragma omp parallel for simd, need to put
6372 lastprivate and perhaps firstprivate too on the
6373 parallel. Similarly for #pragma omp for simd. */
6374 struct gimplify_omp_ctx *octx = outer_ctx;
6375 decl = NULL_TREE;
6376 if (omp_no_lastprivate (ctx))
6377 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6380 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6381 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6382 break;
6383 decl = OMP_CLAUSE_DECL (c);
6384 if (error_operand_p (decl))
6386 decl = NULL_TREE;
6387 break;
6389 flags = GOVD_SEEN;
6390 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6391 flags |= GOVD_FIRSTPRIVATE;
6392 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6393 flags |= GOVD_LASTPRIVATE;
6394 if (octx
6395 && octx->region_type == ORT_WORKSHARE
6396 && octx->combined_loop)
6398 if (octx->outer_context
6399 && (octx->outer_context->region_type
6400 == ORT_COMBINED_PARALLEL
6401 || (octx->outer_context->region_type
6402 == ORT_COMBINED_TEAMS)))
6403 octx = octx->outer_context;
6404 else if (omp_check_private (octx, decl, false))
6405 break;
6407 else if (octx
6408 && (octx->region_type & ORT_TASK) != 0
6409 && octx->combined_loop)
6411 else if (octx
6412 && octx->region_type == ORT_COMBINED_PARALLEL
6413 && ctx->region_type == ORT_WORKSHARE
6414 && octx == outer_ctx)
6415 flags = GOVD_SEEN | GOVD_SHARED;
6416 else if (octx
6417 && octx->region_type == ORT_COMBINED_TARGET)
6418 flags &= ~GOVD_LASTPRIVATE;
6419 else
6420 break;
6421 splay_tree_node on
6422 = splay_tree_lookup (octx->variables,
6423 (splay_tree_key) decl);
6424 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
6426 octx = NULL;
6427 break;
6429 omp_add_variable (octx, decl, flags);
6430 if (octx->outer_context == NULL)
6431 break;
6432 octx = octx->outer_context;
6434 while (1);
6435 if (octx
6436 && decl
6437 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6438 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6439 omp_notice_variable (octx, decl, true);
6441 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6442 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6443 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6445 notice_outer = false;
6446 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
6448 goto do_add;
6450 case OMP_CLAUSE_MAP:
6451 decl = OMP_CLAUSE_DECL (c);
6452 if (error_operand_p (decl))
6453 remove = true;
6454 switch (code)
6456 case OMP_TARGET:
6457 break;
6458 case OMP_TARGET_DATA:
6459 case OMP_TARGET_ENTER_DATA:
6460 case OMP_TARGET_EXIT_DATA:
6461 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6462 /* For target {,enter ,exit }data only the array slice is
6463 mapped, but not the pointer to it. */
6464 remove = true;
6465 break;
6466 default:
6467 break;
6469 if (remove)
6470 break;
6471 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6472 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6473 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6474 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6475 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6477 remove = true;
6478 break;
6480 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
6481 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
6483 OMP_CLAUSE_SIZE (c)
6484 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL);
6485 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
6486 GOVD_FIRSTPRIVATE | GOVD_SEEN);
6488 if (!DECL_P (decl))
6490 tree d = decl, *pd;
6491 if (TREE_CODE (d) == ARRAY_REF)
6493 while (TREE_CODE (d) == ARRAY_REF)
6494 d = TREE_OPERAND (d, 0);
6495 if (TREE_CODE (d) == COMPONENT_REF
6496 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
6497 decl = d;
6499 pd = &OMP_CLAUSE_DECL (c);
6500 if (d == decl
6501 && TREE_CODE (decl) == INDIRECT_REF
6502 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
6503 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
6504 == REFERENCE_TYPE))
6506 pd = &TREE_OPERAND (decl, 0);
6507 decl = TREE_OPERAND (decl, 0);
6509 if (TREE_CODE (decl) == COMPONENT_REF)
6511 while (TREE_CODE (decl) == COMPONENT_REF)
6512 decl = TREE_OPERAND (decl, 0);
6514 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
6515 == GS_ERROR)
6517 remove = true;
6518 break;
6520 if (DECL_P (decl))
6522 if (error_operand_p (decl))
6524 remove = true;
6525 break;
6528 if (TYPE_SIZE_UNIT (TREE_TYPE (decl)) == NULL
6529 || (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (decl)))
6530 != INTEGER_CST))
6532 error_at (OMP_CLAUSE_LOCATION (c),
6533 "mapping field %qE of variable length "
6534 "structure", OMP_CLAUSE_DECL (c));
6535 remove = true;
6536 break;
6539 tree offset;
6540 HOST_WIDE_INT bitsize, bitpos;
6541 machine_mode mode;
6542 int unsignedp, volatilep = 0;
6543 tree base = OMP_CLAUSE_DECL (c);
6544 while (TREE_CODE (base) == ARRAY_REF)
6545 base = TREE_OPERAND (base, 0);
6546 if (TREE_CODE (base) == INDIRECT_REF)
6547 base = TREE_OPERAND (base, 0);
6548 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
6549 &mode, &unsignedp,
6550 &volatilep, false);
6551 gcc_assert (base == decl
6552 && (offset == NULL_TREE
6553 || TREE_CODE (offset) == INTEGER_CST));
6555 splay_tree_node n
6556 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6557 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
6558 == GOMP_MAP_FIRSTPRIVATE_POINTER);
6559 if (n == NULL || (n->value & (ptr ? GOVD_PRIVATE
6560 : GOVD_MAP)) == 0)
6562 if (ptr)
6564 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6565 OMP_CLAUSE_PRIVATE);
6566 OMP_CLAUSE_DECL (c2) = decl;
6567 OMP_CLAUSE_CHAIN (c2) = *orig_list_p;
6568 *orig_list_p = c2;
6569 if (struct_map_to_clause == NULL)
6570 struct_map_to_clause = new hash_map<tree, tree>;
6571 tree *osc;
6572 if (n == NULL || (n->value & GOVD_MAP) == 0)
6573 osc = NULL;
6574 else
6575 osc = struct_map_to_clause->get (decl);
6576 if (osc == NULL)
6577 struct_map_to_clause->put (decl,
6578 tree_cons (NULL_TREE,
6580 NULL_TREE));
6581 else
6582 *osc = tree_cons (*osc, c, NULL_TREE);
6583 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6584 goto do_add_decl;
6586 *list_p = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6587 OMP_CLAUSE_MAP);
6588 OMP_CLAUSE_SET_MAP_KIND (*list_p, GOMP_MAP_STRUCT);
6589 OMP_CLAUSE_DECL (*list_p) = decl;
6590 OMP_CLAUSE_SIZE (*list_p) = size_int (1);
6591 OMP_CLAUSE_CHAIN (*list_p) = c;
6592 if (struct_map_to_clause == NULL)
6593 struct_map_to_clause = new hash_map<tree, tree>;
6594 struct_map_to_clause->put (decl, *list_p);
6595 list_p = &OMP_CLAUSE_CHAIN (*list_p);
6596 flags = GOVD_MAP | GOVD_EXPLICIT;
6597 if (OMP_CLAUSE_MAP_KIND (c) & GOMP_MAP_FLAG_ALWAYS)
6598 flags |= GOVD_SEEN;
6599 goto do_add_decl;
6601 else
6603 tree *osc = struct_map_to_clause->get (decl);
6604 tree *sc = NULL, *pt = NULL;
6605 if (!ptr && TREE_CODE (*osc) == TREE_LIST)
6606 osc = &TREE_PURPOSE (*osc);
6607 if (OMP_CLAUSE_MAP_KIND (c) & GOMP_MAP_FLAG_ALWAYS)
6608 n->value |= GOVD_SEEN;
6609 offset_int o1, o2;
6610 if (offset)
6611 o1 = wi::to_offset (offset);
6612 else
6613 o1 = 0;
6614 if (bitpos)
6615 o1 = o1 + bitpos / BITS_PER_UNIT;
6616 if (ptr)
6617 pt = osc;
6618 else
6619 sc = &OMP_CLAUSE_CHAIN (*osc);
6620 for (; ptr ? (*pt && (sc = &TREE_VALUE (*pt)))
6621 : *sc != c;
6622 ptr ? (pt = &TREE_CHAIN (*pt))
6623 : (sc = &OMP_CLAUSE_CHAIN (*sc)))
6624 if (TREE_CODE (OMP_CLAUSE_DECL (*sc)) != COMPONENT_REF
6625 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
6626 != INDIRECT_REF)
6627 && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != ARRAY_REF)
6628 break;
6629 else
6631 tree offset2;
6632 HOST_WIDE_INT bitsize2, bitpos2;
6633 base = OMP_CLAUSE_DECL (*sc);
6634 if (TREE_CODE (base) == ARRAY_REF)
6636 while (TREE_CODE (base) == ARRAY_REF)
6637 base = TREE_OPERAND (base, 0);
6638 if (TREE_CODE (base) != COMPONENT_REF
6639 || (TREE_CODE (TREE_TYPE (base))
6640 != ARRAY_TYPE))
6641 break;
6643 else if (TREE_CODE (base) == INDIRECT_REF
6644 && (TREE_CODE (TREE_OPERAND (base, 0))
6645 == COMPONENT_REF)
6646 && (TREE_CODE (TREE_TYPE
6647 (TREE_OPERAND (base, 0)))
6648 == REFERENCE_TYPE))
6649 base = TREE_OPERAND (base, 0);
6650 base = get_inner_reference (base, &bitsize2,
6651 &bitpos2, &offset2,
6652 &mode, &unsignedp,
6653 &volatilep, false);
6654 if (base != decl)
6655 break;
6656 gcc_assert (offset == NULL_TREE
6657 || TREE_CODE (offset) == INTEGER_CST);
6658 tree d1 = OMP_CLAUSE_DECL (*sc);
6659 tree d2 = OMP_CLAUSE_DECL (c);
6660 while (TREE_CODE (d1) == ARRAY_REF)
6661 d1 = TREE_OPERAND (d1, 0);
6662 while (TREE_CODE (d2) == ARRAY_REF)
6663 d2 = TREE_OPERAND (d2, 0);
6664 if (TREE_CODE (d1) == INDIRECT_REF)
6665 d1 = TREE_OPERAND (d1, 0);
6666 if (TREE_CODE (d2) == INDIRECT_REF)
6667 d2 = TREE_OPERAND (d2, 0);
6668 while (TREE_CODE (d1) == COMPONENT_REF)
6669 if (TREE_CODE (d2) == COMPONENT_REF
6670 && TREE_OPERAND (d1, 1)
6671 == TREE_OPERAND (d2, 1))
6673 d1 = TREE_OPERAND (d1, 0);
6674 d2 = TREE_OPERAND (d2, 0);
6676 else
6677 break;
6678 if (d1 == d2)
6680 error_at (OMP_CLAUSE_LOCATION (c),
6681 "%qE appears more than once in map "
6682 "clauses", OMP_CLAUSE_DECL (c));
6683 remove = true;
6684 break;
6686 if (offset2)
6687 o2 = wi::to_offset (offset2);
6688 else
6689 o2 = 0;
6690 if (bitpos2)
6691 o2 = o2 + bitpos2 / BITS_PER_UNIT;
6692 if (wi::ltu_p (o1, o2)
6693 || (wi::eq_p (o1, o2) && bitpos < bitpos2))
6694 break;
6696 if (ptr)
6698 if (!remove)
6699 *pt = tree_cons (TREE_PURPOSE (*osc), c, *pt);
6700 break;
6702 if (!remove)
6703 OMP_CLAUSE_SIZE (*osc)
6704 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
6705 size_one_node);
6706 if (!remove && *sc != c)
6708 *list_p = OMP_CLAUSE_CHAIN (c);
6709 OMP_CLAUSE_CHAIN (c) = *sc;
6710 *sc = c;
6711 continue;
6715 break;
6717 flags = GOVD_MAP | GOVD_EXPLICIT;
6718 goto do_add;
6720 case OMP_CLAUSE_DEPEND:
6721 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
6722 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
6724 /* Nothing to do. OMP_CLAUSE_DECL will be lowered in
6725 omp-low.c. */
6726 break;
6728 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6730 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6731 NULL, is_gimple_val, fb_rvalue);
6732 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6734 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6736 remove = true;
6737 break;
6739 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6740 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6741 is_gimple_val, fb_rvalue) == GS_ERROR)
6743 remove = true;
6744 break;
6746 break;
6748 case OMP_CLAUSE_TO:
6749 case OMP_CLAUSE_FROM:
6750 case OMP_CLAUSE__CACHE_:
6751 decl = OMP_CLAUSE_DECL (c);
6752 if (error_operand_p (decl))
6754 remove = true;
6755 break;
6757 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6758 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6759 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6760 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6761 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6763 remove = true;
6764 break;
6766 if (!DECL_P (decl))
6768 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6769 NULL, is_gimple_lvalue, fb_lvalue)
6770 == GS_ERROR)
6772 remove = true;
6773 break;
6775 break;
6777 goto do_notice;
6779 case OMP_CLAUSE_USE_DEVICE_PTR:
6780 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6781 goto do_add;
6782 case OMP_CLAUSE_IS_DEVICE_PTR:
6783 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6784 goto do_add;
6786 do_add:
6787 decl = OMP_CLAUSE_DECL (c);
6788 do_add_decl:
6789 if (error_operand_p (decl))
6791 remove = true;
6792 break;
6794 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
6796 tree t = omp_member_access_dummy_var (decl);
6797 if (t)
6799 tree v = DECL_VALUE_EXPR (decl);
6800 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
6801 if (outer_ctx)
6802 omp_notice_variable (outer_ctx, t, true);
6805 omp_add_variable (ctx, decl, flags);
6806 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6807 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6809 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6810 GOVD_LOCAL | GOVD_SEEN);
6811 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
6812 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
6813 find_decl_expr,
6814 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
6815 NULL) == NULL_TREE)
6816 omp_add_variable (ctx,
6817 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
6818 GOVD_LOCAL | GOVD_SEEN);
6819 gimplify_omp_ctxp = ctx;
6820 push_gimplify_context ();
6822 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6823 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6825 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6826 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6827 pop_gimplify_context
6828 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6829 push_gimplify_context ();
6830 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6831 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6832 pop_gimplify_context
6833 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6834 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6835 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6837 gimplify_omp_ctxp = outer_ctx;
6839 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6840 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6842 gimplify_omp_ctxp = ctx;
6843 push_gimplify_context ();
6844 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6846 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6847 NULL, NULL);
6848 TREE_SIDE_EFFECTS (bind) = 1;
6849 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6850 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6852 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6853 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6854 pop_gimplify_context
6855 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6856 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6858 gimplify_omp_ctxp = outer_ctx;
6860 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6861 && OMP_CLAUSE_LINEAR_STMT (c))
6863 gimplify_omp_ctxp = ctx;
6864 push_gimplify_context ();
6865 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6867 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6868 NULL, NULL);
6869 TREE_SIDE_EFFECTS (bind) = 1;
6870 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6871 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6873 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6874 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6875 pop_gimplify_context
6876 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6877 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6879 gimplify_omp_ctxp = outer_ctx;
6881 if (notice_outer)
6882 goto do_notice;
6883 break;
6885 case OMP_CLAUSE_COPYIN:
6886 case OMP_CLAUSE_COPYPRIVATE:
6887 decl = OMP_CLAUSE_DECL (c);
6888 if (error_operand_p (decl))
6890 remove = true;
6891 break;
6893 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6894 && !remove
6895 && !omp_check_private (ctx, decl, true))
6897 remove = true;
6898 if (is_global_var (decl))
6900 if (DECL_THREAD_LOCAL_P (decl))
6901 remove = false;
6902 else if (DECL_HAS_VALUE_EXPR_P (decl))
6904 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6906 if (value
6907 && DECL_P (value)
6908 && DECL_THREAD_LOCAL_P (value))
6909 remove = false;
6912 if (remove)
6913 error_at (OMP_CLAUSE_LOCATION (c),
6914 "copyprivate variable %qE is not threadprivate"
6915 " or private in outer context", DECL_NAME (decl));
6917 do_notice:
6918 if (outer_ctx)
6919 omp_notice_variable (outer_ctx, decl, true);
6920 if (check_non_private
6921 && region_type == ORT_WORKSHARE
6922 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
6923 || decl == OMP_CLAUSE_DECL (c)
6924 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
6925 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
6926 == ADDR_EXPR)))
6927 && omp_check_private (ctx, decl, false))
6929 error ("%s variable %qE is private in outer context",
6930 check_non_private, DECL_NAME (decl));
6931 remove = true;
6933 break;
6935 case OMP_CLAUSE_IF:
6936 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
6937 && OMP_CLAUSE_IF_MODIFIER (c) != code)
6939 const char *p[2];
6940 for (int i = 0; i < 2; i++)
6941 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
6943 case OMP_PARALLEL: p[i] = "parallel"; break;
6944 case OMP_TASK: p[i] = "task"; break;
6945 case OMP_TASKLOOP: p[i] = "taskloop"; break;
6946 case OMP_TARGET_DATA: p[i] = "target data"; break;
6947 case OMP_TARGET: p[i] = "target"; break;
6948 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
6949 case OMP_TARGET_ENTER_DATA:
6950 p[i] = "target enter data"; break;
6951 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
6952 default: gcc_unreachable ();
6954 error_at (OMP_CLAUSE_LOCATION (c),
6955 "expected %qs %<if%> clause modifier rather than %qs",
6956 p[0], p[1]);
6957 remove = true;
6959 /* Fall through. */
6961 case OMP_CLAUSE_FINAL:
6962 OMP_CLAUSE_OPERAND (c, 0)
6963 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6964 /* Fall through. */
6966 case OMP_CLAUSE_SCHEDULE:
6967 case OMP_CLAUSE_NUM_THREADS:
6968 case OMP_CLAUSE_NUM_TEAMS:
6969 case OMP_CLAUSE_THREAD_LIMIT:
6970 case OMP_CLAUSE_DIST_SCHEDULE:
6971 case OMP_CLAUSE_DEVICE:
6972 case OMP_CLAUSE_PRIORITY:
6973 case OMP_CLAUSE_GRAINSIZE:
6974 case OMP_CLAUSE_NUM_TASKS:
6975 case OMP_CLAUSE_HINT:
6976 case OMP_CLAUSE__CILK_FOR_COUNT_:
6977 case OMP_CLAUSE_ASYNC:
6978 case OMP_CLAUSE_WAIT:
6979 case OMP_CLAUSE_NUM_GANGS:
6980 case OMP_CLAUSE_NUM_WORKERS:
6981 case OMP_CLAUSE_VECTOR_LENGTH:
6982 case OMP_CLAUSE_WORKER:
6983 case OMP_CLAUSE_VECTOR:
6984 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6985 is_gimple_val, fb_rvalue) == GS_ERROR)
6986 remove = true;
6987 break;
6989 case OMP_CLAUSE_GANG:
6990 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6991 is_gimple_val, fb_rvalue) == GS_ERROR)
6992 remove = true;
6993 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
6994 is_gimple_val, fb_rvalue) == GS_ERROR)
6995 remove = true;
6996 break;
6998 case OMP_CLAUSE_DEVICE_RESIDENT:
6999 case OMP_CLAUSE_USE_DEVICE:
7000 case OMP_CLAUSE_INDEPENDENT:
7001 remove = true;
7002 break;
7004 case OMP_CLAUSE_NOWAIT:
7005 case OMP_CLAUSE_ORDERED:
7006 case OMP_CLAUSE_UNTIED:
7007 case OMP_CLAUSE_COLLAPSE:
7008 case OMP_CLAUSE_AUTO:
7009 case OMP_CLAUSE_SEQ:
7010 case OMP_CLAUSE_MERGEABLE:
7011 case OMP_CLAUSE_PROC_BIND:
7012 case OMP_CLAUSE_SAFELEN:
7013 case OMP_CLAUSE_SIMDLEN:
7014 case OMP_CLAUSE_NOGROUP:
7015 case OMP_CLAUSE_THREADS:
7016 case OMP_CLAUSE_SIMD:
7017 break;
7019 case OMP_CLAUSE_DEFAULTMAP:
7020 ctx->target_map_scalars_firstprivate = false;
7021 break;
7023 case OMP_CLAUSE_ALIGNED:
7024 decl = OMP_CLAUSE_DECL (c);
7025 if (error_operand_p (decl))
7027 remove = true;
7028 break;
7030 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
7031 is_gimple_val, fb_rvalue) == GS_ERROR)
7033 remove = true;
7034 break;
7036 if (!is_global_var (decl)
7037 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
7038 omp_add_variable (ctx, decl, GOVD_ALIGNED);
7039 break;
7041 case OMP_CLAUSE_DEFAULT:
7042 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
7043 break;
7045 default:
7046 gcc_unreachable ();
7049 if (remove)
7050 *list_p = OMP_CLAUSE_CHAIN (c);
7051 else
7052 list_p = &OMP_CLAUSE_CHAIN (c);
7055 gimplify_omp_ctxp = ctx;
7056 if (struct_map_to_clause)
7057 delete struct_map_to_clause;
7060 struct gimplify_adjust_omp_clauses_data
7062 tree *list_p;
7063 gimple_seq *pre_p;
7066 /* For all variables that were not actually used within the context,
7067 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
7069 static int
7070 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
7072 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
7073 gimple_seq *pre_p
7074 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
7075 tree decl = (tree) n->key;
7076 unsigned flags = n->value;
7077 enum omp_clause_code code;
7078 tree clause;
7079 bool private_debug;
7081 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
7082 return 0;
7083 if ((flags & GOVD_SEEN) == 0)
7084 return 0;
7085 if (flags & GOVD_DEBUG_PRIVATE)
7087 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
7088 private_debug = true;
7090 else if (flags & GOVD_MAP)
7091 private_debug = false;
7092 else
7093 private_debug
7094 = lang_hooks.decls.omp_private_debug_clause (decl,
7095 !!(flags & GOVD_SHARED));
7096 if (private_debug)
7097 code = OMP_CLAUSE_PRIVATE;
7098 else if (flags & GOVD_MAP)
7099 code = OMP_CLAUSE_MAP;
7100 else if (flags & GOVD_SHARED)
7102 if (is_global_var (decl))
7104 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
7105 while (ctx != NULL)
7107 splay_tree_node on
7108 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7109 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7110 | GOVD_PRIVATE | GOVD_REDUCTION
7111 | GOVD_LINEAR | GOVD_MAP)) != 0)
7112 break;
7113 ctx = ctx->outer_context;
7115 if (ctx == NULL)
7116 return 0;
7118 code = OMP_CLAUSE_SHARED;
7120 else if (flags & GOVD_PRIVATE)
7121 code = OMP_CLAUSE_PRIVATE;
7122 else if (flags & GOVD_FIRSTPRIVATE)
7123 code = OMP_CLAUSE_FIRSTPRIVATE;
7124 else if (flags & GOVD_LASTPRIVATE)
7125 code = OMP_CLAUSE_LASTPRIVATE;
7126 else if (flags & GOVD_ALIGNED)
7127 return 0;
7128 else
7129 gcc_unreachable ();
7131 clause = build_omp_clause (input_location, code);
7132 OMP_CLAUSE_DECL (clause) = decl;
7133 OMP_CLAUSE_CHAIN (clause) = *list_p;
7134 if (private_debug)
7135 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
7136 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
7137 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
7138 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
7140 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
7141 OMP_CLAUSE_DECL (nc) = decl;
7142 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7143 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
7144 OMP_CLAUSE_DECL (clause)
7145 = build_simple_mem_ref_loc (input_location, decl);
7146 OMP_CLAUSE_DECL (clause)
7147 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
7148 build_int_cst (build_pointer_type (char_type_node), 0));
7149 OMP_CLAUSE_SIZE (clause) = size_zero_node;
7150 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7151 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
7152 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
7153 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
7154 OMP_CLAUSE_CHAIN (nc) = *list_p;
7155 OMP_CLAUSE_CHAIN (clause) = nc;
7156 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7157 gimplify_omp_ctxp = ctx->outer_context;
7158 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
7159 pre_p, NULL, is_gimple_val, fb_rvalue);
7160 gimplify_omp_ctxp = ctx;
7162 else if (code == OMP_CLAUSE_MAP)
7164 OMP_CLAUSE_SET_MAP_KIND (clause,
7165 flags & GOVD_MAP_TO_ONLY
7166 ? GOMP_MAP_TO
7167 : GOMP_MAP_TOFROM);
7168 if (DECL_SIZE (decl)
7169 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7171 tree decl2 = DECL_VALUE_EXPR (decl);
7172 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7173 decl2 = TREE_OPERAND (decl2, 0);
7174 gcc_assert (DECL_P (decl2));
7175 tree mem = build_simple_mem_ref (decl2);
7176 OMP_CLAUSE_DECL (clause) = mem;
7177 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7178 if (gimplify_omp_ctxp->outer_context)
7180 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
7181 omp_notice_variable (ctx, decl2, true);
7182 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
7184 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
7185 OMP_CLAUSE_MAP);
7186 OMP_CLAUSE_DECL (nc) = decl;
7187 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7188 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
7189 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
7190 else
7191 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
7192 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
7193 OMP_CLAUSE_CHAIN (clause) = nc;
7195 else
7196 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
7198 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
7200 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
7201 OMP_CLAUSE_DECL (nc) = decl;
7202 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
7203 OMP_CLAUSE_CHAIN (nc) = *list_p;
7204 OMP_CLAUSE_CHAIN (clause) = nc;
7205 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7206 gimplify_omp_ctxp = ctx->outer_context;
7207 lang_hooks.decls.omp_finish_clause (nc, pre_p);
7208 gimplify_omp_ctxp = ctx;
7210 *list_p = clause;
7211 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7212 gimplify_omp_ctxp = ctx->outer_context;
7213 lang_hooks.decls.omp_finish_clause (clause, pre_p);
7214 gimplify_omp_ctxp = ctx;
7215 return 0;
7218 static void
7219 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p,
7220 enum tree_code code)
7222 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7223 tree c, decl;
7225 while ((c = *list_p) != NULL)
7227 splay_tree_node n;
7228 bool remove = false;
7230 switch (OMP_CLAUSE_CODE (c))
7232 case OMP_CLAUSE_PRIVATE:
7233 case OMP_CLAUSE_SHARED:
7234 case OMP_CLAUSE_FIRSTPRIVATE:
7235 case OMP_CLAUSE_LINEAR:
7236 decl = OMP_CLAUSE_DECL (c);
7237 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7238 remove = !(n->value & GOVD_SEEN);
7239 if (! remove)
7241 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
7242 if ((n->value & GOVD_DEBUG_PRIVATE)
7243 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
7245 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
7246 || ((n->value & GOVD_DATA_SHARE_CLASS)
7247 == GOVD_PRIVATE));
7248 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
7249 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
7252 break;
7254 case OMP_CLAUSE_LASTPRIVATE:
7255 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
7256 accurately reflect the presence of a FIRSTPRIVATE clause. */
7257 decl = OMP_CLAUSE_DECL (c);
7258 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7259 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
7260 = (n->value & GOVD_FIRSTPRIVATE) != 0;
7261 if (omp_no_lastprivate (ctx))
7263 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
7264 remove = true;
7265 else
7266 OMP_CLAUSE_CODE (c) = OMP_CLAUSE_PRIVATE;
7268 break;
7270 case OMP_CLAUSE_ALIGNED:
7271 decl = OMP_CLAUSE_DECL (c);
7272 if (!is_global_var (decl))
7274 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7275 remove = n == NULL || !(n->value & GOVD_SEEN);
7276 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
7278 struct gimplify_omp_ctx *octx;
7279 if (n != NULL
7280 && (n->value & (GOVD_DATA_SHARE_CLASS
7281 & ~GOVD_FIRSTPRIVATE)))
7282 remove = true;
7283 else
7284 for (octx = ctx->outer_context; octx;
7285 octx = octx->outer_context)
7287 n = splay_tree_lookup (octx->variables,
7288 (splay_tree_key) decl);
7289 if (n == NULL)
7290 continue;
7291 if (n->value & GOVD_LOCAL)
7292 break;
7293 /* We have to avoid assigning a shared variable
7294 to itself when trying to add
7295 __builtin_assume_aligned. */
7296 if (n->value & GOVD_SHARED)
7298 remove = true;
7299 break;
7304 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7306 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7307 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7308 remove = true;
7310 break;
7312 case OMP_CLAUSE_MAP:
7313 decl = OMP_CLAUSE_DECL (c);
7314 if (!DECL_P (decl))
7316 if ((ctx->region_type & ORT_TARGET) != 0
7317 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7319 if (TREE_CODE (decl) == INDIRECT_REF
7320 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7321 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7322 == REFERENCE_TYPE))
7323 decl = TREE_OPERAND (decl, 0);
7324 if (TREE_CODE (decl) == COMPONENT_REF)
7326 while (TREE_CODE (decl) == COMPONENT_REF)
7327 decl = TREE_OPERAND (decl, 0);
7328 if (DECL_P (decl))
7330 n = splay_tree_lookup (ctx->variables,
7331 (splay_tree_key) decl);
7332 if (!(n->value & GOVD_SEEN))
7333 remove = true;
7337 break;
7339 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7340 if ((ctx->region_type & ORT_TARGET) != 0
7341 && !(n->value & GOVD_SEEN)
7342 && ((OMP_CLAUSE_MAP_KIND (c) & GOMP_MAP_FLAG_ALWAYS) == 0
7343 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT))
7345 remove = true;
7346 /* For struct element mapping, if struct is never referenced
7347 in target block and none of the mapping has always modifier,
7348 remove all the struct element mappings, which immediately
7349 follow the GOMP_MAP_STRUCT map clause. */
7350 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
7352 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
7353 while (cnt--)
7354 OMP_CLAUSE_CHAIN (c)
7355 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
7358 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
7359 && code == OMP_TARGET_EXIT_DATA)
7360 remove = true;
7361 else if (DECL_SIZE (decl)
7362 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
7363 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
7364 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER)
7366 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
7367 for these, TREE_CODE (DECL_SIZE (decl)) will always be
7368 INTEGER_CST. */
7369 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
7371 tree decl2 = DECL_VALUE_EXPR (decl);
7372 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7373 decl2 = TREE_OPERAND (decl2, 0);
7374 gcc_assert (DECL_P (decl2));
7375 tree mem = build_simple_mem_ref (decl2);
7376 OMP_CLAUSE_DECL (c) = mem;
7377 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7378 if (ctx->outer_context)
7380 omp_notice_variable (ctx->outer_context, decl2, true);
7381 omp_notice_variable (ctx->outer_context,
7382 OMP_CLAUSE_SIZE (c), true);
7384 if (((ctx->region_type & ORT_TARGET) != 0
7385 || !ctx->target_firstprivatize_array_bases)
7386 && ((n->value & GOVD_SEEN) == 0
7387 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
7389 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7390 OMP_CLAUSE_MAP);
7391 OMP_CLAUSE_DECL (nc) = decl;
7392 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7393 if (ctx->target_firstprivatize_array_bases)
7394 OMP_CLAUSE_SET_MAP_KIND (nc,
7395 GOMP_MAP_FIRSTPRIVATE_POINTER);
7396 else
7397 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
7398 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
7399 OMP_CLAUSE_CHAIN (c) = nc;
7400 c = nc;
7403 else
7405 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7406 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
7407 if ((n->value & GOVD_SEEN)
7408 && (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)))
7409 OMP_CLAUSE_MAP_PRIVATE (c) = 1;
7411 break;
7413 case OMP_CLAUSE_TO:
7414 case OMP_CLAUSE_FROM:
7415 case OMP_CLAUSE__CACHE_:
7416 decl = OMP_CLAUSE_DECL (c);
7417 if (!DECL_P (decl))
7418 break;
7419 if (DECL_SIZE (decl)
7420 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7422 tree decl2 = DECL_VALUE_EXPR (decl);
7423 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7424 decl2 = TREE_OPERAND (decl2, 0);
7425 gcc_assert (DECL_P (decl2));
7426 tree mem = build_simple_mem_ref (decl2);
7427 OMP_CLAUSE_DECL (c) = mem;
7428 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7429 if (ctx->outer_context)
7431 omp_notice_variable (ctx->outer_context, decl2, true);
7432 omp_notice_variable (ctx->outer_context,
7433 OMP_CLAUSE_SIZE (c), true);
7436 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7437 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
7438 break;
7440 case OMP_CLAUSE_REDUCTION:
7441 case OMP_CLAUSE_COPYIN:
7442 case OMP_CLAUSE_COPYPRIVATE:
7443 case OMP_CLAUSE_IF:
7444 case OMP_CLAUSE_NUM_THREADS:
7445 case OMP_CLAUSE_NUM_TEAMS:
7446 case OMP_CLAUSE_THREAD_LIMIT:
7447 case OMP_CLAUSE_DIST_SCHEDULE:
7448 case OMP_CLAUSE_DEVICE:
7449 case OMP_CLAUSE_SCHEDULE:
7450 case OMP_CLAUSE_NOWAIT:
7451 case OMP_CLAUSE_ORDERED:
7452 case OMP_CLAUSE_DEFAULT:
7453 case OMP_CLAUSE_UNTIED:
7454 case OMP_CLAUSE_COLLAPSE:
7455 case OMP_CLAUSE_FINAL:
7456 case OMP_CLAUSE_MERGEABLE:
7457 case OMP_CLAUSE_PROC_BIND:
7458 case OMP_CLAUSE_SAFELEN:
7459 case OMP_CLAUSE_SIMDLEN:
7460 case OMP_CLAUSE_DEPEND:
7461 case OMP_CLAUSE_PRIORITY:
7462 case OMP_CLAUSE_GRAINSIZE:
7463 case OMP_CLAUSE_NUM_TASKS:
7464 case OMP_CLAUSE_NOGROUP:
7465 case OMP_CLAUSE_THREADS:
7466 case OMP_CLAUSE_SIMD:
7467 case OMP_CLAUSE_HINT:
7468 case OMP_CLAUSE_DEFAULTMAP:
7469 case OMP_CLAUSE_USE_DEVICE_PTR:
7470 case OMP_CLAUSE_IS_DEVICE_PTR:
7471 case OMP_CLAUSE__CILK_FOR_COUNT_:
7472 case OMP_CLAUSE_ASYNC:
7473 case OMP_CLAUSE_WAIT:
7474 case OMP_CLAUSE_DEVICE_RESIDENT:
7475 case OMP_CLAUSE_USE_DEVICE:
7476 case OMP_CLAUSE_INDEPENDENT:
7477 case OMP_CLAUSE_NUM_GANGS:
7478 case OMP_CLAUSE_NUM_WORKERS:
7479 case OMP_CLAUSE_VECTOR_LENGTH:
7480 case OMP_CLAUSE_GANG:
7481 case OMP_CLAUSE_WORKER:
7482 case OMP_CLAUSE_VECTOR:
7483 case OMP_CLAUSE_AUTO:
7484 case OMP_CLAUSE_SEQ:
7485 break;
7487 default:
7488 gcc_unreachable ();
7491 if (remove)
7492 *list_p = OMP_CLAUSE_CHAIN (c);
7493 else
7494 list_p = &OMP_CLAUSE_CHAIN (c);
7497 /* Add in any implicit data sharing. */
7498 struct gimplify_adjust_omp_clauses_data data;
7499 data.list_p = list_p;
7500 data.pre_p = pre_p;
7501 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
7503 gimplify_omp_ctxp = ctx->outer_context;
7504 delete_omp_context (ctx);
7507 /* Gimplify OACC_CACHE. */
7509 static void
7510 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
7512 tree expr = *expr_p;
7514 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_WORKSHARE,
7515 OACC_CACHE);
7516 gimplify_adjust_omp_clauses (pre_p, &OACC_CACHE_CLAUSES (expr), OACC_CACHE);
7518 /* TODO: Do something sensible with this information. */
7520 *expr_p = NULL_TREE;
7523 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
7524 gimplification of the body, as well as scanning the body for used
7525 variables. We need to do this scan now, because variable-sized
7526 decls will be decomposed during gimplification. */
7528 static void
7529 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
7531 tree expr = *expr_p;
7532 gimple *g;
7533 gimple_seq body = NULL;
7535 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
7536 OMP_PARALLEL_COMBINED (expr)
7537 ? ORT_COMBINED_PARALLEL
7538 : ORT_PARALLEL, OMP_PARALLEL);
7540 push_gimplify_context ();
7542 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
7543 if (gimple_code (g) == GIMPLE_BIND)
7544 pop_gimplify_context (g);
7545 else
7546 pop_gimplify_context (NULL);
7548 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr),
7549 OMP_PARALLEL);
7551 g = gimple_build_omp_parallel (body,
7552 OMP_PARALLEL_CLAUSES (expr),
7553 NULL_TREE, NULL_TREE);
7554 if (OMP_PARALLEL_COMBINED (expr))
7555 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
7556 gimplify_seq_add_stmt (pre_p, g);
7557 *expr_p = NULL_TREE;
7560 /* Gimplify the contents of an OMP_TASK statement. This involves
7561 gimplification of the body, as well as scanning the body for used
7562 variables. We need to do this scan now, because variable-sized
7563 decls will be decomposed during gimplification. */
7565 static void
7566 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
7568 tree expr = *expr_p;
7569 gimple *g;
7570 gimple_seq body = NULL;
7572 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
7573 find_omp_clause (OMP_TASK_CLAUSES (expr),
7574 OMP_CLAUSE_UNTIED)
7575 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
7577 push_gimplify_context ();
7579 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
7580 if (gimple_code (g) == GIMPLE_BIND)
7581 pop_gimplify_context (g);
7582 else
7583 pop_gimplify_context (NULL);
7585 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr), OMP_TASK);
7587 g = gimple_build_omp_task (body,
7588 OMP_TASK_CLAUSES (expr),
7589 NULL_TREE, NULL_TREE,
7590 NULL_TREE, NULL_TREE, NULL_TREE);
7591 gimplify_seq_add_stmt (pre_p, g);
7592 *expr_p = NULL_TREE;
7595 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
7596 with non-NULL OMP_FOR_INIT. */
7598 static tree
7599 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
7601 *walk_subtrees = 0;
7602 switch (TREE_CODE (*tp))
7604 case OMP_FOR:
7605 *walk_subtrees = 1;
7606 /* FALLTHRU */
7607 case OMP_SIMD:
7608 if (OMP_FOR_INIT (*tp) != NULL_TREE)
7609 return *tp;
7610 break;
7611 case BIND_EXPR:
7612 case STATEMENT_LIST:
7613 case OMP_PARALLEL:
7614 *walk_subtrees = 1;
7615 break;
7616 default:
7617 break;
7619 return NULL_TREE;
7622 /* Gimplify the gross structure of an OMP_FOR statement. */
7624 static enum gimplify_status
7625 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
7627 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
7628 enum gimplify_status ret = GS_ALL_DONE;
7629 enum gimplify_status tret;
7630 gomp_for *gfor;
7631 gimple_seq for_body, for_pre_body;
7632 int i;
7633 bitmap has_decl_expr = NULL;
7634 enum omp_region_type ort = ORT_WORKSHARE;
7636 orig_for_stmt = for_stmt = *expr_p;
7638 switch (TREE_CODE (for_stmt))
7640 case OMP_FOR:
7641 case CILK_FOR:
7642 case OMP_DISTRIBUTE:
7643 case OACC_LOOP:
7644 break;
7645 case OMP_TASKLOOP:
7646 if (find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
7647 ort = ORT_UNTIED_TASK;
7648 else
7649 ort = ORT_TASK;
7650 break;
7651 case OMP_SIMD:
7652 case CILK_SIMD:
7653 ort = ORT_SIMD;
7654 break;
7655 default:
7656 gcc_unreachable ();
7659 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
7660 clause for the IV. */
7661 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7663 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
7664 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7665 decl = TREE_OPERAND (t, 0);
7666 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
7667 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7668 && OMP_CLAUSE_DECL (c) == decl)
7670 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
7671 break;
7675 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
7677 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
7678 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
7679 find_combined_omp_for, NULL, NULL);
7680 if (inner_for_stmt == NULL_TREE)
7682 gcc_assert (seen_error ());
7683 *expr_p = NULL_TREE;
7684 return GS_ERROR;
7688 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
7689 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
7690 TREE_CODE (for_stmt));
7692 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
7693 gimplify_omp_ctxp->distribute = true;
7695 /* Handle OMP_FOR_INIT. */
7696 for_pre_body = NULL;
7697 if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
7699 has_decl_expr = BITMAP_ALLOC (NULL);
7700 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
7701 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
7702 == VAR_DECL)
7704 t = OMP_FOR_PRE_BODY (for_stmt);
7705 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
7707 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
7709 tree_stmt_iterator si;
7710 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
7711 tsi_next (&si))
7713 t = tsi_stmt (si);
7714 if (TREE_CODE (t) == DECL_EXPR
7715 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
7716 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
7720 if (OMP_FOR_PRE_BODY (for_stmt))
7722 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
7723 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
7724 else
7726 struct gimplify_omp_ctx ctx;
7727 memset (&ctx, 0, sizeof (ctx));
7728 ctx.region_type = ORT_NONE;
7729 gimplify_omp_ctxp = &ctx;
7730 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
7731 gimplify_omp_ctxp = NULL;
7734 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
7736 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
7737 for_stmt = inner_for_stmt;
7739 /* For taskloop, need to gimplify the start, end and step before the
7740 taskloop, outside of the taskloop omp context. */
7741 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
7743 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7745 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7746 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
7748 TREE_OPERAND (t, 1)
7749 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
7750 pre_p, NULL);
7751 tree c = build_omp_clause (input_location,
7752 OMP_CLAUSE_FIRSTPRIVATE);
7753 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
7754 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
7755 OMP_FOR_CLAUSES (orig_for_stmt) = c;
7758 /* Handle OMP_FOR_COND. */
7759 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7760 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
7762 TREE_OPERAND (t, 1)
7763 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
7764 gimple_seq_empty_p (for_pre_body)
7765 ? pre_p : &for_pre_body, NULL);
7766 tree c = build_omp_clause (input_location,
7767 OMP_CLAUSE_FIRSTPRIVATE);
7768 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
7769 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
7770 OMP_FOR_CLAUSES (orig_for_stmt) = c;
7773 /* Handle OMP_FOR_INCR. */
7774 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7775 if (TREE_CODE (t) == MODIFY_EXPR)
7777 decl = TREE_OPERAND (t, 0);
7778 t = TREE_OPERAND (t, 1);
7779 tree *tp = &TREE_OPERAND (t, 1);
7780 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
7781 tp = &TREE_OPERAND (t, 0);
7783 if (!is_gimple_constant (*tp))
7785 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
7786 ? pre_p : &for_pre_body;
7787 *tp = get_initialized_tmp_var (*tp, seq, NULL);
7788 tree c = build_omp_clause (input_location,
7789 OMP_CLAUSE_FIRSTPRIVATE);
7790 OMP_CLAUSE_DECL (c) = *tp;
7791 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
7792 OMP_FOR_CLAUSES (orig_for_stmt) = c;
7797 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
7798 OMP_TASKLOOP);
7801 if (orig_for_stmt != for_stmt)
7802 gimplify_omp_ctxp->combined_loop = true;
7804 for_body = NULL;
7805 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
7806 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
7807 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
7808 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
7810 tree c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
7811 bool is_doacross = false;
7812 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
7814 is_doacross = true;
7815 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
7816 (OMP_FOR_INIT (for_stmt))
7817 * 2);
7819 int collapse = 1;
7820 c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
7821 if (c)
7822 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
7823 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7825 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7826 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7827 decl = TREE_OPERAND (t, 0);
7828 gcc_assert (DECL_P (decl));
7829 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
7830 || POINTER_TYPE_P (TREE_TYPE (decl)));
7831 if (is_doacross)
7833 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
7834 gimplify_omp_ctxp->loop_iter_var.quick_push
7835 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
7836 else
7837 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
7838 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
7841 /* Make sure the iteration variable is private. */
7842 tree c = NULL_TREE;
7843 tree c2 = NULL_TREE;
7844 if (orig_for_stmt != for_stmt)
7845 /* Do this only on innermost construct for combined ones. */;
7846 else if (ort == ORT_SIMD)
7848 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
7849 (splay_tree_key) decl);
7850 omp_is_private (gimplify_omp_ctxp, decl,
7851 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
7852 != 1));
7853 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7854 omp_notice_variable (gimplify_omp_ctxp, decl, true);
7855 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7857 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7858 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
7859 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
7860 if ((has_decl_expr
7861 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
7862 || omp_no_lastprivate (gimplify_omp_ctxp))
7864 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
7865 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7867 struct gimplify_omp_ctx *outer
7868 = gimplify_omp_ctxp->outer_context;
7869 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7871 if (outer->region_type == ORT_WORKSHARE
7872 && outer->combined_loop)
7874 n = splay_tree_lookup (outer->variables,
7875 (splay_tree_key)decl);
7876 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
7878 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
7879 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7884 OMP_CLAUSE_DECL (c) = decl;
7885 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
7886 OMP_FOR_CLAUSES (for_stmt) = c;
7887 omp_add_variable (gimplify_omp_ctxp, decl, flags);
7888 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7890 if (outer->region_type == ORT_WORKSHARE
7891 && outer->combined_loop)
7893 if (outer->outer_context
7894 && (outer->outer_context->region_type
7895 == ORT_COMBINED_PARALLEL))
7896 outer = outer->outer_context;
7897 else if (omp_check_private (outer, decl, false))
7898 outer = NULL;
7900 else if (((outer->region_type & ORT_TASK) != 0)
7901 && outer->combined_loop
7902 && !omp_check_private (gimplify_omp_ctxp,
7903 decl, false))
7905 else if (outer->region_type != ORT_COMBINED_PARALLEL)
7906 outer = NULL;
7907 if (outer)
7909 n = splay_tree_lookup (outer->variables,
7910 (splay_tree_key)decl);
7911 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
7913 omp_add_variable (outer, decl,
7914 GOVD_LASTPRIVATE | GOVD_SEEN);
7915 if (outer->outer_context)
7916 omp_notice_variable (outer->outer_context, decl,
7917 true);
7922 else
7924 bool lastprivate
7925 = (!has_decl_expr
7926 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
7927 && !omp_no_lastprivate (gimplify_omp_ctxp);
7928 struct gimplify_omp_ctx *outer
7929 = gimplify_omp_ctxp->outer_context;
7930 if (outer && lastprivate)
7932 if (outer->region_type == ORT_WORKSHARE
7933 && outer->combined_loop)
7935 n = splay_tree_lookup (outer->variables,
7936 (splay_tree_key)decl);
7937 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
7939 lastprivate = false;
7940 outer = NULL;
7942 else if (outer->outer_context
7943 && (outer->outer_context->region_type
7944 == ORT_COMBINED_PARALLEL))
7945 outer = outer->outer_context;
7946 else if (omp_check_private (outer, decl, false))
7947 outer = NULL;
7949 else if (((outer->region_type & ORT_TASK) != 0)
7950 && outer->combined_loop
7951 && !omp_check_private (gimplify_omp_ctxp,
7952 decl, false))
7954 else if (outer->region_type != ORT_COMBINED_PARALLEL)
7955 outer = NULL;
7956 if (outer)
7958 n = splay_tree_lookup (outer->variables,
7959 (splay_tree_key)decl);
7960 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
7962 omp_add_variable (outer, decl,
7963 GOVD_LASTPRIVATE | GOVD_SEEN);
7964 if (outer->outer_context)
7965 omp_notice_variable (outer->outer_context, decl,
7966 true);
7971 c = build_omp_clause (input_location,
7972 lastprivate ? OMP_CLAUSE_LASTPRIVATE
7973 : OMP_CLAUSE_PRIVATE);
7974 OMP_CLAUSE_DECL (c) = decl;
7975 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
7976 OMP_FOR_CLAUSES (for_stmt) = c;
7977 omp_add_variable (gimplify_omp_ctxp, decl,
7978 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
7979 | GOVD_EXPLICIT | GOVD_SEEN);
7980 c = NULL_TREE;
7983 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
7984 omp_notice_variable (gimplify_omp_ctxp, decl, true);
7985 else
7986 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
7988 /* If DECL is not a gimple register, create a temporary variable to act
7989 as an iteration counter. This is valid, since DECL cannot be
7990 modified in the body of the loop. Similarly for any iteration vars
7991 in simd with collapse > 1 where the iterator vars must be
7992 lastprivate. */
7993 if (orig_for_stmt != for_stmt)
7994 var = decl;
7995 else if (!is_gimple_reg (decl)
7996 || (ort == ORT_SIMD
7997 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
7999 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
8000 TREE_OPERAND (t, 0) = var;
8002 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
8004 if (ort == ORT_SIMD
8005 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
8007 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
8008 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
8009 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
8010 OMP_CLAUSE_DECL (c2) = var;
8011 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
8012 OMP_FOR_CLAUSES (for_stmt) = c2;
8013 omp_add_variable (gimplify_omp_ctxp, var,
8014 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
8015 if (c == NULL_TREE)
8017 c = c2;
8018 c2 = NULL_TREE;
8021 else
8022 omp_add_variable (gimplify_omp_ctxp, var,
8023 GOVD_PRIVATE | GOVD_SEEN);
8025 else
8026 var = decl;
8028 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8029 is_gimple_val, fb_rvalue);
8030 ret = MIN (ret, tret);
8031 if (ret == GS_ERROR)
8032 return ret;
8034 /* Handle OMP_FOR_COND. */
8035 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
8036 gcc_assert (COMPARISON_CLASS_P (t));
8037 gcc_assert (TREE_OPERAND (t, 0) == decl);
8039 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8040 is_gimple_val, fb_rvalue);
8041 ret = MIN (ret, tret);
8043 /* Handle OMP_FOR_INCR. */
8044 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8045 switch (TREE_CODE (t))
8047 case PREINCREMENT_EXPR:
8048 case POSTINCREMENT_EXPR:
8050 tree decl = TREE_OPERAND (t, 0);
8051 /* c_omp_for_incr_canonicalize_ptr() should have been
8052 called to massage things appropriately. */
8053 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8055 if (orig_for_stmt != for_stmt)
8056 break;
8057 t = build_int_cst (TREE_TYPE (decl), 1);
8058 if (c)
8059 OMP_CLAUSE_LINEAR_STEP (c) = t;
8060 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
8061 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
8062 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
8063 break;
8066 case PREDECREMENT_EXPR:
8067 case POSTDECREMENT_EXPR:
8068 /* c_omp_for_incr_canonicalize_ptr() should have been
8069 called to massage things appropriately. */
8070 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8071 if (orig_for_stmt != for_stmt)
8072 break;
8073 t = build_int_cst (TREE_TYPE (decl), -1);
8074 if (c)
8075 OMP_CLAUSE_LINEAR_STEP (c) = t;
8076 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
8077 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
8078 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
8079 break;
8081 case MODIFY_EXPR:
8082 gcc_assert (TREE_OPERAND (t, 0) == decl);
8083 TREE_OPERAND (t, 0) = var;
8085 t = TREE_OPERAND (t, 1);
8086 switch (TREE_CODE (t))
8088 case PLUS_EXPR:
8089 if (TREE_OPERAND (t, 1) == decl)
8091 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
8092 TREE_OPERAND (t, 0) = var;
8093 break;
8096 /* Fallthru. */
8097 case MINUS_EXPR:
8098 case POINTER_PLUS_EXPR:
8099 gcc_assert (TREE_OPERAND (t, 0) == decl);
8100 TREE_OPERAND (t, 0) = var;
8101 break;
8102 default:
8103 gcc_unreachable ();
8106 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8107 is_gimple_val, fb_rvalue);
8108 ret = MIN (ret, tret);
8109 if (c)
8111 tree step = TREE_OPERAND (t, 1);
8112 tree stept = TREE_TYPE (decl);
8113 if (POINTER_TYPE_P (stept))
8114 stept = sizetype;
8115 step = fold_convert (stept, step);
8116 if (TREE_CODE (t) == MINUS_EXPR)
8117 step = fold_build1 (NEGATE_EXPR, stept, step);
8118 OMP_CLAUSE_LINEAR_STEP (c) = step;
8119 if (step != TREE_OPERAND (t, 1))
8121 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
8122 &for_pre_body, NULL,
8123 is_gimple_val, fb_rvalue);
8124 ret = MIN (ret, tret);
8127 break;
8129 default:
8130 gcc_unreachable ();
8133 if (c2)
8135 gcc_assert (c);
8136 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
8139 if ((var != decl || collapse > 1) && orig_for_stmt == for_stmt)
8141 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
8142 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8143 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
8144 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8145 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
8146 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
8147 && OMP_CLAUSE_DECL (c) == decl)
8149 if (is_doacross && (collapse == 1 || i >= collapse))
8150 t = var;
8151 else
8153 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8154 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
8155 gcc_assert (TREE_OPERAND (t, 0) == var);
8156 t = TREE_OPERAND (t, 1);
8157 gcc_assert (TREE_CODE (t) == PLUS_EXPR
8158 || TREE_CODE (t) == MINUS_EXPR
8159 || TREE_CODE (t) == POINTER_PLUS_EXPR);
8160 gcc_assert (TREE_OPERAND (t, 0) == var);
8161 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
8162 is_doacross ? var : decl,
8163 TREE_OPERAND (t, 1));
8165 gimple_seq *seq;
8166 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
8167 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
8168 else
8169 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
8170 gimplify_assign (decl, t, seq);
8175 BITMAP_FREE (has_decl_expr);
8177 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8179 push_gimplify_context ();
8180 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
8182 OMP_FOR_BODY (orig_for_stmt)
8183 = build3 (BIND_EXPR, void_type_node, NULL,
8184 OMP_FOR_BODY (orig_for_stmt), NULL);
8185 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
8189 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
8190 &for_body);
8192 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8194 if (gimple_code (g) == GIMPLE_BIND)
8195 pop_gimplify_context (g);
8196 else
8197 pop_gimplify_context (NULL);
8200 if (orig_for_stmt != for_stmt)
8201 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
8203 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
8204 decl = TREE_OPERAND (t, 0);
8205 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8206 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8207 gimplify_omp_ctxp = ctx->outer_context;
8208 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
8209 gimplify_omp_ctxp = ctx;
8210 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
8211 TREE_OPERAND (t, 0) = var;
8212 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8213 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
8214 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
8217 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt),
8218 TREE_CODE (orig_for_stmt));
8220 int kind;
8221 switch (TREE_CODE (orig_for_stmt))
8223 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
8224 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
8225 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
8226 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
8227 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
8228 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
8229 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
8230 default:
8231 gcc_unreachable ();
8233 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
8234 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
8235 for_pre_body);
8236 if (orig_for_stmt != for_stmt)
8237 gimple_omp_for_set_combined_p (gfor, true);
8238 if (gimplify_omp_ctxp
8239 && (gimplify_omp_ctxp->combined_loop
8240 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
8241 && gimplify_omp_ctxp->outer_context
8242 && gimplify_omp_ctxp->outer_context->combined_loop)))
8244 gimple_omp_for_set_combined_into_p (gfor, true);
8245 if (gimplify_omp_ctxp->combined_loop)
8246 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
8247 else
8248 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
8251 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
8253 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
8254 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
8255 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
8256 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
8257 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
8258 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
8259 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8260 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
8263 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
8264 constructs with GIMPLE_OMP_TASK sandwiched in between them.
8265 The outer taskloop stands for computing the number of iterations,
8266 counts for collapsed loops and holding taskloop specific clauses.
8267 The task construct stands for the effect of data sharing on the
8268 explicit task it creates and the inner taskloop stands for expansion
8269 of the static loop inside of the explicit task construct. */
8270 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8272 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
8273 tree task_clauses = NULL_TREE;
8274 tree c = *gfor_clauses_ptr;
8275 tree *gtask_clauses_ptr = &task_clauses;
8276 tree outer_for_clauses = NULL_TREE;
8277 tree *gforo_clauses_ptr = &outer_for_clauses;
8278 for (; c; c = OMP_CLAUSE_CHAIN (c))
8279 switch (OMP_CLAUSE_CODE (c))
8281 /* These clauses are allowed on task, move them there. */
8282 case OMP_CLAUSE_SHARED:
8283 case OMP_CLAUSE_FIRSTPRIVATE:
8284 case OMP_CLAUSE_DEFAULT:
8285 case OMP_CLAUSE_IF:
8286 case OMP_CLAUSE_UNTIED:
8287 case OMP_CLAUSE_FINAL:
8288 case OMP_CLAUSE_MERGEABLE:
8289 case OMP_CLAUSE_PRIORITY:
8290 *gtask_clauses_ptr = c;
8291 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8292 break;
8293 case OMP_CLAUSE_PRIVATE:
8294 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
8296 /* We want private on outer for and firstprivate
8297 on task. */
8298 *gtask_clauses_ptr
8299 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8300 OMP_CLAUSE_FIRSTPRIVATE);
8301 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
8302 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
8303 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
8304 *gforo_clauses_ptr = c;
8305 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8307 else
8309 *gtask_clauses_ptr = c;
8310 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8312 break;
8313 /* These clauses go into outer taskloop clauses. */
8314 case OMP_CLAUSE_GRAINSIZE:
8315 case OMP_CLAUSE_NUM_TASKS:
8316 case OMP_CLAUSE_NOGROUP:
8317 *gforo_clauses_ptr = c;
8318 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8319 break;
8320 /* Taskloop clause we duplicate on both taskloops. */
8321 case OMP_CLAUSE_COLLAPSE:
8322 *gfor_clauses_ptr = c;
8323 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8324 *gforo_clauses_ptr = copy_node (c);
8325 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
8326 break;
8327 /* For lastprivate, keep the clause on inner taskloop, and add
8328 a shared clause on task. If the same decl is also firstprivate,
8329 add also firstprivate clause on the inner taskloop. */
8330 case OMP_CLAUSE_LASTPRIVATE:
8331 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
8333 /* For taskloop C++ lastprivate IVs, we want:
8334 1) private on outer taskloop
8335 2) firstprivate and shared on task
8336 3) lastprivate on inner taskloop */
8337 *gtask_clauses_ptr
8338 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8339 OMP_CLAUSE_FIRSTPRIVATE);
8340 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
8341 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
8342 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
8343 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
8344 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8345 OMP_CLAUSE_PRIVATE);
8346 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
8347 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
8348 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
8349 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
8351 *gfor_clauses_ptr = c;
8352 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8353 *gtask_clauses_ptr
8354 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
8355 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
8356 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
8357 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
8358 gtask_clauses_ptr
8359 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
8360 break;
8361 default:
8362 gcc_unreachable ();
8364 *gfor_clauses_ptr = NULL_TREE;
8365 *gtask_clauses_ptr = NULL_TREE;
8366 *gforo_clauses_ptr = NULL_TREE;
8367 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
8368 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
8369 NULL_TREE, NULL_TREE, NULL_TREE);
8370 gimple_omp_task_set_taskloop_p (g, true);
8371 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
8372 gomp_for *gforo
8373 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
8374 gimple_omp_for_collapse (gfor),
8375 gimple_omp_for_pre_body (gfor));
8376 gimple_omp_for_set_pre_body (gfor, NULL);
8377 gimple_omp_for_set_combined_p (gforo, true);
8378 gimple_omp_for_set_combined_into_p (gfor, true);
8379 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
8381 t = unshare_expr (gimple_omp_for_index (gfor, i));
8382 gimple_omp_for_set_index (gforo, i, t);
8383 t = unshare_expr (gimple_omp_for_initial (gfor, i));
8384 gimple_omp_for_set_initial (gforo, i, t);
8385 gimple_omp_for_set_cond (gforo, i,
8386 gimple_omp_for_cond (gfor, i));
8387 t = unshare_expr (gimple_omp_for_final (gfor, i));
8388 gimple_omp_for_set_final (gforo, i, t);
8389 t = unshare_expr (gimple_omp_for_incr (gfor, i));
8390 gimple_omp_for_set_incr (gforo, i, t);
8392 gimplify_seq_add_stmt (pre_p, gforo);
8394 else
8395 gimplify_seq_add_stmt (pre_p, gfor);
8396 if (ret != GS_ALL_DONE)
8397 return GS_ERROR;
8398 *expr_p = NULL_TREE;
8399 return GS_ALL_DONE;
8402 /* Gimplify the gross structure of several OMP constructs. */
8404 static void
8405 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
8407 tree expr = *expr_p;
8408 gimple *stmt;
8409 gimple_seq body = NULL;
8410 enum omp_region_type ort;
8412 switch (TREE_CODE (expr))
8414 case OMP_SECTIONS:
8415 case OMP_SINGLE:
8416 ort = ORT_WORKSHARE;
8417 break;
8418 case OMP_TARGET:
8419 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
8420 break;
8421 case OACC_KERNELS:
8422 case OACC_PARALLEL:
8423 ort = ORT_TARGET;
8424 break;
8425 case OACC_DATA:
8426 case OMP_TARGET_DATA:
8427 ort = ORT_TARGET_DATA;
8428 break;
8429 case OMP_TEAMS:
8430 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
8431 break;
8432 default:
8433 gcc_unreachable ();
8435 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
8436 TREE_CODE (expr));
8437 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
8439 push_gimplify_context ();
8440 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
8441 if (gimple_code (g) == GIMPLE_BIND)
8442 pop_gimplify_context (g);
8443 else
8444 pop_gimplify_context (NULL);
8445 if (ort == ORT_TARGET_DATA)
8447 enum built_in_function end_ix;
8448 switch (TREE_CODE (expr))
8450 case OACC_DATA:
8451 end_ix = BUILT_IN_GOACC_DATA_END;
8452 break;
8453 case OMP_TARGET_DATA:
8454 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
8455 break;
8456 default:
8457 gcc_unreachable ();
8459 tree fn = builtin_decl_explicit (end_ix);
8460 g = gimple_build_call (fn, 0);
8461 gimple_seq cleanup = NULL;
8462 gimple_seq_add_stmt (&cleanup, g);
8463 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8464 body = NULL;
8465 gimple_seq_add_stmt (&body, g);
8468 else
8469 gimplify_and_add (OMP_BODY (expr), &body);
8470 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr), TREE_CODE (expr));
8472 switch (TREE_CODE (expr))
8474 case OACC_DATA:
8475 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
8476 OMP_CLAUSES (expr));
8477 break;
8478 case OACC_KERNELS:
8479 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
8480 OMP_CLAUSES (expr));
8481 break;
8482 case OACC_PARALLEL:
8483 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
8484 OMP_CLAUSES (expr));
8485 break;
8486 case OMP_SECTIONS:
8487 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
8488 break;
8489 case OMP_SINGLE:
8490 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
8491 break;
8492 case OMP_TARGET:
8493 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
8494 OMP_CLAUSES (expr));
8495 break;
8496 case OMP_TARGET_DATA:
8497 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
8498 OMP_CLAUSES (expr));
8499 break;
8500 case OMP_TEAMS:
8501 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
8502 break;
8503 default:
8504 gcc_unreachable ();
8507 gimplify_seq_add_stmt (pre_p, stmt);
8508 *expr_p = NULL_TREE;
8511 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
8512 target update constructs. */
8514 static void
8515 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
8517 tree expr = *expr_p;
8518 int kind;
8519 gomp_target *stmt;
8521 switch (TREE_CODE (expr))
8523 case OACC_ENTER_DATA:
8524 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
8525 break;
8526 case OACC_EXIT_DATA:
8527 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
8528 break;
8529 case OACC_UPDATE:
8530 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
8531 break;
8532 case OMP_TARGET_UPDATE:
8533 kind = GF_OMP_TARGET_KIND_UPDATE;
8534 break;
8535 case OMP_TARGET_ENTER_DATA:
8536 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
8537 break;
8538 case OMP_TARGET_EXIT_DATA:
8539 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
8540 break;
8541 default:
8542 gcc_unreachable ();
8544 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
8545 ORT_WORKSHARE, TREE_CODE (expr));
8546 gimplify_adjust_omp_clauses (pre_p, &OMP_STANDALONE_CLAUSES (expr),
8547 TREE_CODE (expr));
8548 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
8550 gimplify_seq_add_stmt (pre_p, stmt);
8551 *expr_p = NULL_TREE;
8554 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
8555 stabilized the lhs of the atomic operation as *ADDR. Return true if
8556 EXPR is this stabilized form. */
8558 static bool
8559 goa_lhs_expr_p (tree expr, tree addr)
8561 /* Also include casts to other type variants. The C front end is fond
8562 of adding these for e.g. volatile variables. This is like
8563 STRIP_TYPE_NOPS but includes the main variant lookup. */
8564 STRIP_USELESS_TYPE_CONVERSION (expr);
8566 if (TREE_CODE (expr) == INDIRECT_REF)
8568 expr = TREE_OPERAND (expr, 0);
8569 while (expr != addr
8570 && (CONVERT_EXPR_P (expr)
8571 || TREE_CODE (expr) == NON_LVALUE_EXPR)
8572 && TREE_CODE (expr) == TREE_CODE (addr)
8573 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
8575 expr = TREE_OPERAND (expr, 0);
8576 addr = TREE_OPERAND (addr, 0);
8578 if (expr == addr)
8579 return true;
8580 return (TREE_CODE (addr) == ADDR_EXPR
8581 && TREE_CODE (expr) == ADDR_EXPR
8582 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
8584 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
8585 return true;
8586 return false;
8589 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
8590 expression does not involve the lhs, evaluate it into a temporary.
8591 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
8592 or -1 if an error was encountered. */
8594 static int
8595 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
8596 tree lhs_var)
8598 tree expr = *expr_p;
8599 int saw_lhs;
8601 if (goa_lhs_expr_p (expr, lhs_addr))
8603 *expr_p = lhs_var;
8604 return 1;
8606 if (is_gimple_val (expr))
8607 return 0;
8609 saw_lhs = 0;
8610 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
8612 case tcc_binary:
8613 case tcc_comparison:
8614 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
8615 lhs_var);
8616 case tcc_unary:
8617 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
8618 lhs_var);
8619 break;
8620 case tcc_expression:
8621 switch (TREE_CODE (expr))
8623 case TRUTH_ANDIF_EXPR:
8624 case TRUTH_ORIF_EXPR:
8625 case TRUTH_AND_EXPR:
8626 case TRUTH_OR_EXPR:
8627 case TRUTH_XOR_EXPR:
8628 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
8629 lhs_addr, lhs_var);
8630 case TRUTH_NOT_EXPR:
8631 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
8632 lhs_addr, lhs_var);
8633 break;
8634 case COMPOUND_EXPR:
8635 /* Break out any preevaluations from cp_build_modify_expr. */
8636 for (; TREE_CODE (expr) == COMPOUND_EXPR;
8637 expr = TREE_OPERAND (expr, 1))
8638 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
8639 *expr_p = expr;
8640 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
8641 default:
8642 break;
8644 break;
8645 default:
8646 break;
8649 if (saw_lhs == 0)
8651 enum gimplify_status gs;
8652 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
8653 if (gs != GS_ALL_DONE)
8654 saw_lhs = -1;
8657 return saw_lhs;
8660 /* Gimplify an OMP_ATOMIC statement. */
8662 static enum gimplify_status
8663 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
8665 tree addr = TREE_OPERAND (*expr_p, 0);
8666 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
8667 ? NULL : TREE_OPERAND (*expr_p, 1);
8668 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
8669 tree tmp_load;
8670 gomp_atomic_load *loadstmt;
8671 gomp_atomic_store *storestmt;
8673 tmp_load = create_tmp_reg (type);
8674 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
8675 return GS_ERROR;
8677 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
8678 != GS_ALL_DONE)
8679 return GS_ERROR;
8681 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
8682 gimplify_seq_add_stmt (pre_p, loadstmt);
8683 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
8684 != GS_ALL_DONE)
8685 return GS_ERROR;
8687 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
8688 rhs = tmp_load;
8689 storestmt = gimple_build_omp_atomic_store (rhs);
8690 gimplify_seq_add_stmt (pre_p, storestmt);
8691 if (OMP_ATOMIC_SEQ_CST (*expr_p))
8693 gimple_omp_atomic_set_seq_cst (loadstmt);
8694 gimple_omp_atomic_set_seq_cst (storestmt);
8696 switch (TREE_CODE (*expr_p))
8698 case OMP_ATOMIC_READ:
8699 case OMP_ATOMIC_CAPTURE_OLD:
8700 *expr_p = tmp_load;
8701 gimple_omp_atomic_set_need_value (loadstmt);
8702 break;
8703 case OMP_ATOMIC_CAPTURE_NEW:
8704 *expr_p = rhs;
8705 gimple_omp_atomic_set_need_value (storestmt);
8706 break;
8707 default:
8708 *expr_p = NULL;
8709 break;
8712 return GS_ALL_DONE;
8715 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
8716 body, and adding some EH bits. */
8718 static enum gimplify_status
8719 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
8721 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
8722 gimple *body_stmt;
8723 gtransaction *trans_stmt;
8724 gimple_seq body = NULL;
8725 int subcode = 0;
8727 /* Wrap the transaction body in a BIND_EXPR so we have a context
8728 where to put decls for OMP. */
8729 if (TREE_CODE (tbody) != BIND_EXPR)
8731 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
8732 TREE_SIDE_EFFECTS (bind) = 1;
8733 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
8734 TRANSACTION_EXPR_BODY (expr) = bind;
8737 push_gimplify_context ();
8738 temp = voidify_wrapper_expr (*expr_p, NULL);
8740 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
8741 pop_gimplify_context (body_stmt);
8743 trans_stmt = gimple_build_transaction (body, NULL);
8744 if (TRANSACTION_EXPR_OUTER (expr))
8745 subcode = GTMA_IS_OUTER;
8746 else if (TRANSACTION_EXPR_RELAXED (expr))
8747 subcode = GTMA_IS_RELAXED;
8748 gimple_transaction_set_subcode (trans_stmt, subcode);
8750 gimplify_seq_add_stmt (pre_p, trans_stmt);
8752 if (temp)
8754 *expr_p = temp;
8755 return GS_OK;
8758 *expr_p = NULL_TREE;
8759 return GS_ALL_DONE;
8762 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
8763 is the OMP_BODY of the original EXPR (which has already been
8764 gimplified so it's not present in the EXPR).
8766 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
8768 static gimple *
8769 gimplify_omp_ordered (tree expr, gimple_seq body)
8771 tree c, decls;
8772 int failures = 0;
8773 unsigned int i;
8774 tree source_c = NULL_TREE;
8775 tree sink_c = NULL_TREE;
8777 if (gimplify_omp_ctxp)
8778 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
8779 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8780 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
8781 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
8782 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
8784 error_at (OMP_CLAUSE_LOCATION (c),
8785 "%<depend%> clause must be closely nested "
8786 "inside a loop with %<ordered%> clause with "
8787 "a parameter");
8788 failures++;
8790 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8791 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8793 bool fail = false;
8794 for (decls = OMP_CLAUSE_DECL (c), i = 0;
8795 decls && TREE_CODE (decls) == TREE_LIST;
8796 decls = TREE_CHAIN (decls), ++i)
8797 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
8798 continue;
8799 else if (TREE_VALUE (decls)
8800 != gimplify_omp_ctxp->loop_iter_var[2 * i])
8802 error_at (OMP_CLAUSE_LOCATION (c),
8803 "variable %qE is not an iteration "
8804 "of outermost loop %d, expected %qE",
8805 TREE_VALUE (decls), i + 1,
8806 gimplify_omp_ctxp->loop_iter_var[2 * i]);
8807 fail = true;
8808 failures++;
8810 else
8811 TREE_VALUE (decls)
8812 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
8813 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
8815 error_at (OMP_CLAUSE_LOCATION (c),
8816 "number of variables in %<depend(sink)%> "
8817 "clause does not match number of "
8818 "iteration variables");
8819 failures++;
8821 sink_c = c;
8823 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8824 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8826 if (source_c)
8828 error_at (OMP_CLAUSE_LOCATION (c),
8829 "more than one %<depend(source)%> clause on an "
8830 "%<ordered%> construct");
8831 failures++;
8833 else
8834 source_c = c;
8836 if (source_c && sink_c)
8838 error_at (OMP_CLAUSE_LOCATION (source_c),
8839 "%<depend(source)%> clause specified together with "
8840 "%<depend(sink:)%> clauses on the same construct");
8841 failures++;
8844 if (failures)
8845 return gimple_build_nop ();
8846 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
8849 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
8850 expression produces a value to be used as an operand inside a GIMPLE
8851 statement, the value will be stored back in *EXPR_P. This value will
8852 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
8853 an SSA_NAME. The corresponding sequence of GIMPLE statements is
8854 emitted in PRE_P and POST_P.
8856 Additionally, this process may overwrite parts of the input
8857 expression during gimplification. Ideally, it should be
8858 possible to do non-destructive gimplification.
8860 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
8861 the expression needs to evaluate to a value to be used as
8862 an operand in a GIMPLE statement, this value will be stored in
8863 *EXPR_P on exit. This happens when the caller specifies one
8864 of fb_lvalue or fb_rvalue fallback flags.
8866 PRE_P will contain the sequence of GIMPLE statements corresponding
8867 to the evaluation of EXPR and all the side-effects that must
8868 be executed before the main expression. On exit, the last
8869 statement of PRE_P is the core statement being gimplified. For
8870 instance, when gimplifying 'if (++a)' the last statement in
8871 PRE_P will be 'if (t.1)' where t.1 is the result of
8872 pre-incrementing 'a'.
8874 POST_P will contain the sequence of GIMPLE statements corresponding
8875 to the evaluation of all the side-effects that must be executed
8876 after the main expression. If this is NULL, the post
8877 side-effects are stored at the end of PRE_P.
8879 The reason why the output is split in two is to handle post
8880 side-effects explicitly. In some cases, an expression may have
8881 inner and outer post side-effects which need to be emitted in
8882 an order different from the one given by the recursive
8883 traversal. For instance, for the expression (*p--)++ the post
8884 side-effects of '--' must actually occur *after* the post
8885 side-effects of '++'. However, gimplification will first visit
8886 the inner expression, so if a separate POST sequence was not
8887 used, the resulting sequence would be:
8889 1 t.1 = *p
8890 2 p = p - 1
8891 3 t.2 = t.1 + 1
8892 4 *p = t.2
8894 However, the post-decrement operation in line #2 must not be
8895 evaluated until after the store to *p at line #4, so the
8896 correct sequence should be:
8898 1 t.1 = *p
8899 2 t.2 = t.1 + 1
8900 3 *p = t.2
8901 4 p = p - 1
8903 So, by specifying a separate post queue, it is possible
8904 to emit the post side-effects in the correct order.
8905 If POST_P is NULL, an internal queue will be used. Before
8906 returning to the caller, the sequence POST_P is appended to
8907 the main output sequence PRE_P.
8909 GIMPLE_TEST_F points to a function that takes a tree T and
8910 returns nonzero if T is in the GIMPLE form requested by the
8911 caller. The GIMPLE predicates are in gimple.c.
8913 FALLBACK tells the function what sort of a temporary we want if
8914 gimplification cannot produce an expression that complies with
8915 GIMPLE_TEST_F.
8917 fb_none means that no temporary should be generated
8918 fb_rvalue means that an rvalue is OK to generate
8919 fb_lvalue means that an lvalue is OK to generate
8920 fb_either means that either is OK, but an lvalue is preferable.
8921 fb_mayfail means that gimplification may fail (in which case
8922 GS_ERROR will be returned)
8924 The return value is either GS_ERROR or GS_ALL_DONE, since this
8925 function iterates until EXPR is completely gimplified or an error
8926 occurs. */
8928 enum gimplify_status
8929 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
8930 bool (*gimple_test_f) (tree), fallback_t fallback)
8932 tree tmp;
8933 gimple_seq internal_pre = NULL;
8934 gimple_seq internal_post = NULL;
8935 tree save_expr;
8936 bool is_statement;
8937 location_t saved_location;
8938 enum gimplify_status ret;
8939 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
8941 save_expr = *expr_p;
8942 if (save_expr == NULL_TREE)
8943 return GS_ALL_DONE;
8945 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
8946 is_statement = gimple_test_f == is_gimple_stmt;
8947 if (is_statement)
8948 gcc_assert (pre_p);
8950 /* Consistency checks. */
8951 if (gimple_test_f == is_gimple_reg)
8952 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
8953 else if (gimple_test_f == is_gimple_val
8954 || gimple_test_f == is_gimple_call_addr
8955 || gimple_test_f == is_gimple_condexpr
8956 || gimple_test_f == is_gimple_mem_rhs
8957 || gimple_test_f == is_gimple_mem_rhs_or_call
8958 || gimple_test_f == is_gimple_reg_rhs
8959 || gimple_test_f == is_gimple_reg_rhs_or_call
8960 || gimple_test_f == is_gimple_asm_val
8961 || gimple_test_f == is_gimple_mem_ref_addr)
8962 gcc_assert (fallback & fb_rvalue);
8963 else if (gimple_test_f == is_gimple_min_lval
8964 || gimple_test_f == is_gimple_lvalue)
8965 gcc_assert (fallback & fb_lvalue);
8966 else if (gimple_test_f == is_gimple_addressable)
8967 gcc_assert (fallback & fb_either);
8968 else if (gimple_test_f == is_gimple_stmt)
8969 gcc_assert (fallback == fb_none);
8970 else
8972 /* We should have recognized the GIMPLE_TEST_F predicate to
8973 know what kind of fallback to use in case a temporary is
8974 needed to hold the value or address of *EXPR_P. */
8975 gcc_unreachable ();
8978 /* We used to check the predicate here and return immediately if it
8979 succeeds. This is wrong; the design is for gimplification to be
8980 idempotent, and for the predicates to only test for valid forms, not
8981 whether they are fully simplified. */
8982 if (pre_p == NULL)
8983 pre_p = &internal_pre;
8985 if (post_p == NULL)
8986 post_p = &internal_post;
8988 /* Remember the last statements added to PRE_P and POST_P. Every
8989 new statement added by the gimplification helpers needs to be
8990 annotated with location information. To centralize the
8991 responsibility, we remember the last statement that had been
8992 added to both queues before gimplifying *EXPR_P. If
8993 gimplification produces new statements in PRE_P and POST_P, those
8994 statements will be annotated with the same location information
8995 as *EXPR_P. */
8996 pre_last_gsi = gsi_last (*pre_p);
8997 post_last_gsi = gsi_last (*post_p);
8999 saved_location = input_location;
9000 if (save_expr != error_mark_node
9001 && EXPR_HAS_LOCATION (*expr_p))
9002 input_location = EXPR_LOCATION (*expr_p);
9004 /* Loop over the specific gimplifiers until the toplevel node
9005 remains the same. */
9008 /* Strip away as many useless type conversions as possible
9009 at the toplevel. */
9010 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
9012 /* Remember the expr. */
9013 save_expr = *expr_p;
9015 /* Die, die, die, my darling. */
9016 if (save_expr == error_mark_node
9017 || (TREE_TYPE (save_expr)
9018 && TREE_TYPE (save_expr) == error_mark_node))
9020 ret = GS_ERROR;
9021 break;
9024 /* Do any language-specific gimplification. */
9025 ret = ((enum gimplify_status)
9026 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
9027 if (ret == GS_OK)
9029 if (*expr_p == NULL_TREE)
9030 break;
9031 if (*expr_p != save_expr)
9032 continue;
9034 else if (ret != GS_UNHANDLED)
9035 break;
9037 /* Make sure that all the cases set 'ret' appropriately. */
9038 ret = GS_UNHANDLED;
9039 switch (TREE_CODE (*expr_p))
9041 /* First deal with the special cases. */
9043 case POSTINCREMENT_EXPR:
9044 case POSTDECREMENT_EXPR:
9045 case PREINCREMENT_EXPR:
9046 case PREDECREMENT_EXPR:
9047 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
9048 fallback != fb_none,
9049 TREE_TYPE (*expr_p));
9050 break;
9052 case VIEW_CONVERT_EXPR:
9053 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
9054 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
9056 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9057 post_p, is_gimple_val, fb_rvalue);
9058 recalculate_side_effects (*expr_p);
9059 break;
9061 /* Fallthru. */
9063 case ARRAY_REF:
9064 case ARRAY_RANGE_REF:
9065 case REALPART_EXPR:
9066 case IMAGPART_EXPR:
9067 case COMPONENT_REF:
9068 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
9069 fallback ? fallback : fb_rvalue);
9070 break;
9072 case COND_EXPR:
9073 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
9075 /* C99 code may assign to an array in a structure value of a
9076 conditional expression, and this has undefined behavior
9077 only on execution, so create a temporary if an lvalue is
9078 required. */
9079 if (fallback == fb_lvalue)
9081 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
9082 mark_addressable (*expr_p);
9083 ret = GS_OK;
9085 break;
9087 case CALL_EXPR:
9088 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
9090 /* C99 code may assign to an array in a structure returned
9091 from a function, and this has undefined behavior only on
9092 execution, so create a temporary if an lvalue is
9093 required. */
9094 if (fallback == fb_lvalue)
9096 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
9097 mark_addressable (*expr_p);
9098 ret = GS_OK;
9100 break;
9102 case TREE_LIST:
9103 gcc_unreachable ();
9105 case COMPOUND_EXPR:
9106 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
9107 break;
9109 case COMPOUND_LITERAL_EXPR:
9110 ret = gimplify_compound_literal_expr (expr_p, pre_p,
9111 gimple_test_f, fallback);
9112 break;
9114 case MODIFY_EXPR:
9115 case INIT_EXPR:
9116 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
9117 fallback != fb_none);
9118 break;
9120 case TRUTH_ANDIF_EXPR:
9121 case TRUTH_ORIF_EXPR:
9123 /* Preserve the original type of the expression and the
9124 source location of the outer expression. */
9125 tree org_type = TREE_TYPE (*expr_p);
9126 *expr_p = gimple_boolify (*expr_p);
9127 *expr_p = build3_loc (input_location, COND_EXPR,
9128 org_type, *expr_p,
9129 fold_convert_loc
9130 (input_location,
9131 org_type, boolean_true_node),
9132 fold_convert_loc
9133 (input_location,
9134 org_type, boolean_false_node));
9135 ret = GS_OK;
9136 break;
9139 case TRUTH_NOT_EXPR:
9141 tree type = TREE_TYPE (*expr_p);
9142 /* The parsers are careful to generate TRUTH_NOT_EXPR
9143 only with operands that are always zero or one.
9144 We do not fold here but handle the only interesting case
9145 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
9146 *expr_p = gimple_boolify (*expr_p);
9147 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
9148 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
9149 TREE_TYPE (*expr_p),
9150 TREE_OPERAND (*expr_p, 0));
9151 else
9152 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
9153 TREE_TYPE (*expr_p),
9154 TREE_OPERAND (*expr_p, 0),
9155 build_int_cst (TREE_TYPE (*expr_p), 1));
9156 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
9157 *expr_p = fold_convert_loc (input_location, type, *expr_p);
9158 ret = GS_OK;
9159 break;
9162 case ADDR_EXPR:
9163 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
9164 break;
9166 case ANNOTATE_EXPR:
9168 tree cond = TREE_OPERAND (*expr_p, 0);
9169 tree kind = TREE_OPERAND (*expr_p, 1);
9170 tree type = TREE_TYPE (cond);
9171 if (!INTEGRAL_TYPE_P (type))
9173 *expr_p = cond;
9174 ret = GS_OK;
9175 break;
9177 tree tmp = create_tmp_var (type);
9178 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
9179 gcall *call
9180 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
9181 gimple_call_set_lhs (call, tmp);
9182 gimplify_seq_add_stmt (pre_p, call);
9183 *expr_p = tmp;
9184 ret = GS_ALL_DONE;
9185 break;
9188 case VA_ARG_EXPR:
9189 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
9190 break;
9192 CASE_CONVERT:
9193 if (IS_EMPTY_STMT (*expr_p))
9195 ret = GS_ALL_DONE;
9196 break;
9199 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
9200 || fallback == fb_none)
9202 /* Just strip a conversion to void (or in void context) and
9203 try again. */
9204 *expr_p = TREE_OPERAND (*expr_p, 0);
9205 ret = GS_OK;
9206 break;
9209 ret = gimplify_conversion (expr_p);
9210 if (ret == GS_ERROR)
9211 break;
9212 if (*expr_p != save_expr)
9213 break;
9214 /* FALLTHRU */
9216 case FIX_TRUNC_EXPR:
9217 /* unary_expr: ... | '(' cast ')' val | ... */
9218 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9219 is_gimple_val, fb_rvalue);
9220 recalculate_side_effects (*expr_p);
9221 break;
9223 case INDIRECT_REF:
9225 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
9226 bool notrap = TREE_THIS_NOTRAP (*expr_p);
9227 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
9229 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
9230 if (*expr_p != save_expr)
9232 ret = GS_OK;
9233 break;
9236 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9237 is_gimple_reg, fb_rvalue);
9238 if (ret == GS_ERROR)
9239 break;
9241 recalculate_side_effects (*expr_p);
9242 *expr_p = fold_build2_loc (input_location, MEM_REF,
9243 TREE_TYPE (*expr_p),
9244 TREE_OPERAND (*expr_p, 0),
9245 build_int_cst (saved_ptr_type, 0));
9246 TREE_THIS_VOLATILE (*expr_p) = volatilep;
9247 TREE_THIS_NOTRAP (*expr_p) = notrap;
9248 ret = GS_OK;
9249 break;
9252 /* We arrive here through the various re-gimplifcation paths. */
9253 case MEM_REF:
9254 /* First try re-folding the whole thing. */
9255 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
9256 TREE_OPERAND (*expr_p, 0),
9257 TREE_OPERAND (*expr_p, 1));
9258 if (tmp)
9260 *expr_p = tmp;
9261 recalculate_side_effects (*expr_p);
9262 ret = GS_OK;
9263 break;
9265 /* Avoid re-gimplifying the address operand if it is already
9266 in suitable form. Re-gimplifying would mark the address
9267 operand addressable. Always gimplify when not in SSA form
9268 as we still may have to gimplify decls with value-exprs. */
9269 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
9270 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
9272 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9273 is_gimple_mem_ref_addr, fb_rvalue);
9274 if (ret == GS_ERROR)
9275 break;
9277 recalculate_side_effects (*expr_p);
9278 ret = GS_ALL_DONE;
9279 break;
9281 /* Constants need not be gimplified. */
9282 case INTEGER_CST:
9283 case REAL_CST:
9284 case FIXED_CST:
9285 case STRING_CST:
9286 case COMPLEX_CST:
9287 case VECTOR_CST:
9288 /* Drop the overflow flag on constants, we do not want
9289 that in the GIMPLE IL. */
9290 if (TREE_OVERFLOW_P (*expr_p))
9291 *expr_p = drop_tree_overflow (*expr_p);
9292 ret = GS_ALL_DONE;
9293 break;
9295 case CONST_DECL:
9296 /* If we require an lvalue, such as for ADDR_EXPR, retain the
9297 CONST_DECL node. Otherwise the decl is replaceable by its
9298 value. */
9299 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
9300 if (fallback & fb_lvalue)
9301 ret = GS_ALL_DONE;
9302 else
9304 *expr_p = DECL_INITIAL (*expr_p);
9305 ret = GS_OK;
9307 break;
9309 case DECL_EXPR:
9310 ret = gimplify_decl_expr (expr_p, pre_p);
9311 break;
9313 case BIND_EXPR:
9314 ret = gimplify_bind_expr (expr_p, pre_p);
9315 break;
9317 case LOOP_EXPR:
9318 ret = gimplify_loop_expr (expr_p, pre_p);
9319 break;
9321 case SWITCH_EXPR:
9322 ret = gimplify_switch_expr (expr_p, pre_p);
9323 break;
9325 case EXIT_EXPR:
9326 ret = gimplify_exit_expr (expr_p);
9327 break;
9329 case GOTO_EXPR:
9330 /* If the target is not LABEL, then it is a computed jump
9331 and the target needs to be gimplified. */
9332 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
9334 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
9335 NULL, is_gimple_val, fb_rvalue);
9336 if (ret == GS_ERROR)
9337 break;
9339 gimplify_seq_add_stmt (pre_p,
9340 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
9341 ret = GS_ALL_DONE;
9342 break;
9344 case PREDICT_EXPR:
9345 gimplify_seq_add_stmt (pre_p,
9346 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
9347 PREDICT_EXPR_OUTCOME (*expr_p)));
9348 ret = GS_ALL_DONE;
9349 break;
9351 case LABEL_EXPR:
9352 ret = GS_ALL_DONE;
9353 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
9354 == current_function_decl);
9355 gimplify_seq_add_stmt (pre_p,
9356 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
9357 break;
9359 case CASE_LABEL_EXPR:
9360 ret = gimplify_case_label_expr (expr_p, pre_p);
9361 break;
9363 case RETURN_EXPR:
9364 ret = gimplify_return_expr (*expr_p, pre_p);
9365 break;
9367 case CONSTRUCTOR:
9368 /* Don't reduce this in place; let gimplify_init_constructor work its
9369 magic. Buf if we're just elaborating this for side effects, just
9370 gimplify any element that has side-effects. */
9371 if (fallback == fb_none)
9373 unsigned HOST_WIDE_INT ix;
9374 tree val;
9375 tree temp = NULL_TREE;
9376 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
9377 if (TREE_SIDE_EFFECTS (val))
9378 append_to_statement_list (val, &temp);
9380 *expr_p = temp;
9381 ret = temp ? GS_OK : GS_ALL_DONE;
9383 /* C99 code may assign to an array in a constructed
9384 structure or union, and this has undefined behavior only
9385 on execution, so create a temporary if an lvalue is
9386 required. */
9387 else if (fallback == fb_lvalue)
9389 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
9390 mark_addressable (*expr_p);
9391 ret = GS_OK;
9393 else
9394 ret = GS_ALL_DONE;
9395 break;
9397 /* The following are special cases that are not handled by the
9398 original GIMPLE grammar. */
9400 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
9401 eliminated. */
9402 case SAVE_EXPR:
9403 ret = gimplify_save_expr (expr_p, pre_p, post_p);
9404 break;
9406 case BIT_FIELD_REF:
9407 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9408 post_p, is_gimple_lvalue, fb_either);
9409 recalculate_side_effects (*expr_p);
9410 break;
9412 case TARGET_MEM_REF:
9414 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
9416 if (TMR_BASE (*expr_p))
9417 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
9418 post_p, is_gimple_mem_ref_addr, fb_either);
9419 if (TMR_INDEX (*expr_p))
9420 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
9421 post_p, is_gimple_val, fb_rvalue);
9422 if (TMR_INDEX2 (*expr_p))
9423 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
9424 post_p, is_gimple_val, fb_rvalue);
9425 /* TMR_STEP and TMR_OFFSET are always integer constants. */
9426 ret = MIN (r0, r1);
9428 break;
9430 case NON_LVALUE_EXPR:
9431 /* This should have been stripped above. */
9432 gcc_unreachable ();
9434 case ASM_EXPR:
9435 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
9436 break;
9438 case TRY_FINALLY_EXPR:
9439 case TRY_CATCH_EXPR:
9441 gimple_seq eval, cleanup;
9442 gtry *try_;
9444 /* Calls to destructors are generated automatically in FINALLY/CATCH
9445 block. They should have location as UNKNOWN_LOCATION. However,
9446 gimplify_call_expr will reset these call stmts to input_location
9447 if it finds stmt's location is unknown. To prevent resetting for
9448 destructors, we set the input_location to unknown.
9449 Note that this only affects the destructor calls in FINALLY/CATCH
9450 block, and will automatically reset to its original value by the
9451 end of gimplify_expr. */
9452 input_location = UNKNOWN_LOCATION;
9453 eval = cleanup = NULL;
9454 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
9455 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
9456 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
9457 if (gimple_seq_empty_p (cleanup))
9459 gimple_seq_add_seq (pre_p, eval);
9460 ret = GS_ALL_DONE;
9461 break;
9463 try_ = gimple_build_try (eval, cleanup,
9464 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
9465 ? GIMPLE_TRY_FINALLY
9466 : GIMPLE_TRY_CATCH);
9467 if (EXPR_HAS_LOCATION (save_expr))
9468 gimple_set_location (try_, EXPR_LOCATION (save_expr));
9469 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
9470 gimple_set_location (try_, saved_location);
9471 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
9472 gimple_try_set_catch_is_cleanup (try_,
9473 TRY_CATCH_IS_CLEANUP (*expr_p));
9474 gimplify_seq_add_stmt (pre_p, try_);
9475 ret = GS_ALL_DONE;
9476 break;
9479 case CLEANUP_POINT_EXPR:
9480 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
9481 break;
9483 case TARGET_EXPR:
9484 ret = gimplify_target_expr (expr_p, pre_p, post_p);
9485 break;
9487 case CATCH_EXPR:
9489 gimple *c;
9490 gimple_seq handler = NULL;
9491 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
9492 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
9493 gimplify_seq_add_stmt (pre_p, c);
9494 ret = GS_ALL_DONE;
9495 break;
9498 case EH_FILTER_EXPR:
9500 gimple *ehf;
9501 gimple_seq failure = NULL;
9503 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
9504 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
9505 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
9506 gimplify_seq_add_stmt (pre_p, ehf);
9507 ret = GS_ALL_DONE;
9508 break;
9511 case OBJ_TYPE_REF:
9513 enum gimplify_status r0, r1;
9514 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
9515 post_p, is_gimple_val, fb_rvalue);
9516 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
9517 post_p, is_gimple_val, fb_rvalue);
9518 TREE_SIDE_EFFECTS (*expr_p) = 0;
9519 ret = MIN (r0, r1);
9521 break;
9523 case LABEL_DECL:
9524 /* We get here when taking the address of a label. We mark
9525 the label as "forced"; meaning it can never be removed and
9526 it is a potential target for any computed goto. */
9527 FORCED_LABEL (*expr_p) = 1;
9528 ret = GS_ALL_DONE;
9529 break;
9531 case STATEMENT_LIST:
9532 ret = gimplify_statement_list (expr_p, pre_p);
9533 break;
9535 case WITH_SIZE_EXPR:
9537 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9538 post_p == &internal_post ? NULL : post_p,
9539 gimple_test_f, fallback);
9540 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
9541 is_gimple_val, fb_rvalue);
9542 ret = GS_ALL_DONE;
9544 break;
9546 case VAR_DECL:
9547 case PARM_DECL:
9548 ret = gimplify_var_or_parm_decl (expr_p);
9549 break;
9551 case RESULT_DECL:
9552 /* When within an OMP context, notice uses of variables. */
9553 if (gimplify_omp_ctxp)
9554 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
9555 ret = GS_ALL_DONE;
9556 break;
9558 case SSA_NAME:
9559 /* Allow callbacks into the gimplifier during optimization. */
9560 ret = GS_ALL_DONE;
9561 break;
9563 case OMP_PARALLEL:
9564 gimplify_omp_parallel (expr_p, pre_p);
9565 ret = GS_ALL_DONE;
9566 break;
9568 case OMP_TASK:
9569 gimplify_omp_task (expr_p, pre_p);
9570 ret = GS_ALL_DONE;
9571 break;
9573 case OMP_FOR:
9574 case OMP_SIMD:
9575 case CILK_SIMD:
9576 case CILK_FOR:
9577 case OMP_DISTRIBUTE:
9578 case OMP_TASKLOOP:
9579 case OACC_LOOP:
9580 ret = gimplify_omp_for (expr_p, pre_p);
9581 break;
9583 case OACC_CACHE:
9584 gimplify_oacc_cache (expr_p, pre_p);
9585 ret = GS_ALL_DONE;
9586 break;
9588 case OACC_HOST_DATA:
9589 case OACC_DECLARE:
9590 sorry ("directive not yet implemented");
9591 ret = GS_ALL_DONE;
9592 break;
9594 case OACC_DATA:
9595 case OACC_KERNELS:
9596 case OACC_PARALLEL:
9597 case OMP_SECTIONS:
9598 case OMP_SINGLE:
9599 case OMP_TARGET:
9600 case OMP_TARGET_DATA:
9601 case OMP_TEAMS:
9602 gimplify_omp_workshare (expr_p, pre_p);
9603 ret = GS_ALL_DONE;
9604 break;
9606 case OACC_ENTER_DATA:
9607 case OACC_EXIT_DATA:
9608 case OACC_UPDATE:
9609 case OMP_TARGET_UPDATE:
9610 case OMP_TARGET_ENTER_DATA:
9611 case OMP_TARGET_EXIT_DATA:
9612 gimplify_omp_target_update (expr_p, pre_p);
9613 ret = GS_ALL_DONE;
9614 break;
9616 case OMP_SECTION:
9617 case OMP_MASTER:
9618 case OMP_TASKGROUP:
9619 case OMP_ORDERED:
9620 case OMP_CRITICAL:
9622 gimple_seq body = NULL;
9623 gimple *g;
9625 gimplify_and_add (OMP_BODY (*expr_p), &body);
9626 switch (TREE_CODE (*expr_p))
9628 case OMP_SECTION:
9629 g = gimple_build_omp_section (body);
9630 break;
9631 case OMP_MASTER:
9632 g = gimple_build_omp_master (body);
9633 break;
9634 case OMP_TASKGROUP:
9636 gimple_seq cleanup = NULL;
9637 tree fn
9638 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
9639 g = gimple_build_call (fn, 0);
9640 gimple_seq_add_stmt (&cleanup, g);
9641 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
9642 body = NULL;
9643 gimple_seq_add_stmt (&body, g);
9644 g = gimple_build_omp_taskgroup (body);
9646 break;
9647 case OMP_ORDERED:
9648 g = gimplify_omp_ordered (*expr_p, body);
9649 break;
9650 case OMP_CRITICAL:
9651 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
9652 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
9653 gimplify_adjust_omp_clauses (pre_p,
9654 &OMP_CRITICAL_CLAUSES (*expr_p),
9655 OMP_CRITICAL);
9656 g = gimple_build_omp_critical (body,
9657 OMP_CRITICAL_NAME (*expr_p),
9658 OMP_CRITICAL_CLAUSES (*expr_p));
9659 break;
9660 default:
9661 gcc_unreachable ();
9663 gimplify_seq_add_stmt (pre_p, g);
9664 ret = GS_ALL_DONE;
9665 break;
9668 case OMP_ATOMIC:
9669 case OMP_ATOMIC_READ:
9670 case OMP_ATOMIC_CAPTURE_OLD:
9671 case OMP_ATOMIC_CAPTURE_NEW:
9672 ret = gimplify_omp_atomic (expr_p, pre_p);
9673 break;
9675 case TRANSACTION_EXPR:
9676 ret = gimplify_transaction (expr_p, pre_p);
9677 break;
9679 case TRUTH_AND_EXPR:
9680 case TRUTH_OR_EXPR:
9681 case TRUTH_XOR_EXPR:
9683 tree orig_type = TREE_TYPE (*expr_p);
9684 tree new_type, xop0, xop1;
9685 *expr_p = gimple_boolify (*expr_p);
9686 new_type = TREE_TYPE (*expr_p);
9687 if (!useless_type_conversion_p (orig_type, new_type))
9689 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
9690 ret = GS_OK;
9691 break;
9694 /* Boolified binary truth expressions are semantically equivalent
9695 to bitwise binary expressions. Canonicalize them to the
9696 bitwise variant. */
9697 switch (TREE_CODE (*expr_p))
9699 case TRUTH_AND_EXPR:
9700 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
9701 break;
9702 case TRUTH_OR_EXPR:
9703 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
9704 break;
9705 case TRUTH_XOR_EXPR:
9706 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
9707 break;
9708 default:
9709 break;
9711 /* Now make sure that operands have compatible type to
9712 expression's new_type. */
9713 xop0 = TREE_OPERAND (*expr_p, 0);
9714 xop1 = TREE_OPERAND (*expr_p, 1);
9715 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
9716 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
9717 new_type,
9718 xop0);
9719 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
9720 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
9721 new_type,
9722 xop1);
9723 /* Continue classified as tcc_binary. */
9724 goto expr_2;
9727 case FMA_EXPR:
9728 case VEC_COND_EXPR:
9729 case VEC_PERM_EXPR:
9730 /* Classified as tcc_expression. */
9731 goto expr_3;
9733 case POINTER_PLUS_EXPR:
9735 enum gimplify_status r0, r1;
9736 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9737 post_p, is_gimple_val, fb_rvalue);
9738 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
9739 post_p, is_gimple_val, fb_rvalue);
9740 recalculate_side_effects (*expr_p);
9741 ret = MIN (r0, r1);
9742 break;
9745 case CILK_SYNC_STMT:
9747 if (!fn_contains_cilk_spawn_p (cfun))
9749 error_at (EXPR_LOCATION (*expr_p),
9750 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
9751 ret = GS_ERROR;
9753 else
9755 gimplify_cilk_sync (expr_p, pre_p);
9756 ret = GS_ALL_DONE;
9758 break;
9761 default:
9762 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
9764 case tcc_comparison:
9765 /* Handle comparison of objects of non scalar mode aggregates
9766 with a call to memcmp. It would be nice to only have to do
9767 this for variable-sized objects, but then we'd have to allow
9768 the same nest of reference nodes we allow for MODIFY_EXPR and
9769 that's too complex.
9771 Compare scalar mode aggregates as scalar mode values. Using
9772 memcmp for them would be very inefficient at best, and is
9773 plain wrong if bitfields are involved. */
9775 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
9777 /* Vector comparisons need no boolification. */
9778 if (TREE_CODE (type) == VECTOR_TYPE)
9779 goto expr_2;
9780 else if (!AGGREGATE_TYPE_P (type))
9782 tree org_type = TREE_TYPE (*expr_p);
9783 *expr_p = gimple_boolify (*expr_p);
9784 if (!useless_type_conversion_p (org_type,
9785 TREE_TYPE (*expr_p)))
9787 *expr_p = fold_convert_loc (input_location,
9788 org_type, *expr_p);
9789 ret = GS_OK;
9791 else
9792 goto expr_2;
9794 else if (TYPE_MODE (type) != BLKmode)
9795 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
9796 else
9797 ret = gimplify_variable_sized_compare (expr_p);
9799 break;
9802 /* If *EXPR_P does not need to be special-cased, handle it
9803 according to its class. */
9804 case tcc_unary:
9805 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9806 post_p, is_gimple_val, fb_rvalue);
9807 break;
9809 case tcc_binary:
9810 expr_2:
9812 enum gimplify_status r0, r1;
9814 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9815 post_p, is_gimple_val, fb_rvalue);
9816 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
9817 post_p, is_gimple_val, fb_rvalue);
9819 ret = MIN (r0, r1);
9820 break;
9823 expr_3:
9825 enum gimplify_status r0, r1, r2;
9827 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9828 post_p, is_gimple_val, fb_rvalue);
9829 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
9830 post_p, is_gimple_val, fb_rvalue);
9831 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
9832 post_p, is_gimple_val, fb_rvalue);
9834 ret = MIN (MIN (r0, r1), r2);
9835 break;
9838 case tcc_declaration:
9839 case tcc_constant:
9840 ret = GS_ALL_DONE;
9841 goto dont_recalculate;
9843 default:
9844 gcc_unreachable ();
9847 recalculate_side_effects (*expr_p);
9849 dont_recalculate:
9850 break;
9853 gcc_assert (*expr_p || ret != GS_OK);
9855 while (ret == GS_OK);
9857 /* If we encountered an error_mark somewhere nested inside, either
9858 stub out the statement or propagate the error back out. */
9859 if (ret == GS_ERROR)
9861 if (is_statement)
9862 *expr_p = NULL;
9863 goto out;
9866 /* This was only valid as a return value from the langhook, which
9867 we handled. Make sure it doesn't escape from any other context. */
9868 gcc_assert (ret != GS_UNHANDLED);
9870 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
9872 /* We aren't looking for a value, and we don't have a valid
9873 statement. If it doesn't have side-effects, throw it away. */
9874 if (!TREE_SIDE_EFFECTS (*expr_p))
9875 *expr_p = NULL;
9876 else if (!TREE_THIS_VOLATILE (*expr_p))
9878 /* This is probably a _REF that contains something nested that
9879 has side effects. Recurse through the operands to find it. */
9880 enum tree_code code = TREE_CODE (*expr_p);
9882 switch (code)
9884 case COMPONENT_REF:
9885 case REALPART_EXPR:
9886 case IMAGPART_EXPR:
9887 case VIEW_CONVERT_EXPR:
9888 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9889 gimple_test_f, fallback);
9890 break;
9892 case ARRAY_REF:
9893 case ARRAY_RANGE_REF:
9894 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9895 gimple_test_f, fallback);
9896 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
9897 gimple_test_f, fallback);
9898 break;
9900 default:
9901 /* Anything else with side-effects must be converted to
9902 a valid statement before we get here. */
9903 gcc_unreachable ();
9906 *expr_p = NULL;
9908 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
9909 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
9911 /* Historically, the compiler has treated a bare reference
9912 to a non-BLKmode volatile lvalue as forcing a load. */
9913 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
9915 /* Normally, we do not want to create a temporary for a
9916 TREE_ADDRESSABLE type because such a type should not be
9917 copied by bitwise-assignment. However, we make an
9918 exception here, as all we are doing here is ensuring that
9919 we read the bytes that make up the type. We use
9920 create_tmp_var_raw because create_tmp_var will abort when
9921 given a TREE_ADDRESSABLE type. */
9922 tree tmp = create_tmp_var_raw (type, "vol");
9923 gimple_add_tmp_var (tmp);
9924 gimplify_assign (tmp, *expr_p, pre_p);
9925 *expr_p = NULL;
9927 else
9928 /* We can't do anything useful with a volatile reference to
9929 an incomplete type, so just throw it away. Likewise for
9930 a BLKmode type, since any implicit inner load should
9931 already have been turned into an explicit one by the
9932 gimplification process. */
9933 *expr_p = NULL;
9936 /* If we are gimplifying at the statement level, we're done. Tack
9937 everything together and return. */
9938 if (fallback == fb_none || is_statement)
9940 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
9941 it out for GC to reclaim it. */
9942 *expr_p = NULL_TREE;
9944 if (!gimple_seq_empty_p (internal_pre)
9945 || !gimple_seq_empty_p (internal_post))
9947 gimplify_seq_add_seq (&internal_pre, internal_post);
9948 gimplify_seq_add_seq (pre_p, internal_pre);
9951 /* The result of gimplifying *EXPR_P is going to be the last few
9952 statements in *PRE_P and *POST_P. Add location information
9953 to all the statements that were added by the gimplification
9954 helpers. */
9955 if (!gimple_seq_empty_p (*pre_p))
9956 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
9958 if (!gimple_seq_empty_p (*post_p))
9959 annotate_all_with_location_after (*post_p, post_last_gsi,
9960 input_location);
9962 goto out;
9965 #ifdef ENABLE_GIMPLE_CHECKING
9966 if (*expr_p)
9968 enum tree_code code = TREE_CODE (*expr_p);
9969 /* These expressions should already be in gimple IR form. */
9970 gcc_assert (code != MODIFY_EXPR
9971 && code != ASM_EXPR
9972 && code != BIND_EXPR
9973 && code != CATCH_EXPR
9974 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
9975 && code != EH_FILTER_EXPR
9976 && code != GOTO_EXPR
9977 && code != LABEL_EXPR
9978 && code != LOOP_EXPR
9979 && code != SWITCH_EXPR
9980 && code != TRY_FINALLY_EXPR
9981 && code != OACC_PARALLEL
9982 && code != OACC_KERNELS
9983 && code != OACC_DATA
9984 && code != OACC_HOST_DATA
9985 && code != OACC_DECLARE
9986 && code != OACC_UPDATE
9987 && code != OACC_ENTER_DATA
9988 && code != OACC_EXIT_DATA
9989 && code != OACC_CACHE
9990 && code != OMP_CRITICAL
9991 && code != OMP_FOR
9992 && code != OACC_LOOP
9993 && code != OMP_MASTER
9994 && code != OMP_TASKGROUP
9995 && code != OMP_ORDERED
9996 && code != OMP_PARALLEL
9997 && code != OMP_SECTIONS
9998 && code != OMP_SECTION
9999 && code != OMP_SINGLE);
10001 #endif
10003 /* Otherwise we're gimplifying a subexpression, so the resulting
10004 value is interesting. If it's a valid operand that matches
10005 GIMPLE_TEST_F, we're done. Unless we are handling some
10006 post-effects internally; if that's the case, we need to copy into
10007 a temporary before adding the post-effects to POST_P. */
10008 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
10009 goto out;
10011 /* Otherwise, we need to create a new temporary for the gimplified
10012 expression. */
10014 /* We can't return an lvalue if we have an internal postqueue. The
10015 object the lvalue refers to would (probably) be modified by the
10016 postqueue; we need to copy the value out first, which means an
10017 rvalue. */
10018 if ((fallback & fb_lvalue)
10019 && gimple_seq_empty_p (internal_post)
10020 && is_gimple_addressable (*expr_p))
10022 /* An lvalue will do. Take the address of the expression, store it
10023 in a temporary, and replace the expression with an INDIRECT_REF of
10024 that temporary. */
10025 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
10026 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
10027 *expr_p = build_simple_mem_ref (tmp);
10029 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
10031 /* An rvalue will do. Assign the gimplified expression into a
10032 new temporary TMP and replace the original expression with
10033 TMP. First, make sure that the expression has a type so that
10034 it can be assigned into a temporary. */
10035 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
10036 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
10038 else
10040 #ifdef ENABLE_GIMPLE_CHECKING
10041 if (!(fallback & fb_mayfail))
10043 fprintf (stderr, "gimplification failed:\n");
10044 print_generic_expr (stderr, *expr_p, 0);
10045 debug_tree (*expr_p);
10046 internal_error ("gimplification failed");
10048 #endif
10049 gcc_assert (fallback & fb_mayfail);
10051 /* If this is an asm statement, and the user asked for the
10052 impossible, don't die. Fail and let gimplify_asm_expr
10053 issue an error. */
10054 ret = GS_ERROR;
10055 goto out;
10058 /* Make sure the temporary matches our predicate. */
10059 gcc_assert ((*gimple_test_f) (*expr_p));
10061 if (!gimple_seq_empty_p (internal_post))
10063 annotate_all_with_location (internal_post, input_location);
10064 gimplify_seq_add_seq (pre_p, internal_post);
10067 out:
10068 input_location = saved_location;
10069 return ret;
10072 /* Look through TYPE for variable-sized objects and gimplify each such
10073 size that we find. Add to LIST_P any statements generated. */
10075 void
10076 gimplify_type_sizes (tree type, gimple_seq *list_p)
10078 tree field, t;
10080 if (type == NULL || type == error_mark_node)
10081 return;
10083 /* We first do the main variant, then copy into any other variants. */
10084 type = TYPE_MAIN_VARIANT (type);
10086 /* Avoid infinite recursion. */
10087 if (TYPE_SIZES_GIMPLIFIED (type))
10088 return;
10090 TYPE_SIZES_GIMPLIFIED (type) = 1;
10092 switch (TREE_CODE (type))
10094 case INTEGER_TYPE:
10095 case ENUMERAL_TYPE:
10096 case BOOLEAN_TYPE:
10097 case REAL_TYPE:
10098 case FIXED_POINT_TYPE:
10099 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
10100 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
10102 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10104 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
10105 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
10107 break;
10109 case ARRAY_TYPE:
10110 /* These types may not have declarations, so handle them here. */
10111 gimplify_type_sizes (TREE_TYPE (type), list_p);
10112 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
10113 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
10114 with assigned stack slots, for -O1+ -g they should be tracked
10115 by VTA. */
10116 if (!(TYPE_NAME (type)
10117 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10118 && DECL_IGNORED_P (TYPE_NAME (type)))
10119 && TYPE_DOMAIN (type)
10120 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
10122 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
10123 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
10124 DECL_IGNORED_P (t) = 0;
10125 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
10126 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
10127 DECL_IGNORED_P (t) = 0;
10129 break;
10131 case RECORD_TYPE:
10132 case UNION_TYPE:
10133 case QUAL_UNION_TYPE:
10134 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10135 if (TREE_CODE (field) == FIELD_DECL)
10137 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
10138 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
10139 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
10140 gimplify_type_sizes (TREE_TYPE (field), list_p);
10142 break;
10144 case POINTER_TYPE:
10145 case REFERENCE_TYPE:
10146 /* We used to recurse on the pointed-to type here, which turned out to
10147 be incorrect because its definition might refer to variables not
10148 yet initialized at this point if a forward declaration is involved.
10150 It was actually useful for anonymous pointed-to types to ensure
10151 that the sizes evaluation dominates every possible later use of the
10152 values. Restricting to such types here would be safe since there
10153 is no possible forward declaration around, but would introduce an
10154 undesirable middle-end semantic to anonymity. We then defer to
10155 front-ends the responsibility of ensuring that the sizes are
10156 evaluated both early and late enough, e.g. by attaching artificial
10157 type declarations to the tree. */
10158 break;
10160 default:
10161 break;
10164 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
10165 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
10167 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10169 TYPE_SIZE (t) = TYPE_SIZE (type);
10170 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
10171 TYPE_SIZES_GIMPLIFIED (t) = 1;
10175 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
10176 a size or position, has had all of its SAVE_EXPRs evaluated.
10177 We add any required statements to *STMT_P. */
10179 void
10180 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
10182 tree expr = *expr_p;
10184 /* We don't do anything if the value isn't there, is constant, or contains
10185 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
10186 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
10187 will want to replace it with a new variable, but that will cause problems
10188 if this type is from outside the function. It's OK to have that here. */
10189 if (is_gimple_sizepos (expr))
10190 return;
10192 *expr_p = unshare_expr (expr);
10194 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
10197 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
10198 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
10199 is true, also gimplify the parameters. */
10201 gbind *
10202 gimplify_body (tree fndecl, bool do_parms)
10204 location_t saved_location = input_location;
10205 gimple_seq parm_stmts, seq;
10206 gimple *outer_stmt;
10207 gbind *outer_bind;
10208 struct cgraph_node *cgn;
10210 timevar_push (TV_TREE_GIMPLIFY);
10212 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
10213 gimplification. */
10214 default_rtl_profile ();
10216 gcc_assert (gimplify_ctxp == NULL);
10217 push_gimplify_context ();
10219 if (flag_openacc || flag_openmp)
10221 gcc_assert (gimplify_omp_ctxp == NULL);
10222 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
10223 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
10226 /* Unshare most shared trees in the body and in that of any nested functions.
10227 It would seem we don't have to do this for nested functions because
10228 they are supposed to be output and then the outer function gimplified
10229 first, but the g++ front end doesn't always do it that way. */
10230 unshare_body (fndecl);
10231 unvisit_body (fndecl);
10233 cgn = cgraph_node::get (fndecl);
10234 if (cgn && cgn->origin)
10235 nonlocal_vlas = new hash_set<tree>;
10237 /* Make sure input_location isn't set to something weird. */
10238 input_location = DECL_SOURCE_LOCATION (fndecl);
10240 /* Resolve callee-copies. This has to be done before processing
10241 the body so that DECL_VALUE_EXPR gets processed correctly. */
10242 parm_stmts = do_parms ? gimplify_parameters () : NULL;
10244 /* Gimplify the function's body. */
10245 seq = NULL;
10246 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
10247 outer_stmt = gimple_seq_first_stmt (seq);
10248 if (!outer_stmt)
10250 outer_stmt = gimple_build_nop ();
10251 gimplify_seq_add_stmt (&seq, outer_stmt);
10254 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
10255 not the case, wrap everything in a GIMPLE_BIND to make it so. */
10256 if (gimple_code (outer_stmt) == GIMPLE_BIND
10257 && gimple_seq_first (seq) == gimple_seq_last (seq))
10258 outer_bind = as_a <gbind *> (outer_stmt);
10259 else
10260 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
10262 DECL_SAVED_TREE (fndecl) = NULL_TREE;
10264 /* If we had callee-copies statements, insert them at the beginning
10265 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
10266 if (!gimple_seq_empty_p (parm_stmts))
10268 tree parm;
10270 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
10271 gimple_bind_set_body (outer_bind, parm_stmts);
10273 for (parm = DECL_ARGUMENTS (current_function_decl);
10274 parm; parm = DECL_CHAIN (parm))
10275 if (DECL_HAS_VALUE_EXPR_P (parm))
10277 DECL_HAS_VALUE_EXPR_P (parm) = 0;
10278 DECL_IGNORED_P (parm) = 0;
10282 if (nonlocal_vlas)
10284 if (nonlocal_vla_vars)
10286 /* tree-nested.c may later on call declare_vars (..., true);
10287 which relies on BLOCK_VARS chain to be the tail of the
10288 gimple_bind_vars chain. Ensure we don't violate that
10289 assumption. */
10290 if (gimple_bind_block (outer_bind)
10291 == DECL_INITIAL (current_function_decl))
10292 declare_vars (nonlocal_vla_vars, outer_bind, true);
10293 else
10294 BLOCK_VARS (DECL_INITIAL (current_function_decl))
10295 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
10296 nonlocal_vla_vars);
10297 nonlocal_vla_vars = NULL_TREE;
10299 delete nonlocal_vlas;
10300 nonlocal_vlas = NULL;
10303 if ((flag_openacc || flag_openmp || flag_openmp_simd)
10304 && gimplify_omp_ctxp)
10306 delete_omp_context (gimplify_omp_ctxp);
10307 gimplify_omp_ctxp = NULL;
10310 pop_gimplify_context (outer_bind);
10311 gcc_assert (gimplify_ctxp == NULL);
10313 if (flag_checking && !seen_error ())
10314 verify_gimple_in_seq (gimple_bind_body (outer_bind));
10316 timevar_pop (TV_TREE_GIMPLIFY);
10317 input_location = saved_location;
10319 return outer_bind;
10322 typedef char *char_p; /* For DEF_VEC_P. */
10324 /* Return whether we should exclude FNDECL from instrumentation. */
10326 static bool
10327 flag_instrument_functions_exclude_p (tree fndecl)
10329 vec<char_p> *v;
10331 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
10332 if (v && v->length () > 0)
10334 const char *name;
10335 int i;
10336 char *s;
10338 name = lang_hooks.decl_printable_name (fndecl, 0);
10339 FOR_EACH_VEC_ELT (*v, i, s)
10340 if (strstr (name, s) != NULL)
10341 return true;
10344 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
10345 if (v && v->length () > 0)
10347 const char *name;
10348 int i;
10349 char *s;
10351 name = DECL_SOURCE_FILE (fndecl);
10352 FOR_EACH_VEC_ELT (*v, i, s)
10353 if (strstr (name, s) != NULL)
10354 return true;
10357 return false;
10360 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
10361 node for the function we want to gimplify.
10363 Return the sequence of GIMPLE statements corresponding to the body
10364 of FNDECL. */
10366 void
10367 gimplify_function_tree (tree fndecl)
10369 tree parm, ret;
10370 gimple_seq seq;
10371 gbind *bind;
10373 gcc_assert (!gimple_body (fndecl));
10375 if (DECL_STRUCT_FUNCTION (fndecl))
10376 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
10377 else
10378 push_struct_function (fndecl);
10380 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
10381 if necessary. */
10382 cfun->curr_properties |= PROP_gimple_lva;
10384 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
10386 /* Preliminarily mark non-addressed complex variables as eligible
10387 for promotion to gimple registers. We'll transform their uses
10388 as we find them. */
10389 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
10390 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
10391 && !TREE_THIS_VOLATILE (parm)
10392 && !needs_to_live_in_memory (parm))
10393 DECL_GIMPLE_REG_P (parm) = 1;
10396 ret = DECL_RESULT (fndecl);
10397 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
10398 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
10399 && !needs_to_live_in_memory (ret))
10400 DECL_GIMPLE_REG_P (ret) = 1;
10402 bind = gimplify_body (fndecl, true);
10404 /* The tree body of the function is no longer needed, replace it
10405 with the new GIMPLE body. */
10406 seq = NULL;
10407 gimple_seq_add_stmt (&seq, bind);
10408 gimple_set_body (fndecl, seq);
10410 /* If we're instrumenting function entry/exit, then prepend the call to
10411 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
10412 catch the exit hook. */
10413 /* ??? Add some way to ignore exceptions for this TFE. */
10414 if (flag_instrument_function_entry_exit
10415 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
10416 && !flag_instrument_functions_exclude_p (fndecl))
10418 tree x;
10419 gbind *new_bind;
10420 gimple *tf;
10421 gimple_seq cleanup = NULL, body = NULL;
10422 tree tmp_var;
10423 gcall *call;
10425 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
10426 call = gimple_build_call (x, 1, integer_zero_node);
10427 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
10428 gimple_call_set_lhs (call, tmp_var);
10429 gimplify_seq_add_stmt (&cleanup, call);
10430 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
10431 call = gimple_build_call (x, 2,
10432 build_fold_addr_expr (current_function_decl),
10433 tmp_var);
10434 gimplify_seq_add_stmt (&cleanup, call);
10435 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
10437 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
10438 call = gimple_build_call (x, 1, integer_zero_node);
10439 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
10440 gimple_call_set_lhs (call, tmp_var);
10441 gimplify_seq_add_stmt (&body, call);
10442 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
10443 call = gimple_build_call (x, 2,
10444 build_fold_addr_expr (current_function_decl),
10445 tmp_var);
10446 gimplify_seq_add_stmt (&body, call);
10447 gimplify_seq_add_stmt (&body, tf);
10448 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
10449 /* Clear the block for BIND, since it is no longer directly inside
10450 the function, but within a try block. */
10451 gimple_bind_set_block (bind, NULL);
10453 /* Replace the current function body with the body
10454 wrapped in the try/finally TF. */
10455 seq = NULL;
10456 gimple_seq_add_stmt (&seq, new_bind);
10457 gimple_set_body (fndecl, seq);
10458 bind = new_bind;
10461 if ((flag_sanitize & SANITIZE_THREAD) != 0
10462 && !lookup_attribute ("no_sanitize_thread", DECL_ATTRIBUTES (fndecl)))
10464 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
10465 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
10466 gbind *new_bind = gimple_build_bind (NULL, tf, gimple_bind_block (bind));
10467 /* Clear the block for BIND, since it is no longer directly inside
10468 the function, but within a try block. */
10469 gimple_bind_set_block (bind, NULL);
10470 /* Replace the current function body with the body
10471 wrapped in the try/finally TF. */
10472 seq = NULL;
10473 gimple_seq_add_stmt (&seq, new_bind);
10474 gimple_set_body (fndecl, seq);
10477 DECL_SAVED_TREE (fndecl) = NULL_TREE;
10478 cfun->curr_properties |= PROP_gimple_any;
10480 pop_cfun ();
10482 dump_function (TDI_generic, fndecl);
10485 /* Return a dummy expression of type TYPE in order to keep going after an
10486 error. */
10488 static tree
10489 dummy_object (tree type)
10491 tree t = build_int_cst (build_pointer_type (type), 0);
10492 return build2 (MEM_REF, type, t, t);
10495 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
10496 builtin function, but a very special sort of operator. */
10498 enum gimplify_status
10499 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
10500 gimple_seq *post_p ATTRIBUTE_UNUSED)
10502 tree promoted_type, have_va_type;
10503 tree valist = TREE_OPERAND (*expr_p, 0);
10504 tree type = TREE_TYPE (*expr_p);
10505 tree t, tag;
10506 location_t loc = EXPR_LOCATION (*expr_p);
10508 /* Verify that valist is of the proper type. */
10509 have_va_type = TREE_TYPE (valist);
10510 if (have_va_type == error_mark_node)
10511 return GS_ERROR;
10512 have_va_type = targetm.canonical_va_list_type (have_va_type);
10514 if (have_va_type == NULL_TREE)
10516 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
10517 return GS_ERROR;
10520 /* Generate a diagnostic for requesting data of a type that cannot
10521 be passed through `...' due to type promotion at the call site. */
10522 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
10523 != type)
10525 static bool gave_help;
10526 bool warned;
10528 /* Unfortunately, this is merely undefined, rather than a constraint
10529 violation, so we cannot make this an error. If this call is never
10530 executed, the program is still strictly conforming. */
10531 warned = warning_at (loc, 0,
10532 "%qT is promoted to %qT when passed through %<...%>",
10533 type, promoted_type);
10534 if (!gave_help && warned)
10536 gave_help = true;
10537 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
10538 promoted_type, type);
10541 /* We can, however, treat "undefined" any way we please.
10542 Call abort to encourage the user to fix the program. */
10543 if (warned)
10544 inform (loc, "if this code is reached, the program will abort");
10545 /* Before the abort, allow the evaluation of the va_list
10546 expression to exit or longjmp. */
10547 gimplify_and_add (valist, pre_p);
10548 t = build_call_expr_loc (loc,
10549 builtin_decl_implicit (BUILT_IN_TRAP), 0);
10550 gimplify_and_add (t, pre_p);
10552 /* This is dead code, but go ahead and finish so that the
10553 mode of the result comes out right. */
10554 *expr_p = dummy_object (type);
10555 return GS_ALL_DONE;
10558 tag = build_int_cst (build_pointer_type (type), 0);
10559 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 2, valist, tag);
10561 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
10562 needs to be expanded. */
10563 cfun->curr_properties &= ~PROP_gimple_lva;
10565 return GS_OK;
10568 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
10570 DST/SRC are the destination and source respectively. You can pass
10571 ungimplified trees in DST or SRC, in which case they will be
10572 converted to a gimple operand if necessary.
10574 This function returns the newly created GIMPLE_ASSIGN tuple. */
10576 gimple *
10577 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
10579 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
10580 gimplify_and_add (t, seq_p);
10581 ggc_free (t);
10582 return gimple_seq_last_stmt (*seq_p);
10585 inline hashval_t
10586 gimplify_hasher::hash (const elt_t *p)
10588 tree t = p->val;
10589 return iterative_hash_expr (t, 0);
10592 inline bool
10593 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
10595 tree t1 = p1->val;
10596 tree t2 = p2->val;
10597 enum tree_code code = TREE_CODE (t1);
10599 if (TREE_CODE (t2) != code
10600 || TREE_TYPE (t1) != TREE_TYPE (t2))
10601 return false;
10603 if (!operand_equal_p (t1, t2, 0))
10604 return false;
10606 /* Only allow them to compare equal if they also hash equal; otherwise
10607 results are nondeterminate, and we fail bootstrap comparison. */
10608 gcc_checking_assert (hash (p1) == hash (p2));
10610 return true;