014-05-09 David Wohlferd <LimeGreenSocks@yahoo.com> Andrew Haley...
[official-gcc.git] / gcc / gimplify.c
blobbe4d71900ac04d55bf5282047168a23d7b520534
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_TASK) != 0)
5688 error ("%qE not specified in enclosing task",
5689 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5690 error_at (ctx->location, "enclosing task");
5692 else if (ctx->region_type == ORT_TEAMS)
5694 error ("%qE not specified in enclosing teams construct",
5695 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5696 error_at (ctx->location, "enclosing teams construct");
5698 else
5700 error ("%qE not specified in enclosing parallel",
5701 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5702 error_at (ctx->location, "enclosing parallel");
5704 /* FALLTHRU */
5705 case OMP_CLAUSE_DEFAULT_SHARED:
5706 flags |= GOVD_SHARED;
5707 break;
5708 case OMP_CLAUSE_DEFAULT_PRIVATE:
5709 flags |= GOVD_PRIVATE;
5710 break;
5711 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5712 flags |= GOVD_FIRSTPRIVATE;
5713 break;
5714 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5715 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5716 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5717 if (ctx->outer_context)
5718 omp_notice_variable (ctx->outer_context, decl, in_code);
5719 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5721 splay_tree_node n2;
5723 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5724 continue;
5725 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5726 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5728 flags |= GOVD_FIRSTPRIVATE;
5729 break;
5731 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5732 break;
5734 if (flags & GOVD_FIRSTPRIVATE)
5735 break;
5736 if (octx == NULL
5737 && (TREE_CODE (decl) == PARM_DECL
5738 || (!is_global_var (decl)
5739 && DECL_CONTEXT (decl) == current_function_decl)))
5741 flags |= GOVD_FIRSTPRIVATE;
5742 break;
5744 flags |= GOVD_SHARED;
5745 break;
5746 default:
5747 gcc_unreachable ();
5750 if ((flags & GOVD_PRIVATE)
5751 && lang_hooks.decls.omp_private_outer_ref (decl))
5752 flags |= GOVD_PRIVATE_OUTER_REF;
5754 omp_add_variable (ctx, decl, flags);
5756 shared = (flags & GOVD_SHARED) != 0;
5757 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5758 goto do_outer;
5761 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5762 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5763 && DECL_SIZE (decl)
5764 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5766 splay_tree_node n2;
5767 tree t = DECL_VALUE_EXPR (decl);
5768 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5769 t = TREE_OPERAND (t, 0);
5770 gcc_assert (DECL_P (t));
5771 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5772 n2->value |= GOVD_SEEN;
5775 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5776 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5778 /* If nothing changed, there's nothing left to do. */
5779 if ((n->value & flags) == flags)
5780 return ret;
5781 flags |= n->value;
5782 n->value = flags;
5784 do_outer:
5785 /* If the variable is private in the current context, then we don't
5786 need to propagate anything to an outer context. */
5787 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5788 return ret;
5789 if (ctx->outer_context
5790 && omp_notice_variable (ctx->outer_context, decl, in_code))
5791 return true;
5792 return ret;
5795 /* Verify that DECL is private within CTX. If there's specific information
5796 to the contrary in the innermost scope, generate an error. */
5798 static bool
5799 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5801 splay_tree_node n;
5803 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5804 if (n != NULL)
5806 if (n->value & GOVD_SHARED)
5808 if (ctx == gimplify_omp_ctxp)
5810 if (simd)
5811 error ("iteration variable %qE is predetermined linear",
5812 DECL_NAME (decl));
5813 else
5814 error ("iteration variable %qE should be private",
5815 DECL_NAME (decl));
5816 n->value = GOVD_PRIVATE;
5817 return true;
5819 else
5820 return false;
5822 else if ((n->value & GOVD_EXPLICIT) != 0
5823 && (ctx == gimplify_omp_ctxp
5824 || (ctx->region_type == ORT_COMBINED_PARALLEL
5825 && gimplify_omp_ctxp->outer_context == ctx)))
5827 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5828 error ("iteration variable %qE should not be firstprivate",
5829 DECL_NAME (decl));
5830 else if ((n->value & GOVD_REDUCTION) != 0)
5831 error ("iteration variable %qE should not be reduction",
5832 DECL_NAME (decl));
5833 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5834 error ("iteration variable %qE should not be lastprivate",
5835 DECL_NAME (decl));
5836 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5837 error ("iteration variable %qE should not be private",
5838 DECL_NAME (decl));
5839 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5840 error ("iteration variable %qE is predetermined linear",
5841 DECL_NAME (decl));
5843 return (ctx == gimplify_omp_ctxp
5844 || (ctx->region_type == ORT_COMBINED_PARALLEL
5845 && gimplify_omp_ctxp->outer_context == ctx));
5848 if (ctx->region_type != ORT_WORKSHARE
5849 && ctx->region_type != ORT_SIMD)
5850 return false;
5851 else if (ctx->outer_context)
5852 return omp_is_private (ctx->outer_context, decl, simd);
5853 return false;
5856 /* Return true if DECL is private within a parallel region
5857 that binds to the current construct's context or in parallel
5858 region's REDUCTION clause. */
5860 static bool
5861 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5863 splay_tree_node n;
5867 ctx = ctx->outer_context;
5868 if (ctx == NULL)
5869 return !(is_global_var (decl)
5870 /* References might be private, but might be shared too,
5871 when checking for copyprivate, assume they might be
5872 private, otherwise assume they might be shared. */
5873 || (!copyprivate
5874 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5876 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5877 continue;
5879 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5880 if (n != NULL)
5881 return (n->value & GOVD_SHARED) == 0;
5883 while (ctx->region_type == ORT_WORKSHARE
5884 || ctx->region_type == ORT_SIMD);
5885 return false;
5888 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5889 and previous omp contexts. */
5891 static void
5892 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5893 enum omp_region_type region_type)
5895 struct gimplify_omp_ctx *ctx, *outer_ctx;
5896 tree c;
5898 ctx = new_omp_context (region_type);
5899 outer_ctx = ctx->outer_context;
5901 while ((c = *list_p) != NULL)
5903 bool remove = false;
5904 bool notice_outer = true;
5905 const char *check_non_private = NULL;
5906 unsigned int flags;
5907 tree decl;
5909 switch (OMP_CLAUSE_CODE (c))
5911 case OMP_CLAUSE_PRIVATE:
5912 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5913 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5915 flags |= GOVD_PRIVATE_OUTER_REF;
5916 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5918 else
5919 notice_outer = false;
5920 goto do_add;
5921 case OMP_CLAUSE_SHARED:
5922 flags = GOVD_SHARED | GOVD_EXPLICIT;
5923 goto do_add;
5924 case OMP_CLAUSE_FIRSTPRIVATE:
5925 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5926 check_non_private = "firstprivate";
5927 goto do_add;
5928 case OMP_CLAUSE_LASTPRIVATE:
5929 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5930 check_non_private = "lastprivate";
5931 goto do_add;
5932 case OMP_CLAUSE_REDUCTION:
5933 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5934 check_non_private = "reduction";
5935 goto do_add;
5936 case OMP_CLAUSE_LINEAR:
5937 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5938 is_gimple_val, fb_rvalue) == GS_ERROR)
5940 remove = true;
5941 break;
5943 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5944 goto do_add;
5946 case OMP_CLAUSE_MAP:
5947 if (OMP_CLAUSE_SIZE (c)
5948 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5949 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5951 remove = true;
5952 break;
5954 decl = OMP_CLAUSE_DECL (c);
5955 if (!DECL_P (decl))
5957 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
5958 NULL, is_gimple_lvalue, fb_lvalue)
5959 == GS_ERROR)
5961 remove = true;
5962 break;
5964 break;
5966 flags = GOVD_MAP | GOVD_EXPLICIT;
5967 goto do_add;
5969 case OMP_CLAUSE_DEPEND:
5970 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
5972 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
5973 NULL, is_gimple_val, fb_rvalue);
5974 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
5976 if (error_operand_p (OMP_CLAUSE_DECL (c)))
5978 remove = true;
5979 break;
5981 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
5982 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
5983 is_gimple_val, fb_rvalue) == GS_ERROR)
5985 remove = true;
5986 break;
5988 break;
5990 case OMP_CLAUSE_TO:
5991 case OMP_CLAUSE_FROM:
5992 if (OMP_CLAUSE_SIZE (c)
5993 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5994 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5996 remove = true;
5997 break;
5999 decl = OMP_CLAUSE_DECL (c);
6000 if (error_operand_p (decl))
6002 remove = true;
6003 break;
6005 if (!DECL_P (decl))
6007 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6008 NULL, is_gimple_lvalue, fb_lvalue)
6009 == GS_ERROR)
6011 remove = true;
6012 break;
6014 break;
6016 goto do_notice;
6018 do_add:
6019 decl = OMP_CLAUSE_DECL (c);
6020 if (error_operand_p (decl))
6022 remove = true;
6023 break;
6025 omp_add_variable (ctx, decl, flags);
6026 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6027 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6029 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6030 GOVD_LOCAL | GOVD_SEEN);
6031 gimplify_omp_ctxp = ctx;
6032 push_gimplify_context ();
6034 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6035 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6037 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6038 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6039 pop_gimplify_context
6040 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6041 push_gimplify_context ();
6042 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6043 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6044 pop_gimplify_context
6045 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6046 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6047 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6049 gimplify_omp_ctxp = outer_ctx;
6051 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6052 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6054 gimplify_omp_ctxp = ctx;
6055 push_gimplify_context ();
6056 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6058 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6059 NULL, NULL);
6060 TREE_SIDE_EFFECTS (bind) = 1;
6061 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6062 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6064 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6065 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6066 pop_gimplify_context
6067 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6068 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6070 gimplify_omp_ctxp = outer_ctx;
6072 if (notice_outer)
6073 goto do_notice;
6074 break;
6076 case OMP_CLAUSE_COPYIN:
6077 case OMP_CLAUSE_COPYPRIVATE:
6078 decl = OMP_CLAUSE_DECL (c);
6079 if (error_operand_p (decl))
6081 remove = true;
6082 break;
6084 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6085 && !remove
6086 && !omp_check_private (ctx, decl, true))
6088 remove = true;
6089 if (is_global_var (decl))
6091 if (DECL_THREAD_LOCAL_P (decl))
6092 remove = false;
6093 else if (DECL_HAS_VALUE_EXPR_P (decl))
6095 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6097 if (value
6098 && DECL_P (value)
6099 && DECL_THREAD_LOCAL_P (value))
6100 remove = false;
6103 if (remove)
6104 error_at (OMP_CLAUSE_LOCATION (c),
6105 "copyprivate variable %qE is not threadprivate"
6106 " or private in outer context", DECL_NAME (decl));
6108 do_notice:
6109 if (outer_ctx)
6110 omp_notice_variable (outer_ctx, decl, true);
6111 if (check_non_private
6112 && region_type == ORT_WORKSHARE
6113 && omp_check_private (ctx, decl, false))
6115 error ("%s variable %qE is private in outer context",
6116 check_non_private, DECL_NAME (decl));
6117 remove = true;
6119 break;
6121 case OMP_CLAUSE_FINAL:
6122 case OMP_CLAUSE_IF:
6123 OMP_CLAUSE_OPERAND (c, 0)
6124 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6125 /* Fall through. */
6127 case OMP_CLAUSE_SCHEDULE:
6128 case OMP_CLAUSE_NUM_THREADS:
6129 case OMP_CLAUSE_NUM_TEAMS:
6130 case OMP_CLAUSE_THREAD_LIMIT:
6131 case OMP_CLAUSE_DIST_SCHEDULE:
6132 case OMP_CLAUSE_DEVICE:
6133 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6134 is_gimple_val, fb_rvalue) == GS_ERROR)
6135 remove = true;
6136 break;
6138 case OMP_CLAUSE_NOWAIT:
6139 case OMP_CLAUSE_ORDERED:
6140 case OMP_CLAUSE_UNTIED:
6141 case OMP_CLAUSE_COLLAPSE:
6142 case OMP_CLAUSE_MERGEABLE:
6143 case OMP_CLAUSE_PROC_BIND:
6144 case OMP_CLAUSE_SAFELEN:
6145 break;
6147 case OMP_CLAUSE_ALIGNED:
6148 decl = OMP_CLAUSE_DECL (c);
6149 if (error_operand_p (decl))
6151 remove = true;
6152 break;
6154 if (!is_global_var (decl)
6155 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6156 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6157 break;
6159 case OMP_CLAUSE_DEFAULT:
6160 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6161 break;
6163 default:
6164 gcc_unreachable ();
6167 if (remove)
6168 *list_p = OMP_CLAUSE_CHAIN (c);
6169 else
6170 list_p = &OMP_CLAUSE_CHAIN (c);
6173 gimplify_omp_ctxp = ctx;
6176 /* For all variables that were not actually used within the context,
6177 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6179 static int
6180 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6182 tree *list_p = (tree *) data;
6183 tree decl = (tree) n->key;
6184 unsigned flags = n->value;
6185 enum omp_clause_code code;
6186 tree clause;
6187 bool private_debug;
6189 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6190 return 0;
6191 if ((flags & GOVD_SEEN) == 0)
6192 return 0;
6193 if (flags & GOVD_DEBUG_PRIVATE)
6195 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6196 private_debug = true;
6198 else if (flags & GOVD_MAP)
6199 private_debug = false;
6200 else
6201 private_debug
6202 = lang_hooks.decls.omp_private_debug_clause (decl,
6203 !!(flags & GOVD_SHARED));
6204 if (private_debug)
6205 code = OMP_CLAUSE_PRIVATE;
6206 else if (flags & GOVD_MAP)
6207 code = OMP_CLAUSE_MAP;
6208 else if (flags & GOVD_SHARED)
6210 if (is_global_var (decl))
6212 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6213 while (ctx != NULL)
6215 splay_tree_node on
6216 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6217 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6218 | GOVD_PRIVATE | GOVD_REDUCTION
6219 | GOVD_LINEAR)) != 0)
6220 break;
6221 ctx = ctx->outer_context;
6223 if (ctx == NULL)
6224 return 0;
6226 code = OMP_CLAUSE_SHARED;
6228 else if (flags & GOVD_PRIVATE)
6229 code = OMP_CLAUSE_PRIVATE;
6230 else if (flags & GOVD_FIRSTPRIVATE)
6231 code = OMP_CLAUSE_FIRSTPRIVATE;
6232 else if (flags & GOVD_LASTPRIVATE)
6233 code = OMP_CLAUSE_LASTPRIVATE;
6234 else if (flags & GOVD_ALIGNED)
6235 return 0;
6236 else
6237 gcc_unreachable ();
6239 clause = build_omp_clause (input_location, code);
6240 OMP_CLAUSE_DECL (clause) = decl;
6241 OMP_CLAUSE_CHAIN (clause) = *list_p;
6242 if (private_debug)
6243 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6244 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6245 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6246 else if (code == OMP_CLAUSE_MAP)
6248 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6249 ? OMP_CLAUSE_MAP_TO
6250 : OMP_CLAUSE_MAP_TOFROM;
6251 if (DECL_SIZE (decl)
6252 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6254 tree decl2 = DECL_VALUE_EXPR (decl);
6255 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6256 decl2 = TREE_OPERAND (decl2, 0);
6257 gcc_assert (DECL_P (decl2));
6258 tree mem = build_simple_mem_ref (decl2);
6259 OMP_CLAUSE_DECL (clause) = mem;
6260 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6261 if (gimplify_omp_ctxp->outer_context)
6263 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6264 omp_notice_variable (ctx, decl2, true);
6265 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6267 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6268 OMP_CLAUSE_MAP);
6269 OMP_CLAUSE_DECL (nc) = decl;
6270 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6271 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6272 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6273 OMP_CLAUSE_CHAIN (clause) = nc;
6276 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6278 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6279 OMP_CLAUSE_DECL (nc) = decl;
6280 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6281 OMP_CLAUSE_CHAIN (nc) = *list_p;
6282 OMP_CLAUSE_CHAIN (clause) = nc;
6283 lang_hooks.decls.omp_finish_clause (nc);
6285 *list_p = clause;
6286 lang_hooks.decls.omp_finish_clause (clause);
6287 return 0;
6290 static void
6291 gimplify_adjust_omp_clauses (tree *list_p)
6293 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6294 tree c, decl;
6296 while ((c = *list_p) != NULL)
6298 splay_tree_node n;
6299 bool remove = false;
6301 switch (OMP_CLAUSE_CODE (c))
6303 case OMP_CLAUSE_PRIVATE:
6304 case OMP_CLAUSE_SHARED:
6305 case OMP_CLAUSE_FIRSTPRIVATE:
6306 case OMP_CLAUSE_LINEAR:
6307 decl = OMP_CLAUSE_DECL (c);
6308 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6309 remove = !(n->value & GOVD_SEEN);
6310 if (! remove)
6312 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6313 if ((n->value & GOVD_DEBUG_PRIVATE)
6314 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6316 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6317 || ((n->value & GOVD_DATA_SHARE_CLASS)
6318 == GOVD_PRIVATE));
6319 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6320 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6322 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6323 && ctx->outer_context
6324 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6325 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6327 if (ctx->outer_context->combined_loop
6328 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6330 n = splay_tree_lookup (ctx->outer_context->variables,
6331 (splay_tree_key) decl);
6332 if (n == NULL
6333 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6335 int flags = GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE;
6336 if (n == NULL)
6337 omp_add_variable (ctx->outer_context, decl,
6338 flags | GOVD_SEEN);
6339 else
6340 n->value |= flags | GOVD_SEEN;
6343 else if (!is_global_var (decl))
6344 omp_notice_variable (ctx->outer_context, decl, true);
6347 break;
6349 case OMP_CLAUSE_LASTPRIVATE:
6350 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6351 accurately reflect the presence of a FIRSTPRIVATE clause. */
6352 decl = OMP_CLAUSE_DECL (c);
6353 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6354 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6355 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6356 break;
6358 case OMP_CLAUSE_ALIGNED:
6359 decl = OMP_CLAUSE_DECL (c);
6360 if (!is_global_var (decl))
6362 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6363 remove = n == NULL || !(n->value & GOVD_SEEN);
6364 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6366 struct gimplify_omp_ctx *octx;
6367 if (n != NULL
6368 && (n->value & (GOVD_DATA_SHARE_CLASS
6369 & ~GOVD_FIRSTPRIVATE)))
6370 remove = true;
6371 else
6372 for (octx = ctx->outer_context; octx;
6373 octx = octx->outer_context)
6375 n = splay_tree_lookup (octx->variables,
6376 (splay_tree_key) decl);
6377 if (n == NULL)
6378 continue;
6379 if (n->value & GOVD_LOCAL)
6380 break;
6381 /* We have to avoid assigning a shared variable
6382 to itself when trying to add
6383 __builtin_assume_aligned. */
6384 if (n->value & GOVD_SHARED)
6386 remove = true;
6387 break;
6392 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6394 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6395 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6396 remove = true;
6398 break;
6400 case OMP_CLAUSE_MAP:
6401 decl = OMP_CLAUSE_DECL (c);
6402 if (!DECL_P (decl))
6403 break;
6404 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6405 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6406 remove = true;
6407 else if (DECL_SIZE (decl)
6408 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6409 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6411 tree decl2 = DECL_VALUE_EXPR (decl);
6412 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6413 decl2 = TREE_OPERAND (decl2, 0);
6414 gcc_assert (DECL_P (decl2));
6415 tree mem = build_simple_mem_ref (decl2);
6416 OMP_CLAUSE_DECL (c) = mem;
6417 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6418 if (ctx->outer_context)
6420 omp_notice_variable (ctx->outer_context, decl2, true);
6421 omp_notice_variable (ctx->outer_context,
6422 OMP_CLAUSE_SIZE (c), true);
6424 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6425 OMP_CLAUSE_MAP);
6426 OMP_CLAUSE_DECL (nc) = decl;
6427 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6428 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6429 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6430 OMP_CLAUSE_CHAIN (c) = nc;
6431 c = nc;
6433 break;
6435 case OMP_CLAUSE_TO:
6436 case OMP_CLAUSE_FROM:
6437 decl = OMP_CLAUSE_DECL (c);
6438 if (!DECL_P (decl))
6439 break;
6440 if (DECL_SIZE (decl)
6441 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6443 tree decl2 = DECL_VALUE_EXPR (decl);
6444 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6445 decl2 = TREE_OPERAND (decl2, 0);
6446 gcc_assert (DECL_P (decl2));
6447 tree mem = build_simple_mem_ref (decl2);
6448 OMP_CLAUSE_DECL (c) = mem;
6449 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6450 if (ctx->outer_context)
6452 omp_notice_variable (ctx->outer_context, decl2, true);
6453 omp_notice_variable (ctx->outer_context,
6454 OMP_CLAUSE_SIZE (c), true);
6457 break;
6459 case OMP_CLAUSE_REDUCTION:
6460 case OMP_CLAUSE_COPYIN:
6461 case OMP_CLAUSE_COPYPRIVATE:
6462 case OMP_CLAUSE_IF:
6463 case OMP_CLAUSE_NUM_THREADS:
6464 case OMP_CLAUSE_NUM_TEAMS:
6465 case OMP_CLAUSE_THREAD_LIMIT:
6466 case OMP_CLAUSE_DIST_SCHEDULE:
6467 case OMP_CLAUSE_DEVICE:
6468 case OMP_CLAUSE_SCHEDULE:
6469 case OMP_CLAUSE_NOWAIT:
6470 case OMP_CLAUSE_ORDERED:
6471 case OMP_CLAUSE_DEFAULT:
6472 case OMP_CLAUSE_UNTIED:
6473 case OMP_CLAUSE_COLLAPSE:
6474 case OMP_CLAUSE_FINAL:
6475 case OMP_CLAUSE_MERGEABLE:
6476 case OMP_CLAUSE_PROC_BIND:
6477 case OMP_CLAUSE_SAFELEN:
6478 case OMP_CLAUSE_DEPEND:
6479 break;
6481 default:
6482 gcc_unreachable ();
6485 if (remove)
6486 *list_p = OMP_CLAUSE_CHAIN (c);
6487 else
6488 list_p = &OMP_CLAUSE_CHAIN (c);
6491 /* Add in any implicit data sharing. */
6492 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6494 gimplify_omp_ctxp = ctx->outer_context;
6495 delete_omp_context (ctx);
6498 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6499 gimplification of the body, as well as scanning the body for used
6500 variables. We need to do this scan now, because variable-sized
6501 decls will be decomposed during gimplification. */
6503 static void
6504 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6506 tree expr = *expr_p;
6507 gimple g;
6508 gimple_seq body = NULL;
6510 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6511 OMP_PARALLEL_COMBINED (expr)
6512 ? ORT_COMBINED_PARALLEL
6513 : ORT_PARALLEL);
6515 push_gimplify_context ();
6517 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6518 if (gimple_code (g) == GIMPLE_BIND)
6519 pop_gimplify_context (g);
6520 else
6521 pop_gimplify_context (NULL);
6523 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6525 g = gimple_build_omp_parallel (body,
6526 OMP_PARALLEL_CLAUSES (expr),
6527 NULL_TREE, NULL_TREE);
6528 if (OMP_PARALLEL_COMBINED (expr))
6529 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6530 gimplify_seq_add_stmt (pre_p, g);
6531 *expr_p = NULL_TREE;
6534 /* Gimplify the contents of an OMP_TASK statement. This involves
6535 gimplification of the body, as well as scanning the body for used
6536 variables. We need to do this scan now, because variable-sized
6537 decls will be decomposed during gimplification. */
6539 static void
6540 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6542 tree expr = *expr_p;
6543 gimple g;
6544 gimple_seq body = NULL;
6546 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6547 find_omp_clause (OMP_TASK_CLAUSES (expr),
6548 OMP_CLAUSE_UNTIED)
6549 ? ORT_UNTIED_TASK : ORT_TASK);
6551 push_gimplify_context ();
6553 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6554 if (gimple_code (g) == GIMPLE_BIND)
6555 pop_gimplify_context (g);
6556 else
6557 pop_gimplify_context (NULL);
6559 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6561 g = gimple_build_omp_task (body,
6562 OMP_TASK_CLAUSES (expr),
6563 NULL_TREE, NULL_TREE,
6564 NULL_TREE, NULL_TREE, NULL_TREE);
6565 gimplify_seq_add_stmt (pre_p, g);
6566 *expr_p = NULL_TREE;
6569 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6570 with non-NULL OMP_FOR_INIT. */
6572 static tree
6573 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6575 *walk_subtrees = 0;
6576 switch (TREE_CODE (*tp))
6578 case OMP_FOR:
6579 *walk_subtrees = 1;
6580 /* FALLTHRU */
6581 case OMP_SIMD:
6582 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6583 return *tp;
6584 break;
6585 case BIND_EXPR:
6586 case STATEMENT_LIST:
6587 case OMP_PARALLEL:
6588 *walk_subtrees = 1;
6589 break;
6590 default:
6591 break;
6593 return NULL_TREE;
6596 /* Gimplify the gross structure of an OMP_FOR statement. */
6598 static enum gimplify_status
6599 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6601 tree for_stmt, orig_for_stmt, decl, var, t;
6602 enum gimplify_status ret = GS_ALL_DONE;
6603 enum gimplify_status tret;
6604 gimple gfor;
6605 gimple_seq for_body, for_pre_body;
6606 int i;
6607 bool simd;
6608 bitmap has_decl_expr = NULL;
6610 orig_for_stmt = for_stmt = *expr_p;
6612 simd = (TREE_CODE (for_stmt) == OMP_SIMD
6613 || TREE_CODE (for_stmt) == CILK_SIMD);
6614 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6615 simd ? ORT_SIMD : ORT_WORKSHARE);
6617 /* Handle OMP_FOR_INIT. */
6618 for_pre_body = NULL;
6619 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6621 has_decl_expr = BITMAP_ALLOC (NULL);
6622 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6623 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6624 == VAR_DECL)
6626 t = OMP_FOR_PRE_BODY (for_stmt);
6627 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6629 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6631 tree_stmt_iterator si;
6632 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6633 tsi_next (&si))
6635 t = tsi_stmt (si);
6636 if (TREE_CODE (t) == DECL_EXPR
6637 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6638 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6642 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6643 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6645 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6647 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6648 NULL, NULL);
6649 gcc_assert (for_stmt != NULL_TREE);
6650 gimplify_omp_ctxp->combined_loop = true;
6653 for_body = NULL;
6654 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6655 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6656 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6657 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6658 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6660 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6661 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6662 decl = TREE_OPERAND (t, 0);
6663 gcc_assert (DECL_P (decl));
6664 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6665 || POINTER_TYPE_P (TREE_TYPE (decl)));
6667 /* Make sure the iteration variable is private. */
6668 tree c = NULL_TREE;
6669 tree c2 = NULL_TREE;
6670 if (orig_for_stmt != for_stmt)
6671 /* Do this only on innermost construct for combined ones. */;
6672 else if (simd)
6674 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6675 (splay_tree_key)decl);
6676 omp_is_private (gimplify_omp_ctxp, decl,
6677 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6678 != 1));
6679 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6680 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6681 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6683 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6684 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6685 if (has_decl_expr
6686 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6687 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6688 OMP_CLAUSE_DECL (c) = decl;
6689 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6690 OMP_FOR_CLAUSES (for_stmt) = c;
6691 omp_add_variable (gimplify_omp_ctxp, decl,
6692 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6694 else
6696 bool lastprivate
6697 = (!has_decl_expr
6698 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6699 c = build_omp_clause (input_location,
6700 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6701 : OMP_CLAUSE_PRIVATE);
6702 OMP_CLAUSE_DECL (c) = decl;
6703 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6704 OMP_FOR_CLAUSES (for_stmt) = c;
6705 omp_add_variable (gimplify_omp_ctxp, decl,
6706 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6707 | GOVD_EXPLICIT | GOVD_SEEN);
6708 c = NULL_TREE;
6711 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6712 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6713 else
6714 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6716 /* If DECL is not a gimple register, create a temporary variable to act
6717 as an iteration counter. This is valid, since DECL cannot be
6718 modified in the body of the loop. */
6719 if (orig_for_stmt != for_stmt)
6720 var = decl;
6721 else if (!is_gimple_reg (decl))
6723 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6724 TREE_OPERAND (t, 0) = var;
6726 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6728 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6730 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6731 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
6732 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
6733 OMP_CLAUSE_DECL (c2) = var;
6734 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
6735 OMP_FOR_CLAUSES (for_stmt) = c2;
6736 omp_add_variable (gimplify_omp_ctxp, var,
6737 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6738 if (c == NULL_TREE)
6740 c = c2;
6741 c2 = NULL_TREE;
6744 else
6745 omp_add_variable (gimplify_omp_ctxp, var,
6746 GOVD_PRIVATE | GOVD_SEEN);
6748 else
6749 var = decl;
6751 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6752 is_gimple_val, fb_rvalue);
6753 ret = MIN (ret, tret);
6754 if (ret == GS_ERROR)
6755 return ret;
6757 /* Handle OMP_FOR_COND. */
6758 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6759 gcc_assert (COMPARISON_CLASS_P (t));
6760 gcc_assert (TREE_OPERAND (t, 0) == decl);
6762 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6763 is_gimple_val, fb_rvalue);
6764 ret = MIN (ret, tret);
6766 /* Handle OMP_FOR_INCR. */
6767 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6768 switch (TREE_CODE (t))
6770 case PREINCREMENT_EXPR:
6771 case POSTINCREMENT_EXPR:
6773 tree decl = TREE_OPERAND (t, 0);
6774 // c_omp_for_incr_canonicalize_ptr() should have been
6775 // called to massage things appropriately.
6776 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6778 if (orig_for_stmt != for_stmt)
6779 break;
6780 t = build_int_cst (TREE_TYPE (decl), 1);
6781 if (c)
6782 OMP_CLAUSE_LINEAR_STEP (c) = t;
6783 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6784 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6785 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6786 break;
6789 case PREDECREMENT_EXPR:
6790 case POSTDECREMENT_EXPR:
6791 if (orig_for_stmt != for_stmt)
6792 break;
6793 t = build_int_cst (TREE_TYPE (decl), -1);
6794 if (c)
6795 OMP_CLAUSE_LINEAR_STEP (c) = t;
6796 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6797 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6798 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6799 break;
6801 case MODIFY_EXPR:
6802 gcc_assert (TREE_OPERAND (t, 0) == decl);
6803 TREE_OPERAND (t, 0) = var;
6805 t = TREE_OPERAND (t, 1);
6806 switch (TREE_CODE (t))
6808 case PLUS_EXPR:
6809 if (TREE_OPERAND (t, 1) == decl)
6811 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6812 TREE_OPERAND (t, 0) = var;
6813 break;
6816 /* Fallthru. */
6817 case MINUS_EXPR:
6818 case POINTER_PLUS_EXPR:
6819 gcc_assert (TREE_OPERAND (t, 0) == decl);
6820 TREE_OPERAND (t, 0) = var;
6821 break;
6822 default:
6823 gcc_unreachable ();
6826 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6827 is_gimple_val, fb_rvalue);
6828 ret = MIN (ret, tret);
6829 if (c)
6831 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
6832 if (TREE_CODE (t) == MINUS_EXPR)
6834 t = TREE_OPERAND (t, 1);
6835 OMP_CLAUSE_LINEAR_STEP (c)
6836 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
6837 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6838 &for_pre_body, NULL,
6839 is_gimple_val, fb_rvalue);
6840 ret = MIN (ret, tret);
6843 break;
6845 default:
6846 gcc_unreachable ();
6849 if (c2)
6851 gcc_assert (c);
6852 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
6855 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6856 && orig_for_stmt == for_stmt)
6858 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6859 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6860 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6861 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6862 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
6863 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
6864 && OMP_CLAUSE_DECL (c) == decl)
6866 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6867 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6868 gcc_assert (TREE_OPERAND (t, 0) == var);
6869 t = TREE_OPERAND (t, 1);
6870 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6871 || TREE_CODE (t) == MINUS_EXPR
6872 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6873 gcc_assert (TREE_OPERAND (t, 0) == var);
6874 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6875 TREE_OPERAND (t, 1));
6876 gimple_seq *seq;
6877 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
6878 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
6879 else
6880 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
6881 gimplify_assign (decl, t, seq);
6886 BITMAP_FREE (has_decl_expr);
6888 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
6890 if (orig_for_stmt != for_stmt)
6891 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6893 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6894 decl = TREE_OPERAND (t, 0);
6895 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6896 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6897 TREE_OPERAND (t, 0) = var;
6898 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6899 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
6900 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
6903 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
6905 int kind;
6906 switch (TREE_CODE (orig_for_stmt))
6908 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
6909 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
6910 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
6911 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
6912 default:
6913 gcc_unreachable ();
6915 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
6916 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6917 for_pre_body);
6918 if (orig_for_stmt != for_stmt)
6919 gimple_omp_for_set_combined_p (gfor, true);
6920 if (gimplify_omp_ctxp
6921 && (gimplify_omp_ctxp->combined_loop
6922 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
6923 && gimplify_omp_ctxp->outer_context
6924 && gimplify_omp_ctxp->outer_context->combined_loop)))
6926 gimple_omp_for_set_combined_into_p (gfor, true);
6927 if (gimplify_omp_ctxp->combined_loop)
6928 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
6929 else
6930 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
6933 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6935 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6936 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6937 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6938 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6939 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6940 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6941 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6942 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6945 gimplify_seq_add_stmt (pre_p, gfor);
6946 if (ret != GS_ALL_DONE)
6947 return GS_ERROR;
6948 *expr_p = NULL_TREE;
6949 return GS_ALL_DONE;
6952 /* Gimplify the gross structure of other OpenMP constructs.
6953 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
6954 and OMP_TEAMS. */
6956 static void
6957 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6959 tree expr = *expr_p;
6960 gimple stmt;
6961 gimple_seq body = NULL;
6962 enum omp_region_type ort = ORT_WORKSHARE;
6964 switch (TREE_CODE (expr))
6966 case OMP_SECTIONS:
6967 case OMP_SINGLE:
6968 break;
6969 case OMP_TARGET:
6970 ort = ORT_TARGET;
6971 break;
6972 case OMP_TARGET_DATA:
6973 ort = ORT_TARGET_DATA;
6974 break;
6975 case OMP_TEAMS:
6976 ort = ORT_TEAMS;
6977 break;
6978 default:
6979 gcc_unreachable ();
6981 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
6982 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
6984 push_gimplify_context ();
6985 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
6986 if (gimple_code (g) == GIMPLE_BIND)
6987 pop_gimplify_context (g);
6988 else
6989 pop_gimplify_context (NULL);
6990 if (ort == ORT_TARGET_DATA)
6992 gimple_seq cleanup = NULL;
6993 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
6994 g = gimple_build_call (fn, 0);
6995 gimple_seq_add_stmt (&cleanup, g);
6996 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
6997 body = NULL;
6998 gimple_seq_add_stmt (&body, g);
7001 else
7002 gimplify_and_add (OMP_BODY (expr), &body);
7003 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
7005 switch (TREE_CODE (expr))
7007 case OMP_SECTIONS:
7008 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7009 break;
7010 case OMP_SINGLE:
7011 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7012 break;
7013 case OMP_TARGET:
7014 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7015 OMP_CLAUSES (expr));
7016 break;
7017 case OMP_TARGET_DATA:
7018 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7019 OMP_CLAUSES (expr));
7020 break;
7021 case OMP_TEAMS:
7022 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7023 break;
7024 default:
7025 gcc_unreachable ();
7028 gimplify_seq_add_stmt (pre_p, stmt);
7029 *expr_p = NULL_TREE;
7032 /* Gimplify the gross structure of OpenMP target update construct. */
7034 static void
7035 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7037 tree expr = *expr_p;
7038 gimple stmt;
7040 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7041 ORT_WORKSHARE);
7042 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
7043 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7044 OMP_TARGET_UPDATE_CLAUSES (expr));
7046 gimplify_seq_add_stmt (pre_p, stmt);
7047 *expr_p = NULL_TREE;
7050 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7051 stabilized the lhs of the atomic operation as *ADDR. Return true if
7052 EXPR is this stabilized form. */
7054 static bool
7055 goa_lhs_expr_p (tree expr, tree addr)
7057 /* Also include casts to other type variants. The C front end is fond
7058 of adding these for e.g. volatile variables. This is like
7059 STRIP_TYPE_NOPS but includes the main variant lookup. */
7060 STRIP_USELESS_TYPE_CONVERSION (expr);
7062 if (TREE_CODE (expr) == INDIRECT_REF)
7064 expr = TREE_OPERAND (expr, 0);
7065 while (expr != addr
7066 && (CONVERT_EXPR_P (expr)
7067 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7068 && TREE_CODE (expr) == TREE_CODE (addr)
7069 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7071 expr = TREE_OPERAND (expr, 0);
7072 addr = TREE_OPERAND (addr, 0);
7074 if (expr == addr)
7075 return true;
7076 return (TREE_CODE (addr) == ADDR_EXPR
7077 && TREE_CODE (expr) == ADDR_EXPR
7078 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7080 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7081 return true;
7082 return false;
7085 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7086 expression does not involve the lhs, evaluate it into a temporary.
7087 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7088 or -1 if an error was encountered. */
7090 static int
7091 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7092 tree lhs_var)
7094 tree expr = *expr_p;
7095 int saw_lhs;
7097 if (goa_lhs_expr_p (expr, lhs_addr))
7099 *expr_p = lhs_var;
7100 return 1;
7102 if (is_gimple_val (expr))
7103 return 0;
7105 saw_lhs = 0;
7106 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7108 case tcc_binary:
7109 case tcc_comparison:
7110 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7111 lhs_var);
7112 case tcc_unary:
7113 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7114 lhs_var);
7115 break;
7116 case tcc_expression:
7117 switch (TREE_CODE (expr))
7119 case TRUTH_ANDIF_EXPR:
7120 case TRUTH_ORIF_EXPR:
7121 case TRUTH_AND_EXPR:
7122 case TRUTH_OR_EXPR:
7123 case TRUTH_XOR_EXPR:
7124 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7125 lhs_addr, lhs_var);
7126 case TRUTH_NOT_EXPR:
7127 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7128 lhs_addr, lhs_var);
7129 break;
7130 case COMPOUND_EXPR:
7131 /* Break out any preevaluations from cp_build_modify_expr. */
7132 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7133 expr = TREE_OPERAND (expr, 1))
7134 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7135 *expr_p = expr;
7136 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7137 default:
7138 break;
7140 break;
7141 default:
7142 break;
7145 if (saw_lhs == 0)
7147 enum gimplify_status gs;
7148 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7149 if (gs != GS_ALL_DONE)
7150 saw_lhs = -1;
7153 return saw_lhs;
7156 /* Gimplify an OMP_ATOMIC statement. */
7158 static enum gimplify_status
7159 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7161 tree addr = TREE_OPERAND (*expr_p, 0);
7162 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7163 ? NULL : TREE_OPERAND (*expr_p, 1);
7164 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7165 tree tmp_load;
7166 gimple loadstmt, storestmt;
7168 tmp_load = create_tmp_reg (type, NULL);
7169 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7170 return GS_ERROR;
7172 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7173 != GS_ALL_DONE)
7174 return GS_ERROR;
7176 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7177 gimplify_seq_add_stmt (pre_p, loadstmt);
7178 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7179 != GS_ALL_DONE)
7180 return GS_ERROR;
7182 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7183 rhs = tmp_load;
7184 storestmt = gimple_build_omp_atomic_store (rhs);
7185 gimplify_seq_add_stmt (pre_p, storestmt);
7186 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7188 gimple_omp_atomic_set_seq_cst (loadstmt);
7189 gimple_omp_atomic_set_seq_cst (storestmt);
7191 switch (TREE_CODE (*expr_p))
7193 case OMP_ATOMIC_READ:
7194 case OMP_ATOMIC_CAPTURE_OLD:
7195 *expr_p = tmp_load;
7196 gimple_omp_atomic_set_need_value (loadstmt);
7197 break;
7198 case OMP_ATOMIC_CAPTURE_NEW:
7199 *expr_p = rhs;
7200 gimple_omp_atomic_set_need_value (storestmt);
7201 break;
7202 default:
7203 *expr_p = NULL;
7204 break;
7207 return GS_ALL_DONE;
7210 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7211 body, and adding some EH bits. */
7213 static enum gimplify_status
7214 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7216 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7217 gimple g;
7218 gimple_seq body = NULL;
7219 int subcode = 0;
7221 /* Wrap the transaction body in a BIND_EXPR so we have a context
7222 where to put decls for OpenMP. */
7223 if (TREE_CODE (tbody) != BIND_EXPR)
7225 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7226 TREE_SIDE_EFFECTS (bind) = 1;
7227 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7228 TRANSACTION_EXPR_BODY (expr) = bind;
7231 push_gimplify_context ();
7232 temp = voidify_wrapper_expr (*expr_p, NULL);
7234 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7235 pop_gimplify_context (g);
7237 g = gimple_build_transaction (body, NULL);
7238 if (TRANSACTION_EXPR_OUTER (expr))
7239 subcode = GTMA_IS_OUTER;
7240 else if (TRANSACTION_EXPR_RELAXED (expr))
7241 subcode = GTMA_IS_RELAXED;
7242 gimple_transaction_set_subcode (g, subcode);
7244 gimplify_seq_add_stmt (pre_p, g);
7246 if (temp)
7248 *expr_p = temp;
7249 return GS_OK;
7252 *expr_p = NULL_TREE;
7253 return GS_ALL_DONE;
7256 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7257 expression produces a value to be used as an operand inside a GIMPLE
7258 statement, the value will be stored back in *EXPR_P. This value will
7259 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7260 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7261 emitted in PRE_P and POST_P.
7263 Additionally, this process may overwrite parts of the input
7264 expression during gimplification. Ideally, it should be
7265 possible to do non-destructive gimplification.
7267 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7268 the expression needs to evaluate to a value to be used as
7269 an operand in a GIMPLE statement, this value will be stored in
7270 *EXPR_P on exit. This happens when the caller specifies one
7271 of fb_lvalue or fb_rvalue fallback flags.
7273 PRE_P will contain the sequence of GIMPLE statements corresponding
7274 to the evaluation of EXPR and all the side-effects that must
7275 be executed before the main expression. On exit, the last
7276 statement of PRE_P is the core statement being gimplified. For
7277 instance, when gimplifying 'if (++a)' the last statement in
7278 PRE_P will be 'if (t.1)' where t.1 is the result of
7279 pre-incrementing 'a'.
7281 POST_P will contain the sequence of GIMPLE statements corresponding
7282 to the evaluation of all the side-effects that must be executed
7283 after the main expression. If this is NULL, the post
7284 side-effects are stored at the end of PRE_P.
7286 The reason why the output is split in two is to handle post
7287 side-effects explicitly. In some cases, an expression may have
7288 inner and outer post side-effects which need to be emitted in
7289 an order different from the one given by the recursive
7290 traversal. For instance, for the expression (*p--)++ the post
7291 side-effects of '--' must actually occur *after* the post
7292 side-effects of '++'. However, gimplification will first visit
7293 the inner expression, so if a separate POST sequence was not
7294 used, the resulting sequence would be:
7296 1 t.1 = *p
7297 2 p = p - 1
7298 3 t.2 = t.1 + 1
7299 4 *p = t.2
7301 However, the post-decrement operation in line #2 must not be
7302 evaluated until after the store to *p at line #4, so the
7303 correct sequence should be:
7305 1 t.1 = *p
7306 2 t.2 = t.1 + 1
7307 3 *p = t.2
7308 4 p = p - 1
7310 So, by specifying a separate post queue, it is possible
7311 to emit the post side-effects in the correct order.
7312 If POST_P is NULL, an internal queue will be used. Before
7313 returning to the caller, the sequence POST_P is appended to
7314 the main output sequence PRE_P.
7316 GIMPLE_TEST_F points to a function that takes a tree T and
7317 returns nonzero if T is in the GIMPLE form requested by the
7318 caller. The GIMPLE predicates are in gimple.c.
7320 FALLBACK tells the function what sort of a temporary we want if
7321 gimplification cannot produce an expression that complies with
7322 GIMPLE_TEST_F.
7324 fb_none means that no temporary should be generated
7325 fb_rvalue means that an rvalue is OK to generate
7326 fb_lvalue means that an lvalue is OK to generate
7327 fb_either means that either is OK, but an lvalue is preferable.
7328 fb_mayfail means that gimplification may fail (in which case
7329 GS_ERROR will be returned)
7331 The return value is either GS_ERROR or GS_ALL_DONE, since this
7332 function iterates until EXPR is completely gimplified or an error
7333 occurs. */
7335 enum gimplify_status
7336 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7337 bool (*gimple_test_f) (tree), fallback_t fallback)
7339 tree tmp;
7340 gimple_seq internal_pre = NULL;
7341 gimple_seq internal_post = NULL;
7342 tree save_expr;
7343 bool is_statement;
7344 location_t saved_location;
7345 enum gimplify_status ret;
7346 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7348 save_expr = *expr_p;
7349 if (save_expr == NULL_TREE)
7350 return GS_ALL_DONE;
7352 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7353 is_statement = gimple_test_f == is_gimple_stmt;
7354 if (is_statement)
7355 gcc_assert (pre_p);
7357 /* Consistency checks. */
7358 if (gimple_test_f == is_gimple_reg)
7359 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7360 else if (gimple_test_f == is_gimple_val
7361 || gimple_test_f == is_gimple_call_addr
7362 || gimple_test_f == is_gimple_condexpr
7363 || gimple_test_f == is_gimple_mem_rhs
7364 || gimple_test_f == is_gimple_mem_rhs_or_call
7365 || gimple_test_f == is_gimple_reg_rhs
7366 || gimple_test_f == is_gimple_reg_rhs_or_call
7367 || gimple_test_f == is_gimple_asm_val
7368 || gimple_test_f == is_gimple_mem_ref_addr)
7369 gcc_assert (fallback & fb_rvalue);
7370 else if (gimple_test_f == is_gimple_min_lval
7371 || gimple_test_f == is_gimple_lvalue)
7372 gcc_assert (fallback & fb_lvalue);
7373 else if (gimple_test_f == is_gimple_addressable)
7374 gcc_assert (fallback & fb_either);
7375 else if (gimple_test_f == is_gimple_stmt)
7376 gcc_assert (fallback == fb_none);
7377 else
7379 /* We should have recognized the GIMPLE_TEST_F predicate to
7380 know what kind of fallback to use in case a temporary is
7381 needed to hold the value or address of *EXPR_P. */
7382 gcc_unreachable ();
7385 /* We used to check the predicate here and return immediately if it
7386 succeeds. This is wrong; the design is for gimplification to be
7387 idempotent, and for the predicates to only test for valid forms, not
7388 whether they are fully simplified. */
7389 if (pre_p == NULL)
7390 pre_p = &internal_pre;
7392 if (post_p == NULL)
7393 post_p = &internal_post;
7395 /* Remember the last statements added to PRE_P and POST_P. Every
7396 new statement added by the gimplification helpers needs to be
7397 annotated with location information. To centralize the
7398 responsibility, we remember the last statement that had been
7399 added to both queues before gimplifying *EXPR_P. If
7400 gimplification produces new statements in PRE_P and POST_P, those
7401 statements will be annotated with the same location information
7402 as *EXPR_P. */
7403 pre_last_gsi = gsi_last (*pre_p);
7404 post_last_gsi = gsi_last (*post_p);
7406 saved_location = input_location;
7407 if (save_expr != error_mark_node
7408 && EXPR_HAS_LOCATION (*expr_p))
7409 input_location = EXPR_LOCATION (*expr_p);
7411 /* Loop over the specific gimplifiers until the toplevel node
7412 remains the same. */
7415 /* Strip away as many useless type conversions as possible
7416 at the toplevel. */
7417 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7419 /* Remember the expr. */
7420 save_expr = *expr_p;
7422 /* Die, die, die, my darling. */
7423 if (save_expr == error_mark_node
7424 || (TREE_TYPE (save_expr)
7425 && TREE_TYPE (save_expr) == error_mark_node))
7427 ret = GS_ERROR;
7428 break;
7431 /* Do any language-specific gimplification. */
7432 ret = ((enum gimplify_status)
7433 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7434 if (ret == GS_OK)
7436 if (*expr_p == NULL_TREE)
7437 break;
7438 if (*expr_p != save_expr)
7439 continue;
7441 else if (ret != GS_UNHANDLED)
7442 break;
7444 /* Make sure that all the cases set 'ret' appropriately. */
7445 ret = GS_UNHANDLED;
7446 switch (TREE_CODE (*expr_p))
7448 /* First deal with the special cases. */
7450 case POSTINCREMENT_EXPR:
7451 case POSTDECREMENT_EXPR:
7452 case PREINCREMENT_EXPR:
7453 case PREDECREMENT_EXPR:
7454 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7455 fallback != fb_none,
7456 TREE_TYPE (*expr_p));
7457 break;
7459 case VIEW_CONVERT_EXPR:
7460 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7461 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7463 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7464 post_p, is_gimple_val, fb_rvalue);
7465 recalculate_side_effects (*expr_p);
7466 break;
7468 /* Fallthru. */
7470 case ARRAY_REF:
7471 case ARRAY_RANGE_REF:
7472 case REALPART_EXPR:
7473 case IMAGPART_EXPR:
7474 case COMPONENT_REF:
7475 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7476 fallback ? fallback : fb_rvalue);
7477 break;
7479 case COND_EXPR:
7480 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7482 /* C99 code may assign to an array in a structure value of a
7483 conditional expression, and this has undefined behavior
7484 only on execution, so create a temporary if an lvalue is
7485 required. */
7486 if (fallback == fb_lvalue)
7488 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7489 mark_addressable (*expr_p);
7490 ret = GS_OK;
7492 break;
7494 case CALL_EXPR:
7495 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7497 /* C99 code may assign to an array in a structure returned
7498 from a function, and this has undefined behavior only on
7499 execution, so create a temporary if an lvalue is
7500 required. */
7501 if (fallback == fb_lvalue)
7503 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7504 mark_addressable (*expr_p);
7505 ret = GS_OK;
7507 break;
7509 case TREE_LIST:
7510 gcc_unreachable ();
7512 case COMPOUND_EXPR:
7513 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7514 break;
7516 case COMPOUND_LITERAL_EXPR:
7517 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7518 gimple_test_f, fallback);
7519 break;
7521 case MODIFY_EXPR:
7522 case INIT_EXPR:
7523 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7524 fallback != fb_none);
7525 break;
7527 case TRUTH_ANDIF_EXPR:
7528 case TRUTH_ORIF_EXPR:
7530 /* Preserve the original type of the expression and the
7531 source location of the outer expression. */
7532 tree org_type = TREE_TYPE (*expr_p);
7533 *expr_p = gimple_boolify (*expr_p);
7534 *expr_p = build3_loc (input_location, COND_EXPR,
7535 org_type, *expr_p,
7536 fold_convert_loc
7537 (input_location,
7538 org_type, boolean_true_node),
7539 fold_convert_loc
7540 (input_location,
7541 org_type, boolean_false_node));
7542 ret = GS_OK;
7543 break;
7546 case TRUTH_NOT_EXPR:
7548 tree type = TREE_TYPE (*expr_p);
7549 /* The parsers are careful to generate TRUTH_NOT_EXPR
7550 only with operands that are always zero or one.
7551 We do not fold here but handle the only interesting case
7552 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7553 *expr_p = gimple_boolify (*expr_p);
7554 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7555 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7556 TREE_TYPE (*expr_p),
7557 TREE_OPERAND (*expr_p, 0));
7558 else
7559 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7560 TREE_TYPE (*expr_p),
7561 TREE_OPERAND (*expr_p, 0),
7562 build_int_cst (TREE_TYPE (*expr_p), 1));
7563 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7564 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7565 ret = GS_OK;
7566 break;
7569 case ADDR_EXPR:
7570 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7571 break;
7573 case ANNOTATE_EXPR:
7575 tree cond = TREE_OPERAND (*expr_p, 0);
7576 tree kind = TREE_OPERAND (*expr_p, 1);
7577 tree type = TREE_TYPE (cond);
7578 if (!INTEGRAL_TYPE_P (type))
7580 *expr_p = cond;
7581 ret = GS_OK;
7582 break;
7584 tree tmp = create_tmp_var (type, NULL);
7585 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7586 gimple call
7587 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7588 gimple_call_set_lhs (call, tmp);
7589 gimplify_seq_add_stmt (pre_p, call);
7590 *expr_p = tmp;
7591 ret = GS_ALL_DONE;
7592 break;
7595 case VA_ARG_EXPR:
7596 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7597 break;
7599 CASE_CONVERT:
7600 if (IS_EMPTY_STMT (*expr_p))
7602 ret = GS_ALL_DONE;
7603 break;
7606 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7607 || fallback == fb_none)
7609 /* Just strip a conversion to void (or in void context) and
7610 try again. */
7611 *expr_p = TREE_OPERAND (*expr_p, 0);
7612 ret = GS_OK;
7613 break;
7616 ret = gimplify_conversion (expr_p);
7617 if (ret == GS_ERROR)
7618 break;
7619 if (*expr_p != save_expr)
7620 break;
7621 /* FALLTHRU */
7623 case FIX_TRUNC_EXPR:
7624 /* unary_expr: ... | '(' cast ')' val | ... */
7625 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7626 is_gimple_val, fb_rvalue);
7627 recalculate_side_effects (*expr_p);
7628 break;
7630 case INDIRECT_REF:
7632 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7633 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7634 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7636 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7637 if (*expr_p != save_expr)
7639 ret = GS_OK;
7640 break;
7643 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7644 is_gimple_reg, fb_rvalue);
7645 if (ret == GS_ERROR)
7646 break;
7648 recalculate_side_effects (*expr_p);
7649 *expr_p = fold_build2_loc (input_location, MEM_REF,
7650 TREE_TYPE (*expr_p),
7651 TREE_OPERAND (*expr_p, 0),
7652 build_int_cst (saved_ptr_type, 0));
7653 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7654 TREE_THIS_NOTRAP (*expr_p) = notrap;
7655 ret = GS_OK;
7656 break;
7659 /* We arrive here through the various re-gimplifcation paths. */
7660 case MEM_REF:
7661 /* First try re-folding the whole thing. */
7662 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7663 TREE_OPERAND (*expr_p, 0),
7664 TREE_OPERAND (*expr_p, 1));
7665 if (tmp)
7667 *expr_p = tmp;
7668 recalculate_side_effects (*expr_p);
7669 ret = GS_OK;
7670 break;
7672 /* Avoid re-gimplifying the address operand if it is already
7673 in suitable form. Re-gimplifying would mark the address
7674 operand addressable. Always gimplify when not in SSA form
7675 as we still may have to gimplify decls with value-exprs. */
7676 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7677 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7679 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7680 is_gimple_mem_ref_addr, fb_rvalue);
7681 if (ret == GS_ERROR)
7682 break;
7684 recalculate_side_effects (*expr_p);
7685 ret = GS_ALL_DONE;
7686 break;
7688 /* Constants need not be gimplified. */
7689 case INTEGER_CST:
7690 case REAL_CST:
7691 case FIXED_CST:
7692 case STRING_CST:
7693 case COMPLEX_CST:
7694 case VECTOR_CST:
7695 /* Drop the overflow flag on constants, we do not want
7696 that in the GIMPLE IL. */
7697 if (TREE_OVERFLOW_P (*expr_p))
7698 *expr_p = drop_tree_overflow (*expr_p);
7699 ret = GS_ALL_DONE;
7700 break;
7702 case CONST_DECL:
7703 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7704 CONST_DECL node. Otherwise the decl is replaceable by its
7705 value. */
7706 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7707 if (fallback & fb_lvalue)
7708 ret = GS_ALL_DONE;
7709 else
7711 *expr_p = DECL_INITIAL (*expr_p);
7712 ret = GS_OK;
7714 break;
7716 case DECL_EXPR:
7717 ret = gimplify_decl_expr (expr_p, pre_p);
7718 break;
7720 case BIND_EXPR:
7721 ret = gimplify_bind_expr (expr_p, pre_p);
7722 break;
7724 case LOOP_EXPR:
7725 ret = gimplify_loop_expr (expr_p, pre_p);
7726 break;
7728 case SWITCH_EXPR:
7729 ret = gimplify_switch_expr (expr_p, pre_p);
7730 break;
7732 case EXIT_EXPR:
7733 ret = gimplify_exit_expr (expr_p);
7734 break;
7736 case GOTO_EXPR:
7737 /* If the target is not LABEL, then it is a computed jump
7738 and the target needs to be gimplified. */
7739 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7741 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7742 NULL, is_gimple_val, fb_rvalue);
7743 if (ret == GS_ERROR)
7744 break;
7746 gimplify_seq_add_stmt (pre_p,
7747 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7748 ret = GS_ALL_DONE;
7749 break;
7751 case PREDICT_EXPR:
7752 gimplify_seq_add_stmt (pre_p,
7753 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7754 PREDICT_EXPR_OUTCOME (*expr_p)));
7755 ret = GS_ALL_DONE;
7756 break;
7758 case LABEL_EXPR:
7759 ret = GS_ALL_DONE;
7760 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7761 == current_function_decl);
7762 gimplify_seq_add_stmt (pre_p,
7763 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7764 break;
7766 case CASE_LABEL_EXPR:
7767 ret = gimplify_case_label_expr (expr_p, pre_p);
7768 break;
7770 case RETURN_EXPR:
7771 ret = gimplify_return_expr (*expr_p, pre_p);
7772 break;
7774 case CONSTRUCTOR:
7775 /* Don't reduce this in place; let gimplify_init_constructor work its
7776 magic. Buf if we're just elaborating this for side effects, just
7777 gimplify any element that has side-effects. */
7778 if (fallback == fb_none)
7780 unsigned HOST_WIDE_INT ix;
7781 tree val;
7782 tree temp = NULL_TREE;
7783 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7784 if (TREE_SIDE_EFFECTS (val))
7785 append_to_statement_list (val, &temp);
7787 *expr_p = temp;
7788 ret = temp ? GS_OK : GS_ALL_DONE;
7790 /* C99 code may assign to an array in a constructed
7791 structure or union, and this has undefined behavior only
7792 on execution, so create a temporary if an lvalue is
7793 required. */
7794 else if (fallback == fb_lvalue)
7796 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7797 mark_addressable (*expr_p);
7798 ret = GS_OK;
7800 else
7801 ret = GS_ALL_DONE;
7802 break;
7804 /* The following are special cases that are not handled by the
7805 original GIMPLE grammar. */
7807 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7808 eliminated. */
7809 case SAVE_EXPR:
7810 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7811 break;
7813 case BIT_FIELD_REF:
7814 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7815 post_p, is_gimple_lvalue, fb_either);
7816 recalculate_side_effects (*expr_p);
7817 break;
7819 case TARGET_MEM_REF:
7821 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7823 if (TMR_BASE (*expr_p))
7824 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7825 post_p, is_gimple_mem_ref_addr, fb_either);
7826 if (TMR_INDEX (*expr_p))
7827 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7828 post_p, is_gimple_val, fb_rvalue);
7829 if (TMR_INDEX2 (*expr_p))
7830 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7831 post_p, is_gimple_val, fb_rvalue);
7832 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7833 ret = MIN (r0, r1);
7835 break;
7837 case NON_LVALUE_EXPR:
7838 /* This should have been stripped above. */
7839 gcc_unreachable ();
7841 case ASM_EXPR:
7842 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7843 break;
7845 case TRY_FINALLY_EXPR:
7846 case TRY_CATCH_EXPR:
7848 gimple_seq eval, cleanup;
7849 gimple try_;
7851 /* Calls to destructors are generated automatically in FINALLY/CATCH
7852 block. They should have location as UNKNOWN_LOCATION. However,
7853 gimplify_call_expr will reset these call stmts to input_location
7854 if it finds stmt's location is unknown. To prevent resetting for
7855 destructors, we set the input_location to unknown.
7856 Note that this only affects the destructor calls in FINALLY/CATCH
7857 block, and will automatically reset to its original value by the
7858 end of gimplify_expr. */
7859 input_location = UNKNOWN_LOCATION;
7860 eval = cleanup = NULL;
7861 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7862 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7863 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7864 if (gimple_seq_empty_p (cleanup))
7866 gimple_seq_add_seq (pre_p, eval);
7867 ret = GS_ALL_DONE;
7868 break;
7870 try_ = gimple_build_try (eval, cleanup,
7871 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7872 ? GIMPLE_TRY_FINALLY
7873 : GIMPLE_TRY_CATCH);
7874 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7875 gimple_set_location (try_, saved_location);
7876 else
7877 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7878 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7879 gimple_try_set_catch_is_cleanup (try_,
7880 TRY_CATCH_IS_CLEANUP (*expr_p));
7881 gimplify_seq_add_stmt (pre_p, try_);
7882 ret = GS_ALL_DONE;
7883 break;
7886 case CLEANUP_POINT_EXPR:
7887 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7888 break;
7890 case TARGET_EXPR:
7891 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7892 break;
7894 case CATCH_EXPR:
7896 gimple c;
7897 gimple_seq handler = NULL;
7898 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7899 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7900 gimplify_seq_add_stmt (pre_p, c);
7901 ret = GS_ALL_DONE;
7902 break;
7905 case EH_FILTER_EXPR:
7907 gimple ehf;
7908 gimple_seq failure = NULL;
7910 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7911 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7912 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7913 gimplify_seq_add_stmt (pre_p, ehf);
7914 ret = GS_ALL_DONE;
7915 break;
7918 case OBJ_TYPE_REF:
7920 enum gimplify_status r0, r1;
7921 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7922 post_p, is_gimple_val, fb_rvalue);
7923 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7924 post_p, is_gimple_val, fb_rvalue);
7925 TREE_SIDE_EFFECTS (*expr_p) = 0;
7926 ret = MIN (r0, r1);
7928 break;
7930 case LABEL_DECL:
7931 /* We get here when taking the address of a label. We mark
7932 the label as "forced"; meaning it can never be removed and
7933 it is a potential target for any computed goto. */
7934 FORCED_LABEL (*expr_p) = 1;
7935 ret = GS_ALL_DONE;
7936 break;
7938 case STATEMENT_LIST:
7939 ret = gimplify_statement_list (expr_p, pre_p);
7940 break;
7942 case WITH_SIZE_EXPR:
7944 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7945 post_p == &internal_post ? NULL : post_p,
7946 gimple_test_f, fallback);
7947 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7948 is_gimple_val, fb_rvalue);
7949 ret = GS_ALL_DONE;
7951 break;
7953 case VAR_DECL:
7954 case PARM_DECL:
7955 ret = gimplify_var_or_parm_decl (expr_p);
7956 break;
7958 case RESULT_DECL:
7959 /* When within an OpenMP context, notice uses of variables. */
7960 if (gimplify_omp_ctxp)
7961 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7962 ret = GS_ALL_DONE;
7963 break;
7965 case SSA_NAME:
7966 /* Allow callbacks into the gimplifier during optimization. */
7967 ret = GS_ALL_DONE;
7968 break;
7970 case OMP_PARALLEL:
7971 gimplify_omp_parallel (expr_p, pre_p);
7972 ret = GS_ALL_DONE;
7973 break;
7975 case OMP_TASK:
7976 gimplify_omp_task (expr_p, pre_p);
7977 ret = GS_ALL_DONE;
7978 break;
7980 case OMP_FOR:
7981 case OMP_SIMD:
7982 case CILK_SIMD:
7983 case OMP_DISTRIBUTE:
7984 ret = gimplify_omp_for (expr_p, pre_p);
7985 break;
7987 case OMP_SECTIONS:
7988 case OMP_SINGLE:
7989 case OMP_TARGET:
7990 case OMP_TARGET_DATA:
7991 case OMP_TEAMS:
7992 gimplify_omp_workshare (expr_p, pre_p);
7993 ret = GS_ALL_DONE;
7994 break;
7996 case OMP_TARGET_UPDATE:
7997 gimplify_omp_target_update (expr_p, pre_p);
7998 ret = GS_ALL_DONE;
7999 break;
8001 case OMP_SECTION:
8002 case OMP_MASTER:
8003 case OMP_TASKGROUP:
8004 case OMP_ORDERED:
8005 case OMP_CRITICAL:
8007 gimple_seq body = NULL;
8008 gimple g;
8010 gimplify_and_add (OMP_BODY (*expr_p), &body);
8011 switch (TREE_CODE (*expr_p))
8013 case OMP_SECTION:
8014 g = gimple_build_omp_section (body);
8015 break;
8016 case OMP_MASTER:
8017 g = gimple_build_omp_master (body);
8018 break;
8019 case OMP_TASKGROUP:
8021 gimple_seq cleanup = NULL;
8022 tree fn
8023 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8024 g = gimple_build_call (fn, 0);
8025 gimple_seq_add_stmt (&cleanup, g);
8026 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8027 body = NULL;
8028 gimple_seq_add_stmt (&body, g);
8029 g = gimple_build_omp_taskgroup (body);
8031 break;
8032 case OMP_ORDERED:
8033 g = gimple_build_omp_ordered (body);
8034 break;
8035 case OMP_CRITICAL:
8036 g = gimple_build_omp_critical (body,
8037 OMP_CRITICAL_NAME (*expr_p));
8038 break;
8039 default:
8040 gcc_unreachable ();
8042 gimplify_seq_add_stmt (pre_p, g);
8043 ret = GS_ALL_DONE;
8044 break;
8047 case OMP_ATOMIC:
8048 case OMP_ATOMIC_READ:
8049 case OMP_ATOMIC_CAPTURE_OLD:
8050 case OMP_ATOMIC_CAPTURE_NEW:
8051 ret = gimplify_omp_atomic (expr_p, pre_p);
8052 break;
8054 case TRANSACTION_EXPR:
8055 ret = gimplify_transaction (expr_p, pre_p);
8056 break;
8058 case TRUTH_AND_EXPR:
8059 case TRUTH_OR_EXPR:
8060 case TRUTH_XOR_EXPR:
8062 tree orig_type = TREE_TYPE (*expr_p);
8063 tree new_type, xop0, xop1;
8064 *expr_p = gimple_boolify (*expr_p);
8065 new_type = TREE_TYPE (*expr_p);
8066 if (!useless_type_conversion_p (orig_type, new_type))
8068 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8069 ret = GS_OK;
8070 break;
8073 /* Boolified binary truth expressions are semantically equivalent
8074 to bitwise binary expressions. Canonicalize them to the
8075 bitwise variant. */
8076 switch (TREE_CODE (*expr_p))
8078 case TRUTH_AND_EXPR:
8079 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8080 break;
8081 case TRUTH_OR_EXPR:
8082 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8083 break;
8084 case TRUTH_XOR_EXPR:
8085 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8086 break;
8087 default:
8088 break;
8090 /* Now make sure that operands have compatible type to
8091 expression's new_type. */
8092 xop0 = TREE_OPERAND (*expr_p, 0);
8093 xop1 = TREE_OPERAND (*expr_p, 1);
8094 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8095 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8096 new_type,
8097 xop0);
8098 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8099 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8100 new_type,
8101 xop1);
8102 /* Continue classified as tcc_binary. */
8103 goto expr_2;
8106 case FMA_EXPR:
8107 case VEC_COND_EXPR:
8108 case VEC_PERM_EXPR:
8109 /* Classified as tcc_expression. */
8110 goto expr_3;
8112 case POINTER_PLUS_EXPR:
8114 enum gimplify_status r0, r1;
8115 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8116 post_p, is_gimple_val, fb_rvalue);
8117 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8118 post_p, is_gimple_val, fb_rvalue);
8119 recalculate_side_effects (*expr_p);
8120 ret = MIN (r0, r1);
8121 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8122 after gimplifying operands - this is similar to how
8123 it would be folding all gimplified stmts on creation
8124 to have them canonicalized, which is what we eventually
8125 should do anyway. */
8126 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8127 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8129 *expr_p = build_fold_addr_expr_with_type_loc
8130 (input_location,
8131 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8132 TREE_OPERAND (*expr_p, 0),
8133 fold_convert (ptr_type_node,
8134 TREE_OPERAND (*expr_p, 1))),
8135 TREE_TYPE (*expr_p));
8136 ret = MIN (ret, GS_OK);
8138 break;
8141 case CILK_SYNC_STMT:
8143 if (!fn_contains_cilk_spawn_p (cfun))
8145 error_at (EXPR_LOCATION (*expr_p),
8146 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8147 ret = GS_ERROR;
8149 else
8151 gimplify_cilk_sync (expr_p, pre_p);
8152 ret = GS_ALL_DONE;
8154 break;
8157 default:
8158 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8160 case tcc_comparison:
8161 /* Handle comparison of objects of non scalar mode aggregates
8162 with a call to memcmp. It would be nice to only have to do
8163 this for variable-sized objects, but then we'd have to allow
8164 the same nest of reference nodes we allow for MODIFY_EXPR and
8165 that's too complex.
8167 Compare scalar mode aggregates as scalar mode values. Using
8168 memcmp for them would be very inefficient at best, and is
8169 plain wrong if bitfields are involved. */
8171 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8173 /* Vector comparisons need no boolification. */
8174 if (TREE_CODE (type) == VECTOR_TYPE)
8175 goto expr_2;
8176 else if (!AGGREGATE_TYPE_P (type))
8178 tree org_type = TREE_TYPE (*expr_p);
8179 *expr_p = gimple_boolify (*expr_p);
8180 if (!useless_type_conversion_p (org_type,
8181 TREE_TYPE (*expr_p)))
8183 *expr_p = fold_convert_loc (input_location,
8184 org_type, *expr_p);
8185 ret = GS_OK;
8187 else
8188 goto expr_2;
8190 else if (TYPE_MODE (type) != BLKmode)
8191 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8192 else
8193 ret = gimplify_variable_sized_compare (expr_p);
8195 break;
8198 /* If *EXPR_P does not need to be special-cased, handle it
8199 according to its class. */
8200 case tcc_unary:
8201 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8202 post_p, is_gimple_val, fb_rvalue);
8203 break;
8205 case tcc_binary:
8206 expr_2:
8208 enum gimplify_status r0, r1;
8210 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8211 post_p, is_gimple_val, fb_rvalue);
8212 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8213 post_p, is_gimple_val, fb_rvalue);
8215 ret = MIN (r0, r1);
8216 break;
8219 expr_3:
8221 enum gimplify_status r0, r1, r2;
8223 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8224 post_p, is_gimple_val, fb_rvalue);
8225 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8226 post_p, is_gimple_val, fb_rvalue);
8227 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8228 post_p, is_gimple_val, fb_rvalue);
8230 ret = MIN (MIN (r0, r1), r2);
8231 break;
8234 case tcc_declaration:
8235 case tcc_constant:
8236 ret = GS_ALL_DONE;
8237 goto dont_recalculate;
8239 default:
8240 gcc_unreachable ();
8243 recalculate_side_effects (*expr_p);
8245 dont_recalculate:
8246 break;
8249 gcc_assert (*expr_p || ret != GS_OK);
8251 while (ret == GS_OK);
8253 /* If we encountered an error_mark somewhere nested inside, either
8254 stub out the statement or propagate the error back out. */
8255 if (ret == GS_ERROR)
8257 if (is_statement)
8258 *expr_p = NULL;
8259 goto out;
8262 /* This was only valid as a return value from the langhook, which
8263 we handled. Make sure it doesn't escape from any other context. */
8264 gcc_assert (ret != GS_UNHANDLED);
8266 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8268 /* We aren't looking for a value, and we don't have a valid
8269 statement. If it doesn't have side-effects, throw it away. */
8270 if (!TREE_SIDE_EFFECTS (*expr_p))
8271 *expr_p = NULL;
8272 else if (!TREE_THIS_VOLATILE (*expr_p))
8274 /* This is probably a _REF that contains something nested that
8275 has side effects. Recurse through the operands to find it. */
8276 enum tree_code code = TREE_CODE (*expr_p);
8278 switch (code)
8280 case COMPONENT_REF:
8281 case REALPART_EXPR:
8282 case IMAGPART_EXPR:
8283 case VIEW_CONVERT_EXPR:
8284 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8285 gimple_test_f, fallback);
8286 break;
8288 case ARRAY_REF:
8289 case ARRAY_RANGE_REF:
8290 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8291 gimple_test_f, fallback);
8292 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8293 gimple_test_f, fallback);
8294 break;
8296 default:
8297 /* Anything else with side-effects must be converted to
8298 a valid statement before we get here. */
8299 gcc_unreachable ();
8302 *expr_p = NULL;
8304 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8305 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8307 /* Historically, the compiler has treated a bare reference
8308 to a non-BLKmode volatile lvalue as forcing a load. */
8309 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8311 /* Normally, we do not want to create a temporary for a
8312 TREE_ADDRESSABLE type because such a type should not be
8313 copied by bitwise-assignment. However, we make an
8314 exception here, as all we are doing here is ensuring that
8315 we read the bytes that make up the type. We use
8316 create_tmp_var_raw because create_tmp_var will abort when
8317 given a TREE_ADDRESSABLE type. */
8318 tree tmp = create_tmp_var_raw (type, "vol");
8319 gimple_add_tmp_var (tmp);
8320 gimplify_assign (tmp, *expr_p, pre_p);
8321 *expr_p = NULL;
8323 else
8324 /* We can't do anything useful with a volatile reference to
8325 an incomplete type, so just throw it away. Likewise for
8326 a BLKmode type, since any implicit inner load should
8327 already have been turned into an explicit one by the
8328 gimplification process. */
8329 *expr_p = NULL;
8332 /* If we are gimplifying at the statement level, we're done. Tack
8333 everything together and return. */
8334 if (fallback == fb_none || is_statement)
8336 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8337 it out for GC to reclaim it. */
8338 *expr_p = NULL_TREE;
8340 if (!gimple_seq_empty_p (internal_pre)
8341 || !gimple_seq_empty_p (internal_post))
8343 gimplify_seq_add_seq (&internal_pre, internal_post);
8344 gimplify_seq_add_seq (pre_p, internal_pre);
8347 /* The result of gimplifying *EXPR_P is going to be the last few
8348 statements in *PRE_P and *POST_P. Add location information
8349 to all the statements that were added by the gimplification
8350 helpers. */
8351 if (!gimple_seq_empty_p (*pre_p))
8352 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8354 if (!gimple_seq_empty_p (*post_p))
8355 annotate_all_with_location_after (*post_p, post_last_gsi,
8356 input_location);
8358 goto out;
8361 #ifdef ENABLE_GIMPLE_CHECKING
8362 if (*expr_p)
8364 enum tree_code code = TREE_CODE (*expr_p);
8365 /* These expressions should already be in gimple IR form. */
8366 gcc_assert (code != MODIFY_EXPR
8367 && code != ASM_EXPR
8368 && code != BIND_EXPR
8369 && code != CATCH_EXPR
8370 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8371 && code != EH_FILTER_EXPR
8372 && code != GOTO_EXPR
8373 && code != LABEL_EXPR
8374 && code != LOOP_EXPR
8375 && code != SWITCH_EXPR
8376 && code != TRY_FINALLY_EXPR
8377 && code != OMP_CRITICAL
8378 && code != OMP_FOR
8379 && code != OMP_MASTER
8380 && code != OMP_TASKGROUP
8381 && code != OMP_ORDERED
8382 && code != OMP_PARALLEL
8383 && code != OMP_SECTIONS
8384 && code != OMP_SECTION
8385 && code != OMP_SINGLE);
8387 #endif
8389 /* Otherwise we're gimplifying a subexpression, so the resulting
8390 value is interesting. If it's a valid operand that matches
8391 GIMPLE_TEST_F, we're done. Unless we are handling some
8392 post-effects internally; if that's the case, we need to copy into
8393 a temporary before adding the post-effects to POST_P. */
8394 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8395 goto out;
8397 /* Otherwise, we need to create a new temporary for the gimplified
8398 expression. */
8400 /* We can't return an lvalue if we have an internal postqueue. The
8401 object the lvalue refers to would (probably) be modified by the
8402 postqueue; we need to copy the value out first, which means an
8403 rvalue. */
8404 if ((fallback & fb_lvalue)
8405 && gimple_seq_empty_p (internal_post)
8406 && is_gimple_addressable (*expr_p))
8408 /* An lvalue will do. Take the address of the expression, store it
8409 in a temporary, and replace the expression with an INDIRECT_REF of
8410 that temporary. */
8411 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8412 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8413 *expr_p = build_simple_mem_ref (tmp);
8415 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8417 /* An rvalue will do. Assign the gimplified expression into a
8418 new temporary TMP and replace the original expression with
8419 TMP. First, make sure that the expression has a type so that
8420 it can be assigned into a temporary. */
8421 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8422 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8424 else
8426 #ifdef ENABLE_GIMPLE_CHECKING
8427 if (!(fallback & fb_mayfail))
8429 fprintf (stderr, "gimplification failed:\n");
8430 print_generic_expr (stderr, *expr_p, 0);
8431 debug_tree (*expr_p);
8432 internal_error ("gimplification failed");
8434 #endif
8435 gcc_assert (fallback & fb_mayfail);
8437 /* If this is an asm statement, and the user asked for the
8438 impossible, don't die. Fail and let gimplify_asm_expr
8439 issue an error. */
8440 ret = GS_ERROR;
8441 goto out;
8444 /* Make sure the temporary matches our predicate. */
8445 gcc_assert ((*gimple_test_f) (*expr_p));
8447 if (!gimple_seq_empty_p (internal_post))
8449 annotate_all_with_location (internal_post, input_location);
8450 gimplify_seq_add_seq (pre_p, internal_post);
8453 out:
8454 input_location = saved_location;
8455 return ret;
8458 /* Look through TYPE for variable-sized objects and gimplify each such
8459 size that we find. Add to LIST_P any statements generated. */
8461 void
8462 gimplify_type_sizes (tree type, gimple_seq *list_p)
8464 tree field, t;
8466 if (type == NULL || type == error_mark_node)
8467 return;
8469 /* We first do the main variant, then copy into any other variants. */
8470 type = TYPE_MAIN_VARIANT (type);
8472 /* Avoid infinite recursion. */
8473 if (TYPE_SIZES_GIMPLIFIED (type))
8474 return;
8476 TYPE_SIZES_GIMPLIFIED (type) = 1;
8478 switch (TREE_CODE (type))
8480 case INTEGER_TYPE:
8481 case ENUMERAL_TYPE:
8482 case BOOLEAN_TYPE:
8483 case REAL_TYPE:
8484 case FIXED_POINT_TYPE:
8485 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8486 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8488 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8490 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8491 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8493 break;
8495 case ARRAY_TYPE:
8496 /* These types may not have declarations, so handle them here. */
8497 gimplify_type_sizes (TREE_TYPE (type), list_p);
8498 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8499 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8500 with assigned stack slots, for -O1+ -g they should be tracked
8501 by VTA. */
8502 if (!(TYPE_NAME (type)
8503 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8504 && DECL_IGNORED_P (TYPE_NAME (type)))
8505 && TYPE_DOMAIN (type)
8506 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8508 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8509 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8510 DECL_IGNORED_P (t) = 0;
8511 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8512 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8513 DECL_IGNORED_P (t) = 0;
8515 break;
8517 case RECORD_TYPE:
8518 case UNION_TYPE:
8519 case QUAL_UNION_TYPE:
8520 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8521 if (TREE_CODE (field) == FIELD_DECL)
8523 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8524 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8525 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8526 gimplify_type_sizes (TREE_TYPE (field), list_p);
8528 break;
8530 case POINTER_TYPE:
8531 case REFERENCE_TYPE:
8532 /* We used to recurse on the pointed-to type here, which turned out to
8533 be incorrect because its definition might refer to variables not
8534 yet initialized at this point if a forward declaration is involved.
8536 It was actually useful for anonymous pointed-to types to ensure
8537 that the sizes evaluation dominates every possible later use of the
8538 values. Restricting to such types here would be safe since there
8539 is no possible forward declaration around, but would introduce an
8540 undesirable middle-end semantic to anonymity. We then defer to
8541 front-ends the responsibility of ensuring that the sizes are
8542 evaluated both early and late enough, e.g. by attaching artificial
8543 type declarations to the tree. */
8544 break;
8546 default:
8547 break;
8550 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8551 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8553 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8555 TYPE_SIZE (t) = TYPE_SIZE (type);
8556 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8557 TYPE_SIZES_GIMPLIFIED (t) = 1;
8561 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8562 a size or position, has had all of its SAVE_EXPRs evaluated.
8563 We add any required statements to *STMT_P. */
8565 void
8566 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8568 tree expr = *expr_p;
8570 /* We don't do anything if the value isn't there, is constant, or contains
8571 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8572 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8573 will want to replace it with a new variable, but that will cause problems
8574 if this type is from outside the function. It's OK to have that here. */
8575 if (is_gimple_sizepos (expr))
8576 return;
8578 *expr_p = unshare_expr (expr);
8580 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8583 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8584 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8585 is true, also gimplify the parameters. */
8587 gimple
8588 gimplify_body (tree fndecl, bool do_parms)
8590 location_t saved_location = input_location;
8591 gimple_seq parm_stmts, seq;
8592 gimple outer_bind;
8593 struct cgraph_node *cgn;
8595 timevar_push (TV_TREE_GIMPLIFY);
8597 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8598 gimplification. */
8599 default_rtl_profile ();
8601 gcc_assert (gimplify_ctxp == NULL);
8602 push_gimplify_context ();
8604 if (flag_openmp)
8606 gcc_assert (gimplify_omp_ctxp == NULL);
8607 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8608 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8611 /* Unshare most shared trees in the body and in that of any nested functions.
8612 It would seem we don't have to do this for nested functions because
8613 they are supposed to be output and then the outer function gimplified
8614 first, but the g++ front end doesn't always do it that way. */
8615 unshare_body (fndecl);
8616 unvisit_body (fndecl);
8618 cgn = cgraph_get_node (fndecl);
8619 if (cgn && cgn->origin)
8620 nonlocal_vlas = pointer_set_create ();
8622 /* Make sure input_location isn't set to something weird. */
8623 input_location = DECL_SOURCE_LOCATION (fndecl);
8625 /* Resolve callee-copies. This has to be done before processing
8626 the body so that DECL_VALUE_EXPR gets processed correctly. */
8627 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8629 /* Gimplify the function's body. */
8630 seq = NULL;
8631 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8632 outer_bind = gimple_seq_first_stmt (seq);
8633 if (!outer_bind)
8635 outer_bind = gimple_build_nop ();
8636 gimplify_seq_add_stmt (&seq, outer_bind);
8639 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8640 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8641 if (gimple_code (outer_bind) == GIMPLE_BIND
8642 && gimple_seq_first (seq) == gimple_seq_last (seq))
8644 else
8645 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8647 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8649 /* If we had callee-copies statements, insert them at the beginning
8650 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8651 if (!gimple_seq_empty_p (parm_stmts))
8653 tree parm;
8655 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8656 gimple_bind_set_body (outer_bind, parm_stmts);
8658 for (parm = DECL_ARGUMENTS (current_function_decl);
8659 parm; parm = DECL_CHAIN (parm))
8660 if (DECL_HAS_VALUE_EXPR_P (parm))
8662 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8663 DECL_IGNORED_P (parm) = 0;
8667 if (nonlocal_vlas)
8669 if (nonlocal_vla_vars)
8671 /* tree-nested.c may later on call declare_vars (..., true);
8672 which relies on BLOCK_VARS chain to be the tail of the
8673 gimple_bind_vars chain. Ensure we don't violate that
8674 assumption. */
8675 if (gimple_bind_block (outer_bind)
8676 == DECL_INITIAL (current_function_decl))
8677 declare_vars (nonlocal_vla_vars, outer_bind, true);
8678 else
8679 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8680 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8681 nonlocal_vla_vars);
8682 nonlocal_vla_vars = NULL_TREE;
8684 pointer_set_destroy (nonlocal_vlas);
8685 nonlocal_vlas = NULL;
8688 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8690 delete_omp_context (gimplify_omp_ctxp);
8691 gimplify_omp_ctxp = NULL;
8694 pop_gimplify_context (outer_bind);
8695 gcc_assert (gimplify_ctxp == NULL);
8697 #ifdef ENABLE_CHECKING
8698 if (!seen_error ())
8699 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8700 #endif
8702 timevar_pop (TV_TREE_GIMPLIFY);
8703 input_location = saved_location;
8705 return outer_bind;
8708 typedef char *char_p; /* For DEF_VEC_P. */
8710 /* Return whether we should exclude FNDECL from instrumentation. */
8712 static bool
8713 flag_instrument_functions_exclude_p (tree fndecl)
8715 vec<char_p> *v;
8717 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8718 if (v && v->length () > 0)
8720 const char *name;
8721 int i;
8722 char *s;
8724 name = lang_hooks.decl_printable_name (fndecl, 0);
8725 FOR_EACH_VEC_ELT (*v, i, s)
8726 if (strstr (name, s) != NULL)
8727 return true;
8730 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8731 if (v && v->length () > 0)
8733 const char *name;
8734 int i;
8735 char *s;
8737 name = DECL_SOURCE_FILE (fndecl);
8738 FOR_EACH_VEC_ELT (*v, i, s)
8739 if (strstr (name, s) != NULL)
8740 return true;
8743 return false;
8746 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8747 node for the function we want to gimplify.
8749 Return the sequence of GIMPLE statements corresponding to the body
8750 of FNDECL. */
8752 void
8753 gimplify_function_tree (tree fndecl)
8755 tree parm, ret;
8756 gimple_seq seq;
8757 gimple bind;
8759 gcc_assert (!gimple_body (fndecl));
8761 if (DECL_STRUCT_FUNCTION (fndecl))
8762 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8763 else
8764 push_struct_function (fndecl);
8766 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8768 /* Preliminarily mark non-addressed complex variables as eligible
8769 for promotion to gimple registers. We'll transform their uses
8770 as we find them. */
8771 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8772 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8773 && !TREE_THIS_VOLATILE (parm)
8774 && !needs_to_live_in_memory (parm))
8775 DECL_GIMPLE_REG_P (parm) = 1;
8778 ret = DECL_RESULT (fndecl);
8779 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8780 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8781 && !needs_to_live_in_memory (ret))
8782 DECL_GIMPLE_REG_P (ret) = 1;
8784 bind = gimplify_body (fndecl, true);
8786 /* The tree body of the function is no longer needed, replace it
8787 with the new GIMPLE body. */
8788 seq = NULL;
8789 gimple_seq_add_stmt (&seq, bind);
8790 gimple_set_body (fndecl, seq);
8792 /* If we're instrumenting function entry/exit, then prepend the call to
8793 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8794 catch the exit hook. */
8795 /* ??? Add some way to ignore exceptions for this TFE. */
8796 if (flag_instrument_function_entry_exit
8797 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8798 && !flag_instrument_functions_exclude_p (fndecl))
8800 tree x;
8801 gimple new_bind;
8802 gimple tf;
8803 gimple_seq cleanup = NULL, body = NULL;
8804 tree tmp_var;
8805 gimple call;
8807 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8808 call = gimple_build_call (x, 1, integer_zero_node);
8809 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8810 gimple_call_set_lhs (call, tmp_var);
8811 gimplify_seq_add_stmt (&cleanup, call);
8812 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8813 call = gimple_build_call (x, 2,
8814 build_fold_addr_expr (current_function_decl),
8815 tmp_var);
8816 gimplify_seq_add_stmt (&cleanup, call);
8817 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8819 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8820 call = gimple_build_call (x, 1, integer_zero_node);
8821 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8822 gimple_call_set_lhs (call, tmp_var);
8823 gimplify_seq_add_stmt (&body, call);
8824 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8825 call = gimple_build_call (x, 2,
8826 build_fold_addr_expr (current_function_decl),
8827 tmp_var);
8828 gimplify_seq_add_stmt (&body, call);
8829 gimplify_seq_add_stmt (&body, tf);
8830 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8831 /* Clear the block for BIND, since it is no longer directly inside
8832 the function, but within a try block. */
8833 gimple_bind_set_block (bind, NULL);
8835 /* Replace the current function body with the body
8836 wrapped in the try/finally TF. */
8837 seq = NULL;
8838 gimple_seq_add_stmt (&seq, new_bind);
8839 gimple_set_body (fndecl, seq);
8842 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8843 cfun->curr_properties = PROP_gimple_any;
8845 pop_cfun ();
8848 /* Return a dummy expression of type TYPE in order to keep going after an
8849 error. */
8851 static tree
8852 dummy_object (tree type)
8854 tree t = build_int_cst (build_pointer_type (type), 0);
8855 return build2 (MEM_REF, type, t, t);
8858 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
8859 builtin function, but a very special sort of operator. */
8861 enum gimplify_status
8862 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8864 tree promoted_type, have_va_type;
8865 tree valist = TREE_OPERAND (*expr_p, 0);
8866 tree type = TREE_TYPE (*expr_p);
8867 tree t;
8868 location_t loc = EXPR_LOCATION (*expr_p);
8870 /* Verify that valist is of the proper type. */
8871 have_va_type = TREE_TYPE (valist);
8872 if (have_va_type == error_mark_node)
8873 return GS_ERROR;
8874 have_va_type = targetm.canonical_va_list_type (have_va_type);
8876 if (have_va_type == NULL_TREE)
8878 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
8879 return GS_ERROR;
8882 /* Generate a diagnostic for requesting data of a type that cannot
8883 be passed through `...' due to type promotion at the call site. */
8884 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
8885 != type)
8887 static bool gave_help;
8888 bool warned;
8890 /* Unfortunately, this is merely undefined, rather than a constraint
8891 violation, so we cannot make this an error. If this call is never
8892 executed, the program is still strictly conforming. */
8893 warned = warning_at (loc, 0,
8894 "%qT is promoted to %qT when passed through %<...%>",
8895 type, promoted_type);
8896 if (!gave_help && warned)
8898 gave_help = true;
8899 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
8900 promoted_type, type);
8903 /* We can, however, treat "undefined" any way we please.
8904 Call abort to encourage the user to fix the program. */
8905 if (warned)
8906 inform (loc, "if this code is reached, the program will abort");
8907 /* Before the abort, allow the evaluation of the va_list
8908 expression to exit or longjmp. */
8909 gimplify_and_add (valist, pre_p);
8910 t = build_call_expr_loc (loc,
8911 builtin_decl_implicit (BUILT_IN_TRAP), 0);
8912 gimplify_and_add (t, pre_p);
8914 /* This is dead code, but go ahead and finish so that the
8915 mode of the result comes out right. */
8916 *expr_p = dummy_object (type);
8917 return GS_ALL_DONE;
8919 else
8921 /* Make it easier for the backends by protecting the valist argument
8922 from multiple evaluations. */
8923 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
8925 /* For this case, the backends will be expecting a pointer to
8926 TREE_TYPE (abi), but it's possible we've
8927 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
8928 So fix it. */
8929 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
8931 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
8932 valist = fold_convert_loc (loc, p1,
8933 build_fold_addr_expr_loc (loc, valist));
8936 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
8938 else
8939 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
8941 if (!targetm.gimplify_va_arg_expr)
8942 /* FIXME: Once most targets are converted we should merely
8943 assert this is non-null. */
8944 return GS_ALL_DONE;
8946 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
8947 return GS_OK;
8951 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
8953 DST/SRC are the destination and source respectively. You can pass
8954 ungimplified trees in DST or SRC, in which case they will be
8955 converted to a gimple operand if necessary.
8957 This function returns the newly created GIMPLE_ASSIGN tuple. */
8959 gimple
8960 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
8962 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
8963 gimplify_and_add (t, seq_p);
8964 ggc_free (t);
8965 return gimple_seq_last_stmt (*seq_p);
8968 inline hashval_t
8969 gimplify_hasher::hash (const value_type *p)
8971 tree t = p->val;
8972 return iterative_hash_expr (t, 0);
8975 inline bool
8976 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
8978 tree t1 = p1->val;
8979 tree t2 = p2->val;
8980 enum tree_code code = TREE_CODE (t1);
8982 if (TREE_CODE (t2) != code
8983 || TREE_TYPE (t1) != TREE_TYPE (t2))
8984 return false;
8986 if (!operand_equal_p (t1, t2, 0))
8987 return false;
8989 #ifdef ENABLE_CHECKING
8990 /* Only allow them to compare equal if they also hash equal; otherwise
8991 results are nondeterminate, and we fail bootstrap comparison. */
8992 gcc_assert (hash (p1) == hash (p2));
8993 #endif
8995 return true;