Merge trunk version 210928 into gupc branch.
[official-gcc.git] / gcc / gimplify.c
blobf87354c76a7a87be785f5ba935a66cfa97030754
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
2333 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
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_PARALLEL) != 0)
5688 error ("%qE not specified in enclosing parallel",
5689 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5690 error_at (ctx->location, "enclosing parallel");
5692 else if ((ctx->region_type & ORT_TASK) != 0)
5694 error ("%qE not specified in enclosing task",
5695 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5696 error_at (ctx->location, "enclosing task");
5698 else if (ctx->region_type == ORT_TEAMS)
5700 error ("%qE not specified in enclosing teams construct",
5701 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5702 error_at (ctx->location, "enclosing teams construct");
5704 else
5705 gcc_unreachable ();
5706 /* FALLTHRU */
5707 case OMP_CLAUSE_DEFAULT_SHARED:
5708 flags |= GOVD_SHARED;
5709 break;
5710 case OMP_CLAUSE_DEFAULT_PRIVATE:
5711 flags |= GOVD_PRIVATE;
5712 break;
5713 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5714 flags |= GOVD_FIRSTPRIVATE;
5715 break;
5716 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5717 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5718 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5719 if (ctx->outer_context)
5720 omp_notice_variable (ctx->outer_context, decl, in_code);
5721 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5723 splay_tree_node n2;
5725 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5726 continue;
5727 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5728 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5730 flags |= GOVD_FIRSTPRIVATE;
5731 break;
5733 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5734 break;
5736 if (flags & GOVD_FIRSTPRIVATE)
5737 break;
5738 if (octx == NULL
5739 && (TREE_CODE (decl) == PARM_DECL
5740 || (!is_global_var (decl)
5741 && DECL_CONTEXT (decl) == current_function_decl)))
5743 flags |= GOVD_FIRSTPRIVATE;
5744 break;
5746 flags |= GOVD_SHARED;
5747 break;
5748 default:
5749 gcc_unreachable ();
5752 if ((flags & GOVD_PRIVATE)
5753 && lang_hooks.decls.omp_private_outer_ref (decl))
5754 flags |= GOVD_PRIVATE_OUTER_REF;
5756 omp_add_variable (ctx, decl, flags);
5758 shared = (flags & GOVD_SHARED) != 0;
5759 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5760 goto do_outer;
5763 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5764 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5765 && DECL_SIZE (decl)
5766 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5768 splay_tree_node n2;
5769 tree t = DECL_VALUE_EXPR (decl);
5770 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5771 t = TREE_OPERAND (t, 0);
5772 gcc_assert (DECL_P (t));
5773 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5774 n2->value |= GOVD_SEEN;
5777 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5778 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5780 /* If nothing changed, there's nothing left to do. */
5781 if ((n->value & flags) == flags)
5782 return ret;
5783 flags |= n->value;
5784 n->value = flags;
5786 do_outer:
5787 /* If the variable is private in the current context, then we don't
5788 need to propagate anything to an outer context. */
5789 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5790 return ret;
5791 if (ctx->outer_context
5792 && omp_notice_variable (ctx->outer_context, decl, in_code))
5793 return true;
5794 return ret;
5797 /* Verify that DECL is private within CTX. If there's specific information
5798 to the contrary in the innermost scope, generate an error. */
5800 static bool
5801 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5803 splay_tree_node n;
5805 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5806 if (n != NULL)
5808 if (n->value & GOVD_SHARED)
5810 if (ctx == gimplify_omp_ctxp)
5812 if (simd)
5813 error ("iteration variable %qE is predetermined linear",
5814 DECL_NAME (decl));
5815 else
5816 error ("iteration variable %qE should be private",
5817 DECL_NAME (decl));
5818 n->value = GOVD_PRIVATE;
5819 return true;
5821 else
5822 return false;
5824 else if ((n->value & GOVD_EXPLICIT) != 0
5825 && (ctx == gimplify_omp_ctxp
5826 || (ctx->region_type == ORT_COMBINED_PARALLEL
5827 && gimplify_omp_ctxp->outer_context == ctx)))
5829 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5830 error ("iteration variable %qE should not be firstprivate",
5831 DECL_NAME (decl));
5832 else if ((n->value & GOVD_REDUCTION) != 0)
5833 error ("iteration variable %qE should not be reduction",
5834 DECL_NAME (decl));
5835 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5836 error ("iteration variable %qE should not be lastprivate",
5837 DECL_NAME (decl));
5838 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5839 error ("iteration variable %qE should not be private",
5840 DECL_NAME (decl));
5841 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5842 error ("iteration variable %qE is predetermined linear",
5843 DECL_NAME (decl));
5845 return (ctx == gimplify_omp_ctxp
5846 || (ctx->region_type == ORT_COMBINED_PARALLEL
5847 && gimplify_omp_ctxp->outer_context == ctx));
5850 if (ctx->region_type != ORT_WORKSHARE
5851 && ctx->region_type != ORT_SIMD)
5852 return false;
5853 else if (ctx->outer_context)
5854 return omp_is_private (ctx->outer_context, decl, simd);
5855 return false;
5858 /* Return true if DECL is private within a parallel region
5859 that binds to the current construct's context or in parallel
5860 region's REDUCTION clause. */
5862 static bool
5863 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5865 splay_tree_node n;
5869 ctx = ctx->outer_context;
5870 if (ctx == NULL)
5871 return !(is_global_var (decl)
5872 /* References might be private, but might be shared too,
5873 when checking for copyprivate, assume they might be
5874 private, otherwise assume they might be shared. */
5875 || (!copyprivate
5876 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5878 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5879 continue;
5881 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5882 if (n != NULL)
5883 return (n->value & GOVD_SHARED) == 0;
5885 while (ctx->region_type == ORT_WORKSHARE
5886 || ctx->region_type == ORT_SIMD);
5887 return false;
5890 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5891 and previous omp contexts. */
5893 static void
5894 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5895 enum omp_region_type region_type)
5897 struct gimplify_omp_ctx *ctx, *outer_ctx;
5898 tree c;
5900 ctx = new_omp_context (region_type);
5901 outer_ctx = ctx->outer_context;
5903 while ((c = *list_p) != NULL)
5905 bool remove = false;
5906 bool notice_outer = true;
5907 const char *check_non_private = NULL;
5908 unsigned int flags;
5909 tree decl;
5911 switch (OMP_CLAUSE_CODE (c))
5913 case OMP_CLAUSE_PRIVATE:
5914 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5915 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5917 flags |= GOVD_PRIVATE_OUTER_REF;
5918 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5920 else
5921 notice_outer = false;
5922 goto do_add;
5923 case OMP_CLAUSE_SHARED:
5924 flags = GOVD_SHARED | GOVD_EXPLICIT;
5925 goto do_add;
5926 case OMP_CLAUSE_FIRSTPRIVATE:
5927 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5928 check_non_private = "firstprivate";
5929 goto do_add;
5930 case OMP_CLAUSE_LASTPRIVATE:
5931 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5932 check_non_private = "lastprivate";
5933 goto do_add;
5934 case OMP_CLAUSE_REDUCTION:
5935 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5936 check_non_private = "reduction";
5937 goto do_add;
5938 case OMP_CLAUSE_LINEAR:
5939 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5940 is_gimple_val, fb_rvalue) == GS_ERROR)
5942 remove = true;
5943 break;
5945 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5946 goto do_add;
5948 case OMP_CLAUSE_MAP:
5949 if (OMP_CLAUSE_SIZE (c)
5950 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5951 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5953 remove = true;
5954 break;
5956 decl = OMP_CLAUSE_DECL (c);
5957 if (!DECL_P (decl))
5959 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
5960 NULL, is_gimple_lvalue, fb_lvalue)
5961 == GS_ERROR)
5963 remove = true;
5964 break;
5966 break;
5968 flags = GOVD_MAP | GOVD_EXPLICIT;
5969 goto do_add;
5971 case OMP_CLAUSE_DEPEND:
5972 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
5974 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
5975 NULL, is_gimple_val, fb_rvalue);
5976 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
5978 if (error_operand_p (OMP_CLAUSE_DECL (c)))
5980 remove = true;
5981 break;
5983 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
5984 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
5985 is_gimple_val, fb_rvalue) == GS_ERROR)
5987 remove = true;
5988 break;
5990 break;
5992 case OMP_CLAUSE_TO:
5993 case OMP_CLAUSE_FROM:
5994 if (OMP_CLAUSE_SIZE (c)
5995 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5996 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5998 remove = true;
5999 break;
6001 decl = OMP_CLAUSE_DECL (c);
6002 if (error_operand_p (decl))
6004 remove = true;
6005 break;
6007 if (!DECL_P (decl))
6009 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6010 NULL, is_gimple_lvalue, fb_lvalue)
6011 == GS_ERROR)
6013 remove = true;
6014 break;
6016 break;
6018 goto do_notice;
6020 do_add:
6021 decl = OMP_CLAUSE_DECL (c);
6022 if (error_operand_p (decl))
6024 remove = true;
6025 break;
6027 omp_add_variable (ctx, decl, flags);
6028 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6029 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6031 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6032 GOVD_LOCAL | GOVD_SEEN);
6033 gimplify_omp_ctxp = ctx;
6034 push_gimplify_context ();
6036 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6037 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6039 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6040 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6041 pop_gimplify_context
6042 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6043 push_gimplify_context ();
6044 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6045 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6046 pop_gimplify_context
6047 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6048 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6049 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6051 gimplify_omp_ctxp = outer_ctx;
6053 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6054 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6056 gimplify_omp_ctxp = ctx;
6057 push_gimplify_context ();
6058 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6060 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6061 NULL, NULL);
6062 TREE_SIDE_EFFECTS (bind) = 1;
6063 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6064 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6066 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6067 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6068 pop_gimplify_context
6069 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6070 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6072 gimplify_omp_ctxp = outer_ctx;
6074 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6075 && OMP_CLAUSE_LINEAR_STMT (c))
6077 gimplify_omp_ctxp = ctx;
6078 push_gimplify_context ();
6079 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6081 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6082 NULL, NULL);
6083 TREE_SIDE_EFFECTS (bind) = 1;
6084 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6085 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6087 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6088 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6089 pop_gimplify_context
6090 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6091 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6093 gimplify_omp_ctxp = outer_ctx;
6095 if (notice_outer)
6096 goto do_notice;
6097 break;
6099 case OMP_CLAUSE_COPYIN:
6100 case OMP_CLAUSE_COPYPRIVATE:
6101 decl = OMP_CLAUSE_DECL (c);
6102 if (error_operand_p (decl))
6104 remove = true;
6105 break;
6107 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6108 && !remove
6109 && !omp_check_private (ctx, decl, true))
6111 remove = true;
6112 if (is_global_var (decl))
6114 if (DECL_THREAD_LOCAL_P (decl))
6115 remove = false;
6116 else if (DECL_HAS_VALUE_EXPR_P (decl))
6118 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6120 if (value
6121 && DECL_P (value)
6122 && DECL_THREAD_LOCAL_P (value))
6123 remove = false;
6126 if (remove)
6127 error_at (OMP_CLAUSE_LOCATION (c),
6128 "copyprivate variable %qE is not threadprivate"
6129 " or private in outer context", DECL_NAME (decl));
6131 do_notice:
6132 if (outer_ctx)
6133 omp_notice_variable (outer_ctx, decl, true);
6134 if (check_non_private
6135 && region_type == ORT_WORKSHARE
6136 && omp_check_private (ctx, decl, false))
6138 error ("%s variable %qE is private in outer context",
6139 check_non_private, DECL_NAME (decl));
6140 remove = true;
6142 break;
6144 case OMP_CLAUSE_FINAL:
6145 case OMP_CLAUSE_IF:
6146 OMP_CLAUSE_OPERAND (c, 0)
6147 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6148 /* Fall through. */
6150 case OMP_CLAUSE_SCHEDULE:
6151 case OMP_CLAUSE_NUM_THREADS:
6152 case OMP_CLAUSE_NUM_TEAMS:
6153 case OMP_CLAUSE_THREAD_LIMIT:
6154 case OMP_CLAUSE_DIST_SCHEDULE:
6155 case OMP_CLAUSE_DEVICE:
6156 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6157 is_gimple_val, fb_rvalue) == GS_ERROR)
6158 remove = true;
6159 break;
6161 case OMP_CLAUSE_NOWAIT:
6162 case OMP_CLAUSE_ORDERED:
6163 case OMP_CLAUSE_UNTIED:
6164 case OMP_CLAUSE_COLLAPSE:
6165 case OMP_CLAUSE_MERGEABLE:
6166 case OMP_CLAUSE_PROC_BIND:
6167 case OMP_CLAUSE_SAFELEN:
6168 break;
6170 case OMP_CLAUSE_ALIGNED:
6171 decl = OMP_CLAUSE_DECL (c);
6172 if (error_operand_p (decl))
6174 remove = true;
6175 break;
6177 if (!is_global_var (decl)
6178 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6179 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6180 break;
6182 case OMP_CLAUSE_DEFAULT:
6183 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6184 break;
6186 default:
6187 gcc_unreachable ();
6190 if (remove)
6191 *list_p = OMP_CLAUSE_CHAIN (c);
6192 else
6193 list_p = &OMP_CLAUSE_CHAIN (c);
6196 gimplify_omp_ctxp = ctx;
6199 /* For all variables that were not actually used within the context,
6200 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6202 static int
6203 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6205 tree *list_p = (tree *) data;
6206 tree decl = (tree) n->key;
6207 unsigned flags = n->value;
6208 enum omp_clause_code code;
6209 tree clause;
6210 bool private_debug;
6212 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6213 return 0;
6214 if ((flags & GOVD_SEEN) == 0)
6215 return 0;
6216 if (flags & GOVD_DEBUG_PRIVATE)
6218 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6219 private_debug = true;
6221 else if (flags & GOVD_MAP)
6222 private_debug = false;
6223 else
6224 private_debug
6225 = lang_hooks.decls.omp_private_debug_clause (decl,
6226 !!(flags & GOVD_SHARED));
6227 if (private_debug)
6228 code = OMP_CLAUSE_PRIVATE;
6229 else if (flags & GOVD_MAP)
6230 code = OMP_CLAUSE_MAP;
6231 else if (flags & GOVD_SHARED)
6233 if (is_global_var (decl))
6235 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6236 while (ctx != NULL)
6238 splay_tree_node on
6239 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6240 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6241 | GOVD_PRIVATE | GOVD_REDUCTION
6242 | GOVD_LINEAR)) != 0)
6243 break;
6244 ctx = ctx->outer_context;
6246 if (ctx == NULL)
6247 return 0;
6249 code = OMP_CLAUSE_SHARED;
6251 else if (flags & GOVD_PRIVATE)
6252 code = OMP_CLAUSE_PRIVATE;
6253 else if (flags & GOVD_FIRSTPRIVATE)
6254 code = OMP_CLAUSE_FIRSTPRIVATE;
6255 else if (flags & GOVD_LASTPRIVATE)
6256 code = OMP_CLAUSE_LASTPRIVATE;
6257 else if (flags & GOVD_ALIGNED)
6258 return 0;
6259 else
6260 gcc_unreachable ();
6262 clause = build_omp_clause (input_location, code);
6263 OMP_CLAUSE_DECL (clause) = decl;
6264 OMP_CLAUSE_CHAIN (clause) = *list_p;
6265 if (private_debug)
6266 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6267 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6268 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6269 else if (code == OMP_CLAUSE_MAP)
6271 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6272 ? OMP_CLAUSE_MAP_TO
6273 : OMP_CLAUSE_MAP_TOFROM;
6274 if (DECL_SIZE (decl)
6275 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6277 tree decl2 = DECL_VALUE_EXPR (decl);
6278 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6279 decl2 = TREE_OPERAND (decl2, 0);
6280 gcc_assert (DECL_P (decl2));
6281 tree mem = build_simple_mem_ref (decl2);
6282 OMP_CLAUSE_DECL (clause) = mem;
6283 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6284 if (gimplify_omp_ctxp->outer_context)
6286 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6287 omp_notice_variable (ctx, decl2, true);
6288 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6290 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6291 OMP_CLAUSE_MAP);
6292 OMP_CLAUSE_DECL (nc) = decl;
6293 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6294 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6295 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6296 OMP_CLAUSE_CHAIN (clause) = nc;
6299 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6301 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6302 OMP_CLAUSE_DECL (nc) = decl;
6303 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6304 OMP_CLAUSE_CHAIN (nc) = *list_p;
6305 OMP_CLAUSE_CHAIN (clause) = nc;
6306 lang_hooks.decls.omp_finish_clause (nc);
6308 *list_p = clause;
6309 lang_hooks.decls.omp_finish_clause (clause);
6310 return 0;
6313 static void
6314 gimplify_adjust_omp_clauses (tree *list_p)
6316 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6317 tree c, decl;
6319 while ((c = *list_p) != NULL)
6321 splay_tree_node n;
6322 bool remove = false;
6324 switch (OMP_CLAUSE_CODE (c))
6326 case OMP_CLAUSE_PRIVATE:
6327 case OMP_CLAUSE_SHARED:
6328 case OMP_CLAUSE_FIRSTPRIVATE:
6329 case OMP_CLAUSE_LINEAR:
6330 decl = OMP_CLAUSE_DECL (c);
6331 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6332 remove = !(n->value & GOVD_SEEN);
6333 if (! remove)
6335 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6336 if ((n->value & GOVD_DEBUG_PRIVATE)
6337 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6339 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6340 || ((n->value & GOVD_DATA_SHARE_CLASS)
6341 == GOVD_PRIVATE));
6342 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6343 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6345 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6346 && ctx->outer_context
6347 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6348 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6350 if (ctx->outer_context->combined_loop
6351 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6353 n = splay_tree_lookup (ctx->outer_context->variables,
6354 (splay_tree_key) decl);
6355 if (n == NULL
6356 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6358 int flags = GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE;
6359 if (n == NULL)
6360 omp_add_variable (ctx->outer_context, decl,
6361 flags | GOVD_SEEN);
6362 else
6363 n->value |= flags | GOVD_SEEN;
6366 else if (!is_global_var (decl))
6367 omp_notice_variable (ctx->outer_context, decl, true);
6370 break;
6372 case OMP_CLAUSE_LASTPRIVATE:
6373 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6374 accurately reflect the presence of a FIRSTPRIVATE clause. */
6375 decl = OMP_CLAUSE_DECL (c);
6376 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6377 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6378 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6379 break;
6381 case OMP_CLAUSE_ALIGNED:
6382 decl = OMP_CLAUSE_DECL (c);
6383 if (!is_global_var (decl))
6385 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6386 remove = n == NULL || !(n->value & GOVD_SEEN);
6387 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6389 struct gimplify_omp_ctx *octx;
6390 if (n != NULL
6391 && (n->value & (GOVD_DATA_SHARE_CLASS
6392 & ~GOVD_FIRSTPRIVATE)))
6393 remove = true;
6394 else
6395 for (octx = ctx->outer_context; octx;
6396 octx = octx->outer_context)
6398 n = splay_tree_lookup (octx->variables,
6399 (splay_tree_key) decl);
6400 if (n == NULL)
6401 continue;
6402 if (n->value & GOVD_LOCAL)
6403 break;
6404 /* We have to avoid assigning a shared variable
6405 to itself when trying to add
6406 __builtin_assume_aligned. */
6407 if (n->value & GOVD_SHARED)
6409 remove = true;
6410 break;
6415 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6417 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6418 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6419 remove = true;
6421 break;
6423 case OMP_CLAUSE_MAP:
6424 decl = OMP_CLAUSE_DECL (c);
6425 if (!DECL_P (decl))
6426 break;
6427 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6428 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6429 remove = true;
6430 else if (DECL_SIZE (decl)
6431 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6432 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6434 tree decl2 = DECL_VALUE_EXPR (decl);
6435 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6436 decl2 = TREE_OPERAND (decl2, 0);
6437 gcc_assert (DECL_P (decl2));
6438 tree mem = build_simple_mem_ref (decl2);
6439 OMP_CLAUSE_DECL (c) = mem;
6440 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6441 if (ctx->outer_context)
6443 omp_notice_variable (ctx->outer_context, decl2, true);
6444 omp_notice_variable (ctx->outer_context,
6445 OMP_CLAUSE_SIZE (c), true);
6447 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6448 OMP_CLAUSE_MAP);
6449 OMP_CLAUSE_DECL (nc) = decl;
6450 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6451 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6452 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6453 OMP_CLAUSE_CHAIN (c) = nc;
6454 c = nc;
6456 break;
6458 case OMP_CLAUSE_TO:
6459 case OMP_CLAUSE_FROM:
6460 decl = OMP_CLAUSE_DECL (c);
6461 if (!DECL_P (decl))
6462 break;
6463 if (DECL_SIZE (decl)
6464 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6466 tree decl2 = DECL_VALUE_EXPR (decl);
6467 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6468 decl2 = TREE_OPERAND (decl2, 0);
6469 gcc_assert (DECL_P (decl2));
6470 tree mem = build_simple_mem_ref (decl2);
6471 OMP_CLAUSE_DECL (c) = mem;
6472 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6473 if (ctx->outer_context)
6475 omp_notice_variable (ctx->outer_context, decl2, true);
6476 omp_notice_variable (ctx->outer_context,
6477 OMP_CLAUSE_SIZE (c), true);
6480 break;
6482 case OMP_CLAUSE_REDUCTION:
6483 case OMP_CLAUSE_COPYIN:
6484 case OMP_CLAUSE_COPYPRIVATE:
6485 case OMP_CLAUSE_IF:
6486 case OMP_CLAUSE_NUM_THREADS:
6487 case OMP_CLAUSE_NUM_TEAMS:
6488 case OMP_CLAUSE_THREAD_LIMIT:
6489 case OMP_CLAUSE_DIST_SCHEDULE:
6490 case OMP_CLAUSE_DEVICE:
6491 case OMP_CLAUSE_SCHEDULE:
6492 case OMP_CLAUSE_NOWAIT:
6493 case OMP_CLAUSE_ORDERED:
6494 case OMP_CLAUSE_DEFAULT:
6495 case OMP_CLAUSE_UNTIED:
6496 case OMP_CLAUSE_COLLAPSE:
6497 case OMP_CLAUSE_FINAL:
6498 case OMP_CLAUSE_MERGEABLE:
6499 case OMP_CLAUSE_PROC_BIND:
6500 case OMP_CLAUSE_SAFELEN:
6501 case OMP_CLAUSE_DEPEND:
6502 break;
6504 default:
6505 gcc_unreachable ();
6508 if (remove)
6509 *list_p = OMP_CLAUSE_CHAIN (c);
6510 else
6511 list_p = &OMP_CLAUSE_CHAIN (c);
6514 /* Add in any implicit data sharing. */
6515 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6517 gimplify_omp_ctxp = ctx->outer_context;
6518 delete_omp_context (ctx);
6521 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6522 gimplification of the body, as well as scanning the body for used
6523 variables. We need to do this scan now, because variable-sized
6524 decls will be decomposed during gimplification. */
6526 static void
6527 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6529 tree expr = *expr_p;
6530 gimple g;
6531 gimple_seq body = NULL;
6533 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6534 OMP_PARALLEL_COMBINED (expr)
6535 ? ORT_COMBINED_PARALLEL
6536 : ORT_PARALLEL);
6538 push_gimplify_context ();
6540 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6541 if (gimple_code (g) == GIMPLE_BIND)
6542 pop_gimplify_context (g);
6543 else
6544 pop_gimplify_context (NULL);
6546 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6548 g = gimple_build_omp_parallel (body,
6549 OMP_PARALLEL_CLAUSES (expr),
6550 NULL_TREE, NULL_TREE);
6551 if (OMP_PARALLEL_COMBINED (expr))
6552 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6553 gimplify_seq_add_stmt (pre_p, g);
6554 *expr_p = NULL_TREE;
6557 /* Gimplify the contents of an OMP_TASK statement. This involves
6558 gimplification of the body, as well as scanning the body for used
6559 variables. We need to do this scan now, because variable-sized
6560 decls will be decomposed during gimplification. */
6562 static void
6563 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6565 tree expr = *expr_p;
6566 gimple g;
6567 gimple_seq body = NULL;
6569 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6570 find_omp_clause (OMP_TASK_CLAUSES (expr),
6571 OMP_CLAUSE_UNTIED)
6572 ? ORT_UNTIED_TASK : ORT_TASK);
6574 push_gimplify_context ();
6576 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6577 if (gimple_code (g) == GIMPLE_BIND)
6578 pop_gimplify_context (g);
6579 else
6580 pop_gimplify_context (NULL);
6582 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6584 g = gimple_build_omp_task (body,
6585 OMP_TASK_CLAUSES (expr),
6586 NULL_TREE, NULL_TREE,
6587 NULL_TREE, NULL_TREE, NULL_TREE);
6588 gimplify_seq_add_stmt (pre_p, g);
6589 *expr_p = NULL_TREE;
6592 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6593 with non-NULL OMP_FOR_INIT. */
6595 static tree
6596 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6598 *walk_subtrees = 0;
6599 switch (TREE_CODE (*tp))
6601 case OMP_FOR:
6602 *walk_subtrees = 1;
6603 /* FALLTHRU */
6604 case OMP_SIMD:
6605 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6606 return *tp;
6607 break;
6608 case BIND_EXPR:
6609 case STATEMENT_LIST:
6610 case OMP_PARALLEL:
6611 *walk_subtrees = 1;
6612 break;
6613 default:
6614 break;
6616 return NULL_TREE;
6619 /* Gimplify the gross structure of an OMP_FOR statement. */
6621 static enum gimplify_status
6622 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6624 tree for_stmt, orig_for_stmt, decl, var, t;
6625 enum gimplify_status ret = GS_ALL_DONE;
6626 enum gimplify_status tret;
6627 gimple gfor;
6628 gimple_seq for_body, for_pre_body;
6629 int i;
6630 bool simd;
6631 bitmap has_decl_expr = NULL;
6633 orig_for_stmt = for_stmt = *expr_p;
6635 simd = (TREE_CODE (for_stmt) == OMP_SIMD
6636 || TREE_CODE (for_stmt) == CILK_SIMD);
6637 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6638 simd ? ORT_SIMD : ORT_WORKSHARE);
6640 /* Handle OMP_FOR_INIT. */
6641 for_pre_body = NULL;
6642 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6644 has_decl_expr = BITMAP_ALLOC (NULL);
6645 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6646 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6647 == VAR_DECL)
6649 t = OMP_FOR_PRE_BODY (for_stmt);
6650 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6652 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6654 tree_stmt_iterator si;
6655 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6656 tsi_next (&si))
6658 t = tsi_stmt (si);
6659 if (TREE_CODE (t) == DECL_EXPR
6660 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6661 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6665 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6666 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6668 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6670 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6671 NULL, NULL);
6672 gcc_assert (for_stmt != NULL_TREE);
6673 gimplify_omp_ctxp->combined_loop = true;
6676 for_body = NULL;
6677 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6678 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6679 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6680 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6681 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6683 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6684 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6685 decl = TREE_OPERAND (t, 0);
6686 gcc_assert (DECL_P (decl));
6687 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6688 || POINTER_TYPE_P (TREE_TYPE (decl)));
6690 /* Make sure the iteration variable is private. */
6691 tree c = NULL_TREE;
6692 tree c2 = NULL_TREE;
6693 if (orig_for_stmt != for_stmt)
6694 /* Do this only on innermost construct for combined ones. */;
6695 else if (simd)
6697 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6698 (splay_tree_key)decl);
6699 omp_is_private (gimplify_omp_ctxp, decl,
6700 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6701 != 1));
6702 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6703 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6704 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6706 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6707 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6708 if (has_decl_expr
6709 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6710 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6711 OMP_CLAUSE_DECL (c) = decl;
6712 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6713 OMP_FOR_CLAUSES (for_stmt) = c;
6714 omp_add_variable (gimplify_omp_ctxp, decl,
6715 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6717 else
6719 bool lastprivate
6720 = (!has_decl_expr
6721 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6722 c = build_omp_clause (input_location,
6723 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6724 : OMP_CLAUSE_PRIVATE);
6725 OMP_CLAUSE_DECL (c) = decl;
6726 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6727 OMP_FOR_CLAUSES (for_stmt) = c;
6728 omp_add_variable (gimplify_omp_ctxp, decl,
6729 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6730 | GOVD_EXPLICIT | GOVD_SEEN);
6731 c = NULL_TREE;
6734 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6735 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6736 else
6737 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6739 /* If DECL is not a gimple register, create a temporary variable to act
6740 as an iteration counter. This is valid, since DECL cannot be
6741 modified in the body of the loop. */
6742 if (orig_for_stmt != for_stmt)
6743 var = decl;
6744 else if (!is_gimple_reg (decl))
6746 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6747 TREE_OPERAND (t, 0) = var;
6749 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6751 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6753 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6754 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
6755 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
6756 OMP_CLAUSE_DECL (c2) = var;
6757 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
6758 OMP_FOR_CLAUSES (for_stmt) = c2;
6759 omp_add_variable (gimplify_omp_ctxp, var,
6760 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6761 if (c == NULL_TREE)
6763 c = c2;
6764 c2 = NULL_TREE;
6767 else
6768 omp_add_variable (gimplify_omp_ctxp, var,
6769 GOVD_PRIVATE | GOVD_SEEN);
6771 else
6772 var = decl;
6774 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6775 is_gimple_val, fb_rvalue);
6776 ret = MIN (ret, tret);
6777 if (ret == GS_ERROR)
6778 return ret;
6780 /* Handle OMP_FOR_COND. */
6781 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6782 gcc_assert (COMPARISON_CLASS_P (t));
6783 gcc_assert (TREE_OPERAND (t, 0) == decl);
6785 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6786 is_gimple_val, fb_rvalue);
6787 ret = MIN (ret, tret);
6789 /* Handle OMP_FOR_INCR. */
6790 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6791 switch (TREE_CODE (t))
6793 case PREINCREMENT_EXPR:
6794 case POSTINCREMENT_EXPR:
6796 tree decl = TREE_OPERAND (t, 0);
6797 // c_omp_for_incr_canonicalize_ptr() should have been
6798 // called to massage things appropriately.
6799 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6801 if (orig_for_stmt != for_stmt)
6802 break;
6803 t = build_int_cst (TREE_TYPE (decl), 1);
6804 if (c)
6805 OMP_CLAUSE_LINEAR_STEP (c) = t;
6806 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6807 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6808 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6809 break;
6812 case PREDECREMENT_EXPR:
6813 case POSTDECREMENT_EXPR:
6814 if (orig_for_stmt != for_stmt)
6815 break;
6816 t = build_int_cst (TREE_TYPE (decl), -1);
6817 if (c)
6818 OMP_CLAUSE_LINEAR_STEP (c) = t;
6819 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6820 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6821 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6822 break;
6824 case MODIFY_EXPR:
6825 gcc_assert (TREE_OPERAND (t, 0) == decl);
6826 TREE_OPERAND (t, 0) = var;
6828 t = TREE_OPERAND (t, 1);
6829 switch (TREE_CODE (t))
6831 case PLUS_EXPR:
6832 if (TREE_OPERAND (t, 1) == decl)
6834 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6835 TREE_OPERAND (t, 0) = var;
6836 break;
6839 /* Fallthru. */
6840 case MINUS_EXPR:
6841 case POINTER_PLUS_EXPR:
6842 gcc_assert (TREE_OPERAND (t, 0) == decl);
6843 TREE_OPERAND (t, 0) = var;
6844 break;
6845 default:
6846 gcc_unreachable ();
6849 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6850 is_gimple_val, fb_rvalue);
6851 ret = MIN (ret, tret);
6852 if (c)
6854 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
6855 if (TREE_CODE (t) == MINUS_EXPR)
6857 t = TREE_OPERAND (t, 1);
6858 OMP_CLAUSE_LINEAR_STEP (c)
6859 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
6860 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6861 &for_pre_body, NULL,
6862 is_gimple_val, fb_rvalue);
6863 ret = MIN (ret, tret);
6866 break;
6868 default:
6869 gcc_unreachable ();
6872 if (c2)
6874 gcc_assert (c);
6875 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
6878 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6879 && orig_for_stmt == for_stmt)
6881 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6882 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6883 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6884 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6885 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
6886 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
6887 && OMP_CLAUSE_DECL (c) == decl)
6889 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6890 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6891 gcc_assert (TREE_OPERAND (t, 0) == var);
6892 t = TREE_OPERAND (t, 1);
6893 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6894 || TREE_CODE (t) == MINUS_EXPR
6895 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6896 gcc_assert (TREE_OPERAND (t, 0) == var);
6897 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6898 TREE_OPERAND (t, 1));
6899 gimple_seq *seq;
6900 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
6901 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
6902 else
6903 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
6904 gimplify_assign (decl, t, seq);
6909 BITMAP_FREE (has_decl_expr);
6911 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
6913 if (orig_for_stmt != for_stmt)
6914 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6916 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6917 decl = TREE_OPERAND (t, 0);
6918 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6919 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6920 TREE_OPERAND (t, 0) = var;
6921 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6922 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
6923 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
6926 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
6928 int kind;
6929 switch (TREE_CODE (orig_for_stmt))
6931 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
6932 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
6933 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
6934 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
6935 default:
6936 gcc_unreachable ();
6938 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
6939 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6940 for_pre_body);
6941 if (orig_for_stmt != for_stmt)
6942 gimple_omp_for_set_combined_p (gfor, true);
6943 if (gimplify_omp_ctxp
6944 && (gimplify_omp_ctxp->combined_loop
6945 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
6946 && gimplify_omp_ctxp->outer_context
6947 && gimplify_omp_ctxp->outer_context->combined_loop)))
6949 gimple_omp_for_set_combined_into_p (gfor, true);
6950 if (gimplify_omp_ctxp->combined_loop)
6951 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
6952 else
6953 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
6956 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6958 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6959 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6960 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6961 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6962 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6963 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6964 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6965 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6968 gimplify_seq_add_stmt (pre_p, gfor);
6969 if (ret != GS_ALL_DONE)
6970 return GS_ERROR;
6971 *expr_p = NULL_TREE;
6972 return GS_ALL_DONE;
6975 /* Gimplify the gross structure of other OpenMP constructs.
6976 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
6977 and OMP_TEAMS. */
6979 static void
6980 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6982 tree expr = *expr_p;
6983 gimple stmt;
6984 gimple_seq body = NULL;
6985 enum omp_region_type ort = ORT_WORKSHARE;
6987 switch (TREE_CODE (expr))
6989 case OMP_SECTIONS:
6990 case OMP_SINGLE:
6991 break;
6992 case OMP_TARGET:
6993 ort = ORT_TARGET;
6994 break;
6995 case OMP_TARGET_DATA:
6996 ort = ORT_TARGET_DATA;
6997 break;
6998 case OMP_TEAMS:
6999 ort = ORT_TEAMS;
7000 break;
7001 default:
7002 gcc_unreachable ();
7004 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7005 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7007 push_gimplify_context ();
7008 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7009 if (gimple_code (g) == GIMPLE_BIND)
7010 pop_gimplify_context (g);
7011 else
7012 pop_gimplify_context (NULL);
7013 if (ort == ORT_TARGET_DATA)
7015 gimple_seq cleanup = NULL;
7016 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
7017 g = gimple_build_call (fn, 0);
7018 gimple_seq_add_stmt (&cleanup, g);
7019 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7020 body = NULL;
7021 gimple_seq_add_stmt (&body, g);
7024 else
7025 gimplify_and_add (OMP_BODY (expr), &body);
7026 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
7028 switch (TREE_CODE (expr))
7030 case OMP_SECTIONS:
7031 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7032 break;
7033 case OMP_SINGLE:
7034 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7035 break;
7036 case OMP_TARGET:
7037 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7038 OMP_CLAUSES (expr));
7039 break;
7040 case OMP_TARGET_DATA:
7041 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7042 OMP_CLAUSES (expr));
7043 break;
7044 case OMP_TEAMS:
7045 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7046 break;
7047 default:
7048 gcc_unreachable ();
7051 gimplify_seq_add_stmt (pre_p, stmt);
7052 *expr_p = NULL_TREE;
7055 /* Gimplify the gross structure of OpenMP target update construct. */
7057 static void
7058 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7060 tree expr = *expr_p;
7061 gimple stmt;
7063 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7064 ORT_WORKSHARE);
7065 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
7066 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7067 OMP_TARGET_UPDATE_CLAUSES (expr));
7069 gimplify_seq_add_stmt (pre_p, stmt);
7070 *expr_p = NULL_TREE;
7073 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7074 stabilized the lhs of the atomic operation as *ADDR. Return true if
7075 EXPR is this stabilized form. */
7077 static bool
7078 goa_lhs_expr_p (tree expr, tree addr)
7080 /* Also include casts to other type variants. The C front end is fond
7081 of adding these for e.g. volatile variables. This is like
7082 STRIP_TYPE_NOPS but includes the main variant lookup. */
7083 STRIP_USELESS_TYPE_CONVERSION (expr);
7085 if (TREE_CODE (expr) == INDIRECT_REF)
7087 expr = TREE_OPERAND (expr, 0);
7088 while (expr != addr
7089 && (CONVERT_EXPR_P (expr)
7090 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7091 && TREE_CODE (expr) == TREE_CODE (addr)
7092 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7094 expr = TREE_OPERAND (expr, 0);
7095 addr = TREE_OPERAND (addr, 0);
7097 if (expr == addr)
7098 return true;
7099 return (TREE_CODE (addr) == ADDR_EXPR
7100 && TREE_CODE (expr) == ADDR_EXPR
7101 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7103 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7104 return true;
7105 return false;
7108 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7109 expression does not involve the lhs, evaluate it into a temporary.
7110 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7111 or -1 if an error was encountered. */
7113 static int
7114 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7115 tree lhs_var)
7117 tree expr = *expr_p;
7118 int saw_lhs;
7120 if (goa_lhs_expr_p (expr, lhs_addr))
7122 *expr_p = lhs_var;
7123 return 1;
7125 if (is_gimple_val (expr))
7126 return 0;
7128 saw_lhs = 0;
7129 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7131 case tcc_binary:
7132 case tcc_comparison:
7133 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7134 lhs_var);
7135 case tcc_unary:
7136 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7137 lhs_var);
7138 break;
7139 case tcc_expression:
7140 switch (TREE_CODE (expr))
7142 case TRUTH_ANDIF_EXPR:
7143 case TRUTH_ORIF_EXPR:
7144 case TRUTH_AND_EXPR:
7145 case TRUTH_OR_EXPR:
7146 case TRUTH_XOR_EXPR:
7147 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7148 lhs_addr, lhs_var);
7149 case TRUTH_NOT_EXPR:
7150 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7151 lhs_addr, lhs_var);
7152 break;
7153 case COMPOUND_EXPR:
7154 /* Break out any preevaluations from cp_build_modify_expr. */
7155 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7156 expr = TREE_OPERAND (expr, 1))
7157 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7158 *expr_p = expr;
7159 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7160 default:
7161 break;
7163 break;
7164 default:
7165 break;
7168 if (saw_lhs == 0)
7170 enum gimplify_status gs;
7171 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7172 if (gs != GS_ALL_DONE)
7173 saw_lhs = -1;
7176 return saw_lhs;
7179 /* Gimplify an OMP_ATOMIC statement. */
7181 static enum gimplify_status
7182 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7184 tree addr = TREE_OPERAND (*expr_p, 0);
7185 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7186 ? NULL : TREE_OPERAND (*expr_p, 1);
7187 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7188 tree tmp_load;
7189 gimple loadstmt, storestmt;
7191 tmp_load = create_tmp_reg (type, NULL);
7192 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7193 return GS_ERROR;
7195 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7196 != GS_ALL_DONE)
7197 return GS_ERROR;
7199 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7200 gimplify_seq_add_stmt (pre_p, loadstmt);
7201 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7202 != GS_ALL_DONE)
7203 return GS_ERROR;
7205 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7206 rhs = tmp_load;
7207 storestmt = gimple_build_omp_atomic_store (rhs);
7208 gimplify_seq_add_stmt (pre_p, storestmt);
7209 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7211 gimple_omp_atomic_set_seq_cst (loadstmt);
7212 gimple_omp_atomic_set_seq_cst (storestmt);
7214 switch (TREE_CODE (*expr_p))
7216 case OMP_ATOMIC_READ:
7217 case OMP_ATOMIC_CAPTURE_OLD:
7218 *expr_p = tmp_load;
7219 gimple_omp_atomic_set_need_value (loadstmt);
7220 break;
7221 case OMP_ATOMIC_CAPTURE_NEW:
7222 *expr_p = rhs;
7223 gimple_omp_atomic_set_need_value (storestmt);
7224 break;
7225 default:
7226 *expr_p = NULL;
7227 break;
7230 return GS_ALL_DONE;
7233 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7234 body, and adding some EH bits. */
7236 static enum gimplify_status
7237 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7239 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7240 gimple g;
7241 gimple_seq body = NULL;
7242 int subcode = 0;
7244 /* Wrap the transaction body in a BIND_EXPR so we have a context
7245 where to put decls for OpenMP. */
7246 if (TREE_CODE (tbody) != BIND_EXPR)
7248 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7249 TREE_SIDE_EFFECTS (bind) = 1;
7250 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7251 TRANSACTION_EXPR_BODY (expr) = bind;
7254 push_gimplify_context ();
7255 temp = voidify_wrapper_expr (*expr_p, NULL);
7257 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7258 pop_gimplify_context (g);
7260 g = gimple_build_transaction (body, NULL);
7261 if (TRANSACTION_EXPR_OUTER (expr))
7262 subcode = GTMA_IS_OUTER;
7263 else if (TRANSACTION_EXPR_RELAXED (expr))
7264 subcode = GTMA_IS_RELAXED;
7265 gimple_transaction_set_subcode (g, subcode);
7267 gimplify_seq_add_stmt (pre_p, g);
7269 if (temp)
7271 *expr_p = temp;
7272 return GS_OK;
7275 *expr_p = NULL_TREE;
7276 return GS_ALL_DONE;
7279 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7280 expression produces a value to be used as an operand inside a GIMPLE
7281 statement, the value will be stored back in *EXPR_P. This value will
7282 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7283 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7284 emitted in PRE_P and POST_P.
7286 Additionally, this process may overwrite parts of the input
7287 expression during gimplification. Ideally, it should be
7288 possible to do non-destructive gimplification.
7290 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7291 the expression needs to evaluate to a value to be used as
7292 an operand in a GIMPLE statement, this value will be stored in
7293 *EXPR_P on exit. This happens when the caller specifies one
7294 of fb_lvalue or fb_rvalue fallback flags.
7296 PRE_P will contain the sequence of GIMPLE statements corresponding
7297 to the evaluation of EXPR and all the side-effects that must
7298 be executed before the main expression. On exit, the last
7299 statement of PRE_P is the core statement being gimplified. For
7300 instance, when gimplifying 'if (++a)' the last statement in
7301 PRE_P will be 'if (t.1)' where t.1 is the result of
7302 pre-incrementing 'a'.
7304 POST_P will contain the sequence of GIMPLE statements corresponding
7305 to the evaluation of all the side-effects that must be executed
7306 after the main expression. If this is NULL, the post
7307 side-effects are stored at the end of PRE_P.
7309 The reason why the output is split in two is to handle post
7310 side-effects explicitly. In some cases, an expression may have
7311 inner and outer post side-effects which need to be emitted in
7312 an order different from the one given by the recursive
7313 traversal. For instance, for the expression (*p--)++ the post
7314 side-effects of '--' must actually occur *after* the post
7315 side-effects of '++'. However, gimplification will first visit
7316 the inner expression, so if a separate POST sequence was not
7317 used, the resulting sequence would be:
7319 1 t.1 = *p
7320 2 p = p - 1
7321 3 t.2 = t.1 + 1
7322 4 *p = t.2
7324 However, the post-decrement operation in line #2 must not be
7325 evaluated until after the store to *p at line #4, so the
7326 correct sequence should be:
7328 1 t.1 = *p
7329 2 t.2 = t.1 + 1
7330 3 *p = t.2
7331 4 p = p - 1
7333 So, by specifying a separate post queue, it is possible
7334 to emit the post side-effects in the correct order.
7335 If POST_P is NULL, an internal queue will be used. Before
7336 returning to the caller, the sequence POST_P is appended to
7337 the main output sequence PRE_P.
7339 GIMPLE_TEST_F points to a function that takes a tree T and
7340 returns nonzero if T is in the GIMPLE form requested by the
7341 caller. The GIMPLE predicates are in gimple.c.
7343 FALLBACK tells the function what sort of a temporary we want if
7344 gimplification cannot produce an expression that complies with
7345 GIMPLE_TEST_F.
7347 fb_none means that no temporary should be generated
7348 fb_rvalue means that an rvalue is OK to generate
7349 fb_lvalue means that an lvalue is OK to generate
7350 fb_either means that either is OK, but an lvalue is preferable.
7351 fb_mayfail means that gimplification may fail (in which case
7352 GS_ERROR will be returned)
7354 The return value is either GS_ERROR or GS_ALL_DONE, since this
7355 function iterates until EXPR is completely gimplified or an error
7356 occurs. */
7358 enum gimplify_status
7359 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7360 bool (*gimple_test_f) (tree), fallback_t fallback)
7362 tree tmp;
7363 gimple_seq internal_pre = NULL;
7364 gimple_seq internal_post = NULL;
7365 tree save_expr;
7366 bool is_statement;
7367 location_t saved_location;
7368 enum gimplify_status ret;
7369 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7371 save_expr = *expr_p;
7372 if (save_expr == NULL_TREE)
7373 return GS_ALL_DONE;
7375 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7376 is_statement = gimple_test_f == is_gimple_stmt;
7377 if (is_statement)
7378 gcc_assert (pre_p);
7380 /* Consistency checks. */
7381 if (gimple_test_f == is_gimple_reg)
7382 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7383 else if (gimple_test_f == is_gimple_val
7384 || gimple_test_f == is_gimple_call_addr
7385 || gimple_test_f == is_gimple_condexpr
7386 || gimple_test_f == is_gimple_mem_rhs
7387 || gimple_test_f == is_gimple_mem_rhs_or_call
7388 || gimple_test_f == is_gimple_reg_rhs
7389 || gimple_test_f == is_gimple_reg_rhs_or_call
7390 || gimple_test_f == is_gimple_asm_val
7391 || gimple_test_f == is_gimple_mem_ref_addr)
7392 gcc_assert (fallback & fb_rvalue);
7393 else if (gimple_test_f == is_gimple_min_lval
7394 || gimple_test_f == is_gimple_lvalue)
7395 gcc_assert (fallback & fb_lvalue);
7396 else if (gimple_test_f == is_gimple_addressable)
7397 gcc_assert (fallback & fb_either);
7398 else if (gimple_test_f == is_gimple_stmt)
7399 gcc_assert (fallback == fb_none);
7400 else
7402 /* We should have recognized the GIMPLE_TEST_F predicate to
7403 know what kind of fallback to use in case a temporary is
7404 needed to hold the value or address of *EXPR_P. */
7405 gcc_unreachable ();
7408 /* We used to check the predicate here and return immediately if it
7409 succeeds. This is wrong; the design is for gimplification to be
7410 idempotent, and for the predicates to only test for valid forms, not
7411 whether they are fully simplified. */
7412 if (pre_p == NULL)
7413 pre_p = &internal_pre;
7415 if (post_p == NULL)
7416 post_p = &internal_post;
7418 /* Remember the last statements added to PRE_P and POST_P. Every
7419 new statement added by the gimplification helpers needs to be
7420 annotated with location information. To centralize the
7421 responsibility, we remember the last statement that had been
7422 added to both queues before gimplifying *EXPR_P. If
7423 gimplification produces new statements in PRE_P and POST_P, those
7424 statements will be annotated with the same location information
7425 as *EXPR_P. */
7426 pre_last_gsi = gsi_last (*pre_p);
7427 post_last_gsi = gsi_last (*post_p);
7429 saved_location = input_location;
7430 if (save_expr != error_mark_node
7431 && EXPR_HAS_LOCATION (*expr_p))
7432 input_location = EXPR_LOCATION (*expr_p);
7434 /* Loop over the specific gimplifiers until the toplevel node
7435 remains the same. */
7438 /* Strip away as many useless type conversions as possible
7439 at the toplevel. */
7440 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7442 /* Remember the expr. */
7443 save_expr = *expr_p;
7445 /* Die, die, die, my darling. */
7446 if (save_expr == error_mark_node
7447 || (TREE_TYPE (save_expr)
7448 && TREE_TYPE (save_expr) == error_mark_node))
7450 ret = GS_ERROR;
7451 break;
7454 /* Do any language-specific gimplification. */
7455 ret = ((enum gimplify_status)
7456 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7457 if (ret == GS_OK)
7459 if (*expr_p == NULL_TREE)
7460 break;
7461 if (*expr_p != save_expr)
7462 continue;
7464 else if (ret != GS_UNHANDLED)
7465 break;
7467 /* Make sure that all the cases set 'ret' appropriately. */
7468 ret = GS_UNHANDLED;
7469 switch (TREE_CODE (*expr_p))
7471 /* First deal with the special cases. */
7473 case POSTINCREMENT_EXPR:
7474 case POSTDECREMENT_EXPR:
7475 case PREINCREMENT_EXPR:
7476 case PREDECREMENT_EXPR:
7477 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7478 fallback != fb_none,
7479 TREE_TYPE (*expr_p));
7480 break;
7482 case VIEW_CONVERT_EXPR:
7483 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7484 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7486 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7487 post_p, is_gimple_val, fb_rvalue);
7488 recalculate_side_effects (*expr_p);
7489 break;
7491 /* Fallthru. */
7493 case ARRAY_REF:
7494 case ARRAY_RANGE_REF:
7495 case REALPART_EXPR:
7496 case IMAGPART_EXPR:
7497 case COMPONENT_REF:
7498 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7499 fallback ? fallback : fb_rvalue);
7500 break;
7502 case COND_EXPR:
7503 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7505 /* C99 code may assign to an array in a structure value of a
7506 conditional expression, and this has undefined behavior
7507 only on execution, so create a temporary if an lvalue is
7508 required. */
7509 if (fallback == fb_lvalue)
7511 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7512 mark_addressable (*expr_p);
7513 ret = GS_OK;
7515 break;
7517 case CALL_EXPR:
7518 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7520 /* C99 code may assign to an array in a structure returned
7521 from a function, and this has undefined behavior only on
7522 execution, so create a temporary if an lvalue is
7523 required. */
7524 if (fallback == fb_lvalue)
7526 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7527 mark_addressable (*expr_p);
7528 ret = GS_OK;
7530 break;
7532 case TREE_LIST:
7533 gcc_unreachable ();
7535 case COMPOUND_EXPR:
7536 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7537 break;
7539 case COMPOUND_LITERAL_EXPR:
7540 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7541 gimple_test_f, fallback);
7542 break;
7544 case MODIFY_EXPR:
7545 case INIT_EXPR:
7546 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7547 fallback != fb_none);
7548 break;
7550 case TRUTH_ANDIF_EXPR:
7551 case TRUTH_ORIF_EXPR:
7553 /* Preserve the original type of the expression and the
7554 source location of the outer expression. */
7555 tree org_type = TREE_TYPE (*expr_p);
7556 *expr_p = gimple_boolify (*expr_p);
7557 *expr_p = build3_loc (input_location, COND_EXPR,
7558 org_type, *expr_p,
7559 fold_convert_loc
7560 (input_location,
7561 org_type, boolean_true_node),
7562 fold_convert_loc
7563 (input_location,
7564 org_type, boolean_false_node));
7565 ret = GS_OK;
7566 break;
7569 case TRUTH_NOT_EXPR:
7571 tree type = TREE_TYPE (*expr_p);
7572 /* The parsers are careful to generate TRUTH_NOT_EXPR
7573 only with operands that are always zero or one.
7574 We do not fold here but handle the only interesting case
7575 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7576 *expr_p = gimple_boolify (*expr_p);
7577 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7578 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7579 TREE_TYPE (*expr_p),
7580 TREE_OPERAND (*expr_p, 0));
7581 else
7582 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7583 TREE_TYPE (*expr_p),
7584 TREE_OPERAND (*expr_p, 0),
7585 build_int_cst (TREE_TYPE (*expr_p), 1));
7586 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7587 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7588 ret = GS_OK;
7589 break;
7592 case ADDR_EXPR:
7593 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7594 break;
7596 case ANNOTATE_EXPR:
7598 tree cond = TREE_OPERAND (*expr_p, 0);
7599 tree kind = TREE_OPERAND (*expr_p, 1);
7600 tree type = TREE_TYPE (cond);
7601 if (!INTEGRAL_TYPE_P (type))
7603 *expr_p = cond;
7604 ret = GS_OK;
7605 break;
7607 tree tmp = create_tmp_var (type, NULL);
7608 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7609 gimple call
7610 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7611 gimple_call_set_lhs (call, tmp);
7612 gimplify_seq_add_stmt (pre_p, call);
7613 *expr_p = tmp;
7614 ret = GS_ALL_DONE;
7615 break;
7618 case VA_ARG_EXPR:
7619 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7620 break;
7622 CASE_CONVERT:
7623 if (IS_EMPTY_STMT (*expr_p))
7625 ret = GS_ALL_DONE;
7626 break;
7629 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7630 || fallback == fb_none)
7632 /* Just strip a conversion to void (or in void context) and
7633 try again. */
7634 *expr_p = TREE_OPERAND (*expr_p, 0);
7635 ret = GS_OK;
7636 break;
7639 ret = gimplify_conversion (expr_p);
7640 if (ret == GS_ERROR)
7641 break;
7642 if (*expr_p != save_expr)
7643 break;
7644 /* FALLTHRU */
7646 case FIX_TRUNC_EXPR:
7647 /* unary_expr: ... | '(' cast ')' val | ... */
7648 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7649 is_gimple_val, fb_rvalue);
7650 recalculate_side_effects (*expr_p);
7651 break;
7653 case INDIRECT_REF:
7655 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7656 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7657 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7659 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7660 if (*expr_p != save_expr)
7662 ret = GS_OK;
7663 break;
7666 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7667 is_gimple_reg, fb_rvalue);
7668 if (ret == GS_ERROR)
7669 break;
7671 recalculate_side_effects (*expr_p);
7672 *expr_p = fold_build2_loc (input_location, MEM_REF,
7673 TREE_TYPE (*expr_p),
7674 TREE_OPERAND (*expr_p, 0),
7675 build_int_cst (saved_ptr_type, 0));
7676 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7677 TREE_THIS_NOTRAP (*expr_p) = notrap;
7678 ret = GS_OK;
7679 break;
7682 /* We arrive here through the various re-gimplifcation paths. */
7683 case MEM_REF:
7684 /* First try re-folding the whole thing. */
7685 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7686 TREE_OPERAND (*expr_p, 0),
7687 TREE_OPERAND (*expr_p, 1));
7688 if (tmp)
7690 *expr_p = tmp;
7691 recalculate_side_effects (*expr_p);
7692 ret = GS_OK;
7693 break;
7695 /* Avoid re-gimplifying the address operand if it is already
7696 in suitable form. Re-gimplifying would mark the address
7697 operand addressable. Always gimplify when not in SSA form
7698 as we still may have to gimplify decls with value-exprs. */
7699 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7700 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7702 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7703 is_gimple_mem_ref_addr, fb_rvalue);
7704 if (ret == GS_ERROR)
7705 break;
7707 recalculate_side_effects (*expr_p);
7708 ret = GS_ALL_DONE;
7709 break;
7711 /* Constants need not be gimplified. */
7712 case INTEGER_CST:
7713 case REAL_CST:
7714 case FIXED_CST:
7715 case STRING_CST:
7716 case COMPLEX_CST:
7717 case VECTOR_CST:
7718 /* Drop the overflow flag on constants, we do not want
7719 that in the GIMPLE IL. */
7720 if (TREE_OVERFLOW_P (*expr_p))
7721 *expr_p = drop_tree_overflow (*expr_p);
7722 ret = GS_ALL_DONE;
7723 break;
7725 case CONST_DECL:
7726 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7727 CONST_DECL node. Otherwise the decl is replaceable by its
7728 value. */
7729 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7730 if (fallback & fb_lvalue)
7731 ret = GS_ALL_DONE;
7732 else
7734 *expr_p = DECL_INITIAL (*expr_p);
7735 ret = GS_OK;
7737 break;
7739 case DECL_EXPR:
7740 ret = gimplify_decl_expr (expr_p, pre_p);
7741 break;
7743 case BIND_EXPR:
7744 ret = gimplify_bind_expr (expr_p, pre_p);
7745 break;
7747 case LOOP_EXPR:
7748 ret = gimplify_loop_expr (expr_p, pre_p);
7749 break;
7751 case SWITCH_EXPR:
7752 ret = gimplify_switch_expr (expr_p, pre_p);
7753 break;
7755 case EXIT_EXPR:
7756 ret = gimplify_exit_expr (expr_p);
7757 break;
7759 case GOTO_EXPR:
7760 /* If the target is not LABEL, then it is a computed jump
7761 and the target needs to be gimplified. */
7762 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7764 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7765 NULL, is_gimple_val, fb_rvalue);
7766 if (ret == GS_ERROR)
7767 break;
7769 gimplify_seq_add_stmt (pre_p,
7770 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7771 ret = GS_ALL_DONE;
7772 break;
7774 case PREDICT_EXPR:
7775 gimplify_seq_add_stmt (pre_p,
7776 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7777 PREDICT_EXPR_OUTCOME (*expr_p)));
7778 ret = GS_ALL_DONE;
7779 break;
7781 case LABEL_EXPR:
7782 ret = GS_ALL_DONE;
7783 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7784 == current_function_decl);
7785 gimplify_seq_add_stmt (pre_p,
7786 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7787 break;
7789 case CASE_LABEL_EXPR:
7790 ret = gimplify_case_label_expr (expr_p, pre_p);
7791 break;
7793 case RETURN_EXPR:
7794 ret = gimplify_return_expr (*expr_p, pre_p);
7795 break;
7797 case CONSTRUCTOR:
7798 /* Don't reduce this in place; let gimplify_init_constructor work its
7799 magic. Buf if we're just elaborating this for side effects, just
7800 gimplify any element that has side-effects. */
7801 if (fallback == fb_none)
7803 unsigned HOST_WIDE_INT ix;
7804 tree val;
7805 tree temp = NULL_TREE;
7806 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7807 if (TREE_SIDE_EFFECTS (val))
7808 append_to_statement_list (val, &temp);
7810 *expr_p = temp;
7811 ret = temp ? GS_OK : GS_ALL_DONE;
7813 /* C99 code may assign to an array in a constructed
7814 structure or union, and this has undefined behavior only
7815 on execution, so create a temporary if an lvalue is
7816 required. */
7817 else if (fallback == fb_lvalue)
7819 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7820 mark_addressable (*expr_p);
7821 ret = GS_OK;
7823 else
7824 ret = GS_ALL_DONE;
7825 break;
7827 /* The following are special cases that are not handled by the
7828 original GIMPLE grammar. */
7830 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7831 eliminated. */
7832 case SAVE_EXPR:
7833 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7834 break;
7836 case BIT_FIELD_REF:
7837 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7838 post_p, is_gimple_lvalue, fb_either);
7839 recalculate_side_effects (*expr_p);
7840 break;
7842 case TARGET_MEM_REF:
7844 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7846 if (TMR_BASE (*expr_p))
7847 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7848 post_p, is_gimple_mem_ref_addr, fb_either);
7849 if (TMR_INDEX (*expr_p))
7850 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7851 post_p, is_gimple_val, fb_rvalue);
7852 if (TMR_INDEX2 (*expr_p))
7853 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7854 post_p, is_gimple_val, fb_rvalue);
7855 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7856 ret = MIN (r0, r1);
7858 break;
7860 case NON_LVALUE_EXPR:
7861 /* This should have been stripped above. */
7862 gcc_unreachable ();
7864 case ASM_EXPR:
7865 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7866 break;
7868 case TRY_FINALLY_EXPR:
7869 case TRY_CATCH_EXPR:
7871 gimple_seq eval, cleanup;
7872 gimple try_;
7874 /* Calls to destructors are generated automatically in FINALLY/CATCH
7875 block. They should have location as UNKNOWN_LOCATION. However,
7876 gimplify_call_expr will reset these call stmts to input_location
7877 if it finds stmt's location is unknown. To prevent resetting for
7878 destructors, we set the input_location to unknown.
7879 Note that this only affects the destructor calls in FINALLY/CATCH
7880 block, and will automatically reset to its original value by the
7881 end of gimplify_expr. */
7882 input_location = UNKNOWN_LOCATION;
7883 eval = cleanup = NULL;
7884 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7885 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7886 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7887 if (gimple_seq_empty_p (cleanup))
7889 gimple_seq_add_seq (pre_p, eval);
7890 ret = GS_ALL_DONE;
7891 break;
7893 try_ = gimple_build_try (eval, cleanup,
7894 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7895 ? GIMPLE_TRY_FINALLY
7896 : GIMPLE_TRY_CATCH);
7897 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7898 gimple_set_location (try_, saved_location);
7899 else
7900 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7901 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7902 gimple_try_set_catch_is_cleanup (try_,
7903 TRY_CATCH_IS_CLEANUP (*expr_p));
7904 gimplify_seq_add_stmt (pre_p, try_);
7905 ret = GS_ALL_DONE;
7906 break;
7909 case CLEANUP_POINT_EXPR:
7910 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7911 break;
7913 case TARGET_EXPR:
7914 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7915 break;
7917 case CATCH_EXPR:
7919 gimple c;
7920 gimple_seq handler = NULL;
7921 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7922 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7923 gimplify_seq_add_stmt (pre_p, c);
7924 ret = GS_ALL_DONE;
7925 break;
7928 case EH_FILTER_EXPR:
7930 gimple ehf;
7931 gimple_seq failure = NULL;
7933 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7934 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7935 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7936 gimplify_seq_add_stmt (pre_p, ehf);
7937 ret = GS_ALL_DONE;
7938 break;
7941 case OBJ_TYPE_REF:
7943 enum gimplify_status r0, r1;
7944 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7945 post_p, is_gimple_val, fb_rvalue);
7946 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7947 post_p, is_gimple_val, fb_rvalue);
7948 TREE_SIDE_EFFECTS (*expr_p) = 0;
7949 ret = MIN (r0, r1);
7951 break;
7953 case LABEL_DECL:
7954 /* We get here when taking the address of a label. We mark
7955 the label as "forced"; meaning it can never be removed and
7956 it is a potential target for any computed goto. */
7957 FORCED_LABEL (*expr_p) = 1;
7958 ret = GS_ALL_DONE;
7959 break;
7961 case STATEMENT_LIST:
7962 ret = gimplify_statement_list (expr_p, pre_p);
7963 break;
7965 case WITH_SIZE_EXPR:
7967 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7968 post_p == &internal_post ? NULL : post_p,
7969 gimple_test_f, fallback);
7970 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7971 is_gimple_val, fb_rvalue);
7972 ret = GS_ALL_DONE;
7974 break;
7976 case VAR_DECL:
7977 case PARM_DECL:
7978 ret = gimplify_var_or_parm_decl (expr_p);
7979 break;
7981 case RESULT_DECL:
7982 /* When within an OpenMP context, notice uses of variables. */
7983 if (gimplify_omp_ctxp)
7984 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7985 ret = GS_ALL_DONE;
7986 break;
7988 case SSA_NAME:
7989 /* Allow callbacks into the gimplifier during optimization. */
7990 ret = GS_ALL_DONE;
7991 break;
7993 case OMP_PARALLEL:
7994 gimplify_omp_parallel (expr_p, pre_p);
7995 ret = GS_ALL_DONE;
7996 break;
7998 case OMP_TASK:
7999 gimplify_omp_task (expr_p, pre_p);
8000 ret = GS_ALL_DONE;
8001 break;
8003 case OMP_FOR:
8004 case OMP_SIMD:
8005 case CILK_SIMD:
8006 case OMP_DISTRIBUTE:
8007 ret = gimplify_omp_for (expr_p, pre_p);
8008 break;
8010 case OMP_SECTIONS:
8011 case OMP_SINGLE:
8012 case OMP_TARGET:
8013 case OMP_TARGET_DATA:
8014 case OMP_TEAMS:
8015 gimplify_omp_workshare (expr_p, pre_p);
8016 ret = GS_ALL_DONE;
8017 break;
8019 case OMP_TARGET_UPDATE:
8020 gimplify_omp_target_update (expr_p, pre_p);
8021 ret = GS_ALL_DONE;
8022 break;
8024 case OMP_SECTION:
8025 case OMP_MASTER:
8026 case OMP_TASKGROUP:
8027 case OMP_ORDERED:
8028 case OMP_CRITICAL:
8030 gimple_seq body = NULL;
8031 gimple g;
8033 gimplify_and_add (OMP_BODY (*expr_p), &body);
8034 switch (TREE_CODE (*expr_p))
8036 case OMP_SECTION:
8037 g = gimple_build_omp_section (body);
8038 break;
8039 case OMP_MASTER:
8040 g = gimple_build_omp_master (body);
8041 break;
8042 case OMP_TASKGROUP:
8044 gimple_seq cleanup = NULL;
8045 tree fn
8046 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8047 g = gimple_build_call (fn, 0);
8048 gimple_seq_add_stmt (&cleanup, g);
8049 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8050 body = NULL;
8051 gimple_seq_add_stmt (&body, g);
8052 g = gimple_build_omp_taskgroup (body);
8054 break;
8055 case OMP_ORDERED:
8056 g = gimple_build_omp_ordered (body);
8057 break;
8058 case OMP_CRITICAL:
8059 g = gimple_build_omp_critical (body,
8060 OMP_CRITICAL_NAME (*expr_p));
8061 break;
8062 default:
8063 gcc_unreachable ();
8065 gimplify_seq_add_stmt (pre_p, g);
8066 ret = GS_ALL_DONE;
8067 break;
8070 case OMP_ATOMIC:
8071 case OMP_ATOMIC_READ:
8072 case OMP_ATOMIC_CAPTURE_OLD:
8073 case OMP_ATOMIC_CAPTURE_NEW:
8074 ret = gimplify_omp_atomic (expr_p, pre_p);
8075 break;
8077 case TRANSACTION_EXPR:
8078 ret = gimplify_transaction (expr_p, pre_p);
8079 break;
8081 case TRUTH_AND_EXPR:
8082 case TRUTH_OR_EXPR:
8083 case TRUTH_XOR_EXPR:
8085 tree orig_type = TREE_TYPE (*expr_p);
8086 tree new_type, xop0, xop1;
8087 *expr_p = gimple_boolify (*expr_p);
8088 new_type = TREE_TYPE (*expr_p);
8089 if (!useless_type_conversion_p (orig_type, new_type))
8091 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8092 ret = GS_OK;
8093 break;
8096 /* Boolified binary truth expressions are semantically equivalent
8097 to bitwise binary expressions. Canonicalize them to the
8098 bitwise variant. */
8099 switch (TREE_CODE (*expr_p))
8101 case TRUTH_AND_EXPR:
8102 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8103 break;
8104 case TRUTH_OR_EXPR:
8105 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8106 break;
8107 case TRUTH_XOR_EXPR:
8108 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8109 break;
8110 default:
8111 break;
8113 /* Now make sure that operands have compatible type to
8114 expression's new_type. */
8115 xop0 = TREE_OPERAND (*expr_p, 0);
8116 xop1 = TREE_OPERAND (*expr_p, 1);
8117 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8118 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8119 new_type,
8120 xop0);
8121 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8122 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8123 new_type,
8124 xop1);
8125 /* Continue classified as tcc_binary. */
8126 goto expr_2;
8129 case FMA_EXPR:
8130 case VEC_COND_EXPR:
8131 case VEC_PERM_EXPR:
8132 /* Classified as tcc_expression. */
8133 goto expr_3;
8135 case POINTER_PLUS_EXPR:
8137 enum gimplify_status r0, r1;
8138 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8139 post_p, is_gimple_val, fb_rvalue);
8140 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8141 post_p, is_gimple_val, fb_rvalue);
8142 recalculate_side_effects (*expr_p);
8143 ret = MIN (r0, r1);
8144 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8145 after gimplifying operands - this is similar to how
8146 it would be folding all gimplified stmts on creation
8147 to have them canonicalized, which is what we eventually
8148 should do anyway. */
8149 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8150 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8152 *expr_p = build_fold_addr_expr_with_type_loc
8153 (input_location,
8154 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8155 TREE_OPERAND (*expr_p, 0),
8156 fold_convert (ptr_type_node,
8157 TREE_OPERAND (*expr_p, 1))),
8158 TREE_TYPE (*expr_p));
8159 ret = MIN (ret, GS_OK);
8161 break;
8164 case CILK_SYNC_STMT:
8166 if (!fn_contains_cilk_spawn_p (cfun))
8168 error_at (EXPR_LOCATION (*expr_p),
8169 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8170 ret = GS_ERROR;
8172 else
8174 gimplify_cilk_sync (expr_p, pre_p);
8175 ret = GS_ALL_DONE;
8177 break;
8180 default:
8181 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8183 case tcc_comparison:
8184 /* Handle comparison of objects of non scalar mode aggregates
8185 with a call to memcmp. It would be nice to only have to do
8186 this for variable-sized objects, but then we'd have to allow
8187 the same nest of reference nodes we allow for MODIFY_EXPR and
8188 that's too complex.
8190 Compare scalar mode aggregates as scalar mode values. Using
8191 memcmp for them would be very inefficient at best, and is
8192 plain wrong if bitfields are involved. */
8194 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8196 /* Vector comparisons need no boolification. */
8197 if (TREE_CODE (type) == VECTOR_TYPE)
8198 goto expr_2;
8199 else if (!AGGREGATE_TYPE_P (type))
8201 tree org_type = TREE_TYPE (*expr_p);
8202 *expr_p = gimple_boolify (*expr_p);
8203 if (!useless_type_conversion_p (org_type,
8204 TREE_TYPE (*expr_p)))
8206 *expr_p = fold_convert_loc (input_location,
8207 org_type, *expr_p);
8208 ret = GS_OK;
8210 else
8211 goto expr_2;
8213 else if (TYPE_MODE (type) != BLKmode)
8214 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8215 else
8216 ret = gimplify_variable_sized_compare (expr_p);
8218 break;
8221 /* If *EXPR_P does not need to be special-cased, handle it
8222 according to its class. */
8223 case tcc_unary:
8224 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8225 post_p, is_gimple_val, fb_rvalue);
8226 break;
8228 case tcc_binary:
8229 expr_2:
8231 enum gimplify_status r0, r1;
8233 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8234 post_p, is_gimple_val, fb_rvalue);
8235 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8236 post_p, is_gimple_val, fb_rvalue);
8238 ret = MIN (r0, r1);
8239 break;
8242 expr_3:
8244 enum gimplify_status r0, r1, r2;
8246 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8247 post_p, is_gimple_val, fb_rvalue);
8248 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8249 post_p, is_gimple_val, fb_rvalue);
8250 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8251 post_p, is_gimple_val, fb_rvalue);
8253 ret = MIN (MIN (r0, r1), r2);
8254 break;
8257 case tcc_declaration:
8258 case tcc_constant:
8259 ret = GS_ALL_DONE;
8260 goto dont_recalculate;
8262 default:
8263 gcc_unreachable ();
8266 recalculate_side_effects (*expr_p);
8268 dont_recalculate:
8269 break;
8272 gcc_assert (*expr_p || ret != GS_OK);
8274 while (ret == GS_OK);
8276 /* If we encountered an error_mark somewhere nested inside, either
8277 stub out the statement or propagate the error back out. */
8278 if (ret == GS_ERROR)
8280 if (is_statement)
8281 *expr_p = NULL;
8282 goto out;
8285 /* This was only valid as a return value from the langhook, which
8286 we handled. Make sure it doesn't escape from any other context. */
8287 gcc_assert (ret != GS_UNHANDLED);
8289 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8291 /* We aren't looking for a value, and we don't have a valid
8292 statement. If it doesn't have side-effects, throw it away. */
8293 if (!TREE_SIDE_EFFECTS (*expr_p))
8294 *expr_p = NULL;
8295 else if (!TREE_THIS_VOLATILE (*expr_p))
8297 /* This is probably a _REF that contains something nested that
8298 has side effects. Recurse through the operands to find it. */
8299 enum tree_code code = TREE_CODE (*expr_p);
8301 switch (code)
8303 case COMPONENT_REF:
8304 case REALPART_EXPR:
8305 case IMAGPART_EXPR:
8306 case VIEW_CONVERT_EXPR:
8307 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8308 gimple_test_f, fallback);
8309 break;
8311 case ARRAY_REF:
8312 case ARRAY_RANGE_REF:
8313 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8314 gimple_test_f, fallback);
8315 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8316 gimple_test_f, fallback);
8317 break;
8319 default:
8320 /* Anything else with side-effects must be converted to
8321 a valid statement before we get here. */
8322 gcc_unreachable ();
8325 *expr_p = NULL;
8327 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8328 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8330 /* Historically, the compiler has treated a bare reference
8331 to a non-BLKmode volatile lvalue as forcing a load. */
8332 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8334 /* Normally, we do not want to create a temporary for a
8335 TREE_ADDRESSABLE type because such a type should not be
8336 copied by bitwise-assignment. However, we make an
8337 exception here, as all we are doing here is ensuring that
8338 we read the bytes that make up the type. We use
8339 create_tmp_var_raw because create_tmp_var will abort when
8340 given a TREE_ADDRESSABLE type. */
8341 tree tmp = create_tmp_var_raw (type, "vol");
8342 gimple_add_tmp_var (tmp);
8343 gimplify_assign (tmp, *expr_p, pre_p);
8344 *expr_p = NULL;
8346 else
8347 /* We can't do anything useful with a volatile reference to
8348 an incomplete type, so just throw it away. Likewise for
8349 a BLKmode type, since any implicit inner load should
8350 already have been turned into an explicit one by the
8351 gimplification process. */
8352 *expr_p = NULL;
8355 /* If we are gimplifying at the statement level, we're done. Tack
8356 everything together and return. */
8357 if (fallback == fb_none || is_statement)
8359 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8360 it out for GC to reclaim it. */
8361 *expr_p = NULL_TREE;
8363 if (!gimple_seq_empty_p (internal_pre)
8364 || !gimple_seq_empty_p (internal_post))
8366 gimplify_seq_add_seq (&internal_pre, internal_post);
8367 gimplify_seq_add_seq (pre_p, internal_pre);
8370 /* The result of gimplifying *EXPR_P is going to be the last few
8371 statements in *PRE_P and *POST_P. Add location information
8372 to all the statements that were added by the gimplification
8373 helpers. */
8374 if (!gimple_seq_empty_p (*pre_p))
8375 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8377 if (!gimple_seq_empty_p (*post_p))
8378 annotate_all_with_location_after (*post_p, post_last_gsi,
8379 input_location);
8381 goto out;
8384 #ifdef ENABLE_GIMPLE_CHECKING
8385 if (*expr_p)
8387 enum tree_code code = TREE_CODE (*expr_p);
8388 /* These expressions should already be in gimple IR form. */
8389 gcc_assert (code != MODIFY_EXPR
8390 && code != ASM_EXPR
8391 && code != BIND_EXPR
8392 && code != CATCH_EXPR
8393 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8394 && code != EH_FILTER_EXPR
8395 && code != GOTO_EXPR
8396 && code != LABEL_EXPR
8397 && code != LOOP_EXPR
8398 && code != SWITCH_EXPR
8399 && code != TRY_FINALLY_EXPR
8400 && code != OMP_CRITICAL
8401 && code != OMP_FOR
8402 && code != OMP_MASTER
8403 && code != OMP_TASKGROUP
8404 && code != OMP_ORDERED
8405 && code != OMP_PARALLEL
8406 && code != OMP_SECTIONS
8407 && code != OMP_SECTION
8408 && code != OMP_SINGLE);
8410 #endif
8412 /* Otherwise we're gimplifying a subexpression, so the resulting
8413 value is interesting. If it's a valid operand that matches
8414 GIMPLE_TEST_F, we're done. Unless we are handling some
8415 post-effects internally; if that's the case, we need to copy into
8416 a temporary before adding the post-effects to POST_P. */
8417 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8418 goto out;
8420 /* Otherwise, we need to create a new temporary for the gimplified
8421 expression. */
8423 /* We can't return an lvalue if we have an internal postqueue. The
8424 object the lvalue refers to would (probably) be modified by the
8425 postqueue; we need to copy the value out first, which means an
8426 rvalue. */
8427 if ((fallback & fb_lvalue)
8428 && gimple_seq_empty_p (internal_post)
8429 && is_gimple_addressable (*expr_p))
8431 /* An lvalue will do. Take the address of the expression, store it
8432 in a temporary, and replace the expression with an INDIRECT_REF of
8433 that temporary. */
8434 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8435 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8436 *expr_p = build_simple_mem_ref (tmp);
8438 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8440 /* An rvalue will do. Assign the gimplified expression into a
8441 new temporary TMP and replace the original expression with
8442 TMP. First, make sure that the expression has a type so that
8443 it can be assigned into a temporary. */
8444 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8445 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8447 else
8449 #ifdef ENABLE_GIMPLE_CHECKING
8450 if (!(fallback & fb_mayfail))
8452 fprintf (stderr, "gimplification failed:\n");
8453 print_generic_expr (stderr, *expr_p, 0);
8454 debug_tree (*expr_p);
8455 internal_error ("gimplification failed");
8457 #endif
8458 gcc_assert (fallback & fb_mayfail);
8460 /* If this is an asm statement, and the user asked for the
8461 impossible, don't die. Fail and let gimplify_asm_expr
8462 issue an error. */
8463 ret = GS_ERROR;
8464 goto out;
8467 /* Make sure the temporary matches our predicate. */
8468 gcc_assert ((*gimple_test_f) (*expr_p));
8470 if (!gimple_seq_empty_p (internal_post))
8472 annotate_all_with_location (internal_post, input_location);
8473 gimplify_seq_add_seq (pre_p, internal_post);
8476 out:
8477 input_location = saved_location;
8478 return ret;
8481 /* Look through TYPE for variable-sized objects and gimplify each such
8482 size that we find. Add to LIST_P any statements generated. */
8484 void
8485 gimplify_type_sizes (tree type, gimple_seq *list_p)
8487 tree field, t;
8489 if (type == NULL || type == error_mark_node)
8490 return;
8492 /* We first do the main variant, then copy into any other variants. */
8493 type = TYPE_MAIN_VARIANT (type);
8495 /* Avoid infinite recursion. */
8496 if (TYPE_SIZES_GIMPLIFIED (type))
8497 return;
8499 TYPE_SIZES_GIMPLIFIED (type) = 1;
8501 switch (TREE_CODE (type))
8503 case INTEGER_TYPE:
8504 case ENUMERAL_TYPE:
8505 case BOOLEAN_TYPE:
8506 case REAL_TYPE:
8507 case FIXED_POINT_TYPE:
8508 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8509 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8511 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8513 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8514 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8516 break;
8518 case ARRAY_TYPE:
8519 /* These types may not have declarations, so handle them here. */
8520 gimplify_type_sizes (TREE_TYPE (type), list_p);
8521 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8522 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8523 with assigned stack slots, for -O1+ -g they should be tracked
8524 by VTA. */
8525 if (!(TYPE_NAME (type)
8526 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8527 && DECL_IGNORED_P (TYPE_NAME (type)))
8528 && TYPE_DOMAIN (type)
8529 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8531 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8532 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8533 DECL_IGNORED_P (t) = 0;
8534 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8535 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8536 DECL_IGNORED_P (t) = 0;
8538 break;
8540 case RECORD_TYPE:
8541 case UNION_TYPE:
8542 case QUAL_UNION_TYPE:
8543 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8544 if (TREE_CODE (field) == FIELD_DECL)
8546 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8547 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8548 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8549 gimplify_type_sizes (TREE_TYPE (field), list_p);
8551 break;
8553 case POINTER_TYPE:
8554 case REFERENCE_TYPE:
8555 /* We used to recurse on the pointed-to type here, which turned out to
8556 be incorrect because its definition might refer to variables not
8557 yet initialized at this point if a forward declaration is involved.
8559 It was actually useful for anonymous pointed-to types to ensure
8560 that the sizes evaluation dominates every possible later use of the
8561 values. Restricting to such types here would be safe since there
8562 is no possible forward declaration around, but would introduce an
8563 undesirable middle-end semantic to anonymity. We then defer to
8564 front-ends the responsibility of ensuring that the sizes are
8565 evaluated both early and late enough, e.g. by attaching artificial
8566 type declarations to the tree. */
8567 break;
8569 default:
8570 break;
8573 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8574 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8576 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8578 TYPE_SIZE (t) = TYPE_SIZE (type);
8579 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8580 TYPE_SIZES_GIMPLIFIED (t) = 1;
8584 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8585 a size or position, has had all of its SAVE_EXPRs evaluated.
8586 We add any required statements to *STMT_P. */
8588 void
8589 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8591 tree expr = *expr_p;
8593 /* We don't do anything if the value isn't there, is constant, or contains
8594 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8595 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8596 will want to replace it with a new variable, but that will cause problems
8597 if this type is from outside the function. It's OK to have that here. */
8598 if (is_gimple_sizepos (expr))
8599 return;
8601 *expr_p = unshare_expr (expr);
8603 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8606 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8607 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8608 is true, also gimplify the parameters. */
8610 gimple
8611 gimplify_body (tree fndecl, bool do_parms)
8613 location_t saved_location = input_location;
8614 gimple_seq parm_stmts, seq;
8615 gimple outer_bind;
8616 struct cgraph_node *cgn;
8618 timevar_push (TV_TREE_GIMPLIFY);
8620 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8621 gimplification. */
8622 default_rtl_profile ();
8624 gcc_assert (gimplify_ctxp == NULL);
8625 push_gimplify_context ();
8627 if (flag_openmp)
8629 gcc_assert (gimplify_omp_ctxp == NULL);
8630 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8631 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8634 /* Unshare most shared trees in the body and in that of any nested functions.
8635 It would seem we don't have to do this for nested functions because
8636 they are supposed to be output and then the outer function gimplified
8637 first, but the g++ front end doesn't always do it that way. */
8638 unshare_body (fndecl);
8639 unvisit_body (fndecl);
8641 cgn = cgraph_get_node (fndecl);
8642 if (cgn && cgn->origin)
8643 nonlocal_vlas = pointer_set_create ();
8645 /* Make sure input_location isn't set to something weird. */
8646 input_location = DECL_SOURCE_LOCATION (fndecl);
8648 /* Resolve callee-copies. This has to be done before processing
8649 the body so that DECL_VALUE_EXPR gets processed correctly. */
8650 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8652 /* Gimplify the function's body. */
8653 seq = NULL;
8654 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8655 outer_bind = gimple_seq_first_stmt (seq);
8656 if (!outer_bind)
8658 outer_bind = gimple_build_nop ();
8659 gimplify_seq_add_stmt (&seq, outer_bind);
8662 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8663 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8664 if (gimple_code (outer_bind) == GIMPLE_BIND
8665 && gimple_seq_first (seq) == gimple_seq_last (seq))
8667 else
8668 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8670 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8672 /* If we had callee-copies statements, insert them at the beginning
8673 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8674 if (!gimple_seq_empty_p (parm_stmts))
8676 tree parm;
8678 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8679 gimple_bind_set_body (outer_bind, parm_stmts);
8681 for (parm = DECL_ARGUMENTS (current_function_decl);
8682 parm; parm = DECL_CHAIN (parm))
8683 if (DECL_HAS_VALUE_EXPR_P (parm))
8685 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8686 DECL_IGNORED_P (parm) = 0;
8690 if (nonlocal_vlas)
8692 if (nonlocal_vla_vars)
8694 /* tree-nested.c may later on call declare_vars (..., true);
8695 which relies on BLOCK_VARS chain to be the tail of the
8696 gimple_bind_vars chain. Ensure we don't violate that
8697 assumption. */
8698 if (gimple_bind_block (outer_bind)
8699 == DECL_INITIAL (current_function_decl))
8700 declare_vars (nonlocal_vla_vars, outer_bind, true);
8701 else
8702 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8703 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8704 nonlocal_vla_vars);
8705 nonlocal_vla_vars = NULL_TREE;
8707 pointer_set_destroy (nonlocal_vlas);
8708 nonlocal_vlas = NULL;
8711 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8713 delete_omp_context (gimplify_omp_ctxp);
8714 gimplify_omp_ctxp = NULL;
8717 pop_gimplify_context (outer_bind);
8718 gcc_assert (gimplify_ctxp == NULL);
8720 #ifdef ENABLE_CHECKING
8721 if (!seen_error ())
8722 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8723 #endif
8725 timevar_pop (TV_TREE_GIMPLIFY);
8726 input_location = saved_location;
8728 return outer_bind;
8731 typedef char *char_p; /* For DEF_VEC_P. */
8733 /* Return whether we should exclude FNDECL from instrumentation. */
8735 bool
8736 flag_instrument_functions_exclude_p (tree fndecl)
8738 vec<char_p> *v;
8740 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8741 if (v && v->length () > 0)
8743 const char *name;
8744 int i;
8745 char *s;
8747 name = lang_hooks.decl_printable_name (fndecl, 0);
8748 FOR_EACH_VEC_ELT (*v, i, s)
8749 if (strstr (name, s) != NULL)
8750 return true;
8753 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8754 if (v && v->length () > 0)
8756 const char *name;
8757 int i;
8758 char *s;
8760 name = DECL_SOURCE_FILE (fndecl);
8761 FOR_EACH_VEC_ELT (*v, i, s)
8762 if (strstr (name, s) != NULL)
8763 return true;
8766 return false;
8769 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8770 node for the function we want to gimplify.
8772 Return the sequence of GIMPLE statements corresponding to the body
8773 of FNDECL. */
8775 void
8776 gimplify_function_tree (tree fndecl)
8778 tree parm, ret;
8779 gimple_seq seq;
8780 gimple bind;
8782 gcc_assert (!gimple_body (fndecl));
8784 if (DECL_STRUCT_FUNCTION (fndecl))
8785 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8786 else
8787 push_struct_function (fndecl);
8789 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8791 /* Preliminarily mark non-addressed complex variables as eligible
8792 for promotion to gimple registers. We'll transform their uses
8793 as we find them. */
8794 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8795 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8796 && !TREE_THIS_VOLATILE (parm)
8797 && !needs_to_live_in_memory (parm))
8798 DECL_GIMPLE_REG_P (parm) = 1;
8801 ret = DECL_RESULT (fndecl);
8802 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8803 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8804 && !needs_to_live_in_memory (ret))
8805 DECL_GIMPLE_REG_P (ret) = 1;
8807 bind = gimplify_body (fndecl, true);
8809 /* The tree body of the function is no longer needed, replace it
8810 with the new GIMPLE body. */
8811 seq = NULL;
8812 gimple_seq_add_stmt (&seq, bind);
8813 gimple_set_body (fndecl, seq);
8815 /* If we're instrumenting function entry/exit, then prepend the call to
8816 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8817 catch the exit hook. */
8818 /* ??? Add some way to ignore exceptions for this TFE. */
8819 if (flag_instrument_function_entry_exit
8820 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8821 && !flag_instrument_functions_exclude_p (fndecl))
8823 tree x;
8824 gimple new_bind;
8825 gimple tf;
8826 gimple_seq cleanup = NULL, body = NULL;
8827 tree tmp_var;
8828 gimple call;
8830 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8831 call = gimple_build_call (x, 1, integer_zero_node);
8832 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8833 gimple_call_set_lhs (call, tmp_var);
8834 gimplify_seq_add_stmt (&cleanup, call);
8835 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8836 call = gimple_build_call (x, 2,
8837 build_fold_addr_expr (current_function_decl),
8838 tmp_var);
8839 gimplify_seq_add_stmt (&cleanup, call);
8840 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8842 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8843 call = gimple_build_call (x, 1, integer_zero_node);
8844 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8845 gimple_call_set_lhs (call, tmp_var);
8846 gimplify_seq_add_stmt (&body, call);
8847 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8848 call = gimple_build_call (x, 2,
8849 build_fold_addr_expr (current_function_decl),
8850 tmp_var);
8851 gimplify_seq_add_stmt (&body, call);
8852 gimplify_seq_add_stmt (&body, tf);
8853 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8854 /* Clear the block for BIND, since it is no longer directly inside
8855 the function, but within a try block. */
8856 gimple_bind_set_block (bind, NULL);
8858 /* Replace the current function body with the body
8859 wrapped in the try/finally TF. */
8860 seq = NULL;
8861 gimple_seq_add_stmt (&seq, new_bind);
8862 gimple_set_body (fndecl, seq);
8865 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8866 cfun->curr_properties = PROP_gimple_any;
8868 pop_cfun ();
8871 /* Return a dummy expression of type TYPE in order to keep going after an
8872 error. */
8874 static tree
8875 dummy_object (tree type)
8877 tree t = build_int_cst (build_pointer_type (type), 0);
8878 return build2 (MEM_REF, type, t, t);
8881 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
8882 builtin function, but a very special sort of operator. */
8884 enum gimplify_status
8885 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8887 tree promoted_type, have_va_type;
8888 tree valist = TREE_OPERAND (*expr_p, 0);
8889 tree type = TREE_TYPE (*expr_p);
8890 tree t;
8891 location_t loc = EXPR_LOCATION (*expr_p);
8893 /* Verify that valist is of the proper type. */
8894 have_va_type = TREE_TYPE (valist);
8895 if (have_va_type == error_mark_node)
8896 return GS_ERROR;
8897 have_va_type = targetm.canonical_va_list_type (have_va_type);
8899 if (have_va_type == NULL_TREE)
8901 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
8902 return GS_ERROR;
8905 /* Generate a diagnostic for requesting data of a type that cannot
8906 be passed through `...' due to type promotion at the call site. */
8907 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
8908 != type)
8910 static bool gave_help;
8911 bool warned;
8913 /* Unfortunately, this is merely undefined, rather than a constraint
8914 violation, so we cannot make this an error. If this call is never
8915 executed, the program is still strictly conforming. */
8916 warned = warning_at (loc, 0,
8917 "%qT is promoted to %qT when passed through %<...%>",
8918 type, promoted_type);
8919 if (!gave_help && warned)
8921 gave_help = true;
8922 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
8923 promoted_type, type);
8926 /* We can, however, treat "undefined" any way we please.
8927 Call abort to encourage the user to fix the program. */
8928 if (warned)
8929 inform (loc, "if this code is reached, the program will abort");
8930 /* Before the abort, allow the evaluation of the va_list
8931 expression to exit or longjmp. */
8932 gimplify_and_add (valist, pre_p);
8933 t = build_call_expr_loc (loc,
8934 builtin_decl_implicit (BUILT_IN_TRAP), 0);
8935 gimplify_and_add (t, pre_p);
8937 /* This is dead code, but go ahead and finish so that the
8938 mode of the result comes out right. */
8939 *expr_p = dummy_object (type);
8940 return GS_ALL_DONE;
8942 else
8944 /* Make it easier for the backends by protecting the valist argument
8945 from multiple evaluations. */
8946 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
8948 /* For this case, the backends will be expecting a pointer to
8949 TREE_TYPE (abi), but it's possible we've
8950 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
8951 So fix it. */
8952 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
8954 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
8955 valist = fold_convert_loc (loc, p1,
8956 build_fold_addr_expr_loc (loc, valist));
8959 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
8961 else
8962 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
8964 if (!targetm.gimplify_va_arg_expr)
8965 /* FIXME: Once most targets are converted we should merely
8966 assert this is non-null. */
8967 return GS_ALL_DONE;
8969 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
8970 return GS_OK;
8974 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
8976 DST/SRC are the destination and source respectively. You can pass
8977 ungimplified trees in DST or SRC, in which case they will be
8978 converted to a gimple operand if necessary.
8980 This function returns the newly created GIMPLE_ASSIGN tuple. */
8982 gimple
8983 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
8985 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
8986 gimplify_and_add (t, seq_p);
8987 ggc_free (t);
8988 return gimple_seq_last_stmt (*seq_p);
8991 inline hashval_t
8992 gimplify_hasher::hash (const value_type *p)
8994 tree t = p->val;
8995 return iterative_hash_expr (t, 0);
8998 inline bool
8999 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9001 tree t1 = p1->val;
9002 tree t2 = p2->val;
9003 enum tree_code code = TREE_CODE (t1);
9005 if (TREE_CODE (t2) != code
9006 || TREE_TYPE (t1) != TREE_TYPE (t2))
9007 return false;
9009 if (!operand_equal_p (t1, t2, 0))
9010 return false;
9012 #ifdef ENABLE_CHECKING
9013 /* Only allow them to compare equal if they also hash equal; otherwise
9014 results are nondeterminate, and we fail bootstrap comparison. */
9015 gcc_assert (hash (p1) == hash (p2));
9016 #endif
9018 return true;