2014-04-14 Martin Jambor <mjambor@suse.cz>
[official-gcc.git] / gcc / gimplify.c
blob744178420ab70242f48b055a2c37825e60ed7b2d
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 "pointer-set.h"
29 #include "hash-table.h"
30 #include "basic-block.h"
31 #include "tree-ssa-alias.h"
32 #include "internal-fn.h"
33 #include "gimple-fold.h"
34 #include "tree-eh.h"
35 #include "gimple-expr.h"
36 #include "is-a.h"
37 #include "gimple.h"
38 #include "gimplify.h"
39 #include "gimple-iterator.h"
40 #include "stringpool.h"
41 #include "calls.h"
42 #include "varasm.h"
43 #include "stor-layout.h"
44 #include "stmt.h"
45 #include "print-tree.h"
46 #include "tree-iterator.h"
47 #include "tree-inline.h"
48 #include "tree-pretty-print.h"
49 #include "langhooks.h"
50 #include "bitmap.h"
51 #include "gimple-ssa.h"
52 #include "cgraph.h"
53 #include "tree-cfg.h"
54 #include "tree-ssanames.h"
55 #include "tree-ssa.h"
56 #include "diagnostic-core.h"
57 #include "target.h"
58 #include "splay-tree.h"
59 #include "omp-low.h"
60 #include "gimple-low.h"
61 #include "cilk.h"
63 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
64 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
66 enum gimplify_omp_var_data
68 GOVD_SEEN = 1,
69 GOVD_EXPLICIT = 2,
70 GOVD_SHARED = 4,
71 GOVD_PRIVATE = 8,
72 GOVD_FIRSTPRIVATE = 16,
73 GOVD_LASTPRIVATE = 32,
74 GOVD_REDUCTION = 64,
75 GOVD_LOCAL = 128,
76 GOVD_MAP = 256,
77 GOVD_DEBUG_PRIVATE = 512,
78 GOVD_PRIVATE_OUTER_REF = 1024,
79 GOVD_LINEAR = 2048,
80 GOVD_ALIGNED = 4096,
81 GOVD_MAP_TO_ONLY = 8192,
82 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
83 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
84 | GOVD_LOCAL)
88 enum omp_region_type
90 ORT_WORKSHARE = 0,
91 ORT_SIMD = 1,
92 ORT_PARALLEL = 2,
93 ORT_COMBINED_PARALLEL = 3,
94 ORT_TASK = 4,
95 ORT_UNTIED_TASK = 5,
96 ORT_TEAMS = 8,
97 ORT_TARGET_DATA = 16,
98 ORT_TARGET = 32
101 /* Gimplify hashtable helper. */
103 struct gimplify_hasher : typed_free_remove <elt_t>
105 typedef elt_t value_type;
106 typedef elt_t compare_type;
107 static inline hashval_t hash (const value_type *);
108 static inline bool equal (const value_type *, const compare_type *);
111 struct gimplify_ctx
113 struct gimplify_ctx *prev_context;
115 vec<gimple> bind_expr_stack;
116 tree temps;
117 gimple_seq conditional_cleanups;
118 tree exit_label;
119 tree return_temp;
121 vec<tree> case_labels;
122 /* The formal temporary table. Should this be persistent? */
123 hash_table <gimplify_hasher> temp_htab;
125 int conditions;
126 bool save_stack;
127 bool into_ssa;
128 bool allow_rhs_cond_expr;
129 bool in_cleanup_point_expr;
132 struct gimplify_omp_ctx
134 struct gimplify_omp_ctx *outer_context;
135 splay_tree variables;
136 struct pointer_set_t *privatized_types;
137 location_t location;
138 enum omp_clause_default_kind default_kind;
139 enum omp_region_type region_type;
140 bool combined_loop;
143 static struct gimplify_ctx *gimplify_ctxp;
144 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
146 /* Forward declaration. */
147 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
149 /* Shorter alias name for the above function for use in gimplify.c
150 only. */
152 static inline void
153 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
155 gimple_seq_add_stmt_without_update (seq_p, gs);
158 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
159 NULL, a new sequence is allocated. This function is
160 similar to gimple_seq_add_seq, but does not scan the operands.
161 During gimplification, we need to manipulate statement sequences
162 before the def/use vectors have been constructed. */
164 static void
165 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
167 gimple_stmt_iterator si;
169 if (src == NULL)
170 return;
172 si = gsi_last (*dst_p);
173 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
177 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
178 and popping gimplify contexts. */
180 static struct gimplify_ctx *ctx_pool = NULL;
182 /* Return a gimplify context struct from the pool. */
184 static inline struct gimplify_ctx *
185 ctx_alloc (void)
187 struct gimplify_ctx * c = ctx_pool;
189 if (c)
190 ctx_pool = c->prev_context;
191 else
192 c = XNEW (struct gimplify_ctx);
194 memset (c, '\0', sizeof (*c));
195 return c;
198 /* Put gimplify context C back into the pool. */
200 static inline void
201 ctx_free (struct gimplify_ctx *c)
203 c->prev_context = ctx_pool;
204 ctx_pool = c;
207 /* Free allocated ctx stack memory. */
209 void
210 free_gimplify_stack (void)
212 struct gimplify_ctx *c;
214 while ((c = ctx_pool))
216 ctx_pool = c->prev_context;
217 free (c);
222 /* Set up a context for the gimplifier. */
224 void
225 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
227 struct gimplify_ctx *c = ctx_alloc ();
229 c->prev_context = gimplify_ctxp;
230 gimplify_ctxp = c;
231 gimplify_ctxp->into_ssa = in_ssa;
232 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
235 /* Tear down a context for the gimplifier. If BODY is non-null, then
236 put the temporaries into the outer BIND_EXPR. Otherwise, put them
237 in the local_decls.
239 BODY is not a sequence, but the first tuple in a sequence. */
241 void
242 pop_gimplify_context (gimple body)
244 struct gimplify_ctx *c = gimplify_ctxp;
246 gcc_assert (c
247 && (!c->bind_expr_stack.exists ()
248 || c->bind_expr_stack.is_empty ()));
249 c->bind_expr_stack.release ();
250 gimplify_ctxp = c->prev_context;
252 if (body)
253 declare_vars (c->temps, body, false);
254 else
255 record_vars (c->temps);
257 if (c->temp_htab.is_created ())
258 c->temp_htab.dispose ();
259 ctx_free (c);
262 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
264 static void
265 gimple_push_bind_expr (gimple gimple_bind)
267 gimplify_ctxp->bind_expr_stack.reserve (8);
268 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
271 /* Pop the first element off the stack of bindings. */
273 static void
274 gimple_pop_bind_expr (void)
276 gimplify_ctxp->bind_expr_stack.pop ();
279 /* Return the first element of the stack of bindings. */
281 gimple
282 gimple_current_bind_expr (void)
284 return gimplify_ctxp->bind_expr_stack.last ();
287 /* Return the stack of bindings created during gimplification. */
289 vec<gimple>
290 gimple_bind_expr_stack (void)
292 return gimplify_ctxp->bind_expr_stack;
295 /* Return true iff there is a COND_EXPR between us and the innermost
296 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
298 static bool
299 gimple_conditional_context (void)
301 return gimplify_ctxp->conditions > 0;
304 /* Note that we've entered a COND_EXPR. */
306 static void
307 gimple_push_condition (void)
309 #ifdef ENABLE_GIMPLE_CHECKING
310 if (gimplify_ctxp->conditions == 0)
311 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
312 #endif
313 ++(gimplify_ctxp->conditions);
316 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
317 now, add any conditional cleanups we've seen to the prequeue. */
319 static void
320 gimple_pop_condition (gimple_seq *pre_p)
322 int conds = --(gimplify_ctxp->conditions);
324 gcc_assert (conds >= 0);
325 if (conds == 0)
327 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
328 gimplify_ctxp->conditional_cleanups = NULL;
332 /* A stable comparison routine for use with splay trees and DECLs. */
334 static int
335 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
337 tree a = (tree) xa;
338 tree b = (tree) xb;
340 return DECL_UID (a) - DECL_UID (b);
343 /* Create a new omp construct that deals with variable remapping. */
345 static struct gimplify_omp_ctx *
346 new_omp_context (enum omp_region_type region_type)
348 struct gimplify_omp_ctx *c;
350 c = XCNEW (struct gimplify_omp_ctx);
351 c->outer_context = gimplify_omp_ctxp;
352 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
353 c->privatized_types = pointer_set_create ();
354 c->location = input_location;
355 c->region_type = region_type;
356 if ((region_type & ORT_TASK) == 0)
357 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
358 else
359 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
361 return c;
364 /* Destroy an omp construct that deals with variable remapping. */
366 static void
367 delete_omp_context (struct gimplify_omp_ctx *c)
369 splay_tree_delete (c->variables);
370 pointer_set_destroy (c->privatized_types);
371 XDELETE (c);
374 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
375 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
377 /* Both gimplify the statement T and append it to *SEQ_P. This function
378 behaves exactly as gimplify_stmt, but you don't have to pass T as a
379 reference. */
381 void
382 gimplify_and_add (tree t, gimple_seq *seq_p)
384 gimplify_stmt (&t, seq_p);
387 /* Gimplify statement T into sequence *SEQ_P, and return the first
388 tuple in the sequence of generated tuples for this statement.
389 Return NULL if gimplifying T produced no tuples. */
391 static gimple
392 gimplify_and_return_first (tree t, gimple_seq *seq_p)
394 gimple_stmt_iterator last = gsi_last (*seq_p);
396 gimplify_and_add (t, seq_p);
398 if (!gsi_end_p (last))
400 gsi_next (&last);
401 return gsi_stmt (last);
403 else
404 return gimple_seq_first_stmt (*seq_p);
407 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
408 LHS, or for a call argument. */
410 static bool
411 is_gimple_mem_rhs (tree t)
413 /* If we're dealing with a renamable type, either source or dest must be
414 a renamed variable. */
415 if (is_gimple_reg_type (TREE_TYPE (t)))
416 return is_gimple_val (t);
417 else
418 return is_gimple_val (t) || is_gimple_lvalue (t);
421 /* Return true if T is a CALL_EXPR or an expression that can be
422 assigned to a temporary. Note that this predicate should only be
423 used during gimplification. See the rationale for this in
424 gimplify_modify_expr. */
426 static bool
427 is_gimple_reg_rhs_or_call (tree t)
429 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
430 || TREE_CODE (t) == CALL_EXPR);
433 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
434 this predicate should only be used during gimplification. See the
435 rationale for this in gimplify_modify_expr. */
437 static bool
438 is_gimple_mem_rhs_or_call (tree t)
440 /* If we're dealing with a renamable type, either source or dest must be
441 a renamed variable. */
442 if (is_gimple_reg_type (TREE_TYPE (t)))
443 return is_gimple_val (t);
444 else
445 return (is_gimple_val (t) || is_gimple_lvalue (t)
446 || TREE_CODE (t) == CALL_EXPR);
449 /* Create a temporary with a name derived from VAL. Subroutine of
450 lookup_tmp_var; nobody else should call this function. */
452 static inline tree
453 create_tmp_from_val (tree val, bool is_formal)
455 /* Drop all qualifiers and address-space information from the value type. */
456 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
457 tree var = create_tmp_var (type, get_name (val));
458 if (is_formal
459 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
460 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE))
461 DECL_GIMPLE_REG_P (var) = 1;
462 return var;
465 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
466 an existing expression temporary. */
468 static tree
469 lookup_tmp_var (tree val, bool is_formal)
471 tree ret;
473 /* If not optimizing, never really reuse a temporary. local-alloc
474 won't allocate any variable that is used in more than one basic
475 block, which means it will go into memory, causing much extra
476 work in reload and final and poorer code generation, outweighing
477 the extra memory allocation here. */
478 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
479 ret = create_tmp_from_val (val, is_formal);
480 else
482 elt_t elt, *elt_p;
483 elt_t **slot;
485 elt.val = val;
486 if (!gimplify_ctxp->temp_htab.is_created ())
487 gimplify_ctxp->temp_htab.create (1000);
488 slot = gimplify_ctxp->temp_htab.find_slot (&elt, INSERT);
489 if (*slot == NULL)
491 elt_p = XNEW (elt_t);
492 elt_p->val = val;
493 elt_p->temp = ret = create_tmp_from_val (val, is_formal);
494 *slot = elt_p;
496 else
498 elt_p = *slot;
499 ret = elt_p->temp;
503 return ret;
506 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
508 static tree
509 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
510 bool is_formal)
512 tree t, mod;
514 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
515 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
516 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
517 fb_rvalue);
519 if (gimplify_ctxp->into_ssa
520 && is_gimple_reg_type (TREE_TYPE (val)))
521 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
522 else
523 t = lookup_tmp_var (val, is_formal);
525 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
527 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
529 /* gimplify_modify_expr might want to reduce this further. */
530 gimplify_and_add (mod, pre_p);
531 ggc_free (mod);
533 return t;
536 /* Return a formal temporary variable initialized with VAL. PRE_P is as
537 in gimplify_expr. Only use this function if:
539 1) The value of the unfactored expression represented by VAL will not
540 change between the initialization and use of the temporary, and
541 2) The temporary will not be otherwise modified.
543 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
544 and #2 means it is inappropriate for && temps.
546 For other cases, use get_initialized_tmp_var instead. */
548 tree
549 get_formal_tmp_var (tree val, gimple_seq *pre_p)
551 return internal_get_tmp_var (val, pre_p, NULL, true);
554 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
555 are as in gimplify_expr. */
557 tree
558 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
560 return internal_get_tmp_var (val, pre_p, post_p, false);
563 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
564 generate debug info for them; otherwise don't. */
566 void
567 declare_vars (tree vars, gimple scope, bool debug_info)
569 tree last = vars;
570 if (last)
572 tree temps, block;
574 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
576 temps = nreverse (last);
578 block = gimple_bind_block (scope);
579 gcc_assert (!block || TREE_CODE (block) == BLOCK);
580 if (!block || !debug_info)
582 DECL_CHAIN (last) = gimple_bind_vars (scope);
583 gimple_bind_set_vars (scope, temps);
585 else
587 /* We need to attach the nodes both to the BIND_EXPR and to its
588 associated BLOCK for debugging purposes. The key point here
589 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
590 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
591 if (BLOCK_VARS (block))
592 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
593 else
595 gimple_bind_set_vars (scope,
596 chainon (gimple_bind_vars (scope), temps));
597 BLOCK_VARS (block) = temps;
603 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
604 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
605 no such upper bound can be obtained. */
607 static void
608 force_constant_size (tree var)
610 /* The only attempt we make is by querying the maximum size of objects
611 of the variable's type. */
613 HOST_WIDE_INT max_size;
615 gcc_assert (TREE_CODE (var) == VAR_DECL);
617 max_size = max_int_size_in_bytes (TREE_TYPE (var));
619 gcc_assert (max_size >= 0);
621 DECL_SIZE_UNIT (var)
622 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
623 DECL_SIZE (var)
624 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
627 /* Push the temporary variable TMP into the current binding. */
629 void
630 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
632 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
634 /* Later processing assumes that the object size is constant, which might
635 not be true at this point. Force the use of a constant upper bound in
636 this case. */
637 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
638 force_constant_size (tmp);
640 DECL_CONTEXT (tmp) = fn->decl;
641 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
643 record_vars_into (tmp, fn->decl);
646 /* Push the temporary variable TMP into the current binding. */
648 void
649 gimple_add_tmp_var (tree tmp)
651 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
653 /* Later processing assumes that the object size is constant, which might
654 not be true at this point. Force the use of a constant upper bound in
655 this case. */
656 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
657 force_constant_size (tmp);
659 DECL_CONTEXT (tmp) = current_function_decl;
660 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
662 if (gimplify_ctxp)
664 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
665 gimplify_ctxp->temps = tmp;
667 /* Mark temporaries local within the nearest enclosing parallel. */
668 if (gimplify_omp_ctxp)
670 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
671 while (ctx
672 && (ctx->region_type == ORT_WORKSHARE
673 || ctx->region_type == ORT_SIMD))
674 ctx = ctx->outer_context;
675 if (ctx)
676 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
679 else if (cfun)
680 record_vars (tmp);
681 else
683 gimple_seq body_seq;
685 /* This case is for nested functions. We need to expose the locals
686 they create. */
687 body_seq = gimple_body (current_function_decl);
688 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
694 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
695 nodes that are referenced more than once in GENERIC functions. This is
696 necessary because gimplification (translation into GIMPLE) is performed
697 by modifying tree nodes in-place, so gimplication of a shared node in a
698 first context could generate an invalid GIMPLE form in a second context.
700 This is achieved with a simple mark/copy/unmark algorithm that walks the
701 GENERIC representation top-down, marks nodes with TREE_VISITED the first
702 time it encounters them, duplicates them if they already have TREE_VISITED
703 set, and finally removes the TREE_VISITED marks it has set.
705 The algorithm works only at the function level, i.e. it generates a GENERIC
706 representation of a function with no nodes shared within the function when
707 passed a GENERIC function (except for nodes that are allowed to be shared).
709 At the global level, it is also necessary to unshare tree nodes that are
710 referenced in more than one function, for the same aforementioned reason.
711 This requires some cooperation from the front-end. There are 2 strategies:
713 1. Manual unsharing. The front-end needs to call unshare_expr on every
714 expression that might end up being shared across functions.
716 2. Deep unsharing. This is an extension of regular unsharing. Instead
717 of calling unshare_expr on expressions that might be shared across
718 functions, the front-end pre-marks them with TREE_VISITED. This will
719 ensure that they are unshared on the first reference within functions
720 when the regular unsharing algorithm runs. The counterpart is that
721 this algorithm must look deeper than for manual unsharing, which is
722 specified by LANG_HOOKS_DEEP_UNSHARING.
724 If there are only few specific cases of node sharing across functions, it is
725 probably easier for a front-end to unshare the expressions manually. On the
726 contrary, if the expressions generated at the global level are as widespread
727 as expressions generated within functions, deep unsharing is very likely the
728 way to go. */
730 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
731 These nodes model computations that must be done once. If we were to
732 unshare something like SAVE_EXPR(i++), the gimplification process would
733 create wrong code. However, if DATA is non-null, it must hold a pointer
734 set that is used to unshare the subtrees of these nodes. */
736 static tree
737 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
739 tree t = *tp;
740 enum tree_code code = TREE_CODE (t);
742 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
743 copy their subtrees if we can make sure to do it only once. */
744 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
746 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
748 else
749 *walk_subtrees = 0;
752 /* Stop at types, decls, constants like copy_tree_r. */
753 else if (TREE_CODE_CLASS (code) == tcc_type
754 || TREE_CODE_CLASS (code) == tcc_declaration
755 || TREE_CODE_CLASS (code) == tcc_constant
756 /* We can't do anything sensible with a BLOCK used as an
757 expression, but we also can't just die when we see it
758 because of non-expression uses. So we avert our eyes
759 and cross our fingers. Silly Java. */
760 || code == BLOCK)
761 *walk_subtrees = 0;
763 /* Cope with the statement expression extension. */
764 else if (code == STATEMENT_LIST)
767 /* Leave the bulk of the work to copy_tree_r itself. */
768 else
769 copy_tree_r (tp, walk_subtrees, NULL);
771 return NULL_TREE;
774 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
775 If *TP has been visited already, then *TP is deeply copied by calling
776 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
778 static tree
779 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
781 tree t = *tp;
782 enum tree_code code = TREE_CODE (t);
784 /* Skip types, decls, and constants. But we do want to look at their
785 types and the bounds of types. Mark them as visited so we properly
786 unmark their subtrees on the unmark pass. If we've already seen them,
787 don't look down further. */
788 if (TREE_CODE_CLASS (code) == tcc_type
789 || TREE_CODE_CLASS (code) == tcc_declaration
790 || TREE_CODE_CLASS (code) == tcc_constant)
792 if (TREE_VISITED (t))
793 *walk_subtrees = 0;
794 else
795 TREE_VISITED (t) = 1;
798 /* If this node has been visited already, unshare it and don't look
799 any deeper. */
800 else if (TREE_VISITED (t))
802 walk_tree (tp, mostly_copy_tree_r, data, NULL);
803 *walk_subtrees = 0;
806 /* Otherwise, mark the node as visited and keep looking. */
807 else
808 TREE_VISITED (t) = 1;
810 return NULL_TREE;
813 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
814 copy_if_shared_r callback unmodified. */
816 static inline void
817 copy_if_shared (tree *tp, void *data)
819 walk_tree (tp, copy_if_shared_r, data, NULL);
822 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
823 any nested functions. */
825 static void
826 unshare_body (tree fndecl)
828 struct cgraph_node *cgn = cgraph_get_node (fndecl);
829 /* If the language requires deep unsharing, we need a pointer set to make
830 sure we don't repeatedly unshare subtrees of unshareable nodes. */
831 struct pointer_set_t *visited
832 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
834 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
835 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
836 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
838 if (visited)
839 pointer_set_destroy (visited);
841 if (cgn)
842 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
843 unshare_body (cgn->decl);
846 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
847 Subtrees are walked until the first unvisited node is encountered. */
849 static tree
850 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
852 tree t = *tp;
854 /* If this node has been visited, unmark it and keep looking. */
855 if (TREE_VISITED (t))
856 TREE_VISITED (t) = 0;
858 /* Otherwise, don't look any deeper. */
859 else
860 *walk_subtrees = 0;
862 return NULL_TREE;
865 /* Unmark the visited trees rooted at *TP. */
867 static inline void
868 unmark_visited (tree *tp)
870 walk_tree (tp, unmark_visited_r, NULL, NULL);
873 /* Likewise, but mark all trees as not visited. */
875 static void
876 unvisit_body (tree fndecl)
878 struct cgraph_node *cgn = cgraph_get_node (fndecl);
880 unmark_visited (&DECL_SAVED_TREE (fndecl));
881 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
882 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
884 if (cgn)
885 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
886 unvisit_body (cgn->decl);
889 /* Unconditionally make an unshared copy of EXPR. This is used when using
890 stored expressions which span multiple functions, such as BINFO_VTABLE,
891 as the normal unsharing process can't tell that they're shared. */
893 tree
894 unshare_expr (tree expr)
896 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
897 return expr;
900 /* Worker for unshare_expr_without_location. */
902 static tree
903 prune_expr_location (tree *tp, int *walk_subtrees, void *)
905 if (EXPR_P (*tp))
906 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
907 else
908 *walk_subtrees = 0;
909 return NULL_TREE;
912 /* Similar to unshare_expr but also prune all expression locations
913 from EXPR. */
915 tree
916 unshare_expr_without_location (tree expr)
918 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
919 if (EXPR_P (expr))
920 walk_tree (&expr, prune_expr_location, NULL, NULL);
921 return expr;
924 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
925 contain statements and have a value. Assign its value to a temporary
926 and give it void_type_node. Return the temporary, or NULL_TREE if
927 WRAPPER was already void. */
929 tree
930 voidify_wrapper_expr (tree wrapper, tree temp)
932 tree type = TREE_TYPE (wrapper);
933 if (type && !VOID_TYPE_P (type))
935 tree *p;
937 /* Set p to point to the body of the wrapper. Loop until we find
938 something that isn't a wrapper. */
939 for (p = &wrapper; p && *p; )
941 switch (TREE_CODE (*p))
943 case BIND_EXPR:
944 TREE_SIDE_EFFECTS (*p) = 1;
945 TREE_TYPE (*p) = void_type_node;
946 /* For a BIND_EXPR, the body is operand 1. */
947 p = &BIND_EXPR_BODY (*p);
948 break;
950 case CLEANUP_POINT_EXPR:
951 case TRY_FINALLY_EXPR:
952 case TRY_CATCH_EXPR:
953 TREE_SIDE_EFFECTS (*p) = 1;
954 TREE_TYPE (*p) = void_type_node;
955 p = &TREE_OPERAND (*p, 0);
956 break;
958 case STATEMENT_LIST:
960 tree_stmt_iterator i = tsi_last (*p);
961 TREE_SIDE_EFFECTS (*p) = 1;
962 TREE_TYPE (*p) = void_type_node;
963 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
965 break;
967 case COMPOUND_EXPR:
968 /* Advance to the last statement. Set all container types to
969 void. */
970 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
972 TREE_SIDE_EFFECTS (*p) = 1;
973 TREE_TYPE (*p) = void_type_node;
975 break;
977 case TRANSACTION_EXPR:
978 TREE_SIDE_EFFECTS (*p) = 1;
979 TREE_TYPE (*p) = void_type_node;
980 p = &TRANSACTION_EXPR_BODY (*p);
981 break;
983 default:
984 /* Assume that any tree upon which voidify_wrapper_expr is
985 directly called is a wrapper, and that its body is op0. */
986 if (p == &wrapper)
988 TREE_SIDE_EFFECTS (*p) = 1;
989 TREE_TYPE (*p) = void_type_node;
990 p = &TREE_OPERAND (*p, 0);
991 break;
993 goto out;
997 out:
998 if (p == NULL || IS_EMPTY_STMT (*p))
999 temp = NULL_TREE;
1000 else if (temp)
1002 /* The wrapper is on the RHS of an assignment that we're pushing
1003 down. */
1004 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1005 || TREE_CODE (temp) == MODIFY_EXPR);
1006 TREE_OPERAND (temp, 1) = *p;
1007 *p = temp;
1009 else
1011 temp = create_tmp_var (type, "retval");
1012 *p = build2 (INIT_EXPR, type, temp, *p);
1015 return temp;
1018 return NULL_TREE;
1021 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1022 a temporary through which they communicate. */
1024 static void
1025 build_stack_save_restore (gimple *save, gimple *restore)
1027 tree tmp_var;
1029 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1030 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1031 gimple_call_set_lhs (*save, tmp_var);
1033 *restore
1034 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1035 1, tmp_var);
1038 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1040 static enum gimplify_status
1041 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1043 tree bind_expr = *expr_p;
1044 bool old_save_stack = gimplify_ctxp->save_stack;
1045 tree t;
1046 gimple gimple_bind;
1047 gimple_seq body, cleanup;
1048 gimple stack_save;
1050 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1052 /* Mark variables seen in this bind expr. */
1053 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1055 if (TREE_CODE (t) == VAR_DECL)
1057 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1059 /* Mark variable as local. */
1060 if (ctx && !DECL_EXTERNAL (t)
1061 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1062 || splay_tree_lookup (ctx->variables,
1063 (splay_tree_key) t) == NULL))
1065 if (ctx->region_type == ORT_SIMD
1066 && TREE_ADDRESSABLE (t)
1067 && !TREE_STATIC (t))
1068 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1069 else
1070 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1073 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1075 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1076 cfun->has_local_explicit_reg_vars = true;
1079 /* Preliminarily mark non-addressed complex variables as eligible
1080 for promotion to gimple registers. We'll transform their uses
1081 as we find them. */
1082 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1083 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1084 && !TREE_THIS_VOLATILE (t)
1085 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1086 && !needs_to_live_in_memory (t))
1087 DECL_GIMPLE_REG_P (t) = 1;
1090 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1091 BIND_EXPR_BLOCK (bind_expr));
1092 gimple_push_bind_expr (gimple_bind);
1094 gimplify_ctxp->save_stack = false;
1096 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1097 body = NULL;
1098 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1099 gimple_bind_set_body (gimple_bind, body);
1101 cleanup = NULL;
1102 stack_save = NULL;
1103 if (gimplify_ctxp->save_stack)
1105 gimple stack_restore;
1107 /* Save stack on entry and restore it on exit. Add a try_finally
1108 block to achieve this. */
1109 build_stack_save_restore (&stack_save, &stack_restore);
1111 gimplify_seq_add_stmt (&cleanup, stack_restore);
1114 /* Add clobbers for all variables that go out of scope. */
1115 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1117 if (TREE_CODE (t) == VAR_DECL
1118 && !is_global_var (t)
1119 && DECL_CONTEXT (t) == current_function_decl
1120 && !DECL_HARD_REGISTER (t)
1121 && !TREE_THIS_VOLATILE (t)
1122 && !DECL_HAS_VALUE_EXPR_P (t)
1123 /* Only care for variables that have to be in memory. Others
1124 will be rewritten into SSA names, hence moved to the top-level. */
1125 && !is_gimple_reg (t)
1126 && flag_stack_reuse != SR_NONE)
1128 tree clobber = build_constructor (TREE_TYPE (t),
1129 NULL);
1130 TREE_THIS_VOLATILE (clobber) = 1;
1131 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1135 if (cleanup)
1137 gimple gs;
1138 gimple_seq new_body;
1140 new_body = NULL;
1141 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1142 GIMPLE_TRY_FINALLY);
1144 if (stack_save)
1145 gimplify_seq_add_stmt (&new_body, stack_save);
1146 gimplify_seq_add_stmt (&new_body, gs);
1147 gimple_bind_set_body (gimple_bind, new_body);
1150 gimplify_ctxp->save_stack = old_save_stack;
1151 gimple_pop_bind_expr ();
1153 gimplify_seq_add_stmt (pre_p, gimple_bind);
1155 if (temp)
1157 *expr_p = temp;
1158 return GS_OK;
1161 *expr_p = NULL_TREE;
1162 return GS_ALL_DONE;
1165 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1166 GIMPLE value, it is assigned to a new temporary and the statement is
1167 re-written to return the temporary.
1169 PRE_P points to the sequence where side effects that must happen before
1170 STMT should be stored. */
1172 static enum gimplify_status
1173 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1175 gimple ret;
1176 tree ret_expr = TREE_OPERAND (stmt, 0);
1177 tree result_decl, result;
1179 if (ret_expr == error_mark_node)
1180 return GS_ERROR;
1182 /* Implicit _Cilk_sync must be inserted right before any return statement
1183 if there is a _Cilk_spawn in the function. If the user has provided a
1184 _Cilk_sync, the optimizer should remove this duplicate one. */
1185 if (fn_contains_cilk_spawn_p (cfun))
1187 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1188 gimplify_and_add (impl_sync, pre_p);
1191 if (!ret_expr
1192 || TREE_CODE (ret_expr) == RESULT_DECL
1193 || ret_expr == error_mark_node)
1195 gimple ret = gimple_build_return (ret_expr);
1196 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1197 gimplify_seq_add_stmt (pre_p, ret);
1198 return GS_ALL_DONE;
1201 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1202 result_decl = NULL_TREE;
1203 else
1205 result_decl = TREE_OPERAND (ret_expr, 0);
1207 /* See through a return by reference. */
1208 if (TREE_CODE (result_decl) == INDIRECT_REF)
1209 result_decl = TREE_OPERAND (result_decl, 0);
1211 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1212 || TREE_CODE (ret_expr) == INIT_EXPR)
1213 && TREE_CODE (result_decl) == RESULT_DECL);
1216 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1217 Recall that aggregate_value_p is FALSE for any aggregate type that is
1218 returned in registers. If we're returning values in registers, then
1219 we don't want to extend the lifetime of the RESULT_DECL, particularly
1220 across another call. In addition, for those aggregates for which
1221 hard_function_value generates a PARALLEL, we'll die during normal
1222 expansion of structure assignments; there's special code in expand_return
1223 to handle this case that does not exist in expand_expr. */
1224 if (!result_decl)
1225 result = NULL_TREE;
1226 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1228 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1230 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1231 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1232 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1233 should be effectively allocated by the caller, i.e. all calls to
1234 this function must be subject to the Return Slot Optimization. */
1235 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1236 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1238 result = result_decl;
1240 else if (gimplify_ctxp->return_temp)
1241 result = gimplify_ctxp->return_temp;
1242 else
1244 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1246 /* ??? With complex control flow (usually involving abnormal edges),
1247 we can wind up warning about an uninitialized value for this. Due
1248 to how this variable is constructed and initialized, this is never
1249 true. Give up and never warn. */
1250 TREE_NO_WARNING (result) = 1;
1252 gimplify_ctxp->return_temp = result;
1255 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1256 Then gimplify the whole thing. */
1257 if (result != result_decl)
1258 TREE_OPERAND (ret_expr, 0) = result;
1260 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1262 ret = gimple_build_return (result);
1263 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1264 gimplify_seq_add_stmt (pre_p, ret);
1266 return GS_ALL_DONE;
1269 /* Gimplify a variable-length array DECL. */
1271 static void
1272 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1274 /* This is a variable-sized decl. Simplify its size and mark it
1275 for deferred expansion. */
1276 tree t, addr, ptr_type;
1278 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1279 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1281 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1282 if (DECL_HAS_VALUE_EXPR_P (decl))
1283 return;
1285 /* All occurrences of this decl in final gimplified code will be
1286 replaced by indirection. Setting DECL_VALUE_EXPR does two
1287 things: First, it lets the rest of the gimplifier know what
1288 replacement to use. Second, it lets the debug info know
1289 where to find the value. */
1290 ptr_type = build_pointer_type (TREE_TYPE (decl));
1291 addr = create_tmp_var (ptr_type, get_name (decl));
1292 DECL_IGNORED_P (addr) = 0;
1293 t = build_fold_indirect_ref (addr);
1294 TREE_THIS_NOTRAP (t) = 1;
1295 SET_DECL_VALUE_EXPR (decl, t);
1296 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1298 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1299 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1300 size_int (DECL_ALIGN (decl)));
1301 /* The call has been built for a variable-sized object. */
1302 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1303 t = fold_convert (ptr_type, t);
1304 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1306 gimplify_and_add (t, seq_p);
1308 /* Indicate that we need to restore the stack level when the
1309 enclosing BIND_EXPR is exited. */
1310 gimplify_ctxp->save_stack = true;
1313 /* A helper function to be called via walk_tree. Mark all labels under *TP
1314 as being forced. To be called for DECL_INITIAL of static variables. */
1316 static tree
1317 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1319 if (TYPE_P (*tp))
1320 *walk_subtrees = 0;
1321 if (TREE_CODE (*tp) == LABEL_DECL)
1322 FORCED_LABEL (*tp) = 1;
1324 return NULL_TREE;
1327 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1328 and initialization explicit. */
1330 static enum gimplify_status
1331 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1333 tree stmt = *stmt_p;
1334 tree decl = DECL_EXPR_DECL (stmt);
1336 *stmt_p = NULL_TREE;
1338 if (TREE_TYPE (decl) == error_mark_node)
1339 return GS_ERROR;
1341 if ((TREE_CODE (decl) == TYPE_DECL
1342 || TREE_CODE (decl) == VAR_DECL)
1343 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1344 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1346 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1347 in case its size expressions contain problematic nodes like CALL_EXPR. */
1348 if (TREE_CODE (decl) == TYPE_DECL
1349 && DECL_ORIGINAL_TYPE (decl)
1350 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1351 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1353 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1355 tree init = DECL_INITIAL (decl);
1357 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1358 || (!TREE_STATIC (decl)
1359 && flag_stack_check == GENERIC_STACK_CHECK
1360 && compare_tree_int (DECL_SIZE_UNIT (decl),
1361 STACK_CHECK_MAX_VAR_SIZE) > 0))
1362 gimplify_vla_decl (decl, seq_p);
1364 /* Some front ends do not explicitly declare all anonymous
1365 artificial variables. We compensate here by declaring the
1366 variables, though it would be better if the front ends would
1367 explicitly declare them. */
1368 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1369 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1370 gimple_add_tmp_var (decl);
1372 if (init && init != error_mark_node)
1374 if (!TREE_STATIC (decl))
1376 DECL_INITIAL (decl) = NULL_TREE;
1377 init = build2 (INIT_EXPR, void_type_node, decl, init);
1378 gimplify_and_add (init, seq_p);
1379 ggc_free (init);
1381 else
1382 /* We must still examine initializers for static variables
1383 as they may contain a label address. */
1384 walk_tree (&init, force_labels_r, NULL, NULL);
1388 return GS_ALL_DONE;
1391 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1392 and replacing the LOOP_EXPR with goto, but if the loop contains an
1393 EXIT_EXPR, we need to append a label for it to jump to. */
1395 static enum gimplify_status
1396 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1398 tree saved_label = gimplify_ctxp->exit_label;
1399 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1401 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1403 gimplify_ctxp->exit_label = NULL_TREE;
1405 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1407 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1409 if (gimplify_ctxp->exit_label)
1410 gimplify_seq_add_stmt (pre_p,
1411 gimple_build_label (gimplify_ctxp->exit_label));
1413 gimplify_ctxp->exit_label = saved_label;
1415 *expr_p = NULL;
1416 return GS_ALL_DONE;
1419 /* Gimplify a statement list onto a sequence. These may be created either
1420 by an enlightened front-end, or by shortcut_cond_expr. */
1422 static enum gimplify_status
1423 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1425 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1427 tree_stmt_iterator i = tsi_start (*expr_p);
1429 while (!tsi_end_p (i))
1431 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1432 tsi_delink (&i);
1435 if (temp)
1437 *expr_p = temp;
1438 return GS_OK;
1441 return GS_ALL_DONE;
1445 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1446 branch to. */
1448 static enum gimplify_status
1449 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1451 tree switch_expr = *expr_p;
1452 gimple_seq switch_body_seq = NULL;
1453 enum gimplify_status ret;
1454 tree index_type = TREE_TYPE (switch_expr);
1455 if (index_type == NULL_TREE)
1456 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1458 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1459 fb_rvalue);
1460 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1461 return ret;
1463 if (SWITCH_BODY (switch_expr))
1465 vec<tree> labels;
1466 vec<tree> saved_labels;
1467 tree default_case = NULL_TREE;
1468 gimple gimple_switch;
1470 /* If someone can be bothered to fill in the labels, they can
1471 be bothered to null out the body too. */
1472 gcc_assert (!SWITCH_LABELS (switch_expr));
1474 /* Save old labels, get new ones from body, then restore the old
1475 labels. Save all the things from the switch body to append after. */
1476 saved_labels = gimplify_ctxp->case_labels;
1477 gimplify_ctxp->case_labels.create (8);
1479 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1480 labels = gimplify_ctxp->case_labels;
1481 gimplify_ctxp->case_labels = saved_labels;
1483 preprocess_case_label_vec_for_gimple (labels, index_type,
1484 &default_case);
1486 if (!default_case)
1488 gimple new_default;
1490 default_case
1491 = build_case_label (NULL_TREE, NULL_TREE,
1492 create_artificial_label (UNKNOWN_LOCATION));
1493 new_default = gimple_build_label (CASE_LABEL (default_case));
1494 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1497 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1498 default_case, labels);
1499 gimplify_seq_add_stmt (pre_p, gimple_switch);
1500 gimplify_seq_add_seq (pre_p, switch_body_seq);
1501 labels.release ();
1503 else
1504 gcc_assert (SWITCH_LABELS (switch_expr));
1506 return GS_ALL_DONE;
1509 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1511 static enum gimplify_status
1512 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1514 struct gimplify_ctx *ctxp;
1515 gimple gimple_label;
1517 /* Invalid OpenMP programs can play Duff's Device type games with
1518 #pragma omp parallel. At least in the C front end, we don't
1519 detect such invalid branches until after gimplification. */
1520 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1521 if (ctxp->case_labels.exists ())
1522 break;
1524 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1525 ctxp->case_labels.safe_push (*expr_p);
1526 gimplify_seq_add_stmt (pre_p, gimple_label);
1528 return GS_ALL_DONE;
1531 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1532 if necessary. */
1534 tree
1535 build_and_jump (tree *label_p)
1537 if (label_p == NULL)
1538 /* If there's nowhere to jump, just fall through. */
1539 return NULL_TREE;
1541 if (*label_p == NULL_TREE)
1543 tree label = create_artificial_label (UNKNOWN_LOCATION);
1544 *label_p = label;
1547 return build1 (GOTO_EXPR, void_type_node, *label_p);
1550 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1551 This also involves building a label to jump to and communicating it to
1552 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1554 static enum gimplify_status
1555 gimplify_exit_expr (tree *expr_p)
1557 tree cond = TREE_OPERAND (*expr_p, 0);
1558 tree expr;
1560 expr = build_and_jump (&gimplify_ctxp->exit_label);
1561 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1562 *expr_p = expr;
1564 return GS_OK;
1567 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1568 different from its canonical type, wrap the whole thing inside a
1569 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1570 type.
1572 The canonical type of a COMPONENT_REF is the type of the field being
1573 referenced--unless the field is a bit-field which can be read directly
1574 in a smaller mode, in which case the canonical type is the
1575 sign-appropriate type corresponding to that mode. */
1577 static void
1578 canonicalize_component_ref (tree *expr_p)
1580 tree expr = *expr_p;
1581 tree type;
1583 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1585 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1586 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1587 else
1588 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1590 /* One could argue that all the stuff below is not necessary for
1591 the non-bitfield case and declare it a FE error if type
1592 adjustment would be needed. */
1593 if (TREE_TYPE (expr) != type)
1595 #ifdef ENABLE_TYPES_CHECKING
1596 tree old_type = TREE_TYPE (expr);
1597 #endif
1598 int type_quals;
1600 /* We need to preserve qualifiers and propagate them from
1601 operand 0. */
1602 type_quals = TYPE_QUALS (type)
1603 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1604 if (TYPE_QUALS (type) != type_quals)
1605 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1607 /* Set the type of the COMPONENT_REF to the underlying type. */
1608 TREE_TYPE (expr) = type;
1610 #ifdef ENABLE_TYPES_CHECKING
1611 /* It is now a FE error, if the conversion from the canonical
1612 type to the original expression type is not useless. */
1613 gcc_assert (useless_type_conversion_p (old_type, type));
1614 #endif
1618 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1619 to foo, embed that change in the ADDR_EXPR by converting
1620 T array[U];
1621 (T *)&array
1623 &array[L]
1624 where L is the lower bound. For simplicity, only do this for constant
1625 lower bound.
1626 The constraint is that the type of &array[L] is trivially convertible
1627 to T *. */
1629 static void
1630 canonicalize_addr_expr (tree *expr_p)
1632 tree expr = *expr_p;
1633 tree addr_expr = TREE_OPERAND (expr, 0);
1634 tree datype, ddatype, pddatype;
1636 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1637 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1638 || TREE_CODE (addr_expr) != ADDR_EXPR)
1639 return;
1641 /* The addr_expr type should be a pointer to an array. */
1642 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1643 if (TREE_CODE (datype) != ARRAY_TYPE)
1644 return;
1646 /* The pointer to element type shall be trivially convertible to
1647 the expression pointer type. */
1648 ddatype = TREE_TYPE (datype);
1649 pddatype = build_pointer_type (ddatype);
1650 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1651 pddatype))
1652 return;
1654 /* The lower bound and element sizes must be constant. */
1655 if (!TYPE_SIZE_UNIT (ddatype)
1656 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1657 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1658 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1659 return;
1661 /* All checks succeeded. Build a new node to merge the cast. */
1662 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1663 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1664 NULL_TREE, NULL_TREE);
1665 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1667 /* We can have stripped a required restrict qualifier above. */
1668 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1669 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1672 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1673 underneath as appropriate. */
1675 static enum gimplify_status
1676 gimplify_conversion (tree *expr_p)
1678 location_t loc = EXPR_LOCATION (*expr_p);
1679 gcc_assert (CONVERT_EXPR_P (*expr_p));
1681 /* Then strip away all but the outermost conversion. */
1682 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1684 /* And remove the outermost conversion if it's useless. */
1685 if (tree_ssa_useless_type_conversion (*expr_p))
1686 *expr_p = TREE_OPERAND (*expr_p, 0);
1688 /* If we still have a conversion at the toplevel,
1689 then canonicalize some constructs. */
1690 if (CONVERT_EXPR_P (*expr_p))
1692 tree sub = TREE_OPERAND (*expr_p, 0);
1694 /* If a NOP conversion is changing the type of a COMPONENT_REF
1695 expression, then canonicalize its type now in order to expose more
1696 redundant conversions. */
1697 if (TREE_CODE (sub) == COMPONENT_REF)
1698 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1700 /* If a NOP conversion is changing a pointer to array of foo
1701 to a pointer to foo, embed that change in the ADDR_EXPR. */
1702 else if (TREE_CODE (sub) == ADDR_EXPR)
1703 canonicalize_addr_expr (expr_p);
1706 /* If we have a conversion to a non-register type force the
1707 use of a VIEW_CONVERT_EXPR instead. */
1708 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1709 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1710 TREE_OPERAND (*expr_p, 0));
1712 return GS_OK;
1715 /* Nonlocal VLAs seen in the current function. */
1716 static struct pointer_set_t *nonlocal_vlas;
1718 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1719 static tree nonlocal_vla_vars;
1721 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1722 DECL_VALUE_EXPR, and it's worth re-examining things. */
1724 static enum gimplify_status
1725 gimplify_var_or_parm_decl (tree *expr_p)
1727 tree decl = *expr_p;
1729 /* ??? If this is a local variable, and it has not been seen in any
1730 outer BIND_EXPR, then it's probably the result of a duplicate
1731 declaration, for which we've already issued an error. It would
1732 be really nice if the front end wouldn't leak these at all.
1733 Currently the only known culprit is C++ destructors, as seen
1734 in g++.old-deja/g++.jason/binding.C. */
1735 if (TREE_CODE (decl) == VAR_DECL
1736 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1737 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1738 && decl_function_context (decl) == current_function_decl)
1740 gcc_assert (seen_error ());
1741 return GS_ERROR;
1744 /* When within an OpenMP context, notice uses of variables. */
1745 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1746 return GS_ALL_DONE;
1748 /* If the decl is an alias for another expression, substitute it now. */
1749 if (DECL_HAS_VALUE_EXPR_P (decl))
1751 tree value_expr = DECL_VALUE_EXPR (decl);
1753 /* For referenced nonlocal VLAs add a decl for debugging purposes
1754 to the current function. */
1755 if (TREE_CODE (decl) == VAR_DECL
1756 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1757 && nonlocal_vlas != NULL
1758 && TREE_CODE (value_expr) == INDIRECT_REF
1759 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1760 && decl_function_context (decl) != current_function_decl)
1762 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1763 while (ctx
1764 && (ctx->region_type == ORT_WORKSHARE
1765 || ctx->region_type == ORT_SIMD))
1766 ctx = ctx->outer_context;
1767 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
1769 tree copy = copy_node (decl);
1771 lang_hooks.dup_lang_specific_decl (copy);
1772 SET_DECL_RTL (copy, 0);
1773 TREE_USED (copy) = 1;
1774 DECL_CHAIN (copy) = nonlocal_vla_vars;
1775 nonlocal_vla_vars = copy;
1776 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1777 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1781 *expr_p = unshare_expr (value_expr);
1782 return GS_OK;
1785 return GS_ALL_DONE;
1788 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1790 static void
1791 recalculate_side_effects (tree t)
1793 enum tree_code code = TREE_CODE (t);
1794 int len = TREE_OPERAND_LENGTH (t);
1795 int i;
1797 switch (TREE_CODE_CLASS (code))
1799 case tcc_expression:
1800 switch (code)
1802 case INIT_EXPR:
1803 case MODIFY_EXPR:
1804 case VA_ARG_EXPR:
1805 case PREDECREMENT_EXPR:
1806 case PREINCREMENT_EXPR:
1807 case POSTDECREMENT_EXPR:
1808 case POSTINCREMENT_EXPR:
1809 /* All of these have side-effects, no matter what their
1810 operands are. */
1811 return;
1813 default:
1814 break;
1816 /* Fall through. */
1818 case tcc_comparison: /* a comparison expression */
1819 case tcc_unary: /* a unary arithmetic expression */
1820 case tcc_binary: /* a binary arithmetic expression */
1821 case tcc_reference: /* a reference */
1822 case tcc_vl_exp: /* a function call */
1823 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1824 for (i = 0; i < len; ++i)
1826 tree op = TREE_OPERAND (t, i);
1827 if (op && TREE_SIDE_EFFECTS (op))
1828 TREE_SIDE_EFFECTS (t) = 1;
1830 break;
1832 case tcc_constant:
1833 /* No side-effects. */
1834 return;
1836 default:
1837 gcc_unreachable ();
1841 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1842 node *EXPR_P.
1844 compound_lval
1845 : min_lval '[' val ']'
1846 | min_lval '.' ID
1847 | compound_lval '[' val ']'
1848 | compound_lval '.' ID
1850 This is not part of the original SIMPLE definition, which separates
1851 array and member references, but it seems reasonable to handle them
1852 together. Also, this way we don't run into problems with union
1853 aliasing; gcc requires that for accesses through a union to alias, the
1854 union reference must be explicit, which was not always the case when we
1855 were splitting up array and member refs.
1857 PRE_P points to the sequence where side effects that must happen before
1858 *EXPR_P should be stored.
1860 POST_P points to the sequence where side effects that must happen after
1861 *EXPR_P should be stored. */
1863 static enum gimplify_status
1864 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1865 fallback_t fallback)
1867 tree *p;
1868 enum gimplify_status ret = GS_ALL_DONE, tret;
1869 int i;
1870 location_t loc = EXPR_LOCATION (*expr_p);
1871 tree expr = *expr_p;
1873 /* Create a stack of the subexpressions so later we can walk them in
1874 order from inner to outer. */
1875 auto_vec<tree, 10> expr_stack;
1877 /* We can handle anything that get_inner_reference can deal with. */
1878 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1880 restart:
1881 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1882 if (TREE_CODE (*p) == INDIRECT_REF)
1883 *p = fold_indirect_ref_loc (loc, *p);
1885 if (handled_component_p (*p))
1887 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1888 additional COMPONENT_REFs. */
1889 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1890 && gimplify_var_or_parm_decl (p) == GS_OK)
1891 goto restart;
1892 else
1893 break;
1895 expr_stack.safe_push (*p);
1898 gcc_assert (expr_stack.length ());
1900 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1901 walked through and P points to the innermost expression.
1903 Java requires that we elaborated nodes in source order. That
1904 means we must gimplify the inner expression followed by each of
1905 the indices, in order. But we can't gimplify the inner
1906 expression until we deal with any variable bounds, sizes, or
1907 positions in order to deal with PLACEHOLDER_EXPRs.
1909 So we do this in three steps. First we deal with the annotations
1910 for any variables in the components, then we gimplify the base,
1911 then we gimplify any indices, from left to right. */
1912 for (i = expr_stack.length () - 1; i >= 0; i--)
1914 tree t = expr_stack[i];
1916 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1918 /* Gimplify the low bound and element type size and put them into
1919 the ARRAY_REF. If these values are set, they have already been
1920 gimplified. */
1921 if (TREE_OPERAND (t, 2) == NULL_TREE)
1923 tree low = unshare_expr (array_ref_low_bound (t));
1924 if (!is_gimple_min_invariant (low))
1926 TREE_OPERAND (t, 2) = low;
1927 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1928 post_p, is_gimple_reg,
1929 fb_rvalue);
1930 ret = MIN (ret, tret);
1933 else
1935 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1936 is_gimple_reg, fb_rvalue);
1937 ret = MIN (ret, tret);
1940 if (TREE_OPERAND (t, 3) == NULL_TREE)
1942 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1943 tree elmt_size = unshare_expr (array_ref_element_size (t));
1944 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1946 /* Divide the element size by the alignment of the element
1947 type (above). */
1948 elmt_size
1949 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1951 if (!is_gimple_min_invariant (elmt_size))
1953 TREE_OPERAND (t, 3) = elmt_size;
1954 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1955 post_p, is_gimple_reg,
1956 fb_rvalue);
1957 ret = MIN (ret, tret);
1960 else
1962 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1963 is_gimple_reg, fb_rvalue);
1964 ret = MIN (ret, tret);
1967 else if (TREE_CODE (t) == COMPONENT_REF)
1969 /* Set the field offset into T and gimplify it. */
1970 if (TREE_OPERAND (t, 2) == NULL_TREE)
1972 tree offset = unshare_expr (component_ref_field_offset (t));
1973 tree field = TREE_OPERAND (t, 1);
1974 tree factor
1975 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1977 /* Divide the offset by its alignment. */
1978 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
1980 if (!is_gimple_min_invariant (offset))
1982 TREE_OPERAND (t, 2) = offset;
1983 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1984 post_p, is_gimple_reg,
1985 fb_rvalue);
1986 ret = MIN (ret, tret);
1989 else
1991 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1992 is_gimple_reg, fb_rvalue);
1993 ret = MIN (ret, tret);
1998 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
1999 so as to match the min_lval predicate. Failure to do so may result
2000 in the creation of large aggregate temporaries. */
2001 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2002 fallback | fb_lvalue);
2003 ret = MIN (ret, tret);
2005 /* And finally, the indices and operands of ARRAY_REF. During this
2006 loop we also remove any useless conversions. */
2007 for (; expr_stack.length () > 0; )
2009 tree t = expr_stack.pop ();
2011 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2013 /* Gimplify the dimension. */
2014 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2016 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2017 is_gimple_val, fb_rvalue);
2018 ret = MIN (ret, tret);
2022 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2024 /* The innermost expression P may have originally had
2025 TREE_SIDE_EFFECTS set which would have caused all the outer
2026 expressions in *EXPR_P leading to P to also have had
2027 TREE_SIDE_EFFECTS set. */
2028 recalculate_side_effects (t);
2031 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2032 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2034 canonicalize_component_ref (expr_p);
2037 expr_stack.release ();
2039 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2041 return ret;
2044 /* Gimplify the self modifying expression pointed to by EXPR_P
2045 (++, --, +=, -=).
2047 PRE_P points to the list where side effects that must happen before
2048 *EXPR_P should be stored.
2050 POST_P points to the list where side effects that must happen after
2051 *EXPR_P should be stored.
2053 WANT_VALUE is nonzero iff we want to use the value of this expression
2054 in another expression.
2056 ARITH_TYPE is the type the computation should be performed in. */
2058 enum gimplify_status
2059 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2060 bool want_value, tree arith_type)
2062 enum tree_code code;
2063 tree lhs, lvalue, rhs, t1;
2064 gimple_seq post = NULL, *orig_post_p = post_p;
2065 bool postfix;
2066 enum tree_code arith_code;
2067 enum gimplify_status ret;
2068 location_t loc = EXPR_LOCATION (*expr_p);
2070 code = TREE_CODE (*expr_p);
2072 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2073 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2075 /* Prefix or postfix? */
2076 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2077 /* Faster to treat as prefix if result is not used. */
2078 postfix = want_value;
2079 else
2080 postfix = false;
2082 /* For postfix, make sure the inner expression's post side effects
2083 are executed after side effects from this expression. */
2084 if (postfix)
2085 post_p = &post;
2087 /* Add or subtract? */
2088 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2089 arith_code = PLUS_EXPR;
2090 else
2091 arith_code = MINUS_EXPR;
2093 /* Gimplify the LHS into a GIMPLE lvalue. */
2094 lvalue = TREE_OPERAND (*expr_p, 0);
2095 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2096 if (ret == GS_ERROR)
2097 return ret;
2099 /* Extract the operands to the arithmetic operation. */
2100 lhs = lvalue;
2101 rhs = TREE_OPERAND (*expr_p, 1);
2103 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2104 that as the result value and in the postqueue operation. */
2105 if (postfix)
2107 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2108 if (ret == GS_ERROR)
2109 return ret;
2111 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2114 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2115 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2117 rhs = convert_to_ptrofftype_loc (loc, rhs);
2118 if (arith_code == MINUS_EXPR)
2119 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2120 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2122 else
2123 t1 = fold_convert (TREE_TYPE (*expr_p),
2124 fold_build2 (arith_code, arith_type,
2125 fold_convert (arith_type, lhs),
2126 fold_convert (arith_type, rhs)));
2128 if (postfix)
2130 gimplify_assign (lvalue, t1, pre_p);
2131 gimplify_seq_add_seq (orig_post_p, post);
2132 *expr_p = lhs;
2133 return GS_ALL_DONE;
2135 else
2137 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2138 return GS_OK;
2142 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2144 static void
2145 maybe_with_size_expr (tree *expr_p)
2147 tree expr = *expr_p;
2148 tree type = TREE_TYPE (expr);
2149 tree size;
2151 /* If we've already wrapped this or the type is error_mark_node, we can't do
2152 anything. */
2153 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2154 || type == error_mark_node)
2155 return;
2157 /* If the size isn't known or is a constant, we have nothing to do. */
2158 size = TYPE_SIZE_UNIT (type);
2159 if (!size || TREE_CODE (size) == INTEGER_CST)
2160 return;
2162 /* Otherwise, make a WITH_SIZE_EXPR. */
2163 size = unshare_expr (size);
2164 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2165 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2168 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2169 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2170 the CALL_EXPR. */
2172 static enum gimplify_status
2173 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2175 bool (*test) (tree);
2176 fallback_t fb;
2178 /* In general, we allow lvalues for function arguments to avoid
2179 extra overhead of copying large aggregates out of even larger
2180 aggregates into temporaries only to copy the temporaries to
2181 the argument list. Make optimizers happy by pulling out to
2182 temporaries those types that fit in registers. */
2183 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2184 test = is_gimple_val, fb = fb_rvalue;
2185 else
2187 test = is_gimple_lvalue, fb = fb_either;
2188 /* Also strip a TARGET_EXPR that would force an extra copy. */
2189 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2191 tree init = TARGET_EXPR_INITIAL (*arg_p);
2192 if (init
2193 && !VOID_TYPE_P (TREE_TYPE (init)))
2194 *arg_p = init;
2198 /* If this is a variable sized type, we must remember the size. */
2199 maybe_with_size_expr (arg_p);
2201 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2202 /* Make sure arguments have the same location as the function call
2203 itself. */
2204 protected_set_expr_location (*arg_p, call_location);
2206 /* There is a sequence point before a function call. Side effects in
2207 the argument list must occur before the actual call. So, when
2208 gimplifying arguments, force gimplify_expr to use an internal
2209 post queue which is then appended to the end of PRE_P. */
2210 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2213 /* Don't fold STMT inside ORT_TARGET, because it can break code by adding decl
2214 references that weren't in the source. We'll do it during omplower pass
2215 instead. */
2217 static bool
2218 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2220 struct gimplify_omp_ctx *ctx;
2221 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2222 if (ctx->region_type == ORT_TARGET)
2223 return false;
2224 return fold_stmt (gsi);
2227 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2228 WANT_VALUE is true if the result of the call is desired. */
2230 static enum gimplify_status
2231 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2233 tree fndecl, parms, p, fnptrtype;
2234 enum gimplify_status ret;
2235 int i, nargs;
2236 gimple call;
2237 bool builtin_va_start_p = false;
2238 location_t loc = EXPR_LOCATION (*expr_p);
2240 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2242 /* For reliable diagnostics during inlining, it is necessary that
2243 every call_expr be annotated with file and line. */
2244 if (! EXPR_HAS_LOCATION (*expr_p))
2245 SET_EXPR_LOCATION (*expr_p, input_location);
2247 /* This may be a call to a builtin function.
2249 Builtin function calls may be transformed into different
2250 (and more efficient) builtin function calls under certain
2251 circumstances. Unfortunately, gimplification can muck things
2252 up enough that the builtin expanders are not aware that certain
2253 transformations are still valid.
2255 So we attempt transformation/gimplification of the call before
2256 we gimplify the CALL_EXPR. At this time we do not manage to
2257 transform all calls in the same manner as the expanders do, but
2258 we do transform most of them. */
2259 fndecl = get_callee_fndecl (*expr_p);
2260 if (fndecl
2261 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2262 switch (DECL_FUNCTION_CODE (fndecl))
2264 case BUILT_IN_VA_START:
2266 builtin_va_start_p = TRUE;
2267 if (call_expr_nargs (*expr_p) < 2)
2269 error ("too few arguments to function %<va_start%>");
2270 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2271 return GS_OK;
2274 if (fold_builtin_next_arg (*expr_p, true))
2276 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2277 return GS_OK;
2279 break;
2281 case BUILT_IN_LINE:
2283 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2284 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2285 return GS_OK;
2287 case BUILT_IN_FILE:
2289 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2290 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2291 return GS_OK;
2293 case BUILT_IN_FUNCTION:
2295 const char *function;
2296 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2297 *expr_p = build_string_literal (strlen (function) + 1, function);
2298 return GS_OK;
2300 default:
2303 if (fndecl && DECL_BUILT_IN (fndecl))
2305 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2306 if (new_tree && new_tree != *expr_p)
2308 /* There was a transformation of this call which computes the
2309 same value, but in a more efficient way. Return and try
2310 again. */
2311 *expr_p = new_tree;
2312 return GS_OK;
2316 /* Remember the original function pointer type. */
2317 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2319 /* There is a sequence point before the call, so any side effects in
2320 the calling expression must occur before the actual call. Force
2321 gimplify_expr to use an internal post queue. */
2322 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2323 is_gimple_call_addr, fb_rvalue);
2325 nargs = call_expr_nargs (*expr_p);
2327 /* Get argument types for verification. */
2328 fndecl = get_callee_fndecl (*expr_p);
2329 parms = NULL_TREE;
2330 if (fndecl)
2331 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2332 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2333 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2335 if (fndecl && DECL_ARGUMENTS (fndecl))
2336 p = DECL_ARGUMENTS (fndecl);
2337 else if (parms)
2338 p = parms;
2339 else
2340 p = NULL_TREE;
2341 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2344 /* If the last argument is __builtin_va_arg_pack () and it is not
2345 passed as a named argument, decrease the number of CALL_EXPR
2346 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2347 if (!p
2348 && i < nargs
2349 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2351 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2352 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2354 if (last_arg_fndecl
2355 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2356 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2357 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2359 tree call = *expr_p;
2361 --nargs;
2362 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2363 CALL_EXPR_FN (call),
2364 nargs, CALL_EXPR_ARGP (call));
2366 /* Copy all CALL_EXPR flags, location and block, except
2367 CALL_EXPR_VA_ARG_PACK flag. */
2368 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2369 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2370 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2371 = CALL_EXPR_RETURN_SLOT_OPT (call);
2372 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2373 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2375 /* Set CALL_EXPR_VA_ARG_PACK. */
2376 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2380 /* Finally, gimplify the function arguments. */
2381 if (nargs > 0)
2383 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2384 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2385 PUSH_ARGS_REVERSED ? i-- : i++)
2387 enum gimplify_status t;
2389 /* Avoid gimplifying the second argument to va_start, which needs to
2390 be the plain PARM_DECL. */
2391 if ((i != 1) || !builtin_va_start_p)
2393 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2394 EXPR_LOCATION (*expr_p));
2396 if (t == GS_ERROR)
2397 ret = GS_ERROR;
2402 /* Verify the function result. */
2403 if (want_value && fndecl
2404 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2406 error_at (loc, "using result of function returning %<void%>");
2407 ret = GS_ERROR;
2410 /* Try this again in case gimplification exposed something. */
2411 if (ret != GS_ERROR)
2413 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2415 if (new_tree && new_tree != *expr_p)
2417 /* There was a transformation of this call which computes the
2418 same value, but in a more efficient way. Return and try
2419 again. */
2420 *expr_p = new_tree;
2421 return GS_OK;
2424 else
2426 *expr_p = error_mark_node;
2427 return GS_ERROR;
2430 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2431 decl. This allows us to eliminate redundant or useless
2432 calls to "const" functions. */
2433 if (TREE_CODE (*expr_p) == CALL_EXPR)
2435 int flags = call_expr_flags (*expr_p);
2436 if (flags & (ECF_CONST | ECF_PURE)
2437 /* An infinite loop is considered a side effect. */
2438 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2439 TREE_SIDE_EFFECTS (*expr_p) = 0;
2442 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2443 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2444 form and delegate the creation of a GIMPLE_CALL to
2445 gimplify_modify_expr. This is always possible because when
2446 WANT_VALUE is true, the caller wants the result of this call into
2447 a temporary, which means that we will emit an INIT_EXPR in
2448 internal_get_tmp_var which will then be handled by
2449 gimplify_modify_expr. */
2450 if (!want_value)
2452 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2453 have to do is replicate it as a GIMPLE_CALL tuple. */
2454 gimple_stmt_iterator gsi;
2455 call = gimple_build_call_from_tree (*expr_p);
2456 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2457 notice_special_calls (call);
2458 gimplify_seq_add_stmt (pre_p, call);
2459 gsi = gsi_last (*pre_p);
2460 maybe_fold_stmt (&gsi);
2461 *expr_p = NULL_TREE;
2463 else
2464 /* Remember the original function type. */
2465 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2466 CALL_EXPR_FN (*expr_p));
2468 return ret;
2471 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2472 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2474 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2475 condition is true or false, respectively. If null, we should generate
2476 our own to skip over the evaluation of this specific expression.
2478 LOCUS is the source location of the COND_EXPR.
2480 This function is the tree equivalent of do_jump.
2482 shortcut_cond_r should only be called by shortcut_cond_expr. */
2484 static tree
2485 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2486 location_t locus)
2488 tree local_label = NULL_TREE;
2489 tree t, expr = NULL;
2491 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2492 retain the shortcut semantics. Just insert the gotos here;
2493 shortcut_cond_expr will append the real blocks later. */
2494 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2496 location_t new_locus;
2498 /* Turn if (a && b) into
2500 if (a); else goto no;
2501 if (b) goto yes; else goto no;
2502 (no:) */
2504 if (false_label_p == NULL)
2505 false_label_p = &local_label;
2507 /* Keep the original source location on the first 'if'. */
2508 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2509 append_to_statement_list (t, &expr);
2511 /* Set the source location of the && on the second 'if'. */
2512 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2513 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2514 new_locus);
2515 append_to_statement_list (t, &expr);
2517 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2519 location_t new_locus;
2521 /* Turn if (a || b) into
2523 if (a) goto yes;
2524 if (b) goto yes; else goto no;
2525 (yes:) */
2527 if (true_label_p == NULL)
2528 true_label_p = &local_label;
2530 /* Keep the original source location on the first 'if'. */
2531 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2532 append_to_statement_list (t, &expr);
2534 /* Set the source location of the || on the second 'if'. */
2535 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2536 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2537 new_locus);
2538 append_to_statement_list (t, &expr);
2540 else if (TREE_CODE (pred) == COND_EXPR
2541 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2542 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2544 location_t new_locus;
2546 /* As long as we're messing with gotos, turn if (a ? b : c) into
2547 if (a)
2548 if (b) goto yes; else goto no;
2549 else
2550 if (c) goto yes; else goto no;
2552 Don't do this if one of the arms has void type, which can happen
2553 in C++ when the arm is throw. */
2555 /* Keep the original source location on the first 'if'. Set the source
2556 location of the ? on the second 'if'. */
2557 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2558 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2559 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2560 false_label_p, locus),
2561 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2562 false_label_p, new_locus));
2564 else
2566 expr = build3 (COND_EXPR, void_type_node, pred,
2567 build_and_jump (true_label_p),
2568 build_and_jump (false_label_p));
2569 SET_EXPR_LOCATION (expr, locus);
2572 if (local_label)
2574 t = build1 (LABEL_EXPR, void_type_node, local_label);
2575 append_to_statement_list (t, &expr);
2578 return expr;
2581 /* Given a conditional expression EXPR with short-circuit boolean
2582 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2583 predicate apart into the equivalent sequence of conditionals. */
2585 static tree
2586 shortcut_cond_expr (tree expr)
2588 tree pred = TREE_OPERAND (expr, 0);
2589 tree then_ = TREE_OPERAND (expr, 1);
2590 tree else_ = TREE_OPERAND (expr, 2);
2591 tree true_label, false_label, end_label, t;
2592 tree *true_label_p;
2593 tree *false_label_p;
2594 bool emit_end, emit_false, jump_over_else;
2595 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2596 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2598 /* First do simple transformations. */
2599 if (!else_se)
2601 /* If there is no 'else', turn
2602 if (a && b) then c
2603 into
2604 if (a) if (b) then c. */
2605 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2607 /* Keep the original source location on the first 'if'. */
2608 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2609 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2610 /* Set the source location of the && on the second 'if'. */
2611 if (EXPR_HAS_LOCATION (pred))
2612 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2613 then_ = shortcut_cond_expr (expr);
2614 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2615 pred = TREE_OPERAND (pred, 0);
2616 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2617 SET_EXPR_LOCATION (expr, locus);
2621 if (!then_se)
2623 /* If there is no 'then', turn
2624 if (a || b); else d
2625 into
2626 if (a); else if (b); else d. */
2627 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2629 /* Keep the original source location on the first 'if'. */
2630 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2631 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2632 /* Set the source location of the || on the second 'if'. */
2633 if (EXPR_HAS_LOCATION (pred))
2634 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2635 else_ = shortcut_cond_expr (expr);
2636 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2637 pred = TREE_OPERAND (pred, 0);
2638 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2639 SET_EXPR_LOCATION (expr, locus);
2643 /* If we're done, great. */
2644 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2645 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2646 return expr;
2648 /* Otherwise we need to mess with gotos. Change
2649 if (a) c; else d;
2651 if (a); else goto no;
2652 c; goto end;
2653 no: d; end:
2654 and recursively gimplify the condition. */
2656 true_label = false_label = end_label = NULL_TREE;
2658 /* If our arms just jump somewhere, hijack those labels so we don't
2659 generate jumps to jumps. */
2661 if (then_
2662 && TREE_CODE (then_) == GOTO_EXPR
2663 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2665 true_label = GOTO_DESTINATION (then_);
2666 then_ = NULL;
2667 then_se = false;
2670 if (else_
2671 && TREE_CODE (else_) == GOTO_EXPR
2672 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2674 false_label = GOTO_DESTINATION (else_);
2675 else_ = NULL;
2676 else_se = false;
2679 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2680 if (true_label)
2681 true_label_p = &true_label;
2682 else
2683 true_label_p = NULL;
2685 /* The 'else' branch also needs a label if it contains interesting code. */
2686 if (false_label || else_se)
2687 false_label_p = &false_label;
2688 else
2689 false_label_p = NULL;
2691 /* If there was nothing else in our arms, just forward the label(s). */
2692 if (!then_se && !else_se)
2693 return shortcut_cond_r (pred, true_label_p, false_label_p,
2694 EXPR_LOC_OR_LOC (expr, input_location));
2696 /* If our last subexpression already has a terminal label, reuse it. */
2697 if (else_se)
2698 t = expr_last (else_);
2699 else if (then_se)
2700 t = expr_last (then_);
2701 else
2702 t = NULL;
2703 if (t && TREE_CODE (t) == LABEL_EXPR)
2704 end_label = LABEL_EXPR_LABEL (t);
2706 /* If we don't care about jumping to the 'else' branch, jump to the end
2707 if the condition is false. */
2708 if (!false_label_p)
2709 false_label_p = &end_label;
2711 /* We only want to emit these labels if we aren't hijacking them. */
2712 emit_end = (end_label == NULL_TREE);
2713 emit_false = (false_label == NULL_TREE);
2715 /* We only emit the jump over the else clause if we have to--if the
2716 then clause may fall through. Otherwise we can wind up with a
2717 useless jump and a useless label at the end of gimplified code,
2718 which will cause us to think that this conditional as a whole
2719 falls through even if it doesn't. If we then inline a function
2720 which ends with such a condition, that can cause us to issue an
2721 inappropriate warning about control reaching the end of a
2722 non-void function. */
2723 jump_over_else = block_may_fallthru (then_);
2725 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2726 EXPR_LOC_OR_LOC (expr, input_location));
2728 expr = NULL;
2729 append_to_statement_list (pred, &expr);
2731 append_to_statement_list (then_, &expr);
2732 if (else_se)
2734 if (jump_over_else)
2736 tree last = expr_last (expr);
2737 t = build_and_jump (&end_label);
2738 if (EXPR_HAS_LOCATION (last))
2739 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2740 append_to_statement_list (t, &expr);
2742 if (emit_false)
2744 t = build1 (LABEL_EXPR, void_type_node, false_label);
2745 append_to_statement_list (t, &expr);
2747 append_to_statement_list (else_, &expr);
2749 if (emit_end && end_label)
2751 t = build1 (LABEL_EXPR, void_type_node, end_label);
2752 append_to_statement_list (t, &expr);
2755 return expr;
2758 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2760 tree
2761 gimple_boolify (tree expr)
2763 tree type = TREE_TYPE (expr);
2764 location_t loc = EXPR_LOCATION (expr);
2766 if (TREE_CODE (expr) == NE_EXPR
2767 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2768 && integer_zerop (TREE_OPERAND (expr, 1)))
2770 tree call = TREE_OPERAND (expr, 0);
2771 tree fn = get_callee_fndecl (call);
2773 /* For __builtin_expect ((long) (x), y) recurse into x as well
2774 if x is truth_value_p. */
2775 if (fn
2776 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2777 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2778 && call_expr_nargs (call) == 2)
2780 tree arg = CALL_EXPR_ARG (call, 0);
2781 if (arg)
2783 if (TREE_CODE (arg) == NOP_EXPR
2784 && TREE_TYPE (arg) == TREE_TYPE (call))
2785 arg = TREE_OPERAND (arg, 0);
2786 if (truth_value_p (TREE_CODE (arg)))
2788 arg = gimple_boolify (arg);
2789 CALL_EXPR_ARG (call, 0)
2790 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2796 switch (TREE_CODE (expr))
2798 case TRUTH_AND_EXPR:
2799 case TRUTH_OR_EXPR:
2800 case TRUTH_XOR_EXPR:
2801 case TRUTH_ANDIF_EXPR:
2802 case TRUTH_ORIF_EXPR:
2803 /* Also boolify the arguments of truth exprs. */
2804 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2805 /* FALLTHRU */
2807 case TRUTH_NOT_EXPR:
2808 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2810 /* These expressions always produce boolean results. */
2811 if (TREE_CODE (type) != BOOLEAN_TYPE)
2812 TREE_TYPE (expr) = boolean_type_node;
2813 return expr;
2815 case ANNOTATE_EXPR:
2816 if ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1))
2817 == annot_expr_ivdep_kind)
2819 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2820 if (TREE_CODE (type) != BOOLEAN_TYPE)
2821 TREE_TYPE (expr) = boolean_type_node;
2822 return expr;
2824 /* FALLTHRU */
2826 default:
2827 if (COMPARISON_CLASS_P (expr))
2829 /* There expressions always prduce boolean results. */
2830 if (TREE_CODE (type) != BOOLEAN_TYPE)
2831 TREE_TYPE (expr) = boolean_type_node;
2832 return expr;
2834 /* Other expressions that get here must have boolean values, but
2835 might need to be converted to the appropriate mode. */
2836 if (TREE_CODE (type) == BOOLEAN_TYPE)
2837 return expr;
2838 return fold_convert_loc (loc, boolean_type_node, expr);
2842 /* Given a conditional expression *EXPR_P without side effects, gimplify
2843 its operands. New statements are inserted to PRE_P. */
2845 static enum gimplify_status
2846 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2848 tree expr = *expr_p, cond;
2849 enum gimplify_status ret, tret;
2850 enum tree_code code;
2852 cond = gimple_boolify (COND_EXPR_COND (expr));
2854 /* We need to handle && and || specially, as their gimplification
2855 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2856 code = TREE_CODE (cond);
2857 if (code == TRUTH_ANDIF_EXPR)
2858 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2859 else if (code == TRUTH_ORIF_EXPR)
2860 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2861 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2862 COND_EXPR_COND (*expr_p) = cond;
2864 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2865 is_gimple_val, fb_rvalue);
2866 ret = MIN (ret, tret);
2867 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2868 is_gimple_val, fb_rvalue);
2870 return MIN (ret, tret);
2873 /* Return true if evaluating EXPR could trap.
2874 EXPR is GENERIC, while tree_could_trap_p can be called
2875 only on GIMPLE. */
2877 static bool
2878 generic_expr_could_trap_p (tree expr)
2880 unsigned i, n;
2882 if (!expr || is_gimple_val (expr))
2883 return false;
2885 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2886 return true;
2888 n = TREE_OPERAND_LENGTH (expr);
2889 for (i = 0; i < n; i++)
2890 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2891 return true;
2893 return false;
2896 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2897 into
2899 if (p) if (p)
2900 t1 = a; a;
2901 else or else
2902 t1 = b; b;
2905 The second form is used when *EXPR_P is of type void.
2907 PRE_P points to the list where side effects that must happen before
2908 *EXPR_P should be stored. */
2910 static enum gimplify_status
2911 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2913 tree expr = *expr_p;
2914 tree type = TREE_TYPE (expr);
2915 location_t loc = EXPR_LOCATION (expr);
2916 tree tmp, arm1, arm2;
2917 enum gimplify_status ret;
2918 tree label_true, label_false, label_cont;
2919 bool have_then_clause_p, have_else_clause_p;
2920 gimple gimple_cond;
2921 enum tree_code pred_code;
2922 gimple_seq seq = NULL;
2924 /* If this COND_EXPR has a value, copy the values into a temporary within
2925 the arms. */
2926 if (!VOID_TYPE_P (type))
2928 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2929 tree result;
2931 /* If either an rvalue is ok or we do not require an lvalue, create the
2932 temporary. But we cannot do that if the type is addressable. */
2933 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
2934 && !TREE_ADDRESSABLE (type))
2936 if (gimplify_ctxp->allow_rhs_cond_expr
2937 /* If either branch has side effects or could trap, it can't be
2938 evaluated unconditionally. */
2939 && !TREE_SIDE_EFFECTS (then_)
2940 && !generic_expr_could_trap_p (then_)
2941 && !TREE_SIDE_EFFECTS (else_)
2942 && !generic_expr_could_trap_p (else_))
2943 return gimplify_pure_cond_expr (expr_p, pre_p);
2945 tmp = create_tmp_var (type, "iftmp");
2946 result = tmp;
2949 /* Otherwise, only create and copy references to the values. */
2950 else
2952 type = build_pointer_type (type);
2954 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2955 then_ = build_fold_addr_expr_loc (loc, then_);
2957 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2958 else_ = build_fold_addr_expr_loc (loc, else_);
2960 expr
2961 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
2963 tmp = create_tmp_var (type, "iftmp");
2964 result = build_simple_mem_ref_loc (loc, tmp);
2967 /* Build the new then clause, `tmp = then_;'. But don't build the
2968 assignment if the value is void; in C++ it can be if it's a throw. */
2969 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2970 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
2972 /* Similarly, build the new else clause, `tmp = else_;'. */
2973 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2974 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
2976 TREE_TYPE (expr) = void_type_node;
2977 recalculate_side_effects (expr);
2979 /* Move the COND_EXPR to the prequeue. */
2980 gimplify_stmt (&expr, pre_p);
2982 *expr_p = result;
2983 return GS_ALL_DONE;
2986 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
2987 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
2988 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
2989 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
2991 /* Make sure the condition has BOOLEAN_TYPE. */
2992 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2994 /* Break apart && and || conditions. */
2995 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2996 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2998 expr = shortcut_cond_expr (expr);
3000 if (expr != *expr_p)
3002 *expr_p = expr;
3004 /* We can't rely on gimplify_expr to re-gimplify the expanded
3005 form properly, as cleanups might cause the target labels to be
3006 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3007 set up a conditional context. */
3008 gimple_push_condition ();
3009 gimplify_stmt (expr_p, &seq);
3010 gimple_pop_condition (pre_p);
3011 gimple_seq_add_seq (pre_p, seq);
3013 return GS_ALL_DONE;
3017 /* Now do the normal gimplification. */
3019 /* Gimplify condition. */
3020 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3021 fb_rvalue);
3022 if (ret == GS_ERROR)
3023 return GS_ERROR;
3024 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3026 gimple_push_condition ();
3028 have_then_clause_p = have_else_clause_p = false;
3029 if (TREE_OPERAND (expr, 1) != NULL
3030 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3031 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3032 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3033 == current_function_decl)
3034 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3035 have different locations, otherwise we end up with incorrect
3036 location information on the branches. */
3037 && (optimize
3038 || !EXPR_HAS_LOCATION (expr)
3039 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3040 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3042 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3043 have_then_clause_p = true;
3045 else
3046 label_true = create_artificial_label (UNKNOWN_LOCATION);
3047 if (TREE_OPERAND (expr, 2) != NULL
3048 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3049 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3050 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3051 == current_function_decl)
3052 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3053 have different locations, otherwise we end up with incorrect
3054 location information on the branches. */
3055 && (optimize
3056 || !EXPR_HAS_LOCATION (expr)
3057 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3058 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3060 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3061 have_else_clause_p = true;
3063 else
3064 label_false = create_artificial_label (UNKNOWN_LOCATION);
3066 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3067 &arm2);
3069 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3070 label_false);
3072 gimplify_seq_add_stmt (&seq, gimple_cond);
3073 label_cont = NULL_TREE;
3074 if (!have_then_clause_p)
3076 /* For if (...) {} else { code; } put label_true after
3077 the else block. */
3078 if (TREE_OPERAND (expr, 1) == NULL_TREE
3079 && !have_else_clause_p
3080 && TREE_OPERAND (expr, 2) != NULL_TREE)
3081 label_cont = label_true;
3082 else
3084 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3085 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3086 /* For if (...) { code; } else {} or
3087 if (...) { code; } else goto label; or
3088 if (...) { code; return; } else { ... }
3089 label_cont isn't needed. */
3090 if (!have_else_clause_p
3091 && TREE_OPERAND (expr, 2) != NULL_TREE
3092 && gimple_seq_may_fallthru (seq))
3094 gimple g;
3095 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3097 g = gimple_build_goto (label_cont);
3099 /* GIMPLE_COND's are very low level; they have embedded
3100 gotos. This particular embedded goto should not be marked
3101 with the location of the original COND_EXPR, as it would
3102 correspond to the COND_EXPR's condition, not the ELSE or the
3103 THEN arms. To avoid marking it with the wrong location, flag
3104 it as "no location". */
3105 gimple_set_do_not_emit_location (g);
3107 gimplify_seq_add_stmt (&seq, g);
3111 if (!have_else_clause_p)
3113 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3114 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3116 if (label_cont)
3117 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3119 gimple_pop_condition (pre_p);
3120 gimple_seq_add_seq (pre_p, seq);
3122 if (ret == GS_ERROR)
3123 ; /* Do nothing. */
3124 else if (have_then_clause_p || have_else_clause_p)
3125 ret = GS_ALL_DONE;
3126 else
3128 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3129 expr = TREE_OPERAND (expr, 0);
3130 gimplify_stmt (&expr, pre_p);
3133 *expr_p = NULL;
3134 return ret;
3137 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3138 to be marked addressable.
3140 We cannot rely on such an expression being directly markable if a temporary
3141 has been created by the gimplification. In this case, we create another
3142 temporary and initialize it with a copy, which will become a store after we
3143 mark it addressable. This can happen if the front-end passed us something
3144 that it could not mark addressable yet, like a Fortran pass-by-reference
3145 parameter (int) floatvar. */
3147 static void
3148 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3150 while (handled_component_p (*expr_p))
3151 expr_p = &TREE_OPERAND (*expr_p, 0);
3152 if (is_gimple_reg (*expr_p))
3153 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3156 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3157 a call to __builtin_memcpy. */
3159 static enum gimplify_status
3160 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3161 gimple_seq *seq_p)
3163 tree t, to, to_ptr, from, from_ptr;
3164 gimple gs;
3165 location_t loc = EXPR_LOCATION (*expr_p);
3167 to = TREE_OPERAND (*expr_p, 0);
3168 from = TREE_OPERAND (*expr_p, 1);
3170 /* Mark the RHS addressable. Beware that it may not be possible to do so
3171 directly if a temporary has been created by the gimplification. */
3172 prepare_gimple_addressable (&from, seq_p);
3174 mark_addressable (from);
3175 from_ptr = build_fold_addr_expr_loc (loc, from);
3176 gimplify_arg (&from_ptr, seq_p, loc);
3178 mark_addressable (to);
3179 to_ptr = build_fold_addr_expr_loc (loc, to);
3180 gimplify_arg (&to_ptr, seq_p, loc);
3182 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3184 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3186 if (want_value)
3188 /* tmp = memcpy() */
3189 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3190 gimple_call_set_lhs (gs, t);
3191 gimplify_seq_add_stmt (seq_p, gs);
3193 *expr_p = build_simple_mem_ref (t);
3194 return GS_ALL_DONE;
3197 gimplify_seq_add_stmt (seq_p, gs);
3198 *expr_p = NULL;
3199 return GS_ALL_DONE;
3202 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3203 a call to __builtin_memset. In this case we know that the RHS is
3204 a CONSTRUCTOR with an empty element list. */
3206 static enum gimplify_status
3207 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3208 gimple_seq *seq_p)
3210 tree t, from, to, to_ptr;
3211 gimple gs;
3212 location_t loc = EXPR_LOCATION (*expr_p);
3214 /* Assert our assumptions, to abort instead of producing wrong code
3215 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3216 not be immediately exposed. */
3217 from = TREE_OPERAND (*expr_p, 1);
3218 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3219 from = TREE_OPERAND (from, 0);
3221 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3222 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3224 /* Now proceed. */
3225 to = TREE_OPERAND (*expr_p, 0);
3227 to_ptr = build_fold_addr_expr_loc (loc, to);
3228 gimplify_arg (&to_ptr, seq_p, loc);
3229 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3231 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3233 if (want_value)
3235 /* tmp = memset() */
3236 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3237 gimple_call_set_lhs (gs, t);
3238 gimplify_seq_add_stmt (seq_p, gs);
3240 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3241 return GS_ALL_DONE;
3244 gimplify_seq_add_stmt (seq_p, gs);
3245 *expr_p = NULL;
3246 return GS_ALL_DONE;
3249 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3250 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3251 assignment. Return non-null if we detect a potential overlap. */
3253 struct gimplify_init_ctor_preeval_data
3255 /* The base decl of the lhs object. May be NULL, in which case we
3256 have to assume the lhs is indirect. */
3257 tree lhs_base_decl;
3259 /* The alias set of the lhs object. */
3260 alias_set_type lhs_alias_set;
3263 static tree
3264 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3266 struct gimplify_init_ctor_preeval_data *data
3267 = (struct gimplify_init_ctor_preeval_data *) xdata;
3268 tree t = *tp;
3270 /* If we find the base object, obviously we have overlap. */
3271 if (data->lhs_base_decl == t)
3272 return t;
3274 /* If the constructor component is indirect, determine if we have a
3275 potential overlap with the lhs. The only bits of information we
3276 have to go on at this point are addressability and alias sets. */
3277 if ((INDIRECT_REF_P (t)
3278 || TREE_CODE (t) == MEM_REF)
3279 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3280 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3281 return t;
3283 /* If the constructor component is a call, determine if it can hide a
3284 potential overlap with the lhs through an INDIRECT_REF like above.
3285 ??? Ugh - this is completely broken. In fact this whole analysis
3286 doesn't look conservative. */
3287 if (TREE_CODE (t) == CALL_EXPR)
3289 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3291 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3292 if (POINTER_TYPE_P (TREE_VALUE (type))
3293 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3294 && alias_sets_conflict_p (data->lhs_alias_set,
3295 get_alias_set
3296 (TREE_TYPE (TREE_VALUE (type)))))
3297 return t;
3300 if (IS_TYPE_OR_DECL_P (t))
3301 *walk_subtrees = 0;
3302 return NULL;
3305 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3306 force values that overlap with the lhs (as described by *DATA)
3307 into temporaries. */
3309 static void
3310 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3311 struct gimplify_init_ctor_preeval_data *data)
3313 enum gimplify_status one;
3315 /* If the value is constant, then there's nothing to pre-evaluate. */
3316 if (TREE_CONSTANT (*expr_p))
3318 /* Ensure it does not have side effects, it might contain a reference to
3319 the object we're initializing. */
3320 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3321 return;
3324 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3325 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3326 return;
3328 /* Recurse for nested constructors. */
3329 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3331 unsigned HOST_WIDE_INT ix;
3332 constructor_elt *ce;
3333 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3335 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3336 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3338 return;
3341 /* If this is a variable sized type, we must remember the size. */
3342 maybe_with_size_expr (expr_p);
3344 /* Gimplify the constructor element to something appropriate for the rhs
3345 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3346 the gimplifier will consider this a store to memory. Doing this
3347 gimplification now means that we won't have to deal with complicated
3348 language-specific trees, nor trees like SAVE_EXPR that can induce
3349 exponential search behavior. */
3350 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3351 if (one == GS_ERROR)
3353 *expr_p = NULL;
3354 return;
3357 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3358 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3359 always be true for all scalars, since is_gimple_mem_rhs insists on a
3360 temporary variable for them. */
3361 if (DECL_P (*expr_p))
3362 return;
3364 /* If this is of variable size, we have no choice but to assume it doesn't
3365 overlap since we can't make a temporary for it. */
3366 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3367 return;
3369 /* Otherwise, we must search for overlap ... */
3370 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3371 return;
3373 /* ... and if found, force the value into a temporary. */
3374 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3377 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3378 a RANGE_EXPR in a CONSTRUCTOR for an array.
3380 var = lower;
3381 loop_entry:
3382 object[var] = value;
3383 if (var == upper)
3384 goto loop_exit;
3385 var = var + 1;
3386 goto loop_entry;
3387 loop_exit:
3389 We increment var _after_ the loop exit check because we might otherwise
3390 fail if upper == TYPE_MAX_VALUE (type for upper).
3392 Note that we never have to deal with SAVE_EXPRs here, because this has
3393 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3395 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3396 gimple_seq *, bool);
3398 static void
3399 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3400 tree value, tree array_elt_type,
3401 gimple_seq *pre_p, bool cleared)
3403 tree loop_entry_label, loop_exit_label, fall_thru_label;
3404 tree var, var_type, cref, tmp;
3406 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3407 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3408 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3410 /* Create and initialize the index variable. */
3411 var_type = TREE_TYPE (upper);
3412 var = create_tmp_var (var_type, NULL);
3413 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3415 /* Add the loop entry label. */
3416 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3418 /* Build the reference. */
3419 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3420 var, NULL_TREE, NULL_TREE);
3422 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3423 the store. Otherwise just assign value to the reference. */
3425 if (TREE_CODE (value) == CONSTRUCTOR)
3426 /* NB we might have to call ourself recursively through
3427 gimplify_init_ctor_eval if the value is a constructor. */
3428 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3429 pre_p, cleared);
3430 else
3431 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3433 /* We exit the loop when the index var is equal to the upper bound. */
3434 gimplify_seq_add_stmt (pre_p,
3435 gimple_build_cond (EQ_EXPR, var, upper,
3436 loop_exit_label, fall_thru_label));
3438 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3440 /* Otherwise, increment the index var... */
3441 tmp = build2 (PLUS_EXPR, var_type, var,
3442 fold_convert (var_type, integer_one_node));
3443 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3445 /* ...and jump back to the loop entry. */
3446 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3448 /* Add the loop exit label. */
3449 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3452 /* Return true if FDECL is accessing a field that is zero sized. */
3454 static bool
3455 zero_sized_field_decl (const_tree fdecl)
3457 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3458 && integer_zerop (DECL_SIZE (fdecl)))
3459 return true;
3460 return false;
3463 /* Return true if TYPE is zero sized. */
3465 static bool
3466 zero_sized_type (const_tree type)
3468 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3469 && integer_zerop (TYPE_SIZE (type)))
3470 return true;
3471 return false;
3474 /* A subroutine of gimplify_init_constructor. Generate individual
3475 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3476 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3477 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3478 zeroed first. */
3480 static void
3481 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3482 gimple_seq *pre_p, bool cleared)
3484 tree array_elt_type = NULL;
3485 unsigned HOST_WIDE_INT ix;
3486 tree purpose, value;
3488 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3489 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3491 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3493 tree cref;
3495 /* NULL values are created above for gimplification errors. */
3496 if (value == NULL)
3497 continue;
3499 if (cleared && initializer_zerop (value))
3500 continue;
3502 /* ??? Here's to hoping the front end fills in all of the indices,
3503 so we don't have to figure out what's missing ourselves. */
3504 gcc_assert (purpose);
3506 /* Skip zero-sized fields, unless value has side-effects. This can
3507 happen with calls to functions returning a zero-sized type, which
3508 we shouldn't discard. As a number of downstream passes don't
3509 expect sets of zero-sized fields, we rely on the gimplification of
3510 the MODIFY_EXPR we make below to drop the assignment statement. */
3511 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3512 continue;
3514 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3515 whole range. */
3516 if (TREE_CODE (purpose) == RANGE_EXPR)
3518 tree lower = TREE_OPERAND (purpose, 0);
3519 tree upper = TREE_OPERAND (purpose, 1);
3521 /* If the lower bound is equal to upper, just treat it as if
3522 upper was the index. */
3523 if (simple_cst_equal (lower, upper))
3524 purpose = upper;
3525 else
3527 gimplify_init_ctor_eval_range (object, lower, upper, value,
3528 array_elt_type, pre_p, cleared);
3529 continue;
3533 if (array_elt_type)
3535 /* Do not use bitsizetype for ARRAY_REF indices. */
3536 if (TYPE_DOMAIN (TREE_TYPE (object)))
3537 purpose
3538 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3539 purpose);
3540 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3541 purpose, NULL_TREE, NULL_TREE);
3543 else
3545 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3546 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3547 unshare_expr (object), purpose, NULL_TREE);
3550 if (TREE_CODE (value) == CONSTRUCTOR
3551 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3552 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3553 pre_p, cleared);
3554 else
3556 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3557 gimplify_and_add (init, pre_p);
3558 ggc_free (init);
3563 /* Return the appropriate RHS predicate for this LHS. */
3565 gimple_predicate
3566 rhs_predicate_for (tree lhs)
3568 if (is_gimple_reg (lhs))
3569 return is_gimple_reg_rhs_or_call;
3570 else
3571 return is_gimple_mem_rhs_or_call;
3574 /* Gimplify a C99 compound literal expression. This just means adding
3575 the DECL_EXPR before the current statement and using its anonymous
3576 decl instead. */
3578 static enum gimplify_status
3579 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3580 bool (*gimple_test_f) (tree),
3581 fallback_t fallback)
3583 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3584 tree decl = DECL_EXPR_DECL (decl_s);
3585 tree init = DECL_INITIAL (decl);
3586 /* Mark the decl as addressable if the compound literal
3587 expression is addressable now, otherwise it is marked too late
3588 after we gimplify the initialization expression. */
3589 if (TREE_ADDRESSABLE (*expr_p))
3590 TREE_ADDRESSABLE (decl) = 1;
3591 /* Otherwise, if we don't need an lvalue and have a literal directly
3592 substitute it. Check if it matches the gimple predicate, as
3593 otherwise we'd generate a new temporary, and we can as well just
3594 use the decl we already have. */
3595 else if (!TREE_ADDRESSABLE (decl)
3596 && init
3597 && (fallback & fb_lvalue) == 0
3598 && gimple_test_f (init))
3600 *expr_p = init;
3601 return GS_OK;
3604 /* Preliminarily mark non-addressed complex variables as eligible
3605 for promotion to gimple registers. We'll transform their uses
3606 as we find them. */
3607 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3608 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3609 && !TREE_THIS_VOLATILE (decl)
3610 && !needs_to_live_in_memory (decl))
3611 DECL_GIMPLE_REG_P (decl) = 1;
3613 /* If the decl is not addressable, then it is being used in some
3614 expression or on the right hand side of a statement, and it can
3615 be put into a readonly data section. */
3616 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3617 TREE_READONLY (decl) = 1;
3619 /* This decl isn't mentioned in the enclosing block, so add it to the
3620 list of temps. FIXME it seems a bit of a kludge to say that
3621 anonymous artificial vars aren't pushed, but everything else is. */
3622 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3623 gimple_add_tmp_var (decl);
3625 gimplify_and_add (decl_s, pre_p);
3626 *expr_p = decl;
3627 return GS_OK;
3630 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3631 return a new CONSTRUCTOR if something changed. */
3633 static tree
3634 optimize_compound_literals_in_ctor (tree orig_ctor)
3636 tree ctor = orig_ctor;
3637 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3638 unsigned int idx, num = vec_safe_length (elts);
3640 for (idx = 0; idx < num; idx++)
3642 tree value = (*elts)[idx].value;
3643 tree newval = value;
3644 if (TREE_CODE (value) == CONSTRUCTOR)
3645 newval = optimize_compound_literals_in_ctor (value);
3646 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3648 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3649 tree decl = DECL_EXPR_DECL (decl_s);
3650 tree init = DECL_INITIAL (decl);
3652 if (!TREE_ADDRESSABLE (value)
3653 && !TREE_ADDRESSABLE (decl)
3654 && init
3655 && TREE_CODE (init) == CONSTRUCTOR)
3656 newval = optimize_compound_literals_in_ctor (init);
3658 if (newval == value)
3659 continue;
3661 if (ctor == orig_ctor)
3663 ctor = copy_node (orig_ctor);
3664 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3665 elts = CONSTRUCTOR_ELTS (ctor);
3667 (*elts)[idx].value = newval;
3669 return ctor;
3672 /* A subroutine of gimplify_modify_expr. Break out elements of a
3673 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3675 Note that we still need to clear any elements that don't have explicit
3676 initializers, so if not all elements are initialized we keep the
3677 original MODIFY_EXPR, we just remove all of the constructor elements.
3679 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3680 GS_ERROR if we would have to create a temporary when gimplifying
3681 this constructor. Otherwise, return GS_OK.
3683 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3685 static enum gimplify_status
3686 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3687 bool want_value, bool notify_temp_creation)
3689 tree object, ctor, type;
3690 enum gimplify_status ret;
3691 vec<constructor_elt, va_gc> *elts;
3693 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3695 if (!notify_temp_creation)
3697 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3698 is_gimple_lvalue, fb_lvalue);
3699 if (ret == GS_ERROR)
3700 return ret;
3703 object = TREE_OPERAND (*expr_p, 0);
3704 ctor = TREE_OPERAND (*expr_p, 1) =
3705 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3706 type = TREE_TYPE (ctor);
3707 elts = CONSTRUCTOR_ELTS (ctor);
3708 ret = GS_ALL_DONE;
3710 switch (TREE_CODE (type))
3712 case RECORD_TYPE:
3713 case UNION_TYPE:
3714 case QUAL_UNION_TYPE:
3715 case ARRAY_TYPE:
3717 struct gimplify_init_ctor_preeval_data preeval_data;
3718 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3719 bool cleared, complete_p, valid_const_initializer;
3721 /* Aggregate types must lower constructors to initialization of
3722 individual elements. The exception is that a CONSTRUCTOR node
3723 with no elements indicates zero-initialization of the whole. */
3724 if (vec_safe_is_empty (elts))
3726 if (notify_temp_creation)
3727 return GS_OK;
3728 break;
3731 /* Fetch information about the constructor to direct later processing.
3732 We might want to make static versions of it in various cases, and
3733 can only do so if it known to be a valid constant initializer. */
3734 valid_const_initializer
3735 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3736 &num_ctor_elements, &complete_p);
3738 /* If a const aggregate variable is being initialized, then it
3739 should never be a lose to promote the variable to be static. */
3740 if (valid_const_initializer
3741 && num_nonzero_elements > 1
3742 && TREE_READONLY (object)
3743 && TREE_CODE (object) == VAR_DECL
3744 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3746 if (notify_temp_creation)
3747 return GS_ERROR;
3748 DECL_INITIAL (object) = ctor;
3749 TREE_STATIC (object) = 1;
3750 if (!DECL_NAME (object))
3751 DECL_NAME (object) = create_tmp_var_name ("C");
3752 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3754 /* ??? C++ doesn't automatically append a .<number> to the
3755 assembler name, and even when it does, it looks at FE private
3756 data structures to figure out what that number should be,
3757 which are not set for this variable. I suppose this is
3758 important for local statics for inline functions, which aren't
3759 "local" in the object file sense. So in order to get a unique
3760 TU-local symbol, we must invoke the lhd version now. */
3761 lhd_set_decl_assembler_name (object);
3763 *expr_p = NULL_TREE;
3764 break;
3767 /* If there are "lots" of initialized elements, even discounting
3768 those that are not address constants (and thus *must* be
3769 computed at runtime), then partition the constructor into
3770 constant and non-constant parts. Block copy the constant
3771 parts in, then generate code for the non-constant parts. */
3772 /* TODO. There's code in cp/typeck.c to do this. */
3774 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3775 /* store_constructor will ignore the clearing of variable-sized
3776 objects. Initializers for such objects must explicitly set
3777 every field that needs to be set. */
3778 cleared = false;
3779 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3780 /* If the constructor isn't complete, clear the whole object
3781 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3783 ??? This ought not to be needed. For any element not present
3784 in the initializer, we should simply set them to zero. Except
3785 we'd need to *find* the elements that are not present, and that
3786 requires trickery to avoid quadratic compile-time behavior in
3787 large cases or excessive memory use in small cases. */
3788 cleared = true;
3789 else if (num_ctor_elements - num_nonzero_elements
3790 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3791 && num_nonzero_elements < num_ctor_elements / 4)
3792 /* If there are "lots" of zeros, it's more efficient to clear
3793 the memory and then set the nonzero elements. */
3794 cleared = true;
3795 else
3796 cleared = false;
3798 /* If there are "lots" of initialized elements, and all of them
3799 are valid address constants, then the entire initializer can
3800 be dropped to memory, and then memcpy'd out. Don't do this
3801 for sparse arrays, though, as it's more efficient to follow
3802 the standard CONSTRUCTOR behavior of memset followed by
3803 individual element initialization. Also don't do this for small
3804 all-zero initializers (which aren't big enough to merit
3805 clearing), and don't try to make bitwise copies of
3806 TREE_ADDRESSABLE types. */
3807 if (valid_const_initializer
3808 && !(cleared || num_nonzero_elements == 0)
3809 && !TREE_ADDRESSABLE (type))
3811 HOST_WIDE_INT size = int_size_in_bytes (type);
3812 unsigned int align;
3814 /* ??? We can still get unbounded array types, at least
3815 from the C++ front end. This seems wrong, but attempt
3816 to work around it for now. */
3817 if (size < 0)
3819 size = int_size_in_bytes (TREE_TYPE (object));
3820 if (size >= 0)
3821 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3824 /* Find the maximum alignment we can assume for the object. */
3825 /* ??? Make use of DECL_OFFSET_ALIGN. */
3826 if (DECL_P (object))
3827 align = DECL_ALIGN (object);
3828 else
3829 align = TYPE_ALIGN (type);
3831 /* Do a block move either if the size is so small as to make
3832 each individual move a sub-unit move on average, or if it
3833 is so large as to make individual moves inefficient. */
3834 if (size > 0
3835 && num_nonzero_elements > 1
3836 && (size < num_nonzero_elements
3837 || !can_move_by_pieces (size, align)))
3839 if (notify_temp_creation)
3840 return GS_ERROR;
3842 walk_tree (&ctor, force_labels_r, NULL, NULL);
3843 ctor = tree_output_constant_def (ctor);
3844 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3845 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3846 TREE_OPERAND (*expr_p, 1) = ctor;
3848 /* This is no longer an assignment of a CONSTRUCTOR, but
3849 we still may have processing to do on the LHS. So
3850 pretend we didn't do anything here to let that happen. */
3851 return GS_UNHANDLED;
3855 /* If the target is volatile, we have non-zero elements and more than
3856 one field to assign, initialize the target from a temporary. */
3857 if (TREE_THIS_VOLATILE (object)
3858 && !TREE_ADDRESSABLE (type)
3859 && num_nonzero_elements > 0
3860 && vec_safe_length (elts) > 1)
3862 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3863 TREE_OPERAND (*expr_p, 0) = temp;
3864 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3865 *expr_p,
3866 build2 (MODIFY_EXPR, void_type_node,
3867 object, temp));
3868 return GS_OK;
3871 if (notify_temp_creation)
3872 return GS_OK;
3874 /* If there are nonzero elements and if needed, pre-evaluate to capture
3875 elements overlapping with the lhs into temporaries. We must do this
3876 before clearing to fetch the values before they are zeroed-out. */
3877 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3879 preeval_data.lhs_base_decl = get_base_address (object);
3880 if (!DECL_P (preeval_data.lhs_base_decl))
3881 preeval_data.lhs_base_decl = NULL;
3882 preeval_data.lhs_alias_set = get_alias_set (object);
3884 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3885 pre_p, post_p, &preeval_data);
3888 if (cleared)
3890 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3891 Note that we still have to gimplify, in order to handle the
3892 case of variable sized types. Avoid shared tree structures. */
3893 CONSTRUCTOR_ELTS (ctor) = NULL;
3894 TREE_SIDE_EFFECTS (ctor) = 0;
3895 object = unshare_expr (object);
3896 gimplify_stmt (expr_p, pre_p);
3899 /* If we have not block cleared the object, or if there are nonzero
3900 elements in the constructor, add assignments to the individual
3901 scalar fields of the object. */
3902 if (!cleared || num_nonzero_elements > 0)
3903 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3905 *expr_p = NULL_TREE;
3907 break;
3909 case COMPLEX_TYPE:
3911 tree r, i;
3913 if (notify_temp_creation)
3914 return GS_OK;
3916 /* Extract the real and imaginary parts out of the ctor. */
3917 gcc_assert (elts->length () == 2);
3918 r = (*elts)[0].value;
3919 i = (*elts)[1].value;
3920 if (r == NULL || i == NULL)
3922 tree zero = build_zero_cst (TREE_TYPE (type));
3923 if (r == NULL)
3924 r = zero;
3925 if (i == NULL)
3926 i = zero;
3929 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3930 represent creation of a complex value. */
3931 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3933 ctor = build_complex (type, r, i);
3934 TREE_OPERAND (*expr_p, 1) = ctor;
3936 else
3938 ctor = build2 (COMPLEX_EXPR, type, r, i);
3939 TREE_OPERAND (*expr_p, 1) = ctor;
3940 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
3941 pre_p,
3942 post_p,
3943 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3944 fb_rvalue);
3947 break;
3949 case VECTOR_TYPE:
3951 unsigned HOST_WIDE_INT ix;
3952 constructor_elt *ce;
3954 if (notify_temp_creation)
3955 return GS_OK;
3957 /* Go ahead and simplify constant constructors to VECTOR_CST. */
3958 if (TREE_CONSTANT (ctor))
3960 bool constant_p = true;
3961 tree value;
3963 /* Even when ctor is constant, it might contain non-*_CST
3964 elements, such as addresses or trapping values like
3965 1.0/0.0 - 1.0/0.0. Such expressions don't belong
3966 in VECTOR_CST nodes. */
3967 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
3968 if (!CONSTANT_CLASS_P (value))
3970 constant_p = false;
3971 break;
3974 if (constant_p)
3976 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
3977 break;
3980 /* Don't reduce an initializer constant even if we can't
3981 make a VECTOR_CST. It won't do anything for us, and it'll
3982 prevent us from representing it as a single constant. */
3983 if (initializer_constant_valid_p (ctor, type))
3984 break;
3986 TREE_CONSTANT (ctor) = 0;
3989 /* Vector types use CONSTRUCTOR all the way through gimple
3990 compilation as a general initializer. */
3991 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
3993 enum gimplify_status tret;
3994 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
3995 fb_rvalue);
3996 if (tret == GS_ERROR)
3997 ret = GS_ERROR;
3999 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4000 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4002 break;
4004 default:
4005 /* So how did we get a CONSTRUCTOR for a scalar type? */
4006 gcc_unreachable ();
4009 if (ret == GS_ERROR)
4010 return GS_ERROR;
4011 else if (want_value)
4013 *expr_p = object;
4014 return GS_OK;
4016 else
4018 /* If we have gimplified both sides of the initializer but have
4019 not emitted an assignment, do so now. */
4020 if (*expr_p)
4022 tree lhs = TREE_OPERAND (*expr_p, 0);
4023 tree rhs = TREE_OPERAND (*expr_p, 1);
4024 gimple init = gimple_build_assign (lhs, rhs);
4025 gimplify_seq_add_stmt (pre_p, init);
4026 *expr_p = NULL;
4029 return GS_ALL_DONE;
4033 /* Given a pointer value OP0, return a simplified version of an
4034 indirection through OP0, or NULL_TREE if no simplification is
4035 possible. This may only be applied to a rhs of an expression.
4036 Note that the resulting type may be different from the type pointed
4037 to in the sense that it is still compatible from the langhooks
4038 point of view. */
4040 static tree
4041 gimple_fold_indirect_ref_rhs (tree t)
4043 return gimple_fold_indirect_ref (t);
4046 /* Subroutine of gimplify_modify_expr to do simplifications of
4047 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4048 something changes. */
4050 static enum gimplify_status
4051 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4052 gimple_seq *pre_p, gimple_seq *post_p,
4053 bool want_value)
4055 enum gimplify_status ret = GS_UNHANDLED;
4056 bool changed;
4060 changed = false;
4061 switch (TREE_CODE (*from_p))
4063 case VAR_DECL:
4064 /* If we're assigning from a read-only variable initialized with
4065 a constructor, do the direct assignment from the constructor,
4066 but only if neither source nor target are volatile since this
4067 latter assignment might end up being done on a per-field basis. */
4068 if (DECL_INITIAL (*from_p)
4069 && TREE_READONLY (*from_p)
4070 && !TREE_THIS_VOLATILE (*from_p)
4071 && !TREE_THIS_VOLATILE (*to_p)
4072 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4074 tree old_from = *from_p;
4075 enum gimplify_status subret;
4077 /* Move the constructor into the RHS. */
4078 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4080 /* Let's see if gimplify_init_constructor will need to put
4081 it in memory. */
4082 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4083 false, true);
4084 if (subret == GS_ERROR)
4086 /* If so, revert the change. */
4087 *from_p = old_from;
4089 else
4091 ret = GS_OK;
4092 changed = true;
4095 break;
4096 case INDIRECT_REF:
4098 /* If we have code like
4100 *(const A*)(A*)&x
4102 where the type of "x" is a (possibly cv-qualified variant
4103 of "A"), treat the entire expression as identical to "x".
4104 This kind of code arises in C++ when an object is bound
4105 to a const reference, and if "x" is a TARGET_EXPR we want
4106 to take advantage of the optimization below. */
4107 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4108 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4109 if (t)
4111 if (TREE_THIS_VOLATILE (t) != volatile_p)
4113 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4114 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4115 build_fold_addr_expr (t));
4116 if (REFERENCE_CLASS_P (t))
4117 TREE_THIS_VOLATILE (t) = volatile_p;
4119 *from_p = t;
4120 ret = GS_OK;
4121 changed = true;
4123 break;
4126 case TARGET_EXPR:
4128 /* If we are initializing something from a TARGET_EXPR, strip the
4129 TARGET_EXPR and initialize it directly, if possible. This can't
4130 be done if the initializer is void, since that implies that the
4131 temporary is set in some non-trivial way.
4133 ??? What about code that pulls out the temp and uses it
4134 elsewhere? I think that such code never uses the TARGET_EXPR as
4135 an initializer. If I'm wrong, we'll die because the temp won't
4136 have any RTL. In that case, I guess we'll need to replace
4137 references somehow. */
4138 tree init = TARGET_EXPR_INITIAL (*from_p);
4140 if (init
4141 && !VOID_TYPE_P (TREE_TYPE (init)))
4143 *from_p = init;
4144 ret = GS_OK;
4145 changed = true;
4148 break;
4150 case COMPOUND_EXPR:
4151 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4152 caught. */
4153 gimplify_compound_expr (from_p, pre_p, true);
4154 ret = GS_OK;
4155 changed = true;
4156 break;
4158 case CONSTRUCTOR:
4159 /* If we already made some changes, let the front end have a
4160 crack at this before we break it down. */
4161 if (ret != GS_UNHANDLED)
4162 break;
4163 /* If we're initializing from a CONSTRUCTOR, break this into
4164 individual MODIFY_EXPRs. */
4165 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4166 false);
4168 case COND_EXPR:
4169 /* If we're assigning to a non-register type, push the assignment
4170 down into the branches. This is mandatory for ADDRESSABLE types,
4171 since we cannot generate temporaries for such, but it saves a
4172 copy in other cases as well. */
4173 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4175 /* This code should mirror the code in gimplify_cond_expr. */
4176 enum tree_code code = TREE_CODE (*expr_p);
4177 tree cond = *from_p;
4178 tree result = *to_p;
4180 ret = gimplify_expr (&result, pre_p, post_p,
4181 is_gimple_lvalue, fb_lvalue);
4182 if (ret != GS_ERROR)
4183 ret = GS_OK;
4185 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4186 TREE_OPERAND (cond, 1)
4187 = build2 (code, void_type_node, result,
4188 TREE_OPERAND (cond, 1));
4189 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4190 TREE_OPERAND (cond, 2)
4191 = build2 (code, void_type_node, unshare_expr (result),
4192 TREE_OPERAND (cond, 2));
4194 TREE_TYPE (cond) = void_type_node;
4195 recalculate_side_effects (cond);
4197 if (want_value)
4199 gimplify_and_add (cond, pre_p);
4200 *expr_p = unshare_expr (result);
4202 else
4203 *expr_p = cond;
4204 return ret;
4206 break;
4208 case CALL_EXPR:
4209 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4210 return slot so that we don't generate a temporary. */
4211 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4212 && aggregate_value_p (*from_p, *from_p))
4214 bool use_target;
4216 if (!(rhs_predicate_for (*to_p))(*from_p))
4217 /* If we need a temporary, *to_p isn't accurate. */
4218 use_target = false;
4219 /* It's OK to use the return slot directly unless it's an NRV. */
4220 else if (TREE_CODE (*to_p) == RESULT_DECL
4221 && DECL_NAME (*to_p) == NULL_TREE
4222 && needs_to_live_in_memory (*to_p))
4223 use_target = true;
4224 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4225 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4226 /* Don't force regs into memory. */
4227 use_target = false;
4228 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4229 /* It's OK to use the target directly if it's being
4230 initialized. */
4231 use_target = true;
4232 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4233 /* Always use the target and thus RSO for variable-sized types.
4234 GIMPLE cannot deal with a variable-sized assignment
4235 embedded in a call statement. */
4236 use_target = true;
4237 else if (TREE_CODE (*to_p) != SSA_NAME
4238 && (!is_gimple_variable (*to_p)
4239 || needs_to_live_in_memory (*to_p)))
4240 /* Don't use the original target if it's already addressable;
4241 if its address escapes, and the called function uses the
4242 NRV optimization, a conforming program could see *to_p
4243 change before the called function returns; see c++/19317.
4244 When optimizing, the return_slot pass marks more functions
4245 as safe after we have escape info. */
4246 use_target = false;
4247 else
4248 use_target = true;
4250 if (use_target)
4252 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4253 mark_addressable (*to_p);
4256 break;
4258 case WITH_SIZE_EXPR:
4259 /* Likewise for calls that return an aggregate of non-constant size,
4260 since we would not be able to generate a temporary at all. */
4261 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4263 *from_p = TREE_OPERAND (*from_p, 0);
4264 /* We don't change ret in this case because the
4265 WITH_SIZE_EXPR might have been added in
4266 gimplify_modify_expr, so returning GS_OK would lead to an
4267 infinite loop. */
4268 changed = true;
4270 break;
4272 /* If we're initializing from a container, push the initialization
4273 inside it. */
4274 case CLEANUP_POINT_EXPR:
4275 case BIND_EXPR:
4276 case STATEMENT_LIST:
4278 tree wrap = *from_p;
4279 tree t;
4281 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4282 fb_lvalue);
4283 if (ret != GS_ERROR)
4284 ret = GS_OK;
4286 t = voidify_wrapper_expr (wrap, *expr_p);
4287 gcc_assert (t == *expr_p);
4289 if (want_value)
4291 gimplify_and_add (wrap, pre_p);
4292 *expr_p = unshare_expr (*to_p);
4294 else
4295 *expr_p = wrap;
4296 return GS_OK;
4299 case COMPOUND_LITERAL_EXPR:
4301 tree complit = TREE_OPERAND (*expr_p, 1);
4302 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4303 tree decl = DECL_EXPR_DECL (decl_s);
4304 tree init = DECL_INITIAL (decl);
4306 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4307 into struct T x = { 0, 1, 2 } if the address of the
4308 compound literal has never been taken. */
4309 if (!TREE_ADDRESSABLE (complit)
4310 && !TREE_ADDRESSABLE (decl)
4311 && init)
4313 *expr_p = copy_node (*expr_p);
4314 TREE_OPERAND (*expr_p, 1) = init;
4315 return GS_OK;
4319 default:
4320 break;
4323 while (changed);
4325 return ret;
4329 /* Return true if T looks like a valid GIMPLE statement. */
4331 static bool
4332 is_gimple_stmt (tree t)
4334 const enum tree_code code = TREE_CODE (t);
4336 switch (code)
4338 case NOP_EXPR:
4339 /* The only valid NOP_EXPR is the empty statement. */
4340 return IS_EMPTY_STMT (t);
4342 case BIND_EXPR:
4343 case COND_EXPR:
4344 /* These are only valid if they're void. */
4345 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4347 case SWITCH_EXPR:
4348 case GOTO_EXPR:
4349 case RETURN_EXPR:
4350 case LABEL_EXPR:
4351 case CASE_LABEL_EXPR:
4352 case TRY_CATCH_EXPR:
4353 case TRY_FINALLY_EXPR:
4354 case EH_FILTER_EXPR:
4355 case CATCH_EXPR:
4356 case ASM_EXPR:
4357 case STATEMENT_LIST:
4358 case OMP_PARALLEL:
4359 case OMP_FOR:
4360 case OMP_SIMD:
4361 case CILK_SIMD:
4362 case OMP_DISTRIBUTE:
4363 case OMP_SECTIONS:
4364 case OMP_SECTION:
4365 case OMP_SINGLE:
4366 case OMP_MASTER:
4367 case OMP_TASKGROUP:
4368 case OMP_ORDERED:
4369 case OMP_CRITICAL:
4370 case OMP_TASK:
4371 /* These are always void. */
4372 return true;
4374 case CALL_EXPR:
4375 case MODIFY_EXPR:
4376 case PREDICT_EXPR:
4377 /* These are valid regardless of their type. */
4378 return true;
4380 default:
4381 return false;
4386 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4387 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4388 DECL_GIMPLE_REG_P set.
4390 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4391 other, unmodified part of the complex object just before the total store.
4392 As a consequence, if the object is still uninitialized, an undefined value
4393 will be loaded into a register, which may result in a spurious exception
4394 if the register is floating-point and the value happens to be a signaling
4395 NaN for example. Then the fully-fledged complex operations lowering pass
4396 followed by a DCE pass are necessary in order to fix things up. */
4398 static enum gimplify_status
4399 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4400 bool want_value)
4402 enum tree_code code, ocode;
4403 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4405 lhs = TREE_OPERAND (*expr_p, 0);
4406 rhs = TREE_OPERAND (*expr_p, 1);
4407 code = TREE_CODE (lhs);
4408 lhs = TREE_OPERAND (lhs, 0);
4410 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4411 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4412 TREE_NO_WARNING (other) = 1;
4413 other = get_formal_tmp_var (other, pre_p);
4415 realpart = code == REALPART_EXPR ? rhs : other;
4416 imagpart = code == REALPART_EXPR ? other : rhs;
4418 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4419 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4420 else
4421 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4423 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4424 *expr_p = (want_value) ? rhs : NULL_TREE;
4426 return GS_ALL_DONE;
4429 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4431 modify_expr
4432 : varname '=' rhs
4433 | '*' ID '=' rhs
4435 PRE_P points to the list where side effects that must happen before
4436 *EXPR_P should be stored.
4438 POST_P points to the list where side effects that must happen after
4439 *EXPR_P should be stored.
4441 WANT_VALUE is nonzero iff we want to use the value of this expression
4442 in another expression. */
4444 static enum gimplify_status
4445 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4446 bool want_value)
4448 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4449 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4450 enum gimplify_status ret = GS_UNHANDLED;
4451 gimple assign;
4452 location_t loc = EXPR_LOCATION (*expr_p);
4453 gimple_stmt_iterator gsi;
4455 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4456 || TREE_CODE (*expr_p) == INIT_EXPR);
4458 /* Trying to simplify a clobber using normal logic doesn't work,
4459 so handle it here. */
4460 if (TREE_CLOBBER_P (*from_p))
4462 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4463 if (ret == GS_ERROR)
4464 return ret;
4465 gcc_assert (!want_value
4466 && (TREE_CODE (*to_p) == VAR_DECL
4467 || TREE_CODE (*to_p) == MEM_REF));
4468 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4469 *expr_p = NULL;
4470 return GS_ALL_DONE;
4473 /* Insert pointer conversions required by the middle-end that are not
4474 required by the frontend. This fixes middle-end type checking for
4475 for example gcc.dg/redecl-6.c. */
4476 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4478 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4479 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4480 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4483 /* See if any simplifications can be done based on what the RHS is. */
4484 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4485 want_value);
4486 if (ret != GS_UNHANDLED)
4487 return ret;
4489 /* For zero sized types only gimplify the left hand side and right hand
4490 side as statements and throw away the assignment. Do this after
4491 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4492 types properly. */
4493 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4495 gimplify_stmt (from_p, pre_p);
4496 gimplify_stmt (to_p, pre_p);
4497 *expr_p = NULL_TREE;
4498 return GS_ALL_DONE;
4501 /* If the value being copied is of variable width, compute the length
4502 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4503 before gimplifying any of the operands so that we can resolve any
4504 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4505 the size of the expression to be copied, not of the destination, so
4506 that is what we must do here. */
4507 maybe_with_size_expr (from_p);
4509 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4510 if (ret == GS_ERROR)
4511 return ret;
4513 /* As a special case, we have to temporarily allow for assignments
4514 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4515 a toplevel statement, when gimplifying the GENERIC expression
4516 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4517 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4519 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4520 prevent gimplify_expr from trying to create a new temporary for
4521 foo's LHS, we tell it that it should only gimplify until it
4522 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4523 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4524 and all we need to do here is set 'a' to be its LHS. */
4525 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4526 fb_rvalue);
4527 if (ret == GS_ERROR)
4528 return ret;
4530 /* Now see if the above changed *from_p to something we handle specially. */
4531 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4532 want_value);
4533 if (ret != GS_UNHANDLED)
4534 return ret;
4536 /* If we've got a variable sized assignment between two lvalues (i.e. does
4537 not involve a call), then we can make things a bit more straightforward
4538 by converting the assignment to memcpy or memset. */
4539 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4541 tree from = TREE_OPERAND (*from_p, 0);
4542 tree size = TREE_OPERAND (*from_p, 1);
4544 if (TREE_CODE (from) == CONSTRUCTOR)
4545 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4547 if (is_gimple_addressable (from))
4549 *from_p = from;
4550 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4551 pre_p);
4555 /* Transform partial stores to non-addressable complex variables into
4556 total stores. This allows us to use real instead of virtual operands
4557 for these variables, which improves optimization. */
4558 if ((TREE_CODE (*to_p) == REALPART_EXPR
4559 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4560 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4561 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4563 /* Try to alleviate the effects of the gimplification creating artificial
4564 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4565 if (!gimplify_ctxp->into_ssa
4566 && TREE_CODE (*from_p) == VAR_DECL
4567 && DECL_IGNORED_P (*from_p)
4568 && DECL_P (*to_p)
4569 && !DECL_IGNORED_P (*to_p))
4571 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4572 DECL_NAME (*from_p)
4573 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4574 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4575 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4578 if (want_value && TREE_THIS_VOLATILE (*to_p))
4579 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4581 if (TREE_CODE (*from_p) == CALL_EXPR)
4583 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4584 instead of a GIMPLE_ASSIGN. */
4585 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4586 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4587 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4588 tree fndecl = get_callee_fndecl (*from_p);
4589 if (fndecl
4590 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4591 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4592 && call_expr_nargs (*from_p) == 3)
4593 assign = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4594 CALL_EXPR_ARG (*from_p, 0),
4595 CALL_EXPR_ARG (*from_p, 1),
4596 CALL_EXPR_ARG (*from_p, 2));
4597 else
4599 assign = gimple_build_call_from_tree (*from_p);
4600 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4602 notice_special_calls (assign);
4603 if (!gimple_call_noreturn_p (assign))
4604 gimple_call_set_lhs (assign, *to_p);
4606 else
4608 assign = gimple_build_assign (*to_p, *from_p);
4609 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4612 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4614 /* We should have got an SSA name from the start. */
4615 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4618 gimplify_seq_add_stmt (pre_p, assign);
4619 gsi = gsi_last (*pre_p);
4620 maybe_fold_stmt (&gsi);
4622 if (want_value)
4624 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4625 return GS_OK;
4627 else
4628 *expr_p = NULL;
4630 return GS_ALL_DONE;
4633 /* Gimplify a comparison between two variable-sized objects. Do this
4634 with a call to BUILT_IN_MEMCMP. */
4636 static enum gimplify_status
4637 gimplify_variable_sized_compare (tree *expr_p)
4639 location_t loc = EXPR_LOCATION (*expr_p);
4640 tree op0 = TREE_OPERAND (*expr_p, 0);
4641 tree op1 = TREE_OPERAND (*expr_p, 1);
4642 tree t, arg, dest, src, expr;
4644 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4645 arg = unshare_expr (arg);
4646 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4647 src = build_fold_addr_expr_loc (loc, op1);
4648 dest = build_fold_addr_expr_loc (loc, op0);
4649 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4650 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4652 expr
4653 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4654 SET_EXPR_LOCATION (expr, loc);
4655 *expr_p = expr;
4657 return GS_OK;
4660 /* Gimplify a comparison between two aggregate objects of integral scalar
4661 mode as a comparison between the bitwise equivalent scalar values. */
4663 static enum gimplify_status
4664 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4666 location_t loc = EXPR_LOCATION (*expr_p);
4667 tree op0 = TREE_OPERAND (*expr_p, 0);
4668 tree op1 = TREE_OPERAND (*expr_p, 1);
4670 tree type = TREE_TYPE (op0);
4671 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4673 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4674 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4676 *expr_p
4677 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4679 return GS_OK;
4682 /* Gimplify an expression sequence. This function gimplifies each
4683 expression and rewrites the original expression with the last
4684 expression of the sequence in GIMPLE form.
4686 PRE_P points to the list where the side effects for all the
4687 expressions in the sequence will be emitted.
4689 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4691 static enum gimplify_status
4692 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4694 tree t = *expr_p;
4698 tree *sub_p = &TREE_OPERAND (t, 0);
4700 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4701 gimplify_compound_expr (sub_p, pre_p, false);
4702 else
4703 gimplify_stmt (sub_p, pre_p);
4705 t = TREE_OPERAND (t, 1);
4707 while (TREE_CODE (t) == COMPOUND_EXPR);
4709 *expr_p = t;
4710 if (want_value)
4711 return GS_OK;
4712 else
4714 gimplify_stmt (expr_p, pre_p);
4715 return GS_ALL_DONE;
4719 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4720 gimplify. After gimplification, EXPR_P will point to a new temporary
4721 that holds the original value of the SAVE_EXPR node.
4723 PRE_P points to the list where side effects that must happen before
4724 *EXPR_P should be stored. */
4726 static enum gimplify_status
4727 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4729 enum gimplify_status ret = GS_ALL_DONE;
4730 tree val;
4732 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4733 val = TREE_OPERAND (*expr_p, 0);
4735 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4736 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4738 /* The operand may be a void-valued expression such as SAVE_EXPRs
4739 generated by the Java frontend for class initialization. It is
4740 being executed only for its side-effects. */
4741 if (TREE_TYPE (val) == void_type_node)
4743 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4744 is_gimple_stmt, fb_none);
4745 val = NULL;
4747 else
4748 val = get_initialized_tmp_var (val, pre_p, post_p);
4750 TREE_OPERAND (*expr_p, 0) = val;
4751 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4754 *expr_p = val;
4756 return ret;
4759 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4761 unary_expr
4762 : ...
4763 | '&' varname
4766 PRE_P points to the list where side effects that must happen before
4767 *EXPR_P should be stored.
4769 POST_P points to the list where side effects that must happen after
4770 *EXPR_P should be stored. */
4772 static enum gimplify_status
4773 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4775 tree expr = *expr_p;
4776 tree op0 = TREE_OPERAND (expr, 0);
4777 enum gimplify_status ret;
4778 location_t loc = EXPR_LOCATION (*expr_p);
4780 switch (TREE_CODE (op0))
4782 case INDIRECT_REF:
4783 do_indirect_ref:
4784 /* Check if we are dealing with an expression of the form '&*ptr'.
4785 While the front end folds away '&*ptr' into 'ptr', these
4786 expressions may be generated internally by the compiler (e.g.,
4787 builtins like __builtin_va_end). */
4788 /* Caution: the silent array decomposition semantics we allow for
4789 ADDR_EXPR means we can't always discard the pair. */
4790 /* Gimplification of the ADDR_EXPR operand may drop
4791 cv-qualification conversions, so make sure we add them if
4792 needed. */
4794 tree op00 = TREE_OPERAND (op0, 0);
4795 tree t_expr = TREE_TYPE (expr);
4796 tree t_op00 = TREE_TYPE (op00);
4798 if (!useless_type_conversion_p (t_expr, t_op00))
4799 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4800 *expr_p = op00;
4801 ret = GS_OK;
4803 break;
4805 case VIEW_CONVERT_EXPR:
4806 /* Take the address of our operand and then convert it to the type of
4807 this ADDR_EXPR.
4809 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4810 all clear. The impact of this transformation is even less clear. */
4812 /* If the operand is a useless conversion, look through it. Doing so
4813 guarantees that the ADDR_EXPR and its operand will remain of the
4814 same type. */
4815 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4816 op0 = TREE_OPERAND (op0, 0);
4818 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4819 build_fold_addr_expr_loc (loc,
4820 TREE_OPERAND (op0, 0)));
4821 ret = GS_OK;
4822 break;
4824 default:
4825 /* We use fb_either here because the C frontend sometimes takes
4826 the address of a call that returns a struct; see
4827 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4828 the implied temporary explicit. */
4830 /* Make the operand addressable. */
4831 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4832 is_gimple_addressable, fb_either);
4833 if (ret == GS_ERROR)
4834 break;
4836 /* Then mark it. Beware that it may not be possible to do so directly
4837 if a temporary has been created by the gimplification. */
4838 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4840 op0 = TREE_OPERAND (expr, 0);
4842 /* For various reasons, the gimplification of the expression
4843 may have made a new INDIRECT_REF. */
4844 if (TREE_CODE (op0) == INDIRECT_REF)
4845 goto do_indirect_ref;
4847 mark_addressable (TREE_OPERAND (expr, 0));
4849 /* The FEs may end up building ADDR_EXPRs early on a decl with
4850 an incomplete type. Re-build ADDR_EXPRs in canonical form
4851 here. */
4852 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4853 *expr_p = build_fold_addr_expr (op0);
4855 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4856 recompute_tree_invariant_for_addr_expr (*expr_p);
4858 /* If we re-built the ADDR_EXPR add a conversion to the original type
4859 if required. */
4860 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4861 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4863 break;
4866 return ret;
4869 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4870 value; output operands should be a gimple lvalue. */
4872 static enum gimplify_status
4873 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4875 tree expr;
4876 int noutputs;
4877 const char **oconstraints;
4878 int i;
4879 tree link;
4880 const char *constraint;
4881 bool allows_mem, allows_reg, is_inout;
4882 enum gimplify_status ret, tret;
4883 gimple stmt;
4884 vec<tree, va_gc> *inputs;
4885 vec<tree, va_gc> *outputs;
4886 vec<tree, va_gc> *clobbers;
4887 vec<tree, va_gc> *labels;
4888 tree link_next;
4890 expr = *expr_p;
4891 noutputs = list_length (ASM_OUTPUTS (expr));
4892 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4894 inputs = NULL;
4895 outputs = NULL;
4896 clobbers = NULL;
4897 labels = NULL;
4899 ret = GS_ALL_DONE;
4900 link_next = NULL_TREE;
4901 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4903 bool ok;
4904 size_t constraint_len;
4906 link_next = TREE_CHAIN (link);
4908 oconstraints[i]
4909 = constraint
4910 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4911 constraint_len = strlen (constraint);
4912 if (constraint_len == 0)
4913 continue;
4915 ok = parse_output_constraint (&constraint, i, 0, 0,
4916 &allows_mem, &allows_reg, &is_inout);
4917 if (!ok)
4919 ret = GS_ERROR;
4920 is_inout = false;
4923 if (!allows_reg && allows_mem)
4924 mark_addressable (TREE_VALUE (link));
4926 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4927 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4928 fb_lvalue | fb_mayfail);
4929 if (tret == GS_ERROR)
4931 error ("invalid lvalue in asm output %d", i);
4932 ret = tret;
4935 vec_safe_push (outputs, link);
4936 TREE_CHAIN (link) = NULL_TREE;
4938 if (is_inout)
4940 /* An input/output operand. To give the optimizers more
4941 flexibility, split it into separate input and output
4942 operands. */
4943 tree input;
4944 char buf[10];
4946 /* Turn the in/out constraint into an output constraint. */
4947 char *p = xstrdup (constraint);
4948 p[0] = '=';
4949 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4951 /* And add a matching input constraint. */
4952 if (allows_reg)
4954 sprintf (buf, "%d", i);
4956 /* If there are multiple alternatives in the constraint,
4957 handle each of them individually. Those that allow register
4958 will be replaced with operand number, the others will stay
4959 unchanged. */
4960 if (strchr (p, ',') != NULL)
4962 size_t len = 0, buflen = strlen (buf);
4963 char *beg, *end, *str, *dst;
4965 for (beg = p + 1;;)
4967 end = strchr (beg, ',');
4968 if (end == NULL)
4969 end = strchr (beg, '\0');
4970 if ((size_t) (end - beg) < buflen)
4971 len += buflen + 1;
4972 else
4973 len += end - beg + 1;
4974 if (*end)
4975 beg = end + 1;
4976 else
4977 break;
4980 str = (char *) alloca (len);
4981 for (beg = p + 1, dst = str;;)
4983 const char *tem;
4984 bool mem_p, reg_p, inout_p;
4986 end = strchr (beg, ',');
4987 if (end)
4988 *end = '\0';
4989 beg[-1] = '=';
4990 tem = beg - 1;
4991 parse_output_constraint (&tem, i, 0, 0,
4992 &mem_p, &reg_p, &inout_p);
4993 if (dst != str)
4994 *dst++ = ',';
4995 if (reg_p)
4997 memcpy (dst, buf, buflen);
4998 dst += buflen;
5000 else
5002 if (end)
5003 len = end - beg;
5004 else
5005 len = strlen (beg);
5006 memcpy (dst, beg, len);
5007 dst += len;
5009 if (end)
5010 beg = end + 1;
5011 else
5012 break;
5014 *dst = '\0';
5015 input = build_string (dst - str, str);
5017 else
5018 input = build_string (strlen (buf), buf);
5020 else
5021 input = build_string (constraint_len - 1, constraint + 1);
5023 free (p);
5025 input = build_tree_list (build_tree_list (NULL_TREE, input),
5026 unshare_expr (TREE_VALUE (link)));
5027 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5031 link_next = NULL_TREE;
5032 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5034 link_next = TREE_CHAIN (link);
5035 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5036 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5037 oconstraints, &allows_mem, &allows_reg);
5039 /* If we can't make copies, we can only accept memory. */
5040 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5042 if (allows_mem)
5043 allows_reg = 0;
5044 else
5046 error ("impossible constraint in %<asm%>");
5047 error ("non-memory input %d must stay in memory", i);
5048 return GS_ERROR;
5052 /* If the operand is a memory input, it should be an lvalue. */
5053 if (!allows_reg && allows_mem)
5055 tree inputv = TREE_VALUE (link);
5056 STRIP_NOPS (inputv);
5057 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5058 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5059 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5060 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5061 TREE_VALUE (link) = error_mark_node;
5062 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5063 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5064 mark_addressable (TREE_VALUE (link));
5065 if (tret == GS_ERROR)
5067 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5068 input_location = EXPR_LOCATION (TREE_VALUE (link));
5069 error ("memory input %d is not directly addressable", i);
5070 ret = tret;
5073 else
5075 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5076 is_gimple_asm_val, fb_rvalue);
5077 if (tret == GS_ERROR)
5078 ret = tret;
5081 TREE_CHAIN (link) = NULL_TREE;
5082 vec_safe_push (inputs, link);
5085 link_next = NULL_TREE;
5086 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5088 link_next = TREE_CHAIN (link);
5089 TREE_CHAIN (link) = NULL_TREE;
5090 vec_safe_push (clobbers, link);
5093 link_next = NULL_TREE;
5094 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5096 link_next = TREE_CHAIN (link);
5097 TREE_CHAIN (link) = NULL_TREE;
5098 vec_safe_push (labels, link);
5101 /* Do not add ASMs with errors to the gimple IL stream. */
5102 if (ret != GS_ERROR)
5104 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5105 inputs, outputs, clobbers, labels);
5107 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5108 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5110 gimplify_seq_add_stmt (pre_p, stmt);
5113 return ret;
5116 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5117 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5118 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5119 return to this function.
5121 FIXME should we complexify the prequeue handling instead? Or use flags
5122 for all the cleanups and let the optimizer tighten them up? The current
5123 code seems pretty fragile; it will break on a cleanup within any
5124 non-conditional nesting. But any such nesting would be broken, anyway;
5125 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5126 and continues out of it. We can do that at the RTL level, though, so
5127 having an optimizer to tighten up try/finally regions would be a Good
5128 Thing. */
5130 static enum gimplify_status
5131 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5133 gimple_stmt_iterator iter;
5134 gimple_seq body_sequence = NULL;
5136 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5138 /* We only care about the number of conditions between the innermost
5139 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5140 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5141 int old_conds = gimplify_ctxp->conditions;
5142 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5143 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5144 gimplify_ctxp->conditions = 0;
5145 gimplify_ctxp->conditional_cleanups = NULL;
5146 gimplify_ctxp->in_cleanup_point_expr = true;
5148 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5150 gimplify_ctxp->conditions = old_conds;
5151 gimplify_ctxp->conditional_cleanups = old_cleanups;
5152 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5154 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5156 gimple wce = gsi_stmt (iter);
5158 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5160 if (gsi_one_before_end_p (iter))
5162 /* Note that gsi_insert_seq_before and gsi_remove do not
5163 scan operands, unlike some other sequence mutators. */
5164 if (!gimple_wce_cleanup_eh_only (wce))
5165 gsi_insert_seq_before_without_update (&iter,
5166 gimple_wce_cleanup (wce),
5167 GSI_SAME_STMT);
5168 gsi_remove (&iter, true);
5169 break;
5171 else
5173 gimple_statement_try *gtry;
5174 gimple_seq seq;
5175 enum gimple_try_flags kind;
5177 if (gimple_wce_cleanup_eh_only (wce))
5178 kind = GIMPLE_TRY_CATCH;
5179 else
5180 kind = GIMPLE_TRY_FINALLY;
5181 seq = gsi_split_seq_after (iter);
5183 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5184 /* Do not use gsi_replace here, as it may scan operands.
5185 We want to do a simple structural modification only. */
5186 gsi_set_stmt (&iter, gtry);
5187 iter = gsi_start (gtry->eval);
5190 else
5191 gsi_next (&iter);
5194 gimplify_seq_add_seq (pre_p, body_sequence);
5195 if (temp)
5197 *expr_p = temp;
5198 return GS_OK;
5200 else
5202 *expr_p = NULL;
5203 return GS_ALL_DONE;
5207 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5208 is the cleanup action required. EH_ONLY is true if the cleanup should
5209 only be executed if an exception is thrown, not on normal exit. */
5211 static void
5212 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5214 gimple wce;
5215 gimple_seq cleanup_stmts = NULL;
5217 /* Errors can result in improperly nested cleanups. Which results in
5218 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5219 if (seen_error ())
5220 return;
5222 if (gimple_conditional_context ())
5224 /* If we're in a conditional context, this is more complex. We only
5225 want to run the cleanup if we actually ran the initialization that
5226 necessitates it, but we want to run it after the end of the
5227 conditional context. So we wrap the try/finally around the
5228 condition and use a flag to determine whether or not to actually
5229 run the destructor. Thus
5231 test ? f(A()) : 0
5233 becomes (approximately)
5235 flag = 0;
5236 try {
5237 if (test) { A::A(temp); flag = 1; val = f(temp); }
5238 else { val = 0; }
5239 } finally {
5240 if (flag) A::~A(temp);
5244 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5245 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5246 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5248 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5249 gimplify_stmt (&cleanup, &cleanup_stmts);
5250 wce = gimple_build_wce (cleanup_stmts);
5252 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5253 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5254 gimplify_seq_add_stmt (pre_p, ftrue);
5256 /* Because of this manipulation, and the EH edges that jump
5257 threading cannot redirect, the temporary (VAR) will appear
5258 to be used uninitialized. Don't warn. */
5259 TREE_NO_WARNING (var) = 1;
5261 else
5263 gimplify_stmt (&cleanup, &cleanup_stmts);
5264 wce = gimple_build_wce (cleanup_stmts);
5265 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5266 gimplify_seq_add_stmt (pre_p, wce);
5270 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5272 static enum gimplify_status
5273 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5275 tree targ = *expr_p;
5276 tree temp = TARGET_EXPR_SLOT (targ);
5277 tree init = TARGET_EXPR_INITIAL (targ);
5278 enum gimplify_status ret;
5280 if (init)
5282 tree cleanup = NULL_TREE;
5284 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5285 to the temps list. Handle also variable length TARGET_EXPRs. */
5286 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5288 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5289 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5290 gimplify_vla_decl (temp, pre_p);
5292 else
5293 gimple_add_tmp_var (temp);
5295 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5296 expression is supposed to initialize the slot. */
5297 if (VOID_TYPE_P (TREE_TYPE (init)))
5298 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5299 else
5301 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5302 init = init_expr;
5303 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5304 init = NULL;
5305 ggc_free (init_expr);
5307 if (ret == GS_ERROR)
5309 /* PR c++/28266 Make sure this is expanded only once. */
5310 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5311 return GS_ERROR;
5313 if (init)
5314 gimplify_and_add (init, pre_p);
5316 /* If needed, push the cleanup for the temp. */
5317 if (TARGET_EXPR_CLEANUP (targ))
5319 if (CLEANUP_EH_ONLY (targ))
5320 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5321 CLEANUP_EH_ONLY (targ), pre_p);
5322 else
5323 cleanup = TARGET_EXPR_CLEANUP (targ);
5326 /* Add a clobber for the temporary going out of scope, like
5327 gimplify_bind_expr. */
5328 if (gimplify_ctxp->in_cleanup_point_expr
5329 && needs_to_live_in_memory (temp)
5330 && flag_stack_reuse == SR_ALL)
5332 tree clobber = build_constructor (TREE_TYPE (temp),
5333 NULL);
5334 TREE_THIS_VOLATILE (clobber) = true;
5335 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5336 if (cleanup)
5337 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5338 clobber);
5339 else
5340 cleanup = clobber;
5343 if (cleanup)
5344 gimple_push_cleanup (temp, cleanup, false, pre_p);
5346 /* Only expand this once. */
5347 TREE_OPERAND (targ, 3) = init;
5348 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5350 else
5351 /* We should have expanded this before. */
5352 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5354 *expr_p = temp;
5355 return GS_OK;
5358 /* Gimplification of expression trees. */
5360 /* Gimplify an expression which appears at statement context. The
5361 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5362 NULL, a new sequence is allocated.
5364 Return true if we actually added a statement to the queue. */
5366 bool
5367 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5369 gimple_seq_node last;
5371 last = gimple_seq_last (*seq_p);
5372 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5373 return last != gimple_seq_last (*seq_p);
5376 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5377 to CTX. If entries already exist, force them to be some flavor of private.
5378 If there is no enclosing parallel, do nothing. */
5380 void
5381 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5383 splay_tree_node n;
5385 if (decl == NULL || !DECL_P (decl))
5386 return;
5390 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5391 if (n != NULL)
5393 if (n->value & GOVD_SHARED)
5394 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5395 else if (n->value & GOVD_MAP)
5396 n->value |= GOVD_MAP_TO_ONLY;
5397 else
5398 return;
5400 else if (ctx->region_type == ORT_TARGET)
5401 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5402 else if (ctx->region_type != ORT_WORKSHARE
5403 && ctx->region_type != ORT_SIMD
5404 && ctx->region_type != ORT_TARGET_DATA)
5405 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5407 ctx = ctx->outer_context;
5409 while (ctx);
5412 /* Similarly for each of the type sizes of TYPE. */
5414 static void
5415 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5417 if (type == NULL || type == error_mark_node)
5418 return;
5419 type = TYPE_MAIN_VARIANT (type);
5421 if (pointer_set_insert (ctx->privatized_types, type))
5422 return;
5424 switch (TREE_CODE (type))
5426 case INTEGER_TYPE:
5427 case ENUMERAL_TYPE:
5428 case BOOLEAN_TYPE:
5429 case REAL_TYPE:
5430 case FIXED_POINT_TYPE:
5431 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5432 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5433 break;
5435 case ARRAY_TYPE:
5436 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5437 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5438 break;
5440 case RECORD_TYPE:
5441 case UNION_TYPE:
5442 case QUAL_UNION_TYPE:
5444 tree field;
5445 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5446 if (TREE_CODE (field) == FIELD_DECL)
5448 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5449 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5452 break;
5454 case POINTER_TYPE:
5455 case REFERENCE_TYPE:
5456 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5457 break;
5459 default:
5460 break;
5463 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5464 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5465 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5468 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5470 static void
5471 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5473 splay_tree_node n;
5474 unsigned int nflags;
5475 tree t;
5477 if (error_operand_p (decl))
5478 return;
5480 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5481 there are constructors involved somewhere. */
5482 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5483 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5484 flags |= GOVD_SEEN;
5486 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5487 if (n != NULL && n->value != GOVD_ALIGNED)
5489 /* We shouldn't be re-adding the decl with the same data
5490 sharing class. */
5491 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5492 /* The only combination of data sharing classes we should see is
5493 FIRSTPRIVATE and LASTPRIVATE. */
5494 nflags = n->value | flags;
5495 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5496 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5497 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5498 n->value = nflags;
5499 return;
5502 /* When adding a variable-sized variable, we have to handle all sorts
5503 of additional bits of data: the pointer replacement variable, and
5504 the parameters of the type. */
5505 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5507 /* Add the pointer replacement variable as PRIVATE if the variable
5508 replacement is private, else FIRSTPRIVATE since we'll need the
5509 address of the original variable either for SHARED, or for the
5510 copy into or out of the context. */
5511 if (!(flags & GOVD_LOCAL))
5513 nflags = flags & GOVD_MAP
5514 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5515 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5516 nflags |= flags & GOVD_SEEN;
5517 t = DECL_VALUE_EXPR (decl);
5518 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5519 t = TREE_OPERAND (t, 0);
5520 gcc_assert (DECL_P (t));
5521 omp_add_variable (ctx, t, nflags);
5524 /* Add all of the variable and type parameters (which should have
5525 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5526 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5527 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5528 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5530 /* The variable-sized variable itself is never SHARED, only some form
5531 of PRIVATE. The sharing would take place via the pointer variable
5532 which we remapped above. */
5533 if (flags & GOVD_SHARED)
5534 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5535 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5537 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5538 alloca statement we generate for the variable, so make sure it
5539 is available. This isn't automatically needed for the SHARED
5540 case, since we won't be allocating local storage then.
5541 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5542 in this case omp_notice_variable will be called later
5543 on when it is gimplified. */
5544 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5545 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5546 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5548 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5549 && lang_hooks.decls.omp_privatize_by_reference (decl))
5551 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5553 /* Similar to the direct variable sized case above, we'll need the
5554 size of references being privatized. */
5555 if ((flags & GOVD_SHARED) == 0)
5557 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5558 if (TREE_CODE (t) != INTEGER_CST)
5559 omp_notice_variable (ctx, t, true);
5563 if (n != NULL)
5564 n->value |= flags;
5565 else
5566 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5569 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5570 This just prints out diagnostics about threadprivate variable uses
5571 in untied tasks. If DECL2 is non-NULL, prevent this warning
5572 on that variable. */
5574 static bool
5575 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5576 tree decl2)
5578 splay_tree_node n;
5579 struct gimplify_omp_ctx *octx;
5581 for (octx = ctx; octx; octx = octx->outer_context)
5582 if (octx->region_type == ORT_TARGET)
5584 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5585 if (n == NULL)
5587 error ("threadprivate variable %qE used in target region",
5588 DECL_NAME (decl));
5589 error_at (octx->location, "enclosing target region");
5590 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5592 if (decl2)
5593 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5596 if (ctx->region_type != ORT_UNTIED_TASK)
5597 return false;
5598 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5599 if (n == NULL)
5601 error ("threadprivate variable %qE used in untied task",
5602 DECL_NAME (decl));
5603 error_at (ctx->location, "enclosing task");
5604 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5606 if (decl2)
5607 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5608 return false;
5611 /* Record the fact that DECL was used within the OpenMP context CTX.
5612 IN_CODE is true when real code uses DECL, and false when we should
5613 merely emit default(none) errors. Return true if DECL is going to
5614 be remapped and thus DECL shouldn't be gimplified into its
5615 DECL_VALUE_EXPR (if any). */
5617 static bool
5618 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5620 splay_tree_node n;
5621 unsigned flags = in_code ? GOVD_SEEN : 0;
5622 bool ret = false, shared;
5624 if (error_operand_p (decl))
5625 return false;
5627 /* Threadprivate variables are predetermined. */
5628 if (is_global_var (decl))
5630 if (DECL_THREAD_LOCAL_P (decl))
5631 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5633 if (DECL_HAS_VALUE_EXPR_P (decl))
5635 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5637 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5638 return omp_notice_threadprivate_variable (ctx, decl, value);
5642 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5643 if (ctx->region_type == ORT_TARGET)
5645 if (n == NULL)
5647 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5649 error ("%qD referenced in target region does not have "
5650 "a mappable type", decl);
5651 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5653 else
5654 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5656 else
5657 n->value |= flags;
5658 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5659 goto do_outer;
5662 if (n == NULL)
5664 enum omp_clause_default_kind default_kind, kind;
5665 struct gimplify_omp_ctx *octx;
5667 if (ctx->region_type == ORT_WORKSHARE
5668 || ctx->region_type == ORT_SIMD
5669 || ctx->region_type == ORT_TARGET_DATA)
5670 goto do_outer;
5672 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5673 remapped firstprivate instead of shared. To some extent this is
5674 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5675 default_kind = ctx->default_kind;
5676 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5677 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5678 default_kind = kind;
5680 switch (default_kind)
5682 case OMP_CLAUSE_DEFAULT_NONE:
5683 if ((ctx->region_type & ORT_TASK) != 0)
5685 error ("%qE not specified in enclosing task",
5686 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5687 error_at (ctx->location, "enclosing task");
5689 else if (ctx->region_type == ORT_TEAMS)
5691 error ("%qE not specified in enclosing teams construct",
5692 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5693 error_at (ctx->location, "enclosing teams construct");
5695 else
5697 error ("%qE not specified in enclosing parallel",
5698 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5699 error_at (ctx->location, "enclosing parallel");
5701 /* FALLTHRU */
5702 case OMP_CLAUSE_DEFAULT_SHARED:
5703 flags |= GOVD_SHARED;
5704 break;
5705 case OMP_CLAUSE_DEFAULT_PRIVATE:
5706 flags |= GOVD_PRIVATE;
5707 break;
5708 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5709 flags |= GOVD_FIRSTPRIVATE;
5710 break;
5711 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5712 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5713 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5714 if (ctx->outer_context)
5715 omp_notice_variable (ctx->outer_context, decl, in_code);
5716 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5718 splay_tree_node n2;
5720 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5721 continue;
5722 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5723 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5725 flags |= GOVD_FIRSTPRIVATE;
5726 break;
5728 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5729 break;
5731 if (flags & GOVD_FIRSTPRIVATE)
5732 break;
5733 if (octx == NULL
5734 && (TREE_CODE (decl) == PARM_DECL
5735 || (!is_global_var (decl)
5736 && DECL_CONTEXT (decl) == current_function_decl)))
5738 flags |= GOVD_FIRSTPRIVATE;
5739 break;
5741 flags |= GOVD_SHARED;
5742 break;
5743 default:
5744 gcc_unreachable ();
5747 if ((flags & GOVD_PRIVATE)
5748 && lang_hooks.decls.omp_private_outer_ref (decl))
5749 flags |= GOVD_PRIVATE_OUTER_REF;
5751 omp_add_variable (ctx, decl, flags);
5753 shared = (flags & GOVD_SHARED) != 0;
5754 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5755 goto do_outer;
5758 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5759 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5760 && DECL_SIZE (decl)
5761 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5763 splay_tree_node n2;
5764 tree t = DECL_VALUE_EXPR (decl);
5765 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5766 t = TREE_OPERAND (t, 0);
5767 gcc_assert (DECL_P (t));
5768 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5769 n2->value |= GOVD_SEEN;
5772 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5773 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5775 /* If nothing changed, there's nothing left to do. */
5776 if ((n->value & flags) == flags)
5777 return ret;
5778 flags |= n->value;
5779 n->value = flags;
5781 do_outer:
5782 /* If the variable is private in the current context, then we don't
5783 need to propagate anything to an outer context. */
5784 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5785 return ret;
5786 if (ctx->outer_context
5787 && omp_notice_variable (ctx->outer_context, decl, in_code))
5788 return true;
5789 return ret;
5792 /* Verify that DECL is private within CTX. If there's specific information
5793 to the contrary in the innermost scope, generate an error. */
5795 static bool
5796 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, bool simd)
5798 splay_tree_node n;
5800 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5801 if (n != NULL)
5803 if (n->value & GOVD_SHARED)
5805 if (ctx == gimplify_omp_ctxp)
5807 if (simd)
5808 error ("iteration variable %qE is predetermined linear",
5809 DECL_NAME (decl));
5810 else
5811 error ("iteration variable %qE should be private",
5812 DECL_NAME (decl));
5813 n->value = GOVD_PRIVATE;
5814 return true;
5816 else
5817 return false;
5819 else if ((n->value & GOVD_EXPLICIT) != 0
5820 && (ctx == gimplify_omp_ctxp
5821 || (ctx->region_type == ORT_COMBINED_PARALLEL
5822 && gimplify_omp_ctxp->outer_context == ctx)))
5824 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5825 error ("iteration variable %qE should not be firstprivate",
5826 DECL_NAME (decl));
5827 else if ((n->value & GOVD_REDUCTION) != 0)
5828 error ("iteration variable %qE should not be reduction",
5829 DECL_NAME (decl));
5830 else if (simd && (n->value & GOVD_LASTPRIVATE) != 0)
5831 error ("iteration variable %qE should not be lastprivate",
5832 DECL_NAME (decl));
5833 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5834 error ("iteration variable %qE should not be private",
5835 DECL_NAME (decl));
5836 else if (simd && (n->value & GOVD_LINEAR) != 0)
5837 error ("iteration variable %qE is predetermined linear",
5838 DECL_NAME (decl));
5840 return (ctx == gimplify_omp_ctxp
5841 || (ctx->region_type == ORT_COMBINED_PARALLEL
5842 && gimplify_omp_ctxp->outer_context == ctx));
5845 if (ctx->region_type != ORT_WORKSHARE
5846 && ctx->region_type != ORT_SIMD)
5847 return false;
5848 else if (ctx->outer_context)
5849 return omp_is_private (ctx->outer_context, decl, simd);
5850 return false;
5853 /* Return true if DECL is private within a parallel region
5854 that binds to the current construct's context or in parallel
5855 region's REDUCTION clause. */
5857 static bool
5858 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5860 splay_tree_node n;
5864 ctx = ctx->outer_context;
5865 if (ctx == NULL)
5866 return !(is_global_var (decl)
5867 /* References might be private, but might be shared too,
5868 when checking for copyprivate, assume they might be
5869 private, otherwise assume they might be shared. */
5870 || (!copyprivate
5871 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5873 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5874 continue;
5876 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5877 if (n != NULL)
5878 return (n->value & GOVD_SHARED) == 0;
5880 while (ctx->region_type == ORT_WORKSHARE
5881 || ctx->region_type == ORT_SIMD);
5882 return false;
5885 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5886 and previous omp contexts. */
5888 static void
5889 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5890 enum omp_region_type region_type)
5892 struct gimplify_omp_ctx *ctx, *outer_ctx;
5893 tree c;
5895 ctx = new_omp_context (region_type);
5896 outer_ctx = ctx->outer_context;
5898 while ((c = *list_p) != NULL)
5900 bool remove = false;
5901 bool notice_outer = true;
5902 const char *check_non_private = NULL;
5903 unsigned int flags;
5904 tree decl;
5906 switch (OMP_CLAUSE_CODE (c))
5908 case OMP_CLAUSE_PRIVATE:
5909 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5910 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5912 flags |= GOVD_PRIVATE_OUTER_REF;
5913 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5915 else
5916 notice_outer = false;
5917 goto do_add;
5918 case OMP_CLAUSE_SHARED:
5919 flags = GOVD_SHARED | GOVD_EXPLICIT;
5920 goto do_add;
5921 case OMP_CLAUSE_FIRSTPRIVATE:
5922 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5923 check_non_private = "firstprivate";
5924 goto do_add;
5925 case OMP_CLAUSE_LASTPRIVATE:
5926 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5927 check_non_private = "lastprivate";
5928 goto do_add;
5929 case OMP_CLAUSE_REDUCTION:
5930 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5931 check_non_private = "reduction";
5932 goto do_add;
5933 case OMP_CLAUSE_LINEAR:
5934 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5935 is_gimple_val, fb_rvalue) == GS_ERROR)
5937 remove = true;
5938 break;
5940 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5941 goto do_add;
5943 case OMP_CLAUSE_MAP:
5944 if (OMP_CLAUSE_SIZE (c)
5945 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5946 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5948 remove = true;
5949 break;
5951 decl = OMP_CLAUSE_DECL (c);
5952 if (!DECL_P (decl))
5954 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
5955 NULL, is_gimple_lvalue, fb_lvalue)
5956 == GS_ERROR)
5958 remove = true;
5959 break;
5961 break;
5963 flags = GOVD_MAP | GOVD_EXPLICIT;
5964 goto do_add;
5966 case OMP_CLAUSE_DEPEND:
5967 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
5969 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
5970 NULL, is_gimple_val, fb_rvalue);
5971 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
5973 if (error_operand_p (OMP_CLAUSE_DECL (c)))
5975 remove = true;
5976 break;
5978 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
5979 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
5980 is_gimple_val, fb_rvalue) == GS_ERROR)
5982 remove = true;
5983 break;
5985 break;
5987 case OMP_CLAUSE_TO:
5988 case OMP_CLAUSE_FROM:
5989 if (OMP_CLAUSE_SIZE (c)
5990 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5991 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5993 remove = true;
5994 break;
5996 decl = OMP_CLAUSE_DECL (c);
5997 if (error_operand_p (decl))
5999 remove = true;
6000 break;
6002 if (!DECL_P (decl))
6004 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6005 NULL, is_gimple_lvalue, fb_lvalue)
6006 == GS_ERROR)
6008 remove = true;
6009 break;
6011 break;
6013 goto do_notice;
6015 do_add:
6016 decl = OMP_CLAUSE_DECL (c);
6017 if (error_operand_p (decl))
6019 remove = true;
6020 break;
6022 omp_add_variable (ctx, decl, flags);
6023 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6024 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6026 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6027 GOVD_LOCAL | GOVD_SEEN);
6028 gimplify_omp_ctxp = ctx;
6029 push_gimplify_context ();
6031 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6032 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6034 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6035 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6036 pop_gimplify_context
6037 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6038 push_gimplify_context ();
6039 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6040 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6041 pop_gimplify_context
6042 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6043 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6044 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6046 gimplify_omp_ctxp = outer_ctx;
6048 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6049 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6051 gimplify_omp_ctxp = ctx;
6052 push_gimplify_context ();
6053 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6055 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6056 NULL, NULL);
6057 TREE_SIDE_EFFECTS (bind) = 1;
6058 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6059 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6061 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6062 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6063 pop_gimplify_context
6064 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6065 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6067 gimplify_omp_ctxp = outer_ctx;
6069 if (notice_outer)
6070 goto do_notice;
6071 break;
6073 case OMP_CLAUSE_COPYIN:
6074 case OMP_CLAUSE_COPYPRIVATE:
6075 decl = OMP_CLAUSE_DECL (c);
6076 if (error_operand_p (decl))
6078 remove = true;
6079 break;
6081 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6082 && !remove
6083 && !omp_check_private (ctx, decl, true))
6085 remove = true;
6086 if (is_global_var (decl))
6088 if (DECL_THREAD_LOCAL_P (decl))
6089 remove = false;
6090 else if (DECL_HAS_VALUE_EXPR_P (decl))
6092 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6094 if (value
6095 && DECL_P (value)
6096 && DECL_THREAD_LOCAL_P (value))
6097 remove = false;
6100 if (remove)
6101 error_at (OMP_CLAUSE_LOCATION (c),
6102 "copyprivate variable %qE is not threadprivate"
6103 " or private in outer context", DECL_NAME (decl));
6105 do_notice:
6106 if (outer_ctx)
6107 omp_notice_variable (outer_ctx, decl, true);
6108 if (check_non_private
6109 && region_type == ORT_WORKSHARE
6110 && omp_check_private (ctx, decl, false))
6112 error ("%s variable %qE is private in outer context",
6113 check_non_private, DECL_NAME (decl));
6114 remove = true;
6116 break;
6118 case OMP_CLAUSE_FINAL:
6119 case OMP_CLAUSE_IF:
6120 OMP_CLAUSE_OPERAND (c, 0)
6121 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6122 /* Fall through. */
6124 case OMP_CLAUSE_SCHEDULE:
6125 case OMP_CLAUSE_NUM_THREADS:
6126 case OMP_CLAUSE_NUM_TEAMS:
6127 case OMP_CLAUSE_THREAD_LIMIT:
6128 case OMP_CLAUSE_DIST_SCHEDULE:
6129 case OMP_CLAUSE_DEVICE:
6130 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6131 is_gimple_val, fb_rvalue) == GS_ERROR)
6132 remove = true;
6133 break;
6135 case OMP_CLAUSE_NOWAIT:
6136 case OMP_CLAUSE_ORDERED:
6137 case OMP_CLAUSE_UNTIED:
6138 case OMP_CLAUSE_COLLAPSE:
6139 case OMP_CLAUSE_MERGEABLE:
6140 case OMP_CLAUSE_PROC_BIND:
6141 case OMP_CLAUSE_SAFELEN:
6142 break;
6144 case OMP_CLAUSE_ALIGNED:
6145 decl = OMP_CLAUSE_DECL (c);
6146 if (error_operand_p (decl))
6148 remove = true;
6149 break;
6151 if (!is_global_var (decl)
6152 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6153 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6154 break;
6156 case OMP_CLAUSE_DEFAULT:
6157 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6158 break;
6160 default:
6161 gcc_unreachable ();
6164 if (remove)
6165 *list_p = OMP_CLAUSE_CHAIN (c);
6166 else
6167 list_p = &OMP_CLAUSE_CHAIN (c);
6170 gimplify_omp_ctxp = ctx;
6173 /* For all variables that were not actually used within the context,
6174 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6176 static int
6177 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6179 tree *list_p = (tree *) data;
6180 tree decl = (tree) n->key;
6181 unsigned flags = n->value;
6182 enum omp_clause_code code;
6183 tree clause;
6184 bool private_debug;
6186 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6187 return 0;
6188 if ((flags & GOVD_SEEN) == 0)
6189 return 0;
6190 if (flags & GOVD_DEBUG_PRIVATE)
6192 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6193 private_debug = true;
6195 else if (flags & GOVD_MAP)
6196 private_debug = false;
6197 else
6198 private_debug
6199 = lang_hooks.decls.omp_private_debug_clause (decl,
6200 !!(flags & GOVD_SHARED));
6201 if (private_debug)
6202 code = OMP_CLAUSE_PRIVATE;
6203 else if (flags & GOVD_MAP)
6204 code = OMP_CLAUSE_MAP;
6205 else if (flags & GOVD_SHARED)
6207 if (is_global_var (decl))
6209 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6210 while (ctx != NULL)
6212 splay_tree_node on
6213 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6214 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6215 | GOVD_PRIVATE | GOVD_REDUCTION
6216 | GOVD_LINEAR)) != 0)
6217 break;
6218 ctx = ctx->outer_context;
6220 if (ctx == NULL)
6221 return 0;
6223 code = OMP_CLAUSE_SHARED;
6225 else if (flags & GOVD_PRIVATE)
6226 code = OMP_CLAUSE_PRIVATE;
6227 else if (flags & GOVD_FIRSTPRIVATE)
6228 code = OMP_CLAUSE_FIRSTPRIVATE;
6229 else if (flags & GOVD_LASTPRIVATE)
6230 code = OMP_CLAUSE_LASTPRIVATE;
6231 else if (flags & GOVD_ALIGNED)
6232 return 0;
6233 else
6234 gcc_unreachable ();
6236 clause = build_omp_clause (input_location, code);
6237 OMP_CLAUSE_DECL (clause) = decl;
6238 OMP_CLAUSE_CHAIN (clause) = *list_p;
6239 if (private_debug)
6240 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6241 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6242 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6243 else if (code == OMP_CLAUSE_MAP)
6245 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6246 ? OMP_CLAUSE_MAP_TO
6247 : OMP_CLAUSE_MAP_TOFROM;
6248 if (DECL_SIZE (decl)
6249 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6251 tree decl2 = DECL_VALUE_EXPR (decl);
6252 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6253 decl2 = TREE_OPERAND (decl2, 0);
6254 gcc_assert (DECL_P (decl2));
6255 tree mem = build_simple_mem_ref (decl2);
6256 OMP_CLAUSE_DECL (clause) = mem;
6257 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6258 if (gimplify_omp_ctxp->outer_context)
6260 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6261 omp_notice_variable (ctx, decl2, true);
6262 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6264 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6265 OMP_CLAUSE_MAP);
6266 OMP_CLAUSE_DECL (nc) = decl;
6267 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6268 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6269 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6270 OMP_CLAUSE_CHAIN (clause) = nc;
6273 *list_p = clause;
6274 lang_hooks.decls.omp_finish_clause (clause);
6276 return 0;
6279 static void
6280 gimplify_adjust_omp_clauses (tree *list_p)
6282 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6283 tree c, decl;
6285 while ((c = *list_p) != NULL)
6287 splay_tree_node n;
6288 bool remove = false;
6290 switch (OMP_CLAUSE_CODE (c))
6292 case OMP_CLAUSE_PRIVATE:
6293 case OMP_CLAUSE_SHARED:
6294 case OMP_CLAUSE_FIRSTPRIVATE:
6295 case OMP_CLAUSE_LINEAR:
6296 decl = OMP_CLAUSE_DECL (c);
6297 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6298 remove = !(n->value & GOVD_SEEN);
6299 if (! remove)
6301 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6302 if ((n->value & GOVD_DEBUG_PRIVATE)
6303 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6305 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6306 || ((n->value & GOVD_DATA_SHARE_CLASS)
6307 == GOVD_PRIVATE));
6308 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6309 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6311 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6312 && ctx->outer_context
6313 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6314 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6315 && !is_global_var (decl))
6317 if (ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
6319 n = splay_tree_lookup (ctx->outer_context->variables,
6320 (splay_tree_key) decl);
6321 if (n == NULL
6322 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6324 int flags = OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6325 ? GOVD_LASTPRIVATE : GOVD_SHARED;
6326 if (n == NULL)
6327 omp_add_variable (ctx->outer_context, decl,
6328 flags | GOVD_SEEN);
6329 else
6330 n->value |= flags | GOVD_SEEN;
6333 else
6334 omp_notice_variable (ctx->outer_context, decl, true);
6337 break;
6339 case OMP_CLAUSE_LASTPRIVATE:
6340 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6341 accurately reflect the presence of a FIRSTPRIVATE clause. */
6342 decl = OMP_CLAUSE_DECL (c);
6343 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6344 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6345 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6346 break;
6348 case OMP_CLAUSE_ALIGNED:
6349 decl = OMP_CLAUSE_DECL (c);
6350 if (!is_global_var (decl))
6352 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6353 remove = n == NULL || !(n->value & GOVD_SEEN);
6354 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6356 struct gimplify_omp_ctx *octx;
6357 if (n != NULL
6358 && (n->value & (GOVD_DATA_SHARE_CLASS
6359 & ~GOVD_FIRSTPRIVATE)))
6360 remove = true;
6361 else
6362 for (octx = ctx->outer_context; octx;
6363 octx = octx->outer_context)
6365 n = splay_tree_lookup (octx->variables,
6366 (splay_tree_key) decl);
6367 if (n == NULL)
6368 continue;
6369 if (n->value & GOVD_LOCAL)
6370 break;
6371 /* We have to avoid assigning a shared variable
6372 to itself when trying to add
6373 __builtin_assume_aligned. */
6374 if (n->value & GOVD_SHARED)
6376 remove = true;
6377 break;
6382 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6384 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6385 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6386 remove = true;
6388 break;
6390 case OMP_CLAUSE_MAP:
6391 decl = OMP_CLAUSE_DECL (c);
6392 if (!DECL_P (decl))
6393 break;
6394 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6395 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6396 remove = true;
6397 else if (DECL_SIZE (decl)
6398 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6399 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6401 tree decl2 = DECL_VALUE_EXPR (decl);
6402 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6403 decl2 = TREE_OPERAND (decl2, 0);
6404 gcc_assert (DECL_P (decl2));
6405 tree mem = build_simple_mem_ref (decl2);
6406 OMP_CLAUSE_DECL (c) = mem;
6407 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6408 if (ctx->outer_context)
6410 omp_notice_variable (ctx->outer_context, decl2, true);
6411 omp_notice_variable (ctx->outer_context,
6412 OMP_CLAUSE_SIZE (c), true);
6414 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6415 OMP_CLAUSE_MAP);
6416 OMP_CLAUSE_DECL (nc) = decl;
6417 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6418 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6419 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6420 OMP_CLAUSE_CHAIN (c) = nc;
6421 c = nc;
6423 break;
6425 case OMP_CLAUSE_TO:
6426 case OMP_CLAUSE_FROM:
6427 decl = OMP_CLAUSE_DECL (c);
6428 if (!DECL_P (decl))
6429 break;
6430 if (DECL_SIZE (decl)
6431 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6433 tree decl2 = DECL_VALUE_EXPR (decl);
6434 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6435 decl2 = TREE_OPERAND (decl2, 0);
6436 gcc_assert (DECL_P (decl2));
6437 tree mem = build_simple_mem_ref (decl2);
6438 OMP_CLAUSE_DECL (c) = mem;
6439 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6440 if (ctx->outer_context)
6442 omp_notice_variable (ctx->outer_context, decl2, true);
6443 omp_notice_variable (ctx->outer_context,
6444 OMP_CLAUSE_SIZE (c), true);
6447 break;
6449 case OMP_CLAUSE_REDUCTION:
6450 case OMP_CLAUSE_COPYIN:
6451 case OMP_CLAUSE_COPYPRIVATE:
6452 case OMP_CLAUSE_IF:
6453 case OMP_CLAUSE_NUM_THREADS:
6454 case OMP_CLAUSE_NUM_TEAMS:
6455 case OMP_CLAUSE_THREAD_LIMIT:
6456 case OMP_CLAUSE_DIST_SCHEDULE:
6457 case OMP_CLAUSE_DEVICE:
6458 case OMP_CLAUSE_SCHEDULE:
6459 case OMP_CLAUSE_NOWAIT:
6460 case OMP_CLAUSE_ORDERED:
6461 case OMP_CLAUSE_DEFAULT:
6462 case OMP_CLAUSE_UNTIED:
6463 case OMP_CLAUSE_COLLAPSE:
6464 case OMP_CLAUSE_FINAL:
6465 case OMP_CLAUSE_MERGEABLE:
6466 case OMP_CLAUSE_PROC_BIND:
6467 case OMP_CLAUSE_SAFELEN:
6468 case OMP_CLAUSE_DEPEND:
6469 break;
6471 default:
6472 gcc_unreachable ();
6475 if (remove)
6476 *list_p = OMP_CLAUSE_CHAIN (c);
6477 else
6478 list_p = &OMP_CLAUSE_CHAIN (c);
6481 /* Add in any implicit data sharing. */
6482 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6484 gimplify_omp_ctxp = ctx->outer_context;
6485 delete_omp_context (ctx);
6488 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6489 gimplification of the body, as well as scanning the body for used
6490 variables. We need to do this scan now, because variable-sized
6491 decls will be decomposed during gimplification. */
6493 static void
6494 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6496 tree expr = *expr_p;
6497 gimple g;
6498 gimple_seq body = NULL;
6500 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6501 OMP_PARALLEL_COMBINED (expr)
6502 ? ORT_COMBINED_PARALLEL
6503 : ORT_PARALLEL);
6505 push_gimplify_context ();
6507 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6508 if (gimple_code (g) == GIMPLE_BIND)
6509 pop_gimplify_context (g);
6510 else
6511 pop_gimplify_context (NULL);
6513 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6515 g = gimple_build_omp_parallel (body,
6516 OMP_PARALLEL_CLAUSES (expr),
6517 NULL_TREE, NULL_TREE);
6518 if (OMP_PARALLEL_COMBINED (expr))
6519 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6520 gimplify_seq_add_stmt (pre_p, g);
6521 *expr_p = NULL_TREE;
6524 /* Gimplify the contents of an OMP_TASK statement. This involves
6525 gimplification of the body, as well as scanning the body for used
6526 variables. We need to do this scan now, because variable-sized
6527 decls will be decomposed during gimplification. */
6529 static void
6530 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6532 tree expr = *expr_p;
6533 gimple g;
6534 gimple_seq body = NULL;
6536 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6537 find_omp_clause (OMP_TASK_CLAUSES (expr),
6538 OMP_CLAUSE_UNTIED)
6539 ? ORT_UNTIED_TASK : ORT_TASK);
6541 push_gimplify_context ();
6543 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6544 if (gimple_code (g) == GIMPLE_BIND)
6545 pop_gimplify_context (g);
6546 else
6547 pop_gimplify_context (NULL);
6549 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6551 g = gimple_build_omp_task (body,
6552 OMP_TASK_CLAUSES (expr),
6553 NULL_TREE, NULL_TREE,
6554 NULL_TREE, NULL_TREE, NULL_TREE);
6555 gimplify_seq_add_stmt (pre_p, g);
6556 *expr_p = NULL_TREE;
6559 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6560 with non-NULL OMP_FOR_INIT. */
6562 static tree
6563 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6565 *walk_subtrees = 0;
6566 switch (TREE_CODE (*tp))
6568 case OMP_FOR:
6569 *walk_subtrees = 1;
6570 /* FALLTHRU */
6571 case OMP_SIMD:
6572 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6573 return *tp;
6574 break;
6575 case BIND_EXPR:
6576 case STATEMENT_LIST:
6577 case OMP_PARALLEL:
6578 *walk_subtrees = 1;
6579 break;
6580 default:
6581 break;
6583 return NULL_TREE;
6586 /* Gimplify the gross structure of an OMP_FOR statement. */
6588 static enum gimplify_status
6589 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6591 tree for_stmt, orig_for_stmt, decl, var, t;
6592 enum gimplify_status ret = GS_ALL_DONE;
6593 enum gimplify_status tret;
6594 gimple gfor;
6595 gimple_seq for_body, for_pre_body;
6596 int i;
6597 bool simd;
6598 bitmap has_decl_expr = NULL;
6600 orig_for_stmt = for_stmt = *expr_p;
6602 simd = TREE_CODE (for_stmt) == OMP_SIMD
6603 || TREE_CODE (for_stmt) == CILK_SIMD;
6604 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6605 simd ? ORT_SIMD : ORT_WORKSHARE);
6607 /* Handle OMP_FOR_INIT. */
6608 for_pre_body = NULL;
6609 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6611 has_decl_expr = BITMAP_ALLOC (NULL);
6612 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6613 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6614 == VAR_DECL)
6616 t = OMP_FOR_PRE_BODY (for_stmt);
6617 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6619 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6621 tree_stmt_iterator si;
6622 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6623 tsi_next (&si))
6625 t = tsi_stmt (si);
6626 if (TREE_CODE (t) == DECL_EXPR
6627 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6628 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6632 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6633 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6635 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6637 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6638 NULL, NULL);
6639 gcc_assert (for_stmt != NULL_TREE);
6640 gimplify_omp_ctxp->combined_loop = true;
6643 for_body = NULL;
6644 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6645 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6646 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6647 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6648 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6650 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6651 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6652 decl = TREE_OPERAND (t, 0);
6653 gcc_assert (DECL_P (decl));
6654 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6655 || POINTER_TYPE_P (TREE_TYPE (decl)));
6657 /* Make sure the iteration variable is private. */
6658 tree c = NULL_TREE;
6659 if (orig_for_stmt != for_stmt)
6660 /* Do this only on innermost construct for combined ones. */;
6661 else if (simd)
6663 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6664 (splay_tree_key)decl);
6665 omp_is_private (gimplify_omp_ctxp, decl, simd);
6666 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6667 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6668 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6670 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6671 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6672 if (has_decl_expr
6673 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6674 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6675 OMP_CLAUSE_DECL (c) = decl;
6676 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6677 OMP_FOR_CLAUSES (for_stmt) = c;
6678 omp_add_variable (gimplify_omp_ctxp, decl,
6679 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6681 else
6683 bool lastprivate
6684 = (!has_decl_expr
6685 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6686 c = build_omp_clause (input_location,
6687 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6688 : OMP_CLAUSE_PRIVATE);
6689 OMP_CLAUSE_DECL (c) = decl;
6690 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6691 omp_add_variable (gimplify_omp_ctxp, decl,
6692 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6693 | GOVD_SEEN);
6694 c = NULL_TREE;
6697 else if (omp_is_private (gimplify_omp_ctxp, decl, simd))
6698 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6699 else
6700 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6702 /* If DECL is not a gimple register, create a temporary variable to act
6703 as an iteration counter. This is valid, since DECL cannot be
6704 modified in the body of the loop. */
6705 if (orig_for_stmt != for_stmt)
6706 var = decl;
6707 else if (!is_gimple_reg (decl))
6709 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6710 TREE_OPERAND (t, 0) = var;
6712 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6714 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6716 else
6717 var = decl;
6719 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6720 is_gimple_val, fb_rvalue);
6721 ret = MIN (ret, tret);
6722 if (ret == GS_ERROR)
6723 return ret;
6725 /* Handle OMP_FOR_COND. */
6726 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6727 gcc_assert (COMPARISON_CLASS_P (t));
6728 gcc_assert (TREE_OPERAND (t, 0) == decl);
6730 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6731 is_gimple_val, fb_rvalue);
6732 ret = MIN (ret, tret);
6734 /* Handle OMP_FOR_INCR. */
6735 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6736 switch (TREE_CODE (t))
6738 case PREINCREMENT_EXPR:
6739 case POSTINCREMENT_EXPR:
6741 tree decl = TREE_OPERAND (t, 0);
6742 // c_omp_for_incr_canonicalize_ptr() should have been
6743 // called to massage things appropriately.
6744 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6746 if (orig_for_stmt != for_stmt)
6747 break;
6748 t = build_int_cst (TREE_TYPE (decl), 1);
6749 if (c)
6750 OMP_CLAUSE_LINEAR_STEP (c) = t;
6751 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6752 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6753 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6754 break;
6757 case PREDECREMENT_EXPR:
6758 case POSTDECREMENT_EXPR:
6759 if (orig_for_stmt != for_stmt)
6760 break;
6761 t = build_int_cst (TREE_TYPE (decl), -1);
6762 if (c)
6763 OMP_CLAUSE_LINEAR_STEP (c) = t;
6764 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6765 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6766 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6767 break;
6769 case MODIFY_EXPR:
6770 gcc_assert (TREE_OPERAND (t, 0) == decl);
6771 TREE_OPERAND (t, 0) = var;
6773 t = TREE_OPERAND (t, 1);
6774 switch (TREE_CODE (t))
6776 case PLUS_EXPR:
6777 if (TREE_OPERAND (t, 1) == decl)
6779 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6780 TREE_OPERAND (t, 0) = var;
6781 break;
6784 /* Fallthru. */
6785 case MINUS_EXPR:
6786 case POINTER_PLUS_EXPR:
6787 gcc_assert (TREE_OPERAND (t, 0) == decl);
6788 TREE_OPERAND (t, 0) = var;
6789 break;
6790 default:
6791 gcc_unreachable ();
6794 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6795 is_gimple_val, fb_rvalue);
6796 ret = MIN (ret, tret);
6797 if (c)
6799 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
6800 if (TREE_CODE (t) == MINUS_EXPR)
6802 t = TREE_OPERAND (t, 1);
6803 OMP_CLAUSE_LINEAR_STEP (c)
6804 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
6805 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6806 &for_pre_body, NULL,
6807 is_gimple_val, fb_rvalue);
6808 ret = MIN (ret, tret);
6811 break;
6813 default:
6814 gcc_unreachable ();
6817 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6818 && orig_for_stmt == for_stmt)
6820 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6821 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6822 && OMP_CLAUSE_DECL (c) == decl
6823 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6825 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6826 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6827 gcc_assert (TREE_OPERAND (t, 0) == var);
6828 t = TREE_OPERAND (t, 1);
6829 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6830 || TREE_CODE (t) == MINUS_EXPR
6831 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6832 gcc_assert (TREE_OPERAND (t, 0) == var);
6833 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6834 TREE_OPERAND (t, 1));
6835 gimplify_assign (decl, t,
6836 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6841 BITMAP_FREE (has_decl_expr);
6843 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
6845 if (orig_for_stmt != for_stmt)
6846 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6848 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6849 decl = TREE_OPERAND (t, 0);
6850 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6851 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6852 TREE_OPERAND (t, 0) = var;
6853 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6854 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
6855 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
6858 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
6860 int kind;
6861 switch (TREE_CODE (orig_for_stmt))
6863 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
6864 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
6865 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
6866 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
6867 default:
6868 gcc_unreachable ();
6870 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
6871 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6872 for_pre_body);
6873 if (orig_for_stmt != for_stmt)
6874 gimple_omp_for_set_combined_p (gfor, true);
6875 if (gimplify_omp_ctxp
6876 && (gimplify_omp_ctxp->combined_loop
6877 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
6878 && gimplify_omp_ctxp->outer_context
6879 && gimplify_omp_ctxp->outer_context->combined_loop)))
6881 gimple_omp_for_set_combined_into_p (gfor, true);
6882 if (gimplify_omp_ctxp->combined_loop)
6883 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
6884 else
6885 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
6888 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6890 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6891 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6892 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6893 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6894 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6895 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6896 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6897 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6900 gimplify_seq_add_stmt (pre_p, gfor);
6901 if (ret != GS_ALL_DONE)
6902 return GS_ERROR;
6903 *expr_p = NULL_TREE;
6904 return GS_ALL_DONE;
6907 /* Gimplify the gross structure of other OpenMP constructs.
6908 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
6909 and OMP_TEAMS. */
6911 static void
6912 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6914 tree expr = *expr_p;
6915 gimple stmt;
6916 gimple_seq body = NULL;
6917 enum omp_region_type ort = ORT_WORKSHARE;
6919 switch (TREE_CODE (expr))
6921 case OMP_SECTIONS:
6922 case OMP_SINGLE:
6923 break;
6924 case OMP_TARGET:
6925 ort = ORT_TARGET;
6926 break;
6927 case OMP_TARGET_DATA:
6928 ort = ORT_TARGET_DATA;
6929 break;
6930 case OMP_TEAMS:
6931 ort = ORT_TEAMS;
6932 break;
6933 default:
6934 gcc_unreachable ();
6936 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
6937 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
6939 push_gimplify_context ();
6940 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
6941 if (gimple_code (g) == GIMPLE_BIND)
6942 pop_gimplify_context (g);
6943 else
6944 pop_gimplify_context (NULL);
6945 if (ort == ORT_TARGET_DATA)
6947 gimple_seq cleanup = NULL;
6948 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
6949 g = gimple_build_call (fn, 0);
6950 gimple_seq_add_stmt (&cleanup, g);
6951 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
6952 body = NULL;
6953 gimple_seq_add_stmt (&body, g);
6956 else
6957 gimplify_and_add (OMP_BODY (expr), &body);
6958 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6960 switch (TREE_CODE (expr))
6962 case OMP_SECTIONS:
6963 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6964 break;
6965 case OMP_SINGLE:
6966 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6967 break;
6968 case OMP_TARGET:
6969 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
6970 OMP_CLAUSES (expr));
6971 break;
6972 case OMP_TARGET_DATA:
6973 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
6974 OMP_CLAUSES (expr));
6975 break;
6976 case OMP_TEAMS:
6977 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
6978 break;
6979 default:
6980 gcc_unreachable ();
6983 gimplify_seq_add_stmt (pre_p, stmt);
6984 *expr_p = NULL_TREE;
6987 /* Gimplify the gross structure of OpenMP target update construct. */
6989 static void
6990 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
6992 tree expr = *expr_p;
6993 gimple stmt;
6995 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
6996 ORT_WORKSHARE);
6997 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
6998 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
6999 OMP_TARGET_UPDATE_CLAUSES (expr));
7001 gimplify_seq_add_stmt (pre_p, stmt);
7002 *expr_p = NULL_TREE;
7005 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7006 stabilized the lhs of the atomic operation as *ADDR. Return true if
7007 EXPR is this stabilized form. */
7009 static bool
7010 goa_lhs_expr_p (tree expr, tree addr)
7012 /* Also include casts to other type variants. The C front end is fond
7013 of adding these for e.g. volatile variables. This is like
7014 STRIP_TYPE_NOPS but includes the main variant lookup. */
7015 STRIP_USELESS_TYPE_CONVERSION (expr);
7017 if (TREE_CODE (expr) == INDIRECT_REF)
7019 expr = TREE_OPERAND (expr, 0);
7020 while (expr != addr
7021 && (CONVERT_EXPR_P (expr)
7022 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7023 && TREE_CODE (expr) == TREE_CODE (addr)
7024 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7026 expr = TREE_OPERAND (expr, 0);
7027 addr = TREE_OPERAND (addr, 0);
7029 if (expr == addr)
7030 return true;
7031 return (TREE_CODE (addr) == ADDR_EXPR
7032 && TREE_CODE (expr) == ADDR_EXPR
7033 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7035 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7036 return true;
7037 return false;
7040 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7041 expression does not involve the lhs, evaluate it into a temporary.
7042 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7043 or -1 if an error was encountered. */
7045 static int
7046 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7047 tree lhs_var)
7049 tree expr = *expr_p;
7050 int saw_lhs;
7052 if (goa_lhs_expr_p (expr, lhs_addr))
7054 *expr_p = lhs_var;
7055 return 1;
7057 if (is_gimple_val (expr))
7058 return 0;
7060 saw_lhs = 0;
7061 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7063 case tcc_binary:
7064 case tcc_comparison:
7065 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7066 lhs_var);
7067 case tcc_unary:
7068 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7069 lhs_var);
7070 break;
7071 case tcc_expression:
7072 switch (TREE_CODE (expr))
7074 case TRUTH_ANDIF_EXPR:
7075 case TRUTH_ORIF_EXPR:
7076 case TRUTH_AND_EXPR:
7077 case TRUTH_OR_EXPR:
7078 case TRUTH_XOR_EXPR:
7079 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7080 lhs_addr, lhs_var);
7081 case TRUTH_NOT_EXPR:
7082 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7083 lhs_addr, lhs_var);
7084 break;
7085 case COMPOUND_EXPR:
7086 /* Break out any preevaluations from cp_build_modify_expr. */
7087 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7088 expr = TREE_OPERAND (expr, 1))
7089 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7090 *expr_p = expr;
7091 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7092 default:
7093 break;
7095 break;
7096 default:
7097 break;
7100 if (saw_lhs == 0)
7102 enum gimplify_status gs;
7103 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7104 if (gs != GS_ALL_DONE)
7105 saw_lhs = -1;
7108 return saw_lhs;
7111 /* Gimplify an OMP_ATOMIC statement. */
7113 static enum gimplify_status
7114 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7116 tree addr = TREE_OPERAND (*expr_p, 0);
7117 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7118 ? NULL : TREE_OPERAND (*expr_p, 1);
7119 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7120 tree tmp_load;
7121 gimple loadstmt, storestmt;
7123 tmp_load = create_tmp_reg (type, NULL);
7124 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7125 return GS_ERROR;
7127 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7128 != GS_ALL_DONE)
7129 return GS_ERROR;
7131 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7132 gimplify_seq_add_stmt (pre_p, loadstmt);
7133 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7134 != GS_ALL_DONE)
7135 return GS_ERROR;
7137 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7138 rhs = tmp_load;
7139 storestmt = gimple_build_omp_atomic_store (rhs);
7140 gimplify_seq_add_stmt (pre_p, storestmt);
7141 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7143 gimple_omp_atomic_set_seq_cst (loadstmt);
7144 gimple_omp_atomic_set_seq_cst (storestmt);
7146 switch (TREE_CODE (*expr_p))
7148 case OMP_ATOMIC_READ:
7149 case OMP_ATOMIC_CAPTURE_OLD:
7150 *expr_p = tmp_load;
7151 gimple_omp_atomic_set_need_value (loadstmt);
7152 break;
7153 case OMP_ATOMIC_CAPTURE_NEW:
7154 *expr_p = rhs;
7155 gimple_omp_atomic_set_need_value (storestmt);
7156 break;
7157 default:
7158 *expr_p = NULL;
7159 break;
7162 return GS_ALL_DONE;
7165 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7166 body, and adding some EH bits. */
7168 static enum gimplify_status
7169 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7171 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7172 gimple g;
7173 gimple_seq body = NULL;
7174 int subcode = 0;
7176 /* Wrap the transaction body in a BIND_EXPR so we have a context
7177 where to put decls for OpenMP. */
7178 if (TREE_CODE (tbody) != BIND_EXPR)
7180 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7181 TREE_SIDE_EFFECTS (bind) = 1;
7182 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7183 TRANSACTION_EXPR_BODY (expr) = bind;
7186 push_gimplify_context ();
7187 temp = voidify_wrapper_expr (*expr_p, NULL);
7189 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7190 pop_gimplify_context (g);
7192 g = gimple_build_transaction (body, NULL);
7193 if (TRANSACTION_EXPR_OUTER (expr))
7194 subcode = GTMA_IS_OUTER;
7195 else if (TRANSACTION_EXPR_RELAXED (expr))
7196 subcode = GTMA_IS_RELAXED;
7197 gimple_transaction_set_subcode (g, subcode);
7199 gimplify_seq_add_stmt (pre_p, g);
7201 if (temp)
7203 *expr_p = temp;
7204 return GS_OK;
7207 *expr_p = NULL_TREE;
7208 return GS_ALL_DONE;
7211 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7212 expression produces a value to be used as an operand inside a GIMPLE
7213 statement, the value will be stored back in *EXPR_P. This value will
7214 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7215 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7216 emitted in PRE_P and POST_P.
7218 Additionally, this process may overwrite parts of the input
7219 expression during gimplification. Ideally, it should be
7220 possible to do non-destructive gimplification.
7222 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7223 the expression needs to evaluate to a value to be used as
7224 an operand in a GIMPLE statement, this value will be stored in
7225 *EXPR_P on exit. This happens when the caller specifies one
7226 of fb_lvalue or fb_rvalue fallback flags.
7228 PRE_P will contain the sequence of GIMPLE statements corresponding
7229 to the evaluation of EXPR and all the side-effects that must
7230 be executed before the main expression. On exit, the last
7231 statement of PRE_P is the core statement being gimplified. For
7232 instance, when gimplifying 'if (++a)' the last statement in
7233 PRE_P will be 'if (t.1)' where t.1 is the result of
7234 pre-incrementing 'a'.
7236 POST_P will contain the sequence of GIMPLE statements corresponding
7237 to the evaluation of all the side-effects that must be executed
7238 after the main expression. If this is NULL, the post
7239 side-effects are stored at the end of PRE_P.
7241 The reason why the output is split in two is to handle post
7242 side-effects explicitly. In some cases, an expression may have
7243 inner and outer post side-effects which need to be emitted in
7244 an order different from the one given by the recursive
7245 traversal. For instance, for the expression (*p--)++ the post
7246 side-effects of '--' must actually occur *after* the post
7247 side-effects of '++'. However, gimplification will first visit
7248 the inner expression, so if a separate POST sequence was not
7249 used, the resulting sequence would be:
7251 1 t.1 = *p
7252 2 p = p - 1
7253 3 t.2 = t.1 + 1
7254 4 *p = t.2
7256 However, the post-decrement operation in line #2 must not be
7257 evaluated until after the store to *p at line #4, so the
7258 correct sequence should be:
7260 1 t.1 = *p
7261 2 t.2 = t.1 + 1
7262 3 *p = t.2
7263 4 p = p - 1
7265 So, by specifying a separate post queue, it is possible
7266 to emit the post side-effects in the correct order.
7267 If POST_P is NULL, an internal queue will be used. Before
7268 returning to the caller, the sequence POST_P is appended to
7269 the main output sequence PRE_P.
7271 GIMPLE_TEST_F points to a function that takes a tree T and
7272 returns nonzero if T is in the GIMPLE form requested by the
7273 caller. The GIMPLE predicates are in gimple.c.
7275 FALLBACK tells the function what sort of a temporary we want if
7276 gimplification cannot produce an expression that complies with
7277 GIMPLE_TEST_F.
7279 fb_none means that no temporary should be generated
7280 fb_rvalue means that an rvalue is OK to generate
7281 fb_lvalue means that an lvalue is OK to generate
7282 fb_either means that either is OK, but an lvalue is preferable.
7283 fb_mayfail means that gimplification may fail (in which case
7284 GS_ERROR will be returned)
7286 The return value is either GS_ERROR or GS_ALL_DONE, since this
7287 function iterates until EXPR is completely gimplified or an error
7288 occurs. */
7290 enum gimplify_status
7291 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7292 bool (*gimple_test_f) (tree), fallback_t fallback)
7294 tree tmp;
7295 gimple_seq internal_pre = NULL;
7296 gimple_seq internal_post = NULL;
7297 tree save_expr;
7298 bool is_statement;
7299 location_t saved_location;
7300 enum gimplify_status ret;
7301 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7303 save_expr = *expr_p;
7304 if (save_expr == NULL_TREE)
7305 return GS_ALL_DONE;
7307 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7308 is_statement = gimple_test_f == is_gimple_stmt;
7309 if (is_statement)
7310 gcc_assert (pre_p);
7312 /* Consistency checks. */
7313 if (gimple_test_f == is_gimple_reg)
7314 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7315 else if (gimple_test_f == is_gimple_val
7316 || gimple_test_f == is_gimple_call_addr
7317 || gimple_test_f == is_gimple_condexpr
7318 || gimple_test_f == is_gimple_mem_rhs
7319 || gimple_test_f == is_gimple_mem_rhs_or_call
7320 || gimple_test_f == is_gimple_reg_rhs
7321 || gimple_test_f == is_gimple_reg_rhs_or_call
7322 || gimple_test_f == is_gimple_asm_val
7323 || gimple_test_f == is_gimple_mem_ref_addr)
7324 gcc_assert (fallback & fb_rvalue);
7325 else if (gimple_test_f == is_gimple_min_lval
7326 || gimple_test_f == is_gimple_lvalue)
7327 gcc_assert (fallback & fb_lvalue);
7328 else if (gimple_test_f == is_gimple_addressable)
7329 gcc_assert (fallback & fb_either);
7330 else if (gimple_test_f == is_gimple_stmt)
7331 gcc_assert (fallback == fb_none);
7332 else
7334 /* We should have recognized the GIMPLE_TEST_F predicate to
7335 know what kind of fallback to use in case a temporary is
7336 needed to hold the value or address of *EXPR_P. */
7337 gcc_unreachable ();
7340 /* We used to check the predicate here and return immediately if it
7341 succeeds. This is wrong; the design is for gimplification to be
7342 idempotent, and for the predicates to only test for valid forms, not
7343 whether they are fully simplified. */
7344 if (pre_p == NULL)
7345 pre_p = &internal_pre;
7347 if (post_p == NULL)
7348 post_p = &internal_post;
7350 /* Remember the last statements added to PRE_P and POST_P. Every
7351 new statement added by the gimplification helpers needs to be
7352 annotated with location information. To centralize the
7353 responsibility, we remember the last statement that had been
7354 added to both queues before gimplifying *EXPR_P. If
7355 gimplification produces new statements in PRE_P and POST_P, those
7356 statements will be annotated with the same location information
7357 as *EXPR_P. */
7358 pre_last_gsi = gsi_last (*pre_p);
7359 post_last_gsi = gsi_last (*post_p);
7361 saved_location = input_location;
7362 if (save_expr != error_mark_node
7363 && EXPR_HAS_LOCATION (*expr_p))
7364 input_location = EXPR_LOCATION (*expr_p);
7366 /* Loop over the specific gimplifiers until the toplevel node
7367 remains the same. */
7370 /* Strip away as many useless type conversions as possible
7371 at the toplevel. */
7372 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7374 /* Remember the expr. */
7375 save_expr = *expr_p;
7377 /* Die, die, die, my darling. */
7378 if (save_expr == error_mark_node
7379 || (TREE_TYPE (save_expr)
7380 && TREE_TYPE (save_expr) == error_mark_node))
7382 ret = GS_ERROR;
7383 break;
7386 /* Do any language-specific gimplification. */
7387 ret = ((enum gimplify_status)
7388 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7389 if (ret == GS_OK)
7391 if (*expr_p == NULL_TREE)
7392 break;
7393 if (*expr_p != save_expr)
7394 continue;
7396 else if (ret != GS_UNHANDLED)
7397 break;
7399 /* Make sure that all the cases set 'ret' appropriately. */
7400 ret = GS_UNHANDLED;
7401 switch (TREE_CODE (*expr_p))
7403 /* First deal with the special cases. */
7405 case POSTINCREMENT_EXPR:
7406 case POSTDECREMENT_EXPR:
7407 case PREINCREMENT_EXPR:
7408 case PREDECREMENT_EXPR:
7409 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7410 fallback != fb_none,
7411 TREE_TYPE (*expr_p));
7412 break;
7414 case VIEW_CONVERT_EXPR:
7415 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7416 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7418 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7419 post_p, is_gimple_val, fb_rvalue);
7420 recalculate_side_effects (*expr_p);
7421 break;
7423 /* Fallthru. */
7425 case ARRAY_REF:
7426 case ARRAY_RANGE_REF:
7427 case REALPART_EXPR:
7428 case IMAGPART_EXPR:
7429 case COMPONENT_REF:
7430 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7431 fallback ? fallback : fb_rvalue);
7432 break;
7434 case COND_EXPR:
7435 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7437 /* C99 code may assign to an array in a structure value of a
7438 conditional expression, and this has undefined behavior
7439 only on execution, so create a temporary if an lvalue is
7440 required. */
7441 if (fallback == fb_lvalue)
7443 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7444 mark_addressable (*expr_p);
7445 ret = GS_OK;
7447 break;
7449 case CALL_EXPR:
7450 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7452 /* C99 code may assign to an array in a structure returned
7453 from a function, and this has undefined behavior only on
7454 execution, so create a temporary if an lvalue is
7455 required. */
7456 if (fallback == fb_lvalue)
7458 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7459 mark_addressable (*expr_p);
7460 ret = GS_OK;
7462 break;
7464 case TREE_LIST:
7465 gcc_unreachable ();
7467 case COMPOUND_EXPR:
7468 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7469 break;
7471 case COMPOUND_LITERAL_EXPR:
7472 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7473 gimple_test_f, fallback);
7474 break;
7476 case MODIFY_EXPR:
7477 case INIT_EXPR:
7478 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7479 fallback != fb_none);
7480 break;
7482 case TRUTH_ANDIF_EXPR:
7483 case TRUTH_ORIF_EXPR:
7485 /* Preserve the original type of the expression and the
7486 source location of the outer expression. */
7487 tree org_type = TREE_TYPE (*expr_p);
7488 *expr_p = gimple_boolify (*expr_p);
7489 *expr_p = build3_loc (input_location, COND_EXPR,
7490 org_type, *expr_p,
7491 fold_convert_loc
7492 (input_location,
7493 org_type, boolean_true_node),
7494 fold_convert_loc
7495 (input_location,
7496 org_type, boolean_false_node));
7497 ret = GS_OK;
7498 break;
7501 case TRUTH_NOT_EXPR:
7503 tree type = TREE_TYPE (*expr_p);
7504 /* The parsers are careful to generate TRUTH_NOT_EXPR
7505 only with operands that are always zero or one.
7506 We do not fold here but handle the only interesting case
7507 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7508 *expr_p = gimple_boolify (*expr_p);
7509 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7510 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7511 TREE_TYPE (*expr_p),
7512 TREE_OPERAND (*expr_p, 0));
7513 else
7514 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7515 TREE_TYPE (*expr_p),
7516 TREE_OPERAND (*expr_p, 0),
7517 build_int_cst (TREE_TYPE (*expr_p), 1));
7518 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7519 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7520 ret = GS_OK;
7521 break;
7524 case ADDR_EXPR:
7525 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7526 break;
7528 case ANNOTATE_EXPR:
7530 tree cond = TREE_OPERAND (*expr_p, 0);
7531 tree id = TREE_OPERAND (*expr_p, 1);
7532 tree type = TREE_TYPE (cond);
7533 if (!INTEGRAL_TYPE_P (type))
7535 *expr_p = cond;
7536 ret = GS_OK;
7537 break;
7539 tree tmp = create_tmp_var (type, NULL);
7540 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7541 gimple call = gimple_build_call_internal (IFN_ANNOTATE, 2,
7542 cond, id);
7543 gimple_call_set_lhs (call, tmp);
7544 gimplify_seq_add_stmt (pre_p, call);
7545 *expr_p = tmp;
7546 ret = GS_ALL_DONE;
7547 break;
7550 case VA_ARG_EXPR:
7551 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7552 break;
7554 CASE_CONVERT:
7555 if (IS_EMPTY_STMT (*expr_p))
7557 ret = GS_ALL_DONE;
7558 break;
7561 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7562 || fallback == fb_none)
7564 /* Just strip a conversion to void (or in void context) and
7565 try again. */
7566 *expr_p = TREE_OPERAND (*expr_p, 0);
7567 ret = GS_OK;
7568 break;
7571 ret = gimplify_conversion (expr_p);
7572 if (ret == GS_ERROR)
7573 break;
7574 if (*expr_p != save_expr)
7575 break;
7576 /* FALLTHRU */
7578 case FIX_TRUNC_EXPR:
7579 /* unary_expr: ... | '(' cast ')' val | ... */
7580 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7581 is_gimple_val, fb_rvalue);
7582 recalculate_side_effects (*expr_p);
7583 break;
7585 case INDIRECT_REF:
7587 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7588 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7589 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7591 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7592 if (*expr_p != save_expr)
7594 ret = GS_OK;
7595 break;
7598 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7599 is_gimple_reg, fb_rvalue);
7600 if (ret == GS_ERROR)
7601 break;
7603 recalculate_side_effects (*expr_p);
7604 *expr_p = fold_build2_loc (input_location, MEM_REF,
7605 TREE_TYPE (*expr_p),
7606 TREE_OPERAND (*expr_p, 0),
7607 build_int_cst (saved_ptr_type, 0));
7608 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7609 TREE_THIS_NOTRAP (*expr_p) = notrap;
7610 ret = GS_OK;
7611 break;
7614 /* We arrive here through the various re-gimplifcation paths. */
7615 case MEM_REF:
7616 /* First try re-folding the whole thing. */
7617 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7618 TREE_OPERAND (*expr_p, 0),
7619 TREE_OPERAND (*expr_p, 1));
7620 if (tmp)
7622 *expr_p = tmp;
7623 recalculate_side_effects (*expr_p);
7624 ret = GS_OK;
7625 break;
7627 /* Avoid re-gimplifying the address operand if it is already
7628 in suitable form. Re-gimplifying would mark the address
7629 operand addressable. Always gimplify when not in SSA form
7630 as we still may have to gimplify decls with value-exprs. */
7631 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7632 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7634 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7635 is_gimple_mem_ref_addr, fb_rvalue);
7636 if (ret == GS_ERROR)
7637 break;
7639 recalculate_side_effects (*expr_p);
7640 ret = GS_ALL_DONE;
7641 break;
7643 /* Constants need not be gimplified. */
7644 case INTEGER_CST:
7645 case REAL_CST:
7646 case FIXED_CST:
7647 case STRING_CST:
7648 case COMPLEX_CST:
7649 case VECTOR_CST:
7650 /* Drop the overflow flag on constants, we do not want
7651 that in the GIMPLE IL. */
7652 if (TREE_OVERFLOW_P (*expr_p))
7653 *expr_p = drop_tree_overflow (*expr_p);
7654 ret = GS_ALL_DONE;
7655 break;
7657 case CONST_DECL:
7658 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7659 CONST_DECL node. Otherwise the decl is replaceable by its
7660 value. */
7661 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7662 if (fallback & fb_lvalue)
7663 ret = GS_ALL_DONE;
7664 else
7666 *expr_p = DECL_INITIAL (*expr_p);
7667 ret = GS_OK;
7669 break;
7671 case DECL_EXPR:
7672 ret = gimplify_decl_expr (expr_p, pre_p);
7673 break;
7675 case BIND_EXPR:
7676 ret = gimplify_bind_expr (expr_p, pre_p);
7677 break;
7679 case LOOP_EXPR:
7680 ret = gimplify_loop_expr (expr_p, pre_p);
7681 break;
7683 case SWITCH_EXPR:
7684 ret = gimplify_switch_expr (expr_p, pre_p);
7685 break;
7687 case EXIT_EXPR:
7688 ret = gimplify_exit_expr (expr_p);
7689 break;
7691 case GOTO_EXPR:
7692 /* If the target is not LABEL, then it is a computed jump
7693 and the target needs to be gimplified. */
7694 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7696 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7697 NULL, is_gimple_val, fb_rvalue);
7698 if (ret == GS_ERROR)
7699 break;
7701 gimplify_seq_add_stmt (pre_p,
7702 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7703 ret = GS_ALL_DONE;
7704 break;
7706 case PREDICT_EXPR:
7707 gimplify_seq_add_stmt (pre_p,
7708 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7709 PREDICT_EXPR_OUTCOME (*expr_p)));
7710 ret = GS_ALL_DONE;
7711 break;
7713 case LABEL_EXPR:
7714 ret = GS_ALL_DONE;
7715 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7716 == current_function_decl);
7717 gimplify_seq_add_stmt (pre_p,
7718 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7719 break;
7721 case CASE_LABEL_EXPR:
7722 ret = gimplify_case_label_expr (expr_p, pre_p);
7723 break;
7725 case RETURN_EXPR:
7726 ret = gimplify_return_expr (*expr_p, pre_p);
7727 break;
7729 case CONSTRUCTOR:
7730 /* Don't reduce this in place; let gimplify_init_constructor work its
7731 magic. Buf if we're just elaborating this for side effects, just
7732 gimplify any element that has side-effects. */
7733 if (fallback == fb_none)
7735 unsigned HOST_WIDE_INT ix;
7736 tree val;
7737 tree temp = NULL_TREE;
7738 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7739 if (TREE_SIDE_EFFECTS (val))
7740 append_to_statement_list (val, &temp);
7742 *expr_p = temp;
7743 ret = temp ? GS_OK : GS_ALL_DONE;
7745 /* C99 code may assign to an array in a constructed
7746 structure or union, and this has undefined behavior only
7747 on execution, so create a temporary if an lvalue is
7748 required. */
7749 else if (fallback == fb_lvalue)
7751 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7752 mark_addressable (*expr_p);
7753 ret = GS_OK;
7755 else
7756 ret = GS_ALL_DONE;
7757 break;
7759 /* The following are special cases that are not handled by the
7760 original GIMPLE grammar. */
7762 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7763 eliminated. */
7764 case SAVE_EXPR:
7765 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7766 break;
7768 case BIT_FIELD_REF:
7769 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7770 post_p, is_gimple_lvalue, fb_either);
7771 recalculate_side_effects (*expr_p);
7772 break;
7774 case TARGET_MEM_REF:
7776 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7778 if (TMR_BASE (*expr_p))
7779 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7780 post_p, is_gimple_mem_ref_addr, fb_either);
7781 if (TMR_INDEX (*expr_p))
7782 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7783 post_p, is_gimple_val, fb_rvalue);
7784 if (TMR_INDEX2 (*expr_p))
7785 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7786 post_p, is_gimple_val, fb_rvalue);
7787 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7788 ret = MIN (r0, r1);
7790 break;
7792 case NON_LVALUE_EXPR:
7793 /* This should have been stripped above. */
7794 gcc_unreachable ();
7796 case ASM_EXPR:
7797 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7798 break;
7800 case TRY_FINALLY_EXPR:
7801 case TRY_CATCH_EXPR:
7803 gimple_seq eval, cleanup;
7804 gimple try_;
7806 /* Calls to destructors are generated automatically in FINALLY/CATCH
7807 block. They should have location as UNKNOWN_LOCATION. However,
7808 gimplify_call_expr will reset these call stmts to input_location
7809 if it finds stmt's location is unknown. To prevent resetting for
7810 destructors, we set the input_location to unknown.
7811 Note that this only affects the destructor calls in FINALLY/CATCH
7812 block, and will automatically reset to its original value by the
7813 end of gimplify_expr. */
7814 input_location = UNKNOWN_LOCATION;
7815 eval = cleanup = NULL;
7816 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7817 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7818 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7819 if (gimple_seq_empty_p (cleanup))
7821 gimple_seq_add_seq (pre_p, eval);
7822 ret = GS_ALL_DONE;
7823 break;
7825 try_ = gimple_build_try (eval, cleanup,
7826 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7827 ? GIMPLE_TRY_FINALLY
7828 : GIMPLE_TRY_CATCH);
7829 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7830 gimple_set_location (try_, saved_location);
7831 else
7832 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7833 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7834 gimple_try_set_catch_is_cleanup (try_,
7835 TRY_CATCH_IS_CLEANUP (*expr_p));
7836 gimplify_seq_add_stmt (pre_p, try_);
7837 ret = GS_ALL_DONE;
7838 break;
7841 case CLEANUP_POINT_EXPR:
7842 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7843 break;
7845 case TARGET_EXPR:
7846 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7847 break;
7849 case CATCH_EXPR:
7851 gimple c;
7852 gimple_seq handler = NULL;
7853 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7854 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7855 gimplify_seq_add_stmt (pre_p, c);
7856 ret = GS_ALL_DONE;
7857 break;
7860 case EH_FILTER_EXPR:
7862 gimple ehf;
7863 gimple_seq failure = NULL;
7865 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7866 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7867 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7868 gimplify_seq_add_stmt (pre_p, ehf);
7869 ret = GS_ALL_DONE;
7870 break;
7873 case OBJ_TYPE_REF:
7875 enum gimplify_status r0, r1;
7876 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7877 post_p, is_gimple_val, fb_rvalue);
7878 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7879 post_p, is_gimple_val, fb_rvalue);
7880 TREE_SIDE_EFFECTS (*expr_p) = 0;
7881 ret = MIN (r0, r1);
7883 break;
7885 case LABEL_DECL:
7886 /* We get here when taking the address of a label. We mark
7887 the label as "forced"; meaning it can never be removed and
7888 it is a potential target for any computed goto. */
7889 FORCED_LABEL (*expr_p) = 1;
7890 ret = GS_ALL_DONE;
7891 break;
7893 case STATEMENT_LIST:
7894 ret = gimplify_statement_list (expr_p, pre_p);
7895 break;
7897 case WITH_SIZE_EXPR:
7899 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7900 post_p == &internal_post ? NULL : post_p,
7901 gimple_test_f, fallback);
7902 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7903 is_gimple_val, fb_rvalue);
7904 ret = GS_ALL_DONE;
7906 break;
7908 case VAR_DECL:
7909 case PARM_DECL:
7910 ret = gimplify_var_or_parm_decl (expr_p);
7911 break;
7913 case RESULT_DECL:
7914 /* When within an OpenMP context, notice uses of variables. */
7915 if (gimplify_omp_ctxp)
7916 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7917 ret = GS_ALL_DONE;
7918 break;
7920 case SSA_NAME:
7921 /* Allow callbacks into the gimplifier during optimization. */
7922 ret = GS_ALL_DONE;
7923 break;
7925 case OMP_PARALLEL:
7926 gimplify_omp_parallel (expr_p, pre_p);
7927 ret = GS_ALL_DONE;
7928 break;
7930 case OMP_TASK:
7931 gimplify_omp_task (expr_p, pre_p);
7932 ret = GS_ALL_DONE;
7933 break;
7935 case OMP_FOR:
7936 case OMP_SIMD:
7937 case CILK_SIMD:
7938 case OMP_DISTRIBUTE:
7939 ret = gimplify_omp_for (expr_p, pre_p);
7940 break;
7942 case OMP_SECTIONS:
7943 case OMP_SINGLE:
7944 case OMP_TARGET:
7945 case OMP_TARGET_DATA:
7946 case OMP_TEAMS:
7947 gimplify_omp_workshare (expr_p, pre_p);
7948 ret = GS_ALL_DONE;
7949 break;
7951 case OMP_TARGET_UPDATE:
7952 gimplify_omp_target_update (expr_p, pre_p);
7953 ret = GS_ALL_DONE;
7954 break;
7956 case OMP_SECTION:
7957 case OMP_MASTER:
7958 case OMP_TASKGROUP:
7959 case OMP_ORDERED:
7960 case OMP_CRITICAL:
7962 gimple_seq body = NULL;
7963 gimple g;
7965 gimplify_and_add (OMP_BODY (*expr_p), &body);
7966 switch (TREE_CODE (*expr_p))
7968 case OMP_SECTION:
7969 g = gimple_build_omp_section (body);
7970 break;
7971 case OMP_MASTER:
7972 g = gimple_build_omp_master (body);
7973 break;
7974 case OMP_TASKGROUP:
7976 gimple_seq cleanup = NULL;
7977 tree fn
7978 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
7979 g = gimple_build_call (fn, 0);
7980 gimple_seq_add_stmt (&cleanup, g);
7981 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7982 body = NULL;
7983 gimple_seq_add_stmt (&body, g);
7984 g = gimple_build_omp_taskgroup (body);
7986 break;
7987 case OMP_ORDERED:
7988 g = gimple_build_omp_ordered (body);
7989 break;
7990 case OMP_CRITICAL:
7991 g = gimple_build_omp_critical (body,
7992 OMP_CRITICAL_NAME (*expr_p));
7993 break;
7994 default:
7995 gcc_unreachable ();
7997 gimplify_seq_add_stmt (pre_p, g);
7998 ret = GS_ALL_DONE;
7999 break;
8002 case OMP_ATOMIC:
8003 case OMP_ATOMIC_READ:
8004 case OMP_ATOMIC_CAPTURE_OLD:
8005 case OMP_ATOMIC_CAPTURE_NEW:
8006 ret = gimplify_omp_atomic (expr_p, pre_p);
8007 break;
8009 case TRANSACTION_EXPR:
8010 ret = gimplify_transaction (expr_p, pre_p);
8011 break;
8013 case TRUTH_AND_EXPR:
8014 case TRUTH_OR_EXPR:
8015 case TRUTH_XOR_EXPR:
8017 tree orig_type = TREE_TYPE (*expr_p);
8018 tree new_type, xop0, xop1;
8019 *expr_p = gimple_boolify (*expr_p);
8020 new_type = TREE_TYPE (*expr_p);
8021 if (!useless_type_conversion_p (orig_type, new_type))
8023 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8024 ret = GS_OK;
8025 break;
8028 /* Boolified binary truth expressions are semantically equivalent
8029 to bitwise binary expressions. Canonicalize them to the
8030 bitwise variant. */
8031 switch (TREE_CODE (*expr_p))
8033 case TRUTH_AND_EXPR:
8034 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8035 break;
8036 case TRUTH_OR_EXPR:
8037 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8038 break;
8039 case TRUTH_XOR_EXPR:
8040 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8041 break;
8042 default:
8043 break;
8045 /* Now make sure that operands have compatible type to
8046 expression's new_type. */
8047 xop0 = TREE_OPERAND (*expr_p, 0);
8048 xop1 = TREE_OPERAND (*expr_p, 1);
8049 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8050 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8051 new_type,
8052 xop0);
8053 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8054 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8055 new_type,
8056 xop1);
8057 /* Continue classified as tcc_binary. */
8058 goto expr_2;
8061 case FMA_EXPR:
8062 case VEC_COND_EXPR:
8063 case VEC_PERM_EXPR:
8064 /* Classified as tcc_expression. */
8065 goto expr_3;
8067 case POINTER_PLUS_EXPR:
8069 enum gimplify_status r0, r1;
8070 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8071 post_p, is_gimple_val, fb_rvalue);
8072 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8073 post_p, is_gimple_val, fb_rvalue);
8074 recalculate_side_effects (*expr_p);
8075 ret = MIN (r0, r1);
8076 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8077 after gimplifying operands - this is similar to how
8078 it would be folding all gimplified stmts on creation
8079 to have them canonicalized, which is what we eventually
8080 should do anyway. */
8081 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8082 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8084 *expr_p = build_fold_addr_expr_with_type_loc
8085 (input_location,
8086 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8087 TREE_OPERAND (*expr_p, 0),
8088 fold_convert (ptr_type_node,
8089 TREE_OPERAND (*expr_p, 1))),
8090 TREE_TYPE (*expr_p));
8091 ret = MIN (ret, GS_OK);
8093 break;
8096 case CILK_SYNC_STMT:
8098 if (!fn_contains_cilk_spawn_p (cfun))
8100 error_at (EXPR_LOCATION (*expr_p),
8101 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8102 ret = GS_ERROR;
8104 else
8106 gimplify_cilk_sync (expr_p, pre_p);
8107 ret = GS_ALL_DONE;
8109 break;
8112 default:
8113 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8115 case tcc_comparison:
8116 /* Handle comparison of objects of non scalar mode aggregates
8117 with a call to memcmp. It would be nice to only have to do
8118 this for variable-sized objects, but then we'd have to allow
8119 the same nest of reference nodes we allow for MODIFY_EXPR and
8120 that's too complex.
8122 Compare scalar mode aggregates as scalar mode values. Using
8123 memcmp for them would be very inefficient at best, and is
8124 plain wrong if bitfields are involved. */
8126 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8128 /* Vector comparisons need no boolification. */
8129 if (TREE_CODE (type) == VECTOR_TYPE)
8130 goto expr_2;
8131 else if (!AGGREGATE_TYPE_P (type))
8133 tree org_type = TREE_TYPE (*expr_p);
8134 *expr_p = gimple_boolify (*expr_p);
8135 if (!useless_type_conversion_p (org_type,
8136 TREE_TYPE (*expr_p)))
8138 *expr_p = fold_convert_loc (input_location,
8139 org_type, *expr_p);
8140 ret = GS_OK;
8142 else
8143 goto expr_2;
8145 else if (TYPE_MODE (type) != BLKmode)
8146 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8147 else
8148 ret = gimplify_variable_sized_compare (expr_p);
8150 break;
8153 /* If *EXPR_P does not need to be special-cased, handle it
8154 according to its class. */
8155 case tcc_unary:
8156 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8157 post_p, is_gimple_val, fb_rvalue);
8158 break;
8160 case tcc_binary:
8161 expr_2:
8163 enum gimplify_status r0, r1;
8165 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8166 post_p, is_gimple_val, fb_rvalue);
8167 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8168 post_p, is_gimple_val, fb_rvalue);
8170 ret = MIN (r0, r1);
8171 break;
8174 expr_3:
8176 enum gimplify_status r0, r1, r2;
8178 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8179 post_p, is_gimple_val, fb_rvalue);
8180 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8181 post_p, is_gimple_val, fb_rvalue);
8182 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8183 post_p, is_gimple_val, fb_rvalue);
8185 ret = MIN (MIN (r0, r1), r2);
8186 break;
8189 case tcc_declaration:
8190 case tcc_constant:
8191 ret = GS_ALL_DONE;
8192 goto dont_recalculate;
8194 default:
8195 gcc_unreachable ();
8198 recalculate_side_effects (*expr_p);
8200 dont_recalculate:
8201 break;
8204 gcc_assert (*expr_p || ret != GS_OK);
8206 while (ret == GS_OK);
8208 /* If we encountered an error_mark somewhere nested inside, either
8209 stub out the statement or propagate the error back out. */
8210 if (ret == GS_ERROR)
8212 if (is_statement)
8213 *expr_p = NULL;
8214 goto out;
8217 /* This was only valid as a return value from the langhook, which
8218 we handled. Make sure it doesn't escape from any other context. */
8219 gcc_assert (ret != GS_UNHANDLED);
8221 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8223 /* We aren't looking for a value, and we don't have a valid
8224 statement. If it doesn't have side-effects, throw it away. */
8225 if (!TREE_SIDE_EFFECTS (*expr_p))
8226 *expr_p = NULL;
8227 else if (!TREE_THIS_VOLATILE (*expr_p))
8229 /* This is probably a _REF that contains something nested that
8230 has side effects. Recurse through the operands to find it. */
8231 enum tree_code code = TREE_CODE (*expr_p);
8233 switch (code)
8235 case COMPONENT_REF:
8236 case REALPART_EXPR:
8237 case IMAGPART_EXPR:
8238 case VIEW_CONVERT_EXPR:
8239 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8240 gimple_test_f, fallback);
8241 break;
8243 case ARRAY_REF:
8244 case ARRAY_RANGE_REF:
8245 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8246 gimple_test_f, fallback);
8247 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8248 gimple_test_f, fallback);
8249 break;
8251 default:
8252 /* Anything else with side-effects must be converted to
8253 a valid statement before we get here. */
8254 gcc_unreachable ();
8257 *expr_p = NULL;
8259 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8260 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8262 /* Historically, the compiler has treated a bare reference
8263 to a non-BLKmode volatile lvalue as forcing a load. */
8264 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8266 /* Normally, we do not want to create a temporary for a
8267 TREE_ADDRESSABLE type because such a type should not be
8268 copied by bitwise-assignment. However, we make an
8269 exception here, as all we are doing here is ensuring that
8270 we read the bytes that make up the type. We use
8271 create_tmp_var_raw because create_tmp_var will abort when
8272 given a TREE_ADDRESSABLE type. */
8273 tree tmp = create_tmp_var_raw (type, "vol");
8274 gimple_add_tmp_var (tmp);
8275 gimplify_assign (tmp, *expr_p, pre_p);
8276 *expr_p = NULL;
8278 else
8279 /* We can't do anything useful with a volatile reference to
8280 an incomplete type, so just throw it away. Likewise for
8281 a BLKmode type, since any implicit inner load should
8282 already have been turned into an explicit one by the
8283 gimplification process. */
8284 *expr_p = NULL;
8287 /* If we are gimplifying at the statement level, we're done. Tack
8288 everything together and return. */
8289 if (fallback == fb_none || is_statement)
8291 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8292 it out for GC to reclaim it. */
8293 *expr_p = NULL_TREE;
8295 if (!gimple_seq_empty_p (internal_pre)
8296 || !gimple_seq_empty_p (internal_post))
8298 gimplify_seq_add_seq (&internal_pre, internal_post);
8299 gimplify_seq_add_seq (pre_p, internal_pre);
8302 /* The result of gimplifying *EXPR_P is going to be the last few
8303 statements in *PRE_P and *POST_P. Add location information
8304 to all the statements that were added by the gimplification
8305 helpers. */
8306 if (!gimple_seq_empty_p (*pre_p))
8307 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8309 if (!gimple_seq_empty_p (*post_p))
8310 annotate_all_with_location_after (*post_p, post_last_gsi,
8311 input_location);
8313 goto out;
8316 #ifdef ENABLE_GIMPLE_CHECKING
8317 if (*expr_p)
8319 enum tree_code code = TREE_CODE (*expr_p);
8320 /* These expressions should already be in gimple IR form. */
8321 gcc_assert (code != MODIFY_EXPR
8322 && code != ASM_EXPR
8323 && code != BIND_EXPR
8324 && code != CATCH_EXPR
8325 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8326 && code != EH_FILTER_EXPR
8327 && code != GOTO_EXPR
8328 && code != LABEL_EXPR
8329 && code != LOOP_EXPR
8330 && code != SWITCH_EXPR
8331 && code != TRY_FINALLY_EXPR
8332 && code != OMP_CRITICAL
8333 && code != OMP_FOR
8334 && code != OMP_MASTER
8335 && code != OMP_TASKGROUP
8336 && code != OMP_ORDERED
8337 && code != OMP_PARALLEL
8338 && code != OMP_SECTIONS
8339 && code != OMP_SECTION
8340 && code != OMP_SINGLE);
8342 #endif
8344 /* Otherwise we're gimplifying a subexpression, so the resulting
8345 value is interesting. If it's a valid operand that matches
8346 GIMPLE_TEST_F, we're done. Unless we are handling some
8347 post-effects internally; if that's the case, we need to copy into
8348 a temporary before adding the post-effects to POST_P. */
8349 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8350 goto out;
8352 /* Otherwise, we need to create a new temporary for the gimplified
8353 expression. */
8355 /* We can't return an lvalue if we have an internal postqueue. The
8356 object the lvalue refers to would (probably) be modified by the
8357 postqueue; we need to copy the value out first, which means an
8358 rvalue. */
8359 if ((fallback & fb_lvalue)
8360 && gimple_seq_empty_p (internal_post)
8361 && is_gimple_addressable (*expr_p))
8363 /* An lvalue will do. Take the address of the expression, store it
8364 in a temporary, and replace the expression with an INDIRECT_REF of
8365 that temporary. */
8366 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8367 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8368 *expr_p = build_simple_mem_ref (tmp);
8370 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8372 /* An rvalue will do. Assign the gimplified expression into a
8373 new temporary TMP and replace the original expression with
8374 TMP. First, make sure that the expression has a type so that
8375 it can be assigned into a temporary. */
8376 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8377 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8379 else
8381 #ifdef ENABLE_GIMPLE_CHECKING
8382 if (!(fallback & fb_mayfail))
8384 fprintf (stderr, "gimplification failed:\n");
8385 print_generic_expr (stderr, *expr_p, 0);
8386 debug_tree (*expr_p);
8387 internal_error ("gimplification failed");
8389 #endif
8390 gcc_assert (fallback & fb_mayfail);
8392 /* If this is an asm statement, and the user asked for the
8393 impossible, don't die. Fail and let gimplify_asm_expr
8394 issue an error. */
8395 ret = GS_ERROR;
8396 goto out;
8399 /* Make sure the temporary matches our predicate. */
8400 gcc_assert ((*gimple_test_f) (*expr_p));
8402 if (!gimple_seq_empty_p (internal_post))
8404 annotate_all_with_location (internal_post, input_location);
8405 gimplify_seq_add_seq (pre_p, internal_post);
8408 out:
8409 input_location = saved_location;
8410 return ret;
8413 /* Look through TYPE for variable-sized objects and gimplify each such
8414 size that we find. Add to LIST_P any statements generated. */
8416 void
8417 gimplify_type_sizes (tree type, gimple_seq *list_p)
8419 tree field, t;
8421 if (type == NULL || type == error_mark_node)
8422 return;
8424 /* We first do the main variant, then copy into any other variants. */
8425 type = TYPE_MAIN_VARIANT (type);
8427 /* Avoid infinite recursion. */
8428 if (TYPE_SIZES_GIMPLIFIED (type))
8429 return;
8431 TYPE_SIZES_GIMPLIFIED (type) = 1;
8433 switch (TREE_CODE (type))
8435 case INTEGER_TYPE:
8436 case ENUMERAL_TYPE:
8437 case BOOLEAN_TYPE:
8438 case REAL_TYPE:
8439 case FIXED_POINT_TYPE:
8440 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8441 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8443 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8445 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8446 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8448 break;
8450 case ARRAY_TYPE:
8451 /* These types may not have declarations, so handle them here. */
8452 gimplify_type_sizes (TREE_TYPE (type), list_p);
8453 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8454 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8455 with assigned stack slots, for -O1+ -g they should be tracked
8456 by VTA. */
8457 if (!(TYPE_NAME (type)
8458 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8459 && DECL_IGNORED_P (TYPE_NAME (type)))
8460 && TYPE_DOMAIN (type)
8461 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8463 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8464 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8465 DECL_IGNORED_P (t) = 0;
8466 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8467 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8468 DECL_IGNORED_P (t) = 0;
8470 break;
8472 case RECORD_TYPE:
8473 case UNION_TYPE:
8474 case QUAL_UNION_TYPE:
8475 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8476 if (TREE_CODE (field) == FIELD_DECL)
8478 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8479 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8480 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8481 gimplify_type_sizes (TREE_TYPE (field), list_p);
8483 break;
8485 case POINTER_TYPE:
8486 case REFERENCE_TYPE:
8487 /* We used to recurse on the pointed-to type here, which turned out to
8488 be incorrect because its definition might refer to variables not
8489 yet initialized at this point if a forward declaration is involved.
8491 It was actually useful for anonymous pointed-to types to ensure
8492 that the sizes evaluation dominates every possible later use of the
8493 values. Restricting to such types here would be safe since there
8494 is no possible forward declaration around, but would introduce an
8495 undesirable middle-end semantic to anonymity. We then defer to
8496 front-ends the responsibility of ensuring that the sizes are
8497 evaluated both early and late enough, e.g. by attaching artificial
8498 type declarations to the tree. */
8499 break;
8501 default:
8502 break;
8505 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8506 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8508 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8510 TYPE_SIZE (t) = TYPE_SIZE (type);
8511 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8512 TYPE_SIZES_GIMPLIFIED (t) = 1;
8516 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8517 a size or position, has had all of its SAVE_EXPRs evaluated.
8518 We add any required statements to *STMT_P. */
8520 void
8521 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8523 tree expr = *expr_p;
8525 /* We don't do anything if the value isn't there, is constant, or contains
8526 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8527 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8528 will want to replace it with a new variable, but that will cause problems
8529 if this type is from outside the function. It's OK to have that here. */
8530 if (is_gimple_sizepos (expr))
8531 return;
8533 *expr_p = unshare_expr (expr);
8535 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8538 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8539 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8540 is true, also gimplify the parameters. */
8542 gimple
8543 gimplify_body (tree fndecl, bool do_parms)
8545 location_t saved_location = input_location;
8546 gimple_seq parm_stmts, seq;
8547 gimple outer_bind;
8548 struct cgraph_node *cgn;
8550 timevar_push (TV_TREE_GIMPLIFY);
8552 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8553 gimplification. */
8554 default_rtl_profile ();
8556 gcc_assert (gimplify_ctxp == NULL);
8557 push_gimplify_context ();
8559 if (flag_openmp)
8561 gcc_assert (gimplify_omp_ctxp == NULL);
8562 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8563 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8566 /* Unshare most shared trees in the body and in that of any nested functions.
8567 It would seem we don't have to do this for nested functions because
8568 they are supposed to be output and then the outer function gimplified
8569 first, but the g++ front end doesn't always do it that way. */
8570 unshare_body (fndecl);
8571 unvisit_body (fndecl);
8573 cgn = cgraph_get_node (fndecl);
8574 if (cgn && cgn->origin)
8575 nonlocal_vlas = pointer_set_create ();
8577 /* Make sure input_location isn't set to something weird. */
8578 input_location = DECL_SOURCE_LOCATION (fndecl);
8580 /* Resolve callee-copies. This has to be done before processing
8581 the body so that DECL_VALUE_EXPR gets processed correctly. */
8582 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8584 /* Gimplify the function's body. */
8585 seq = NULL;
8586 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8587 outer_bind = gimple_seq_first_stmt (seq);
8588 if (!outer_bind)
8590 outer_bind = gimple_build_nop ();
8591 gimplify_seq_add_stmt (&seq, outer_bind);
8594 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8595 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8596 if (gimple_code (outer_bind) == GIMPLE_BIND
8597 && gimple_seq_first (seq) == gimple_seq_last (seq))
8599 else
8600 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8602 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8604 /* If we had callee-copies statements, insert them at the beginning
8605 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8606 if (!gimple_seq_empty_p (parm_stmts))
8608 tree parm;
8610 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8611 gimple_bind_set_body (outer_bind, parm_stmts);
8613 for (parm = DECL_ARGUMENTS (current_function_decl);
8614 parm; parm = DECL_CHAIN (parm))
8615 if (DECL_HAS_VALUE_EXPR_P (parm))
8617 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8618 DECL_IGNORED_P (parm) = 0;
8622 if (nonlocal_vlas)
8624 if (nonlocal_vla_vars)
8626 /* tree-nested.c may later on call declare_vars (..., true);
8627 which relies on BLOCK_VARS chain to be the tail of the
8628 gimple_bind_vars chain. Ensure we don't violate that
8629 assumption. */
8630 if (gimple_bind_block (outer_bind)
8631 == DECL_INITIAL (current_function_decl))
8632 declare_vars (nonlocal_vla_vars, outer_bind, true);
8633 else
8634 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8635 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8636 nonlocal_vla_vars);
8637 nonlocal_vla_vars = NULL_TREE;
8639 pointer_set_destroy (nonlocal_vlas);
8640 nonlocal_vlas = NULL;
8643 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8645 delete_omp_context (gimplify_omp_ctxp);
8646 gimplify_omp_ctxp = NULL;
8649 pop_gimplify_context (outer_bind);
8650 gcc_assert (gimplify_ctxp == NULL);
8652 #ifdef ENABLE_CHECKING
8653 if (!seen_error ())
8654 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8655 #endif
8657 timevar_pop (TV_TREE_GIMPLIFY);
8658 input_location = saved_location;
8660 return outer_bind;
8663 typedef char *char_p; /* For DEF_VEC_P. */
8665 /* Return whether we should exclude FNDECL from instrumentation. */
8667 static bool
8668 flag_instrument_functions_exclude_p (tree fndecl)
8670 vec<char_p> *v;
8672 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8673 if (v && v->length () > 0)
8675 const char *name;
8676 int i;
8677 char *s;
8679 name = lang_hooks.decl_printable_name (fndecl, 0);
8680 FOR_EACH_VEC_ELT (*v, i, s)
8681 if (strstr (name, s) != NULL)
8682 return true;
8685 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8686 if (v && v->length () > 0)
8688 const char *name;
8689 int i;
8690 char *s;
8692 name = DECL_SOURCE_FILE (fndecl);
8693 FOR_EACH_VEC_ELT (*v, i, s)
8694 if (strstr (name, s) != NULL)
8695 return true;
8698 return false;
8701 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8702 node for the function we want to gimplify.
8704 Return the sequence of GIMPLE statements corresponding to the body
8705 of FNDECL. */
8707 void
8708 gimplify_function_tree (tree fndecl)
8710 tree parm, ret;
8711 gimple_seq seq;
8712 gimple bind;
8714 gcc_assert (!gimple_body (fndecl));
8716 if (DECL_STRUCT_FUNCTION (fndecl))
8717 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8718 else
8719 push_struct_function (fndecl);
8721 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8723 /* Preliminarily mark non-addressed complex variables as eligible
8724 for promotion to gimple registers. We'll transform their uses
8725 as we find them. */
8726 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8727 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8728 && !TREE_THIS_VOLATILE (parm)
8729 && !needs_to_live_in_memory (parm))
8730 DECL_GIMPLE_REG_P (parm) = 1;
8733 ret = DECL_RESULT (fndecl);
8734 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8735 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8736 && !needs_to_live_in_memory (ret))
8737 DECL_GIMPLE_REG_P (ret) = 1;
8739 bind = gimplify_body (fndecl, true);
8741 /* The tree body of the function is no longer needed, replace it
8742 with the new GIMPLE body. */
8743 seq = NULL;
8744 gimple_seq_add_stmt (&seq, bind);
8745 gimple_set_body (fndecl, seq);
8747 /* If we're instrumenting function entry/exit, then prepend the call to
8748 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8749 catch the exit hook. */
8750 /* ??? Add some way to ignore exceptions for this TFE. */
8751 if (flag_instrument_function_entry_exit
8752 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8753 && !flag_instrument_functions_exclude_p (fndecl))
8755 tree x;
8756 gimple new_bind;
8757 gimple tf;
8758 gimple_seq cleanup = NULL, body = NULL;
8759 tree tmp_var;
8760 gimple call;
8762 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8763 call = gimple_build_call (x, 1, integer_zero_node);
8764 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8765 gimple_call_set_lhs (call, tmp_var);
8766 gimplify_seq_add_stmt (&cleanup, call);
8767 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8768 call = gimple_build_call (x, 2,
8769 build_fold_addr_expr (current_function_decl),
8770 tmp_var);
8771 gimplify_seq_add_stmt (&cleanup, call);
8772 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8774 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8775 call = gimple_build_call (x, 1, integer_zero_node);
8776 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8777 gimple_call_set_lhs (call, tmp_var);
8778 gimplify_seq_add_stmt (&body, call);
8779 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8780 call = gimple_build_call (x, 2,
8781 build_fold_addr_expr (current_function_decl),
8782 tmp_var);
8783 gimplify_seq_add_stmt (&body, call);
8784 gimplify_seq_add_stmt (&body, tf);
8785 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8786 /* Clear the block for BIND, since it is no longer directly inside
8787 the function, but within a try block. */
8788 gimple_bind_set_block (bind, NULL);
8790 /* Replace the current function body with the body
8791 wrapped in the try/finally TF. */
8792 seq = NULL;
8793 gimple_seq_add_stmt (&seq, new_bind);
8794 gimple_set_body (fndecl, seq);
8797 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8798 cfun->curr_properties = PROP_gimple_any;
8800 pop_cfun ();
8803 /* Return a dummy expression of type TYPE in order to keep going after an
8804 error. */
8806 static tree
8807 dummy_object (tree type)
8809 tree t = build_int_cst (build_pointer_type (type), 0);
8810 return build2 (MEM_REF, type, t, t);
8813 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
8814 builtin function, but a very special sort of operator. */
8816 enum gimplify_status
8817 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8819 tree promoted_type, have_va_type;
8820 tree valist = TREE_OPERAND (*expr_p, 0);
8821 tree type = TREE_TYPE (*expr_p);
8822 tree t;
8823 location_t loc = EXPR_LOCATION (*expr_p);
8825 /* Verify that valist is of the proper type. */
8826 have_va_type = TREE_TYPE (valist);
8827 if (have_va_type == error_mark_node)
8828 return GS_ERROR;
8829 have_va_type = targetm.canonical_va_list_type (have_va_type);
8831 if (have_va_type == NULL_TREE)
8833 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
8834 return GS_ERROR;
8837 /* Generate a diagnostic for requesting data of a type that cannot
8838 be passed through `...' due to type promotion at the call site. */
8839 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
8840 != type)
8842 static bool gave_help;
8843 bool warned;
8845 /* Unfortunately, this is merely undefined, rather than a constraint
8846 violation, so we cannot make this an error. If this call is never
8847 executed, the program is still strictly conforming. */
8848 warned = warning_at (loc, 0,
8849 "%qT is promoted to %qT when passed through %<...%>",
8850 type, promoted_type);
8851 if (!gave_help && warned)
8853 gave_help = true;
8854 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
8855 promoted_type, type);
8858 /* We can, however, treat "undefined" any way we please.
8859 Call abort to encourage the user to fix the program. */
8860 if (warned)
8861 inform (loc, "if this code is reached, the program will abort");
8862 /* Before the abort, allow the evaluation of the va_list
8863 expression to exit or longjmp. */
8864 gimplify_and_add (valist, pre_p);
8865 t = build_call_expr_loc (loc,
8866 builtin_decl_implicit (BUILT_IN_TRAP), 0);
8867 gimplify_and_add (t, pre_p);
8869 /* This is dead code, but go ahead and finish so that the
8870 mode of the result comes out right. */
8871 *expr_p = dummy_object (type);
8872 return GS_ALL_DONE;
8874 else
8876 /* Make it easier for the backends by protecting the valist argument
8877 from multiple evaluations. */
8878 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
8880 /* For this case, the backends will be expecting a pointer to
8881 TREE_TYPE (abi), but it's possible we've
8882 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
8883 So fix it. */
8884 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
8886 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
8887 valist = fold_convert_loc (loc, p1,
8888 build_fold_addr_expr_loc (loc, valist));
8891 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
8893 else
8894 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
8896 if (!targetm.gimplify_va_arg_expr)
8897 /* FIXME: Once most targets are converted we should merely
8898 assert this is non-null. */
8899 return GS_ALL_DONE;
8901 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
8902 return GS_OK;
8906 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
8908 DST/SRC are the destination and source respectively. You can pass
8909 ungimplified trees in DST or SRC, in which case they will be
8910 converted to a gimple operand if necessary.
8912 This function returns the newly created GIMPLE_ASSIGN tuple. */
8914 gimple
8915 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
8917 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
8918 gimplify_and_add (t, seq_p);
8919 ggc_free (t);
8920 return gimple_seq_last_stmt (*seq_p);
8923 inline hashval_t
8924 gimplify_hasher::hash (const value_type *p)
8926 tree t = p->val;
8927 return iterative_hash_expr (t, 0);
8930 inline bool
8931 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
8933 tree t1 = p1->val;
8934 tree t2 = p2->val;
8935 enum tree_code code = TREE_CODE (t1);
8937 if (TREE_CODE (t2) != code
8938 || TREE_TYPE (t1) != TREE_TYPE (t2))
8939 return false;
8941 if (!operand_equal_p (t1, t2, 0))
8942 return false;
8944 #ifdef ENABLE_CHECKING
8945 /* Only allow them to compare equal if they also hash equal; otherwise
8946 results are nondeterminate, and we fail bootstrap comparison. */
8947 gcc_assert (hash (p1) == hash (p2));
8948 #endif
8950 return true;