2014-04-24 Segher Boessenkool <segher@kernel.crashing.org>
[official-gcc.git] / gcc / gimplify.c
blobd7470fbb0a672ba0101dddf370166608b3af83ea
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 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2818 case annot_expr_ivdep_kind:
2819 case annot_expr_no_vector_kind:
2820 case annot_expr_vector_kind:
2821 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2822 if (TREE_CODE (type) != BOOLEAN_TYPE)
2823 TREE_TYPE (expr) = boolean_type_node;
2824 return expr;
2825 default:
2826 gcc_unreachable ();
2829 default:
2830 if (COMPARISON_CLASS_P (expr))
2832 /* There expressions always prduce boolean results. */
2833 if (TREE_CODE (type) != BOOLEAN_TYPE)
2834 TREE_TYPE (expr) = boolean_type_node;
2835 return expr;
2837 /* Other expressions that get here must have boolean values, but
2838 might need to be converted to the appropriate mode. */
2839 if (TREE_CODE (type) == BOOLEAN_TYPE)
2840 return expr;
2841 return fold_convert_loc (loc, boolean_type_node, expr);
2845 /* Given a conditional expression *EXPR_P without side effects, gimplify
2846 its operands. New statements are inserted to PRE_P. */
2848 static enum gimplify_status
2849 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2851 tree expr = *expr_p, cond;
2852 enum gimplify_status ret, tret;
2853 enum tree_code code;
2855 cond = gimple_boolify (COND_EXPR_COND (expr));
2857 /* We need to handle && and || specially, as their gimplification
2858 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2859 code = TREE_CODE (cond);
2860 if (code == TRUTH_ANDIF_EXPR)
2861 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2862 else if (code == TRUTH_ORIF_EXPR)
2863 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2864 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2865 COND_EXPR_COND (*expr_p) = cond;
2867 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2868 is_gimple_val, fb_rvalue);
2869 ret = MIN (ret, tret);
2870 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2871 is_gimple_val, fb_rvalue);
2873 return MIN (ret, tret);
2876 /* Return true if evaluating EXPR could trap.
2877 EXPR is GENERIC, while tree_could_trap_p can be called
2878 only on GIMPLE. */
2880 static bool
2881 generic_expr_could_trap_p (tree expr)
2883 unsigned i, n;
2885 if (!expr || is_gimple_val (expr))
2886 return false;
2888 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2889 return true;
2891 n = TREE_OPERAND_LENGTH (expr);
2892 for (i = 0; i < n; i++)
2893 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2894 return true;
2896 return false;
2899 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2900 into
2902 if (p) if (p)
2903 t1 = a; a;
2904 else or else
2905 t1 = b; b;
2908 The second form is used when *EXPR_P is of type void.
2910 PRE_P points to the list where side effects that must happen before
2911 *EXPR_P should be stored. */
2913 static enum gimplify_status
2914 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2916 tree expr = *expr_p;
2917 tree type = TREE_TYPE (expr);
2918 location_t loc = EXPR_LOCATION (expr);
2919 tree tmp, arm1, arm2;
2920 enum gimplify_status ret;
2921 tree label_true, label_false, label_cont;
2922 bool have_then_clause_p, have_else_clause_p;
2923 gimple gimple_cond;
2924 enum tree_code pred_code;
2925 gimple_seq seq = NULL;
2927 /* If this COND_EXPR has a value, copy the values into a temporary within
2928 the arms. */
2929 if (!VOID_TYPE_P (type))
2931 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2932 tree result;
2934 /* If either an rvalue is ok or we do not require an lvalue, create the
2935 temporary. But we cannot do that if the type is addressable. */
2936 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
2937 && !TREE_ADDRESSABLE (type))
2939 if (gimplify_ctxp->allow_rhs_cond_expr
2940 /* If either branch has side effects or could trap, it can't be
2941 evaluated unconditionally. */
2942 && !TREE_SIDE_EFFECTS (then_)
2943 && !generic_expr_could_trap_p (then_)
2944 && !TREE_SIDE_EFFECTS (else_)
2945 && !generic_expr_could_trap_p (else_))
2946 return gimplify_pure_cond_expr (expr_p, pre_p);
2948 tmp = create_tmp_var (type, "iftmp");
2949 result = tmp;
2952 /* Otherwise, only create and copy references to the values. */
2953 else
2955 type = build_pointer_type (type);
2957 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2958 then_ = build_fold_addr_expr_loc (loc, then_);
2960 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2961 else_ = build_fold_addr_expr_loc (loc, else_);
2963 expr
2964 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
2966 tmp = create_tmp_var (type, "iftmp");
2967 result = build_simple_mem_ref_loc (loc, tmp);
2970 /* Build the new then clause, `tmp = then_;'. But don't build the
2971 assignment if the value is void; in C++ it can be if it's a throw. */
2972 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2973 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
2975 /* Similarly, build the new else clause, `tmp = else_;'. */
2976 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2977 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
2979 TREE_TYPE (expr) = void_type_node;
2980 recalculate_side_effects (expr);
2982 /* Move the COND_EXPR to the prequeue. */
2983 gimplify_stmt (&expr, pre_p);
2985 *expr_p = result;
2986 return GS_ALL_DONE;
2989 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
2990 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
2991 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
2992 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
2994 /* Make sure the condition has BOOLEAN_TYPE. */
2995 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2997 /* Break apart && and || conditions. */
2998 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2999 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3001 expr = shortcut_cond_expr (expr);
3003 if (expr != *expr_p)
3005 *expr_p = expr;
3007 /* We can't rely on gimplify_expr to re-gimplify the expanded
3008 form properly, as cleanups might cause the target labels to be
3009 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3010 set up a conditional context. */
3011 gimple_push_condition ();
3012 gimplify_stmt (expr_p, &seq);
3013 gimple_pop_condition (pre_p);
3014 gimple_seq_add_seq (pre_p, seq);
3016 return GS_ALL_DONE;
3020 /* Now do the normal gimplification. */
3022 /* Gimplify condition. */
3023 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3024 fb_rvalue);
3025 if (ret == GS_ERROR)
3026 return GS_ERROR;
3027 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3029 gimple_push_condition ();
3031 have_then_clause_p = have_else_clause_p = false;
3032 if (TREE_OPERAND (expr, 1) != NULL
3033 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3034 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3035 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3036 == current_function_decl)
3037 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3038 have different locations, otherwise we end up with incorrect
3039 location information on the branches. */
3040 && (optimize
3041 || !EXPR_HAS_LOCATION (expr)
3042 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3043 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3045 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3046 have_then_clause_p = true;
3048 else
3049 label_true = create_artificial_label (UNKNOWN_LOCATION);
3050 if (TREE_OPERAND (expr, 2) != NULL
3051 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3052 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3053 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3054 == current_function_decl)
3055 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3056 have different locations, otherwise we end up with incorrect
3057 location information on the branches. */
3058 && (optimize
3059 || !EXPR_HAS_LOCATION (expr)
3060 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3061 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3063 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3064 have_else_clause_p = true;
3066 else
3067 label_false = create_artificial_label (UNKNOWN_LOCATION);
3069 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3070 &arm2);
3072 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3073 label_false);
3075 gimplify_seq_add_stmt (&seq, gimple_cond);
3076 label_cont = NULL_TREE;
3077 if (!have_then_clause_p)
3079 /* For if (...) {} else { code; } put label_true after
3080 the else block. */
3081 if (TREE_OPERAND (expr, 1) == NULL_TREE
3082 && !have_else_clause_p
3083 && TREE_OPERAND (expr, 2) != NULL_TREE)
3084 label_cont = label_true;
3085 else
3087 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3088 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3089 /* For if (...) { code; } else {} or
3090 if (...) { code; } else goto label; or
3091 if (...) { code; return; } else { ... }
3092 label_cont isn't needed. */
3093 if (!have_else_clause_p
3094 && TREE_OPERAND (expr, 2) != NULL_TREE
3095 && gimple_seq_may_fallthru (seq))
3097 gimple g;
3098 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3100 g = gimple_build_goto (label_cont);
3102 /* GIMPLE_COND's are very low level; they have embedded
3103 gotos. This particular embedded goto should not be marked
3104 with the location of the original COND_EXPR, as it would
3105 correspond to the COND_EXPR's condition, not the ELSE or the
3106 THEN arms. To avoid marking it with the wrong location, flag
3107 it as "no location". */
3108 gimple_set_do_not_emit_location (g);
3110 gimplify_seq_add_stmt (&seq, g);
3114 if (!have_else_clause_p)
3116 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3117 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3119 if (label_cont)
3120 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3122 gimple_pop_condition (pre_p);
3123 gimple_seq_add_seq (pre_p, seq);
3125 if (ret == GS_ERROR)
3126 ; /* Do nothing. */
3127 else if (have_then_clause_p || have_else_clause_p)
3128 ret = GS_ALL_DONE;
3129 else
3131 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3132 expr = TREE_OPERAND (expr, 0);
3133 gimplify_stmt (&expr, pre_p);
3136 *expr_p = NULL;
3137 return ret;
3140 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3141 to be marked addressable.
3143 We cannot rely on such an expression being directly markable if a temporary
3144 has been created by the gimplification. In this case, we create another
3145 temporary and initialize it with a copy, which will become a store after we
3146 mark it addressable. This can happen if the front-end passed us something
3147 that it could not mark addressable yet, like a Fortran pass-by-reference
3148 parameter (int) floatvar. */
3150 static void
3151 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3153 while (handled_component_p (*expr_p))
3154 expr_p = &TREE_OPERAND (*expr_p, 0);
3155 if (is_gimple_reg (*expr_p))
3156 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3159 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3160 a call to __builtin_memcpy. */
3162 static enum gimplify_status
3163 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3164 gimple_seq *seq_p)
3166 tree t, to, to_ptr, from, from_ptr;
3167 gimple gs;
3168 location_t loc = EXPR_LOCATION (*expr_p);
3170 to = TREE_OPERAND (*expr_p, 0);
3171 from = TREE_OPERAND (*expr_p, 1);
3173 /* Mark the RHS addressable. Beware that it may not be possible to do so
3174 directly if a temporary has been created by the gimplification. */
3175 prepare_gimple_addressable (&from, seq_p);
3177 mark_addressable (from);
3178 from_ptr = build_fold_addr_expr_loc (loc, from);
3179 gimplify_arg (&from_ptr, seq_p, loc);
3181 mark_addressable (to);
3182 to_ptr = build_fold_addr_expr_loc (loc, to);
3183 gimplify_arg (&to_ptr, seq_p, loc);
3185 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3187 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3189 if (want_value)
3191 /* tmp = memcpy() */
3192 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3193 gimple_call_set_lhs (gs, t);
3194 gimplify_seq_add_stmt (seq_p, gs);
3196 *expr_p = build_simple_mem_ref (t);
3197 return GS_ALL_DONE;
3200 gimplify_seq_add_stmt (seq_p, gs);
3201 *expr_p = NULL;
3202 return GS_ALL_DONE;
3205 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3206 a call to __builtin_memset. In this case we know that the RHS is
3207 a CONSTRUCTOR with an empty element list. */
3209 static enum gimplify_status
3210 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3211 gimple_seq *seq_p)
3213 tree t, from, to, to_ptr;
3214 gimple gs;
3215 location_t loc = EXPR_LOCATION (*expr_p);
3217 /* Assert our assumptions, to abort instead of producing wrong code
3218 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3219 not be immediately exposed. */
3220 from = TREE_OPERAND (*expr_p, 1);
3221 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3222 from = TREE_OPERAND (from, 0);
3224 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3225 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3227 /* Now proceed. */
3228 to = TREE_OPERAND (*expr_p, 0);
3230 to_ptr = build_fold_addr_expr_loc (loc, to);
3231 gimplify_arg (&to_ptr, seq_p, loc);
3232 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3234 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3236 if (want_value)
3238 /* tmp = memset() */
3239 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3240 gimple_call_set_lhs (gs, t);
3241 gimplify_seq_add_stmt (seq_p, gs);
3243 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3244 return GS_ALL_DONE;
3247 gimplify_seq_add_stmt (seq_p, gs);
3248 *expr_p = NULL;
3249 return GS_ALL_DONE;
3252 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3253 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3254 assignment. Return non-null if we detect a potential overlap. */
3256 struct gimplify_init_ctor_preeval_data
3258 /* The base decl of the lhs object. May be NULL, in which case we
3259 have to assume the lhs is indirect. */
3260 tree lhs_base_decl;
3262 /* The alias set of the lhs object. */
3263 alias_set_type lhs_alias_set;
3266 static tree
3267 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3269 struct gimplify_init_ctor_preeval_data *data
3270 = (struct gimplify_init_ctor_preeval_data *) xdata;
3271 tree t = *tp;
3273 /* If we find the base object, obviously we have overlap. */
3274 if (data->lhs_base_decl == t)
3275 return t;
3277 /* If the constructor component is indirect, determine if we have a
3278 potential overlap with the lhs. The only bits of information we
3279 have to go on at this point are addressability and alias sets. */
3280 if ((INDIRECT_REF_P (t)
3281 || TREE_CODE (t) == MEM_REF)
3282 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3283 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3284 return t;
3286 /* If the constructor component is a call, determine if it can hide a
3287 potential overlap with the lhs through an INDIRECT_REF like above.
3288 ??? Ugh - this is completely broken. In fact this whole analysis
3289 doesn't look conservative. */
3290 if (TREE_CODE (t) == CALL_EXPR)
3292 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3294 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3295 if (POINTER_TYPE_P (TREE_VALUE (type))
3296 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3297 && alias_sets_conflict_p (data->lhs_alias_set,
3298 get_alias_set
3299 (TREE_TYPE (TREE_VALUE (type)))))
3300 return t;
3303 if (IS_TYPE_OR_DECL_P (t))
3304 *walk_subtrees = 0;
3305 return NULL;
3308 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3309 force values that overlap with the lhs (as described by *DATA)
3310 into temporaries. */
3312 static void
3313 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3314 struct gimplify_init_ctor_preeval_data *data)
3316 enum gimplify_status one;
3318 /* If the value is constant, then there's nothing to pre-evaluate. */
3319 if (TREE_CONSTANT (*expr_p))
3321 /* Ensure it does not have side effects, it might contain a reference to
3322 the object we're initializing. */
3323 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3324 return;
3327 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3328 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3329 return;
3331 /* Recurse for nested constructors. */
3332 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3334 unsigned HOST_WIDE_INT ix;
3335 constructor_elt *ce;
3336 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3338 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3339 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3341 return;
3344 /* If this is a variable sized type, we must remember the size. */
3345 maybe_with_size_expr (expr_p);
3347 /* Gimplify the constructor element to something appropriate for the rhs
3348 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3349 the gimplifier will consider this a store to memory. Doing this
3350 gimplification now means that we won't have to deal with complicated
3351 language-specific trees, nor trees like SAVE_EXPR that can induce
3352 exponential search behavior. */
3353 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3354 if (one == GS_ERROR)
3356 *expr_p = NULL;
3357 return;
3360 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3361 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3362 always be true for all scalars, since is_gimple_mem_rhs insists on a
3363 temporary variable for them. */
3364 if (DECL_P (*expr_p))
3365 return;
3367 /* If this is of variable size, we have no choice but to assume it doesn't
3368 overlap since we can't make a temporary for it. */
3369 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3370 return;
3372 /* Otherwise, we must search for overlap ... */
3373 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3374 return;
3376 /* ... and if found, force the value into a temporary. */
3377 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3380 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3381 a RANGE_EXPR in a CONSTRUCTOR for an array.
3383 var = lower;
3384 loop_entry:
3385 object[var] = value;
3386 if (var == upper)
3387 goto loop_exit;
3388 var = var + 1;
3389 goto loop_entry;
3390 loop_exit:
3392 We increment var _after_ the loop exit check because we might otherwise
3393 fail if upper == TYPE_MAX_VALUE (type for upper).
3395 Note that we never have to deal with SAVE_EXPRs here, because this has
3396 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3398 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3399 gimple_seq *, bool);
3401 static void
3402 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3403 tree value, tree array_elt_type,
3404 gimple_seq *pre_p, bool cleared)
3406 tree loop_entry_label, loop_exit_label, fall_thru_label;
3407 tree var, var_type, cref, tmp;
3409 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3410 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3411 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3413 /* Create and initialize the index variable. */
3414 var_type = TREE_TYPE (upper);
3415 var = create_tmp_var (var_type, NULL);
3416 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3418 /* Add the loop entry label. */
3419 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3421 /* Build the reference. */
3422 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3423 var, NULL_TREE, NULL_TREE);
3425 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3426 the store. Otherwise just assign value to the reference. */
3428 if (TREE_CODE (value) == CONSTRUCTOR)
3429 /* NB we might have to call ourself recursively through
3430 gimplify_init_ctor_eval if the value is a constructor. */
3431 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3432 pre_p, cleared);
3433 else
3434 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3436 /* We exit the loop when the index var is equal to the upper bound. */
3437 gimplify_seq_add_stmt (pre_p,
3438 gimple_build_cond (EQ_EXPR, var, upper,
3439 loop_exit_label, fall_thru_label));
3441 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3443 /* Otherwise, increment the index var... */
3444 tmp = build2 (PLUS_EXPR, var_type, var,
3445 fold_convert (var_type, integer_one_node));
3446 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3448 /* ...and jump back to the loop entry. */
3449 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3451 /* Add the loop exit label. */
3452 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3455 /* Return true if FDECL is accessing a field that is zero sized. */
3457 static bool
3458 zero_sized_field_decl (const_tree fdecl)
3460 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3461 && integer_zerop (DECL_SIZE (fdecl)))
3462 return true;
3463 return false;
3466 /* Return true if TYPE is zero sized. */
3468 static bool
3469 zero_sized_type (const_tree type)
3471 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3472 && integer_zerop (TYPE_SIZE (type)))
3473 return true;
3474 return false;
3477 /* A subroutine of gimplify_init_constructor. Generate individual
3478 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3479 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3480 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3481 zeroed first. */
3483 static void
3484 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3485 gimple_seq *pre_p, bool cleared)
3487 tree array_elt_type = NULL;
3488 unsigned HOST_WIDE_INT ix;
3489 tree purpose, value;
3491 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3492 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3494 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3496 tree cref;
3498 /* NULL values are created above for gimplification errors. */
3499 if (value == NULL)
3500 continue;
3502 if (cleared && initializer_zerop (value))
3503 continue;
3505 /* ??? Here's to hoping the front end fills in all of the indices,
3506 so we don't have to figure out what's missing ourselves. */
3507 gcc_assert (purpose);
3509 /* Skip zero-sized fields, unless value has side-effects. This can
3510 happen with calls to functions returning a zero-sized type, which
3511 we shouldn't discard. As a number of downstream passes don't
3512 expect sets of zero-sized fields, we rely on the gimplification of
3513 the MODIFY_EXPR we make below to drop the assignment statement. */
3514 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3515 continue;
3517 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3518 whole range. */
3519 if (TREE_CODE (purpose) == RANGE_EXPR)
3521 tree lower = TREE_OPERAND (purpose, 0);
3522 tree upper = TREE_OPERAND (purpose, 1);
3524 /* If the lower bound is equal to upper, just treat it as if
3525 upper was the index. */
3526 if (simple_cst_equal (lower, upper))
3527 purpose = upper;
3528 else
3530 gimplify_init_ctor_eval_range (object, lower, upper, value,
3531 array_elt_type, pre_p, cleared);
3532 continue;
3536 if (array_elt_type)
3538 /* Do not use bitsizetype for ARRAY_REF indices. */
3539 if (TYPE_DOMAIN (TREE_TYPE (object)))
3540 purpose
3541 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3542 purpose);
3543 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3544 purpose, NULL_TREE, NULL_TREE);
3546 else
3548 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3549 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3550 unshare_expr (object), purpose, NULL_TREE);
3553 if (TREE_CODE (value) == CONSTRUCTOR
3554 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3555 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3556 pre_p, cleared);
3557 else
3559 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3560 gimplify_and_add (init, pre_p);
3561 ggc_free (init);
3566 /* Return the appropriate RHS predicate for this LHS. */
3568 gimple_predicate
3569 rhs_predicate_for (tree lhs)
3571 if (is_gimple_reg (lhs))
3572 return is_gimple_reg_rhs_or_call;
3573 else
3574 return is_gimple_mem_rhs_or_call;
3577 /* Gimplify a C99 compound literal expression. This just means adding
3578 the DECL_EXPR before the current statement and using its anonymous
3579 decl instead. */
3581 static enum gimplify_status
3582 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3583 bool (*gimple_test_f) (tree),
3584 fallback_t fallback)
3586 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3587 tree decl = DECL_EXPR_DECL (decl_s);
3588 tree init = DECL_INITIAL (decl);
3589 /* Mark the decl as addressable if the compound literal
3590 expression is addressable now, otherwise it is marked too late
3591 after we gimplify the initialization expression. */
3592 if (TREE_ADDRESSABLE (*expr_p))
3593 TREE_ADDRESSABLE (decl) = 1;
3594 /* Otherwise, if we don't need an lvalue and have a literal directly
3595 substitute it. Check if it matches the gimple predicate, as
3596 otherwise we'd generate a new temporary, and we can as well just
3597 use the decl we already have. */
3598 else if (!TREE_ADDRESSABLE (decl)
3599 && init
3600 && (fallback & fb_lvalue) == 0
3601 && gimple_test_f (init))
3603 *expr_p = init;
3604 return GS_OK;
3607 /* Preliminarily mark non-addressed complex variables as eligible
3608 for promotion to gimple registers. We'll transform their uses
3609 as we find them. */
3610 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3611 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3612 && !TREE_THIS_VOLATILE (decl)
3613 && !needs_to_live_in_memory (decl))
3614 DECL_GIMPLE_REG_P (decl) = 1;
3616 /* If the decl is not addressable, then it is being used in some
3617 expression or on the right hand side of a statement, and it can
3618 be put into a readonly data section. */
3619 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3620 TREE_READONLY (decl) = 1;
3622 /* This decl isn't mentioned in the enclosing block, so add it to the
3623 list of temps. FIXME it seems a bit of a kludge to say that
3624 anonymous artificial vars aren't pushed, but everything else is. */
3625 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3626 gimple_add_tmp_var (decl);
3628 gimplify_and_add (decl_s, pre_p);
3629 *expr_p = decl;
3630 return GS_OK;
3633 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3634 return a new CONSTRUCTOR if something changed. */
3636 static tree
3637 optimize_compound_literals_in_ctor (tree orig_ctor)
3639 tree ctor = orig_ctor;
3640 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3641 unsigned int idx, num = vec_safe_length (elts);
3643 for (idx = 0; idx < num; idx++)
3645 tree value = (*elts)[idx].value;
3646 tree newval = value;
3647 if (TREE_CODE (value) == CONSTRUCTOR)
3648 newval = optimize_compound_literals_in_ctor (value);
3649 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3651 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3652 tree decl = DECL_EXPR_DECL (decl_s);
3653 tree init = DECL_INITIAL (decl);
3655 if (!TREE_ADDRESSABLE (value)
3656 && !TREE_ADDRESSABLE (decl)
3657 && init
3658 && TREE_CODE (init) == CONSTRUCTOR)
3659 newval = optimize_compound_literals_in_ctor (init);
3661 if (newval == value)
3662 continue;
3664 if (ctor == orig_ctor)
3666 ctor = copy_node (orig_ctor);
3667 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3668 elts = CONSTRUCTOR_ELTS (ctor);
3670 (*elts)[idx].value = newval;
3672 return ctor;
3675 /* A subroutine of gimplify_modify_expr. Break out elements of a
3676 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3678 Note that we still need to clear any elements that don't have explicit
3679 initializers, so if not all elements are initialized we keep the
3680 original MODIFY_EXPR, we just remove all of the constructor elements.
3682 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3683 GS_ERROR if we would have to create a temporary when gimplifying
3684 this constructor. Otherwise, return GS_OK.
3686 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3688 static enum gimplify_status
3689 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3690 bool want_value, bool notify_temp_creation)
3692 tree object, ctor, type;
3693 enum gimplify_status ret;
3694 vec<constructor_elt, va_gc> *elts;
3696 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3698 if (!notify_temp_creation)
3700 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3701 is_gimple_lvalue, fb_lvalue);
3702 if (ret == GS_ERROR)
3703 return ret;
3706 object = TREE_OPERAND (*expr_p, 0);
3707 ctor = TREE_OPERAND (*expr_p, 1) =
3708 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3709 type = TREE_TYPE (ctor);
3710 elts = CONSTRUCTOR_ELTS (ctor);
3711 ret = GS_ALL_DONE;
3713 switch (TREE_CODE (type))
3715 case RECORD_TYPE:
3716 case UNION_TYPE:
3717 case QUAL_UNION_TYPE:
3718 case ARRAY_TYPE:
3720 struct gimplify_init_ctor_preeval_data preeval_data;
3721 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3722 bool cleared, complete_p, valid_const_initializer;
3724 /* Aggregate types must lower constructors to initialization of
3725 individual elements. The exception is that a CONSTRUCTOR node
3726 with no elements indicates zero-initialization of the whole. */
3727 if (vec_safe_is_empty (elts))
3729 if (notify_temp_creation)
3730 return GS_OK;
3731 break;
3734 /* Fetch information about the constructor to direct later processing.
3735 We might want to make static versions of it in various cases, and
3736 can only do so if it known to be a valid constant initializer. */
3737 valid_const_initializer
3738 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3739 &num_ctor_elements, &complete_p);
3741 /* If a const aggregate variable is being initialized, then it
3742 should never be a lose to promote the variable to be static. */
3743 if (valid_const_initializer
3744 && num_nonzero_elements > 1
3745 && TREE_READONLY (object)
3746 && TREE_CODE (object) == VAR_DECL
3747 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3749 if (notify_temp_creation)
3750 return GS_ERROR;
3751 DECL_INITIAL (object) = ctor;
3752 TREE_STATIC (object) = 1;
3753 if (!DECL_NAME (object))
3754 DECL_NAME (object) = create_tmp_var_name ("C");
3755 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3757 /* ??? C++ doesn't automatically append a .<number> to the
3758 assembler name, and even when it does, it looks at FE private
3759 data structures to figure out what that number should be,
3760 which are not set for this variable. I suppose this is
3761 important for local statics for inline functions, which aren't
3762 "local" in the object file sense. So in order to get a unique
3763 TU-local symbol, we must invoke the lhd version now. */
3764 lhd_set_decl_assembler_name (object);
3766 *expr_p = NULL_TREE;
3767 break;
3770 /* If there are "lots" of initialized elements, even discounting
3771 those that are not address constants (and thus *must* be
3772 computed at runtime), then partition the constructor into
3773 constant and non-constant parts. Block copy the constant
3774 parts in, then generate code for the non-constant parts. */
3775 /* TODO. There's code in cp/typeck.c to do this. */
3777 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3778 /* store_constructor will ignore the clearing of variable-sized
3779 objects. Initializers for such objects must explicitly set
3780 every field that needs to be set. */
3781 cleared = false;
3782 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3783 /* If the constructor isn't complete, clear the whole object
3784 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3786 ??? This ought not to be needed. For any element not present
3787 in the initializer, we should simply set them to zero. Except
3788 we'd need to *find* the elements that are not present, and that
3789 requires trickery to avoid quadratic compile-time behavior in
3790 large cases or excessive memory use in small cases. */
3791 cleared = true;
3792 else if (num_ctor_elements - num_nonzero_elements
3793 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3794 && num_nonzero_elements < num_ctor_elements / 4)
3795 /* If there are "lots" of zeros, it's more efficient to clear
3796 the memory and then set the nonzero elements. */
3797 cleared = true;
3798 else
3799 cleared = false;
3801 /* If there are "lots" of initialized elements, and all of them
3802 are valid address constants, then the entire initializer can
3803 be dropped to memory, and then memcpy'd out. Don't do this
3804 for sparse arrays, though, as it's more efficient to follow
3805 the standard CONSTRUCTOR behavior of memset followed by
3806 individual element initialization. Also don't do this for small
3807 all-zero initializers (which aren't big enough to merit
3808 clearing), and don't try to make bitwise copies of
3809 TREE_ADDRESSABLE types. */
3810 if (valid_const_initializer
3811 && !(cleared || num_nonzero_elements == 0)
3812 && !TREE_ADDRESSABLE (type))
3814 HOST_WIDE_INT size = int_size_in_bytes (type);
3815 unsigned int align;
3817 /* ??? We can still get unbounded array types, at least
3818 from the C++ front end. This seems wrong, but attempt
3819 to work around it for now. */
3820 if (size < 0)
3822 size = int_size_in_bytes (TREE_TYPE (object));
3823 if (size >= 0)
3824 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3827 /* Find the maximum alignment we can assume for the object. */
3828 /* ??? Make use of DECL_OFFSET_ALIGN. */
3829 if (DECL_P (object))
3830 align = DECL_ALIGN (object);
3831 else
3832 align = TYPE_ALIGN (type);
3834 /* Do a block move either if the size is so small as to make
3835 each individual move a sub-unit move on average, or if it
3836 is so large as to make individual moves inefficient. */
3837 if (size > 0
3838 && num_nonzero_elements > 1
3839 && (size < num_nonzero_elements
3840 || !can_move_by_pieces (size, align)))
3842 if (notify_temp_creation)
3843 return GS_ERROR;
3845 walk_tree (&ctor, force_labels_r, NULL, NULL);
3846 ctor = tree_output_constant_def (ctor);
3847 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3848 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3849 TREE_OPERAND (*expr_p, 1) = ctor;
3851 /* This is no longer an assignment of a CONSTRUCTOR, but
3852 we still may have processing to do on the LHS. So
3853 pretend we didn't do anything here to let that happen. */
3854 return GS_UNHANDLED;
3858 /* If the target is volatile, we have non-zero elements and more than
3859 one field to assign, initialize the target from a temporary. */
3860 if (TREE_THIS_VOLATILE (object)
3861 && !TREE_ADDRESSABLE (type)
3862 && num_nonzero_elements > 0
3863 && vec_safe_length (elts) > 1)
3865 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3866 TREE_OPERAND (*expr_p, 0) = temp;
3867 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3868 *expr_p,
3869 build2 (MODIFY_EXPR, void_type_node,
3870 object, temp));
3871 return GS_OK;
3874 if (notify_temp_creation)
3875 return GS_OK;
3877 /* If there are nonzero elements and if needed, pre-evaluate to capture
3878 elements overlapping with the lhs into temporaries. We must do this
3879 before clearing to fetch the values before they are zeroed-out. */
3880 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3882 preeval_data.lhs_base_decl = get_base_address (object);
3883 if (!DECL_P (preeval_data.lhs_base_decl))
3884 preeval_data.lhs_base_decl = NULL;
3885 preeval_data.lhs_alias_set = get_alias_set (object);
3887 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3888 pre_p, post_p, &preeval_data);
3891 if (cleared)
3893 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3894 Note that we still have to gimplify, in order to handle the
3895 case of variable sized types. Avoid shared tree structures. */
3896 CONSTRUCTOR_ELTS (ctor) = NULL;
3897 TREE_SIDE_EFFECTS (ctor) = 0;
3898 object = unshare_expr (object);
3899 gimplify_stmt (expr_p, pre_p);
3902 /* If we have not block cleared the object, or if there are nonzero
3903 elements in the constructor, add assignments to the individual
3904 scalar fields of the object. */
3905 if (!cleared || num_nonzero_elements > 0)
3906 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3908 *expr_p = NULL_TREE;
3910 break;
3912 case COMPLEX_TYPE:
3914 tree r, i;
3916 if (notify_temp_creation)
3917 return GS_OK;
3919 /* Extract the real and imaginary parts out of the ctor. */
3920 gcc_assert (elts->length () == 2);
3921 r = (*elts)[0].value;
3922 i = (*elts)[1].value;
3923 if (r == NULL || i == NULL)
3925 tree zero = build_zero_cst (TREE_TYPE (type));
3926 if (r == NULL)
3927 r = zero;
3928 if (i == NULL)
3929 i = zero;
3932 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3933 represent creation of a complex value. */
3934 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3936 ctor = build_complex (type, r, i);
3937 TREE_OPERAND (*expr_p, 1) = ctor;
3939 else
3941 ctor = build2 (COMPLEX_EXPR, type, r, i);
3942 TREE_OPERAND (*expr_p, 1) = ctor;
3943 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
3944 pre_p,
3945 post_p,
3946 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3947 fb_rvalue);
3950 break;
3952 case VECTOR_TYPE:
3954 unsigned HOST_WIDE_INT ix;
3955 constructor_elt *ce;
3957 if (notify_temp_creation)
3958 return GS_OK;
3960 /* Go ahead and simplify constant constructors to VECTOR_CST. */
3961 if (TREE_CONSTANT (ctor))
3963 bool constant_p = true;
3964 tree value;
3966 /* Even when ctor is constant, it might contain non-*_CST
3967 elements, such as addresses or trapping values like
3968 1.0/0.0 - 1.0/0.0. Such expressions don't belong
3969 in VECTOR_CST nodes. */
3970 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
3971 if (!CONSTANT_CLASS_P (value))
3973 constant_p = false;
3974 break;
3977 if (constant_p)
3979 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
3980 break;
3983 /* Don't reduce an initializer constant even if we can't
3984 make a VECTOR_CST. It won't do anything for us, and it'll
3985 prevent us from representing it as a single constant. */
3986 if (initializer_constant_valid_p (ctor, type))
3987 break;
3989 TREE_CONSTANT (ctor) = 0;
3992 /* Vector types use CONSTRUCTOR all the way through gimple
3993 compilation as a general initializer. */
3994 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
3996 enum gimplify_status tret;
3997 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
3998 fb_rvalue);
3999 if (tret == GS_ERROR)
4000 ret = GS_ERROR;
4002 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4003 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4005 break;
4007 default:
4008 /* So how did we get a CONSTRUCTOR for a scalar type? */
4009 gcc_unreachable ();
4012 if (ret == GS_ERROR)
4013 return GS_ERROR;
4014 else if (want_value)
4016 *expr_p = object;
4017 return GS_OK;
4019 else
4021 /* If we have gimplified both sides of the initializer but have
4022 not emitted an assignment, do so now. */
4023 if (*expr_p)
4025 tree lhs = TREE_OPERAND (*expr_p, 0);
4026 tree rhs = TREE_OPERAND (*expr_p, 1);
4027 gimple init = gimple_build_assign (lhs, rhs);
4028 gimplify_seq_add_stmt (pre_p, init);
4029 *expr_p = NULL;
4032 return GS_ALL_DONE;
4036 /* Given a pointer value OP0, return a simplified version of an
4037 indirection through OP0, or NULL_TREE if no simplification is
4038 possible. This may only be applied to a rhs of an expression.
4039 Note that the resulting type may be different from the type pointed
4040 to in the sense that it is still compatible from the langhooks
4041 point of view. */
4043 static tree
4044 gimple_fold_indirect_ref_rhs (tree t)
4046 return gimple_fold_indirect_ref (t);
4049 /* Subroutine of gimplify_modify_expr to do simplifications of
4050 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4051 something changes. */
4053 static enum gimplify_status
4054 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4055 gimple_seq *pre_p, gimple_seq *post_p,
4056 bool want_value)
4058 enum gimplify_status ret = GS_UNHANDLED;
4059 bool changed;
4063 changed = false;
4064 switch (TREE_CODE (*from_p))
4066 case VAR_DECL:
4067 /* If we're assigning from a read-only variable initialized with
4068 a constructor, do the direct assignment from the constructor,
4069 but only if neither source nor target are volatile since this
4070 latter assignment might end up being done on a per-field basis. */
4071 if (DECL_INITIAL (*from_p)
4072 && TREE_READONLY (*from_p)
4073 && !TREE_THIS_VOLATILE (*from_p)
4074 && !TREE_THIS_VOLATILE (*to_p)
4075 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4077 tree old_from = *from_p;
4078 enum gimplify_status subret;
4080 /* Move the constructor into the RHS. */
4081 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4083 /* Let's see if gimplify_init_constructor will need to put
4084 it in memory. */
4085 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4086 false, true);
4087 if (subret == GS_ERROR)
4089 /* If so, revert the change. */
4090 *from_p = old_from;
4092 else
4094 ret = GS_OK;
4095 changed = true;
4098 break;
4099 case INDIRECT_REF:
4101 /* If we have code like
4103 *(const A*)(A*)&x
4105 where the type of "x" is a (possibly cv-qualified variant
4106 of "A"), treat the entire expression as identical to "x".
4107 This kind of code arises in C++ when an object is bound
4108 to a const reference, and if "x" is a TARGET_EXPR we want
4109 to take advantage of the optimization below. */
4110 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4111 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4112 if (t)
4114 if (TREE_THIS_VOLATILE (t) != volatile_p)
4116 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4117 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4118 build_fold_addr_expr (t));
4119 if (REFERENCE_CLASS_P (t))
4120 TREE_THIS_VOLATILE (t) = volatile_p;
4122 *from_p = t;
4123 ret = GS_OK;
4124 changed = true;
4126 break;
4129 case TARGET_EXPR:
4131 /* If we are initializing something from a TARGET_EXPR, strip the
4132 TARGET_EXPR and initialize it directly, if possible. This can't
4133 be done if the initializer is void, since that implies that the
4134 temporary is set in some non-trivial way.
4136 ??? What about code that pulls out the temp and uses it
4137 elsewhere? I think that such code never uses the TARGET_EXPR as
4138 an initializer. If I'm wrong, we'll die because the temp won't
4139 have any RTL. In that case, I guess we'll need to replace
4140 references somehow. */
4141 tree init = TARGET_EXPR_INITIAL (*from_p);
4143 if (init
4144 && !VOID_TYPE_P (TREE_TYPE (init)))
4146 *from_p = init;
4147 ret = GS_OK;
4148 changed = true;
4151 break;
4153 case COMPOUND_EXPR:
4154 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4155 caught. */
4156 gimplify_compound_expr (from_p, pre_p, true);
4157 ret = GS_OK;
4158 changed = true;
4159 break;
4161 case CONSTRUCTOR:
4162 /* If we already made some changes, let the front end have a
4163 crack at this before we break it down. */
4164 if (ret != GS_UNHANDLED)
4165 break;
4166 /* If we're initializing from a CONSTRUCTOR, break this into
4167 individual MODIFY_EXPRs. */
4168 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4169 false);
4171 case COND_EXPR:
4172 /* If we're assigning to a non-register type, push the assignment
4173 down into the branches. This is mandatory for ADDRESSABLE types,
4174 since we cannot generate temporaries for such, but it saves a
4175 copy in other cases as well. */
4176 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4178 /* This code should mirror the code in gimplify_cond_expr. */
4179 enum tree_code code = TREE_CODE (*expr_p);
4180 tree cond = *from_p;
4181 tree result = *to_p;
4183 ret = gimplify_expr (&result, pre_p, post_p,
4184 is_gimple_lvalue, fb_lvalue);
4185 if (ret != GS_ERROR)
4186 ret = GS_OK;
4188 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4189 TREE_OPERAND (cond, 1)
4190 = build2 (code, void_type_node, result,
4191 TREE_OPERAND (cond, 1));
4192 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4193 TREE_OPERAND (cond, 2)
4194 = build2 (code, void_type_node, unshare_expr (result),
4195 TREE_OPERAND (cond, 2));
4197 TREE_TYPE (cond) = void_type_node;
4198 recalculate_side_effects (cond);
4200 if (want_value)
4202 gimplify_and_add (cond, pre_p);
4203 *expr_p = unshare_expr (result);
4205 else
4206 *expr_p = cond;
4207 return ret;
4209 break;
4211 case CALL_EXPR:
4212 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4213 return slot so that we don't generate a temporary. */
4214 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4215 && aggregate_value_p (*from_p, *from_p))
4217 bool use_target;
4219 if (!(rhs_predicate_for (*to_p))(*from_p))
4220 /* If we need a temporary, *to_p isn't accurate. */
4221 use_target = false;
4222 /* It's OK to use the return slot directly unless it's an NRV. */
4223 else if (TREE_CODE (*to_p) == RESULT_DECL
4224 && DECL_NAME (*to_p) == NULL_TREE
4225 && needs_to_live_in_memory (*to_p))
4226 use_target = true;
4227 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4228 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4229 /* Don't force regs into memory. */
4230 use_target = false;
4231 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4232 /* It's OK to use the target directly if it's being
4233 initialized. */
4234 use_target = true;
4235 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4236 /* Always use the target and thus RSO for variable-sized types.
4237 GIMPLE cannot deal with a variable-sized assignment
4238 embedded in a call statement. */
4239 use_target = true;
4240 else if (TREE_CODE (*to_p) != SSA_NAME
4241 && (!is_gimple_variable (*to_p)
4242 || needs_to_live_in_memory (*to_p)))
4243 /* Don't use the original target if it's already addressable;
4244 if its address escapes, and the called function uses the
4245 NRV optimization, a conforming program could see *to_p
4246 change before the called function returns; see c++/19317.
4247 When optimizing, the return_slot pass marks more functions
4248 as safe after we have escape info. */
4249 use_target = false;
4250 else
4251 use_target = true;
4253 if (use_target)
4255 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4256 mark_addressable (*to_p);
4259 break;
4261 case WITH_SIZE_EXPR:
4262 /* Likewise for calls that return an aggregate of non-constant size,
4263 since we would not be able to generate a temporary at all. */
4264 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4266 *from_p = TREE_OPERAND (*from_p, 0);
4267 /* We don't change ret in this case because the
4268 WITH_SIZE_EXPR might have been added in
4269 gimplify_modify_expr, so returning GS_OK would lead to an
4270 infinite loop. */
4271 changed = true;
4273 break;
4275 /* If we're initializing from a container, push the initialization
4276 inside it. */
4277 case CLEANUP_POINT_EXPR:
4278 case BIND_EXPR:
4279 case STATEMENT_LIST:
4281 tree wrap = *from_p;
4282 tree t;
4284 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4285 fb_lvalue);
4286 if (ret != GS_ERROR)
4287 ret = GS_OK;
4289 t = voidify_wrapper_expr (wrap, *expr_p);
4290 gcc_assert (t == *expr_p);
4292 if (want_value)
4294 gimplify_and_add (wrap, pre_p);
4295 *expr_p = unshare_expr (*to_p);
4297 else
4298 *expr_p = wrap;
4299 return GS_OK;
4302 case COMPOUND_LITERAL_EXPR:
4304 tree complit = TREE_OPERAND (*expr_p, 1);
4305 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4306 tree decl = DECL_EXPR_DECL (decl_s);
4307 tree init = DECL_INITIAL (decl);
4309 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4310 into struct T x = { 0, 1, 2 } if the address of the
4311 compound literal has never been taken. */
4312 if (!TREE_ADDRESSABLE (complit)
4313 && !TREE_ADDRESSABLE (decl)
4314 && init)
4316 *expr_p = copy_node (*expr_p);
4317 TREE_OPERAND (*expr_p, 1) = init;
4318 return GS_OK;
4322 default:
4323 break;
4326 while (changed);
4328 return ret;
4332 /* Return true if T looks like a valid GIMPLE statement. */
4334 static bool
4335 is_gimple_stmt (tree t)
4337 const enum tree_code code = TREE_CODE (t);
4339 switch (code)
4341 case NOP_EXPR:
4342 /* The only valid NOP_EXPR is the empty statement. */
4343 return IS_EMPTY_STMT (t);
4345 case BIND_EXPR:
4346 case COND_EXPR:
4347 /* These are only valid if they're void. */
4348 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4350 case SWITCH_EXPR:
4351 case GOTO_EXPR:
4352 case RETURN_EXPR:
4353 case LABEL_EXPR:
4354 case CASE_LABEL_EXPR:
4355 case TRY_CATCH_EXPR:
4356 case TRY_FINALLY_EXPR:
4357 case EH_FILTER_EXPR:
4358 case CATCH_EXPR:
4359 case ASM_EXPR:
4360 case STATEMENT_LIST:
4361 case OMP_PARALLEL:
4362 case OMP_FOR:
4363 case OMP_SIMD:
4364 case CILK_SIMD:
4365 case OMP_DISTRIBUTE:
4366 case OMP_SECTIONS:
4367 case OMP_SECTION:
4368 case OMP_SINGLE:
4369 case OMP_MASTER:
4370 case OMP_TASKGROUP:
4371 case OMP_ORDERED:
4372 case OMP_CRITICAL:
4373 case OMP_TASK:
4374 /* These are always void. */
4375 return true;
4377 case CALL_EXPR:
4378 case MODIFY_EXPR:
4379 case PREDICT_EXPR:
4380 /* These are valid regardless of their type. */
4381 return true;
4383 default:
4384 return false;
4389 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4390 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4391 DECL_GIMPLE_REG_P set.
4393 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4394 other, unmodified part of the complex object just before the total store.
4395 As a consequence, if the object is still uninitialized, an undefined value
4396 will be loaded into a register, which may result in a spurious exception
4397 if the register is floating-point and the value happens to be a signaling
4398 NaN for example. Then the fully-fledged complex operations lowering pass
4399 followed by a DCE pass are necessary in order to fix things up. */
4401 static enum gimplify_status
4402 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4403 bool want_value)
4405 enum tree_code code, ocode;
4406 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4408 lhs = TREE_OPERAND (*expr_p, 0);
4409 rhs = TREE_OPERAND (*expr_p, 1);
4410 code = TREE_CODE (lhs);
4411 lhs = TREE_OPERAND (lhs, 0);
4413 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4414 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4415 TREE_NO_WARNING (other) = 1;
4416 other = get_formal_tmp_var (other, pre_p);
4418 realpart = code == REALPART_EXPR ? rhs : other;
4419 imagpart = code == REALPART_EXPR ? other : rhs;
4421 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4422 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4423 else
4424 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4426 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4427 *expr_p = (want_value) ? rhs : NULL_TREE;
4429 return GS_ALL_DONE;
4432 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4434 modify_expr
4435 : varname '=' rhs
4436 | '*' ID '=' rhs
4438 PRE_P points to the list where side effects that must happen before
4439 *EXPR_P should be stored.
4441 POST_P points to the list where side effects that must happen after
4442 *EXPR_P should be stored.
4444 WANT_VALUE is nonzero iff we want to use the value of this expression
4445 in another expression. */
4447 static enum gimplify_status
4448 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4449 bool want_value)
4451 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4452 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4453 enum gimplify_status ret = GS_UNHANDLED;
4454 gimple assign;
4455 location_t loc = EXPR_LOCATION (*expr_p);
4456 gimple_stmt_iterator gsi;
4458 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4459 || TREE_CODE (*expr_p) == INIT_EXPR);
4461 /* Trying to simplify a clobber using normal logic doesn't work,
4462 so handle it here. */
4463 if (TREE_CLOBBER_P (*from_p))
4465 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4466 if (ret == GS_ERROR)
4467 return ret;
4468 gcc_assert (!want_value
4469 && (TREE_CODE (*to_p) == VAR_DECL
4470 || TREE_CODE (*to_p) == MEM_REF));
4471 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4472 *expr_p = NULL;
4473 return GS_ALL_DONE;
4476 /* Insert pointer conversions required by the middle-end that are not
4477 required by the frontend. This fixes middle-end type checking for
4478 for example gcc.dg/redecl-6.c. */
4479 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4481 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4482 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4483 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4486 /* See if any simplifications can be done based on what the RHS is. */
4487 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4488 want_value);
4489 if (ret != GS_UNHANDLED)
4490 return ret;
4492 /* For zero sized types only gimplify the left hand side and right hand
4493 side as statements and throw away the assignment. Do this after
4494 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4495 types properly. */
4496 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4498 gimplify_stmt (from_p, pre_p);
4499 gimplify_stmt (to_p, pre_p);
4500 *expr_p = NULL_TREE;
4501 return GS_ALL_DONE;
4504 /* If the value being copied is of variable width, compute the length
4505 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4506 before gimplifying any of the operands so that we can resolve any
4507 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4508 the size of the expression to be copied, not of the destination, so
4509 that is what we must do here. */
4510 maybe_with_size_expr (from_p);
4512 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4513 if (ret == GS_ERROR)
4514 return ret;
4516 /* As a special case, we have to temporarily allow for assignments
4517 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4518 a toplevel statement, when gimplifying the GENERIC expression
4519 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4520 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4522 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4523 prevent gimplify_expr from trying to create a new temporary for
4524 foo's LHS, we tell it that it should only gimplify until it
4525 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4526 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4527 and all we need to do here is set 'a' to be its LHS. */
4528 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4529 fb_rvalue);
4530 if (ret == GS_ERROR)
4531 return ret;
4533 /* Now see if the above changed *from_p to something we handle specially. */
4534 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4535 want_value);
4536 if (ret != GS_UNHANDLED)
4537 return ret;
4539 /* If we've got a variable sized assignment between two lvalues (i.e. does
4540 not involve a call), then we can make things a bit more straightforward
4541 by converting the assignment to memcpy or memset. */
4542 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4544 tree from = TREE_OPERAND (*from_p, 0);
4545 tree size = TREE_OPERAND (*from_p, 1);
4547 if (TREE_CODE (from) == CONSTRUCTOR)
4548 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4550 if (is_gimple_addressable (from))
4552 *from_p = from;
4553 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4554 pre_p);
4558 /* Transform partial stores to non-addressable complex variables into
4559 total stores. This allows us to use real instead of virtual operands
4560 for these variables, which improves optimization. */
4561 if ((TREE_CODE (*to_p) == REALPART_EXPR
4562 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4563 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4564 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4566 /* Try to alleviate the effects of the gimplification creating artificial
4567 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4568 if (!gimplify_ctxp->into_ssa
4569 && TREE_CODE (*from_p) == VAR_DECL
4570 && DECL_IGNORED_P (*from_p)
4571 && DECL_P (*to_p)
4572 && !DECL_IGNORED_P (*to_p))
4574 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4575 DECL_NAME (*from_p)
4576 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4577 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4578 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4581 if (want_value && TREE_THIS_VOLATILE (*to_p))
4582 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4584 if (TREE_CODE (*from_p) == CALL_EXPR)
4586 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4587 instead of a GIMPLE_ASSIGN. */
4588 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4589 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4590 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4591 tree fndecl = get_callee_fndecl (*from_p);
4592 if (fndecl
4593 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4594 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4595 && call_expr_nargs (*from_p) == 3)
4596 assign = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4597 CALL_EXPR_ARG (*from_p, 0),
4598 CALL_EXPR_ARG (*from_p, 1),
4599 CALL_EXPR_ARG (*from_p, 2));
4600 else
4602 assign = gimple_build_call_from_tree (*from_p);
4603 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4605 notice_special_calls (assign);
4606 if (!gimple_call_noreturn_p (assign))
4607 gimple_call_set_lhs (assign, *to_p);
4609 else
4611 assign = gimple_build_assign (*to_p, *from_p);
4612 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4615 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4617 /* We should have got an SSA name from the start. */
4618 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4621 gimplify_seq_add_stmt (pre_p, assign);
4622 gsi = gsi_last (*pre_p);
4623 maybe_fold_stmt (&gsi);
4625 if (want_value)
4627 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4628 return GS_OK;
4630 else
4631 *expr_p = NULL;
4633 return GS_ALL_DONE;
4636 /* Gimplify a comparison between two variable-sized objects. Do this
4637 with a call to BUILT_IN_MEMCMP. */
4639 static enum gimplify_status
4640 gimplify_variable_sized_compare (tree *expr_p)
4642 location_t loc = EXPR_LOCATION (*expr_p);
4643 tree op0 = TREE_OPERAND (*expr_p, 0);
4644 tree op1 = TREE_OPERAND (*expr_p, 1);
4645 tree t, arg, dest, src, expr;
4647 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4648 arg = unshare_expr (arg);
4649 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4650 src = build_fold_addr_expr_loc (loc, op1);
4651 dest = build_fold_addr_expr_loc (loc, op0);
4652 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4653 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4655 expr
4656 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4657 SET_EXPR_LOCATION (expr, loc);
4658 *expr_p = expr;
4660 return GS_OK;
4663 /* Gimplify a comparison between two aggregate objects of integral scalar
4664 mode as a comparison between the bitwise equivalent scalar values. */
4666 static enum gimplify_status
4667 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4669 location_t loc = EXPR_LOCATION (*expr_p);
4670 tree op0 = TREE_OPERAND (*expr_p, 0);
4671 tree op1 = TREE_OPERAND (*expr_p, 1);
4673 tree type = TREE_TYPE (op0);
4674 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4676 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4677 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4679 *expr_p
4680 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4682 return GS_OK;
4685 /* Gimplify an expression sequence. This function gimplifies each
4686 expression and rewrites the original expression with the last
4687 expression of the sequence in GIMPLE form.
4689 PRE_P points to the list where the side effects for all the
4690 expressions in the sequence will be emitted.
4692 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4694 static enum gimplify_status
4695 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4697 tree t = *expr_p;
4701 tree *sub_p = &TREE_OPERAND (t, 0);
4703 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4704 gimplify_compound_expr (sub_p, pre_p, false);
4705 else
4706 gimplify_stmt (sub_p, pre_p);
4708 t = TREE_OPERAND (t, 1);
4710 while (TREE_CODE (t) == COMPOUND_EXPR);
4712 *expr_p = t;
4713 if (want_value)
4714 return GS_OK;
4715 else
4717 gimplify_stmt (expr_p, pre_p);
4718 return GS_ALL_DONE;
4722 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4723 gimplify. After gimplification, EXPR_P will point to a new temporary
4724 that holds the original value of the SAVE_EXPR node.
4726 PRE_P points to the list where side effects that must happen before
4727 *EXPR_P should be stored. */
4729 static enum gimplify_status
4730 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4732 enum gimplify_status ret = GS_ALL_DONE;
4733 tree val;
4735 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4736 val = TREE_OPERAND (*expr_p, 0);
4738 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4739 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4741 /* The operand may be a void-valued expression such as SAVE_EXPRs
4742 generated by the Java frontend for class initialization. It is
4743 being executed only for its side-effects. */
4744 if (TREE_TYPE (val) == void_type_node)
4746 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4747 is_gimple_stmt, fb_none);
4748 val = NULL;
4750 else
4751 val = get_initialized_tmp_var (val, pre_p, post_p);
4753 TREE_OPERAND (*expr_p, 0) = val;
4754 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4757 *expr_p = val;
4759 return ret;
4762 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4764 unary_expr
4765 : ...
4766 | '&' varname
4769 PRE_P points to the list where side effects that must happen before
4770 *EXPR_P should be stored.
4772 POST_P points to the list where side effects that must happen after
4773 *EXPR_P should be stored. */
4775 static enum gimplify_status
4776 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4778 tree expr = *expr_p;
4779 tree op0 = TREE_OPERAND (expr, 0);
4780 enum gimplify_status ret;
4781 location_t loc = EXPR_LOCATION (*expr_p);
4783 switch (TREE_CODE (op0))
4785 case INDIRECT_REF:
4786 do_indirect_ref:
4787 /* Check if we are dealing with an expression of the form '&*ptr'.
4788 While the front end folds away '&*ptr' into 'ptr', these
4789 expressions may be generated internally by the compiler (e.g.,
4790 builtins like __builtin_va_end). */
4791 /* Caution: the silent array decomposition semantics we allow for
4792 ADDR_EXPR means we can't always discard the pair. */
4793 /* Gimplification of the ADDR_EXPR operand may drop
4794 cv-qualification conversions, so make sure we add them if
4795 needed. */
4797 tree op00 = TREE_OPERAND (op0, 0);
4798 tree t_expr = TREE_TYPE (expr);
4799 tree t_op00 = TREE_TYPE (op00);
4801 if (!useless_type_conversion_p (t_expr, t_op00))
4802 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4803 *expr_p = op00;
4804 ret = GS_OK;
4806 break;
4808 case VIEW_CONVERT_EXPR:
4809 /* Take the address of our operand and then convert it to the type of
4810 this ADDR_EXPR.
4812 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4813 all clear. The impact of this transformation is even less clear. */
4815 /* If the operand is a useless conversion, look through it. Doing so
4816 guarantees that the ADDR_EXPR and its operand will remain of the
4817 same type. */
4818 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4819 op0 = TREE_OPERAND (op0, 0);
4821 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4822 build_fold_addr_expr_loc (loc,
4823 TREE_OPERAND (op0, 0)));
4824 ret = GS_OK;
4825 break;
4827 default:
4828 /* We use fb_either here because the C frontend sometimes takes
4829 the address of a call that returns a struct; see
4830 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4831 the implied temporary explicit. */
4833 /* Make the operand addressable. */
4834 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4835 is_gimple_addressable, fb_either);
4836 if (ret == GS_ERROR)
4837 break;
4839 /* Then mark it. Beware that it may not be possible to do so directly
4840 if a temporary has been created by the gimplification. */
4841 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4843 op0 = TREE_OPERAND (expr, 0);
4845 /* For various reasons, the gimplification of the expression
4846 may have made a new INDIRECT_REF. */
4847 if (TREE_CODE (op0) == INDIRECT_REF)
4848 goto do_indirect_ref;
4850 mark_addressable (TREE_OPERAND (expr, 0));
4852 /* The FEs may end up building ADDR_EXPRs early on a decl with
4853 an incomplete type. Re-build ADDR_EXPRs in canonical form
4854 here. */
4855 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4856 *expr_p = build_fold_addr_expr (op0);
4858 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4859 recompute_tree_invariant_for_addr_expr (*expr_p);
4861 /* If we re-built the ADDR_EXPR add a conversion to the original type
4862 if required. */
4863 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4864 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4866 break;
4869 return ret;
4872 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4873 value; output operands should be a gimple lvalue. */
4875 static enum gimplify_status
4876 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4878 tree expr;
4879 int noutputs;
4880 const char **oconstraints;
4881 int i;
4882 tree link;
4883 const char *constraint;
4884 bool allows_mem, allows_reg, is_inout;
4885 enum gimplify_status ret, tret;
4886 gimple stmt;
4887 vec<tree, va_gc> *inputs;
4888 vec<tree, va_gc> *outputs;
4889 vec<tree, va_gc> *clobbers;
4890 vec<tree, va_gc> *labels;
4891 tree link_next;
4893 expr = *expr_p;
4894 noutputs = list_length (ASM_OUTPUTS (expr));
4895 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4897 inputs = NULL;
4898 outputs = NULL;
4899 clobbers = NULL;
4900 labels = NULL;
4902 ret = GS_ALL_DONE;
4903 link_next = NULL_TREE;
4904 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4906 bool ok;
4907 size_t constraint_len;
4909 link_next = TREE_CHAIN (link);
4911 oconstraints[i]
4912 = constraint
4913 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4914 constraint_len = strlen (constraint);
4915 if (constraint_len == 0)
4916 continue;
4918 ok = parse_output_constraint (&constraint, i, 0, 0,
4919 &allows_mem, &allows_reg, &is_inout);
4920 if (!ok)
4922 ret = GS_ERROR;
4923 is_inout = false;
4926 if (!allows_reg && allows_mem)
4927 mark_addressable (TREE_VALUE (link));
4929 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4930 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4931 fb_lvalue | fb_mayfail);
4932 if (tret == GS_ERROR)
4934 error ("invalid lvalue in asm output %d", i);
4935 ret = tret;
4938 vec_safe_push (outputs, link);
4939 TREE_CHAIN (link) = NULL_TREE;
4941 if (is_inout)
4943 /* An input/output operand. To give the optimizers more
4944 flexibility, split it into separate input and output
4945 operands. */
4946 tree input;
4947 char buf[10];
4949 /* Turn the in/out constraint into an output constraint. */
4950 char *p = xstrdup (constraint);
4951 p[0] = '=';
4952 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4954 /* And add a matching input constraint. */
4955 if (allows_reg)
4957 sprintf (buf, "%d", i);
4959 /* If there are multiple alternatives in the constraint,
4960 handle each of them individually. Those that allow register
4961 will be replaced with operand number, the others will stay
4962 unchanged. */
4963 if (strchr (p, ',') != NULL)
4965 size_t len = 0, buflen = strlen (buf);
4966 char *beg, *end, *str, *dst;
4968 for (beg = p + 1;;)
4970 end = strchr (beg, ',');
4971 if (end == NULL)
4972 end = strchr (beg, '\0');
4973 if ((size_t) (end - beg) < buflen)
4974 len += buflen + 1;
4975 else
4976 len += end - beg + 1;
4977 if (*end)
4978 beg = end + 1;
4979 else
4980 break;
4983 str = (char *) alloca (len);
4984 for (beg = p + 1, dst = str;;)
4986 const char *tem;
4987 bool mem_p, reg_p, inout_p;
4989 end = strchr (beg, ',');
4990 if (end)
4991 *end = '\0';
4992 beg[-1] = '=';
4993 tem = beg - 1;
4994 parse_output_constraint (&tem, i, 0, 0,
4995 &mem_p, &reg_p, &inout_p);
4996 if (dst != str)
4997 *dst++ = ',';
4998 if (reg_p)
5000 memcpy (dst, buf, buflen);
5001 dst += buflen;
5003 else
5005 if (end)
5006 len = end - beg;
5007 else
5008 len = strlen (beg);
5009 memcpy (dst, beg, len);
5010 dst += len;
5012 if (end)
5013 beg = end + 1;
5014 else
5015 break;
5017 *dst = '\0';
5018 input = build_string (dst - str, str);
5020 else
5021 input = build_string (strlen (buf), buf);
5023 else
5024 input = build_string (constraint_len - 1, constraint + 1);
5026 free (p);
5028 input = build_tree_list (build_tree_list (NULL_TREE, input),
5029 unshare_expr (TREE_VALUE (link)));
5030 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5034 link_next = NULL_TREE;
5035 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5037 link_next = TREE_CHAIN (link);
5038 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5039 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5040 oconstraints, &allows_mem, &allows_reg);
5042 /* If we can't make copies, we can only accept memory. */
5043 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5045 if (allows_mem)
5046 allows_reg = 0;
5047 else
5049 error ("impossible constraint in %<asm%>");
5050 error ("non-memory input %d must stay in memory", i);
5051 return GS_ERROR;
5055 /* If the operand is a memory input, it should be an lvalue. */
5056 if (!allows_reg && allows_mem)
5058 tree inputv = TREE_VALUE (link);
5059 STRIP_NOPS (inputv);
5060 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5061 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5062 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5063 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5064 TREE_VALUE (link) = error_mark_node;
5065 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5066 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5067 mark_addressable (TREE_VALUE (link));
5068 if (tret == GS_ERROR)
5070 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5071 input_location = EXPR_LOCATION (TREE_VALUE (link));
5072 error ("memory input %d is not directly addressable", i);
5073 ret = tret;
5076 else
5078 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5079 is_gimple_asm_val, fb_rvalue);
5080 if (tret == GS_ERROR)
5081 ret = tret;
5084 TREE_CHAIN (link) = NULL_TREE;
5085 vec_safe_push (inputs, link);
5088 link_next = NULL_TREE;
5089 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5091 link_next = TREE_CHAIN (link);
5092 TREE_CHAIN (link) = NULL_TREE;
5093 vec_safe_push (clobbers, link);
5096 link_next = NULL_TREE;
5097 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5099 link_next = TREE_CHAIN (link);
5100 TREE_CHAIN (link) = NULL_TREE;
5101 vec_safe_push (labels, link);
5104 /* Do not add ASMs with errors to the gimple IL stream. */
5105 if (ret != GS_ERROR)
5107 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5108 inputs, outputs, clobbers, labels);
5110 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5111 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5113 gimplify_seq_add_stmt (pre_p, stmt);
5116 return ret;
5119 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5120 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5121 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5122 return to this function.
5124 FIXME should we complexify the prequeue handling instead? Or use flags
5125 for all the cleanups and let the optimizer tighten them up? The current
5126 code seems pretty fragile; it will break on a cleanup within any
5127 non-conditional nesting. But any such nesting would be broken, anyway;
5128 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5129 and continues out of it. We can do that at the RTL level, though, so
5130 having an optimizer to tighten up try/finally regions would be a Good
5131 Thing. */
5133 static enum gimplify_status
5134 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5136 gimple_stmt_iterator iter;
5137 gimple_seq body_sequence = NULL;
5139 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5141 /* We only care about the number of conditions between the innermost
5142 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5143 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5144 int old_conds = gimplify_ctxp->conditions;
5145 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5146 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5147 gimplify_ctxp->conditions = 0;
5148 gimplify_ctxp->conditional_cleanups = NULL;
5149 gimplify_ctxp->in_cleanup_point_expr = true;
5151 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5153 gimplify_ctxp->conditions = old_conds;
5154 gimplify_ctxp->conditional_cleanups = old_cleanups;
5155 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5157 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5159 gimple wce = gsi_stmt (iter);
5161 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5163 if (gsi_one_before_end_p (iter))
5165 /* Note that gsi_insert_seq_before and gsi_remove do not
5166 scan operands, unlike some other sequence mutators. */
5167 if (!gimple_wce_cleanup_eh_only (wce))
5168 gsi_insert_seq_before_without_update (&iter,
5169 gimple_wce_cleanup (wce),
5170 GSI_SAME_STMT);
5171 gsi_remove (&iter, true);
5172 break;
5174 else
5176 gimple_statement_try *gtry;
5177 gimple_seq seq;
5178 enum gimple_try_flags kind;
5180 if (gimple_wce_cleanup_eh_only (wce))
5181 kind = GIMPLE_TRY_CATCH;
5182 else
5183 kind = GIMPLE_TRY_FINALLY;
5184 seq = gsi_split_seq_after (iter);
5186 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5187 /* Do not use gsi_replace here, as it may scan operands.
5188 We want to do a simple structural modification only. */
5189 gsi_set_stmt (&iter, gtry);
5190 iter = gsi_start (gtry->eval);
5193 else
5194 gsi_next (&iter);
5197 gimplify_seq_add_seq (pre_p, body_sequence);
5198 if (temp)
5200 *expr_p = temp;
5201 return GS_OK;
5203 else
5205 *expr_p = NULL;
5206 return GS_ALL_DONE;
5210 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5211 is the cleanup action required. EH_ONLY is true if the cleanup should
5212 only be executed if an exception is thrown, not on normal exit. */
5214 static void
5215 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5217 gimple wce;
5218 gimple_seq cleanup_stmts = NULL;
5220 /* Errors can result in improperly nested cleanups. Which results in
5221 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5222 if (seen_error ())
5223 return;
5225 if (gimple_conditional_context ())
5227 /* If we're in a conditional context, this is more complex. We only
5228 want to run the cleanup if we actually ran the initialization that
5229 necessitates it, but we want to run it after the end of the
5230 conditional context. So we wrap the try/finally around the
5231 condition and use a flag to determine whether or not to actually
5232 run the destructor. Thus
5234 test ? f(A()) : 0
5236 becomes (approximately)
5238 flag = 0;
5239 try {
5240 if (test) { A::A(temp); flag = 1; val = f(temp); }
5241 else { val = 0; }
5242 } finally {
5243 if (flag) A::~A(temp);
5247 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5248 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5249 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5251 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5252 gimplify_stmt (&cleanup, &cleanup_stmts);
5253 wce = gimple_build_wce (cleanup_stmts);
5255 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5256 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5257 gimplify_seq_add_stmt (pre_p, ftrue);
5259 /* Because of this manipulation, and the EH edges that jump
5260 threading cannot redirect, the temporary (VAR) will appear
5261 to be used uninitialized. Don't warn. */
5262 TREE_NO_WARNING (var) = 1;
5264 else
5266 gimplify_stmt (&cleanup, &cleanup_stmts);
5267 wce = gimple_build_wce (cleanup_stmts);
5268 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5269 gimplify_seq_add_stmt (pre_p, wce);
5273 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5275 static enum gimplify_status
5276 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5278 tree targ = *expr_p;
5279 tree temp = TARGET_EXPR_SLOT (targ);
5280 tree init = TARGET_EXPR_INITIAL (targ);
5281 enum gimplify_status ret;
5283 if (init)
5285 tree cleanup = NULL_TREE;
5287 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5288 to the temps list. Handle also variable length TARGET_EXPRs. */
5289 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5291 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5292 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5293 gimplify_vla_decl (temp, pre_p);
5295 else
5296 gimple_add_tmp_var (temp);
5298 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5299 expression is supposed to initialize the slot. */
5300 if (VOID_TYPE_P (TREE_TYPE (init)))
5301 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5302 else
5304 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5305 init = init_expr;
5306 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5307 init = NULL;
5308 ggc_free (init_expr);
5310 if (ret == GS_ERROR)
5312 /* PR c++/28266 Make sure this is expanded only once. */
5313 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5314 return GS_ERROR;
5316 if (init)
5317 gimplify_and_add (init, pre_p);
5319 /* If needed, push the cleanup for the temp. */
5320 if (TARGET_EXPR_CLEANUP (targ))
5322 if (CLEANUP_EH_ONLY (targ))
5323 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5324 CLEANUP_EH_ONLY (targ), pre_p);
5325 else
5326 cleanup = TARGET_EXPR_CLEANUP (targ);
5329 /* Add a clobber for the temporary going out of scope, like
5330 gimplify_bind_expr. */
5331 if (gimplify_ctxp->in_cleanup_point_expr
5332 && needs_to_live_in_memory (temp)
5333 && flag_stack_reuse == SR_ALL)
5335 tree clobber = build_constructor (TREE_TYPE (temp),
5336 NULL);
5337 TREE_THIS_VOLATILE (clobber) = true;
5338 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5339 if (cleanup)
5340 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5341 clobber);
5342 else
5343 cleanup = clobber;
5346 if (cleanup)
5347 gimple_push_cleanup (temp, cleanup, false, pre_p);
5349 /* Only expand this once. */
5350 TREE_OPERAND (targ, 3) = init;
5351 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5353 else
5354 /* We should have expanded this before. */
5355 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5357 *expr_p = temp;
5358 return GS_OK;
5361 /* Gimplification of expression trees. */
5363 /* Gimplify an expression which appears at statement context. The
5364 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5365 NULL, a new sequence is allocated.
5367 Return true if we actually added a statement to the queue. */
5369 bool
5370 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5372 gimple_seq_node last;
5374 last = gimple_seq_last (*seq_p);
5375 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5376 return last != gimple_seq_last (*seq_p);
5379 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5380 to CTX. If entries already exist, force them to be some flavor of private.
5381 If there is no enclosing parallel, do nothing. */
5383 void
5384 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5386 splay_tree_node n;
5388 if (decl == NULL || !DECL_P (decl))
5389 return;
5393 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5394 if (n != NULL)
5396 if (n->value & GOVD_SHARED)
5397 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5398 else if (n->value & GOVD_MAP)
5399 n->value |= GOVD_MAP_TO_ONLY;
5400 else
5401 return;
5403 else if (ctx->region_type == ORT_TARGET)
5404 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5405 else if (ctx->region_type != ORT_WORKSHARE
5406 && ctx->region_type != ORT_SIMD
5407 && ctx->region_type != ORT_TARGET_DATA)
5408 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5410 ctx = ctx->outer_context;
5412 while (ctx);
5415 /* Similarly for each of the type sizes of TYPE. */
5417 static void
5418 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5420 if (type == NULL || type == error_mark_node)
5421 return;
5422 type = TYPE_MAIN_VARIANT (type);
5424 if (pointer_set_insert (ctx->privatized_types, type))
5425 return;
5427 switch (TREE_CODE (type))
5429 case INTEGER_TYPE:
5430 case ENUMERAL_TYPE:
5431 case BOOLEAN_TYPE:
5432 case REAL_TYPE:
5433 case FIXED_POINT_TYPE:
5434 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5435 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5436 break;
5438 case ARRAY_TYPE:
5439 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5440 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5441 break;
5443 case RECORD_TYPE:
5444 case UNION_TYPE:
5445 case QUAL_UNION_TYPE:
5447 tree field;
5448 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5449 if (TREE_CODE (field) == FIELD_DECL)
5451 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5452 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5455 break;
5457 case POINTER_TYPE:
5458 case REFERENCE_TYPE:
5459 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5460 break;
5462 default:
5463 break;
5466 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5467 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5468 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5471 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5473 static void
5474 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5476 splay_tree_node n;
5477 unsigned int nflags;
5478 tree t;
5480 if (error_operand_p (decl))
5481 return;
5483 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5484 there are constructors involved somewhere. */
5485 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5486 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5487 flags |= GOVD_SEEN;
5489 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5490 if (n != NULL && n->value != GOVD_ALIGNED)
5492 /* We shouldn't be re-adding the decl with the same data
5493 sharing class. */
5494 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5495 /* The only combination of data sharing classes we should see is
5496 FIRSTPRIVATE and LASTPRIVATE. */
5497 nflags = n->value | flags;
5498 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5499 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5500 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5501 n->value = nflags;
5502 return;
5505 /* When adding a variable-sized variable, we have to handle all sorts
5506 of additional bits of data: the pointer replacement variable, and
5507 the parameters of the type. */
5508 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5510 /* Add the pointer replacement variable as PRIVATE if the variable
5511 replacement is private, else FIRSTPRIVATE since we'll need the
5512 address of the original variable either for SHARED, or for the
5513 copy into or out of the context. */
5514 if (!(flags & GOVD_LOCAL))
5516 nflags = flags & GOVD_MAP
5517 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5518 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5519 nflags |= flags & GOVD_SEEN;
5520 t = DECL_VALUE_EXPR (decl);
5521 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5522 t = TREE_OPERAND (t, 0);
5523 gcc_assert (DECL_P (t));
5524 omp_add_variable (ctx, t, nflags);
5527 /* Add all of the variable and type parameters (which should have
5528 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5529 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5530 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5531 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5533 /* The variable-sized variable itself is never SHARED, only some form
5534 of PRIVATE. The sharing would take place via the pointer variable
5535 which we remapped above. */
5536 if (flags & GOVD_SHARED)
5537 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5538 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5540 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5541 alloca statement we generate for the variable, so make sure it
5542 is available. This isn't automatically needed for the SHARED
5543 case, since we won't be allocating local storage then.
5544 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5545 in this case omp_notice_variable will be called later
5546 on when it is gimplified. */
5547 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5548 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5549 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5551 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5552 && lang_hooks.decls.omp_privatize_by_reference (decl))
5554 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5556 /* Similar to the direct variable sized case above, we'll need the
5557 size of references being privatized. */
5558 if ((flags & GOVD_SHARED) == 0)
5560 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5561 if (TREE_CODE (t) != INTEGER_CST)
5562 omp_notice_variable (ctx, t, true);
5566 if (n != NULL)
5567 n->value |= flags;
5568 else
5569 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5572 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5573 This just prints out diagnostics about threadprivate variable uses
5574 in untied tasks. If DECL2 is non-NULL, prevent this warning
5575 on that variable. */
5577 static bool
5578 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5579 tree decl2)
5581 splay_tree_node n;
5582 struct gimplify_omp_ctx *octx;
5584 for (octx = ctx; octx; octx = octx->outer_context)
5585 if (octx->region_type == ORT_TARGET)
5587 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5588 if (n == NULL)
5590 error ("threadprivate variable %qE used in target region",
5591 DECL_NAME (decl));
5592 error_at (octx->location, "enclosing target region");
5593 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5595 if (decl2)
5596 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5599 if (ctx->region_type != ORT_UNTIED_TASK)
5600 return false;
5601 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5602 if (n == NULL)
5604 error ("threadprivate variable %qE used in untied task",
5605 DECL_NAME (decl));
5606 error_at (ctx->location, "enclosing task");
5607 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5609 if (decl2)
5610 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5611 return false;
5614 /* Record the fact that DECL was used within the OpenMP context CTX.
5615 IN_CODE is true when real code uses DECL, and false when we should
5616 merely emit default(none) errors. Return true if DECL is going to
5617 be remapped and thus DECL shouldn't be gimplified into its
5618 DECL_VALUE_EXPR (if any). */
5620 static bool
5621 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5623 splay_tree_node n;
5624 unsigned flags = in_code ? GOVD_SEEN : 0;
5625 bool ret = false, shared;
5627 if (error_operand_p (decl))
5628 return false;
5630 /* Threadprivate variables are predetermined. */
5631 if (is_global_var (decl))
5633 if (DECL_THREAD_LOCAL_P (decl))
5634 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5636 if (DECL_HAS_VALUE_EXPR_P (decl))
5638 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5640 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5641 return omp_notice_threadprivate_variable (ctx, decl, value);
5645 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5646 if (ctx->region_type == ORT_TARGET)
5648 if (n == NULL)
5650 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5652 error ("%qD referenced in target region does not have "
5653 "a mappable type", decl);
5654 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5656 else
5657 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5659 else
5660 n->value |= flags;
5661 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5662 goto do_outer;
5665 if (n == NULL)
5667 enum omp_clause_default_kind default_kind, kind;
5668 struct gimplify_omp_ctx *octx;
5670 if (ctx->region_type == ORT_WORKSHARE
5671 || ctx->region_type == ORT_SIMD
5672 || ctx->region_type == ORT_TARGET_DATA)
5673 goto do_outer;
5675 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5676 remapped firstprivate instead of shared. To some extent this is
5677 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5678 default_kind = ctx->default_kind;
5679 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5680 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5681 default_kind = kind;
5683 switch (default_kind)
5685 case OMP_CLAUSE_DEFAULT_NONE:
5686 if ((ctx->region_type & ORT_TASK) != 0)
5688 error ("%qE not specified in enclosing task",
5689 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5690 error_at (ctx->location, "enclosing task");
5692 else if (ctx->region_type == ORT_TEAMS)
5694 error ("%qE not specified in enclosing teams construct",
5695 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5696 error_at (ctx->location, "enclosing teams construct");
5698 else
5700 error ("%qE not specified in enclosing parallel",
5701 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5702 error_at (ctx->location, "enclosing parallel");
5704 /* FALLTHRU */
5705 case OMP_CLAUSE_DEFAULT_SHARED:
5706 flags |= GOVD_SHARED;
5707 break;
5708 case OMP_CLAUSE_DEFAULT_PRIVATE:
5709 flags |= GOVD_PRIVATE;
5710 break;
5711 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5712 flags |= GOVD_FIRSTPRIVATE;
5713 break;
5714 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5715 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5716 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5717 if (ctx->outer_context)
5718 omp_notice_variable (ctx->outer_context, decl, in_code);
5719 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5721 splay_tree_node n2;
5723 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5724 continue;
5725 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5726 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5728 flags |= GOVD_FIRSTPRIVATE;
5729 break;
5731 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5732 break;
5734 if (flags & GOVD_FIRSTPRIVATE)
5735 break;
5736 if (octx == NULL
5737 && (TREE_CODE (decl) == PARM_DECL
5738 || (!is_global_var (decl)
5739 && DECL_CONTEXT (decl) == current_function_decl)))
5741 flags |= GOVD_FIRSTPRIVATE;
5742 break;
5744 flags |= GOVD_SHARED;
5745 break;
5746 default:
5747 gcc_unreachable ();
5750 if ((flags & GOVD_PRIVATE)
5751 && lang_hooks.decls.omp_private_outer_ref (decl))
5752 flags |= GOVD_PRIVATE_OUTER_REF;
5754 omp_add_variable (ctx, decl, flags);
5756 shared = (flags & GOVD_SHARED) != 0;
5757 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5758 goto do_outer;
5761 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5762 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5763 && DECL_SIZE (decl)
5764 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5766 splay_tree_node n2;
5767 tree t = DECL_VALUE_EXPR (decl);
5768 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5769 t = TREE_OPERAND (t, 0);
5770 gcc_assert (DECL_P (t));
5771 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5772 n2->value |= GOVD_SEEN;
5775 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5776 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5778 /* If nothing changed, there's nothing left to do. */
5779 if ((n->value & flags) == flags)
5780 return ret;
5781 flags |= n->value;
5782 n->value = flags;
5784 do_outer:
5785 /* If the variable is private in the current context, then we don't
5786 need to propagate anything to an outer context. */
5787 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5788 return ret;
5789 if (ctx->outer_context
5790 && omp_notice_variable (ctx->outer_context, decl, in_code))
5791 return true;
5792 return ret;
5795 /* Verify that DECL is private within CTX. If there's specific information
5796 to the contrary in the innermost scope, generate an error. */
5798 static bool
5799 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, bool simd)
5801 splay_tree_node n;
5803 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5804 if (n != NULL)
5806 if (n->value & GOVD_SHARED)
5808 if (ctx == gimplify_omp_ctxp)
5810 if (simd)
5811 error ("iteration variable %qE is predetermined linear",
5812 DECL_NAME (decl));
5813 else
5814 error ("iteration variable %qE should be private",
5815 DECL_NAME (decl));
5816 n->value = GOVD_PRIVATE;
5817 return true;
5819 else
5820 return false;
5822 else if ((n->value & GOVD_EXPLICIT) != 0
5823 && (ctx == gimplify_omp_ctxp
5824 || (ctx->region_type == ORT_COMBINED_PARALLEL
5825 && gimplify_omp_ctxp->outer_context == ctx)))
5827 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5828 error ("iteration variable %qE should not be firstprivate",
5829 DECL_NAME (decl));
5830 else if ((n->value & GOVD_REDUCTION) != 0)
5831 error ("iteration variable %qE should not be reduction",
5832 DECL_NAME (decl));
5833 else if (simd && (n->value & GOVD_LASTPRIVATE) != 0)
5834 error ("iteration variable %qE should not be lastprivate",
5835 DECL_NAME (decl));
5836 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5837 error ("iteration variable %qE should not be private",
5838 DECL_NAME (decl));
5839 else if (simd && (n->value & GOVD_LINEAR) != 0)
5840 error ("iteration variable %qE is predetermined linear",
5841 DECL_NAME (decl));
5843 return (ctx == gimplify_omp_ctxp
5844 || (ctx->region_type == ORT_COMBINED_PARALLEL
5845 && gimplify_omp_ctxp->outer_context == ctx));
5848 if (ctx->region_type != ORT_WORKSHARE
5849 && ctx->region_type != ORT_SIMD)
5850 return false;
5851 else if (ctx->outer_context)
5852 return omp_is_private (ctx->outer_context, decl, simd);
5853 return false;
5856 /* Return true if DECL is private within a parallel region
5857 that binds to the current construct's context or in parallel
5858 region's REDUCTION clause. */
5860 static bool
5861 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5863 splay_tree_node n;
5867 ctx = ctx->outer_context;
5868 if (ctx == NULL)
5869 return !(is_global_var (decl)
5870 /* References might be private, but might be shared too,
5871 when checking for copyprivate, assume they might be
5872 private, otherwise assume they might be shared. */
5873 || (!copyprivate
5874 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5876 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5877 continue;
5879 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5880 if (n != NULL)
5881 return (n->value & GOVD_SHARED) == 0;
5883 while (ctx->region_type == ORT_WORKSHARE
5884 || ctx->region_type == ORT_SIMD);
5885 return false;
5888 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5889 and previous omp contexts. */
5891 static void
5892 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5893 enum omp_region_type region_type)
5895 struct gimplify_omp_ctx *ctx, *outer_ctx;
5896 tree c;
5898 ctx = new_omp_context (region_type);
5899 outer_ctx = ctx->outer_context;
5901 while ((c = *list_p) != NULL)
5903 bool remove = false;
5904 bool notice_outer = true;
5905 const char *check_non_private = NULL;
5906 unsigned int flags;
5907 tree decl;
5909 switch (OMP_CLAUSE_CODE (c))
5911 case OMP_CLAUSE_PRIVATE:
5912 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5913 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5915 flags |= GOVD_PRIVATE_OUTER_REF;
5916 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5918 else
5919 notice_outer = false;
5920 goto do_add;
5921 case OMP_CLAUSE_SHARED:
5922 flags = GOVD_SHARED | GOVD_EXPLICIT;
5923 goto do_add;
5924 case OMP_CLAUSE_FIRSTPRIVATE:
5925 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5926 check_non_private = "firstprivate";
5927 goto do_add;
5928 case OMP_CLAUSE_LASTPRIVATE:
5929 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5930 check_non_private = "lastprivate";
5931 goto do_add;
5932 case OMP_CLAUSE_REDUCTION:
5933 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5934 check_non_private = "reduction";
5935 goto do_add;
5936 case OMP_CLAUSE_LINEAR:
5937 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5938 is_gimple_val, fb_rvalue) == GS_ERROR)
5940 remove = true;
5941 break;
5943 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5944 goto do_add;
5946 case OMP_CLAUSE_MAP:
5947 if (OMP_CLAUSE_SIZE (c)
5948 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5949 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5951 remove = true;
5952 break;
5954 decl = OMP_CLAUSE_DECL (c);
5955 if (!DECL_P (decl))
5957 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
5958 NULL, is_gimple_lvalue, fb_lvalue)
5959 == GS_ERROR)
5961 remove = true;
5962 break;
5964 break;
5966 flags = GOVD_MAP | GOVD_EXPLICIT;
5967 goto do_add;
5969 case OMP_CLAUSE_DEPEND:
5970 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
5972 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
5973 NULL, is_gimple_val, fb_rvalue);
5974 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
5976 if (error_operand_p (OMP_CLAUSE_DECL (c)))
5978 remove = true;
5979 break;
5981 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
5982 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
5983 is_gimple_val, fb_rvalue) == GS_ERROR)
5985 remove = true;
5986 break;
5988 break;
5990 case OMP_CLAUSE_TO:
5991 case OMP_CLAUSE_FROM:
5992 if (OMP_CLAUSE_SIZE (c)
5993 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5994 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5996 remove = true;
5997 break;
5999 decl = OMP_CLAUSE_DECL (c);
6000 if (error_operand_p (decl))
6002 remove = true;
6003 break;
6005 if (!DECL_P (decl))
6007 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6008 NULL, is_gimple_lvalue, fb_lvalue)
6009 == GS_ERROR)
6011 remove = true;
6012 break;
6014 break;
6016 goto do_notice;
6018 do_add:
6019 decl = OMP_CLAUSE_DECL (c);
6020 if (error_operand_p (decl))
6022 remove = true;
6023 break;
6025 omp_add_variable (ctx, decl, flags);
6026 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6027 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6029 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6030 GOVD_LOCAL | GOVD_SEEN);
6031 gimplify_omp_ctxp = ctx;
6032 push_gimplify_context ();
6034 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6035 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6037 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6038 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6039 pop_gimplify_context
6040 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6041 push_gimplify_context ();
6042 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6043 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6044 pop_gimplify_context
6045 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6046 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6047 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6049 gimplify_omp_ctxp = outer_ctx;
6051 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6052 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6054 gimplify_omp_ctxp = ctx;
6055 push_gimplify_context ();
6056 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6058 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6059 NULL, NULL);
6060 TREE_SIDE_EFFECTS (bind) = 1;
6061 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6062 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6064 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6065 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6066 pop_gimplify_context
6067 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6068 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6070 gimplify_omp_ctxp = outer_ctx;
6072 if (notice_outer)
6073 goto do_notice;
6074 break;
6076 case OMP_CLAUSE_COPYIN:
6077 case OMP_CLAUSE_COPYPRIVATE:
6078 decl = OMP_CLAUSE_DECL (c);
6079 if (error_operand_p (decl))
6081 remove = true;
6082 break;
6084 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6085 && !remove
6086 && !omp_check_private (ctx, decl, true))
6088 remove = true;
6089 if (is_global_var (decl))
6091 if (DECL_THREAD_LOCAL_P (decl))
6092 remove = false;
6093 else if (DECL_HAS_VALUE_EXPR_P (decl))
6095 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6097 if (value
6098 && DECL_P (value)
6099 && DECL_THREAD_LOCAL_P (value))
6100 remove = false;
6103 if (remove)
6104 error_at (OMP_CLAUSE_LOCATION (c),
6105 "copyprivate variable %qE is not threadprivate"
6106 " or private in outer context", DECL_NAME (decl));
6108 do_notice:
6109 if (outer_ctx)
6110 omp_notice_variable (outer_ctx, decl, true);
6111 if (check_non_private
6112 && region_type == ORT_WORKSHARE
6113 && omp_check_private (ctx, decl, false))
6115 error ("%s variable %qE is private in outer context",
6116 check_non_private, DECL_NAME (decl));
6117 remove = true;
6119 break;
6121 case OMP_CLAUSE_FINAL:
6122 case OMP_CLAUSE_IF:
6123 OMP_CLAUSE_OPERAND (c, 0)
6124 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6125 /* Fall through. */
6127 case OMP_CLAUSE_SCHEDULE:
6128 case OMP_CLAUSE_NUM_THREADS:
6129 case OMP_CLAUSE_NUM_TEAMS:
6130 case OMP_CLAUSE_THREAD_LIMIT:
6131 case OMP_CLAUSE_DIST_SCHEDULE:
6132 case OMP_CLAUSE_DEVICE:
6133 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6134 is_gimple_val, fb_rvalue) == GS_ERROR)
6135 remove = true;
6136 break;
6138 case OMP_CLAUSE_NOWAIT:
6139 case OMP_CLAUSE_ORDERED:
6140 case OMP_CLAUSE_UNTIED:
6141 case OMP_CLAUSE_COLLAPSE:
6142 case OMP_CLAUSE_MERGEABLE:
6143 case OMP_CLAUSE_PROC_BIND:
6144 case OMP_CLAUSE_SAFELEN:
6145 break;
6147 case OMP_CLAUSE_ALIGNED:
6148 decl = OMP_CLAUSE_DECL (c);
6149 if (error_operand_p (decl))
6151 remove = true;
6152 break;
6154 if (!is_global_var (decl)
6155 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6156 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6157 break;
6159 case OMP_CLAUSE_DEFAULT:
6160 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6161 break;
6163 default:
6164 gcc_unreachable ();
6167 if (remove)
6168 *list_p = OMP_CLAUSE_CHAIN (c);
6169 else
6170 list_p = &OMP_CLAUSE_CHAIN (c);
6173 gimplify_omp_ctxp = ctx;
6176 /* For all variables that were not actually used within the context,
6177 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6179 static int
6180 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6182 tree *list_p = (tree *) data;
6183 tree decl = (tree) n->key;
6184 unsigned flags = n->value;
6185 enum omp_clause_code code;
6186 tree clause;
6187 bool private_debug;
6189 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6190 return 0;
6191 if ((flags & GOVD_SEEN) == 0)
6192 return 0;
6193 if (flags & GOVD_DEBUG_PRIVATE)
6195 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6196 private_debug = true;
6198 else if (flags & GOVD_MAP)
6199 private_debug = false;
6200 else
6201 private_debug
6202 = lang_hooks.decls.omp_private_debug_clause (decl,
6203 !!(flags & GOVD_SHARED));
6204 if (private_debug)
6205 code = OMP_CLAUSE_PRIVATE;
6206 else if (flags & GOVD_MAP)
6207 code = OMP_CLAUSE_MAP;
6208 else if (flags & GOVD_SHARED)
6210 if (is_global_var (decl))
6212 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6213 while (ctx != NULL)
6215 splay_tree_node on
6216 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6217 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6218 | GOVD_PRIVATE | GOVD_REDUCTION
6219 | GOVD_LINEAR)) != 0)
6220 break;
6221 ctx = ctx->outer_context;
6223 if (ctx == NULL)
6224 return 0;
6226 code = OMP_CLAUSE_SHARED;
6228 else if (flags & GOVD_PRIVATE)
6229 code = OMP_CLAUSE_PRIVATE;
6230 else if (flags & GOVD_FIRSTPRIVATE)
6231 code = OMP_CLAUSE_FIRSTPRIVATE;
6232 else if (flags & GOVD_LASTPRIVATE)
6233 code = OMP_CLAUSE_LASTPRIVATE;
6234 else if (flags & GOVD_ALIGNED)
6235 return 0;
6236 else
6237 gcc_unreachable ();
6239 clause = build_omp_clause (input_location, code);
6240 OMP_CLAUSE_DECL (clause) = decl;
6241 OMP_CLAUSE_CHAIN (clause) = *list_p;
6242 if (private_debug)
6243 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6244 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6245 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6246 else if (code == OMP_CLAUSE_MAP)
6248 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6249 ? OMP_CLAUSE_MAP_TO
6250 : OMP_CLAUSE_MAP_TOFROM;
6251 if (DECL_SIZE (decl)
6252 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6254 tree decl2 = DECL_VALUE_EXPR (decl);
6255 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6256 decl2 = TREE_OPERAND (decl2, 0);
6257 gcc_assert (DECL_P (decl2));
6258 tree mem = build_simple_mem_ref (decl2);
6259 OMP_CLAUSE_DECL (clause) = mem;
6260 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6261 if (gimplify_omp_ctxp->outer_context)
6263 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6264 omp_notice_variable (ctx, decl2, true);
6265 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6267 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6268 OMP_CLAUSE_MAP);
6269 OMP_CLAUSE_DECL (nc) = decl;
6270 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6271 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6272 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6273 OMP_CLAUSE_CHAIN (clause) = nc;
6276 *list_p = clause;
6277 lang_hooks.decls.omp_finish_clause (clause);
6279 return 0;
6282 static void
6283 gimplify_adjust_omp_clauses (tree *list_p)
6285 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6286 tree c, decl;
6288 while ((c = *list_p) != NULL)
6290 splay_tree_node n;
6291 bool remove = false;
6293 switch (OMP_CLAUSE_CODE (c))
6295 case OMP_CLAUSE_PRIVATE:
6296 case OMP_CLAUSE_SHARED:
6297 case OMP_CLAUSE_FIRSTPRIVATE:
6298 case OMP_CLAUSE_LINEAR:
6299 decl = OMP_CLAUSE_DECL (c);
6300 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6301 remove = !(n->value & GOVD_SEEN);
6302 if (! remove)
6304 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6305 if ((n->value & GOVD_DEBUG_PRIVATE)
6306 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6308 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6309 || ((n->value & GOVD_DATA_SHARE_CLASS)
6310 == GOVD_PRIVATE));
6311 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6312 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6314 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6315 && ctx->outer_context
6316 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6317 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6318 && !is_global_var (decl))
6320 if (ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
6322 n = splay_tree_lookup (ctx->outer_context->variables,
6323 (splay_tree_key) decl);
6324 if (n == NULL
6325 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6327 int flags = OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6328 ? GOVD_LASTPRIVATE : GOVD_SHARED;
6329 if (n == NULL)
6330 omp_add_variable (ctx->outer_context, decl,
6331 flags | GOVD_SEEN);
6332 else
6333 n->value |= flags | GOVD_SEEN;
6336 else
6337 omp_notice_variable (ctx->outer_context, decl, true);
6340 break;
6342 case OMP_CLAUSE_LASTPRIVATE:
6343 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6344 accurately reflect the presence of a FIRSTPRIVATE clause. */
6345 decl = OMP_CLAUSE_DECL (c);
6346 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6347 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6348 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6349 break;
6351 case OMP_CLAUSE_ALIGNED:
6352 decl = OMP_CLAUSE_DECL (c);
6353 if (!is_global_var (decl))
6355 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6356 remove = n == NULL || !(n->value & GOVD_SEEN);
6357 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6359 struct gimplify_omp_ctx *octx;
6360 if (n != NULL
6361 && (n->value & (GOVD_DATA_SHARE_CLASS
6362 & ~GOVD_FIRSTPRIVATE)))
6363 remove = true;
6364 else
6365 for (octx = ctx->outer_context; octx;
6366 octx = octx->outer_context)
6368 n = splay_tree_lookup (octx->variables,
6369 (splay_tree_key) decl);
6370 if (n == NULL)
6371 continue;
6372 if (n->value & GOVD_LOCAL)
6373 break;
6374 /* We have to avoid assigning a shared variable
6375 to itself when trying to add
6376 __builtin_assume_aligned. */
6377 if (n->value & GOVD_SHARED)
6379 remove = true;
6380 break;
6385 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6387 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6388 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6389 remove = true;
6391 break;
6393 case OMP_CLAUSE_MAP:
6394 decl = OMP_CLAUSE_DECL (c);
6395 if (!DECL_P (decl))
6396 break;
6397 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6398 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6399 remove = true;
6400 else if (DECL_SIZE (decl)
6401 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6402 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6404 tree decl2 = DECL_VALUE_EXPR (decl);
6405 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6406 decl2 = TREE_OPERAND (decl2, 0);
6407 gcc_assert (DECL_P (decl2));
6408 tree mem = build_simple_mem_ref (decl2);
6409 OMP_CLAUSE_DECL (c) = mem;
6410 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6411 if (ctx->outer_context)
6413 omp_notice_variable (ctx->outer_context, decl2, true);
6414 omp_notice_variable (ctx->outer_context,
6415 OMP_CLAUSE_SIZE (c), true);
6417 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6418 OMP_CLAUSE_MAP);
6419 OMP_CLAUSE_DECL (nc) = decl;
6420 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6421 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6422 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6423 OMP_CLAUSE_CHAIN (c) = nc;
6424 c = nc;
6426 break;
6428 case OMP_CLAUSE_TO:
6429 case OMP_CLAUSE_FROM:
6430 decl = OMP_CLAUSE_DECL (c);
6431 if (!DECL_P (decl))
6432 break;
6433 if (DECL_SIZE (decl)
6434 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6436 tree decl2 = DECL_VALUE_EXPR (decl);
6437 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6438 decl2 = TREE_OPERAND (decl2, 0);
6439 gcc_assert (DECL_P (decl2));
6440 tree mem = build_simple_mem_ref (decl2);
6441 OMP_CLAUSE_DECL (c) = mem;
6442 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6443 if (ctx->outer_context)
6445 omp_notice_variable (ctx->outer_context, decl2, true);
6446 omp_notice_variable (ctx->outer_context,
6447 OMP_CLAUSE_SIZE (c), true);
6450 break;
6452 case OMP_CLAUSE_REDUCTION:
6453 case OMP_CLAUSE_COPYIN:
6454 case OMP_CLAUSE_COPYPRIVATE:
6455 case OMP_CLAUSE_IF:
6456 case OMP_CLAUSE_NUM_THREADS:
6457 case OMP_CLAUSE_NUM_TEAMS:
6458 case OMP_CLAUSE_THREAD_LIMIT:
6459 case OMP_CLAUSE_DIST_SCHEDULE:
6460 case OMP_CLAUSE_DEVICE:
6461 case OMP_CLAUSE_SCHEDULE:
6462 case OMP_CLAUSE_NOWAIT:
6463 case OMP_CLAUSE_ORDERED:
6464 case OMP_CLAUSE_DEFAULT:
6465 case OMP_CLAUSE_UNTIED:
6466 case OMP_CLAUSE_COLLAPSE:
6467 case OMP_CLAUSE_FINAL:
6468 case OMP_CLAUSE_MERGEABLE:
6469 case OMP_CLAUSE_PROC_BIND:
6470 case OMP_CLAUSE_SAFELEN:
6471 case OMP_CLAUSE_DEPEND:
6472 break;
6474 default:
6475 gcc_unreachable ();
6478 if (remove)
6479 *list_p = OMP_CLAUSE_CHAIN (c);
6480 else
6481 list_p = &OMP_CLAUSE_CHAIN (c);
6484 /* Add in any implicit data sharing. */
6485 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6487 gimplify_omp_ctxp = ctx->outer_context;
6488 delete_omp_context (ctx);
6491 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6492 gimplification of the body, as well as scanning the body for used
6493 variables. We need to do this scan now, because variable-sized
6494 decls will be decomposed during gimplification. */
6496 static void
6497 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6499 tree expr = *expr_p;
6500 gimple g;
6501 gimple_seq body = NULL;
6503 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6504 OMP_PARALLEL_COMBINED (expr)
6505 ? ORT_COMBINED_PARALLEL
6506 : ORT_PARALLEL);
6508 push_gimplify_context ();
6510 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6511 if (gimple_code (g) == GIMPLE_BIND)
6512 pop_gimplify_context (g);
6513 else
6514 pop_gimplify_context (NULL);
6516 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6518 g = gimple_build_omp_parallel (body,
6519 OMP_PARALLEL_CLAUSES (expr),
6520 NULL_TREE, NULL_TREE);
6521 if (OMP_PARALLEL_COMBINED (expr))
6522 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6523 gimplify_seq_add_stmt (pre_p, g);
6524 *expr_p = NULL_TREE;
6527 /* Gimplify the contents of an OMP_TASK statement. This involves
6528 gimplification of the body, as well as scanning the body for used
6529 variables. We need to do this scan now, because variable-sized
6530 decls will be decomposed during gimplification. */
6532 static void
6533 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6535 tree expr = *expr_p;
6536 gimple g;
6537 gimple_seq body = NULL;
6539 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6540 find_omp_clause (OMP_TASK_CLAUSES (expr),
6541 OMP_CLAUSE_UNTIED)
6542 ? ORT_UNTIED_TASK : ORT_TASK);
6544 push_gimplify_context ();
6546 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6547 if (gimple_code (g) == GIMPLE_BIND)
6548 pop_gimplify_context (g);
6549 else
6550 pop_gimplify_context (NULL);
6552 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6554 g = gimple_build_omp_task (body,
6555 OMP_TASK_CLAUSES (expr),
6556 NULL_TREE, NULL_TREE,
6557 NULL_TREE, NULL_TREE, NULL_TREE);
6558 gimplify_seq_add_stmt (pre_p, g);
6559 *expr_p = NULL_TREE;
6562 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6563 with non-NULL OMP_FOR_INIT. */
6565 static tree
6566 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6568 *walk_subtrees = 0;
6569 switch (TREE_CODE (*tp))
6571 case OMP_FOR:
6572 *walk_subtrees = 1;
6573 /* FALLTHRU */
6574 case OMP_SIMD:
6575 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6576 return *tp;
6577 break;
6578 case BIND_EXPR:
6579 case STATEMENT_LIST:
6580 case OMP_PARALLEL:
6581 *walk_subtrees = 1;
6582 break;
6583 default:
6584 break;
6586 return NULL_TREE;
6589 /* Gimplify the gross structure of an OMP_FOR statement. */
6591 static enum gimplify_status
6592 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6594 tree for_stmt, orig_for_stmt, decl, var, t;
6595 enum gimplify_status ret = GS_ALL_DONE;
6596 enum gimplify_status tret;
6597 gimple gfor;
6598 gimple_seq for_body, for_pre_body;
6599 int i;
6600 bool simd;
6601 bitmap has_decl_expr = NULL;
6603 orig_for_stmt = for_stmt = *expr_p;
6605 simd = TREE_CODE (for_stmt) == OMP_SIMD
6606 || TREE_CODE (for_stmt) == CILK_SIMD;
6607 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6608 simd ? ORT_SIMD : ORT_WORKSHARE);
6610 /* Handle OMP_FOR_INIT. */
6611 for_pre_body = NULL;
6612 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6614 has_decl_expr = BITMAP_ALLOC (NULL);
6615 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6616 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6617 == VAR_DECL)
6619 t = OMP_FOR_PRE_BODY (for_stmt);
6620 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6622 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6624 tree_stmt_iterator si;
6625 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6626 tsi_next (&si))
6628 t = tsi_stmt (si);
6629 if (TREE_CODE (t) == DECL_EXPR
6630 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6631 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6635 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6636 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6638 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6640 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6641 NULL, NULL);
6642 gcc_assert (for_stmt != NULL_TREE);
6643 gimplify_omp_ctxp->combined_loop = true;
6646 for_body = NULL;
6647 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6648 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6649 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6650 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6651 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6653 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6654 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6655 decl = TREE_OPERAND (t, 0);
6656 gcc_assert (DECL_P (decl));
6657 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6658 || POINTER_TYPE_P (TREE_TYPE (decl)));
6660 /* Make sure the iteration variable is private. */
6661 tree c = NULL_TREE;
6662 if (orig_for_stmt != for_stmt)
6663 /* Do this only on innermost construct for combined ones. */;
6664 else if (simd)
6666 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6667 (splay_tree_key)decl);
6668 omp_is_private (gimplify_omp_ctxp, decl, simd);
6669 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6670 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6671 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6673 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6674 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6675 if (has_decl_expr
6676 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6677 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6678 OMP_CLAUSE_DECL (c) = decl;
6679 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6680 OMP_FOR_CLAUSES (for_stmt) = c;
6681 omp_add_variable (gimplify_omp_ctxp, decl,
6682 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6684 else
6686 bool lastprivate
6687 = (!has_decl_expr
6688 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6689 c = build_omp_clause (input_location,
6690 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6691 : OMP_CLAUSE_PRIVATE);
6692 OMP_CLAUSE_DECL (c) = decl;
6693 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6694 omp_add_variable (gimplify_omp_ctxp, decl,
6695 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6696 | GOVD_SEEN);
6697 c = NULL_TREE;
6700 else if (omp_is_private (gimplify_omp_ctxp, decl, simd))
6701 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6702 else
6703 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6705 /* If DECL is not a gimple register, create a temporary variable to act
6706 as an iteration counter. This is valid, since DECL cannot be
6707 modified in the body of the loop. */
6708 if (orig_for_stmt != for_stmt)
6709 var = decl;
6710 else if (!is_gimple_reg (decl))
6712 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6713 TREE_OPERAND (t, 0) = var;
6715 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6717 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6719 else
6720 var = decl;
6722 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6723 is_gimple_val, fb_rvalue);
6724 ret = MIN (ret, tret);
6725 if (ret == GS_ERROR)
6726 return ret;
6728 /* Handle OMP_FOR_COND. */
6729 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6730 gcc_assert (COMPARISON_CLASS_P (t));
6731 gcc_assert (TREE_OPERAND (t, 0) == decl);
6733 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6734 is_gimple_val, fb_rvalue);
6735 ret = MIN (ret, tret);
6737 /* Handle OMP_FOR_INCR. */
6738 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6739 switch (TREE_CODE (t))
6741 case PREINCREMENT_EXPR:
6742 case POSTINCREMENT_EXPR:
6744 tree decl = TREE_OPERAND (t, 0);
6745 // c_omp_for_incr_canonicalize_ptr() should have been
6746 // called to massage things appropriately.
6747 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6749 if (orig_for_stmt != for_stmt)
6750 break;
6751 t = build_int_cst (TREE_TYPE (decl), 1);
6752 if (c)
6753 OMP_CLAUSE_LINEAR_STEP (c) = t;
6754 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6755 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6756 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6757 break;
6760 case PREDECREMENT_EXPR:
6761 case POSTDECREMENT_EXPR:
6762 if (orig_for_stmt != for_stmt)
6763 break;
6764 t = build_int_cst (TREE_TYPE (decl), -1);
6765 if (c)
6766 OMP_CLAUSE_LINEAR_STEP (c) = t;
6767 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6768 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6769 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6770 break;
6772 case MODIFY_EXPR:
6773 gcc_assert (TREE_OPERAND (t, 0) == decl);
6774 TREE_OPERAND (t, 0) = var;
6776 t = TREE_OPERAND (t, 1);
6777 switch (TREE_CODE (t))
6779 case PLUS_EXPR:
6780 if (TREE_OPERAND (t, 1) == decl)
6782 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6783 TREE_OPERAND (t, 0) = var;
6784 break;
6787 /* Fallthru. */
6788 case MINUS_EXPR:
6789 case POINTER_PLUS_EXPR:
6790 gcc_assert (TREE_OPERAND (t, 0) == decl);
6791 TREE_OPERAND (t, 0) = var;
6792 break;
6793 default:
6794 gcc_unreachable ();
6797 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6798 is_gimple_val, fb_rvalue);
6799 ret = MIN (ret, tret);
6800 if (c)
6802 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
6803 if (TREE_CODE (t) == MINUS_EXPR)
6805 t = TREE_OPERAND (t, 1);
6806 OMP_CLAUSE_LINEAR_STEP (c)
6807 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
6808 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6809 &for_pre_body, NULL,
6810 is_gimple_val, fb_rvalue);
6811 ret = MIN (ret, tret);
6814 break;
6816 default:
6817 gcc_unreachable ();
6820 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6821 && orig_for_stmt == for_stmt)
6823 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6824 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6825 && OMP_CLAUSE_DECL (c) == decl
6826 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6828 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6829 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6830 gcc_assert (TREE_OPERAND (t, 0) == var);
6831 t = TREE_OPERAND (t, 1);
6832 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6833 || TREE_CODE (t) == MINUS_EXPR
6834 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6835 gcc_assert (TREE_OPERAND (t, 0) == var);
6836 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6837 TREE_OPERAND (t, 1));
6838 gimplify_assign (decl, t,
6839 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6844 BITMAP_FREE (has_decl_expr);
6846 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
6848 if (orig_for_stmt != for_stmt)
6849 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6851 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6852 decl = TREE_OPERAND (t, 0);
6853 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6854 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6855 TREE_OPERAND (t, 0) = var;
6856 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6857 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
6858 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
6861 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
6863 int kind;
6864 switch (TREE_CODE (orig_for_stmt))
6866 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
6867 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
6868 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
6869 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
6870 default:
6871 gcc_unreachable ();
6873 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
6874 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6875 for_pre_body);
6876 if (orig_for_stmt != for_stmt)
6877 gimple_omp_for_set_combined_p (gfor, true);
6878 if (gimplify_omp_ctxp
6879 && (gimplify_omp_ctxp->combined_loop
6880 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
6881 && gimplify_omp_ctxp->outer_context
6882 && gimplify_omp_ctxp->outer_context->combined_loop)))
6884 gimple_omp_for_set_combined_into_p (gfor, true);
6885 if (gimplify_omp_ctxp->combined_loop)
6886 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
6887 else
6888 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
6891 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6893 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6894 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6895 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6896 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6897 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6898 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6899 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6900 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6903 gimplify_seq_add_stmt (pre_p, gfor);
6904 if (ret != GS_ALL_DONE)
6905 return GS_ERROR;
6906 *expr_p = NULL_TREE;
6907 return GS_ALL_DONE;
6910 /* Gimplify the gross structure of other OpenMP constructs.
6911 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
6912 and OMP_TEAMS. */
6914 static void
6915 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6917 tree expr = *expr_p;
6918 gimple stmt;
6919 gimple_seq body = NULL;
6920 enum omp_region_type ort = ORT_WORKSHARE;
6922 switch (TREE_CODE (expr))
6924 case OMP_SECTIONS:
6925 case OMP_SINGLE:
6926 break;
6927 case OMP_TARGET:
6928 ort = ORT_TARGET;
6929 break;
6930 case OMP_TARGET_DATA:
6931 ort = ORT_TARGET_DATA;
6932 break;
6933 case OMP_TEAMS:
6934 ort = ORT_TEAMS;
6935 break;
6936 default:
6937 gcc_unreachable ();
6939 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
6940 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
6942 push_gimplify_context ();
6943 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
6944 if (gimple_code (g) == GIMPLE_BIND)
6945 pop_gimplify_context (g);
6946 else
6947 pop_gimplify_context (NULL);
6948 if (ort == ORT_TARGET_DATA)
6950 gimple_seq cleanup = NULL;
6951 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
6952 g = gimple_build_call (fn, 0);
6953 gimple_seq_add_stmt (&cleanup, g);
6954 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
6955 body = NULL;
6956 gimple_seq_add_stmt (&body, g);
6959 else
6960 gimplify_and_add (OMP_BODY (expr), &body);
6961 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6963 switch (TREE_CODE (expr))
6965 case OMP_SECTIONS:
6966 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6967 break;
6968 case OMP_SINGLE:
6969 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6970 break;
6971 case OMP_TARGET:
6972 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
6973 OMP_CLAUSES (expr));
6974 break;
6975 case OMP_TARGET_DATA:
6976 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
6977 OMP_CLAUSES (expr));
6978 break;
6979 case OMP_TEAMS:
6980 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
6981 break;
6982 default:
6983 gcc_unreachable ();
6986 gimplify_seq_add_stmt (pre_p, stmt);
6987 *expr_p = NULL_TREE;
6990 /* Gimplify the gross structure of OpenMP target update construct. */
6992 static void
6993 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
6995 tree expr = *expr_p;
6996 gimple stmt;
6998 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
6999 ORT_WORKSHARE);
7000 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
7001 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7002 OMP_TARGET_UPDATE_CLAUSES (expr));
7004 gimplify_seq_add_stmt (pre_p, stmt);
7005 *expr_p = NULL_TREE;
7008 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7009 stabilized the lhs of the atomic operation as *ADDR. Return true if
7010 EXPR is this stabilized form. */
7012 static bool
7013 goa_lhs_expr_p (tree expr, tree addr)
7015 /* Also include casts to other type variants. The C front end is fond
7016 of adding these for e.g. volatile variables. This is like
7017 STRIP_TYPE_NOPS but includes the main variant lookup. */
7018 STRIP_USELESS_TYPE_CONVERSION (expr);
7020 if (TREE_CODE (expr) == INDIRECT_REF)
7022 expr = TREE_OPERAND (expr, 0);
7023 while (expr != addr
7024 && (CONVERT_EXPR_P (expr)
7025 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7026 && TREE_CODE (expr) == TREE_CODE (addr)
7027 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7029 expr = TREE_OPERAND (expr, 0);
7030 addr = TREE_OPERAND (addr, 0);
7032 if (expr == addr)
7033 return true;
7034 return (TREE_CODE (addr) == ADDR_EXPR
7035 && TREE_CODE (expr) == ADDR_EXPR
7036 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7038 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7039 return true;
7040 return false;
7043 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7044 expression does not involve the lhs, evaluate it into a temporary.
7045 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7046 or -1 if an error was encountered. */
7048 static int
7049 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7050 tree lhs_var)
7052 tree expr = *expr_p;
7053 int saw_lhs;
7055 if (goa_lhs_expr_p (expr, lhs_addr))
7057 *expr_p = lhs_var;
7058 return 1;
7060 if (is_gimple_val (expr))
7061 return 0;
7063 saw_lhs = 0;
7064 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7066 case tcc_binary:
7067 case tcc_comparison:
7068 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7069 lhs_var);
7070 case tcc_unary:
7071 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7072 lhs_var);
7073 break;
7074 case tcc_expression:
7075 switch (TREE_CODE (expr))
7077 case TRUTH_ANDIF_EXPR:
7078 case TRUTH_ORIF_EXPR:
7079 case TRUTH_AND_EXPR:
7080 case TRUTH_OR_EXPR:
7081 case TRUTH_XOR_EXPR:
7082 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7083 lhs_addr, lhs_var);
7084 case TRUTH_NOT_EXPR:
7085 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7086 lhs_addr, lhs_var);
7087 break;
7088 case COMPOUND_EXPR:
7089 /* Break out any preevaluations from cp_build_modify_expr. */
7090 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7091 expr = TREE_OPERAND (expr, 1))
7092 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7093 *expr_p = expr;
7094 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7095 default:
7096 break;
7098 break;
7099 default:
7100 break;
7103 if (saw_lhs == 0)
7105 enum gimplify_status gs;
7106 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7107 if (gs != GS_ALL_DONE)
7108 saw_lhs = -1;
7111 return saw_lhs;
7114 /* Gimplify an OMP_ATOMIC statement. */
7116 static enum gimplify_status
7117 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7119 tree addr = TREE_OPERAND (*expr_p, 0);
7120 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7121 ? NULL : TREE_OPERAND (*expr_p, 1);
7122 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7123 tree tmp_load;
7124 gimple loadstmt, storestmt;
7126 tmp_load = create_tmp_reg (type, NULL);
7127 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7128 return GS_ERROR;
7130 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7131 != GS_ALL_DONE)
7132 return GS_ERROR;
7134 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7135 gimplify_seq_add_stmt (pre_p, loadstmt);
7136 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7137 != GS_ALL_DONE)
7138 return GS_ERROR;
7140 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7141 rhs = tmp_load;
7142 storestmt = gimple_build_omp_atomic_store (rhs);
7143 gimplify_seq_add_stmt (pre_p, storestmt);
7144 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7146 gimple_omp_atomic_set_seq_cst (loadstmt);
7147 gimple_omp_atomic_set_seq_cst (storestmt);
7149 switch (TREE_CODE (*expr_p))
7151 case OMP_ATOMIC_READ:
7152 case OMP_ATOMIC_CAPTURE_OLD:
7153 *expr_p = tmp_load;
7154 gimple_omp_atomic_set_need_value (loadstmt);
7155 break;
7156 case OMP_ATOMIC_CAPTURE_NEW:
7157 *expr_p = rhs;
7158 gimple_omp_atomic_set_need_value (storestmt);
7159 break;
7160 default:
7161 *expr_p = NULL;
7162 break;
7165 return GS_ALL_DONE;
7168 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7169 body, and adding some EH bits. */
7171 static enum gimplify_status
7172 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7174 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7175 gimple g;
7176 gimple_seq body = NULL;
7177 int subcode = 0;
7179 /* Wrap the transaction body in a BIND_EXPR so we have a context
7180 where to put decls for OpenMP. */
7181 if (TREE_CODE (tbody) != BIND_EXPR)
7183 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7184 TREE_SIDE_EFFECTS (bind) = 1;
7185 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7186 TRANSACTION_EXPR_BODY (expr) = bind;
7189 push_gimplify_context ();
7190 temp = voidify_wrapper_expr (*expr_p, NULL);
7192 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7193 pop_gimplify_context (g);
7195 g = gimple_build_transaction (body, NULL);
7196 if (TRANSACTION_EXPR_OUTER (expr))
7197 subcode = GTMA_IS_OUTER;
7198 else if (TRANSACTION_EXPR_RELAXED (expr))
7199 subcode = GTMA_IS_RELAXED;
7200 gimple_transaction_set_subcode (g, subcode);
7202 gimplify_seq_add_stmt (pre_p, g);
7204 if (temp)
7206 *expr_p = temp;
7207 return GS_OK;
7210 *expr_p = NULL_TREE;
7211 return GS_ALL_DONE;
7214 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7215 expression produces a value to be used as an operand inside a GIMPLE
7216 statement, the value will be stored back in *EXPR_P. This value will
7217 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7218 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7219 emitted in PRE_P and POST_P.
7221 Additionally, this process may overwrite parts of the input
7222 expression during gimplification. Ideally, it should be
7223 possible to do non-destructive gimplification.
7225 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7226 the expression needs to evaluate to a value to be used as
7227 an operand in a GIMPLE statement, this value will be stored in
7228 *EXPR_P on exit. This happens when the caller specifies one
7229 of fb_lvalue or fb_rvalue fallback flags.
7231 PRE_P will contain the sequence of GIMPLE statements corresponding
7232 to the evaluation of EXPR and all the side-effects that must
7233 be executed before the main expression. On exit, the last
7234 statement of PRE_P is the core statement being gimplified. For
7235 instance, when gimplifying 'if (++a)' the last statement in
7236 PRE_P will be 'if (t.1)' where t.1 is the result of
7237 pre-incrementing 'a'.
7239 POST_P will contain the sequence of GIMPLE statements corresponding
7240 to the evaluation of all the side-effects that must be executed
7241 after the main expression. If this is NULL, the post
7242 side-effects are stored at the end of PRE_P.
7244 The reason why the output is split in two is to handle post
7245 side-effects explicitly. In some cases, an expression may have
7246 inner and outer post side-effects which need to be emitted in
7247 an order different from the one given by the recursive
7248 traversal. For instance, for the expression (*p--)++ the post
7249 side-effects of '--' must actually occur *after* the post
7250 side-effects of '++'. However, gimplification will first visit
7251 the inner expression, so if a separate POST sequence was not
7252 used, the resulting sequence would be:
7254 1 t.1 = *p
7255 2 p = p - 1
7256 3 t.2 = t.1 + 1
7257 4 *p = t.2
7259 However, the post-decrement operation in line #2 must not be
7260 evaluated until after the store to *p at line #4, so the
7261 correct sequence should be:
7263 1 t.1 = *p
7264 2 t.2 = t.1 + 1
7265 3 *p = t.2
7266 4 p = p - 1
7268 So, by specifying a separate post queue, it is possible
7269 to emit the post side-effects in the correct order.
7270 If POST_P is NULL, an internal queue will be used. Before
7271 returning to the caller, the sequence POST_P is appended to
7272 the main output sequence PRE_P.
7274 GIMPLE_TEST_F points to a function that takes a tree T and
7275 returns nonzero if T is in the GIMPLE form requested by the
7276 caller. The GIMPLE predicates are in gimple.c.
7278 FALLBACK tells the function what sort of a temporary we want if
7279 gimplification cannot produce an expression that complies with
7280 GIMPLE_TEST_F.
7282 fb_none means that no temporary should be generated
7283 fb_rvalue means that an rvalue is OK to generate
7284 fb_lvalue means that an lvalue is OK to generate
7285 fb_either means that either is OK, but an lvalue is preferable.
7286 fb_mayfail means that gimplification may fail (in which case
7287 GS_ERROR will be returned)
7289 The return value is either GS_ERROR or GS_ALL_DONE, since this
7290 function iterates until EXPR is completely gimplified or an error
7291 occurs. */
7293 enum gimplify_status
7294 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7295 bool (*gimple_test_f) (tree), fallback_t fallback)
7297 tree tmp;
7298 gimple_seq internal_pre = NULL;
7299 gimple_seq internal_post = NULL;
7300 tree save_expr;
7301 bool is_statement;
7302 location_t saved_location;
7303 enum gimplify_status ret;
7304 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7306 save_expr = *expr_p;
7307 if (save_expr == NULL_TREE)
7308 return GS_ALL_DONE;
7310 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7311 is_statement = gimple_test_f == is_gimple_stmt;
7312 if (is_statement)
7313 gcc_assert (pre_p);
7315 /* Consistency checks. */
7316 if (gimple_test_f == is_gimple_reg)
7317 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7318 else if (gimple_test_f == is_gimple_val
7319 || gimple_test_f == is_gimple_call_addr
7320 || gimple_test_f == is_gimple_condexpr
7321 || gimple_test_f == is_gimple_mem_rhs
7322 || gimple_test_f == is_gimple_mem_rhs_or_call
7323 || gimple_test_f == is_gimple_reg_rhs
7324 || gimple_test_f == is_gimple_reg_rhs_or_call
7325 || gimple_test_f == is_gimple_asm_val
7326 || gimple_test_f == is_gimple_mem_ref_addr)
7327 gcc_assert (fallback & fb_rvalue);
7328 else if (gimple_test_f == is_gimple_min_lval
7329 || gimple_test_f == is_gimple_lvalue)
7330 gcc_assert (fallback & fb_lvalue);
7331 else if (gimple_test_f == is_gimple_addressable)
7332 gcc_assert (fallback & fb_either);
7333 else if (gimple_test_f == is_gimple_stmt)
7334 gcc_assert (fallback == fb_none);
7335 else
7337 /* We should have recognized the GIMPLE_TEST_F predicate to
7338 know what kind of fallback to use in case a temporary is
7339 needed to hold the value or address of *EXPR_P. */
7340 gcc_unreachable ();
7343 /* We used to check the predicate here and return immediately if it
7344 succeeds. This is wrong; the design is for gimplification to be
7345 idempotent, and for the predicates to only test for valid forms, not
7346 whether they are fully simplified. */
7347 if (pre_p == NULL)
7348 pre_p = &internal_pre;
7350 if (post_p == NULL)
7351 post_p = &internal_post;
7353 /* Remember the last statements added to PRE_P and POST_P. Every
7354 new statement added by the gimplification helpers needs to be
7355 annotated with location information. To centralize the
7356 responsibility, we remember the last statement that had been
7357 added to both queues before gimplifying *EXPR_P. If
7358 gimplification produces new statements in PRE_P and POST_P, those
7359 statements will be annotated with the same location information
7360 as *EXPR_P. */
7361 pre_last_gsi = gsi_last (*pre_p);
7362 post_last_gsi = gsi_last (*post_p);
7364 saved_location = input_location;
7365 if (save_expr != error_mark_node
7366 && EXPR_HAS_LOCATION (*expr_p))
7367 input_location = EXPR_LOCATION (*expr_p);
7369 /* Loop over the specific gimplifiers until the toplevel node
7370 remains the same. */
7373 /* Strip away as many useless type conversions as possible
7374 at the toplevel. */
7375 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7377 /* Remember the expr. */
7378 save_expr = *expr_p;
7380 /* Die, die, die, my darling. */
7381 if (save_expr == error_mark_node
7382 || (TREE_TYPE (save_expr)
7383 && TREE_TYPE (save_expr) == error_mark_node))
7385 ret = GS_ERROR;
7386 break;
7389 /* Do any language-specific gimplification. */
7390 ret = ((enum gimplify_status)
7391 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7392 if (ret == GS_OK)
7394 if (*expr_p == NULL_TREE)
7395 break;
7396 if (*expr_p != save_expr)
7397 continue;
7399 else if (ret != GS_UNHANDLED)
7400 break;
7402 /* Make sure that all the cases set 'ret' appropriately. */
7403 ret = GS_UNHANDLED;
7404 switch (TREE_CODE (*expr_p))
7406 /* First deal with the special cases. */
7408 case POSTINCREMENT_EXPR:
7409 case POSTDECREMENT_EXPR:
7410 case PREINCREMENT_EXPR:
7411 case PREDECREMENT_EXPR:
7412 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7413 fallback != fb_none,
7414 TREE_TYPE (*expr_p));
7415 break;
7417 case VIEW_CONVERT_EXPR:
7418 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7419 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7421 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7422 post_p, is_gimple_val, fb_rvalue);
7423 recalculate_side_effects (*expr_p);
7424 break;
7426 /* Fallthru. */
7428 case ARRAY_REF:
7429 case ARRAY_RANGE_REF:
7430 case REALPART_EXPR:
7431 case IMAGPART_EXPR:
7432 case COMPONENT_REF:
7433 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7434 fallback ? fallback : fb_rvalue);
7435 break;
7437 case COND_EXPR:
7438 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7440 /* C99 code may assign to an array in a structure value of a
7441 conditional expression, and this has undefined behavior
7442 only on execution, so create a temporary if an lvalue is
7443 required. */
7444 if (fallback == fb_lvalue)
7446 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7447 mark_addressable (*expr_p);
7448 ret = GS_OK;
7450 break;
7452 case CALL_EXPR:
7453 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7455 /* C99 code may assign to an array in a structure returned
7456 from a function, and this has undefined behavior only on
7457 execution, so create a temporary if an lvalue is
7458 required. */
7459 if (fallback == fb_lvalue)
7461 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7462 mark_addressable (*expr_p);
7463 ret = GS_OK;
7465 break;
7467 case TREE_LIST:
7468 gcc_unreachable ();
7470 case COMPOUND_EXPR:
7471 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7472 break;
7474 case COMPOUND_LITERAL_EXPR:
7475 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7476 gimple_test_f, fallback);
7477 break;
7479 case MODIFY_EXPR:
7480 case INIT_EXPR:
7481 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7482 fallback != fb_none);
7483 break;
7485 case TRUTH_ANDIF_EXPR:
7486 case TRUTH_ORIF_EXPR:
7488 /* Preserve the original type of the expression and the
7489 source location of the outer expression. */
7490 tree org_type = TREE_TYPE (*expr_p);
7491 *expr_p = gimple_boolify (*expr_p);
7492 *expr_p = build3_loc (input_location, COND_EXPR,
7493 org_type, *expr_p,
7494 fold_convert_loc
7495 (input_location,
7496 org_type, boolean_true_node),
7497 fold_convert_loc
7498 (input_location,
7499 org_type, boolean_false_node));
7500 ret = GS_OK;
7501 break;
7504 case TRUTH_NOT_EXPR:
7506 tree type = TREE_TYPE (*expr_p);
7507 /* The parsers are careful to generate TRUTH_NOT_EXPR
7508 only with operands that are always zero or one.
7509 We do not fold here but handle the only interesting case
7510 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7511 *expr_p = gimple_boolify (*expr_p);
7512 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7513 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7514 TREE_TYPE (*expr_p),
7515 TREE_OPERAND (*expr_p, 0));
7516 else
7517 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7518 TREE_TYPE (*expr_p),
7519 TREE_OPERAND (*expr_p, 0),
7520 build_int_cst (TREE_TYPE (*expr_p), 1));
7521 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7522 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7523 ret = GS_OK;
7524 break;
7527 case ADDR_EXPR:
7528 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7529 break;
7531 case ANNOTATE_EXPR:
7533 tree cond = TREE_OPERAND (*expr_p, 0);
7534 tree kind = TREE_OPERAND (*expr_p, 1);
7535 tree type = TREE_TYPE (cond);
7536 if (!INTEGRAL_TYPE_P (type))
7538 *expr_p = cond;
7539 ret = GS_OK;
7540 break;
7542 tree tmp = create_tmp_var (type, NULL);
7543 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7544 gimple call
7545 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7546 gimple_call_set_lhs (call, tmp);
7547 gimplify_seq_add_stmt (pre_p, call);
7548 *expr_p = tmp;
7549 ret = GS_ALL_DONE;
7550 break;
7553 case VA_ARG_EXPR:
7554 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7555 break;
7557 CASE_CONVERT:
7558 if (IS_EMPTY_STMT (*expr_p))
7560 ret = GS_ALL_DONE;
7561 break;
7564 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7565 || fallback == fb_none)
7567 /* Just strip a conversion to void (or in void context) and
7568 try again. */
7569 *expr_p = TREE_OPERAND (*expr_p, 0);
7570 ret = GS_OK;
7571 break;
7574 ret = gimplify_conversion (expr_p);
7575 if (ret == GS_ERROR)
7576 break;
7577 if (*expr_p != save_expr)
7578 break;
7579 /* FALLTHRU */
7581 case FIX_TRUNC_EXPR:
7582 /* unary_expr: ... | '(' cast ')' val | ... */
7583 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7584 is_gimple_val, fb_rvalue);
7585 recalculate_side_effects (*expr_p);
7586 break;
7588 case INDIRECT_REF:
7590 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7591 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7592 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7594 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7595 if (*expr_p != save_expr)
7597 ret = GS_OK;
7598 break;
7601 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7602 is_gimple_reg, fb_rvalue);
7603 if (ret == GS_ERROR)
7604 break;
7606 recalculate_side_effects (*expr_p);
7607 *expr_p = fold_build2_loc (input_location, MEM_REF,
7608 TREE_TYPE (*expr_p),
7609 TREE_OPERAND (*expr_p, 0),
7610 build_int_cst (saved_ptr_type, 0));
7611 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7612 TREE_THIS_NOTRAP (*expr_p) = notrap;
7613 ret = GS_OK;
7614 break;
7617 /* We arrive here through the various re-gimplifcation paths. */
7618 case MEM_REF:
7619 /* First try re-folding the whole thing. */
7620 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7621 TREE_OPERAND (*expr_p, 0),
7622 TREE_OPERAND (*expr_p, 1));
7623 if (tmp)
7625 *expr_p = tmp;
7626 recalculate_side_effects (*expr_p);
7627 ret = GS_OK;
7628 break;
7630 /* Avoid re-gimplifying the address operand if it is already
7631 in suitable form. Re-gimplifying would mark the address
7632 operand addressable. Always gimplify when not in SSA form
7633 as we still may have to gimplify decls with value-exprs. */
7634 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7635 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7637 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7638 is_gimple_mem_ref_addr, fb_rvalue);
7639 if (ret == GS_ERROR)
7640 break;
7642 recalculate_side_effects (*expr_p);
7643 ret = GS_ALL_DONE;
7644 break;
7646 /* Constants need not be gimplified. */
7647 case INTEGER_CST:
7648 case REAL_CST:
7649 case FIXED_CST:
7650 case STRING_CST:
7651 case COMPLEX_CST:
7652 case VECTOR_CST:
7653 /* Drop the overflow flag on constants, we do not want
7654 that in the GIMPLE IL. */
7655 if (TREE_OVERFLOW_P (*expr_p))
7656 *expr_p = drop_tree_overflow (*expr_p);
7657 ret = GS_ALL_DONE;
7658 break;
7660 case CONST_DECL:
7661 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7662 CONST_DECL node. Otherwise the decl is replaceable by its
7663 value. */
7664 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7665 if (fallback & fb_lvalue)
7666 ret = GS_ALL_DONE;
7667 else
7669 *expr_p = DECL_INITIAL (*expr_p);
7670 ret = GS_OK;
7672 break;
7674 case DECL_EXPR:
7675 ret = gimplify_decl_expr (expr_p, pre_p);
7676 break;
7678 case BIND_EXPR:
7679 ret = gimplify_bind_expr (expr_p, pre_p);
7680 break;
7682 case LOOP_EXPR:
7683 ret = gimplify_loop_expr (expr_p, pre_p);
7684 break;
7686 case SWITCH_EXPR:
7687 ret = gimplify_switch_expr (expr_p, pre_p);
7688 break;
7690 case EXIT_EXPR:
7691 ret = gimplify_exit_expr (expr_p);
7692 break;
7694 case GOTO_EXPR:
7695 /* If the target is not LABEL, then it is a computed jump
7696 and the target needs to be gimplified. */
7697 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7699 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7700 NULL, is_gimple_val, fb_rvalue);
7701 if (ret == GS_ERROR)
7702 break;
7704 gimplify_seq_add_stmt (pre_p,
7705 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7706 ret = GS_ALL_DONE;
7707 break;
7709 case PREDICT_EXPR:
7710 gimplify_seq_add_stmt (pre_p,
7711 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7712 PREDICT_EXPR_OUTCOME (*expr_p)));
7713 ret = GS_ALL_DONE;
7714 break;
7716 case LABEL_EXPR:
7717 ret = GS_ALL_DONE;
7718 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7719 == current_function_decl);
7720 gimplify_seq_add_stmt (pre_p,
7721 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7722 break;
7724 case CASE_LABEL_EXPR:
7725 ret = gimplify_case_label_expr (expr_p, pre_p);
7726 break;
7728 case RETURN_EXPR:
7729 ret = gimplify_return_expr (*expr_p, pre_p);
7730 break;
7732 case CONSTRUCTOR:
7733 /* Don't reduce this in place; let gimplify_init_constructor work its
7734 magic. Buf if we're just elaborating this for side effects, just
7735 gimplify any element that has side-effects. */
7736 if (fallback == fb_none)
7738 unsigned HOST_WIDE_INT ix;
7739 tree val;
7740 tree temp = NULL_TREE;
7741 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7742 if (TREE_SIDE_EFFECTS (val))
7743 append_to_statement_list (val, &temp);
7745 *expr_p = temp;
7746 ret = temp ? GS_OK : GS_ALL_DONE;
7748 /* C99 code may assign to an array in a constructed
7749 structure or union, and this has undefined behavior only
7750 on execution, so create a temporary if an lvalue is
7751 required. */
7752 else if (fallback == fb_lvalue)
7754 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7755 mark_addressable (*expr_p);
7756 ret = GS_OK;
7758 else
7759 ret = GS_ALL_DONE;
7760 break;
7762 /* The following are special cases that are not handled by the
7763 original GIMPLE grammar. */
7765 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7766 eliminated. */
7767 case SAVE_EXPR:
7768 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7769 break;
7771 case BIT_FIELD_REF:
7772 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7773 post_p, is_gimple_lvalue, fb_either);
7774 recalculate_side_effects (*expr_p);
7775 break;
7777 case TARGET_MEM_REF:
7779 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7781 if (TMR_BASE (*expr_p))
7782 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7783 post_p, is_gimple_mem_ref_addr, fb_either);
7784 if (TMR_INDEX (*expr_p))
7785 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7786 post_p, is_gimple_val, fb_rvalue);
7787 if (TMR_INDEX2 (*expr_p))
7788 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7789 post_p, is_gimple_val, fb_rvalue);
7790 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7791 ret = MIN (r0, r1);
7793 break;
7795 case NON_LVALUE_EXPR:
7796 /* This should have been stripped above. */
7797 gcc_unreachable ();
7799 case ASM_EXPR:
7800 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7801 break;
7803 case TRY_FINALLY_EXPR:
7804 case TRY_CATCH_EXPR:
7806 gimple_seq eval, cleanup;
7807 gimple try_;
7809 /* Calls to destructors are generated automatically in FINALLY/CATCH
7810 block. They should have location as UNKNOWN_LOCATION. However,
7811 gimplify_call_expr will reset these call stmts to input_location
7812 if it finds stmt's location is unknown. To prevent resetting for
7813 destructors, we set the input_location to unknown.
7814 Note that this only affects the destructor calls in FINALLY/CATCH
7815 block, and will automatically reset to its original value by the
7816 end of gimplify_expr. */
7817 input_location = UNKNOWN_LOCATION;
7818 eval = cleanup = NULL;
7819 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7820 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7821 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7822 if (gimple_seq_empty_p (cleanup))
7824 gimple_seq_add_seq (pre_p, eval);
7825 ret = GS_ALL_DONE;
7826 break;
7828 try_ = gimple_build_try (eval, cleanup,
7829 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7830 ? GIMPLE_TRY_FINALLY
7831 : GIMPLE_TRY_CATCH);
7832 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7833 gimple_set_location (try_, saved_location);
7834 else
7835 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7836 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7837 gimple_try_set_catch_is_cleanup (try_,
7838 TRY_CATCH_IS_CLEANUP (*expr_p));
7839 gimplify_seq_add_stmt (pre_p, try_);
7840 ret = GS_ALL_DONE;
7841 break;
7844 case CLEANUP_POINT_EXPR:
7845 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7846 break;
7848 case TARGET_EXPR:
7849 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7850 break;
7852 case CATCH_EXPR:
7854 gimple c;
7855 gimple_seq handler = NULL;
7856 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7857 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7858 gimplify_seq_add_stmt (pre_p, c);
7859 ret = GS_ALL_DONE;
7860 break;
7863 case EH_FILTER_EXPR:
7865 gimple ehf;
7866 gimple_seq failure = NULL;
7868 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7869 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7870 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7871 gimplify_seq_add_stmt (pre_p, ehf);
7872 ret = GS_ALL_DONE;
7873 break;
7876 case OBJ_TYPE_REF:
7878 enum gimplify_status r0, r1;
7879 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7880 post_p, is_gimple_val, fb_rvalue);
7881 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7882 post_p, is_gimple_val, fb_rvalue);
7883 TREE_SIDE_EFFECTS (*expr_p) = 0;
7884 ret = MIN (r0, r1);
7886 break;
7888 case LABEL_DECL:
7889 /* We get here when taking the address of a label. We mark
7890 the label as "forced"; meaning it can never be removed and
7891 it is a potential target for any computed goto. */
7892 FORCED_LABEL (*expr_p) = 1;
7893 ret = GS_ALL_DONE;
7894 break;
7896 case STATEMENT_LIST:
7897 ret = gimplify_statement_list (expr_p, pre_p);
7898 break;
7900 case WITH_SIZE_EXPR:
7902 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7903 post_p == &internal_post ? NULL : post_p,
7904 gimple_test_f, fallback);
7905 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7906 is_gimple_val, fb_rvalue);
7907 ret = GS_ALL_DONE;
7909 break;
7911 case VAR_DECL:
7912 case PARM_DECL:
7913 ret = gimplify_var_or_parm_decl (expr_p);
7914 break;
7916 case RESULT_DECL:
7917 /* When within an OpenMP context, notice uses of variables. */
7918 if (gimplify_omp_ctxp)
7919 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7920 ret = GS_ALL_DONE;
7921 break;
7923 case SSA_NAME:
7924 /* Allow callbacks into the gimplifier during optimization. */
7925 ret = GS_ALL_DONE;
7926 break;
7928 case OMP_PARALLEL:
7929 gimplify_omp_parallel (expr_p, pre_p);
7930 ret = GS_ALL_DONE;
7931 break;
7933 case OMP_TASK:
7934 gimplify_omp_task (expr_p, pre_p);
7935 ret = GS_ALL_DONE;
7936 break;
7938 case OMP_FOR:
7939 case OMP_SIMD:
7940 case CILK_SIMD:
7941 case OMP_DISTRIBUTE:
7942 ret = gimplify_omp_for (expr_p, pre_p);
7943 break;
7945 case OMP_SECTIONS:
7946 case OMP_SINGLE:
7947 case OMP_TARGET:
7948 case OMP_TARGET_DATA:
7949 case OMP_TEAMS:
7950 gimplify_omp_workshare (expr_p, pre_p);
7951 ret = GS_ALL_DONE;
7952 break;
7954 case OMP_TARGET_UPDATE:
7955 gimplify_omp_target_update (expr_p, pre_p);
7956 ret = GS_ALL_DONE;
7957 break;
7959 case OMP_SECTION:
7960 case OMP_MASTER:
7961 case OMP_TASKGROUP:
7962 case OMP_ORDERED:
7963 case OMP_CRITICAL:
7965 gimple_seq body = NULL;
7966 gimple g;
7968 gimplify_and_add (OMP_BODY (*expr_p), &body);
7969 switch (TREE_CODE (*expr_p))
7971 case OMP_SECTION:
7972 g = gimple_build_omp_section (body);
7973 break;
7974 case OMP_MASTER:
7975 g = gimple_build_omp_master (body);
7976 break;
7977 case OMP_TASKGROUP:
7979 gimple_seq cleanup = NULL;
7980 tree fn
7981 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
7982 g = gimple_build_call (fn, 0);
7983 gimple_seq_add_stmt (&cleanup, g);
7984 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7985 body = NULL;
7986 gimple_seq_add_stmt (&body, g);
7987 g = gimple_build_omp_taskgroup (body);
7989 break;
7990 case OMP_ORDERED:
7991 g = gimple_build_omp_ordered (body);
7992 break;
7993 case OMP_CRITICAL:
7994 g = gimple_build_omp_critical (body,
7995 OMP_CRITICAL_NAME (*expr_p));
7996 break;
7997 default:
7998 gcc_unreachable ();
8000 gimplify_seq_add_stmt (pre_p, g);
8001 ret = GS_ALL_DONE;
8002 break;
8005 case OMP_ATOMIC:
8006 case OMP_ATOMIC_READ:
8007 case OMP_ATOMIC_CAPTURE_OLD:
8008 case OMP_ATOMIC_CAPTURE_NEW:
8009 ret = gimplify_omp_atomic (expr_p, pre_p);
8010 break;
8012 case TRANSACTION_EXPR:
8013 ret = gimplify_transaction (expr_p, pre_p);
8014 break;
8016 case TRUTH_AND_EXPR:
8017 case TRUTH_OR_EXPR:
8018 case TRUTH_XOR_EXPR:
8020 tree orig_type = TREE_TYPE (*expr_p);
8021 tree new_type, xop0, xop1;
8022 *expr_p = gimple_boolify (*expr_p);
8023 new_type = TREE_TYPE (*expr_p);
8024 if (!useless_type_conversion_p (orig_type, new_type))
8026 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8027 ret = GS_OK;
8028 break;
8031 /* Boolified binary truth expressions are semantically equivalent
8032 to bitwise binary expressions. Canonicalize them to the
8033 bitwise variant. */
8034 switch (TREE_CODE (*expr_p))
8036 case TRUTH_AND_EXPR:
8037 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8038 break;
8039 case TRUTH_OR_EXPR:
8040 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8041 break;
8042 case TRUTH_XOR_EXPR:
8043 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8044 break;
8045 default:
8046 break;
8048 /* Now make sure that operands have compatible type to
8049 expression's new_type. */
8050 xop0 = TREE_OPERAND (*expr_p, 0);
8051 xop1 = TREE_OPERAND (*expr_p, 1);
8052 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8053 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8054 new_type,
8055 xop0);
8056 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8057 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8058 new_type,
8059 xop1);
8060 /* Continue classified as tcc_binary. */
8061 goto expr_2;
8064 case FMA_EXPR:
8065 case VEC_COND_EXPR:
8066 case VEC_PERM_EXPR:
8067 /* Classified as tcc_expression. */
8068 goto expr_3;
8070 case POINTER_PLUS_EXPR:
8072 enum gimplify_status r0, r1;
8073 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8074 post_p, is_gimple_val, fb_rvalue);
8075 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8076 post_p, is_gimple_val, fb_rvalue);
8077 recalculate_side_effects (*expr_p);
8078 ret = MIN (r0, r1);
8079 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8080 after gimplifying operands - this is similar to how
8081 it would be folding all gimplified stmts on creation
8082 to have them canonicalized, which is what we eventually
8083 should do anyway. */
8084 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8085 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8087 *expr_p = build_fold_addr_expr_with_type_loc
8088 (input_location,
8089 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8090 TREE_OPERAND (*expr_p, 0),
8091 fold_convert (ptr_type_node,
8092 TREE_OPERAND (*expr_p, 1))),
8093 TREE_TYPE (*expr_p));
8094 ret = MIN (ret, GS_OK);
8096 break;
8099 case CILK_SYNC_STMT:
8101 if (!fn_contains_cilk_spawn_p (cfun))
8103 error_at (EXPR_LOCATION (*expr_p),
8104 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8105 ret = GS_ERROR;
8107 else
8109 gimplify_cilk_sync (expr_p, pre_p);
8110 ret = GS_ALL_DONE;
8112 break;
8115 default:
8116 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8118 case tcc_comparison:
8119 /* Handle comparison of objects of non scalar mode aggregates
8120 with a call to memcmp. It would be nice to only have to do
8121 this for variable-sized objects, but then we'd have to allow
8122 the same nest of reference nodes we allow for MODIFY_EXPR and
8123 that's too complex.
8125 Compare scalar mode aggregates as scalar mode values. Using
8126 memcmp for them would be very inefficient at best, and is
8127 plain wrong if bitfields are involved. */
8129 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8131 /* Vector comparisons need no boolification. */
8132 if (TREE_CODE (type) == VECTOR_TYPE)
8133 goto expr_2;
8134 else if (!AGGREGATE_TYPE_P (type))
8136 tree org_type = TREE_TYPE (*expr_p);
8137 *expr_p = gimple_boolify (*expr_p);
8138 if (!useless_type_conversion_p (org_type,
8139 TREE_TYPE (*expr_p)))
8141 *expr_p = fold_convert_loc (input_location,
8142 org_type, *expr_p);
8143 ret = GS_OK;
8145 else
8146 goto expr_2;
8148 else if (TYPE_MODE (type) != BLKmode)
8149 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8150 else
8151 ret = gimplify_variable_sized_compare (expr_p);
8153 break;
8156 /* If *EXPR_P does not need to be special-cased, handle it
8157 according to its class. */
8158 case tcc_unary:
8159 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8160 post_p, is_gimple_val, fb_rvalue);
8161 break;
8163 case tcc_binary:
8164 expr_2:
8166 enum gimplify_status r0, r1;
8168 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8169 post_p, is_gimple_val, fb_rvalue);
8170 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8171 post_p, is_gimple_val, fb_rvalue);
8173 ret = MIN (r0, r1);
8174 break;
8177 expr_3:
8179 enum gimplify_status r0, r1, r2;
8181 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8182 post_p, is_gimple_val, fb_rvalue);
8183 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8184 post_p, is_gimple_val, fb_rvalue);
8185 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8186 post_p, is_gimple_val, fb_rvalue);
8188 ret = MIN (MIN (r0, r1), r2);
8189 break;
8192 case tcc_declaration:
8193 case tcc_constant:
8194 ret = GS_ALL_DONE;
8195 goto dont_recalculate;
8197 default:
8198 gcc_unreachable ();
8201 recalculate_side_effects (*expr_p);
8203 dont_recalculate:
8204 break;
8207 gcc_assert (*expr_p || ret != GS_OK);
8209 while (ret == GS_OK);
8211 /* If we encountered an error_mark somewhere nested inside, either
8212 stub out the statement or propagate the error back out. */
8213 if (ret == GS_ERROR)
8215 if (is_statement)
8216 *expr_p = NULL;
8217 goto out;
8220 /* This was only valid as a return value from the langhook, which
8221 we handled. Make sure it doesn't escape from any other context. */
8222 gcc_assert (ret != GS_UNHANDLED);
8224 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8226 /* We aren't looking for a value, and we don't have a valid
8227 statement. If it doesn't have side-effects, throw it away. */
8228 if (!TREE_SIDE_EFFECTS (*expr_p))
8229 *expr_p = NULL;
8230 else if (!TREE_THIS_VOLATILE (*expr_p))
8232 /* This is probably a _REF that contains something nested that
8233 has side effects. Recurse through the operands to find it. */
8234 enum tree_code code = TREE_CODE (*expr_p);
8236 switch (code)
8238 case COMPONENT_REF:
8239 case REALPART_EXPR:
8240 case IMAGPART_EXPR:
8241 case VIEW_CONVERT_EXPR:
8242 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8243 gimple_test_f, fallback);
8244 break;
8246 case ARRAY_REF:
8247 case ARRAY_RANGE_REF:
8248 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8249 gimple_test_f, fallback);
8250 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8251 gimple_test_f, fallback);
8252 break;
8254 default:
8255 /* Anything else with side-effects must be converted to
8256 a valid statement before we get here. */
8257 gcc_unreachable ();
8260 *expr_p = NULL;
8262 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8263 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8265 /* Historically, the compiler has treated a bare reference
8266 to a non-BLKmode volatile lvalue as forcing a load. */
8267 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8269 /* Normally, we do not want to create a temporary for a
8270 TREE_ADDRESSABLE type because such a type should not be
8271 copied by bitwise-assignment. However, we make an
8272 exception here, as all we are doing here is ensuring that
8273 we read the bytes that make up the type. We use
8274 create_tmp_var_raw because create_tmp_var will abort when
8275 given a TREE_ADDRESSABLE type. */
8276 tree tmp = create_tmp_var_raw (type, "vol");
8277 gimple_add_tmp_var (tmp);
8278 gimplify_assign (tmp, *expr_p, pre_p);
8279 *expr_p = NULL;
8281 else
8282 /* We can't do anything useful with a volatile reference to
8283 an incomplete type, so just throw it away. Likewise for
8284 a BLKmode type, since any implicit inner load should
8285 already have been turned into an explicit one by the
8286 gimplification process. */
8287 *expr_p = NULL;
8290 /* If we are gimplifying at the statement level, we're done. Tack
8291 everything together and return. */
8292 if (fallback == fb_none || is_statement)
8294 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8295 it out for GC to reclaim it. */
8296 *expr_p = NULL_TREE;
8298 if (!gimple_seq_empty_p (internal_pre)
8299 || !gimple_seq_empty_p (internal_post))
8301 gimplify_seq_add_seq (&internal_pre, internal_post);
8302 gimplify_seq_add_seq (pre_p, internal_pre);
8305 /* The result of gimplifying *EXPR_P is going to be the last few
8306 statements in *PRE_P and *POST_P. Add location information
8307 to all the statements that were added by the gimplification
8308 helpers. */
8309 if (!gimple_seq_empty_p (*pre_p))
8310 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8312 if (!gimple_seq_empty_p (*post_p))
8313 annotate_all_with_location_after (*post_p, post_last_gsi,
8314 input_location);
8316 goto out;
8319 #ifdef ENABLE_GIMPLE_CHECKING
8320 if (*expr_p)
8322 enum tree_code code = TREE_CODE (*expr_p);
8323 /* These expressions should already be in gimple IR form. */
8324 gcc_assert (code != MODIFY_EXPR
8325 && code != ASM_EXPR
8326 && code != BIND_EXPR
8327 && code != CATCH_EXPR
8328 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8329 && code != EH_FILTER_EXPR
8330 && code != GOTO_EXPR
8331 && code != LABEL_EXPR
8332 && code != LOOP_EXPR
8333 && code != SWITCH_EXPR
8334 && code != TRY_FINALLY_EXPR
8335 && code != OMP_CRITICAL
8336 && code != OMP_FOR
8337 && code != OMP_MASTER
8338 && code != OMP_TASKGROUP
8339 && code != OMP_ORDERED
8340 && code != OMP_PARALLEL
8341 && code != OMP_SECTIONS
8342 && code != OMP_SECTION
8343 && code != OMP_SINGLE);
8345 #endif
8347 /* Otherwise we're gimplifying a subexpression, so the resulting
8348 value is interesting. If it's a valid operand that matches
8349 GIMPLE_TEST_F, we're done. Unless we are handling some
8350 post-effects internally; if that's the case, we need to copy into
8351 a temporary before adding the post-effects to POST_P. */
8352 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8353 goto out;
8355 /* Otherwise, we need to create a new temporary for the gimplified
8356 expression. */
8358 /* We can't return an lvalue if we have an internal postqueue. The
8359 object the lvalue refers to would (probably) be modified by the
8360 postqueue; we need to copy the value out first, which means an
8361 rvalue. */
8362 if ((fallback & fb_lvalue)
8363 && gimple_seq_empty_p (internal_post)
8364 && is_gimple_addressable (*expr_p))
8366 /* An lvalue will do. Take the address of the expression, store it
8367 in a temporary, and replace the expression with an INDIRECT_REF of
8368 that temporary. */
8369 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8370 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8371 *expr_p = build_simple_mem_ref (tmp);
8373 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8375 /* An rvalue will do. Assign the gimplified expression into a
8376 new temporary TMP and replace the original expression with
8377 TMP. First, make sure that the expression has a type so that
8378 it can be assigned into a temporary. */
8379 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8380 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8382 else
8384 #ifdef ENABLE_GIMPLE_CHECKING
8385 if (!(fallback & fb_mayfail))
8387 fprintf (stderr, "gimplification failed:\n");
8388 print_generic_expr (stderr, *expr_p, 0);
8389 debug_tree (*expr_p);
8390 internal_error ("gimplification failed");
8392 #endif
8393 gcc_assert (fallback & fb_mayfail);
8395 /* If this is an asm statement, and the user asked for the
8396 impossible, don't die. Fail and let gimplify_asm_expr
8397 issue an error. */
8398 ret = GS_ERROR;
8399 goto out;
8402 /* Make sure the temporary matches our predicate. */
8403 gcc_assert ((*gimple_test_f) (*expr_p));
8405 if (!gimple_seq_empty_p (internal_post))
8407 annotate_all_with_location (internal_post, input_location);
8408 gimplify_seq_add_seq (pre_p, internal_post);
8411 out:
8412 input_location = saved_location;
8413 return ret;
8416 /* Look through TYPE for variable-sized objects and gimplify each such
8417 size that we find. Add to LIST_P any statements generated. */
8419 void
8420 gimplify_type_sizes (tree type, gimple_seq *list_p)
8422 tree field, t;
8424 if (type == NULL || type == error_mark_node)
8425 return;
8427 /* We first do the main variant, then copy into any other variants. */
8428 type = TYPE_MAIN_VARIANT (type);
8430 /* Avoid infinite recursion. */
8431 if (TYPE_SIZES_GIMPLIFIED (type))
8432 return;
8434 TYPE_SIZES_GIMPLIFIED (type) = 1;
8436 switch (TREE_CODE (type))
8438 case INTEGER_TYPE:
8439 case ENUMERAL_TYPE:
8440 case BOOLEAN_TYPE:
8441 case REAL_TYPE:
8442 case FIXED_POINT_TYPE:
8443 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8444 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8446 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8448 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8449 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8451 break;
8453 case ARRAY_TYPE:
8454 /* These types may not have declarations, so handle them here. */
8455 gimplify_type_sizes (TREE_TYPE (type), list_p);
8456 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8457 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8458 with assigned stack slots, for -O1+ -g they should be tracked
8459 by VTA. */
8460 if (!(TYPE_NAME (type)
8461 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8462 && DECL_IGNORED_P (TYPE_NAME (type)))
8463 && TYPE_DOMAIN (type)
8464 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8466 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8467 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8468 DECL_IGNORED_P (t) = 0;
8469 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8470 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8471 DECL_IGNORED_P (t) = 0;
8473 break;
8475 case RECORD_TYPE:
8476 case UNION_TYPE:
8477 case QUAL_UNION_TYPE:
8478 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8479 if (TREE_CODE (field) == FIELD_DECL)
8481 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8482 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8483 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8484 gimplify_type_sizes (TREE_TYPE (field), list_p);
8486 break;
8488 case POINTER_TYPE:
8489 case REFERENCE_TYPE:
8490 /* We used to recurse on the pointed-to type here, which turned out to
8491 be incorrect because its definition might refer to variables not
8492 yet initialized at this point if a forward declaration is involved.
8494 It was actually useful for anonymous pointed-to types to ensure
8495 that the sizes evaluation dominates every possible later use of the
8496 values. Restricting to such types here would be safe since there
8497 is no possible forward declaration around, but would introduce an
8498 undesirable middle-end semantic to anonymity. We then defer to
8499 front-ends the responsibility of ensuring that the sizes are
8500 evaluated both early and late enough, e.g. by attaching artificial
8501 type declarations to the tree. */
8502 break;
8504 default:
8505 break;
8508 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8509 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8511 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8513 TYPE_SIZE (t) = TYPE_SIZE (type);
8514 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8515 TYPE_SIZES_GIMPLIFIED (t) = 1;
8519 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8520 a size or position, has had all of its SAVE_EXPRs evaluated.
8521 We add any required statements to *STMT_P. */
8523 void
8524 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8526 tree expr = *expr_p;
8528 /* We don't do anything if the value isn't there, is constant, or contains
8529 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8530 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8531 will want to replace it with a new variable, but that will cause problems
8532 if this type is from outside the function. It's OK to have that here. */
8533 if (is_gimple_sizepos (expr))
8534 return;
8536 *expr_p = unshare_expr (expr);
8538 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8541 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8542 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8543 is true, also gimplify the parameters. */
8545 gimple
8546 gimplify_body (tree fndecl, bool do_parms)
8548 location_t saved_location = input_location;
8549 gimple_seq parm_stmts, seq;
8550 gimple outer_bind;
8551 struct cgraph_node *cgn;
8553 timevar_push (TV_TREE_GIMPLIFY);
8555 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8556 gimplification. */
8557 default_rtl_profile ();
8559 gcc_assert (gimplify_ctxp == NULL);
8560 push_gimplify_context ();
8562 if (flag_openmp)
8564 gcc_assert (gimplify_omp_ctxp == NULL);
8565 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8566 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8569 /* Unshare most shared trees in the body and in that of any nested functions.
8570 It would seem we don't have to do this for nested functions because
8571 they are supposed to be output and then the outer function gimplified
8572 first, but the g++ front end doesn't always do it that way. */
8573 unshare_body (fndecl);
8574 unvisit_body (fndecl);
8576 cgn = cgraph_get_node (fndecl);
8577 if (cgn && cgn->origin)
8578 nonlocal_vlas = pointer_set_create ();
8580 /* Make sure input_location isn't set to something weird. */
8581 input_location = DECL_SOURCE_LOCATION (fndecl);
8583 /* Resolve callee-copies. This has to be done before processing
8584 the body so that DECL_VALUE_EXPR gets processed correctly. */
8585 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8587 /* Gimplify the function's body. */
8588 seq = NULL;
8589 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8590 outer_bind = gimple_seq_first_stmt (seq);
8591 if (!outer_bind)
8593 outer_bind = gimple_build_nop ();
8594 gimplify_seq_add_stmt (&seq, outer_bind);
8597 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8598 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8599 if (gimple_code (outer_bind) == GIMPLE_BIND
8600 && gimple_seq_first (seq) == gimple_seq_last (seq))
8602 else
8603 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8605 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8607 /* If we had callee-copies statements, insert them at the beginning
8608 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8609 if (!gimple_seq_empty_p (parm_stmts))
8611 tree parm;
8613 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8614 gimple_bind_set_body (outer_bind, parm_stmts);
8616 for (parm = DECL_ARGUMENTS (current_function_decl);
8617 parm; parm = DECL_CHAIN (parm))
8618 if (DECL_HAS_VALUE_EXPR_P (parm))
8620 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8621 DECL_IGNORED_P (parm) = 0;
8625 if (nonlocal_vlas)
8627 if (nonlocal_vla_vars)
8629 /* tree-nested.c may later on call declare_vars (..., true);
8630 which relies on BLOCK_VARS chain to be the tail of the
8631 gimple_bind_vars chain. Ensure we don't violate that
8632 assumption. */
8633 if (gimple_bind_block (outer_bind)
8634 == DECL_INITIAL (current_function_decl))
8635 declare_vars (nonlocal_vla_vars, outer_bind, true);
8636 else
8637 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8638 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8639 nonlocal_vla_vars);
8640 nonlocal_vla_vars = NULL_TREE;
8642 pointer_set_destroy (nonlocal_vlas);
8643 nonlocal_vlas = NULL;
8646 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8648 delete_omp_context (gimplify_omp_ctxp);
8649 gimplify_omp_ctxp = NULL;
8652 pop_gimplify_context (outer_bind);
8653 gcc_assert (gimplify_ctxp == NULL);
8655 #ifdef ENABLE_CHECKING
8656 if (!seen_error ())
8657 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8658 #endif
8660 timevar_pop (TV_TREE_GIMPLIFY);
8661 input_location = saved_location;
8663 return outer_bind;
8666 typedef char *char_p; /* For DEF_VEC_P. */
8668 /* Return whether we should exclude FNDECL from instrumentation. */
8670 static bool
8671 flag_instrument_functions_exclude_p (tree fndecl)
8673 vec<char_p> *v;
8675 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8676 if (v && v->length () > 0)
8678 const char *name;
8679 int i;
8680 char *s;
8682 name = lang_hooks.decl_printable_name (fndecl, 0);
8683 FOR_EACH_VEC_ELT (*v, i, s)
8684 if (strstr (name, s) != NULL)
8685 return true;
8688 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8689 if (v && v->length () > 0)
8691 const char *name;
8692 int i;
8693 char *s;
8695 name = DECL_SOURCE_FILE (fndecl);
8696 FOR_EACH_VEC_ELT (*v, i, s)
8697 if (strstr (name, s) != NULL)
8698 return true;
8701 return false;
8704 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8705 node for the function we want to gimplify.
8707 Return the sequence of GIMPLE statements corresponding to the body
8708 of FNDECL. */
8710 void
8711 gimplify_function_tree (tree fndecl)
8713 tree parm, ret;
8714 gimple_seq seq;
8715 gimple bind;
8717 gcc_assert (!gimple_body (fndecl));
8719 if (DECL_STRUCT_FUNCTION (fndecl))
8720 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8721 else
8722 push_struct_function (fndecl);
8724 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8726 /* Preliminarily mark non-addressed complex variables as eligible
8727 for promotion to gimple registers. We'll transform their uses
8728 as we find them. */
8729 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8730 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8731 && !TREE_THIS_VOLATILE (parm)
8732 && !needs_to_live_in_memory (parm))
8733 DECL_GIMPLE_REG_P (parm) = 1;
8736 ret = DECL_RESULT (fndecl);
8737 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8738 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8739 && !needs_to_live_in_memory (ret))
8740 DECL_GIMPLE_REG_P (ret) = 1;
8742 bind = gimplify_body (fndecl, true);
8744 /* The tree body of the function is no longer needed, replace it
8745 with the new GIMPLE body. */
8746 seq = NULL;
8747 gimple_seq_add_stmt (&seq, bind);
8748 gimple_set_body (fndecl, seq);
8750 /* If we're instrumenting function entry/exit, then prepend the call to
8751 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8752 catch the exit hook. */
8753 /* ??? Add some way to ignore exceptions for this TFE. */
8754 if (flag_instrument_function_entry_exit
8755 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8756 && !flag_instrument_functions_exclude_p (fndecl))
8758 tree x;
8759 gimple new_bind;
8760 gimple tf;
8761 gimple_seq cleanup = NULL, body = NULL;
8762 tree tmp_var;
8763 gimple call;
8765 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8766 call = gimple_build_call (x, 1, integer_zero_node);
8767 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8768 gimple_call_set_lhs (call, tmp_var);
8769 gimplify_seq_add_stmt (&cleanup, call);
8770 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8771 call = gimple_build_call (x, 2,
8772 build_fold_addr_expr (current_function_decl),
8773 tmp_var);
8774 gimplify_seq_add_stmt (&cleanup, call);
8775 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8777 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8778 call = gimple_build_call (x, 1, integer_zero_node);
8779 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8780 gimple_call_set_lhs (call, tmp_var);
8781 gimplify_seq_add_stmt (&body, call);
8782 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8783 call = gimple_build_call (x, 2,
8784 build_fold_addr_expr (current_function_decl),
8785 tmp_var);
8786 gimplify_seq_add_stmt (&body, call);
8787 gimplify_seq_add_stmt (&body, tf);
8788 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8789 /* Clear the block for BIND, since it is no longer directly inside
8790 the function, but within a try block. */
8791 gimple_bind_set_block (bind, NULL);
8793 /* Replace the current function body with the body
8794 wrapped in the try/finally TF. */
8795 seq = NULL;
8796 gimple_seq_add_stmt (&seq, new_bind);
8797 gimple_set_body (fndecl, seq);
8800 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8801 cfun->curr_properties = PROP_gimple_any;
8803 pop_cfun ();
8806 /* Return a dummy expression of type TYPE in order to keep going after an
8807 error. */
8809 static tree
8810 dummy_object (tree type)
8812 tree t = build_int_cst (build_pointer_type (type), 0);
8813 return build2 (MEM_REF, type, t, t);
8816 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
8817 builtin function, but a very special sort of operator. */
8819 enum gimplify_status
8820 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8822 tree promoted_type, have_va_type;
8823 tree valist = TREE_OPERAND (*expr_p, 0);
8824 tree type = TREE_TYPE (*expr_p);
8825 tree t;
8826 location_t loc = EXPR_LOCATION (*expr_p);
8828 /* Verify that valist is of the proper type. */
8829 have_va_type = TREE_TYPE (valist);
8830 if (have_va_type == error_mark_node)
8831 return GS_ERROR;
8832 have_va_type = targetm.canonical_va_list_type (have_va_type);
8834 if (have_va_type == NULL_TREE)
8836 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
8837 return GS_ERROR;
8840 /* Generate a diagnostic for requesting data of a type that cannot
8841 be passed through `...' due to type promotion at the call site. */
8842 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
8843 != type)
8845 static bool gave_help;
8846 bool warned;
8848 /* Unfortunately, this is merely undefined, rather than a constraint
8849 violation, so we cannot make this an error. If this call is never
8850 executed, the program is still strictly conforming. */
8851 warned = warning_at (loc, 0,
8852 "%qT is promoted to %qT when passed through %<...%>",
8853 type, promoted_type);
8854 if (!gave_help && warned)
8856 gave_help = true;
8857 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
8858 promoted_type, type);
8861 /* We can, however, treat "undefined" any way we please.
8862 Call abort to encourage the user to fix the program. */
8863 if (warned)
8864 inform (loc, "if this code is reached, the program will abort");
8865 /* Before the abort, allow the evaluation of the va_list
8866 expression to exit or longjmp. */
8867 gimplify_and_add (valist, pre_p);
8868 t = build_call_expr_loc (loc,
8869 builtin_decl_implicit (BUILT_IN_TRAP), 0);
8870 gimplify_and_add (t, pre_p);
8872 /* This is dead code, but go ahead and finish so that the
8873 mode of the result comes out right. */
8874 *expr_p = dummy_object (type);
8875 return GS_ALL_DONE;
8877 else
8879 /* Make it easier for the backends by protecting the valist argument
8880 from multiple evaluations. */
8881 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
8883 /* For this case, the backends will be expecting a pointer to
8884 TREE_TYPE (abi), but it's possible we've
8885 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
8886 So fix it. */
8887 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
8889 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
8890 valist = fold_convert_loc (loc, p1,
8891 build_fold_addr_expr_loc (loc, valist));
8894 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
8896 else
8897 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
8899 if (!targetm.gimplify_va_arg_expr)
8900 /* FIXME: Once most targets are converted we should merely
8901 assert this is non-null. */
8902 return GS_ALL_DONE;
8904 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
8905 return GS_OK;
8909 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
8911 DST/SRC are the destination and source respectively. You can pass
8912 ungimplified trees in DST or SRC, in which case they will be
8913 converted to a gimple operand if necessary.
8915 This function returns the newly created GIMPLE_ASSIGN tuple. */
8917 gimple
8918 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
8920 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
8921 gimplify_and_add (t, seq_p);
8922 ggc_free (t);
8923 return gimple_seq_last_stmt (*seq_p);
8926 inline hashval_t
8927 gimplify_hasher::hash (const value_type *p)
8929 tree t = p->val;
8930 return iterative_hash_expr (t, 0);
8933 inline bool
8934 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
8936 tree t1 = p1->val;
8937 tree t2 = p2->val;
8938 enum tree_code code = TREE_CODE (t1);
8940 if (TREE_CODE (t2) != code
8941 || TREE_TYPE (t1) != TREE_TYPE (t2))
8942 return false;
8944 if (!operand_equal_p (t1, t2, 0))
8945 return false;
8947 #ifdef ENABLE_CHECKING
8948 /* Only allow them to compare equal if they also hash equal; otherwise
8949 results are nondeterminate, and we fail bootstrap comparison. */
8950 gcc_assert (hash (p1) == hash (p2));
8951 #endif
8953 return true;