OpenACC acc_on_device: Fix logic error introduced in an earlier change.
[official-gcc.git] / gcc / gimplify.c
blob6972def19b628245ba716403be41d0394fec322c
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2014 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 "tree.h"
27 #include "expr.h"
28 #include "hash-set.h"
29 #include "hash-table.h"
30 #include "predict.h"
31 #include "vec.h"
32 #include "hashtab.h"
33 #include "machmode.h"
34 #include "tm.h"
35 #include "hard-reg-set.h"
36 #include "input.h"
37 #include "function.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-fold.h"
42 #include "tree-eh.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "gimplify.h"
47 #include "gimple-iterator.h"
48 #include "stringpool.h"
49 #include "calls.h"
50 #include "varasm.h"
51 #include "stor-layout.h"
52 #include "stmt.h"
53 #include "print-tree.h"
54 #include "tree-iterator.h"
55 #include "tree-inline.h"
56 #include "tree-pretty-print.h"
57 #include "langhooks.h"
58 #include "bitmap.h"
59 #include "gimple-ssa.h"
60 #include "hash-map.h"
61 #include "plugin-api.h"
62 #include "ipa-ref.h"
63 #include "cgraph.h"
64 #include "tree-cfg.h"
65 #include "tree-ssanames.h"
66 #include "tree-ssa.h"
67 #include "diagnostic-core.h"
68 #include "target.h"
69 #include "splay-tree.h"
70 #include "omp-low.h"
71 #include "gimple-low.h"
72 #include "cilk.h"
74 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
75 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
76 #include "builtins.h"
78 enum gimplify_omp_var_data
80 GOVD_SEEN = 1,
81 GOVD_EXPLICIT = 2,
82 GOVD_SHARED = 4,
83 GOVD_PRIVATE = 8,
84 GOVD_FIRSTPRIVATE = 16,
85 GOVD_LASTPRIVATE = 32,
86 GOVD_REDUCTION = 64,
87 GOVD_LOCAL = 128,
88 GOVD_MAP = 256,
89 GOVD_DEBUG_PRIVATE = 512,
90 GOVD_PRIVATE_OUTER_REF = 1024,
91 GOVD_LINEAR = 2048,
92 GOVD_ALIGNED = 4096,
94 /* Flag for GOVD_MAP: don't copy back. */
95 GOVD_MAP_TO_ONLY = 8192,
97 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
98 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
99 | GOVD_LOCAL)
103 enum omp_region_type
105 ORT_WORKSHARE = 0,
106 ORT_SIMD = 1,
107 ORT_PARALLEL = 2,
108 ORT_COMBINED_PARALLEL = 3,
109 ORT_TASK = 4,
110 ORT_UNTIED_TASK = 5,
111 ORT_TEAMS = 8,
112 /* Data region. */
113 ORT_TARGET_DATA = 16,
114 /* Data region with offloading. */
115 ORT_TARGET = 32
118 /* Gimplify hashtable helper. */
120 struct gimplify_hasher : typed_free_remove <elt_t>
122 typedef elt_t value_type;
123 typedef elt_t compare_type;
124 static inline hashval_t hash (const value_type *);
125 static inline bool equal (const value_type *, const compare_type *);
128 struct gimplify_ctx
130 struct gimplify_ctx *prev_context;
132 vec<gbind *> bind_expr_stack;
133 tree temps;
134 gimple_seq conditional_cleanups;
135 tree exit_label;
136 tree return_temp;
138 vec<tree> case_labels;
139 /* The formal temporary table. Should this be persistent? */
140 hash_table<gimplify_hasher> *temp_htab;
142 int conditions;
143 bool save_stack;
144 bool into_ssa;
145 bool allow_rhs_cond_expr;
146 bool in_cleanup_point_expr;
149 struct gimplify_omp_ctx
151 struct gimplify_omp_ctx *outer_context;
152 splay_tree variables;
153 hash_set<tree> *privatized_types;
154 location_t location;
155 enum omp_clause_default_kind default_kind;
156 enum omp_region_type region_type;
157 bool combined_loop;
158 bool distribute;
161 static struct gimplify_ctx *gimplify_ctxp;
162 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
164 /* Forward declaration. */
165 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
167 /* Shorter alias name for the above function for use in gimplify.c
168 only. */
170 static inline void
171 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
173 gimple_seq_add_stmt_without_update (seq_p, gs);
176 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
177 NULL, a new sequence is allocated. This function is
178 similar to gimple_seq_add_seq, but does not scan the operands.
179 During gimplification, we need to manipulate statement sequences
180 before the def/use vectors have been constructed. */
182 static void
183 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
185 gimple_stmt_iterator si;
187 if (src == NULL)
188 return;
190 si = gsi_last (*dst_p);
191 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
195 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
196 and popping gimplify contexts. */
198 static struct gimplify_ctx *ctx_pool = NULL;
200 /* Return a gimplify context struct from the pool. */
202 static inline struct gimplify_ctx *
203 ctx_alloc (void)
205 struct gimplify_ctx * c = ctx_pool;
207 if (c)
208 ctx_pool = c->prev_context;
209 else
210 c = XNEW (struct gimplify_ctx);
212 memset (c, '\0', sizeof (*c));
213 return c;
216 /* Put gimplify context C back into the pool. */
218 static inline void
219 ctx_free (struct gimplify_ctx *c)
221 c->prev_context = ctx_pool;
222 ctx_pool = c;
225 /* Free allocated ctx stack memory. */
227 void
228 free_gimplify_stack (void)
230 struct gimplify_ctx *c;
232 while ((c = ctx_pool))
234 ctx_pool = c->prev_context;
235 free (c);
240 /* Set up a context for the gimplifier. */
242 void
243 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
245 struct gimplify_ctx *c = ctx_alloc ();
247 c->prev_context = gimplify_ctxp;
248 gimplify_ctxp = c;
249 gimplify_ctxp->into_ssa = in_ssa;
250 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
253 /* Tear down a context for the gimplifier. If BODY is non-null, then
254 put the temporaries into the outer BIND_EXPR. Otherwise, put them
255 in the local_decls.
257 BODY is not a sequence, but the first tuple in a sequence. */
259 void
260 pop_gimplify_context (gimple body)
262 struct gimplify_ctx *c = gimplify_ctxp;
264 gcc_assert (c
265 && (!c->bind_expr_stack.exists ()
266 || c->bind_expr_stack.is_empty ()));
267 c->bind_expr_stack.release ();
268 gimplify_ctxp = c->prev_context;
270 if (body)
271 declare_vars (c->temps, body, false);
272 else
273 record_vars (c->temps);
275 delete c->temp_htab;
276 c->temp_htab = NULL;
277 ctx_free (c);
280 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
282 static void
283 gimple_push_bind_expr (gbind *bind_stmt)
285 gimplify_ctxp->bind_expr_stack.reserve (8);
286 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
289 /* Pop the first element off the stack of bindings. */
291 static void
292 gimple_pop_bind_expr (void)
294 gimplify_ctxp->bind_expr_stack.pop ();
297 /* Return the first element of the stack of bindings. */
299 gbind *
300 gimple_current_bind_expr (void)
302 return gimplify_ctxp->bind_expr_stack.last ();
305 /* Return the stack of bindings created during gimplification. */
307 vec<gbind *>
308 gimple_bind_expr_stack (void)
310 return gimplify_ctxp->bind_expr_stack;
313 /* Return true iff there is a COND_EXPR between us and the innermost
314 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
316 static bool
317 gimple_conditional_context (void)
319 return gimplify_ctxp->conditions > 0;
322 /* Note that we've entered a COND_EXPR. */
324 static void
325 gimple_push_condition (void)
327 #ifdef ENABLE_GIMPLE_CHECKING
328 if (gimplify_ctxp->conditions == 0)
329 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
330 #endif
331 ++(gimplify_ctxp->conditions);
334 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
335 now, add any conditional cleanups we've seen to the prequeue. */
337 static void
338 gimple_pop_condition (gimple_seq *pre_p)
340 int conds = --(gimplify_ctxp->conditions);
342 gcc_assert (conds >= 0);
343 if (conds == 0)
345 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
346 gimplify_ctxp->conditional_cleanups = NULL;
350 /* A stable comparison routine for use with splay trees and DECLs. */
352 static int
353 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
355 tree a = (tree) xa;
356 tree b = (tree) xb;
358 return DECL_UID (a) - DECL_UID (b);
361 /* Create a new omp construct that deals with variable remapping. */
363 static struct gimplify_omp_ctx *
364 new_omp_context (enum omp_region_type region_type)
366 struct gimplify_omp_ctx *c;
368 c = XCNEW (struct gimplify_omp_ctx);
369 c->outer_context = gimplify_omp_ctxp;
370 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
371 c->privatized_types = new hash_set<tree>;
372 c->location = input_location;
373 c->region_type = region_type;
374 if ((region_type & ORT_TASK) == 0)
375 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
376 else
377 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
379 return c;
382 /* Destroy an omp construct that deals with variable remapping. */
384 static void
385 delete_omp_context (struct gimplify_omp_ctx *c)
387 splay_tree_delete (c->variables);
388 delete c->privatized_types;
389 XDELETE (c);
392 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
393 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
395 /* Both gimplify the statement T and append it to *SEQ_P. This function
396 behaves exactly as gimplify_stmt, but you don't have to pass T as a
397 reference. */
399 void
400 gimplify_and_add (tree t, gimple_seq *seq_p)
402 gimplify_stmt (&t, seq_p);
405 /* Gimplify statement T into sequence *SEQ_P, and return the first
406 tuple in the sequence of generated tuples for this statement.
407 Return NULL if gimplifying T produced no tuples. */
409 static gimple
410 gimplify_and_return_first (tree t, gimple_seq *seq_p)
412 gimple_stmt_iterator last = gsi_last (*seq_p);
414 gimplify_and_add (t, seq_p);
416 if (!gsi_end_p (last))
418 gsi_next (&last);
419 return gsi_stmt (last);
421 else
422 return gimple_seq_first_stmt (*seq_p);
425 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
426 LHS, or for a call argument. */
428 static bool
429 is_gimple_mem_rhs (tree t)
431 /* If we're dealing with a renamable type, either source or dest must be
432 a renamed variable. */
433 if (is_gimple_reg_type (TREE_TYPE (t)))
434 return is_gimple_val (t);
435 else
436 return is_gimple_val (t) || is_gimple_lvalue (t);
439 /* Return true if T is a CALL_EXPR or an expression that can be
440 assigned to a temporary. Note that this predicate should only be
441 used during gimplification. See the rationale for this in
442 gimplify_modify_expr. */
444 static bool
445 is_gimple_reg_rhs_or_call (tree t)
447 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
448 || TREE_CODE (t) == CALL_EXPR);
451 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
452 this predicate should only be used during gimplification. See the
453 rationale for this in gimplify_modify_expr. */
455 static bool
456 is_gimple_mem_rhs_or_call (tree t)
458 /* If we're dealing with a renamable type, either source or dest must be
459 a renamed variable. */
460 if (is_gimple_reg_type (TREE_TYPE (t)))
461 return is_gimple_val (t);
462 else
463 return (is_gimple_val (t) || is_gimple_lvalue (t)
464 || TREE_CODE (t) == CALL_EXPR);
467 /* Create a temporary with a name derived from VAL. Subroutine of
468 lookup_tmp_var; nobody else should call this function. */
470 static inline tree
471 create_tmp_from_val (tree val)
473 /* Drop all qualifiers and address-space information from the value type. */
474 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
475 tree var = create_tmp_var (type, get_name (val));
476 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
477 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
478 DECL_GIMPLE_REG_P (var) = 1;
479 return var;
482 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
483 an existing expression temporary. */
485 static tree
486 lookup_tmp_var (tree val, bool is_formal)
488 tree ret;
490 /* If not optimizing, never really reuse a temporary. local-alloc
491 won't allocate any variable that is used in more than one basic
492 block, which means it will go into memory, causing much extra
493 work in reload and final and poorer code generation, outweighing
494 the extra memory allocation here. */
495 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
496 ret = create_tmp_from_val (val);
497 else
499 elt_t elt, *elt_p;
500 elt_t **slot;
502 elt.val = val;
503 if (!gimplify_ctxp->temp_htab)
504 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
505 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
506 if (*slot == NULL)
508 elt_p = XNEW (elt_t);
509 elt_p->val = val;
510 elt_p->temp = ret = create_tmp_from_val (val);
511 *slot = elt_p;
513 else
515 elt_p = *slot;
516 ret = elt_p->temp;
520 return ret;
523 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
525 static tree
526 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
527 bool is_formal)
529 tree t, mod;
531 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
532 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
533 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
534 fb_rvalue);
536 if (gimplify_ctxp->into_ssa
537 && is_gimple_reg_type (TREE_TYPE (val)))
538 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
539 else
540 t = lookup_tmp_var (val, is_formal);
542 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
544 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
546 /* gimplify_modify_expr might want to reduce this further. */
547 gimplify_and_add (mod, pre_p);
548 ggc_free (mod);
550 return t;
553 /* Return a formal temporary variable initialized with VAL. PRE_P is as
554 in gimplify_expr. Only use this function if:
556 1) The value of the unfactored expression represented by VAL will not
557 change between the initialization and use of the temporary, and
558 2) The temporary will not be otherwise modified.
560 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
561 and #2 means it is inappropriate for && temps.
563 For other cases, use get_initialized_tmp_var instead. */
565 tree
566 get_formal_tmp_var (tree val, gimple_seq *pre_p)
568 return internal_get_tmp_var (val, pre_p, NULL, true);
571 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
572 are as in gimplify_expr. */
574 tree
575 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
577 return internal_get_tmp_var (val, pre_p, post_p, false);
580 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
581 generate debug info for them; otherwise don't. */
583 void
584 declare_vars (tree vars, gimple gs, bool debug_info)
586 tree last = vars;
587 if (last)
589 tree temps, block;
591 gbind *scope = as_a <gbind *> (gs);
593 temps = nreverse (last);
595 block = gimple_bind_block (scope);
596 gcc_assert (!block || TREE_CODE (block) == BLOCK);
597 if (!block || !debug_info)
599 DECL_CHAIN (last) = gimple_bind_vars (scope);
600 gimple_bind_set_vars (scope, temps);
602 else
604 /* We need to attach the nodes both to the BIND_EXPR and to its
605 associated BLOCK for debugging purposes. The key point here
606 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
607 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
608 if (BLOCK_VARS (block))
609 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
610 else
612 gimple_bind_set_vars (scope,
613 chainon (gimple_bind_vars (scope), temps));
614 BLOCK_VARS (block) = temps;
620 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
621 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
622 no such upper bound can be obtained. */
624 static void
625 force_constant_size (tree var)
627 /* The only attempt we make is by querying the maximum size of objects
628 of the variable's type. */
630 HOST_WIDE_INT max_size;
632 gcc_assert (TREE_CODE (var) == VAR_DECL);
634 max_size = max_int_size_in_bytes (TREE_TYPE (var));
636 gcc_assert (max_size >= 0);
638 DECL_SIZE_UNIT (var)
639 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
640 DECL_SIZE (var)
641 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
644 /* Push the temporary variable TMP into the current binding. */
646 void
647 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
649 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
651 /* Later processing assumes that the object size is constant, which might
652 not be true at this point. Force the use of a constant upper bound in
653 this case. */
654 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
655 force_constant_size (tmp);
657 DECL_CONTEXT (tmp) = fn->decl;
658 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
660 record_vars_into (tmp, fn->decl);
663 /* Push the temporary variable TMP into the current binding. */
665 void
666 gimple_add_tmp_var (tree tmp)
668 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
670 /* Later processing assumes that the object size is constant, which might
671 not be true at this point. Force the use of a constant upper bound in
672 this case. */
673 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
674 force_constant_size (tmp);
676 DECL_CONTEXT (tmp) = current_function_decl;
677 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
679 if (gimplify_ctxp)
681 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
682 gimplify_ctxp->temps = tmp;
684 /* Mark temporaries local within the nearest enclosing parallel. */
685 if (gimplify_omp_ctxp)
687 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
688 while (ctx
689 && (ctx->region_type == ORT_WORKSHARE
690 || ctx->region_type == ORT_SIMD))
691 ctx = ctx->outer_context;
692 if (ctx)
693 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
696 else if (cfun)
697 record_vars (tmp);
698 else
700 gimple_seq body_seq;
702 /* This case is for nested functions. We need to expose the locals
703 they create. */
704 body_seq = gimple_body (current_function_decl);
705 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
711 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
712 nodes that are referenced more than once in GENERIC functions. This is
713 necessary because gimplification (translation into GIMPLE) is performed
714 by modifying tree nodes in-place, so gimplication of a shared node in a
715 first context could generate an invalid GIMPLE form in a second context.
717 This is achieved with a simple mark/copy/unmark algorithm that walks the
718 GENERIC representation top-down, marks nodes with TREE_VISITED the first
719 time it encounters them, duplicates them if they already have TREE_VISITED
720 set, and finally removes the TREE_VISITED marks it has set.
722 The algorithm works only at the function level, i.e. it generates a GENERIC
723 representation of a function with no nodes shared within the function when
724 passed a GENERIC function (except for nodes that are allowed to be shared).
726 At the global level, it is also necessary to unshare tree nodes that are
727 referenced in more than one function, for the same aforementioned reason.
728 This requires some cooperation from the front-end. There are 2 strategies:
730 1. Manual unsharing. The front-end needs to call unshare_expr on every
731 expression that might end up being shared across functions.
733 2. Deep unsharing. This is an extension of regular unsharing. Instead
734 of calling unshare_expr on expressions that might be shared across
735 functions, the front-end pre-marks them with TREE_VISITED. This will
736 ensure that they are unshared on the first reference within functions
737 when the regular unsharing algorithm runs. The counterpart is that
738 this algorithm must look deeper than for manual unsharing, which is
739 specified by LANG_HOOKS_DEEP_UNSHARING.
741 If there are only few specific cases of node sharing across functions, it is
742 probably easier for a front-end to unshare the expressions manually. On the
743 contrary, if the expressions generated at the global level are as widespread
744 as expressions generated within functions, deep unsharing is very likely the
745 way to go. */
747 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
748 These nodes model computations that must be done once. If we were to
749 unshare something like SAVE_EXPR(i++), the gimplification process would
750 create wrong code. However, if DATA is non-null, it must hold a pointer
751 set that is used to unshare the subtrees of these nodes. */
753 static tree
754 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
756 tree t = *tp;
757 enum tree_code code = TREE_CODE (t);
759 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
760 copy their subtrees if we can make sure to do it only once. */
761 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
763 if (data && !((hash_set<tree> *)data)->add (t))
765 else
766 *walk_subtrees = 0;
769 /* Stop at types, decls, constants like copy_tree_r. */
770 else if (TREE_CODE_CLASS (code) == tcc_type
771 || TREE_CODE_CLASS (code) == tcc_declaration
772 || TREE_CODE_CLASS (code) == tcc_constant
773 /* We can't do anything sensible with a BLOCK used as an
774 expression, but we also can't just die when we see it
775 because of non-expression uses. So we avert our eyes
776 and cross our fingers. Silly Java. */
777 || code == BLOCK)
778 *walk_subtrees = 0;
780 /* Cope with the statement expression extension. */
781 else if (code == STATEMENT_LIST)
784 /* Leave the bulk of the work to copy_tree_r itself. */
785 else
786 copy_tree_r (tp, walk_subtrees, NULL);
788 return NULL_TREE;
791 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
792 If *TP has been visited already, then *TP is deeply copied by calling
793 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
795 static tree
796 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
798 tree t = *tp;
799 enum tree_code code = TREE_CODE (t);
801 /* Skip types, decls, and constants. But we do want to look at their
802 types and the bounds of types. Mark them as visited so we properly
803 unmark their subtrees on the unmark pass. If we've already seen them,
804 don't look down further. */
805 if (TREE_CODE_CLASS (code) == tcc_type
806 || TREE_CODE_CLASS (code) == tcc_declaration
807 || TREE_CODE_CLASS (code) == tcc_constant)
809 if (TREE_VISITED (t))
810 *walk_subtrees = 0;
811 else
812 TREE_VISITED (t) = 1;
815 /* If this node has been visited already, unshare it and don't look
816 any deeper. */
817 else if (TREE_VISITED (t))
819 walk_tree (tp, mostly_copy_tree_r, data, NULL);
820 *walk_subtrees = 0;
823 /* Otherwise, mark the node as visited and keep looking. */
824 else
825 TREE_VISITED (t) = 1;
827 return NULL_TREE;
830 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
831 copy_if_shared_r callback unmodified. */
833 static inline void
834 copy_if_shared (tree *tp, void *data)
836 walk_tree (tp, copy_if_shared_r, data, NULL);
839 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
840 any nested functions. */
842 static void
843 unshare_body (tree fndecl)
845 struct cgraph_node *cgn = cgraph_node::get (fndecl);
846 /* If the language requires deep unsharing, we need a pointer set to make
847 sure we don't repeatedly unshare subtrees of unshareable nodes. */
848 hash_set<tree> *visited
849 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
851 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
852 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
853 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
855 delete visited;
857 if (cgn)
858 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
859 unshare_body (cgn->decl);
862 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
863 Subtrees are walked until the first unvisited node is encountered. */
865 static tree
866 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
868 tree t = *tp;
870 /* If this node has been visited, unmark it and keep looking. */
871 if (TREE_VISITED (t))
872 TREE_VISITED (t) = 0;
874 /* Otherwise, don't look any deeper. */
875 else
876 *walk_subtrees = 0;
878 return NULL_TREE;
881 /* Unmark the visited trees rooted at *TP. */
883 static inline void
884 unmark_visited (tree *tp)
886 walk_tree (tp, unmark_visited_r, NULL, NULL);
889 /* Likewise, but mark all trees as not visited. */
891 static void
892 unvisit_body (tree fndecl)
894 struct cgraph_node *cgn = cgraph_node::get (fndecl);
896 unmark_visited (&DECL_SAVED_TREE (fndecl));
897 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
898 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
900 if (cgn)
901 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
902 unvisit_body (cgn->decl);
905 /* Unconditionally make an unshared copy of EXPR. This is used when using
906 stored expressions which span multiple functions, such as BINFO_VTABLE,
907 as the normal unsharing process can't tell that they're shared. */
909 tree
910 unshare_expr (tree expr)
912 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
913 return expr;
916 /* Worker for unshare_expr_without_location. */
918 static tree
919 prune_expr_location (tree *tp, int *walk_subtrees, void *)
921 if (EXPR_P (*tp))
922 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
923 else
924 *walk_subtrees = 0;
925 return NULL_TREE;
928 /* Similar to unshare_expr but also prune all expression locations
929 from EXPR. */
931 tree
932 unshare_expr_without_location (tree expr)
934 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
935 if (EXPR_P (expr))
936 walk_tree (&expr, prune_expr_location, NULL, NULL);
937 return expr;
940 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
941 contain statements and have a value. Assign its value to a temporary
942 and give it void_type_node. Return the temporary, or NULL_TREE if
943 WRAPPER was already void. */
945 tree
946 voidify_wrapper_expr (tree wrapper, tree temp)
948 tree type = TREE_TYPE (wrapper);
949 if (type && !VOID_TYPE_P (type))
951 tree *p;
953 /* Set p to point to the body of the wrapper. Loop until we find
954 something that isn't a wrapper. */
955 for (p = &wrapper; p && *p; )
957 switch (TREE_CODE (*p))
959 case BIND_EXPR:
960 TREE_SIDE_EFFECTS (*p) = 1;
961 TREE_TYPE (*p) = void_type_node;
962 /* For a BIND_EXPR, the body is operand 1. */
963 p = &BIND_EXPR_BODY (*p);
964 break;
966 case CLEANUP_POINT_EXPR:
967 case TRY_FINALLY_EXPR:
968 case TRY_CATCH_EXPR:
969 TREE_SIDE_EFFECTS (*p) = 1;
970 TREE_TYPE (*p) = void_type_node;
971 p = &TREE_OPERAND (*p, 0);
972 break;
974 case STATEMENT_LIST:
976 tree_stmt_iterator i = tsi_last (*p);
977 TREE_SIDE_EFFECTS (*p) = 1;
978 TREE_TYPE (*p) = void_type_node;
979 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
981 break;
983 case COMPOUND_EXPR:
984 /* Advance to the last statement. Set all container types to
985 void. */
986 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
988 TREE_SIDE_EFFECTS (*p) = 1;
989 TREE_TYPE (*p) = void_type_node;
991 break;
993 case TRANSACTION_EXPR:
994 TREE_SIDE_EFFECTS (*p) = 1;
995 TREE_TYPE (*p) = void_type_node;
996 p = &TRANSACTION_EXPR_BODY (*p);
997 break;
999 default:
1000 /* Assume that any tree upon which voidify_wrapper_expr is
1001 directly called is a wrapper, and that its body is op0. */
1002 if (p == &wrapper)
1004 TREE_SIDE_EFFECTS (*p) = 1;
1005 TREE_TYPE (*p) = void_type_node;
1006 p = &TREE_OPERAND (*p, 0);
1007 break;
1009 goto out;
1013 out:
1014 if (p == NULL || IS_EMPTY_STMT (*p))
1015 temp = NULL_TREE;
1016 else if (temp)
1018 /* The wrapper is on the RHS of an assignment that we're pushing
1019 down. */
1020 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1021 || TREE_CODE (temp) == MODIFY_EXPR);
1022 TREE_OPERAND (temp, 1) = *p;
1023 *p = temp;
1025 else
1027 temp = create_tmp_var (type, "retval");
1028 *p = build2 (INIT_EXPR, type, temp, *p);
1031 return temp;
1034 return NULL_TREE;
1037 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1038 a temporary through which they communicate. */
1040 static void
1041 build_stack_save_restore (gcall **save, gcall **restore)
1043 tree tmp_var;
1045 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1046 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1047 gimple_call_set_lhs (*save, tmp_var);
1049 *restore
1050 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1051 1, tmp_var);
1054 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1056 static enum gimplify_status
1057 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1059 tree bind_expr = *expr_p;
1060 bool old_save_stack = gimplify_ctxp->save_stack;
1061 tree t;
1062 gbind *bind_stmt;
1063 gimple_seq body, cleanup;
1064 gcall *stack_save;
1065 location_t start_locus = 0, end_locus = 0;
1067 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1069 /* Mark variables seen in this bind expr. */
1070 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1072 if (TREE_CODE (t) == VAR_DECL)
1074 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1076 /* Mark variable as local. */
1077 if (ctx && !DECL_EXTERNAL (t)
1078 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1079 || splay_tree_lookup (ctx->variables,
1080 (splay_tree_key) t) == NULL))
1082 if (ctx->region_type == ORT_SIMD
1083 && TREE_ADDRESSABLE (t)
1084 && !TREE_STATIC (t))
1085 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1086 else
1087 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1090 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1092 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1093 cfun->has_local_explicit_reg_vars = true;
1096 /* Preliminarily mark non-addressed complex variables as eligible
1097 for promotion to gimple registers. We'll transform their uses
1098 as we find them. */
1099 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1100 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1101 && !TREE_THIS_VOLATILE (t)
1102 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1103 && !needs_to_live_in_memory (t))
1104 DECL_GIMPLE_REG_P (t) = 1;
1107 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1108 BIND_EXPR_BLOCK (bind_expr));
1109 gimple_push_bind_expr (bind_stmt);
1111 gimplify_ctxp->save_stack = false;
1113 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1114 body = NULL;
1115 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1116 gimple_bind_set_body (bind_stmt, body);
1118 /* Source location wise, the cleanup code (stack_restore and clobbers)
1119 belongs to the end of the block, so propagate what we have. The
1120 stack_save operation belongs to the beginning of block, which we can
1121 infer from the bind_expr directly if the block has no explicit
1122 assignment. */
1123 if (BIND_EXPR_BLOCK (bind_expr))
1125 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1126 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1128 if (start_locus == 0)
1129 start_locus = EXPR_LOCATION (bind_expr);
1131 cleanup = NULL;
1132 stack_save = NULL;
1133 if (gimplify_ctxp->save_stack)
1135 gcall *stack_restore;
1137 /* Save stack on entry and restore it on exit. Add a try_finally
1138 block to achieve this. */
1139 build_stack_save_restore (&stack_save, &stack_restore);
1141 gimple_set_location (stack_save, start_locus);
1142 gimple_set_location (stack_restore, end_locus);
1144 gimplify_seq_add_stmt (&cleanup, stack_restore);
1147 /* Add clobbers for all variables that go out of scope. */
1148 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1150 if (TREE_CODE (t) == VAR_DECL
1151 && !is_global_var (t)
1152 && DECL_CONTEXT (t) == current_function_decl
1153 && !DECL_HARD_REGISTER (t)
1154 && !TREE_THIS_VOLATILE (t)
1155 && !DECL_HAS_VALUE_EXPR_P (t)
1156 /* Only care for variables that have to be in memory. Others
1157 will be rewritten into SSA names, hence moved to the top-level. */
1158 && !is_gimple_reg (t)
1159 && flag_stack_reuse != SR_NONE)
1161 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1162 gimple clobber_stmt;
1163 TREE_THIS_VOLATILE (clobber) = 1;
1164 clobber_stmt = gimple_build_assign (t, clobber);
1165 gimple_set_location (clobber_stmt, end_locus);
1166 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1170 if (cleanup)
1172 gtry *gs;
1173 gimple_seq new_body;
1175 new_body = NULL;
1176 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1177 GIMPLE_TRY_FINALLY);
1179 if (stack_save)
1180 gimplify_seq_add_stmt (&new_body, stack_save);
1181 gimplify_seq_add_stmt (&new_body, gs);
1182 gimple_bind_set_body (bind_stmt, new_body);
1185 gimplify_ctxp->save_stack = old_save_stack;
1186 gimple_pop_bind_expr ();
1188 gimplify_seq_add_stmt (pre_p, bind_stmt);
1190 if (temp)
1192 *expr_p = temp;
1193 return GS_OK;
1196 *expr_p = NULL_TREE;
1197 return GS_ALL_DONE;
1200 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1201 GIMPLE value, it is assigned to a new temporary and the statement is
1202 re-written to return the temporary.
1204 PRE_P points to the sequence where side effects that must happen before
1205 STMT should be stored. */
1207 static enum gimplify_status
1208 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1210 greturn *ret;
1211 tree ret_expr = TREE_OPERAND (stmt, 0);
1212 tree result_decl, result;
1214 if (ret_expr == error_mark_node)
1215 return GS_ERROR;
1217 /* Implicit _Cilk_sync must be inserted right before any return statement
1218 if there is a _Cilk_spawn in the function. If the user has provided a
1219 _Cilk_sync, the optimizer should remove this duplicate one. */
1220 if (fn_contains_cilk_spawn_p (cfun))
1222 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1223 gimplify_and_add (impl_sync, pre_p);
1226 if (!ret_expr
1227 || TREE_CODE (ret_expr) == RESULT_DECL
1228 || ret_expr == error_mark_node)
1230 greturn *ret = gimple_build_return (ret_expr);
1231 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1232 gimplify_seq_add_stmt (pre_p, ret);
1233 return GS_ALL_DONE;
1236 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1237 result_decl = NULL_TREE;
1238 else
1240 result_decl = TREE_OPERAND (ret_expr, 0);
1242 /* See through a return by reference. */
1243 if (TREE_CODE (result_decl) == INDIRECT_REF)
1244 result_decl = TREE_OPERAND (result_decl, 0);
1246 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1247 || TREE_CODE (ret_expr) == INIT_EXPR)
1248 && TREE_CODE (result_decl) == RESULT_DECL);
1251 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1252 Recall that aggregate_value_p is FALSE for any aggregate type that is
1253 returned in registers. If we're returning values in registers, then
1254 we don't want to extend the lifetime of the RESULT_DECL, particularly
1255 across another call. In addition, for those aggregates for which
1256 hard_function_value generates a PARALLEL, we'll die during normal
1257 expansion of structure assignments; there's special code in expand_return
1258 to handle this case that does not exist in expand_expr. */
1259 if (!result_decl)
1260 result = NULL_TREE;
1261 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1263 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1265 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1266 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1267 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1268 should be effectively allocated by the caller, i.e. all calls to
1269 this function must be subject to the Return Slot Optimization. */
1270 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1271 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1273 result = result_decl;
1275 else if (gimplify_ctxp->return_temp)
1276 result = gimplify_ctxp->return_temp;
1277 else
1279 result = create_tmp_reg (TREE_TYPE (result_decl));
1281 /* ??? With complex control flow (usually involving abnormal edges),
1282 we can wind up warning about an uninitialized value for this. Due
1283 to how this variable is constructed and initialized, this is never
1284 true. Give up and never warn. */
1285 TREE_NO_WARNING (result) = 1;
1287 gimplify_ctxp->return_temp = result;
1290 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1291 Then gimplify the whole thing. */
1292 if (result != result_decl)
1293 TREE_OPERAND (ret_expr, 0) = result;
1295 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1297 ret = gimple_build_return (result);
1298 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1299 gimplify_seq_add_stmt (pre_p, ret);
1301 return GS_ALL_DONE;
1304 /* Gimplify a variable-length array DECL. */
1306 static void
1307 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1309 /* This is a variable-sized decl. Simplify its size and mark it
1310 for deferred expansion. */
1311 tree t, addr, ptr_type;
1313 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1314 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1316 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1317 if (DECL_HAS_VALUE_EXPR_P (decl))
1318 return;
1320 /* All occurrences of this decl in final gimplified code will be
1321 replaced by indirection. Setting DECL_VALUE_EXPR does two
1322 things: First, it lets the rest of the gimplifier know what
1323 replacement to use. Second, it lets the debug info know
1324 where to find the value. */
1325 ptr_type = build_pointer_type (TREE_TYPE (decl));
1326 addr = create_tmp_var (ptr_type, get_name (decl));
1327 DECL_IGNORED_P (addr) = 0;
1328 t = build_fold_indirect_ref (addr);
1329 TREE_THIS_NOTRAP (t) = 1;
1330 SET_DECL_VALUE_EXPR (decl, t);
1331 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1333 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1334 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1335 size_int (DECL_ALIGN (decl)));
1336 /* The call has been built for a variable-sized object. */
1337 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1338 t = fold_convert (ptr_type, t);
1339 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1341 gimplify_and_add (t, seq_p);
1343 /* Indicate that we need to restore the stack level when the
1344 enclosing BIND_EXPR is exited. */
1345 gimplify_ctxp->save_stack = true;
1348 /* A helper function to be called via walk_tree. Mark all labels under *TP
1349 as being forced. To be called for DECL_INITIAL of static variables. */
1351 static tree
1352 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1354 if (TYPE_P (*tp))
1355 *walk_subtrees = 0;
1356 if (TREE_CODE (*tp) == LABEL_DECL)
1357 FORCED_LABEL (*tp) = 1;
1359 return NULL_TREE;
1362 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1363 and initialization explicit. */
1365 static enum gimplify_status
1366 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1368 tree stmt = *stmt_p;
1369 tree decl = DECL_EXPR_DECL (stmt);
1371 *stmt_p = NULL_TREE;
1373 if (TREE_TYPE (decl) == error_mark_node)
1374 return GS_ERROR;
1376 if ((TREE_CODE (decl) == TYPE_DECL
1377 || TREE_CODE (decl) == VAR_DECL)
1378 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1379 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1381 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1382 in case its size expressions contain problematic nodes like CALL_EXPR. */
1383 if (TREE_CODE (decl) == TYPE_DECL
1384 && DECL_ORIGINAL_TYPE (decl)
1385 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1386 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1388 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1390 tree init = DECL_INITIAL (decl);
1392 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1393 || (!TREE_STATIC (decl)
1394 && flag_stack_check == GENERIC_STACK_CHECK
1395 && compare_tree_int (DECL_SIZE_UNIT (decl),
1396 STACK_CHECK_MAX_VAR_SIZE) > 0))
1397 gimplify_vla_decl (decl, seq_p);
1399 /* Some front ends do not explicitly declare all anonymous
1400 artificial variables. We compensate here by declaring the
1401 variables, though it would be better if the front ends would
1402 explicitly declare them. */
1403 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1404 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1405 gimple_add_tmp_var (decl);
1407 if (init && init != error_mark_node)
1409 if (!TREE_STATIC (decl))
1411 DECL_INITIAL (decl) = NULL_TREE;
1412 init = build2 (INIT_EXPR, void_type_node, decl, init);
1413 gimplify_and_add (init, seq_p);
1414 ggc_free (init);
1416 else
1417 /* We must still examine initializers for static variables
1418 as they may contain a label address. */
1419 walk_tree (&init, force_labels_r, NULL, NULL);
1423 return GS_ALL_DONE;
1426 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1427 and replacing the LOOP_EXPR with goto, but if the loop contains an
1428 EXIT_EXPR, we need to append a label for it to jump to. */
1430 static enum gimplify_status
1431 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1433 tree saved_label = gimplify_ctxp->exit_label;
1434 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1436 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1438 gimplify_ctxp->exit_label = NULL_TREE;
1440 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1442 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1444 if (gimplify_ctxp->exit_label)
1445 gimplify_seq_add_stmt (pre_p,
1446 gimple_build_label (gimplify_ctxp->exit_label));
1448 gimplify_ctxp->exit_label = saved_label;
1450 *expr_p = NULL;
1451 return GS_ALL_DONE;
1454 /* Gimplify a statement list onto a sequence. These may be created either
1455 by an enlightened front-end, or by shortcut_cond_expr. */
1457 static enum gimplify_status
1458 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1460 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1462 tree_stmt_iterator i = tsi_start (*expr_p);
1464 while (!tsi_end_p (i))
1466 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1467 tsi_delink (&i);
1470 if (temp)
1472 *expr_p = temp;
1473 return GS_OK;
1476 return GS_ALL_DONE;
1480 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1481 branch to. */
1483 static enum gimplify_status
1484 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1486 tree switch_expr = *expr_p;
1487 gimple_seq switch_body_seq = NULL;
1488 enum gimplify_status ret;
1489 tree index_type = TREE_TYPE (switch_expr);
1490 if (index_type == NULL_TREE)
1491 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1493 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1494 fb_rvalue);
1495 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1496 return ret;
1498 if (SWITCH_BODY (switch_expr))
1500 vec<tree> labels;
1501 vec<tree> saved_labels;
1502 tree default_case = NULL_TREE;
1503 gswitch *switch_stmt;
1505 /* If someone can be bothered to fill in the labels, they can
1506 be bothered to null out the body too. */
1507 gcc_assert (!SWITCH_LABELS (switch_expr));
1509 /* Save old labels, get new ones from body, then restore the old
1510 labels. Save all the things from the switch body to append after. */
1511 saved_labels = gimplify_ctxp->case_labels;
1512 gimplify_ctxp->case_labels.create (8);
1514 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1515 labels = gimplify_ctxp->case_labels;
1516 gimplify_ctxp->case_labels = saved_labels;
1518 preprocess_case_label_vec_for_gimple (labels, index_type,
1519 &default_case);
1521 if (!default_case)
1523 glabel *new_default;
1525 default_case
1526 = build_case_label (NULL_TREE, NULL_TREE,
1527 create_artificial_label (UNKNOWN_LOCATION));
1528 new_default = gimple_build_label (CASE_LABEL (default_case));
1529 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1532 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
1533 default_case, labels);
1534 gimplify_seq_add_stmt (pre_p, switch_stmt);
1535 gimplify_seq_add_seq (pre_p, switch_body_seq);
1536 labels.release ();
1538 else
1539 gcc_assert (SWITCH_LABELS (switch_expr));
1541 return GS_ALL_DONE;
1544 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1546 static enum gimplify_status
1547 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1549 struct gimplify_ctx *ctxp;
1550 glabel *label_stmt;
1552 /* Invalid programs can play Duff's Device type games with, for example,
1553 #pragma omp parallel. At least in the C front end, we don't
1554 detect such invalid branches until after gimplification, in the
1555 diagnose_omp_blocks pass. */
1556 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1557 if (ctxp->case_labels.exists ())
1558 break;
1560 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
1561 ctxp->case_labels.safe_push (*expr_p);
1562 gimplify_seq_add_stmt (pre_p, label_stmt);
1564 return GS_ALL_DONE;
1567 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1568 if necessary. */
1570 tree
1571 build_and_jump (tree *label_p)
1573 if (label_p == NULL)
1574 /* If there's nowhere to jump, just fall through. */
1575 return NULL_TREE;
1577 if (*label_p == NULL_TREE)
1579 tree label = create_artificial_label (UNKNOWN_LOCATION);
1580 *label_p = label;
1583 return build1 (GOTO_EXPR, void_type_node, *label_p);
1586 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1587 This also involves building a label to jump to and communicating it to
1588 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1590 static enum gimplify_status
1591 gimplify_exit_expr (tree *expr_p)
1593 tree cond = TREE_OPERAND (*expr_p, 0);
1594 tree expr;
1596 expr = build_and_jump (&gimplify_ctxp->exit_label);
1597 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1598 *expr_p = expr;
1600 return GS_OK;
1603 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1604 different from its canonical type, wrap the whole thing inside a
1605 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1606 type.
1608 The canonical type of a COMPONENT_REF is the type of the field being
1609 referenced--unless the field is a bit-field which can be read directly
1610 in a smaller mode, in which case the canonical type is the
1611 sign-appropriate type corresponding to that mode. */
1613 static void
1614 canonicalize_component_ref (tree *expr_p)
1616 tree expr = *expr_p;
1617 tree type;
1619 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1621 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1622 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1623 else
1624 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1626 /* One could argue that all the stuff below is not necessary for
1627 the non-bitfield case and declare it a FE error if type
1628 adjustment would be needed. */
1629 if (TREE_TYPE (expr) != type)
1631 #ifdef ENABLE_TYPES_CHECKING
1632 tree old_type = TREE_TYPE (expr);
1633 #endif
1634 int type_quals;
1636 /* We need to preserve qualifiers and propagate them from
1637 operand 0. */
1638 type_quals = TYPE_QUALS (type)
1639 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1640 if (TYPE_QUALS (type) != type_quals)
1641 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1643 /* Set the type of the COMPONENT_REF to the underlying type. */
1644 TREE_TYPE (expr) = type;
1646 #ifdef ENABLE_TYPES_CHECKING
1647 /* It is now a FE error, if the conversion from the canonical
1648 type to the original expression type is not useless. */
1649 gcc_assert (useless_type_conversion_p (old_type, type));
1650 #endif
1654 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1655 to foo, embed that change in the ADDR_EXPR by converting
1656 T array[U];
1657 (T *)&array
1659 &array[L]
1660 where L is the lower bound. For simplicity, only do this for constant
1661 lower bound.
1662 The constraint is that the type of &array[L] is trivially convertible
1663 to T *. */
1665 static void
1666 canonicalize_addr_expr (tree *expr_p)
1668 tree expr = *expr_p;
1669 tree addr_expr = TREE_OPERAND (expr, 0);
1670 tree datype, ddatype, pddatype;
1672 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1673 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1674 || TREE_CODE (addr_expr) != ADDR_EXPR)
1675 return;
1677 /* The addr_expr type should be a pointer to an array. */
1678 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1679 if (TREE_CODE (datype) != ARRAY_TYPE)
1680 return;
1682 /* The pointer to element type shall be trivially convertible to
1683 the expression pointer type. */
1684 ddatype = TREE_TYPE (datype);
1685 pddatype = build_pointer_type (ddatype);
1686 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1687 pddatype))
1688 return;
1690 /* The lower bound and element sizes must be constant. */
1691 if (!TYPE_SIZE_UNIT (ddatype)
1692 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1693 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1694 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1695 return;
1697 /* All checks succeeded. Build a new node to merge the cast. */
1698 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1699 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1700 NULL_TREE, NULL_TREE);
1701 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1703 /* We can have stripped a required restrict qualifier above. */
1704 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1705 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1708 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1709 underneath as appropriate. */
1711 static enum gimplify_status
1712 gimplify_conversion (tree *expr_p)
1714 location_t loc = EXPR_LOCATION (*expr_p);
1715 gcc_assert (CONVERT_EXPR_P (*expr_p));
1717 /* Then strip away all but the outermost conversion. */
1718 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1720 /* And remove the outermost conversion if it's useless. */
1721 if (tree_ssa_useless_type_conversion (*expr_p))
1722 *expr_p = TREE_OPERAND (*expr_p, 0);
1724 /* If we still have a conversion at the toplevel,
1725 then canonicalize some constructs. */
1726 if (CONVERT_EXPR_P (*expr_p))
1728 tree sub = TREE_OPERAND (*expr_p, 0);
1730 /* If a NOP conversion is changing the type of a COMPONENT_REF
1731 expression, then canonicalize its type now in order to expose more
1732 redundant conversions. */
1733 if (TREE_CODE (sub) == COMPONENT_REF)
1734 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1736 /* If a NOP conversion is changing a pointer to array of foo
1737 to a pointer to foo, embed that change in the ADDR_EXPR. */
1738 else if (TREE_CODE (sub) == ADDR_EXPR)
1739 canonicalize_addr_expr (expr_p);
1742 /* If we have a conversion to a non-register type force the
1743 use of a VIEW_CONVERT_EXPR instead. */
1744 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1745 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1746 TREE_OPERAND (*expr_p, 0));
1748 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
1749 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
1750 TREE_SET_CODE (*expr_p, NOP_EXPR);
1752 return GS_OK;
1755 /* Nonlocal VLAs seen in the current function. */
1756 static hash_set<tree> *nonlocal_vlas;
1758 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1759 static tree nonlocal_vla_vars;
1761 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1762 DECL_VALUE_EXPR, and it's worth re-examining things. */
1764 static enum gimplify_status
1765 gimplify_var_or_parm_decl (tree *expr_p)
1767 tree decl = *expr_p;
1769 /* ??? If this is a local variable, and it has not been seen in any
1770 outer BIND_EXPR, then it's probably the result of a duplicate
1771 declaration, for which we've already issued an error. It would
1772 be really nice if the front end wouldn't leak these at all.
1773 Currently the only known culprit is C++ destructors, as seen
1774 in g++.old-deja/g++.jason/binding.C. */
1775 if (TREE_CODE (decl) == VAR_DECL
1776 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1777 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1778 && decl_function_context (decl) == current_function_decl)
1780 gcc_assert (seen_error ());
1781 return GS_ERROR;
1784 /* When within an OMP context, notice uses of variables. */
1785 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1786 return GS_ALL_DONE;
1788 /* If the decl is an alias for another expression, substitute it now. */
1789 if (DECL_HAS_VALUE_EXPR_P (decl))
1791 tree value_expr = DECL_VALUE_EXPR (decl);
1793 /* For referenced nonlocal VLAs add a decl for debugging purposes
1794 to the current function. */
1795 if (TREE_CODE (decl) == VAR_DECL
1796 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1797 && nonlocal_vlas != NULL
1798 && TREE_CODE (value_expr) == INDIRECT_REF
1799 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1800 && decl_function_context (decl) != current_function_decl)
1802 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1803 while (ctx
1804 && (ctx->region_type == ORT_WORKSHARE
1805 || ctx->region_type == ORT_SIMD))
1806 ctx = ctx->outer_context;
1807 if (!ctx && !nonlocal_vlas->add (decl))
1809 tree copy = copy_node (decl);
1811 lang_hooks.dup_lang_specific_decl (copy);
1812 SET_DECL_RTL (copy, 0);
1813 TREE_USED (copy) = 1;
1814 DECL_CHAIN (copy) = nonlocal_vla_vars;
1815 nonlocal_vla_vars = copy;
1816 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1817 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1821 *expr_p = unshare_expr (value_expr);
1822 return GS_OK;
1825 return GS_ALL_DONE;
1828 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1830 static void
1831 recalculate_side_effects (tree t)
1833 enum tree_code code = TREE_CODE (t);
1834 int len = TREE_OPERAND_LENGTH (t);
1835 int i;
1837 switch (TREE_CODE_CLASS (code))
1839 case tcc_expression:
1840 switch (code)
1842 case INIT_EXPR:
1843 case MODIFY_EXPR:
1844 case VA_ARG_EXPR:
1845 case PREDECREMENT_EXPR:
1846 case PREINCREMENT_EXPR:
1847 case POSTDECREMENT_EXPR:
1848 case POSTINCREMENT_EXPR:
1849 /* All of these have side-effects, no matter what their
1850 operands are. */
1851 return;
1853 default:
1854 break;
1856 /* Fall through. */
1858 case tcc_comparison: /* a comparison expression */
1859 case tcc_unary: /* a unary arithmetic expression */
1860 case tcc_binary: /* a binary arithmetic expression */
1861 case tcc_reference: /* a reference */
1862 case tcc_vl_exp: /* a function call */
1863 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1864 for (i = 0; i < len; ++i)
1866 tree op = TREE_OPERAND (t, i);
1867 if (op && TREE_SIDE_EFFECTS (op))
1868 TREE_SIDE_EFFECTS (t) = 1;
1870 break;
1872 case tcc_constant:
1873 /* No side-effects. */
1874 return;
1876 default:
1877 gcc_unreachable ();
1881 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1882 node *EXPR_P.
1884 compound_lval
1885 : min_lval '[' val ']'
1886 | min_lval '.' ID
1887 | compound_lval '[' val ']'
1888 | compound_lval '.' ID
1890 This is not part of the original SIMPLE definition, which separates
1891 array and member references, but it seems reasonable to handle them
1892 together. Also, this way we don't run into problems with union
1893 aliasing; gcc requires that for accesses through a union to alias, the
1894 union reference must be explicit, which was not always the case when we
1895 were splitting up array and member refs.
1897 PRE_P points to the sequence where side effects that must happen before
1898 *EXPR_P should be stored.
1900 POST_P points to the sequence where side effects that must happen after
1901 *EXPR_P should be stored. */
1903 static enum gimplify_status
1904 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1905 fallback_t fallback)
1907 tree *p;
1908 enum gimplify_status ret = GS_ALL_DONE, tret;
1909 int i;
1910 location_t loc = EXPR_LOCATION (*expr_p);
1911 tree expr = *expr_p;
1913 /* Create a stack of the subexpressions so later we can walk them in
1914 order from inner to outer. */
1915 auto_vec<tree, 10> expr_stack;
1917 /* We can handle anything that get_inner_reference can deal with. */
1918 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1920 restart:
1921 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1922 if (TREE_CODE (*p) == INDIRECT_REF)
1923 *p = fold_indirect_ref_loc (loc, *p);
1925 if (handled_component_p (*p))
1927 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1928 additional COMPONENT_REFs. */
1929 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1930 && gimplify_var_or_parm_decl (p) == GS_OK)
1931 goto restart;
1932 else
1933 break;
1935 expr_stack.safe_push (*p);
1938 gcc_assert (expr_stack.length ());
1940 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1941 walked through and P points to the innermost expression.
1943 Java requires that we elaborated nodes in source order. That
1944 means we must gimplify the inner expression followed by each of
1945 the indices, in order. But we can't gimplify the inner
1946 expression until we deal with any variable bounds, sizes, or
1947 positions in order to deal with PLACEHOLDER_EXPRs.
1949 So we do this in three steps. First we deal with the annotations
1950 for any variables in the components, then we gimplify the base,
1951 then we gimplify any indices, from left to right. */
1952 for (i = expr_stack.length () - 1; i >= 0; i--)
1954 tree t = expr_stack[i];
1956 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1958 /* Gimplify the low bound and element type size and put them into
1959 the ARRAY_REF. If these values are set, they have already been
1960 gimplified. */
1961 if (TREE_OPERAND (t, 2) == NULL_TREE)
1963 tree low = unshare_expr (array_ref_low_bound (t));
1964 if (!is_gimple_min_invariant (low))
1966 TREE_OPERAND (t, 2) = low;
1967 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1968 post_p, is_gimple_reg,
1969 fb_rvalue);
1970 ret = MIN (ret, tret);
1973 else
1975 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1976 is_gimple_reg, fb_rvalue);
1977 ret = MIN (ret, tret);
1980 if (TREE_OPERAND (t, 3) == NULL_TREE)
1982 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1983 tree elmt_size = unshare_expr (array_ref_element_size (t));
1984 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1986 /* Divide the element size by the alignment of the element
1987 type (above). */
1988 elmt_size
1989 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1991 if (!is_gimple_min_invariant (elmt_size))
1993 TREE_OPERAND (t, 3) = elmt_size;
1994 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1995 post_p, is_gimple_reg,
1996 fb_rvalue);
1997 ret = MIN (ret, tret);
2000 else
2002 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2003 is_gimple_reg, fb_rvalue);
2004 ret = MIN (ret, tret);
2007 else if (TREE_CODE (t) == COMPONENT_REF)
2009 /* Set the field offset into T and gimplify it. */
2010 if (TREE_OPERAND (t, 2) == NULL_TREE)
2012 tree offset = unshare_expr (component_ref_field_offset (t));
2013 tree field = TREE_OPERAND (t, 1);
2014 tree factor
2015 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2017 /* Divide the offset by its alignment. */
2018 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2020 if (!is_gimple_min_invariant (offset))
2022 TREE_OPERAND (t, 2) = offset;
2023 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2024 post_p, is_gimple_reg,
2025 fb_rvalue);
2026 ret = MIN (ret, tret);
2029 else
2031 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2032 is_gimple_reg, fb_rvalue);
2033 ret = MIN (ret, tret);
2038 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2039 so as to match the min_lval predicate. Failure to do so may result
2040 in the creation of large aggregate temporaries. */
2041 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2042 fallback | fb_lvalue);
2043 ret = MIN (ret, tret);
2045 /* And finally, the indices and operands of ARRAY_REF. During this
2046 loop we also remove any useless conversions. */
2047 for (; expr_stack.length () > 0; )
2049 tree t = expr_stack.pop ();
2051 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2053 /* Gimplify the dimension. */
2054 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2056 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2057 is_gimple_val, fb_rvalue);
2058 ret = MIN (ret, tret);
2062 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2064 /* The innermost expression P may have originally had
2065 TREE_SIDE_EFFECTS set which would have caused all the outer
2066 expressions in *EXPR_P leading to P to also have had
2067 TREE_SIDE_EFFECTS set. */
2068 recalculate_side_effects (t);
2071 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2072 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2074 canonicalize_component_ref (expr_p);
2077 expr_stack.release ();
2079 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2081 return ret;
2084 /* Gimplify the self modifying expression pointed to by EXPR_P
2085 (++, --, +=, -=).
2087 PRE_P points to the list where side effects that must happen before
2088 *EXPR_P should be stored.
2090 POST_P points to the list where side effects that must happen after
2091 *EXPR_P should be stored.
2093 WANT_VALUE is nonzero iff we want to use the value of this expression
2094 in another expression.
2096 ARITH_TYPE is the type the computation should be performed in. */
2098 enum gimplify_status
2099 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2100 bool want_value, tree arith_type)
2102 enum tree_code code;
2103 tree lhs, lvalue, rhs, t1;
2104 gimple_seq post = NULL, *orig_post_p = post_p;
2105 bool postfix;
2106 enum tree_code arith_code;
2107 enum gimplify_status ret;
2108 location_t loc = EXPR_LOCATION (*expr_p);
2110 code = TREE_CODE (*expr_p);
2112 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2113 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2115 /* Prefix or postfix? */
2116 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2117 /* Faster to treat as prefix if result is not used. */
2118 postfix = want_value;
2119 else
2120 postfix = false;
2122 /* For postfix, make sure the inner expression's post side effects
2123 are executed after side effects from this expression. */
2124 if (postfix)
2125 post_p = &post;
2127 /* Add or subtract? */
2128 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2129 arith_code = PLUS_EXPR;
2130 else
2131 arith_code = MINUS_EXPR;
2133 /* Gimplify the LHS into a GIMPLE lvalue. */
2134 lvalue = TREE_OPERAND (*expr_p, 0);
2135 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2136 if (ret == GS_ERROR)
2137 return ret;
2139 /* Extract the operands to the arithmetic operation. */
2140 lhs = lvalue;
2141 rhs = TREE_OPERAND (*expr_p, 1);
2143 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2144 that as the result value and in the postqueue operation. */
2145 if (postfix)
2147 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2148 if (ret == GS_ERROR)
2149 return ret;
2151 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2154 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2155 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2157 rhs = convert_to_ptrofftype_loc (loc, rhs);
2158 if (arith_code == MINUS_EXPR)
2159 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2160 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2162 else
2163 t1 = fold_convert (TREE_TYPE (*expr_p),
2164 fold_build2 (arith_code, arith_type,
2165 fold_convert (arith_type, lhs),
2166 fold_convert (arith_type, rhs)));
2168 if (postfix)
2170 gimplify_assign (lvalue, t1, pre_p);
2171 gimplify_seq_add_seq (orig_post_p, post);
2172 *expr_p = lhs;
2173 return GS_ALL_DONE;
2175 else
2177 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2178 return GS_OK;
2182 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2184 static void
2185 maybe_with_size_expr (tree *expr_p)
2187 tree expr = *expr_p;
2188 tree type = TREE_TYPE (expr);
2189 tree size;
2191 /* If we've already wrapped this or the type is error_mark_node, we can't do
2192 anything. */
2193 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2194 || type == error_mark_node)
2195 return;
2197 /* If the size isn't known or is a constant, we have nothing to do. */
2198 size = TYPE_SIZE_UNIT (type);
2199 if (!size || TREE_CODE (size) == INTEGER_CST)
2200 return;
2202 /* Otherwise, make a WITH_SIZE_EXPR. */
2203 size = unshare_expr (size);
2204 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2205 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2208 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2209 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2210 the CALL_EXPR. */
2212 enum gimplify_status
2213 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2215 bool (*test) (tree);
2216 fallback_t fb;
2218 /* In general, we allow lvalues for function arguments to avoid
2219 extra overhead of copying large aggregates out of even larger
2220 aggregates into temporaries only to copy the temporaries to
2221 the argument list. Make optimizers happy by pulling out to
2222 temporaries those types that fit in registers. */
2223 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2224 test = is_gimple_val, fb = fb_rvalue;
2225 else
2227 test = is_gimple_lvalue, fb = fb_either;
2228 /* Also strip a TARGET_EXPR that would force an extra copy. */
2229 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2231 tree init = TARGET_EXPR_INITIAL (*arg_p);
2232 if (init
2233 && !VOID_TYPE_P (TREE_TYPE (init)))
2234 *arg_p = init;
2238 /* If this is a variable sized type, we must remember the size. */
2239 maybe_with_size_expr (arg_p);
2241 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2242 /* Make sure arguments have the same location as the function call
2243 itself. */
2244 protected_set_expr_location (*arg_p, call_location);
2246 /* There is a sequence point before a function call. Side effects in
2247 the argument list must occur before the actual call. So, when
2248 gimplifying arguments, force gimplify_expr to use an internal
2249 post queue which is then appended to the end of PRE_P. */
2250 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2253 /* Don't fold inside offloading regions: it can break code by adding decl
2254 references that weren't in the source. We'll do it during omplower pass
2255 instead. */
2257 static bool
2258 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2260 struct gimplify_omp_ctx *ctx;
2261 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2262 if (ctx->region_type == ORT_TARGET)
2263 return false;
2264 return fold_stmt (gsi);
2267 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2268 WANT_VALUE is true if the result of the call is desired. */
2270 static enum gimplify_status
2271 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2273 tree fndecl, parms, p, fnptrtype;
2274 enum gimplify_status ret;
2275 int i, nargs;
2276 gcall *call;
2277 bool builtin_va_start_p = false;
2278 location_t loc = EXPR_LOCATION (*expr_p);
2280 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2282 /* For reliable diagnostics during inlining, it is necessary that
2283 every call_expr be annotated with file and line. */
2284 if (! EXPR_HAS_LOCATION (*expr_p))
2285 SET_EXPR_LOCATION (*expr_p, input_location);
2287 /* Gimplify internal functions created in the FEs. */
2288 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2290 if (want_value)
2291 return GS_ALL_DONE;
2293 nargs = call_expr_nargs (*expr_p);
2294 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2295 auto_vec<tree> vargs (nargs);
2297 for (i = 0; i < nargs; i++)
2299 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2300 EXPR_LOCATION (*expr_p));
2301 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2303 gimple call = gimple_build_call_internal_vec (ifn, vargs);
2304 gimplify_seq_add_stmt (pre_p, call);
2305 return GS_ALL_DONE;
2308 /* This may be a call to a builtin function.
2310 Builtin function calls may be transformed into different
2311 (and more efficient) builtin function calls under certain
2312 circumstances. Unfortunately, gimplification can muck things
2313 up enough that the builtin expanders are not aware that certain
2314 transformations are still valid.
2316 So we attempt transformation/gimplification of the call before
2317 we gimplify the CALL_EXPR. At this time we do not manage to
2318 transform all calls in the same manner as the expanders do, but
2319 we do transform most of them. */
2320 fndecl = get_callee_fndecl (*expr_p);
2321 if (fndecl
2322 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2323 switch (DECL_FUNCTION_CODE (fndecl))
2325 case BUILT_IN_VA_START:
2327 builtin_va_start_p = TRUE;
2328 if (call_expr_nargs (*expr_p) < 2)
2330 error ("too few arguments to function %<va_start%>");
2331 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2332 return GS_OK;
2335 if (fold_builtin_next_arg (*expr_p, true))
2337 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2338 return GS_OK;
2340 break;
2342 case BUILT_IN_LINE:
2344 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2345 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2346 return GS_OK;
2348 case BUILT_IN_FILE:
2350 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2351 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2352 return GS_OK;
2354 case BUILT_IN_FUNCTION:
2356 const char *function;
2357 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2358 *expr_p = build_string_literal (strlen (function) + 1, function);
2359 return GS_OK;
2361 default:
2364 if (fndecl && DECL_BUILT_IN (fndecl))
2366 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2367 if (new_tree && new_tree != *expr_p)
2369 /* There was a transformation of this call which computes the
2370 same value, but in a more efficient way. Return and try
2371 again. */
2372 *expr_p = new_tree;
2373 return GS_OK;
2377 /* Remember the original function pointer type. */
2378 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2380 /* There is a sequence point before the call, so any side effects in
2381 the calling expression must occur before the actual call. Force
2382 gimplify_expr to use an internal post queue. */
2383 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2384 is_gimple_call_addr, fb_rvalue);
2386 nargs = call_expr_nargs (*expr_p);
2388 /* Get argument types for verification. */
2389 fndecl = get_callee_fndecl (*expr_p);
2390 parms = NULL_TREE;
2391 if (fndecl)
2392 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2393 else
2394 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2396 if (fndecl && DECL_ARGUMENTS (fndecl))
2397 p = DECL_ARGUMENTS (fndecl);
2398 else if (parms)
2399 p = parms;
2400 else
2401 p = NULL_TREE;
2402 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2405 /* If the last argument is __builtin_va_arg_pack () and it is not
2406 passed as a named argument, decrease the number of CALL_EXPR
2407 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2408 if (!p
2409 && i < nargs
2410 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2412 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2413 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2415 if (last_arg_fndecl
2416 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2417 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2418 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2420 tree call = *expr_p;
2422 --nargs;
2423 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2424 CALL_EXPR_FN (call),
2425 nargs, CALL_EXPR_ARGP (call));
2427 /* Copy all CALL_EXPR flags, location and block, except
2428 CALL_EXPR_VA_ARG_PACK flag. */
2429 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2430 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2431 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2432 = CALL_EXPR_RETURN_SLOT_OPT (call);
2433 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2434 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2436 /* Set CALL_EXPR_VA_ARG_PACK. */
2437 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2441 /* Gimplify the function arguments. */
2442 if (nargs > 0)
2444 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2445 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2446 PUSH_ARGS_REVERSED ? i-- : i++)
2448 enum gimplify_status t;
2450 /* Avoid gimplifying the second argument to va_start, which needs to
2451 be the plain PARM_DECL. */
2452 if ((i != 1) || !builtin_va_start_p)
2454 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2455 EXPR_LOCATION (*expr_p));
2457 if (t == GS_ERROR)
2458 ret = GS_ERROR;
2463 /* Gimplify the static chain. */
2464 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
2466 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
2467 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
2468 else
2470 enum gimplify_status t;
2471 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
2472 EXPR_LOCATION (*expr_p));
2473 if (t == GS_ERROR)
2474 ret = GS_ERROR;
2478 /* Verify the function result. */
2479 if (want_value && fndecl
2480 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2482 error_at (loc, "using result of function returning %<void%>");
2483 ret = GS_ERROR;
2486 /* Try this again in case gimplification exposed something. */
2487 if (ret != GS_ERROR)
2489 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2491 if (new_tree && new_tree != *expr_p)
2493 /* There was a transformation of this call which computes the
2494 same value, but in a more efficient way. Return and try
2495 again. */
2496 *expr_p = new_tree;
2497 return GS_OK;
2500 else
2502 *expr_p = error_mark_node;
2503 return GS_ERROR;
2506 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2507 decl. This allows us to eliminate redundant or useless
2508 calls to "const" functions. */
2509 if (TREE_CODE (*expr_p) == CALL_EXPR)
2511 int flags = call_expr_flags (*expr_p);
2512 if (flags & (ECF_CONST | ECF_PURE)
2513 /* An infinite loop is considered a side effect. */
2514 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2515 TREE_SIDE_EFFECTS (*expr_p) = 0;
2518 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2519 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2520 form and delegate the creation of a GIMPLE_CALL to
2521 gimplify_modify_expr. This is always possible because when
2522 WANT_VALUE is true, the caller wants the result of this call into
2523 a temporary, which means that we will emit an INIT_EXPR in
2524 internal_get_tmp_var which will then be handled by
2525 gimplify_modify_expr. */
2526 if (!want_value)
2528 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2529 have to do is replicate it as a GIMPLE_CALL tuple. */
2530 gimple_stmt_iterator gsi;
2531 call = gimple_build_call_from_tree (*expr_p);
2532 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2533 notice_special_calls (call);
2534 gimplify_seq_add_stmt (pre_p, call);
2535 gsi = gsi_last (*pre_p);
2536 maybe_fold_stmt (&gsi);
2537 *expr_p = NULL_TREE;
2539 else
2540 /* Remember the original function type. */
2541 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2542 CALL_EXPR_FN (*expr_p));
2544 return ret;
2547 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2548 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2550 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2551 condition is true or false, respectively. If null, we should generate
2552 our own to skip over the evaluation of this specific expression.
2554 LOCUS is the source location of the COND_EXPR.
2556 This function is the tree equivalent of do_jump.
2558 shortcut_cond_r should only be called by shortcut_cond_expr. */
2560 static tree
2561 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2562 location_t locus)
2564 tree local_label = NULL_TREE;
2565 tree t, expr = NULL;
2567 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2568 retain the shortcut semantics. Just insert the gotos here;
2569 shortcut_cond_expr will append the real blocks later. */
2570 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2572 location_t new_locus;
2574 /* Turn if (a && b) into
2576 if (a); else goto no;
2577 if (b) goto yes; else goto no;
2578 (no:) */
2580 if (false_label_p == NULL)
2581 false_label_p = &local_label;
2583 /* Keep the original source location on the first 'if'. */
2584 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2585 append_to_statement_list (t, &expr);
2587 /* Set the source location of the && on the second 'if'. */
2588 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2589 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2590 new_locus);
2591 append_to_statement_list (t, &expr);
2593 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2595 location_t new_locus;
2597 /* Turn if (a || b) into
2599 if (a) goto yes;
2600 if (b) goto yes; else goto no;
2601 (yes:) */
2603 if (true_label_p == NULL)
2604 true_label_p = &local_label;
2606 /* Keep the original source location on the first 'if'. */
2607 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2608 append_to_statement_list (t, &expr);
2610 /* Set the source location of the || on the second 'if'. */
2611 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2612 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2613 new_locus);
2614 append_to_statement_list (t, &expr);
2616 else if (TREE_CODE (pred) == COND_EXPR
2617 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2618 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2620 location_t new_locus;
2622 /* As long as we're messing with gotos, turn if (a ? b : c) into
2623 if (a)
2624 if (b) goto yes; else goto no;
2625 else
2626 if (c) goto yes; else goto no;
2628 Don't do this if one of the arms has void type, which can happen
2629 in C++ when the arm is throw. */
2631 /* Keep the original source location on the first 'if'. Set the source
2632 location of the ? on the second 'if'. */
2633 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2634 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2635 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2636 false_label_p, locus),
2637 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2638 false_label_p, new_locus));
2640 else
2642 expr = build3 (COND_EXPR, void_type_node, pred,
2643 build_and_jump (true_label_p),
2644 build_and_jump (false_label_p));
2645 SET_EXPR_LOCATION (expr, locus);
2648 if (local_label)
2650 t = build1 (LABEL_EXPR, void_type_node, local_label);
2651 append_to_statement_list (t, &expr);
2654 return expr;
2657 /* Given a conditional expression EXPR with short-circuit boolean
2658 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2659 predicate apart into the equivalent sequence of conditionals. */
2661 static tree
2662 shortcut_cond_expr (tree expr)
2664 tree pred = TREE_OPERAND (expr, 0);
2665 tree then_ = TREE_OPERAND (expr, 1);
2666 tree else_ = TREE_OPERAND (expr, 2);
2667 tree true_label, false_label, end_label, t;
2668 tree *true_label_p;
2669 tree *false_label_p;
2670 bool emit_end, emit_false, jump_over_else;
2671 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2672 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2674 /* First do simple transformations. */
2675 if (!else_se)
2677 /* If there is no 'else', turn
2678 if (a && b) then c
2679 into
2680 if (a) if (b) then c. */
2681 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2683 /* Keep the original source location on the first 'if'. */
2684 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2685 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2686 /* Set the source location of the && on the second 'if'. */
2687 if (EXPR_HAS_LOCATION (pred))
2688 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2689 then_ = shortcut_cond_expr (expr);
2690 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2691 pred = TREE_OPERAND (pred, 0);
2692 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2693 SET_EXPR_LOCATION (expr, locus);
2697 if (!then_se)
2699 /* If there is no 'then', turn
2700 if (a || b); else d
2701 into
2702 if (a); else if (b); else d. */
2703 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2705 /* Keep the original source location on the first 'if'. */
2706 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2707 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2708 /* Set the source location of the || on the second 'if'. */
2709 if (EXPR_HAS_LOCATION (pred))
2710 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2711 else_ = shortcut_cond_expr (expr);
2712 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2713 pred = TREE_OPERAND (pred, 0);
2714 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2715 SET_EXPR_LOCATION (expr, locus);
2719 /* If we're done, great. */
2720 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2721 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2722 return expr;
2724 /* Otherwise we need to mess with gotos. Change
2725 if (a) c; else d;
2727 if (a); else goto no;
2728 c; goto end;
2729 no: d; end:
2730 and recursively gimplify the condition. */
2732 true_label = false_label = end_label = NULL_TREE;
2734 /* If our arms just jump somewhere, hijack those labels so we don't
2735 generate jumps to jumps. */
2737 if (then_
2738 && TREE_CODE (then_) == GOTO_EXPR
2739 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2741 true_label = GOTO_DESTINATION (then_);
2742 then_ = NULL;
2743 then_se = false;
2746 if (else_
2747 && TREE_CODE (else_) == GOTO_EXPR
2748 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2750 false_label = GOTO_DESTINATION (else_);
2751 else_ = NULL;
2752 else_se = false;
2755 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2756 if (true_label)
2757 true_label_p = &true_label;
2758 else
2759 true_label_p = NULL;
2761 /* The 'else' branch also needs a label if it contains interesting code. */
2762 if (false_label || else_se)
2763 false_label_p = &false_label;
2764 else
2765 false_label_p = NULL;
2767 /* If there was nothing else in our arms, just forward the label(s). */
2768 if (!then_se && !else_se)
2769 return shortcut_cond_r (pred, true_label_p, false_label_p,
2770 EXPR_LOC_OR_LOC (expr, input_location));
2772 /* If our last subexpression already has a terminal label, reuse it. */
2773 if (else_se)
2774 t = expr_last (else_);
2775 else if (then_se)
2776 t = expr_last (then_);
2777 else
2778 t = NULL;
2779 if (t && TREE_CODE (t) == LABEL_EXPR)
2780 end_label = LABEL_EXPR_LABEL (t);
2782 /* If we don't care about jumping to the 'else' branch, jump to the end
2783 if the condition is false. */
2784 if (!false_label_p)
2785 false_label_p = &end_label;
2787 /* We only want to emit these labels if we aren't hijacking them. */
2788 emit_end = (end_label == NULL_TREE);
2789 emit_false = (false_label == NULL_TREE);
2791 /* We only emit the jump over the else clause if we have to--if the
2792 then clause may fall through. Otherwise we can wind up with a
2793 useless jump and a useless label at the end of gimplified code,
2794 which will cause us to think that this conditional as a whole
2795 falls through even if it doesn't. If we then inline a function
2796 which ends with such a condition, that can cause us to issue an
2797 inappropriate warning about control reaching the end of a
2798 non-void function. */
2799 jump_over_else = block_may_fallthru (then_);
2801 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2802 EXPR_LOC_OR_LOC (expr, input_location));
2804 expr = NULL;
2805 append_to_statement_list (pred, &expr);
2807 append_to_statement_list (then_, &expr);
2808 if (else_se)
2810 if (jump_over_else)
2812 tree last = expr_last (expr);
2813 t = build_and_jump (&end_label);
2814 if (EXPR_HAS_LOCATION (last))
2815 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2816 append_to_statement_list (t, &expr);
2818 if (emit_false)
2820 t = build1 (LABEL_EXPR, void_type_node, false_label);
2821 append_to_statement_list (t, &expr);
2823 append_to_statement_list (else_, &expr);
2825 if (emit_end && end_label)
2827 t = build1 (LABEL_EXPR, void_type_node, end_label);
2828 append_to_statement_list (t, &expr);
2831 return expr;
2834 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2836 tree
2837 gimple_boolify (tree expr)
2839 tree type = TREE_TYPE (expr);
2840 location_t loc = EXPR_LOCATION (expr);
2842 if (TREE_CODE (expr) == NE_EXPR
2843 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2844 && integer_zerop (TREE_OPERAND (expr, 1)))
2846 tree call = TREE_OPERAND (expr, 0);
2847 tree fn = get_callee_fndecl (call);
2849 /* For __builtin_expect ((long) (x), y) recurse into x as well
2850 if x is truth_value_p. */
2851 if (fn
2852 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2853 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2854 && call_expr_nargs (call) == 2)
2856 tree arg = CALL_EXPR_ARG (call, 0);
2857 if (arg)
2859 if (TREE_CODE (arg) == NOP_EXPR
2860 && TREE_TYPE (arg) == TREE_TYPE (call))
2861 arg = TREE_OPERAND (arg, 0);
2862 if (truth_value_p (TREE_CODE (arg)))
2864 arg = gimple_boolify (arg);
2865 CALL_EXPR_ARG (call, 0)
2866 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2872 switch (TREE_CODE (expr))
2874 case TRUTH_AND_EXPR:
2875 case TRUTH_OR_EXPR:
2876 case TRUTH_XOR_EXPR:
2877 case TRUTH_ANDIF_EXPR:
2878 case TRUTH_ORIF_EXPR:
2879 /* Also boolify the arguments of truth exprs. */
2880 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2881 /* FALLTHRU */
2883 case TRUTH_NOT_EXPR:
2884 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2886 /* These expressions always produce boolean results. */
2887 if (TREE_CODE (type) != BOOLEAN_TYPE)
2888 TREE_TYPE (expr) = boolean_type_node;
2889 return expr;
2891 case ANNOTATE_EXPR:
2892 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2894 case annot_expr_ivdep_kind:
2895 case annot_expr_no_vector_kind:
2896 case annot_expr_vector_kind:
2897 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2898 if (TREE_CODE (type) != BOOLEAN_TYPE)
2899 TREE_TYPE (expr) = boolean_type_node;
2900 return expr;
2901 default:
2902 gcc_unreachable ();
2905 default:
2906 if (COMPARISON_CLASS_P (expr))
2908 /* There expressions always prduce boolean results. */
2909 if (TREE_CODE (type) != BOOLEAN_TYPE)
2910 TREE_TYPE (expr) = boolean_type_node;
2911 return expr;
2913 /* Other expressions that get here must have boolean values, but
2914 might need to be converted to the appropriate mode. */
2915 if (TREE_CODE (type) == BOOLEAN_TYPE)
2916 return expr;
2917 return fold_convert_loc (loc, boolean_type_node, expr);
2921 /* Given a conditional expression *EXPR_P without side effects, gimplify
2922 its operands. New statements are inserted to PRE_P. */
2924 static enum gimplify_status
2925 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2927 tree expr = *expr_p, cond;
2928 enum gimplify_status ret, tret;
2929 enum tree_code code;
2931 cond = gimple_boolify (COND_EXPR_COND (expr));
2933 /* We need to handle && and || specially, as their gimplification
2934 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2935 code = TREE_CODE (cond);
2936 if (code == TRUTH_ANDIF_EXPR)
2937 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2938 else if (code == TRUTH_ORIF_EXPR)
2939 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2940 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2941 COND_EXPR_COND (*expr_p) = cond;
2943 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2944 is_gimple_val, fb_rvalue);
2945 ret = MIN (ret, tret);
2946 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2947 is_gimple_val, fb_rvalue);
2949 return MIN (ret, tret);
2952 /* Return true if evaluating EXPR could trap.
2953 EXPR is GENERIC, while tree_could_trap_p can be called
2954 only on GIMPLE. */
2956 static bool
2957 generic_expr_could_trap_p (tree expr)
2959 unsigned i, n;
2961 if (!expr || is_gimple_val (expr))
2962 return false;
2964 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2965 return true;
2967 n = TREE_OPERAND_LENGTH (expr);
2968 for (i = 0; i < n; i++)
2969 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2970 return true;
2972 return false;
2975 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2976 into
2978 if (p) if (p)
2979 t1 = a; a;
2980 else or else
2981 t1 = b; b;
2984 The second form is used when *EXPR_P is of type void.
2986 PRE_P points to the list where side effects that must happen before
2987 *EXPR_P should be stored. */
2989 static enum gimplify_status
2990 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2992 tree expr = *expr_p;
2993 tree type = TREE_TYPE (expr);
2994 location_t loc = EXPR_LOCATION (expr);
2995 tree tmp, arm1, arm2;
2996 enum gimplify_status ret;
2997 tree label_true, label_false, label_cont;
2998 bool have_then_clause_p, have_else_clause_p;
2999 gcond *cond_stmt;
3000 enum tree_code pred_code;
3001 gimple_seq seq = NULL;
3003 /* If this COND_EXPR has a value, copy the values into a temporary within
3004 the arms. */
3005 if (!VOID_TYPE_P (type))
3007 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3008 tree result;
3010 /* If either an rvalue is ok or we do not require an lvalue, create the
3011 temporary. But we cannot do that if the type is addressable. */
3012 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3013 && !TREE_ADDRESSABLE (type))
3015 if (gimplify_ctxp->allow_rhs_cond_expr
3016 /* If either branch has side effects or could trap, it can't be
3017 evaluated unconditionally. */
3018 && !TREE_SIDE_EFFECTS (then_)
3019 && !generic_expr_could_trap_p (then_)
3020 && !TREE_SIDE_EFFECTS (else_)
3021 && !generic_expr_could_trap_p (else_))
3022 return gimplify_pure_cond_expr (expr_p, pre_p);
3024 tmp = create_tmp_var (type, "iftmp");
3025 result = tmp;
3028 /* Otherwise, only create and copy references to the values. */
3029 else
3031 type = build_pointer_type (type);
3033 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3034 then_ = build_fold_addr_expr_loc (loc, then_);
3036 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3037 else_ = build_fold_addr_expr_loc (loc, else_);
3039 expr
3040 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3042 tmp = create_tmp_var (type, "iftmp");
3043 result = build_simple_mem_ref_loc (loc, tmp);
3046 /* Build the new then clause, `tmp = then_;'. But don't build the
3047 assignment if the value is void; in C++ it can be if it's a throw. */
3048 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3049 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3051 /* Similarly, build the new else clause, `tmp = else_;'. */
3052 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3053 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3055 TREE_TYPE (expr) = void_type_node;
3056 recalculate_side_effects (expr);
3058 /* Move the COND_EXPR to the prequeue. */
3059 gimplify_stmt (&expr, pre_p);
3061 *expr_p = result;
3062 return GS_ALL_DONE;
3065 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3066 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3067 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3068 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3070 /* Make sure the condition has BOOLEAN_TYPE. */
3071 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3073 /* Break apart && and || conditions. */
3074 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3075 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3077 expr = shortcut_cond_expr (expr);
3079 if (expr != *expr_p)
3081 *expr_p = expr;
3083 /* We can't rely on gimplify_expr to re-gimplify the expanded
3084 form properly, as cleanups might cause the target labels to be
3085 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3086 set up a conditional context. */
3087 gimple_push_condition ();
3088 gimplify_stmt (expr_p, &seq);
3089 gimple_pop_condition (pre_p);
3090 gimple_seq_add_seq (pre_p, seq);
3092 return GS_ALL_DONE;
3096 /* Now do the normal gimplification. */
3098 /* Gimplify condition. */
3099 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3100 fb_rvalue);
3101 if (ret == GS_ERROR)
3102 return GS_ERROR;
3103 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3105 gimple_push_condition ();
3107 have_then_clause_p = have_else_clause_p = false;
3108 if (TREE_OPERAND (expr, 1) != NULL
3109 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3110 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3111 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3112 == current_function_decl)
3113 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3114 have different locations, otherwise we end up with incorrect
3115 location information on the branches. */
3116 && (optimize
3117 || !EXPR_HAS_LOCATION (expr)
3118 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3119 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3121 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3122 have_then_clause_p = true;
3124 else
3125 label_true = create_artificial_label (UNKNOWN_LOCATION);
3126 if (TREE_OPERAND (expr, 2) != NULL
3127 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3128 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3129 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3130 == current_function_decl)
3131 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3132 have different locations, otherwise we end up with incorrect
3133 location information on the branches. */
3134 && (optimize
3135 || !EXPR_HAS_LOCATION (expr)
3136 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3137 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3139 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3140 have_else_clause_p = true;
3142 else
3143 label_false = create_artificial_label (UNKNOWN_LOCATION);
3145 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3146 &arm2);
3148 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3149 label_false);
3151 gimplify_seq_add_stmt (&seq, cond_stmt);
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 if (cleared)
3982 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3983 Note that we still have to gimplify, in order to handle the
3984 case of variable sized types. Avoid shared tree structures. */
3985 CONSTRUCTOR_ELTS (ctor) = NULL;
3986 TREE_SIDE_EFFECTS (ctor) = 0;
3987 object = unshare_expr (object);
3988 gimplify_stmt (expr_p, pre_p);
3991 /* If we have not block cleared the object, or if there are nonzero
3992 elements in the constructor, add assignments to the individual
3993 scalar fields of the object. */
3994 if (!cleared || num_nonzero_elements > 0)
3995 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3997 *expr_p = NULL_TREE;
3999 break;
4001 case COMPLEX_TYPE:
4003 tree r, i;
4005 if (notify_temp_creation)
4006 return GS_OK;
4008 /* Extract the real and imaginary parts out of the ctor. */
4009 gcc_assert (elts->length () == 2);
4010 r = (*elts)[0].value;
4011 i = (*elts)[1].value;
4012 if (r == NULL || i == NULL)
4014 tree zero = build_zero_cst (TREE_TYPE (type));
4015 if (r == NULL)
4016 r = zero;
4017 if (i == NULL)
4018 i = zero;
4021 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4022 represent creation of a complex value. */
4023 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4025 ctor = build_complex (type, r, i);
4026 TREE_OPERAND (*expr_p, 1) = ctor;
4028 else
4030 ctor = build2 (COMPLEX_EXPR, type, r, i);
4031 TREE_OPERAND (*expr_p, 1) = ctor;
4032 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4033 pre_p,
4034 post_p,
4035 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4036 fb_rvalue);
4039 break;
4041 case VECTOR_TYPE:
4043 unsigned HOST_WIDE_INT ix;
4044 constructor_elt *ce;
4046 if (notify_temp_creation)
4047 return GS_OK;
4049 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4050 if (TREE_CONSTANT (ctor))
4052 bool constant_p = true;
4053 tree value;
4055 /* Even when ctor is constant, it might contain non-*_CST
4056 elements, such as addresses or trapping values like
4057 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4058 in VECTOR_CST nodes. */
4059 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4060 if (!CONSTANT_CLASS_P (value))
4062 constant_p = false;
4063 break;
4066 if (constant_p)
4068 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4069 break;
4072 TREE_CONSTANT (ctor) = 0;
4075 /* Vector types use CONSTRUCTOR all the way through gimple
4076 compilation as a general initializer. */
4077 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4079 enum gimplify_status tret;
4080 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4081 fb_rvalue);
4082 if (tret == GS_ERROR)
4083 ret = GS_ERROR;
4085 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4086 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4088 break;
4090 default:
4091 /* So how did we get a CONSTRUCTOR for a scalar type? */
4092 gcc_unreachable ();
4095 if (ret == GS_ERROR)
4096 return GS_ERROR;
4097 else if (want_value)
4099 *expr_p = object;
4100 return GS_OK;
4102 else
4104 /* If we have gimplified both sides of the initializer but have
4105 not emitted an assignment, do so now. */
4106 if (*expr_p)
4108 tree lhs = TREE_OPERAND (*expr_p, 0);
4109 tree rhs = TREE_OPERAND (*expr_p, 1);
4110 gassign *init = gimple_build_assign (lhs, rhs);
4111 gimplify_seq_add_stmt (pre_p, init);
4112 *expr_p = NULL;
4115 return GS_ALL_DONE;
4119 /* Given a pointer value OP0, return a simplified version of an
4120 indirection through OP0, or NULL_TREE if no simplification is
4121 possible. This may only be applied to a rhs of an expression.
4122 Note that the resulting type may be different from the type pointed
4123 to in the sense that it is still compatible from the langhooks
4124 point of view. */
4126 static tree
4127 gimple_fold_indirect_ref_rhs (tree t)
4129 return gimple_fold_indirect_ref (t);
4132 /* Subroutine of gimplify_modify_expr to do simplifications of
4133 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4134 something changes. */
4136 static enum gimplify_status
4137 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4138 gimple_seq *pre_p, gimple_seq *post_p,
4139 bool want_value)
4141 enum gimplify_status ret = GS_UNHANDLED;
4142 bool changed;
4146 changed = false;
4147 switch (TREE_CODE (*from_p))
4149 case VAR_DECL:
4150 /* If we're assigning from a read-only variable initialized with
4151 a constructor, do the direct assignment from the constructor,
4152 but only if neither source nor target are volatile since this
4153 latter assignment might end up being done on a per-field basis. */
4154 if (DECL_INITIAL (*from_p)
4155 && TREE_READONLY (*from_p)
4156 && !TREE_THIS_VOLATILE (*from_p)
4157 && !TREE_THIS_VOLATILE (*to_p)
4158 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4160 tree old_from = *from_p;
4161 enum gimplify_status subret;
4163 /* Move the constructor into the RHS. */
4164 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4166 /* Let's see if gimplify_init_constructor will need to put
4167 it in memory. */
4168 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4169 false, true);
4170 if (subret == GS_ERROR)
4172 /* If so, revert the change. */
4173 *from_p = old_from;
4175 else
4177 ret = GS_OK;
4178 changed = true;
4181 break;
4182 case INDIRECT_REF:
4184 /* If we have code like
4186 *(const A*)(A*)&x
4188 where the type of "x" is a (possibly cv-qualified variant
4189 of "A"), treat the entire expression as identical to "x".
4190 This kind of code arises in C++ when an object is bound
4191 to a const reference, and if "x" is a TARGET_EXPR we want
4192 to take advantage of the optimization below. */
4193 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4194 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4195 if (t)
4197 if (TREE_THIS_VOLATILE (t) != volatile_p)
4199 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4200 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4201 build_fold_addr_expr (t));
4202 if (REFERENCE_CLASS_P (t))
4203 TREE_THIS_VOLATILE (t) = volatile_p;
4205 *from_p = t;
4206 ret = GS_OK;
4207 changed = true;
4209 break;
4212 case TARGET_EXPR:
4214 /* If we are initializing something from a TARGET_EXPR, strip the
4215 TARGET_EXPR and initialize it directly, if possible. This can't
4216 be done if the initializer is void, since that implies that the
4217 temporary is set in some non-trivial way.
4219 ??? What about code that pulls out the temp and uses it
4220 elsewhere? I think that such code never uses the TARGET_EXPR as
4221 an initializer. If I'm wrong, we'll die because the temp won't
4222 have any RTL. In that case, I guess we'll need to replace
4223 references somehow. */
4224 tree init = TARGET_EXPR_INITIAL (*from_p);
4226 if (init
4227 && !VOID_TYPE_P (TREE_TYPE (init)))
4229 *from_p = init;
4230 ret = GS_OK;
4231 changed = true;
4234 break;
4236 case COMPOUND_EXPR:
4237 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4238 caught. */
4239 gimplify_compound_expr (from_p, pre_p, true);
4240 ret = GS_OK;
4241 changed = true;
4242 break;
4244 case CONSTRUCTOR:
4245 /* If we already made some changes, let the front end have a
4246 crack at this before we break it down. */
4247 if (ret != GS_UNHANDLED)
4248 break;
4249 /* If we're initializing from a CONSTRUCTOR, break this into
4250 individual MODIFY_EXPRs. */
4251 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4252 false);
4254 case COND_EXPR:
4255 /* If we're assigning to a non-register type, push the assignment
4256 down into the branches. This is mandatory for ADDRESSABLE types,
4257 since we cannot generate temporaries for such, but it saves a
4258 copy in other cases as well. */
4259 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4261 /* This code should mirror the code in gimplify_cond_expr. */
4262 enum tree_code code = TREE_CODE (*expr_p);
4263 tree cond = *from_p;
4264 tree result = *to_p;
4266 ret = gimplify_expr (&result, pre_p, post_p,
4267 is_gimple_lvalue, fb_lvalue);
4268 if (ret != GS_ERROR)
4269 ret = GS_OK;
4271 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4272 TREE_OPERAND (cond, 1)
4273 = build2 (code, void_type_node, result,
4274 TREE_OPERAND (cond, 1));
4275 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4276 TREE_OPERAND (cond, 2)
4277 = build2 (code, void_type_node, unshare_expr (result),
4278 TREE_OPERAND (cond, 2));
4280 TREE_TYPE (cond) = void_type_node;
4281 recalculate_side_effects (cond);
4283 if (want_value)
4285 gimplify_and_add (cond, pre_p);
4286 *expr_p = unshare_expr (result);
4288 else
4289 *expr_p = cond;
4290 return ret;
4292 break;
4294 case CALL_EXPR:
4295 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4296 return slot so that we don't generate a temporary. */
4297 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4298 && aggregate_value_p (*from_p, *from_p))
4300 bool use_target;
4302 if (!(rhs_predicate_for (*to_p))(*from_p))
4303 /* If we need a temporary, *to_p isn't accurate. */
4304 use_target = false;
4305 /* It's OK to use the return slot directly unless it's an NRV. */
4306 else if (TREE_CODE (*to_p) == RESULT_DECL
4307 && DECL_NAME (*to_p) == NULL_TREE
4308 && needs_to_live_in_memory (*to_p))
4309 use_target = true;
4310 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4311 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4312 /* Don't force regs into memory. */
4313 use_target = false;
4314 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4315 /* It's OK to use the target directly if it's being
4316 initialized. */
4317 use_target = true;
4318 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4319 /* Always use the target and thus RSO for variable-sized types.
4320 GIMPLE cannot deal with a variable-sized assignment
4321 embedded in a call statement. */
4322 use_target = true;
4323 else if (TREE_CODE (*to_p) != SSA_NAME
4324 && (!is_gimple_variable (*to_p)
4325 || needs_to_live_in_memory (*to_p)))
4326 /* Don't use the original target if it's already addressable;
4327 if its address escapes, and the called function uses the
4328 NRV optimization, a conforming program could see *to_p
4329 change before the called function returns; see c++/19317.
4330 When optimizing, the return_slot pass marks more functions
4331 as safe after we have escape info. */
4332 use_target = false;
4333 else
4334 use_target = true;
4336 if (use_target)
4338 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4339 mark_addressable (*to_p);
4342 break;
4344 case WITH_SIZE_EXPR:
4345 /* Likewise for calls that return an aggregate of non-constant size,
4346 since we would not be able to generate a temporary at all. */
4347 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4349 *from_p = TREE_OPERAND (*from_p, 0);
4350 /* We don't change ret in this case because the
4351 WITH_SIZE_EXPR might have been added in
4352 gimplify_modify_expr, so returning GS_OK would lead to an
4353 infinite loop. */
4354 changed = true;
4356 break;
4358 /* If we're initializing from a container, push the initialization
4359 inside it. */
4360 case CLEANUP_POINT_EXPR:
4361 case BIND_EXPR:
4362 case STATEMENT_LIST:
4364 tree wrap = *from_p;
4365 tree t;
4367 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4368 fb_lvalue);
4369 if (ret != GS_ERROR)
4370 ret = GS_OK;
4372 t = voidify_wrapper_expr (wrap, *expr_p);
4373 gcc_assert (t == *expr_p);
4375 if (want_value)
4377 gimplify_and_add (wrap, pre_p);
4378 *expr_p = unshare_expr (*to_p);
4380 else
4381 *expr_p = wrap;
4382 return GS_OK;
4385 case COMPOUND_LITERAL_EXPR:
4387 tree complit = TREE_OPERAND (*expr_p, 1);
4388 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4389 tree decl = DECL_EXPR_DECL (decl_s);
4390 tree init = DECL_INITIAL (decl);
4392 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4393 into struct T x = { 0, 1, 2 } if the address of the
4394 compound literal has never been taken. */
4395 if (!TREE_ADDRESSABLE (complit)
4396 && !TREE_ADDRESSABLE (decl)
4397 && init)
4399 *expr_p = copy_node (*expr_p);
4400 TREE_OPERAND (*expr_p, 1) = init;
4401 return GS_OK;
4405 default:
4406 break;
4409 while (changed);
4411 return ret;
4415 /* Return true if T looks like a valid GIMPLE statement. */
4417 static bool
4418 is_gimple_stmt (tree t)
4420 const enum tree_code code = TREE_CODE (t);
4422 switch (code)
4424 case NOP_EXPR:
4425 /* The only valid NOP_EXPR is the empty statement. */
4426 return IS_EMPTY_STMT (t);
4428 case BIND_EXPR:
4429 case COND_EXPR:
4430 /* These are only valid if they're void. */
4431 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4433 case SWITCH_EXPR:
4434 case GOTO_EXPR:
4435 case RETURN_EXPR:
4436 case LABEL_EXPR:
4437 case CASE_LABEL_EXPR:
4438 case TRY_CATCH_EXPR:
4439 case TRY_FINALLY_EXPR:
4440 case EH_FILTER_EXPR:
4441 case CATCH_EXPR:
4442 case ASM_EXPR:
4443 case STATEMENT_LIST:
4444 case OACC_PARALLEL:
4445 case OACC_KERNELS:
4446 case OACC_DATA:
4447 case OACC_HOST_DATA:
4448 case OACC_DECLARE:
4449 case OACC_UPDATE:
4450 case OACC_ENTER_DATA:
4451 case OACC_EXIT_DATA:
4452 case OACC_CACHE:
4453 case OMP_PARALLEL:
4454 case OMP_FOR:
4455 case OMP_SIMD:
4456 case CILK_SIMD:
4457 case OMP_DISTRIBUTE:
4458 case OACC_LOOP:
4459 case OMP_SECTIONS:
4460 case OMP_SECTION:
4461 case OMP_SINGLE:
4462 case OMP_MASTER:
4463 case OMP_TASKGROUP:
4464 case OMP_ORDERED:
4465 case OMP_CRITICAL:
4466 case OMP_TASK:
4467 /* These are always void. */
4468 return true;
4470 case CALL_EXPR:
4471 case MODIFY_EXPR:
4472 case PREDICT_EXPR:
4473 /* These are valid regardless of their type. */
4474 return true;
4476 default:
4477 return false;
4482 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4483 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4484 DECL_GIMPLE_REG_P set.
4486 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4487 other, unmodified part of the complex object just before the total store.
4488 As a consequence, if the object is still uninitialized, an undefined value
4489 will be loaded into a register, which may result in a spurious exception
4490 if the register is floating-point and the value happens to be a signaling
4491 NaN for example. Then the fully-fledged complex operations lowering pass
4492 followed by a DCE pass are necessary in order to fix things up. */
4494 static enum gimplify_status
4495 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4496 bool want_value)
4498 enum tree_code code, ocode;
4499 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4501 lhs = TREE_OPERAND (*expr_p, 0);
4502 rhs = TREE_OPERAND (*expr_p, 1);
4503 code = TREE_CODE (lhs);
4504 lhs = TREE_OPERAND (lhs, 0);
4506 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4507 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4508 TREE_NO_WARNING (other) = 1;
4509 other = get_formal_tmp_var (other, pre_p);
4511 realpart = code == REALPART_EXPR ? rhs : other;
4512 imagpart = code == REALPART_EXPR ? other : rhs;
4514 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4515 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4516 else
4517 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4519 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4520 *expr_p = (want_value) ? rhs : NULL_TREE;
4522 return GS_ALL_DONE;
4525 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4527 modify_expr
4528 : varname '=' rhs
4529 | '*' ID '=' rhs
4531 PRE_P points to the list where side effects that must happen before
4532 *EXPR_P should be stored.
4534 POST_P points to the list where side effects that must happen after
4535 *EXPR_P should be stored.
4537 WANT_VALUE is nonzero iff we want to use the value of this expression
4538 in another expression. */
4540 static enum gimplify_status
4541 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4542 bool want_value)
4544 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4545 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4546 enum gimplify_status ret = GS_UNHANDLED;
4547 gimple assign;
4548 location_t loc = EXPR_LOCATION (*expr_p);
4549 gimple_stmt_iterator gsi;
4551 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4552 || TREE_CODE (*expr_p) == INIT_EXPR);
4554 /* Trying to simplify a clobber using normal logic doesn't work,
4555 so handle it here. */
4556 if (TREE_CLOBBER_P (*from_p))
4558 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4559 if (ret == GS_ERROR)
4560 return ret;
4561 gcc_assert (!want_value
4562 && (TREE_CODE (*to_p) == VAR_DECL
4563 || TREE_CODE (*to_p) == MEM_REF));
4564 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4565 *expr_p = NULL;
4566 return GS_ALL_DONE;
4569 /* Insert pointer conversions required by the middle-end that are not
4570 required by the frontend. This fixes middle-end type checking for
4571 for example gcc.dg/redecl-6.c. */
4572 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4574 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4575 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4576 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4579 /* See if any simplifications can be done based on what the RHS is. */
4580 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4581 want_value);
4582 if (ret != GS_UNHANDLED)
4583 return ret;
4585 /* For zero sized types only gimplify the left hand side and right hand
4586 side as statements and throw away the assignment. Do this after
4587 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4588 types properly. */
4589 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4591 gimplify_stmt (from_p, pre_p);
4592 gimplify_stmt (to_p, pre_p);
4593 *expr_p = NULL_TREE;
4594 return GS_ALL_DONE;
4597 /* If the value being copied is of variable width, compute the length
4598 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4599 before gimplifying any of the operands so that we can resolve any
4600 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4601 the size of the expression to be copied, not of the destination, so
4602 that is what we must do here. */
4603 maybe_with_size_expr (from_p);
4605 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4606 if (ret == GS_ERROR)
4607 return ret;
4609 /* As a special case, we have to temporarily allow for assignments
4610 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4611 a toplevel statement, when gimplifying the GENERIC expression
4612 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4613 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4615 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4616 prevent gimplify_expr from trying to create a new temporary for
4617 foo's LHS, we tell it that it should only gimplify until it
4618 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4619 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4620 and all we need to do here is set 'a' to be its LHS. */
4621 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4622 fb_rvalue);
4623 if (ret == GS_ERROR)
4624 return ret;
4626 /* Now see if the above changed *from_p to something we handle specially. */
4627 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4628 want_value);
4629 if (ret != GS_UNHANDLED)
4630 return ret;
4632 /* If we've got a variable sized assignment between two lvalues (i.e. does
4633 not involve a call), then we can make things a bit more straightforward
4634 by converting the assignment to memcpy or memset. */
4635 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4637 tree from = TREE_OPERAND (*from_p, 0);
4638 tree size = TREE_OPERAND (*from_p, 1);
4640 if (TREE_CODE (from) == CONSTRUCTOR)
4641 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4643 if (is_gimple_addressable (from))
4645 *from_p = from;
4646 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4647 pre_p);
4651 /* Transform partial stores to non-addressable complex variables into
4652 total stores. This allows us to use real instead of virtual operands
4653 for these variables, which improves optimization. */
4654 if ((TREE_CODE (*to_p) == REALPART_EXPR
4655 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4656 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4657 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4659 /* Try to alleviate the effects of the gimplification creating artificial
4660 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4661 if (!gimplify_ctxp->into_ssa
4662 && TREE_CODE (*from_p) == VAR_DECL
4663 && DECL_IGNORED_P (*from_p)
4664 && DECL_P (*to_p)
4665 && !DECL_IGNORED_P (*to_p))
4667 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4668 DECL_NAME (*from_p)
4669 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4670 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4671 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4674 if (want_value && TREE_THIS_VOLATILE (*to_p))
4675 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4677 if (TREE_CODE (*from_p) == CALL_EXPR)
4679 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4680 instead of a GIMPLE_ASSIGN. */
4681 gcall *call_stmt;
4682 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
4684 /* Gimplify internal functions created in the FEs. */
4685 int nargs = call_expr_nargs (*from_p), i;
4686 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
4687 auto_vec<tree> vargs (nargs);
4689 for (i = 0; i < nargs; i++)
4691 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
4692 EXPR_LOCATION (*from_p));
4693 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
4695 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
4696 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
4698 else
4700 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4701 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4702 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4703 tree fndecl = get_callee_fndecl (*from_p);
4704 if (fndecl
4705 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4706 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4707 && call_expr_nargs (*from_p) == 3)
4708 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4709 CALL_EXPR_ARG (*from_p, 0),
4710 CALL_EXPR_ARG (*from_p, 1),
4711 CALL_EXPR_ARG (*from_p, 2));
4712 else
4714 call_stmt = gimple_build_call_from_tree (*from_p);
4715 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
4718 notice_special_calls (call_stmt);
4719 if (!gimple_call_noreturn_p (call_stmt))
4720 gimple_call_set_lhs (call_stmt, *to_p);
4721 assign = call_stmt;
4723 else
4725 assign = gimple_build_assign (*to_p, *from_p);
4726 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4729 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4731 /* We should have got an SSA name from the start. */
4732 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4735 gimplify_seq_add_stmt (pre_p, assign);
4736 gsi = gsi_last (*pre_p);
4737 maybe_fold_stmt (&gsi);
4739 if (want_value)
4741 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4742 return GS_OK;
4744 else
4745 *expr_p = NULL;
4747 return GS_ALL_DONE;
4750 /* Gimplify a comparison between two variable-sized objects. Do this
4751 with a call to BUILT_IN_MEMCMP. */
4753 static enum gimplify_status
4754 gimplify_variable_sized_compare (tree *expr_p)
4756 location_t loc = EXPR_LOCATION (*expr_p);
4757 tree op0 = TREE_OPERAND (*expr_p, 0);
4758 tree op1 = TREE_OPERAND (*expr_p, 1);
4759 tree t, arg, dest, src, expr;
4761 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4762 arg = unshare_expr (arg);
4763 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4764 src = build_fold_addr_expr_loc (loc, op1);
4765 dest = build_fold_addr_expr_loc (loc, op0);
4766 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4767 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4769 expr
4770 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4771 SET_EXPR_LOCATION (expr, loc);
4772 *expr_p = expr;
4774 return GS_OK;
4777 /* Gimplify a comparison between two aggregate objects of integral scalar
4778 mode as a comparison between the bitwise equivalent scalar values. */
4780 static enum gimplify_status
4781 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4783 location_t loc = EXPR_LOCATION (*expr_p);
4784 tree op0 = TREE_OPERAND (*expr_p, 0);
4785 tree op1 = TREE_OPERAND (*expr_p, 1);
4787 tree type = TREE_TYPE (op0);
4788 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4790 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4791 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4793 *expr_p
4794 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4796 return GS_OK;
4799 /* Gimplify an expression sequence. This function gimplifies each
4800 expression and rewrites the original expression with the last
4801 expression of the sequence in GIMPLE form.
4803 PRE_P points to the list where the side effects for all the
4804 expressions in the sequence will be emitted.
4806 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4808 static enum gimplify_status
4809 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4811 tree t = *expr_p;
4815 tree *sub_p = &TREE_OPERAND (t, 0);
4817 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4818 gimplify_compound_expr (sub_p, pre_p, false);
4819 else
4820 gimplify_stmt (sub_p, pre_p);
4822 t = TREE_OPERAND (t, 1);
4824 while (TREE_CODE (t) == COMPOUND_EXPR);
4826 *expr_p = t;
4827 if (want_value)
4828 return GS_OK;
4829 else
4831 gimplify_stmt (expr_p, pre_p);
4832 return GS_ALL_DONE;
4836 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4837 gimplify. After gimplification, EXPR_P will point to a new temporary
4838 that holds the original value of the SAVE_EXPR node.
4840 PRE_P points to the list where side effects that must happen before
4841 *EXPR_P should be stored. */
4843 static enum gimplify_status
4844 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4846 enum gimplify_status ret = GS_ALL_DONE;
4847 tree val;
4849 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4850 val = TREE_OPERAND (*expr_p, 0);
4852 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4853 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4855 /* The operand may be a void-valued expression such as SAVE_EXPRs
4856 generated by the Java frontend for class initialization. It is
4857 being executed only for its side-effects. */
4858 if (TREE_TYPE (val) == void_type_node)
4860 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4861 is_gimple_stmt, fb_none);
4862 val = NULL;
4864 else
4865 val = get_initialized_tmp_var (val, pre_p, post_p);
4867 TREE_OPERAND (*expr_p, 0) = val;
4868 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4871 *expr_p = val;
4873 return ret;
4876 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4878 unary_expr
4879 : ...
4880 | '&' varname
4883 PRE_P points to the list where side effects that must happen before
4884 *EXPR_P should be stored.
4886 POST_P points to the list where side effects that must happen after
4887 *EXPR_P should be stored. */
4889 static enum gimplify_status
4890 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4892 tree expr = *expr_p;
4893 tree op0 = TREE_OPERAND (expr, 0);
4894 enum gimplify_status ret;
4895 location_t loc = EXPR_LOCATION (*expr_p);
4897 switch (TREE_CODE (op0))
4899 case INDIRECT_REF:
4900 do_indirect_ref:
4901 /* Check if we are dealing with an expression of the form '&*ptr'.
4902 While the front end folds away '&*ptr' into 'ptr', these
4903 expressions may be generated internally by the compiler (e.g.,
4904 builtins like __builtin_va_end). */
4905 /* Caution: the silent array decomposition semantics we allow for
4906 ADDR_EXPR means we can't always discard the pair. */
4907 /* Gimplification of the ADDR_EXPR operand may drop
4908 cv-qualification conversions, so make sure we add them if
4909 needed. */
4911 tree op00 = TREE_OPERAND (op0, 0);
4912 tree t_expr = TREE_TYPE (expr);
4913 tree t_op00 = TREE_TYPE (op00);
4915 if (!useless_type_conversion_p (t_expr, t_op00))
4916 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4917 *expr_p = op00;
4918 ret = GS_OK;
4920 break;
4922 case VIEW_CONVERT_EXPR:
4923 /* Take the address of our operand and then convert it to the type of
4924 this ADDR_EXPR.
4926 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4927 all clear. The impact of this transformation is even less clear. */
4929 /* If the operand is a useless conversion, look through it. Doing so
4930 guarantees that the ADDR_EXPR and its operand will remain of the
4931 same type. */
4932 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4933 op0 = TREE_OPERAND (op0, 0);
4935 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4936 build_fold_addr_expr_loc (loc,
4937 TREE_OPERAND (op0, 0)));
4938 ret = GS_OK;
4939 break;
4941 default:
4942 /* We use fb_either here because the C frontend sometimes takes
4943 the address of a call that returns a struct; see
4944 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4945 the implied temporary explicit. */
4947 /* Make the operand addressable. */
4948 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4949 is_gimple_addressable, fb_either);
4950 if (ret == GS_ERROR)
4951 break;
4953 /* Then mark it. Beware that it may not be possible to do so directly
4954 if a temporary has been created by the gimplification. */
4955 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4957 op0 = TREE_OPERAND (expr, 0);
4959 /* For various reasons, the gimplification of the expression
4960 may have made a new INDIRECT_REF. */
4961 if (TREE_CODE (op0) == INDIRECT_REF)
4962 goto do_indirect_ref;
4964 mark_addressable (TREE_OPERAND (expr, 0));
4966 /* The FEs may end up building ADDR_EXPRs early on a decl with
4967 an incomplete type. Re-build ADDR_EXPRs in canonical form
4968 here. */
4969 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4970 *expr_p = build_fold_addr_expr (op0);
4972 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4973 recompute_tree_invariant_for_addr_expr (*expr_p);
4975 /* If we re-built the ADDR_EXPR add a conversion to the original type
4976 if required. */
4977 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4978 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4980 break;
4983 return ret;
4986 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4987 value; output operands should be a gimple lvalue. */
4989 static enum gimplify_status
4990 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4992 tree expr;
4993 int noutputs;
4994 const char **oconstraints;
4995 int i;
4996 tree link;
4997 const char *constraint;
4998 bool allows_mem, allows_reg, is_inout;
4999 enum gimplify_status ret, tret;
5000 gasm *stmt;
5001 vec<tree, va_gc> *inputs;
5002 vec<tree, va_gc> *outputs;
5003 vec<tree, va_gc> *clobbers;
5004 vec<tree, va_gc> *labels;
5005 tree link_next;
5007 expr = *expr_p;
5008 noutputs = list_length (ASM_OUTPUTS (expr));
5009 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5011 inputs = NULL;
5012 outputs = NULL;
5013 clobbers = NULL;
5014 labels = NULL;
5016 ret = GS_ALL_DONE;
5017 link_next = NULL_TREE;
5018 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5020 bool ok;
5021 size_t constraint_len;
5023 link_next = TREE_CHAIN (link);
5025 oconstraints[i]
5026 = constraint
5027 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5028 constraint_len = strlen (constraint);
5029 if (constraint_len == 0)
5030 continue;
5032 ok = parse_output_constraint (&constraint, i, 0, 0,
5033 &allows_mem, &allows_reg, &is_inout);
5034 if (!ok)
5036 ret = GS_ERROR;
5037 is_inout = false;
5040 if (!allows_reg && allows_mem)
5041 mark_addressable (TREE_VALUE (link));
5043 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5044 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5045 fb_lvalue | fb_mayfail);
5046 if (tret == GS_ERROR)
5048 error ("invalid lvalue in asm output %d", i);
5049 ret = tret;
5052 vec_safe_push (outputs, link);
5053 TREE_CHAIN (link) = NULL_TREE;
5055 if (is_inout)
5057 /* An input/output operand. To give the optimizers more
5058 flexibility, split it into separate input and output
5059 operands. */
5060 tree input;
5061 char buf[10];
5063 /* Turn the in/out constraint into an output constraint. */
5064 char *p = xstrdup (constraint);
5065 p[0] = '=';
5066 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5068 /* And add a matching input constraint. */
5069 if (allows_reg)
5071 sprintf (buf, "%d", i);
5073 /* If there are multiple alternatives in the constraint,
5074 handle each of them individually. Those that allow register
5075 will be replaced with operand number, the others will stay
5076 unchanged. */
5077 if (strchr (p, ',') != NULL)
5079 size_t len = 0, buflen = strlen (buf);
5080 char *beg, *end, *str, *dst;
5082 for (beg = p + 1;;)
5084 end = strchr (beg, ',');
5085 if (end == NULL)
5086 end = strchr (beg, '\0');
5087 if ((size_t) (end - beg) < buflen)
5088 len += buflen + 1;
5089 else
5090 len += end - beg + 1;
5091 if (*end)
5092 beg = end + 1;
5093 else
5094 break;
5097 str = (char *) alloca (len);
5098 for (beg = p + 1, dst = str;;)
5100 const char *tem;
5101 bool mem_p, reg_p, inout_p;
5103 end = strchr (beg, ',');
5104 if (end)
5105 *end = '\0';
5106 beg[-1] = '=';
5107 tem = beg - 1;
5108 parse_output_constraint (&tem, i, 0, 0,
5109 &mem_p, &reg_p, &inout_p);
5110 if (dst != str)
5111 *dst++ = ',';
5112 if (reg_p)
5114 memcpy (dst, buf, buflen);
5115 dst += buflen;
5117 else
5119 if (end)
5120 len = end - beg;
5121 else
5122 len = strlen (beg);
5123 memcpy (dst, beg, len);
5124 dst += len;
5126 if (end)
5127 beg = end + 1;
5128 else
5129 break;
5131 *dst = '\0';
5132 input = build_string (dst - str, str);
5134 else
5135 input = build_string (strlen (buf), buf);
5137 else
5138 input = build_string (constraint_len - 1, constraint + 1);
5140 free (p);
5142 input = build_tree_list (build_tree_list (NULL_TREE, input),
5143 unshare_expr (TREE_VALUE (link)));
5144 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5148 link_next = NULL_TREE;
5149 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5151 link_next = TREE_CHAIN (link);
5152 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5153 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5154 oconstraints, &allows_mem, &allows_reg);
5156 /* If we can't make copies, we can only accept memory. */
5157 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5159 if (allows_mem)
5160 allows_reg = 0;
5161 else
5163 error ("impossible constraint in %<asm%>");
5164 error ("non-memory input %d must stay in memory", i);
5165 return GS_ERROR;
5169 /* If the operand is a memory input, it should be an lvalue. */
5170 if (!allows_reg && allows_mem)
5172 tree inputv = TREE_VALUE (link);
5173 STRIP_NOPS (inputv);
5174 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5175 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5176 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5177 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5178 TREE_VALUE (link) = error_mark_node;
5179 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5180 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5181 mark_addressable (TREE_VALUE (link));
5182 if (tret == GS_ERROR)
5184 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5185 input_location = EXPR_LOCATION (TREE_VALUE (link));
5186 error ("memory input %d is not directly addressable", i);
5187 ret = tret;
5190 else
5192 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5193 is_gimple_asm_val, fb_rvalue);
5194 if (tret == GS_ERROR)
5195 ret = tret;
5198 TREE_CHAIN (link) = NULL_TREE;
5199 vec_safe_push (inputs, link);
5202 link_next = NULL_TREE;
5203 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5205 link_next = TREE_CHAIN (link);
5206 TREE_CHAIN (link) = NULL_TREE;
5207 vec_safe_push (clobbers, link);
5210 link_next = NULL_TREE;
5211 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5213 link_next = TREE_CHAIN (link);
5214 TREE_CHAIN (link) = NULL_TREE;
5215 vec_safe_push (labels, link);
5218 /* Do not add ASMs with errors to the gimple IL stream. */
5219 if (ret != GS_ERROR)
5221 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5222 inputs, outputs, clobbers, labels);
5224 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5225 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5227 gimplify_seq_add_stmt (pre_p, stmt);
5230 return ret;
5233 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5234 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5235 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5236 return to this function.
5238 FIXME should we complexify the prequeue handling instead? Or use flags
5239 for all the cleanups and let the optimizer tighten them up? The current
5240 code seems pretty fragile; it will break on a cleanup within any
5241 non-conditional nesting. But any such nesting would be broken, anyway;
5242 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5243 and continues out of it. We can do that at the RTL level, though, so
5244 having an optimizer to tighten up try/finally regions would be a Good
5245 Thing. */
5247 static enum gimplify_status
5248 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5250 gimple_stmt_iterator iter;
5251 gimple_seq body_sequence = NULL;
5253 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5255 /* We only care about the number of conditions between the innermost
5256 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5257 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5258 int old_conds = gimplify_ctxp->conditions;
5259 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5260 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5261 gimplify_ctxp->conditions = 0;
5262 gimplify_ctxp->conditional_cleanups = NULL;
5263 gimplify_ctxp->in_cleanup_point_expr = true;
5265 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5267 gimplify_ctxp->conditions = old_conds;
5268 gimplify_ctxp->conditional_cleanups = old_cleanups;
5269 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5271 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5273 gimple wce = gsi_stmt (iter);
5275 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5277 if (gsi_one_before_end_p (iter))
5279 /* Note that gsi_insert_seq_before and gsi_remove do not
5280 scan operands, unlike some other sequence mutators. */
5281 if (!gimple_wce_cleanup_eh_only (wce))
5282 gsi_insert_seq_before_without_update (&iter,
5283 gimple_wce_cleanup (wce),
5284 GSI_SAME_STMT);
5285 gsi_remove (&iter, true);
5286 break;
5288 else
5290 gtry *gtry;
5291 gimple_seq seq;
5292 enum gimple_try_flags kind;
5294 if (gimple_wce_cleanup_eh_only (wce))
5295 kind = GIMPLE_TRY_CATCH;
5296 else
5297 kind = GIMPLE_TRY_FINALLY;
5298 seq = gsi_split_seq_after (iter);
5300 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5301 /* Do not use gsi_replace here, as it may scan operands.
5302 We want to do a simple structural modification only. */
5303 gsi_set_stmt (&iter, gtry);
5304 iter = gsi_start (gtry->eval);
5307 else
5308 gsi_next (&iter);
5311 gimplify_seq_add_seq (pre_p, body_sequence);
5312 if (temp)
5314 *expr_p = temp;
5315 return GS_OK;
5317 else
5319 *expr_p = NULL;
5320 return GS_ALL_DONE;
5324 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5325 is the cleanup action required. EH_ONLY is true if the cleanup should
5326 only be executed if an exception is thrown, not on normal exit. */
5328 static void
5329 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5331 gimple wce;
5332 gimple_seq cleanup_stmts = NULL;
5334 /* Errors can result in improperly nested cleanups. Which results in
5335 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5336 if (seen_error ())
5337 return;
5339 if (gimple_conditional_context ())
5341 /* If we're in a conditional context, this is more complex. We only
5342 want to run the cleanup if we actually ran the initialization that
5343 necessitates it, but we want to run it after the end of the
5344 conditional context. So we wrap the try/finally around the
5345 condition and use a flag to determine whether or not to actually
5346 run the destructor. Thus
5348 test ? f(A()) : 0
5350 becomes (approximately)
5352 flag = 0;
5353 try {
5354 if (test) { A::A(temp); flag = 1; val = f(temp); }
5355 else { val = 0; }
5356 } finally {
5357 if (flag) A::~A(temp);
5361 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5362 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
5363 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
5365 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5366 gimplify_stmt (&cleanup, &cleanup_stmts);
5367 wce = gimple_build_wce (cleanup_stmts);
5369 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5370 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5371 gimplify_seq_add_stmt (pre_p, ftrue);
5373 /* Because of this manipulation, and the EH edges that jump
5374 threading cannot redirect, the temporary (VAR) will appear
5375 to be used uninitialized. Don't warn. */
5376 TREE_NO_WARNING (var) = 1;
5378 else
5380 gimplify_stmt (&cleanup, &cleanup_stmts);
5381 wce = gimple_build_wce (cleanup_stmts);
5382 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5383 gimplify_seq_add_stmt (pre_p, wce);
5387 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5389 static enum gimplify_status
5390 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5392 tree targ = *expr_p;
5393 tree temp = TARGET_EXPR_SLOT (targ);
5394 tree init = TARGET_EXPR_INITIAL (targ);
5395 enum gimplify_status ret;
5397 if (init)
5399 tree cleanup = NULL_TREE;
5401 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5402 to the temps list. Handle also variable length TARGET_EXPRs. */
5403 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5405 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5406 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5407 gimplify_vla_decl (temp, pre_p);
5409 else
5410 gimple_add_tmp_var (temp);
5412 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5413 expression is supposed to initialize the slot. */
5414 if (VOID_TYPE_P (TREE_TYPE (init)))
5415 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5416 else
5418 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5419 init = init_expr;
5420 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5421 init = NULL;
5422 ggc_free (init_expr);
5424 if (ret == GS_ERROR)
5426 /* PR c++/28266 Make sure this is expanded only once. */
5427 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5428 return GS_ERROR;
5430 if (init)
5431 gimplify_and_add (init, pre_p);
5433 /* If needed, push the cleanup for the temp. */
5434 if (TARGET_EXPR_CLEANUP (targ))
5436 if (CLEANUP_EH_ONLY (targ))
5437 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5438 CLEANUP_EH_ONLY (targ), pre_p);
5439 else
5440 cleanup = TARGET_EXPR_CLEANUP (targ);
5443 /* Add a clobber for the temporary going out of scope, like
5444 gimplify_bind_expr. */
5445 if (gimplify_ctxp->in_cleanup_point_expr
5446 && needs_to_live_in_memory (temp)
5447 && flag_stack_reuse == SR_ALL)
5449 tree clobber = build_constructor (TREE_TYPE (temp),
5450 NULL);
5451 TREE_THIS_VOLATILE (clobber) = true;
5452 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5453 if (cleanup)
5454 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5455 clobber);
5456 else
5457 cleanup = clobber;
5460 if (cleanup)
5461 gimple_push_cleanup (temp, cleanup, false, pre_p);
5463 /* Only expand this once. */
5464 TREE_OPERAND (targ, 3) = init;
5465 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5467 else
5468 /* We should have expanded this before. */
5469 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5471 *expr_p = temp;
5472 return GS_OK;
5475 /* Gimplification of expression trees. */
5477 /* Gimplify an expression which appears at statement context. The
5478 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5479 NULL, a new sequence is allocated.
5481 Return true if we actually added a statement to the queue. */
5483 bool
5484 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5486 gimple_seq_node last;
5488 last = gimple_seq_last (*seq_p);
5489 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5490 return last != gimple_seq_last (*seq_p);
5493 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5494 to CTX. If entries already exist, force them to be some flavor of private.
5495 If there is no enclosing parallel, do nothing. */
5497 void
5498 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5500 splay_tree_node n;
5502 if (decl == NULL || !DECL_P (decl))
5503 return;
5507 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5508 if (n != NULL)
5510 if (n->value & GOVD_SHARED)
5511 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5512 else if (n->value & GOVD_MAP)
5513 n->value |= GOVD_MAP_TO_ONLY;
5514 else
5515 return;
5517 else if (ctx->region_type == ORT_TARGET)
5518 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5519 else if (ctx->region_type != ORT_WORKSHARE
5520 && ctx->region_type != ORT_SIMD
5521 && ctx->region_type != ORT_TARGET_DATA)
5522 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5524 ctx = ctx->outer_context;
5526 while (ctx);
5529 /* Similarly for each of the type sizes of TYPE. */
5531 static void
5532 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5534 if (type == NULL || type == error_mark_node)
5535 return;
5536 type = TYPE_MAIN_VARIANT (type);
5538 if (ctx->privatized_types->add (type))
5539 return;
5541 switch (TREE_CODE (type))
5543 case INTEGER_TYPE:
5544 case ENUMERAL_TYPE:
5545 case BOOLEAN_TYPE:
5546 case REAL_TYPE:
5547 case FIXED_POINT_TYPE:
5548 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5549 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5550 break;
5552 case ARRAY_TYPE:
5553 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5554 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5555 break;
5557 case RECORD_TYPE:
5558 case UNION_TYPE:
5559 case QUAL_UNION_TYPE:
5561 tree field;
5562 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5563 if (TREE_CODE (field) == FIELD_DECL)
5565 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5566 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5569 break;
5571 case POINTER_TYPE:
5572 case REFERENCE_TYPE:
5573 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5574 break;
5576 default:
5577 break;
5580 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5581 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5582 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5585 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
5587 static void
5588 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5590 splay_tree_node n;
5591 unsigned int nflags;
5592 tree t;
5594 if (error_operand_p (decl))
5595 return;
5597 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5598 there are constructors involved somewhere. */
5599 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5600 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5601 flags |= GOVD_SEEN;
5603 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5604 if (n != NULL && n->value != GOVD_ALIGNED)
5606 /* We shouldn't be re-adding the decl with the same data
5607 sharing class. */
5608 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5609 /* The only combination of data sharing classes we should see is
5610 FIRSTPRIVATE and LASTPRIVATE. */
5611 nflags = n->value | flags;
5612 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5613 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5614 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5615 n->value = nflags;
5616 return;
5619 /* When adding a variable-sized variable, we have to handle all sorts
5620 of additional bits of data: the pointer replacement variable, and
5621 the parameters of the type. */
5622 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5624 /* Add the pointer replacement variable as PRIVATE if the variable
5625 replacement is private, else FIRSTPRIVATE since we'll need the
5626 address of the original variable either for SHARED, or for the
5627 copy into or out of the context. */
5628 if (!(flags & GOVD_LOCAL))
5630 if (flags & GOVD_MAP)
5631 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5632 else if (flags & GOVD_PRIVATE)
5633 nflags = GOVD_PRIVATE;
5634 else
5635 nflags = GOVD_FIRSTPRIVATE;
5636 nflags |= flags & GOVD_SEEN;
5637 t = DECL_VALUE_EXPR (decl);
5638 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5639 t = TREE_OPERAND (t, 0);
5640 gcc_assert (DECL_P (t));
5641 omp_add_variable (ctx, t, nflags);
5644 /* Add all of the variable and type parameters (which should have
5645 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5646 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5647 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5648 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5650 /* The variable-sized variable itself is never SHARED, only some form
5651 of PRIVATE. The sharing would take place via the pointer variable
5652 which we remapped above. */
5653 if (flags & GOVD_SHARED)
5654 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5655 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5657 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5658 alloca statement we generate for the variable, so make sure it
5659 is available. This isn't automatically needed for the SHARED
5660 case, since we won't be allocating local storage then.
5661 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5662 in this case omp_notice_variable will be called later
5663 on when it is gimplified. */
5664 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5665 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5666 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5668 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5669 && lang_hooks.decls.omp_privatize_by_reference (decl))
5671 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5673 /* Similar to the direct variable sized case above, we'll need the
5674 size of references being privatized. */
5675 if ((flags & GOVD_SHARED) == 0)
5677 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5678 if (TREE_CODE (t) != INTEGER_CST)
5679 omp_notice_variable (ctx, t, true);
5683 if (n != NULL)
5684 n->value |= flags;
5685 else
5686 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5689 /* Notice a threadprivate variable DECL used in OMP context CTX.
5690 This just prints out diagnostics about threadprivate variable uses
5691 in untied tasks. If DECL2 is non-NULL, prevent this warning
5692 on that variable. */
5694 static bool
5695 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5696 tree decl2)
5698 splay_tree_node n;
5699 struct gimplify_omp_ctx *octx;
5701 for (octx = ctx; octx; octx = octx->outer_context)
5702 if (octx->region_type == ORT_TARGET)
5704 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5705 if (n == NULL)
5707 error ("threadprivate variable %qE used in target region",
5708 DECL_NAME (decl));
5709 error_at (octx->location, "enclosing target region");
5710 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5712 if (decl2)
5713 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5716 if (ctx->region_type != ORT_UNTIED_TASK)
5717 return false;
5718 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5719 if (n == NULL)
5721 error ("threadprivate variable %qE used in untied task",
5722 DECL_NAME (decl));
5723 error_at (ctx->location, "enclosing task");
5724 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5726 if (decl2)
5727 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5728 return false;
5731 /* Record the fact that DECL was used within the OMP context CTX.
5732 IN_CODE is true when real code uses DECL, and false when we should
5733 merely emit default(none) errors. Return true if DECL is going to
5734 be remapped and thus DECL shouldn't be gimplified into its
5735 DECL_VALUE_EXPR (if any). */
5737 static bool
5738 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5740 splay_tree_node n;
5741 unsigned flags = in_code ? GOVD_SEEN : 0;
5742 bool ret = false, shared;
5744 if (error_operand_p (decl))
5745 return false;
5747 /* Threadprivate variables are predetermined. */
5748 if (is_global_var (decl))
5750 if (DECL_THREAD_LOCAL_P (decl))
5751 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5753 if (DECL_HAS_VALUE_EXPR_P (decl))
5755 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5757 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5758 return omp_notice_threadprivate_variable (ctx, decl, value);
5762 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5763 if (ctx->region_type == ORT_TARGET)
5765 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5766 if (n == NULL)
5768 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5770 error ("%qD referenced in target region does not have "
5771 "a mappable type", decl);
5772 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5774 else
5775 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5777 else
5779 /* If nothing changed, there's nothing left to do. */
5780 if ((n->value & flags) == flags)
5781 return ret;
5782 n->value |= flags;
5784 goto do_outer;
5787 if (n == NULL)
5789 enum omp_clause_default_kind default_kind, kind;
5790 struct gimplify_omp_ctx *octx;
5792 if (ctx->region_type == ORT_WORKSHARE
5793 || ctx->region_type == ORT_SIMD
5794 || ctx->region_type == ORT_TARGET_DATA)
5795 goto do_outer;
5797 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5798 remapped firstprivate instead of shared. To some extent this is
5799 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5800 default_kind = ctx->default_kind;
5801 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5802 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5803 default_kind = kind;
5805 switch (default_kind)
5807 case OMP_CLAUSE_DEFAULT_NONE:
5808 if ((ctx->region_type & ORT_PARALLEL) != 0)
5810 error ("%qE not specified in enclosing parallel",
5811 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5812 error_at (ctx->location, "enclosing parallel");
5814 else if ((ctx->region_type & ORT_TASK) != 0)
5816 error ("%qE not specified in enclosing task",
5817 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5818 error_at (ctx->location, "enclosing task");
5820 else if (ctx->region_type == ORT_TEAMS)
5822 error ("%qE not specified in enclosing teams construct",
5823 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5824 error_at (ctx->location, "enclosing teams construct");
5826 else
5827 gcc_unreachable ();
5828 /* FALLTHRU */
5829 case OMP_CLAUSE_DEFAULT_SHARED:
5830 flags |= GOVD_SHARED;
5831 break;
5832 case OMP_CLAUSE_DEFAULT_PRIVATE:
5833 flags |= GOVD_PRIVATE;
5834 break;
5835 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5836 flags |= GOVD_FIRSTPRIVATE;
5837 break;
5838 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5839 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5840 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5841 if (ctx->outer_context)
5842 omp_notice_variable (ctx->outer_context, decl, in_code);
5843 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5845 splay_tree_node n2;
5847 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5848 continue;
5849 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5850 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5852 flags |= GOVD_FIRSTPRIVATE;
5853 break;
5855 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5856 break;
5858 if (flags & GOVD_FIRSTPRIVATE)
5859 break;
5860 if (octx == NULL
5861 && (TREE_CODE (decl) == PARM_DECL
5862 || (!is_global_var (decl)
5863 && DECL_CONTEXT (decl) == current_function_decl)))
5865 flags |= GOVD_FIRSTPRIVATE;
5866 break;
5868 flags |= GOVD_SHARED;
5869 break;
5870 default:
5871 gcc_unreachable ();
5874 if ((flags & GOVD_PRIVATE)
5875 && lang_hooks.decls.omp_private_outer_ref (decl))
5876 flags |= GOVD_PRIVATE_OUTER_REF;
5878 omp_add_variable (ctx, decl, flags);
5880 shared = (flags & GOVD_SHARED) != 0;
5881 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5882 goto do_outer;
5885 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5886 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5887 && DECL_SIZE (decl)
5888 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5890 splay_tree_node n2;
5891 tree t = DECL_VALUE_EXPR (decl);
5892 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5893 t = TREE_OPERAND (t, 0);
5894 gcc_assert (DECL_P (t));
5895 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5896 n2->value |= GOVD_SEEN;
5899 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5900 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5902 /* If nothing changed, there's nothing left to do. */
5903 if ((n->value & flags) == flags)
5904 return ret;
5905 flags |= n->value;
5906 n->value = flags;
5908 do_outer:
5909 /* If the variable is private in the current context, then we don't
5910 need to propagate anything to an outer context. */
5911 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5912 return ret;
5913 if (ctx->outer_context
5914 && omp_notice_variable (ctx->outer_context, decl, in_code))
5915 return true;
5916 return ret;
5919 /* Verify that DECL is private within CTX. If there's specific information
5920 to the contrary in the innermost scope, generate an error. */
5922 static bool
5923 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5925 splay_tree_node n;
5927 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5928 if (n != NULL)
5930 if (n->value & GOVD_SHARED)
5932 if (ctx == gimplify_omp_ctxp)
5934 if (simd)
5935 error ("iteration variable %qE is predetermined linear",
5936 DECL_NAME (decl));
5937 else
5938 error ("iteration variable %qE should be private",
5939 DECL_NAME (decl));
5940 n->value = GOVD_PRIVATE;
5941 return true;
5943 else
5944 return false;
5946 else if ((n->value & GOVD_EXPLICIT) != 0
5947 && (ctx == gimplify_omp_ctxp
5948 || (ctx->region_type == ORT_COMBINED_PARALLEL
5949 && gimplify_omp_ctxp->outer_context == ctx)))
5951 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5952 error ("iteration variable %qE should not be firstprivate",
5953 DECL_NAME (decl));
5954 else if ((n->value & GOVD_REDUCTION) != 0)
5955 error ("iteration variable %qE should not be reduction",
5956 DECL_NAME (decl));
5957 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5958 error ("iteration variable %qE should not be lastprivate",
5959 DECL_NAME (decl));
5960 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5961 error ("iteration variable %qE should not be private",
5962 DECL_NAME (decl));
5963 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5964 error ("iteration variable %qE is predetermined linear",
5965 DECL_NAME (decl));
5967 return (ctx == gimplify_omp_ctxp
5968 || (ctx->region_type == ORT_COMBINED_PARALLEL
5969 && gimplify_omp_ctxp->outer_context == ctx));
5972 if (ctx->region_type != ORT_WORKSHARE
5973 && ctx->region_type != ORT_SIMD)
5974 return false;
5975 else if (ctx->outer_context)
5976 return omp_is_private (ctx->outer_context, decl, simd);
5977 return false;
5980 /* Return true if DECL is private within a parallel region
5981 that binds to the current construct's context or in parallel
5982 region's REDUCTION clause. */
5984 static bool
5985 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5987 splay_tree_node n;
5991 ctx = ctx->outer_context;
5992 if (ctx == NULL)
5993 return !(is_global_var (decl)
5994 /* References might be private, but might be shared too,
5995 when checking for copyprivate, assume they might be
5996 private, otherwise assume they might be shared. */
5997 || (!copyprivate
5998 && lang_hooks.decls.omp_privatize_by_reference (decl)));
6000 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
6001 continue;
6003 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6004 if (n != NULL)
6005 return (n->value & GOVD_SHARED) == 0;
6007 while (ctx->region_type == ORT_WORKSHARE
6008 || ctx->region_type == ORT_SIMD);
6009 return false;
6012 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
6013 and previous omp contexts. */
6015 static void
6016 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6017 enum omp_region_type region_type)
6019 struct gimplify_omp_ctx *ctx, *outer_ctx;
6020 tree c;
6022 ctx = new_omp_context (region_type);
6023 outer_ctx = ctx->outer_context;
6025 while ((c = *list_p) != NULL)
6027 bool remove = false;
6028 bool notice_outer = true;
6029 const char *check_non_private = NULL;
6030 unsigned int flags;
6031 tree decl;
6033 switch (OMP_CLAUSE_CODE (c))
6035 case OMP_CLAUSE_PRIVATE:
6036 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6037 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6039 flags |= GOVD_PRIVATE_OUTER_REF;
6040 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6042 else
6043 notice_outer = false;
6044 goto do_add;
6045 case OMP_CLAUSE_SHARED:
6046 flags = GOVD_SHARED | GOVD_EXPLICIT;
6047 goto do_add;
6048 case OMP_CLAUSE_FIRSTPRIVATE:
6049 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6050 check_non_private = "firstprivate";
6051 goto do_add;
6052 case OMP_CLAUSE_LASTPRIVATE:
6053 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6054 check_non_private = "lastprivate";
6055 goto do_add;
6056 case OMP_CLAUSE_REDUCTION:
6057 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6058 check_non_private = "reduction";
6059 goto do_add;
6060 case OMP_CLAUSE_LINEAR:
6061 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6062 is_gimple_val, fb_rvalue) == GS_ERROR)
6064 remove = true;
6065 break;
6067 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6068 goto do_add;
6070 case OMP_CLAUSE_MAP:
6071 decl = OMP_CLAUSE_DECL (c);
6072 if (error_operand_p (decl))
6074 remove = true;
6075 break;
6077 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6078 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6079 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6080 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6081 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6083 remove = true;
6084 break;
6086 if (!DECL_P (decl))
6088 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6089 NULL, is_gimple_lvalue, fb_lvalue)
6090 == GS_ERROR)
6092 remove = true;
6093 break;
6095 break;
6097 flags = GOVD_MAP | GOVD_EXPLICIT;
6098 goto do_add;
6100 case OMP_CLAUSE_DEPEND:
6101 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6103 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6104 NULL, is_gimple_val, fb_rvalue);
6105 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6107 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6109 remove = true;
6110 break;
6112 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6113 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6114 is_gimple_val, fb_rvalue) == GS_ERROR)
6116 remove = true;
6117 break;
6119 break;
6121 case OMP_CLAUSE_TO:
6122 case OMP_CLAUSE_FROM:
6123 case OMP_CLAUSE__CACHE_:
6124 decl = OMP_CLAUSE_DECL (c);
6125 if (error_operand_p (decl))
6127 remove = true;
6128 break;
6130 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6131 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6132 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6133 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6134 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6136 remove = true;
6137 break;
6139 if (!DECL_P (decl))
6141 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6142 NULL, is_gimple_lvalue, fb_lvalue)
6143 == GS_ERROR)
6145 remove = true;
6146 break;
6148 break;
6150 goto do_notice;
6152 do_add:
6153 decl = OMP_CLAUSE_DECL (c);
6154 if (error_operand_p (decl))
6156 remove = true;
6157 break;
6159 omp_add_variable (ctx, decl, flags);
6160 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6161 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6163 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6164 GOVD_LOCAL | GOVD_SEEN);
6165 gimplify_omp_ctxp = ctx;
6166 push_gimplify_context ();
6168 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6169 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6171 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6172 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6173 pop_gimplify_context
6174 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6175 push_gimplify_context ();
6176 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6177 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6178 pop_gimplify_context
6179 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6180 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6181 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6183 gimplify_omp_ctxp = outer_ctx;
6185 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6186 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6188 gimplify_omp_ctxp = ctx;
6189 push_gimplify_context ();
6190 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6192 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6193 NULL, NULL);
6194 TREE_SIDE_EFFECTS (bind) = 1;
6195 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6196 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6198 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6199 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6200 pop_gimplify_context
6201 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6202 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6204 gimplify_omp_ctxp = outer_ctx;
6206 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6207 && OMP_CLAUSE_LINEAR_STMT (c))
6209 gimplify_omp_ctxp = ctx;
6210 push_gimplify_context ();
6211 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6213 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6214 NULL, NULL);
6215 TREE_SIDE_EFFECTS (bind) = 1;
6216 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6217 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6219 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6220 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6221 pop_gimplify_context
6222 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6223 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6225 gimplify_omp_ctxp = outer_ctx;
6227 if (notice_outer)
6228 goto do_notice;
6229 break;
6231 case OMP_CLAUSE_COPYIN:
6232 case OMP_CLAUSE_COPYPRIVATE:
6233 decl = OMP_CLAUSE_DECL (c);
6234 if (error_operand_p (decl))
6236 remove = true;
6237 break;
6239 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6240 && !remove
6241 && !omp_check_private (ctx, decl, true))
6243 remove = true;
6244 if (is_global_var (decl))
6246 if (DECL_THREAD_LOCAL_P (decl))
6247 remove = false;
6248 else if (DECL_HAS_VALUE_EXPR_P (decl))
6250 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6252 if (value
6253 && DECL_P (value)
6254 && DECL_THREAD_LOCAL_P (value))
6255 remove = false;
6258 if (remove)
6259 error_at (OMP_CLAUSE_LOCATION (c),
6260 "copyprivate variable %qE is not threadprivate"
6261 " or private in outer context", DECL_NAME (decl));
6263 do_notice:
6264 if (outer_ctx)
6265 omp_notice_variable (outer_ctx, decl, true);
6266 if (check_non_private
6267 && region_type == ORT_WORKSHARE
6268 && omp_check_private (ctx, decl, false))
6270 error ("%s variable %qE is private in outer context",
6271 check_non_private, DECL_NAME (decl));
6272 remove = true;
6274 break;
6276 case OMP_CLAUSE_FINAL:
6277 case OMP_CLAUSE_IF:
6278 OMP_CLAUSE_OPERAND (c, 0)
6279 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6280 /* Fall through. */
6282 case OMP_CLAUSE_SCHEDULE:
6283 case OMP_CLAUSE_NUM_THREADS:
6284 case OMP_CLAUSE_NUM_TEAMS:
6285 case OMP_CLAUSE_THREAD_LIMIT:
6286 case OMP_CLAUSE_DIST_SCHEDULE:
6287 case OMP_CLAUSE_DEVICE:
6288 case OMP_CLAUSE__CILK_FOR_COUNT_:
6289 case OMP_CLAUSE_ASYNC:
6290 case OMP_CLAUSE_WAIT:
6291 case OMP_CLAUSE_NUM_GANGS:
6292 case OMP_CLAUSE_NUM_WORKERS:
6293 case OMP_CLAUSE_VECTOR_LENGTH:
6294 case OMP_CLAUSE_GANG:
6295 case OMP_CLAUSE_WORKER:
6296 case OMP_CLAUSE_VECTOR:
6297 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6298 is_gimple_val, fb_rvalue) == GS_ERROR)
6299 remove = true;
6300 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_GANG
6301 && gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
6302 is_gimple_val, fb_rvalue) == GS_ERROR)
6303 remove = true;
6304 break;
6306 case OMP_CLAUSE_DEVICE_RESIDENT:
6307 case OMP_CLAUSE_USE_DEVICE:
6308 case OMP_CLAUSE_INDEPENDENT:
6309 remove = true;
6310 break;
6312 case OMP_CLAUSE_NOWAIT:
6313 case OMP_CLAUSE_ORDERED:
6314 case OMP_CLAUSE_UNTIED:
6315 case OMP_CLAUSE_COLLAPSE:
6316 case OMP_CLAUSE_AUTO:
6317 case OMP_CLAUSE_SEQ:
6318 case OMP_CLAUSE_MERGEABLE:
6319 case OMP_CLAUSE_PROC_BIND:
6320 case OMP_CLAUSE_SAFELEN:
6321 break;
6323 case OMP_CLAUSE_ALIGNED:
6324 decl = OMP_CLAUSE_DECL (c);
6325 if (error_operand_p (decl))
6327 remove = true;
6328 break;
6330 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6331 is_gimple_val, fb_rvalue) == GS_ERROR)
6333 remove = true;
6334 break;
6336 if (!is_global_var (decl)
6337 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6338 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6339 break;
6341 case OMP_CLAUSE_DEFAULT:
6342 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6343 break;
6345 default:
6346 gcc_unreachable ();
6349 if (remove)
6350 *list_p = OMP_CLAUSE_CHAIN (c);
6351 else
6352 list_p = &OMP_CLAUSE_CHAIN (c);
6355 gimplify_omp_ctxp = ctx;
6358 struct gimplify_adjust_omp_clauses_data
6360 tree *list_p;
6361 gimple_seq *pre_p;
6364 /* For all variables that were not actually used within the context,
6365 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6367 static int
6368 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6370 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6371 gimple_seq *pre_p
6372 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6373 tree decl = (tree) n->key;
6374 unsigned flags = n->value;
6375 enum omp_clause_code code;
6376 tree clause;
6377 bool private_debug;
6379 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6380 return 0;
6381 if ((flags & GOVD_SEEN) == 0)
6382 return 0;
6383 if (flags & GOVD_DEBUG_PRIVATE)
6385 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6386 private_debug = true;
6388 else if (flags & GOVD_MAP)
6389 private_debug = false;
6390 else
6391 private_debug
6392 = lang_hooks.decls.omp_private_debug_clause (decl,
6393 !!(flags & GOVD_SHARED));
6394 if (private_debug)
6395 code = OMP_CLAUSE_PRIVATE;
6396 else if (flags & GOVD_MAP)
6397 code = OMP_CLAUSE_MAP;
6398 else if (flags & GOVD_SHARED)
6400 if (is_global_var (decl))
6402 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6403 while (ctx != NULL)
6405 splay_tree_node on
6406 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6407 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6408 | GOVD_PRIVATE | GOVD_REDUCTION
6409 | GOVD_LINEAR | GOVD_MAP)) != 0)
6410 break;
6411 ctx = ctx->outer_context;
6413 if (ctx == NULL)
6414 return 0;
6416 code = OMP_CLAUSE_SHARED;
6418 else if (flags & GOVD_PRIVATE)
6419 code = OMP_CLAUSE_PRIVATE;
6420 else if (flags & GOVD_FIRSTPRIVATE)
6421 code = OMP_CLAUSE_FIRSTPRIVATE;
6422 else if (flags & GOVD_LASTPRIVATE)
6423 code = OMP_CLAUSE_LASTPRIVATE;
6424 else if (flags & GOVD_ALIGNED)
6425 return 0;
6426 else
6427 gcc_unreachable ();
6429 clause = build_omp_clause (input_location, code);
6430 OMP_CLAUSE_DECL (clause) = decl;
6431 OMP_CLAUSE_CHAIN (clause) = *list_p;
6432 if (private_debug)
6433 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6434 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6435 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6436 else if (code == OMP_CLAUSE_MAP)
6438 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6439 ? OMP_CLAUSE_MAP_TO
6440 : OMP_CLAUSE_MAP_TOFROM;
6441 if (DECL_SIZE (decl)
6442 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6444 tree decl2 = DECL_VALUE_EXPR (decl);
6445 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6446 decl2 = TREE_OPERAND (decl2, 0);
6447 gcc_assert (DECL_P (decl2));
6448 tree mem = build_simple_mem_ref (decl2);
6449 OMP_CLAUSE_DECL (clause) = mem;
6450 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6451 if (gimplify_omp_ctxp->outer_context)
6453 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6454 omp_notice_variable (ctx, decl2, true);
6455 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6457 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6458 OMP_CLAUSE_MAP);
6459 OMP_CLAUSE_DECL (nc) = decl;
6460 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6461 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6462 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6463 OMP_CLAUSE_CHAIN (clause) = nc;
6465 else
6466 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6468 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6470 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6471 OMP_CLAUSE_DECL (nc) = decl;
6472 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6473 OMP_CLAUSE_CHAIN (nc) = *list_p;
6474 OMP_CLAUSE_CHAIN (clause) = nc;
6475 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6476 gimplify_omp_ctxp = ctx->outer_context;
6477 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6478 gimplify_omp_ctxp = ctx;
6480 *list_p = clause;
6481 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6482 gimplify_omp_ctxp = ctx->outer_context;
6483 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6484 gimplify_omp_ctxp = ctx;
6485 return 0;
6488 static void
6489 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6491 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6492 tree c, decl;
6494 while ((c = *list_p) != NULL)
6496 splay_tree_node n;
6497 bool remove = false;
6499 switch (OMP_CLAUSE_CODE (c))
6501 case OMP_CLAUSE_PRIVATE:
6502 case OMP_CLAUSE_SHARED:
6503 case OMP_CLAUSE_FIRSTPRIVATE:
6504 case OMP_CLAUSE_LINEAR:
6505 decl = OMP_CLAUSE_DECL (c);
6506 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6507 remove = !(n->value & GOVD_SEEN);
6508 if (! remove)
6510 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6511 if ((n->value & GOVD_DEBUG_PRIVATE)
6512 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6514 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6515 || ((n->value & GOVD_DATA_SHARE_CLASS)
6516 == GOVD_PRIVATE));
6517 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6518 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6520 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6521 && ctx->outer_context
6522 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6523 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6525 if (ctx->outer_context->combined_loop
6526 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6528 n = splay_tree_lookup (ctx->outer_context->variables,
6529 (splay_tree_key) decl);
6530 if (n == NULL
6531 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6533 int flags = GOVD_FIRSTPRIVATE;
6534 /* #pragma omp distribute does not allow
6535 lastprivate clause. */
6536 if (!ctx->outer_context->distribute)
6537 flags |= GOVD_LASTPRIVATE;
6538 if (n == NULL)
6539 omp_add_variable (ctx->outer_context, decl,
6540 flags | GOVD_SEEN);
6541 else
6542 n->value |= flags | GOVD_SEEN;
6545 else if (!is_global_var (decl))
6546 omp_notice_variable (ctx->outer_context, decl, true);
6549 break;
6551 case OMP_CLAUSE_LASTPRIVATE:
6552 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6553 accurately reflect the presence of a FIRSTPRIVATE clause. */
6554 decl = OMP_CLAUSE_DECL (c);
6555 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6556 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6557 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6558 break;
6560 case OMP_CLAUSE_ALIGNED:
6561 decl = OMP_CLAUSE_DECL (c);
6562 if (!is_global_var (decl))
6564 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6565 remove = n == NULL || !(n->value & GOVD_SEEN);
6566 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6568 struct gimplify_omp_ctx *octx;
6569 if (n != NULL
6570 && (n->value & (GOVD_DATA_SHARE_CLASS
6571 & ~GOVD_FIRSTPRIVATE)))
6572 remove = true;
6573 else
6574 for (octx = ctx->outer_context; octx;
6575 octx = octx->outer_context)
6577 n = splay_tree_lookup (octx->variables,
6578 (splay_tree_key) decl);
6579 if (n == NULL)
6580 continue;
6581 if (n->value & GOVD_LOCAL)
6582 break;
6583 /* We have to avoid assigning a shared variable
6584 to itself when trying to add
6585 __builtin_assume_aligned. */
6586 if (n->value & GOVD_SHARED)
6588 remove = true;
6589 break;
6594 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6596 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6597 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6598 remove = true;
6600 break;
6602 case OMP_CLAUSE_MAP:
6603 decl = OMP_CLAUSE_DECL (c);
6604 if (!DECL_P (decl))
6605 break;
6606 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6607 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6608 remove = true;
6609 else if (DECL_SIZE (decl)
6610 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6611 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6613 /* For OMP_CLAUSE_MAP_FORCE_DEVICEPTR, we'll never enter here,
6614 because for these, TREE_CODE (DECL_SIZE (decl)) will always be
6615 INTEGER_CST. */
6616 gcc_assert (OMP_CLAUSE_MAP_KIND (c)
6617 != OMP_CLAUSE_MAP_FORCE_DEVICEPTR);
6619 tree decl2 = DECL_VALUE_EXPR (decl);
6620 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6621 decl2 = TREE_OPERAND (decl2, 0);
6622 gcc_assert (DECL_P (decl2));
6623 tree mem = build_simple_mem_ref (decl2);
6624 OMP_CLAUSE_DECL (c) = mem;
6625 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6626 if (ctx->outer_context)
6628 omp_notice_variable (ctx->outer_context, decl2, true);
6629 omp_notice_variable (ctx->outer_context,
6630 OMP_CLAUSE_SIZE (c), true);
6632 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6633 OMP_CLAUSE_MAP);
6634 OMP_CLAUSE_DECL (nc) = decl;
6635 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6636 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6637 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6638 OMP_CLAUSE_CHAIN (c) = nc;
6639 c = nc;
6641 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6642 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6643 break;
6645 case OMP_CLAUSE_TO:
6646 case OMP_CLAUSE_FROM:
6647 case OMP_CLAUSE__CACHE_:
6648 decl = OMP_CLAUSE_DECL (c);
6649 if (!DECL_P (decl))
6650 break;
6651 if (DECL_SIZE (decl)
6652 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6654 tree decl2 = DECL_VALUE_EXPR (decl);
6655 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6656 decl2 = TREE_OPERAND (decl2, 0);
6657 gcc_assert (DECL_P (decl2));
6658 tree mem = build_simple_mem_ref (decl2);
6659 OMP_CLAUSE_DECL (c) = mem;
6660 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6661 if (ctx->outer_context)
6663 omp_notice_variable (ctx->outer_context, decl2, true);
6664 omp_notice_variable (ctx->outer_context,
6665 OMP_CLAUSE_SIZE (c), true);
6668 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6669 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6670 break;
6672 case OMP_CLAUSE_REDUCTION:
6673 case OMP_CLAUSE_COPYIN:
6674 case OMP_CLAUSE_COPYPRIVATE:
6675 case OMP_CLAUSE_IF:
6676 case OMP_CLAUSE_NUM_THREADS:
6677 case OMP_CLAUSE_NUM_TEAMS:
6678 case OMP_CLAUSE_THREAD_LIMIT:
6679 case OMP_CLAUSE_DIST_SCHEDULE:
6680 case OMP_CLAUSE_DEVICE:
6681 case OMP_CLAUSE_SCHEDULE:
6682 case OMP_CLAUSE_NOWAIT:
6683 case OMP_CLAUSE_ORDERED:
6684 case OMP_CLAUSE_DEFAULT:
6685 case OMP_CLAUSE_UNTIED:
6686 case OMP_CLAUSE_COLLAPSE:
6687 case OMP_CLAUSE_FINAL:
6688 case OMP_CLAUSE_MERGEABLE:
6689 case OMP_CLAUSE_PROC_BIND:
6690 case OMP_CLAUSE_SAFELEN:
6691 case OMP_CLAUSE_DEPEND:
6692 case OMP_CLAUSE__CILK_FOR_COUNT_:
6693 case OMP_CLAUSE_ASYNC:
6694 case OMP_CLAUSE_WAIT:
6695 case OMP_CLAUSE_DEVICE_RESIDENT:
6696 case OMP_CLAUSE_USE_DEVICE:
6697 case OMP_CLAUSE_INDEPENDENT:
6698 case OMP_CLAUSE_NUM_GANGS:
6699 case OMP_CLAUSE_NUM_WORKERS:
6700 case OMP_CLAUSE_VECTOR_LENGTH:
6701 case OMP_CLAUSE_GANG:
6702 case OMP_CLAUSE_WORKER:
6703 case OMP_CLAUSE_VECTOR:
6704 case OMP_CLAUSE_AUTO:
6705 case OMP_CLAUSE_SEQ:
6706 break;
6708 default:
6709 gcc_unreachable ();
6712 if (remove)
6713 *list_p = OMP_CLAUSE_CHAIN (c);
6714 else
6715 list_p = &OMP_CLAUSE_CHAIN (c);
6718 /* Add in any implicit data sharing. */
6719 struct gimplify_adjust_omp_clauses_data data;
6720 data.list_p = list_p;
6721 data.pre_p = pre_p;
6722 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6724 gimplify_omp_ctxp = ctx->outer_context;
6725 delete_omp_context (ctx);
6728 /* Gimplify OACC_CACHE. */
6730 static void
6731 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
6733 tree expr = *expr_p;
6735 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6736 gimplify_adjust_omp_clauses (pre_p, &OACC_CACHE_CLAUSES (expr));
6738 /* TODO: Do something sensible with this information. */
6740 *expr_p = NULL_TREE;
6743 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6744 gimplification of the body, as well as scanning the body for used
6745 variables. We need to do this scan now, because variable-sized
6746 decls will be decomposed during gimplification. */
6748 static void
6749 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6751 tree expr = *expr_p;
6752 gimple g;
6753 gimple_seq body = NULL;
6755 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6756 OMP_PARALLEL_COMBINED (expr)
6757 ? ORT_COMBINED_PARALLEL
6758 : ORT_PARALLEL);
6760 push_gimplify_context ();
6762 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6763 if (gimple_code (g) == GIMPLE_BIND)
6764 pop_gimplify_context (g);
6765 else
6766 pop_gimplify_context (NULL);
6768 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6770 g = gimple_build_omp_parallel (body,
6771 OMP_PARALLEL_CLAUSES (expr),
6772 NULL_TREE, NULL_TREE);
6773 if (OMP_PARALLEL_COMBINED (expr))
6774 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6775 gimplify_seq_add_stmt (pre_p, g);
6776 *expr_p = NULL_TREE;
6779 /* Gimplify the contents of an OMP_TASK statement. This involves
6780 gimplification of the body, as well as scanning the body for used
6781 variables. We need to do this scan now, because variable-sized
6782 decls will be decomposed during gimplification. */
6784 static void
6785 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6787 tree expr = *expr_p;
6788 gimple g;
6789 gimple_seq body = NULL;
6791 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6792 find_omp_clause (OMP_TASK_CLAUSES (expr),
6793 OMP_CLAUSE_UNTIED)
6794 ? ORT_UNTIED_TASK : ORT_TASK);
6796 push_gimplify_context ();
6798 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6799 if (gimple_code (g) == GIMPLE_BIND)
6800 pop_gimplify_context (g);
6801 else
6802 pop_gimplify_context (NULL);
6804 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6806 g = gimple_build_omp_task (body,
6807 OMP_TASK_CLAUSES (expr),
6808 NULL_TREE, NULL_TREE,
6809 NULL_TREE, NULL_TREE, NULL_TREE);
6810 gimplify_seq_add_stmt (pre_p, g);
6811 *expr_p = NULL_TREE;
6814 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6815 with non-NULL OMP_FOR_INIT. */
6817 static tree
6818 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6820 *walk_subtrees = 0;
6821 switch (TREE_CODE (*tp))
6823 case OMP_FOR:
6824 *walk_subtrees = 1;
6825 /* FALLTHRU */
6826 case OMP_SIMD:
6827 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6828 return *tp;
6829 break;
6830 case BIND_EXPR:
6831 case STATEMENT_LIST:
6832 case OMP_PARALLEL:
6833 *walk_subtrees = 1;
6834 break;
6835 default:
6836 break;
6838 return NULL_TREE;
6841 /* Gimplify the gross structure of an OMP_FOR statement. */
6843 static enum gimplify_status
6844 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6846 tree for_stmt, orig_for_stmt, decl, var, t;
6847 enum gimplify_status ret = GS_ALL_DONE;
6848 enum gimplify_status tret;
6849 gomp_for *gfor;
6850 gimple_seq for_body, for_pre_body;
6851 int i;
6852 bool simd;
6853 bitmap has_decl_expr = NULL;
6855 orig_for_stmt = for_stmt = *expr_p;
6857 switch (TREE_CODE (for_stmt))
6859 case OMP_FOR:
6860 case CILK_FOR:
6861 case OMP_DISTRIBUTE:
6862 case OACC_LOOP:
6863 simd = false;
6864 break;
6865 case OMP_SIMD:
6866 case CILK_SIMD:
6867 simd = true;
6868 break;
6869 default:
6870 gcc_unreachable ();
6873 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6874 simd ? ORT_SIMD : ORT_WORKSHARE);
6875 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6876 gimplify_omp_ctxp->distribute = true;
6878 /* Handle OMP_FOR_INIT. */
6879 for_pre_body = NULL;
6880 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6882 has_decl_expr = BITMAP_ALLOC (NULL);
6883 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6884 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6885 == VAR_DECL)
6887 t = OMP_FOR_PRE_BODY (for_stmt);
6888 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6890 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6892 tree_stmt_iterator si;
6893 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6894 tsi_next (&si))
6896 t = tsi_stmt (si);
6897 if (TREE_CODE (t) == DECL_EXPR
6898 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6899 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6903 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6904 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6906 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6908 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
6909 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6910 NULL, NULL);
6911 gcc_assert (for_stmt != NULL_TREE);
6912 gimplify_omp_ctxp->combined_loop = true;
6915 for_body = NULL;
6916 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6917 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6918 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6919 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6920 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6922 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6923 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6924 decl = TREE_OPERAND (t, 0);
6925 gcc_assert (DECL_P (decl));
6926 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6927 || POINTER_TYPE_P (TREE_TYPE (decl)));
6929 /* Make sure the iteration variable is private. */
6930 tree c = NULL_TREE;
6931 tree c2 = NULL_TREE;
6932 if (orig_for_stmt != for_stmt)
6933 /* Do this only on innermost construct for combined ones. */;
6934 else if (simd)
6936 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6937 (splay_tree_key)decl);
6938 omp_is_private (gimplify_omp_ctxp, decl,
6939 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6940 != 1));
6941 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6942 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6943 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6945 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6946 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6947 if (has_decl_expr
6948 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6949 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6950 OMP_CLAUSE_DECL (c) = decl;
6951 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6952 OMP_FOR_CLAUSES (for_stmt) = c;
6953 omp_add_variable (gimplify_omp_ctxp, decl,
6954 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6956 else
6958 bool lastprivate
6959 = (!has_decl_expr
6960 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6961 if (lastprivate
6962 && gimplify_omp_ctxp->outer_context
6963 && gimplify_omp_ctxp->outer_context->region_type
6964 == ORT_WORKSHARE
6965 && gimplify_omp_ctxp->outer_context->combined_loop
6966 && !gimplify_omp_ctxp->outer_context->distribute)
6968 struct gimplify_omp_ctx *outer
6969 = gimplify_omp_ctxp->outer_context;
6970 n = splay_tree_lookup (outer->variables,
6971 (splay_tree_key) decl);
6972 if (n != NULL
6973 && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6974 lastprivate = false;
6975 else if (omp_check_private (outer, decl, false))
6976 error ("lastprivate variable %qE is private in outer "
6977 "context", DECL_NAME (decl));
6978 else
6980 omp_add_variable (outer, decl,
6981 GOVD_LASTPRIVATE | GOVD_SEEN);
6982 if (outer->outer_context)
6983 omp_notice_variable (outer->outer_context, decl, true);
6986 c = build_omp_clause (input_location,
6987 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6988 : OMP_CLAUSE_PRIVATE);
6989 OMP_CLAUSE_DECL (c) = decl;
6990 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6991 OMP_FOR_CLAUSES (for_stmt) = c;
6992 omp_add_variable (gimplify_omp_ctxp, decl,
6993 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6994 | GOVD_EXPLICIT | GOVD_SEEN);
6995 c = NULL_TREE;
6998 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6999 omp_notice_variable (gimplify_omp_ctxp, decl, true);
7000 else
7001 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
7003 /* If DECL is not a gimple register, create a temporary variable to act
7004 as an iteration counter. This is valid, since DECL cannot be
7005 modified in the body of the loop. Similarly for any iteration vars
7006 in simd with collapse > 1 where the iterator vars must be
7007 lastprivate. */
7008 if (orig_for_stmt != for_stmt)
7009 var = decl;
7010 else if (!is_gimple_reg (decl)
7011 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
7013 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7014 TREE_OPERAND (t, 0) = var;
7016 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
7018 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7020 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7021 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
7022 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
7023 OMP_CLAUSE_DECL (c2) = var;
7024 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
7025 OMP_FOR_CLAUSES (for_stmt) = c2;
7026 omp_add_variable (gimplify_omp_ctxp, var,
7027 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
7028 if (c == NULL_TREE)
7030 c = c2;
7031 c2 = NULL_TREE;
7034 else
7035 omp_add_variable (gimplify_omp_ctxp, var,
7036 GOVD_PRIVATE | GOVD_SEEN);
7038 else
7039 var = decl;
7041 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7042 is_gimple_val, fb_rvalue);
7043 ret = MIN (ret, tret);
7044 if (ret == GS_ERROR)
7045 return ret;
7047 /* Handle OMP_FOR_COND. */
7048 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7049 gcc_assert (COMPARISON_CLASS_P (t));
7050 gcc_assert (TREE_OPERAND (t, 0) == decl);
7052 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7053 is_gimple_val, fb_rvalue);
7054 ret = MIN (ret, tret);
7056 /* Handle OMP_FOR_INCR. */
7057 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7058 switch (TREE_CODE (t))
7060 case PREINCREMENT_EXPR:
7061 case POSTINCREMENT_EXPR:
7063 tree decl = TREE_OPERAND (t, 0);
7064 /* c_omp_for_incr_canonicalize_ptr() should have been
7065 called to massage things appropriately. */
7066 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7068 if (orig_for_stmt != for_stmt)
7069 break;
7070 t = build_int_cst (TREE_TYPE (decl), 1);
7071 if (c)
7072 OMP_CLAUSE_LINEAR_STEP (c) = t;
7073 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7074 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7075 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7076 break;
7079 case PREDECREMENT_EXPR:
7080 case POSTDECREMENT_EXPR:
7081 /* c_omp_for_incr_canonicalize_ptr() should have been
7082 called to massage things appropriately. */
7083 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7084 if (orig_for_stmt != for_stmt)
7085 break;
7086 t = build_int_cst (TREE_TYPE (decl), -1);
7087 if (c)
7088 OMP_CLAUSE_LINEAR_STEP (c) = t;
7089 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7090 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7091 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7092 break;
7094 case MODIFY_EXPR:
7095 gcc_assert (TREE_OPERAND (t, 0) == decl);
7096 TREE_OPERAND (t, 0) = var;
7098 t = TREE_OPERAND (t, 1);
7099 switch (TREE_CODE (t))
7101 case PLUS_EXPR:
7102 if (TREE_OPERAND (t, 1) == decl)
7104 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
7105 TREE_OPERAND (t, 0) = var;
7106 break;
7109 /* Fallthru. */
7110 case MINUS_EXPR:
7111 case POINTER_PLUS_EXPR:
7112 gcc_assert (TREE_OPERAND (t, 0) == decl);
7113 TREE_OPERAND (t, 0) = var;
7114 break;
7115 default:
7116 gcc_unreachable ();
7119 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7120 is_gimple_val, fb_rvalue);
7121 ret = MIN (ret, tret);
7122 if (c)
7124 tree step = TREE_OPERAND (t, 1);
7125 tree stept = TREE_TYPE (decl);
7126 if (POINTER_TYPE_P (stept))
7127 stept = sizetype;
7128 step = fold_convert (stept, step);
7129 if (TREE_CODE (t) == MINUS_EXPR)
7130 step = fold_build1 (NEGATE_EXPR, stept, step);
7131 OMP_CLAUSE_LINEAR_STEP (c) = step;
7132 if (step != TREE_OPERAND (t, 1))
7134 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7135 &for_pre_body, NULL,
7136 is_gimple_val, fb_rvalue);
7137 ret = MIN (ret, tret);
7140 break;
7142 default:
7143 gcc_unreachable ();
7146 if (c2)
7148 gcc_assert (c);
7149 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7152 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7153 && orig_for_stmt == for_stmt)
7155 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7156 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7157 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7158 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7159 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7160 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7161 && OMP_CLAUSE_DECL (c) == decl)
7163 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7164 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7165 gcc_assert (TREE_OPERAND (t, 0) == var);
7166 t = TREE_OPERAND (t, 1);
7167 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7168 || TREE_CODE (t) == MINUS_EXPR
7169 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7170 gcc_assert (TREE_OPERAND (t, 0) == var);
7171 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7172 TREE_OPERAND (t, 1));
7173 gimple_seq *seq;
7174 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7175 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7176 else
7177 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7178 gimplify_assign (decl, t, seq);
7183 BITMAP_FREE (has_decl_expr);
7185 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7187 if (orig_for_stmt != for_stmt)
7188 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7190 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7191 decl = TREE_OPERAND (t, 0);
7192 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7193 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7194 TREE_OPERAND (t, 0) = var;
7195 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7196 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7197 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7200 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7202 int kind;
7203 switch (TREE_CODE (orig_for_stmt))
7205 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7206 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7207 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7208 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7209 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7210 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
7211 default:
7212 gcc_unreachable ();
7214 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7215 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7216 for_pre_body);
7217 if (orig_for_stmt != for_stmt)
7218 gimple_omp_for_set_combined_p (gfor, true);
7219 if (gimplify_omp_ctxp
7220 && (gimplify_omp_ctxp->combined_loop
7221 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7222 && gimplify_omp_ctxp->outer_context
7223 && gimplify_omp_ctxp->outer_context->combined_loop)))
7225 gimple_omp_for_set_combined_into_p (gfor, true);
7226 if (gimplify_omp_ctxp->combined_loop)
7227 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7228 else
7229 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7232 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7234 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7235 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7236 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7237 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7238 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7239 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7240 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7241 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7244 gimplify_seq_add_stmt (pre_p, gfor);
7245 if (ret != GS_ALL_DONE)
7246 return GS_ERROR;
7247 *expr_p = NULL_TREE;
7248 return GS_ALL_DONE;
7251 /* Gimplify the gross structure of several OMP constructs. */
7253 static void
7254 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7256 tree expr = *expr_p;
7257 gimple stmt;
7258 gimple_seq body = NULL;
7259 enum omp_region_type ort;
7261 switch (TREE_CODE (expr))
7263 case OMP_SECTIONS:
7264 case OMP_SINGLE:
7265 ort = ORT_WORKSHARE;
7266 break;
7267 case OACC_KERNELS:
7268 case OACC_PARALLEL:
7269 case OMP_TARGET:
7270 ort = ORT_TARGET;
7271 break;
7272 case OACC_DATA:
7273 case OMP_TARGET_DATA:
7274 ort = ORT_TARGET_DATA;
7275 break;
7276 case OMP_TEAMS:
7277 ort = ORT_TEAMS;
7278 break;
7279 default:
7280 gcc_unreachable ();
7282 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7283 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7285 push_gimplify_context ();
7286 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7287 if (gimple_code (g) == GIMPLE_BIND)
7288 pop_gimplify_context (g);
7289 else
7290 pop_gimplify_context (NULL);
7291 if (ort == ORT_TARGET_DATA)
7293 enum built_in_function end_ix;
7294 switch (TREE_CODE (expr))
7296 case OACC_DATA:
7297 end_ix = BUILT_IN_GOACC_DATA_END;
7298 break;
7299 case OMP_TARGET_DATA:
7300 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
7301 break;
7302 default:
7303 gcc_unreachable ();
7305 tree fn = builtin_decl_explicit (end_ix);
7306 g = gimple_build_call (fn, 0);
7307 gimple_seq cleanup = NULL;
7308 gimple_seq_add_stmt (&cleanup, g);
7309 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7310 body = NULL;
7311 gimple_seq_add_stmt (&body, g);
7314 else
7315 gimplify_and_add (OMP_BODY (expr), &body);
7316 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7318 switch (TREE_CODE (expr))
7320 case OACC_DATA:
7321 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
7322 OMP_CLAUSES (expr));
7323 break;
7324 case OACC_KERNELS:
7325 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
7326 OMP_CLAUSES (expr));
7327 break;
7328 case OACC_PARALLEL:
7329 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
7330 OMP_CLAUSES (expr));
7331 break;
7332 case OMP_SECTIONS:
7333 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7334 break;
7335 case OMP_SINGLE:
7336 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7337 break;
7338 case OMP_TARGET:
7339 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7340 OMP_CLAUSES (expr));
7341 break;
7342 case OMP_TARGET_DATA:
7343 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7344 OMP_CLAUSES (expr));
7345 break;
7346 case OMP_TEAMS:
7347 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7348 break;
7349 default:
7350 gcc_unreachable ();
7353 gimplify_seq_add_stmt (pre_p, stmt);
7354 *expr_p = NULL_TREE;
7357 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
7358 target update constructs. */
7360 static void
7361 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7363 tree expr = *expr_p, clauses;
7364 int kind;
7365 gomp_target *stmt;
7367 switch (TREE_CODE (expr))
7369 case OACC_ENTER_DATA:
7370 clauses = OACC_ENTER_DATA_CLAUSES (expr);
7371 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7372 break;
7373 case OACC_EXIT_DATA:
7374 clauses = OACC_EXIT_DATA_CLAUSES (expr);
7375 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7376 break;
7377 case OACC_UPDATE:
7378 clauses = OACC_UPDATE_CLAUSES (expr);
7379 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
7380 break;
7381 case OMP_TARGET_UPDATE:
7382 clauses = OMP_TARGET_UPDATE_CLAUSES (expr);
7383 kind = GF_OMP_TARGET_KIND_UPDATE;
7384 break;
7385 default:
7386 gcc_unreachable ();
7388 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_WORKSHARE);
7389 gimplify_adjust_omp_clauses (pre_p, &clauses);
7390 stmt = gimple_build_omp_target (NULL, kind, clauses);
7392 gimplify_seq_add_stmt (pre_p, stmt);
7393 *expr_p = NULL_TREE;
7396 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7397 stabilized the lhs of the atomic operation as *ADDR. Return true if
7398 EXPR is this stabilized form. */
7400 static bool
7401 goa_lhs_expr_p (tree expr, tree addr)
7403 /* Also include casts to other type variants. The C front end is fond
7404 of adding these for e.g. volatile variables. This is like
7405 STRIP_TYPE_NOPS but includes the main variant lookup. */
7406 STRIP_USELESS_TYPE_CONVERSION (expr);
7408 if (TREE_CODE (expr) == INDIRECT_REF)
7410 expr = TREE_OPERAND (expr, 0);
7411 while (expr != addr
7412 && (CONVERT_EXPR_P (expr)
7413 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7414 && TREE_CODE (expr) == TREE_CODE (addr)
7415 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7417 expr = TREE_OPERAND (expr, 0);
7418 addr = TREE_OPERAND (addr, 0);
7420 if (expr == addr)
7421 return true;
7422 return (TREE_CODE (addr) == ADDR_EXPR
7423 && TREE_CODE (expr) == ADDR_EXPR
7424 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7426 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7427 return true;
7428 return false;
7431 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7432 expression does not involve the lhs, evaluate it into a temporary.
7433 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7434 or -1 if an error was encountered. */
7436 static int
7437 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7438 tree lhs_var)
7440 tree expr = *expr_p;
7441 int saw_lhs;
7443 if (goa_lhs_expr_p (expr, lhs_addr))
7445 *expr_p = lhs_var;
7446 return 1;
7448 if (is_gimple_val (expr))
7449 return 0;
7451 saw_lhs = 0;
7452 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7454 case tcc_binary:
7455 case tcc_comparison:
7456 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7457 lhs_var);
7458 case tcc_unary:
7459 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7460 lhs_var);
7461 break;
7462 case tcc_expression:
7463 switch (TREE_CODE (expr))
7465 case TRUTH_ANDIF_EXPR:
7466 case TRUTH_ORIF_EXPR:
7467 case TRUTH_AND_EXPR:
7468 case TRUTH_OR_EXPR:
7469 case TRUTH_XOR_EXPR:
7470 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7471 lhs_addr, lhs_var);
7472 case TRUTH_NOT_EXPR:
7473 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7474 lhs_addr, lhs_var);
7475 break;
7476 case COMPOUND_EXPR:
7477 /* Break out any preevaluations from cp_build_modify_expr. */
7478 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7479 expr = TREE_OPERAND (expr, 1))
7480 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7481 *expr_p = expr;
7482 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7483 default:
7484 break;
7486 break;
7487 default:
7488 break;
7491 if (saw_lhs == 0)
7493 enum gimplify_status gs;
7494 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7495 if (gs != GS_ALL_DONE)
7496 saw_lhs = -1;
7499 return saw_lhs;
7502 /* Gimplify an OMP_ATOMIC statement. */
7504 static enum gimplify_status
7505 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7507 tree addr = TREE_OPERAND (*expr_p, 0);
7508 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7509 ? NULL : TREE_OPERAND (*expr_p, 1);
7510 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7511 tree tmp_load;
7512 gomp_atomic_load *loadstmt;
7513 gomp_atomic_store *storestmt;
7515 tmp_load = create_tmp_reg (type);
7516 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7517 return GS_ERROR;
7519 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7520 != GS_ALL_DONE)
7521 return GS_ERROR;
7523 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7524 gimplify_seq_add_stmt (pre_p, loadstmt);
7525 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7526 != GS_ALL_DONE)
7527 return GS_ERROR;
7529 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7530 rhs = tmp_load;
7531 storestmt = gimple_build_omp_atomic_store (rhs);
7532 gimplify_seq_add_stmt (pre_p, storestmt);
7533 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7535 gimple_omp_atomic_set_seq_cst (loadstmt);
7536 gimple_omp_atomic_set_seq_cst (storestmt);
7538 switch (TREE_CODE (*expr_p))
7540 case OMP_ATOMIC_READ:
7541 case OMP_ATOMIC_CAPTURE_OLD:
7542 *expr_p = tmp_load;
7543 gimple_omp_atomic_set_need_value (loadstmt);
7544 break;
7545 case OMP_ATOMIC_CAPTURE_NEW:
7546 *expr_p = rhs;
7547 gimple_omp_atomic_set_need_value (storestmt);
7548 break;
7549 default:
7550 *expr_p = NULL;
7551 break;
7554 return GS_ALL_DONE;
7557 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7558 body, and adding some EH bits. */
7560 static enum gimplify_status
7561 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7563 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7564 gimple body_stmt;
7565 gtransaction *trans_stmt;
7566 gimple_seq body = NULL;
7567 int subcode = 0;
7569 /* Wrap the transaction body in a BIND_EXPR so we have a context
7570 where to put decls for OMP. */
7571 if (TREE_CODE (tbody) != BIND_EXPR)
7573 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7574 TREE_SIDE_EFFECTS (bind) = 1;
7575 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7576 TRANSACTION_EXPR_BODY (expr) = bind;
7579 push_gimplify_context ();
7580 temp = voidify_wrapper_expr (*expr_p, NULL);
7582 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7583 pop_gimplify_context (body_stmt);
7585 trans_stmt = gimple_build_transaction (body, NULL);
7586 if (TRANSACTION_EXPR_OUTER (expr))
7587 subcode = GTMA_IS_OUTER;
7588 else if (TRANSACTION_EXPR_RELAXED (expr))
7589 subcode = GTMA_IS_RELAXED;
7590 gimple_transaction_set_subcode (trans_stmt, subcode);
7592 gimplify_seq_add_stmt (pre_p, trans_stmt);
7594 if (temp)
7596 *expr_p = temp;
7597 return GS_OK;
7600 *expr_p = NULL_TREE;
7601 return GS_ALL_DONE;
7604 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7605 expression produces a value to be used as an operand inside a GIMPLE
7606 statement, the value will be stored back in *EXPR_P. This value will
7607 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7608 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7609 emitted in PRE_P and POST_P.
7611 Additionally, this process may overwrite parts of the input
7612 expression during gimplification. Ideally, it should be
7613 possible to do non-destructive gimplification.
7615 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7616 the expression needs to evaluate to a value to be used as
7617 an operand in a GIMPLE statement, this value will be stored in
7618 *EXPR_P on exit. This happens when the caller specifies one
7619 of fb_lvalue or fb_rvalue fallback flags.
7621 PRE_P will contain the sequence of GIMPLE statements corresponding
7622 to the evaluation of EXPR and all the side-effects that must
7623 be executed before the main expression. On exit, the last
7624 statement of PRE_P is the core statement being gimplified. For
7625 instance, when gimplifying 'if (++a)' the last statement in
7626 PRE_P will be 'if (t.1)' where t.1 is the result of
7627 pre-incrementing 'a'.
7629 POST_P will contain the sequence of GIMPLE statements corresponding
7630 to the evaluation of all the side-effects that must be executed
7631 after the main expression. If this is NULL, the post
7632 side-effects are stored at the end of PRE_P.
7634 The reason why the output is split in two is to handle post
7635 side-effects explicitly. In some cases, an expression may have
7636 inner and outer post side-effects which need to be emitted in
7637 an order different from the one given by the recursive
7638 traversal. For instance, for the expression (*p--)++ the post
7639 side-effects of '--' must actually occur *after* the post
7640 side-effects of '++'. However, gimplification will first visit
7641 the inner expression, so if a separate POST sequence was not
7642 used, the resulting sequence would be:
7644 1 t.1 = *p
7645 2 p = p - 1
7646 3 t.2 = t.1 + 1
7647 4 *p = t.2
7649 However, the post-decrement operation in line #2 must not be
7650 evaluated until after the store to *p at line #4, so the
7651 correct sequence should be:
7653 1 t.1 = *p
7654 2 t.2 = t.1 + 1
7655 3 *p = t.2
7656 4 p = p - 1
7658 So, by specifying a separate post queue, it is possible
7659 to emit the post side-effects in the correct order.
7660 If POST_P is NULL, an internal queue will be used. Before
7661 returning to the caller, the sequence POST_P is appended to
7662 the main output sequence PRE_P.
7664 GIMPLE_TEST_F points to a function that takes a tree T and
7665 returns nonzero if T is in the GIMPLE form requested by the
7666 caller. The GIMPLE predicates are in gimple.c.
7668 FALLBACK tells the function what sort of a temporary we want if
7669 gimplification cannot produce an expression that complies with
7670 GIMPLE_TEST_F.
7672 fb_none means that no temporary should be generated
7673 fb_rvalue means that an rvalue is OK to generate
7674 fb_lvalue means that an lvalue is OK to generate
7675 fb_either means that either is OK, but an lvalue is preferable.
7676 fb_mayfail means that gimplification may fail (in which case
7677 GS_ERROR will be returned)
7679 The return value is either GS_ERROR or GS_ALL_DONE, since this
7680 function iterates until EXPR is completely gimplified or an error
7681 occurs. */
7683 enum gimplify_status
7684 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7685 bool (*gimple_test_f) (tree), fallback_t fallback)
7687 tree tmp;
7688 gimple_seq internal_pre = NULL;
7689 gimple_seq internal_post = NULL;
7690 tree save_expr;
7691 bool is_statement;
7692 location_t saved_location;
7693 enum gimplify_status ret;
7694 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7696 save_expr = *expr_p;
7697 if (save_expr == NULL_TREE)
7698 return GS_ALL_DONE;
7700 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7701 is_statement = gimple_test_f == is_gimple_stmt;
7702 if (is_statement)
7703 gcc_assert (pre_p);
7705 /* Consistency checks. */
7706 if (gimple_test_f == is_gimple_reg)
7707 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7708 else if (gimple_test_f == is_gimple_val
7709 || gimple_test_f == is_gimple_call_addr
7710 || gimple_test_f == is_gimple_condexpr
7711 || gimple_test_f == is_gimple_mem_rhs
7712 || gimple_test_f == is_gimple_mem_rhs_or_call
7713 || gimple_test_f == is_gimple_reg_rhs
7714 || gimple_test_f == is_gimple_reg_rhs_or_call
7715 || gimple_test_f == is_gimple_asm_val
7716 || gimple_test_f == is_gimple_mem_ref_addr)
7717 gcc_assert (fallback & fb_rvalue);
7718 else if (gimple_test_f == is_gimple_min_lval
7719 || gimple_test_f == is_gimple_lvalue)
7720 gcc_assert (fallback & fb_lvalue);
7721 else if (gimple_test_f == is_gimple_addressable)
7722 gcc_assert (fallback & fb_either);
7723 else if (gimple_test_f == is_gimple_stmt)
7724 gcc_assert (fallback == fb_none);
7725 else
7727 /* We should have recognized the GIMPLE_TEST_F predicate to
7728 know what kind of fallback to use in case a temporary is
7729 needed to hold the value or address of *EXPR_P. */
7730 gcc_unreachable ();
7733 /* We used to check the predicate here and return immediately if it
7734 succeeds. This is wrong; the design is for gimplification to be
7735 idempotent, and for the predicates to only test for valid forms, not
7736 whether they are fully simplified. */
7737 if (pre_p == NULL)
7738 pre_p = &internal_pre;
7740 if (post_p == NULL)
7741 post_p = &internal_post;
7743 /* Remember the last statements added to PRE_P and POST_P. Every
7744 new statement added by the gimplification helpers needs to be
7745 annotated with location information. To centralize the
7746 responsibility, we remember the last statement that had been
7747 added to both queues before gimplifying *EXPR_P. If
7748 gimplification produces new statements in PRE_P and POST_P, those
7749 statements will be annotated with the same location information
7750 as *EXPR_P. */
7751 pre_last_gsi = gsi_last (*pre_p);
7752 post_last_gsi = gsi_last (*post_p);
7754 saved_location = input_location;
7755 if (save_expr != error_mark_node
7756 && EXPR_HAS_LOCATION (*expr_p))
7757 input_location = EXPR_LOCATION (*expr_p);
7759 /* Loop over the specific gimplifiers until the toplevel node
7760 remains the same. */
7763 /* Strip away as many useless type conversions as possible
7764 at the toplevel. */
7765 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7767 /* Remember the expr. */
7768 save_expr = *expr_p;
7770 /* Die, die, die, my darling. */
7771 if (save_expr == error_mark_node
7772 || (TREE_TYPE (save_expr)
7773 && TREE_TYPE (save_expr) == error_mark_node))
7775 ret = GS_ERROR;
7776 break;
7779 /* Do any language-specific gimplification. */
7780 ret = ((enum gimplify_status)
7781 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7782 if (ret == GS_OK)
7784 if (*expr_p == NULL_TREE)
7785 break;
7786 if (*expr_p != save_expr)
7787 continue;
7789 else if (ret != GS_UNHANDLED)
7790 break;
7792 /* Make sure that all the cases set 'ret' appropriately. */
7793 ret = GS_UNHANDLED;
7794 switch (TREE_CODE (*expr_p))
7796 /* First deal with the special cases. */
7798 case POSTINCREMENT_EXPR:
7799 case POSTDECREMENT_EXPR:
7800 case PREINCREMENT_EXPR:
7801 case PREDECREMENT_EXPR:
7802 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7803 fallback != fb_none,
7804 TREE_TYPE (*expr_p));
7805 break;
7807 case VIEW_CONVERT_EXPR:
7808 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7809 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7811 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7812 post_p, is_gimple_val, fb_rvalue);
7813 recalculate_side_effects (*expr_p);
7814 break;
7816 /* Fallthru. */
7818 case ARRAY_REF:
7819 case ARRAY_RANGE_REF:
7820 case REALPART_EXPR:
7821 case IMAGPART_EXPR:
7822 case COMPONENT_REF:
7823 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7824 fallback ? fallback : fb_rvalue);
7825 break;
7827 case COND_EXPR:
7828 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7830 /* C99 code may assign to an array in a structure value of a
7831 conditional expression, and this has undefined behavior
7832 only on execution, so create a temporary if an lvalue is
7833 required. */
7834 if (fallback == fb_lvalue)
7836 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7837 mark_addressable (*expr_p);
7838 ret = GS_OK;
7840 break;
7842 case CALL_EXPR:
7843 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7845 /* C99 code may assign to an array in a structure returned
7846 from a function, and this has undefined behavior only on
7847 execution, so create a temporary if an lvalue is
7848 required. */
7849 if (fallback == fb_lvalue)
7851 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7852 mark_addressable (*expr_p);
7853 ret = GS_OK;
7855 break;
7857 case TREE_LIST:
7858 gcc_unreachable ();
7860 case COMPOUND_EXPR:
7861 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7862 break;
7864 case COMPOUND_LITERAL_EXPR:
7865 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7866 gimple_test_f, fallback);
7867 break;
7869 case MODIFY_EXPR:
7870 case INIT_EXPR:
7871 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7872 fallback != fb_none);
7873 break;
7875 case TRUTH_ANDIF_EXPR:
7876 case TRUTH_ORIF_EXPR:
7878 /* Preserve the original type of the expression and the
7879 source location of the outer expression. */
7880 tree org_type = TREE_TYPE (*expr_p);
7881 *expr_p = gimple_boolify (*expr_p);
7882 *expr_p = build3_loc (input_location, COND_EXPR,
7883 org_type, *expr_p,
7884 fold_convert_loc
7885 (input_location,
7886 org_type, boolean_true_node),
7887 fold_convert_loc
7888 (input_location,
7889 org_type, boolean_false_node));
7890 ret = GS_OK;
7891 break;
7894 case TRUTH_NOT_EXPR:
7896 tree type = TREE_TYPE (*expr_p);
7897 /* The parsers are careful to generate TRUTH_NOT_EXPR
7898 only with operands that are always zero or one.
7899 We do not fold here but handle the only interesting case
7900 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7901 *expr_p = gimple_boolify (*expr_p);
7902 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7903 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7904 TREE_TYPE (*expr_p),
7905 TREE_OPERAND (*expr_p, 0));
7906 else
7907 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7908 TREE_TYPE (*expr_p),
7909 TREE_OPERAND (*expr_p, 0),
7910 build_int_cst (TREE_TYPE (*expr_p), 1));
7911 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7912 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7913 ret = GS_OK;
7914 break;
7917 case ADDR_EXPR:
7918 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7919 break;
7921 case ANNOTATE_EXPR:
7923 tree cond = TREE_OPERAND (*expr_p, 0);
7924 tree kind = TREE_OPERAND (*expr_p, 1);
7925 tree type = TREE_TYPE (cond);
7926 if (!INTEGRAL_TYPE_P (type))
7928 *expr_p = cond;
7929 ret = GS_OK;
7930 break;
7932 tree tmp = create_tmp_var (type);
7933 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7934 gcall *call
7935 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7936 gimple_call_set_lhs (call, tmp);
7937 gimplify_seq_add_stmt (pre_p, call);
7938 *expr_p = tmp;
7939 ret = GS_ALL_DONE;
7940 break;
7943 case VA_ARG_EXPR:
7944 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7945 break;
7947 CASE_CONVERT:
7948 if (IS_EMPTY_STMT (*expr_p))
7950 ret = GS_ALL_DONE;
7951 break;
7954 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7955 || fallback == fb_none)
7957 /* Just strip a conversion to void (or in void context) and
7958 try again. */
7959 *expr_p = TREE_OPERAND (*expr_p, 0);
7960 ret = GS_OK;
7961 break;
7964 ret = gimplify_conversion (expr_p);
7965 if (ret == GS_ERROR)
7966 break;
7967 if (*expr_p != save_expr)
7968 break;
7969 /* FALLTHRU */
7971 case FIX_TRUNC_EXPR:
7972 /* unary_expr: ... | '(' cast ')' val | ... */
7973 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7974 is_gimple_val, fb_rvalue);
7975 recalculate_side_effects (*expr_p);
7976 break;
7978 case INDIRECT_REF:
7980 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7981 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7982 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7984 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7985 if (*expr_p != save_expr)
7987 ret = GS_OK;
7988 break;
7991 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7992 is_gimple_reg, fb_rvalue);
7993 if (ret == GS_ERROR)
7994 break;
7996 recalculate_side_effects (*expr_p);
7997 *expr_p = fold_build2_loc (input_location, MEM_REF,
7998 TREE_TYPE (*expr_p),
7999 TREE_OPERAND (*expr_p, 0),
8000 build_int_cst (saved_ptr_type, 0));
8001 TREE_THIS_VOLATILE (*expr_p) = volatilep;
8002 TREE_THIS_NOTRAP (*expr_p) = notrap;
8003 ret = GS_OK;
8004 break;
8007 /* We arrive here through the various re-gimplifcation paths. */
8008 case MEM_REF:
8009 /* First try re-folding the whole thing. */
8010 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
8011 TREE_OPERAND (*expr_p, 0),
8012 TREE_OPERAND (*expr_p, 1));
8013 if (tmp)
8015 *expr_p = tmp;
8016 recalculate_side_effects (*expr_p);
8017 ret = GS_OK;
8018 break;
8020 /* Avoid re-gimplifying the address operand if it is already
8021 in suitable form. Re-gimplifying would mark the address
8022 operand addressable. Always gimplify when not in SSA form
8023 as we still may have to gimplify decls with value-exprs. */
8024 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
8025 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
8027 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8028 is_gimple_mem_ref_addr, fb_rvalue);
8029 if (ret == GS_ERROR)
8030 break;
8032 recalculate_side_effects (*expr_p);
8033 ret = GS_ALL_DONE;
8034 break;
8036 /* Constants need not be gimplified. */
8037 case INTEGER_CST:
8038 case REAL_CST:
8039 case FIXED_CST:
8040 case STRING_CST:
8041 case COMPLEX_CST:
8042 case VECTOR_CST:
8043 /* Drop the overflow flag on constants, we do not want
8044 that in the GIMPLE IL. */
8045 if (TREE_OVERFLOW_P (*expr_p))
8046 *expr_p = drop_tree_overflow (*expr_p);
8047 ret = GS_ALL_DONE;
8048 break;
8050 case CONST_DECL:
8051 /* If we require an lvalue, such as for ADDR_EXPR, retain the
8052 CONST_DECL node. Otherwise the decl is replaceable by its
8053 value. */
8054 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
8055 if (fallback & fb_lvalue)
8056 ret = GS_ALL_DONE;
8057 else
8059 *expr_p = DECL_INITIAL (*expr_p);
8060 ret = GS_OK;
8062 break;
8064 case DECL_EXPR:
8065 ret = gimplify_decl_expr (expr_p, pre_p);
8066 break;
8068 case BIND_EXPR:
8069 ret = gimplify_bind_expr (expr_p, pre_p);
8070 break;
8072 case LOOP_EXPR:
8073 ret = gimplify_loop_expr (expr_p, pre_p);
8074 break;
8076 case SWITCH_EXPR:
8077 ret = gimplify_switch_expr (expr_p, pre_p);
8078 break;
8080 case EXIT_EXPR:
8081 ret = gimplify_exit_expr (expr_p);
8082 break;
8084 case GOTO_EXPR:
8085 /* If the target is not LABEL, then it is a computed jump
8086 and the target needs to be gimplified. */
8087 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
8089 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
8090 NULL, is_gimple_val, fb_rvalue);
8091 if (ret == GS_ERROR)
8092 break;
8094 gimplify_seq_add_stmt (pre_p,
8095 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
8096 ret = GS_ALL_DONE;
8097 break;
8099 case PREDICT_EXPR:
8100 gimplify_seq_add_stmt (pre_p,
8101 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
8102 PREDICT_EXPR_OUTCOME (*expr_p)));
8103 ret = GS_ALL_DONE;
8104 break;
8106 case LABEL_EXPR:
8107 ret = GS_ALL_DONE;
8108 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
8109 == current_function_decl);
8110 gimplify_seq_add_stmt (pre_p,
8111 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
8112 break;
8114 case CASE_LABEL_EXPR:
8115 ret = gimplify_case_label_expr (expr_p, pre_p);
8116 break;
8118 case RETURN_EXPR:
8119 ret = gimplify_return_expr (*expr_p, pre_p);
8120 break;
8122 case CONSTRUCTOR:
8123 /* Don't reduce this in place; let gimplify_init_constructor work its
8124 magic. Buf if we're just elaborating this for side effects, just
8125 gimplify any element that has side-effects. */
8126 if (fallback == fb_none)
8128 unsigned HOST_WIDE_INT ix;
8129 tree val;
8130 tree temp = NULL_TREE;
8131 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
8132 if (TREE_SIDE_EFFECTS (val))
8133 append_to_statement_list (val, &temp);
8135 *expr_p = temp;
8136 ret = temp ? GS_OK : GS_ALL_DONE;
8138 /* C99 code may assign to an array in a constructed
8139 structure or union, and this has undefined behavior only
8140 on execution, so create a temporary if an lvalue is
8141 required. */
8142 else if (fallback == fb_lvalue)
8144 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8145 mark_addressable (*expr_p);
8146 ret = GS_OK;
8148 else
8149 ret = GS_ALL_DONE;
8150 break;
8152 /* The following are special cases that are not handled by the
8153 original GIMPLE grammar. */
8155 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
8156 eliminated. */
8157 case SAVE_EXPR:
8158 ret = gimplify_save_expr (expr_p, pre_p, post_p);
8159 break;
8161 case BIT_FIELD_REF:
8162 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8163 post_p, is_gimple_lvalue, fb_either);
8164 recalculate_side_effects (*expr_p);
8165 break;
8167 case TARGET_MEM_REF:
8169 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
8171 if (TMR_BASE (*expr_p))
8172 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
8173 post_p, is_gimple_mem_ref_addr, fb_either);
8174 if (TMR_INDEX (*expr_p))
8175 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
8176 post_p, is_gimple_val, fb_rvalue);
8177 if (TMR_INDEX2 (*expr_p))
8178 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
8179 post_p, is_gimple_val, fb_rvalue);
8180 /* TMR_STEP and TMR_OFFSET are always integer constants. */
8181 ret = MIN (r0, r1);
8183 break;
8185 case NON_LVALUE_EXPR:
8186 /* This should have been stripped above. */
8187 gcc_unreachable ();
8189 case ASM_EXPR:
8190 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
8191 break;
8193 case TRY_FINALLY_EXPR:
8194 case TRY_CATCH_EXPR:
8196 gimple_seq eval, cleanup;
8197 gtry *try_;
8199 /* Calls to destructors are generated automatically in FINALLY/CATCH
8200 block. They should have location as UNKNOWN_LOCATION. However,
8201 gimplify_call_expr will reset these call stmts to input_location
8202 if it finds stmt's location is unknown. To prevent resetting for
8203 destructors, we set the input_location to unknown.
8204 Note that this only affects the destructor calls in FINALLY/CATCH
8205 block, and will automatically reset to its original value by the
8206 end of gimplify_expr. */
8207 input_location = UNKNOWN_LOCATION;
8208 eval = cleanup = NULL;
8209 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8210 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8211 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8212 if (gimple_seq_empty_p (cleanup))
8214 gimple_seq_add_seq (pre_p, eval);
8215 ret = GS_ALL_DONE;
8216 break;
8218 try_ = gimple_build_try (eval, cleanup,
8219 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8220 ? GIMPLE_TRY_FINALLY
8221 : GIMPLE_TRY_CATCH);
8222 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8223 gimple_set_location (try_, saved_location);
8224 else
8225 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8226 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8227 gimple_try_set_catch_is_cleanup (try_,
8228 TRY_CATCH_IS_CLEANUP (*expr_p));
8229 gimplify_seq_add_stmt (pre_p, try_);
8230 ret = GS_ALL_DONE;
8231 break;
8234 case CLEANUP_POINT_EXPR:
8235 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8236 break;
8238 case TARGET_EXPR:
8239 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8240 break;
8242 case CATCH_EXPR:
8244 gimple c;
8245 gimple_seq handler = NULL;
8246 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8247 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8248 gimplify_seq_add_stmt (pre_p, c);
8249 ret = GS_ALL_DONE;
8250 break;
8253 case EH_FILTER_EXPR:
8255 gimple ehf;
8256 gimple_seq failure = NULL;
8258 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8259 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8260 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8261 gimplify_seq_add_stmt (pre_p, ehf);
8262 ret = GS_ALL_DONE;
8263 break;
8266 case OBJ_TYPE_REF:
8268 enum gimplify_status r0, r1;
8269 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8270 post_p, is_gimple_val, fb_rvalue);
8271 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8272 post_p, is_gimple_val, fb_rvalue);
8273 TREE_SIDE_EFFECTS (*expr_p) = 0;
8274 ret = MIN (r0, r1);
8276 break;
8278 case LABEL_DECL:
8279 /* We get here when taking the address of a label. We mark
8280 the label as "forced"; meaning it can never be removed and
8281 it is a potential target for any computed goto. */
8282 FORCED_LABEL (*expr_p) = 1;
8283 ret = GS_ALL_DONE;
8284 break;
8286 case STATEMENT_LIST:
8287 ret = gimplify_statement_list (expr_p, pre_p);
8288 break;
8290 case WITH_SIZE_EXPR:
8292 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8293 post_p == &internal_post ? NULL : post_p,
8294 gimple_test_f, fallback);
8295 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8296 is_gimple_val, fb_rvalue);
8297 ret = GS_ALL_DONE;
8299 break;
8301 case VAR_DECL:
8302 case PARM_DECL:
8303 ret = gimplify_var_or_parm_decl (expr_p);
8304 break;
8306 case RESULT_DECL:
8307 /* When within an OMP context, notice uses of variables. */
8308 if (gimplify_omp_ctxp)
8309 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8310 ret = GS_ALL_DONE;
8311 break;
8313 case SSA_NAME:
8314 /* Allow callbacks into the gimplifier during optimization. */
8315 ret = GS_ALL_DONE;
8316 break;
8318 case OMP_PARALLEL:
8319 gimplify_omp_parallel (expr_p, pre_p);
8320 ret = GS_ALL_DONE;
8321 break;
8323 case OMP_TASK:
8324 gimplify_omp_task (expr_p, pre_p);
8325 ret = GS_ALL_DONE;
8326 break;
8328 case OMP_FOR:
8329 case OMP_SIMD:
8330 case CILK_SIMD:
8331 case CILK_FOR:
8332 case OMP_DISTRIBUTE:
8333 case OACC_LOOP:
8334 ret = gimplify_omp_for (expr_p, pre_p);
8335 break;
8337 case OACC_CACHE:
8338 gimplify_oacc_cache (expr_p, pre_p);
8339 ret = GS_ALL_DONE;
8340 break;
8342 case OACC_HOST_DATA:
8343 case OACC_DECLARE:
8344 sorry ("directive not yet implemented");
8345 ret = GS_ALL_DONE;
8346 break;
8348 case OACC_KERNELS:
8349 if (OACC_KERNELS_COMBINED (*expr_p))
8350 sorry ("directive not yet implemented");
8351 else
8352 gimplify_omp_workshare (expr_p, pre_p);
8353 ret = GS_ALL_DONE;
8354 break;
8356 case OACC_PARALLEL:
8357 if (OACC_PARALLEL_COMBINED (*expr_p))
8358 sorry ("directive not yet implemented");
8359 else
8360 gimplify_omp_workshare (expr_p, pre_p);
8361 ret = GS_ALL_DONE;
8362 break;
8364 case OACC_DATA:
8365 case OMP_SECTIONS:
8366 case OMP_SINGLE:
8367 case OMP_TARGET:
8368 case OMP_TARGET_DATA:
8369 case OMP_TEAMS:
8370 gimplify_omp_workshare (expr_p, pre_p);
8371 ret = GS_ALL_DONE;
8372 break;
8374 case OACC_ENTER_DATA:
8375 case OACC_EXIT_DATA:
8376 case OACC_UPDATE:
8377 case OMP_TARGET_UPDATE:
8378 gimplify_omp_target_update (expr_p, pre_p);
8379 ret = GS_ALL_DONE;
8380 break;
8382 case OMP_SECTION:
8383 case OMP_MASTER:
8384 case OMP_TASKGROUP:
8385 case OMP_ORDERED:
8386 case OMP_CRITICAL:
8388 gimple_seq body = NULL;
8389 gimple g;
8391 gimplify_and_add (OMP_BODY (*expr_p), &body);
8392 switch (TREE_CODE (*expr_p))
8394 case OMP_SECTION:
8395 g = gimple_build_omp_section (body);
8396 break;
8397 case OMP_MASTER:
8398 g = gimple_build_omp_master (body);
8399 break;
8400 case OMP_TASKGROUP:
8402 gimple_seq cleanup = NULL;
8403 tree fn
8404 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8405 g = gimple_build_call (fn, 0);
8406 gimple_seq_add_stmt (&cleanup, g);
8407 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8408 body = NULL;
8409 gimple_seq_add_stmt (&body, g);
8410 g = gimple_build_omp_taskgroup (body);
8412 break;
8413 case OMP_ORDERED:
8414 g = gimple_build_omp_ordered (body);
8415 break;
8416 case OMP_CRITICAL:
8417 g = gimple_build_omp_critical (body,
8418 OMP_CRITICAL_NAME (*expr_p));
8419 break;
8420 default:
8421 gcc_unreachable ();
8423 gimplify_seq_add_stmt (pre_p, g);
8424 ret = GS_ALL_DONE;
8425 break;
8428 case OMP_ATOMIC:
8429 case OMP_ATOMIC_READ:
8430 case OMP_ATOMIC_CAPTURE_OLD:
8431 case OMP_ATOMIC_CAPTURE_NEW:
8432 ret = gimplify_omp_atomic (expr_p, pre_p);
8433 break;
8435 case TRANSACTION_EXPR:
8436 ret = gimplify_transaction (expr_p, pre_p);
8437 break;
8439 case TRUTH_AND_EXPR:
8440 case TRUTH_OR_EXPR:
8441 case TRUTH_XOR_EXPR:
8443 tree orig_type = TREE_TYPE (*expr_p);
8444 tree new_type, xop0, xop1;
8445 *expr_p = gimple_boolify (*expr_p);
8446 new_type = TREE_TYPE (*expr_p);
8447 if (!useless_type_conversion_p (orig_type, new_type))
8449 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8450 ret = GS_OK;
8451 break;
8454 /* Boolified binary truth expressions are semantically equivalent
8455 to bitwise binary expressions. Canonicalize them to the
8456 bitwise variant. */
8457 switch (TREE_CODE (*expr_p))
8459 case TRUTH_AND_EXPR:
8460 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8461 break;
8462 case TRUTH_OR_EXPR:
8463 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8464 break;
8465 case TRUTH_XOR_EXPR:
8466 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8467 break;
8468 default:
8469 break;
8471 /* Now make sure that operands have compatible type to
8472 expression's new_type. */
8473 xop0 = TREE_OPERAND (*expr_p, 0);
8474 xop1 = TREE_OPERAND (*expr_p, 1);
8475 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8476 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8477 new_type,
8478 xop0);
8479 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8480 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8481 new_type,
8482 xop1);
8483 /* Continue classified as tcc_binary. */
8484 goto expr_2;
8487 case FMA_EXPR:
8488 case VEC_COND_EXPR:
8489 case VEC_PERM_EXPR:
8490 /* Classified as tcc_expression. */
8491 goto expr_3;
8493 case POINTER_PLUS_EXPR:
8495 enum gimplify_status r0, r1;
8496 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8497 post_p, is_gimple_val, fb_rvalue);
8498 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8499 post_p, is_gimple_val, fb_rvalue);
8500 recalculate_side_effects (*expr_p);
8501 ret = MIN (r0, r1);
8502 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8503 after gimplifying operands - this is similar to how
8504 it would be folding all gimplified stmts on creation
8505 to have them canonicalized, which is what we eventually
8506 should do anyway. */
8507 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8508 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8510 *expr_p = build_fold_addr_expr_with_type_loc
8511 (input_location,
8512 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8513 TREE_OPERAND (*expr_p, 0),
8514 fold_convert (ptr_type_node,
8515 TREE_OPERAND (*expr_p, 1))),
8516 TREE_TYPE (*expr_p));
8517 ret = MIN (ret, GS_OK);
8519 break;
8522 case CILK_SYNC_STMT:
8524 if (!fn_contains_cilk_spawn_p (cfun))
8526 error_at (EXPR_LOCATION (*expr_p),
8527 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8528 ret = GS_ERROR;
8530 else
8532 gimplify_cilk_sync (expr_p, pre_p);
8533 ret = GS_ALL_DONE;
8535 break;
8538 default:
8539 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8541 case tcc_comparison:
8542 /* Handle comparison of objects of non scalar mode aggregates
8543 with a call to memcmp. It would be nice to only have to do
8544 this for variable-sized objects, but then we'd have to allow
8545 the same nest of reference nodes we allow for MODIFY_EXPR and
8546 that's too complex.
8548 Compare scalar mode aggregates as scalar mode values. Using
8549 memcmp for them would be very inefficient at best, and is
8550 plain wrong if bitfields are involved. */
8552 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8554 /* Vector comparisons need no boolification. */
8555 if (TREE_CODE (type) == VECTOR_TYPE)
8556 goto expr_2;
8557 else if (!AGGREGATE_TYPE_P (type))
8559 tree org_type = TREE_TYPE (*expr_p);
8560 *expr_p = gimple_boolify (*expr_p);
8561 if (!useless_type_conversion_p (org_type,
8562 TREE_TYPE (*expr_p)))
8564 *expr_p = fold_convert_loc (input_location,
8565 org_type, *expr_p);
8566 ret = GS_OK;
8568 else
8569 goto expr_2;
8571 else if (TYPE_MODE (type) != BLKmode)
8572 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8573 else
8574 ret = gimplify_variable_sized_compare (expr_p);
8576 break;
8579 /* If *EXPR_P does not need to be special-cased, handle it
8580 according to its class. */
8581 case tcc_unary:
8582 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8583 post_p, is_gimple_val, fb_rvalue);
8584 break;
8586 case tcc_binary:
8587 expr_2:
8589 enum gimplify_status r0, r1;
8591 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8592 post_p, is_gimple_val, fb_rvalue);
8593 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8594 post_p, is_gimple_val, fb_rvalue);
8596 ret = MIN (r0, r1);
8597 break;
8600 expr_3:
8602 enum gimplify_status r0, r1, r2;
8604 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8605 post_p, is_gimple_val, fb_rvalue);
8606 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8607 post_p, is_gimple_val, fb_rvalue);
8608 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8609 post_p, is_gimple_val, fb_rvalue);
8611 ret = MIN (MIN (r0, r1), r2);
8612 break;
8615 case tcc_declaration:
8616 case tcc_constant:
8617 ret = GS_ALL_DONE;
8618 goto dont_recalculate;
8620 default:
8621 gcc_unreachable ();
8624 recalculate_side_effects (*expr_p);
8626 dont_recalculate:
8627 break;
8630 gcc_assert (*expr_p || ret != GS_OK);
8632 while (ret == GS_OK);
8634 /* If we encountered an error_mark somewhere nested inside, either
8635 stub out the statement or propagate the error back out. */
8636 if (ret == GS_ERROR)
8638 if (is_statement)
8639 *expr_p = NULL;
8640 goto out;
8643 /* This was only valid as a return value from the langhook, which
8644 we handled. Make sure it doesn't escape from any other context. */
8645 gcc_assert (ret != GS_UNHANDLED);
8647 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8649 /* We aren't looking for a value, and we don't have a valid
8650 statement. If it doesn't have side-effects, throw it away. */
8651 if (!TREE_SIDE_EFFECTS (*expr_p))
8652 *expr_p = NULL;
8653 else if (!TREE_THIS_VOLATILE (*expr_p))
8655 /* This is probably a _REF that contains something nested that
8656 has side effects. Recurse through the operands to find it. */
8657 enum tree_code code = TREE_CODE (*expr_p);
8659 switch (code)
8661 case COMPONENT_REF:
8662 case REALPART_EXPR:
8663 case IMAGPART_EXPR:
8664 case VIEW_CONVERT_EXPR:
8665 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8666 gimple_test_f, fallback);
8667 break;
8669 case ARRAY_REF:
8670 case ARRAY_RANGE_REF:
8671 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8672 gimple_test_f, fallback);
8673 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8674 gimple_test_f, fallback);
8675 break;
8677 default:
8678 /* Anything else with side-effects must be converted to
8679 a valid statement before we get here. */
8680 gcc_unreachable ();
8683 *expr_p = NULL;
8685 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8686 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8688 /* Historically, the compiler has treated a bare reference
8689 to a non-BLKmode volatile lvalue as forcing a load. */
8690 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8692 /* Normally, we do not want to create a temporary for a
8693 TREE_ADDRESSABLE type because such a type should not be
8694 copied by bitwise-assignment. However, we make an
8695 exception here, as all we are doing here is ensuring that
8696 we read the bytes that make up the type. We use
8697 create_tmp_var_raw because create_tmp_var will abort when
8698 given a TREE_ADDRESSABLE type. */
8699 tree tmp = create_tmp_var_raw (type, "vol");
8700 gimple_add_tmp_var (tmp);
8701 gimplify_assign (tmp, *expr_p, pre_p);
8702 *expr_p = NULL;
8704 else
8705 /* We can't do anything useful with a volatile reference to
8706 an incomplete type, so just throw it away. Likewise for
8707 a BLKmode type, since any implicit inner load should
8708 already have been turned into an explicit one by the
8709 gimplification process. */
8710 *expr_p = NULL;
8713 /* If we are gimplifying at the statement level, we're done. Tack
8714 everything together and return. */
8715 if (fallback == fb_none || is_statement)
8717 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8718 it out for GC to reclaim it. */
8719 *expr_p = NULL_TREE;
8721 if (!gimple_seq_empty_p (internal_pre)
8722 || !gimple_seq_empty_p (internal_post))
8724 gimplify_seq_add_seq (&internal_pre, internal_post);
8725 gimplify_seq_add_seq (pre_p, internal_pre);
8728 /* The result of gimplifying *EXPR_P is going to be the last few
8729 statements in *PRE_P and *POST_P. Add location information
8730 to all the statements that were added by the gimplification
8731 helpers. */
8732 if (!gimple_seq_empty_p (*pre_p))
8733 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8735 if (!gimple_seq_empty_p (*post_p))
8736 annotate_all_with_location_after (*post_p, post_last_gsi,
8737 input_location);
8739 goto out;
8742 #ifdef ENABLE_GIMPLE_CHECKING
8743 if (*expr_p)
8745 enum tree_code code = TREE_CODE (*expr_p);
8746 /* These expressions should already be in gimple IR form. */
8747 gcc_assert (code != MODIFY_EXPR
8748 && code != ASM_EXPR
8749 && code != BIND_EXPR
8750 && code != CATCH_EXPR
8751 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8752 && code != EH_FILTER_EXPR
8753 && code != GOTO_EXPR
8754 && code != LABEL_EXPR
8755 && code != LOOP_EXPR
8756 && code != SWITCH_EXPR
8757 && code != TRY_FINALLY_EXPR
8758 && code != OACC_PARALLEL
8759 && code != OACC_KERNELS
8760 && code != OACC_DATA
8761 && code != OACC_HOST_DATA
8762 && code != OACC_DECLARE
8763 && code != OACC_UPDATE
8764 && code != OACC_ENTER_DATA
8765 && code != OACC_EXIT_DATA
8766 && code != OACC_CACHE
8767 && code != OMP_CRITICAL
8768 && code != OMP_FOR
8769 && code != OACC_LOOP
8770 && code != OMP_MASTER
8771 && code != OMP_TASKGROUP
8772 && code != OMP_ORDERED
8773 && code != OMP_PARALLEL
8774 && code != OMP_SECTIONS
8775 && code != OMP_SECTION
8776 && code != OMP_SINGLE);
8778 #endif
8780 /* Otherwise we're gimplifying a subexpression, so the resulting
8781 value is interesting. If it's a valid operand that matches
8782 GIMPLE_TEST_F, we're done. Unless we are handling some
8783 post-effects internally; if that's the case, we need to copy into
8784 a temporary before adding the post-effects to POST_P. */
8785 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8786 goto out;
8788 /* Otherwise, we need to create a new temporary for the gimplified
8789 expression. */
8791 /* We can't return an lvalue if we have an internal postqueue. The
8792 object the lvalue refers to would (probably) be modified by the
8793 postqueue; we need to copy the value out first, which means an
8794 rvalue. */
8795 if ((fallback & fb_lvalue)
8796 && gimple_seq_empty_p (internal_post)
8797 && is_gimple_addressable (*expr_p))
8799 /* An lvalue will do. Take the address of the expression, store it
8800 in a temporary, and replace the expression with an INDIRECT_REF of
8801 that temporary. */
8802 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8803 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8804 *expr_p = build_simple_mem_ref (tmp);
8806 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8808 /* An rvalue will do. Assign the gimplified expression into a
8809 new temporary TMP and replace the original expression with
8810 TMP. First, make sure that the expression has a type so that
8811 it can be assigned into a temporary. */
8812 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8813 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8815 else
8817 #ifdef ENABLE_GIMPLE_CHECKING
8818 if (!(fallback & fb_mayfail))
8820 fprintf (stderr, "gimplification failed:\n");
8821 print_generic_expr (stderr, *expr_p, 0);
8822 debug_tree (*expr_p);
8823 internal_error ("gimplification failed");
8825 #endif
8826 gcc_assert (fallback & fb_mayfail);
8828 /* If this is an asm statement, and the user asked for the
8829 impossible, don't die. Fail and let gimplify_asm_expr
8830 issue an error. */
8831 ret = GS_ERROR;
8832 goto out;
8835 /* Make sure the temporary matches our predicate. */
8836 gcc_assert ((*gimple_test_f) (*expr_p));
8838 if (!gimple_seq_empty_p (internal_post))
8840 annotate_all_with_location (internal_post, input_location);
8841 gimplify_seq_add_seq (pre_p, internal_post);
8844 out:
8845 input_location = saved_location;
8846 return ret;
8849 /* Look through TYPE for variable-sized objects and gimplify each such
8850 size that we find. Add to LIST_P any statements generated. */
8852 void
8853 gimplify_type_sizes (tree type, gimple_seq *list_p)
8855 tree field, t;
8857 if (type == NULL || type == error_mark_node)
8858 return;
8860 /* We first do the main variant, then copy into any other variants. */
8861 type = TYPE_MAIN_VARIANT (type);
8863 /* Avoid infinite recursion. */
8864 if (TYPE_SIZES_GIMPLIFIED (type))
8865 return;
8867 TYPE_SIZES_GIMPLIFIED (type) = 1;
8869 switch (TREE_CODE (type))
8871 case INTEGER_TYPE:
8872 case ENUMERAL_TYPE:
8873 case BOOLEAN_TYPE:
8874 case REAL_TYPE:
8875 case FIXED_POINT_TYPE:
8876 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8877 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8879 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8881 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8882 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8884 break;
8886 case ARRAY_TYPE:
8887 /* These types may not have declarations, so handle them here. */
8888 gimplify_type_sizes (TREE_TYPE (type), list_p);
8889 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8890 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8891 with assigned stack slots, for -O1+ -g they should be tracked
8892 by VTA. */
8893 if (!(TYPE_NAME (type)
8894 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8895 && DECL_IGNORED_P (TYPE_NAME (type)))
8896 && TYPE_DOMAIN (type)
8897 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8899 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8900 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8901 DECL_IGNORED_P (t) = 0;
8902 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8903 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8904 DECL_IGNORED_P (t) = 0;
8906 break;
8908 case RECORD_TYPE:
8909 case UNION_TYPE:
8910 case QUAL_UNION_TYPE:
8911 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8912 if (TREE_CODE (field) == FIELD_DECL)
8914 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8915 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8916 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8917 gimplify_type_sizes (TREE_TYPE (field), list_p);
8919 break;
8921 case POINTER_TYPE:
8922 case REFERENCE_TYPE:
8923 /* We used to recurse on the pointed-to type here, which turned out to
8924 be incorrect because its definition might refer to variables not
8925 yet initialized at this point if a forward declaration is involved.
8927 It was actually useful for anonymous pointed-to types to ensure
8928 that the sizes evaluation dominates every possible later use of the
8929 values. Restricting to such types here would be safe since there
8930 is no possible forward declaration around, but would introduce an
8931 undesirable middle-end semantic to anonymity. We then defer to
8932 front-ends the responsibility of ensuring that the sizes are
8933 evaluated both early and late enough, e.g. by attaching artificial
8934 type declarations to the tree. */
8935 break;
8937 default:
8938 break;
8941 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8942 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8944 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8946 TYPE_SIZE (t) = TYPE_SIZE (type);
8947 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8948 TYPE_SIZES_GIMPLIFIED (t) = 1;
8952 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8953 a size or position, has had all of its SAVE_EXPRs evaluated.
8954 We add any required statements to *STMT_P. */
8956 void
8957 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8959 tree expr = *expr_p;
8961 /* We don't do anything if the value isn't there, is constant, or contains
8962 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8963 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8964 will want to replace it with a new variable, but that will cause problems
8965 if this type is from outside the function. It's OK to have that here. */
8966 if (is_gimple_sizepos (expr))
8967 return;
8969 *expr_p = unshare_expr (expr);
8971 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8974 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8975 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8976 is true, also gimplify the parameters. */
8978 gbind *
8979 gimplify_body (tree fndecl, bool do_parms)
8981 location_t saved_location = input_location;
8982 gimple_seq parm_stmts, seq;
8983 gimple outer_stmt;
8984 gbind *outer_bind;
8985 struct cgraph_node *cgn;
8987 timevar_push (TV_TREE_GIMPLIFY);
8989 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8990 gimplification. */
8991 default_rtl_profile ();
8993 gcc_assert (gimplify_ctxp == NULL);
8994 push_gimplify_context ();
8996 if (flag_openacc || flag_openmp)
8998 gcc_assert (gimplify_omp_ctxp == NULL);
8999 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
9000 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
9003 /* Unshare most shared trees in the body and in that of any nested functions.
9004 It would seem we don't have to do this for nested functions because
9005 they are supposed to be output and then the outer function gimplified
9006 first, but the g++ front end doesn't always do it that way. */
9007 unshare_body (fndecl);
9008 unvisit_body (fndecl);
9010 cgn = cgraph_node::get (fndecl);
9011 if (cgn && cgn->origin)
9012 nonlocal_vlas = new hash_set<tree>;
9014 /* Make sure input_location isn't set to something weird. */
9015 input_location = DECL_SOURCE_LOCATION (fndecl);
9017 /* Resolve callee-copies. This has to be done before processing
9018 the body so that DECL_VALUE_EXPR gets processed correctly. */
9019 parm_stmts = do_parms ? gimplify_parameters () : NULL;
9021 /* Gimplify the function's body. */
9022 seq = NULL;
9023 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
9024 outer_stmt = gimple_seq_first_stmt (seq);
9025 if (!outer_stmt)
9027 outer_stmt = gimple_build_nop ();
9028 gimplify_seq_add_stmt (&seq, outer_stmt);
9031 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
9032 not the case, wrap everything in a GIMPLE_BIND to make it so. */
9033 if (gimple_code (outer_stmt) == GIMPLE_BIND
9034 && gimple_seq_first (seq) == gimple_seq_last (seq))
9035 outer_bind = as_a <gbind *> (outer_stmt);
9036 else
9037 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
9039 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9041 /* If we had callee-copies statements, insert them at the beginning
9042 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
9043 if (!gimple_seq_empty_p (parm_stmts))
9045 tree parm;
9047 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
9048 gimple_bind_set_body (outer_bind, parm_stmts);
9050 for (parm = DECL_ARGUMENTS (current_function_decl);
9051 parm; parm = DECL_CHAIN (parm))
9052 if (DECL_HAS_VALUE_EXPR_P (parm))
9054 DECL_HAS_VALUE_EXPR_P (parm) = 0;
9055 DECL_IGNORED_P (parm) = 0;
9059 if (nonlocal_vlas)
9061 if (nonlocal_vla_vars)
9063 /* tree-nested.c may later on call declare_vars (..., true);
9064 which relies on BLOCK_VARS chain to be the tail of the
9065 gimple_bind_vars chain. Ensure we don't violate that
9066 assumption. */
9067 if (gimple_bind_block (outer_bind)
9068 == DECL_INITIAL (current_function_decl))
9069 declare_vars (nonlocal_vla_vars, outer_bind, true);
9070 else
9071 BLOCK_VARS (DECL_INITIAL (current_function_decl))
9072 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
9073 nonlocal_vla_vars);
9074 nonlocal_vla_vars = NULL_TREE;
9076 delete nonlocal_vlas;
9077 nonlocal_vlas = NULL;
9080 if ((flag_openacc || flag_openmp || flag_openmp_simd)
9081 && gimplify_omp_ctxp)
9083 delete_omp_context (gimplify_omp_ctxp);
9084 gimplify_omp_ctxp = NULL;
9087 pop_gimplify_context (outer_bind);
9088 gcc_assert (gimplify_ctxp == NULL);
9090 #ifdef ENABLE_CHECKING
9091 if (!seen_error ())
9092 verify_gimple_in_seq (gimple_bind_body (outer_bind));
9093 #endif
9095 timevar_pop (TV_TREE_GIMPLIFY);
9096 input_location = saved_location;
9098 return outer_bind;
9101 typedef char *char_p; /* For DEF_VEC_P. */
9103 /* Return whether we should exclude FNDECL from instrumentation. */
9105 static bool
9106 flag_instrument_functions_exclude_p (tree fndecl)
9108 vec<char_p> *v;
9110 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
9111 if (v && v->length () > 0)
9113 const char *name;
9114 int i;
9115 char *s;
9117 name = lang_hooks.decl_printable_name (fndecl, 0);
9118 FOR_EACH_VEC_ELT (*v, i, s)
9119 if (strstr (name, s) != NULL)
9120 return true;
9123 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
9124 if (v && v->length () > 0)
9126 const char *name;
9127 int i;
9128 char *s;
9130 name = DECL_SOURCE_FILE (fndecl);
9131 FOR_EACH_VEC_ELT (*v, i, s)
9132 if (strstr (name, s) != NULL)
9133 return true;
9136 return false;
9139 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
9140 node for the function we want to gimplify.
9142 Return the sequence of GIMPLE statements corresponding to the body
9143 of FNDECL. */
9145 void
9146 gimplify_function_tree (tree fndecl)
9148 tree parm, ret;
9149 gimple_seq seq;
9150 gbind *bind;
9152 gcc_assert (!gimple_body (fndecl));
9154 if (DECL_STRUCT_FUNCTION (fndecl))
9155 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
9156 else
9157 push_struct_function (fndecl);
9159 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
9161 /* Preliminarily mark non-addressed complex variables as eligible
9162 for promotion to gimple registers. We'll transform their uses
9163 as we find them. */
9164 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
9165 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
9166 && !TREE_THIS_VOLATILE (parm)
9167 && !needs_to_live_in_memory (parm))
9168 DECL_GIMPLE_REG_P (parm) = 1;
9171 ret = DECL_RESULT (fndecl);
9172 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
9173 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
9174 && !needs_to_live_in_memory (ret))
9175 DECL_GIMPLE_REG_P (ret) = 1;
9177 bind = gimplify_body (fndecl, true);
9179 /* The tree body of the function is no longer needed, replace it
9180 with the new GIMPLE body. */
9181 seq = NULL;
9182 gimple_seq_add_stmt (&seq, bind);
9183 gimple_set_body (fndecl, seq);
9185 /* If we're instrumenting function entry/exit, then prepend the call to
9186 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
9187 catch the exit hook. */
9188 /* ??? Add some way to ignore exceptions for this TFE. */
9189 if (flag_instrument_function_entry_exit
9190 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
9191 && !flag_instrument_functions_exclude_p (fndecl))
9193 tree x;
9194 gbind *new_bind;
9195 gimple tf;
9196 gimple_seq cleanup = NULL, body = NULL;
9197 tree tmp_var;
9198 gcall *call;
9200 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9201 call = gimple_build_call (x, 1, integer_zero_node);
9202 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9203 gimple_call_set_lhs (call, tmp_var);
9204 gimplify_seq_add_stmt (&cleanup, call);
9205 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
9206 call = gimple_build_call (x, 2,
9207 build_fold_addr_expr (current_function_decl),
9208 tmp_var);
9209 gimplify_seq_add_stmt (&cleanup, call);
9210 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
9212 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9213 call = gimple_build_call (x, 1, integer_zero_node);
9214 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9215 gimple_call_set_lhs (call, tmp_var);
9216 gimplify_seq_add_stmt (&body, call);
9217 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
9218 call = gimple_build_call (x, 2,
9219 build_fold_addr_expr (current_function_decl),
9220 tmp_var);
9221 gimplify_seq_add_stmt (&body, call);
9222 gimplify_seq_add_stmt (&body, tf);
9223 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
9224 /* Clear the block for BIND, since it is no longer directly inside
9225 the function, but within a try block. */
9226 gimple_bind_set_block (bind, NULL);
9228 /* Replace the current function body with the body
9229 wrapped in the try/finally TF. */
9230 seq = NULL;
9231 gimple_seq_add_stmt (&seq, new_bind);
9232 gimple_set_body (fndecl, seq);
9235 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9236 cfun->curr_properties = PROP_gimple_any;
9238 pop_cfun ();
9241 /* Return a dummy expression of type TYPE in order to keep going after an
9242 error. */
9244 static tree
9245 dummy_object (tree type)
9247 tree t = build_int_cst (build_pointer_type (type), 0);
9248 return build2 (MEM_REF, type, t, t);
9251 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9252 builtin function, but a very special sort of operator. */
9254 enum gimplify_status
9255 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9257 tree promoted_type, have_va_type;
9258 tree valist = TREE_OPERAND (*expr_p, 0);
9259 tree type = TREE_TYPE (*expr_p);
9260 tree t;
9261 location_t loc = EXPR_LOCATION (*expr_p);
9263 /* Verify that valist is of the proper type. */
9264 have_va_type = TREE_TYPE (valist);
9265 if (have_va_type == error_mark_node)
9266 return GS_ERROR;
9267 have_va_type = targetm.canonical_va_list_type (have_va_type);
9269 if (have_va_type == NULL_TREE)
9271 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9272 return GS_ERROR;
9275 /* Generate a diagnostic for requesting data of a type that cannot
9276 be passed through `...' due to type promotion at the call site. */
9277 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9278 != type)
9280 static bool gave_help;
9281 bool warned;
9283 /* Unfortunately, this is merely undefined, rather than a constraint
9284 violation, so we cannot make this an error. If this call is never
9285 executed, the program is still strictly conforming. */
9286 warned = warning_at (loc, 0,
9287 "%qT is promoted to %qT when passed through %<...%>",
9288 type, promoted_type);
9289 if (!gave_help && warned)
9291 gave_help = true;
9292 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9293 promoted_type, type);
9296 /* We can, however, treat "undefined" any way we please.
9297 Call abort to encourage the user to fix the program. */
9298 if (warned)
9299 inform (loc, "if this code is reached, the program will abort");
9300 /* Before the abort, allow the evaluation of the va_list
9301 expression to exit or longjmp. */
9302 gimplify_and_add (valist, pre_p);
9303 t = build_call_expr_loc (loc,
9304 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9305 gimplify_and_add (t, pre_p);
9307 /* This is dead code, but go ahead and finish so that the
9308 mode of the result comes out right. */
9309 *expr_p = dummy_object (type);
9310 return GS_ALL_DONE;
9312 else
9314 /* Make it easier for the backends by protecting the valist argument
9315 from multiple evaluations. */
9316 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9318 /* For this case, the backends will be expecting a pointer to
9319 TREE_TYPE (abi), but it's possible we've
9320 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9321 So fix it. */
9322 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9324 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9325 valist = fold_convert_loc (loc, p1,
9326 build_fold_addr_expr_loc (loc, valist));
9329 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9331 else
9332 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9334 if (!targetm.gimplify_va_arg_expr)
9335 /* FIXME: Once most targets are converted we should merely
9336 assert this is non-null. */
9337 return GS_ALL_DONE;
9339 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9340 return GS_OK;
9344 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9346 DST/SRC are the destination and source respectively. You can pass
9347 ungimplified trees in DST or SRC, in which case they will be
9348 converted to a gimple operand if necessary.
9350 This function returns the newly created GIMPLE_ASSIGN tuple. */
9352 gimple
9353 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9355 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9356 gimplify_and_add (t, seq_p);
9357 ggc_free (t);
9358 return gimple_seq_last_stmt (*seq_p);
9361 inline hashval_t
9362 gimplify_hasher::hash (const value_type *p)
9364 tree t = p->val;
9365 return iterative_hash_expr (t, 0);
9368 inline bool
9369 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9371 tree t1 = p1->val;
9372 tree t2 = p2->val;
9373 enum tree_code code = TREE_CODE (t1);
9375 if (TREE_CODE (t2) != code
9376 || TREE_TYPE (t1) != TREE_TYPE (t2))
9377 return false;
9379 if (!operand_equal_p (t1, t2, 0))
9380 return false;
9382 #ifdef ENABLE_CHECKING
9383 /* Only allow them to compare equal if they also hash equal; otherwise
9384 results are nondeterminate, and we fail bootstrap comparison. */
9385 gcc_assert (hash (p1) == hash (p2));
9386 #endif
9388 return true;