gcc/testsuite/
[official-gcc.git] / gcc / gimplify.c
blob32416331ac5c6e43c2f3f6fddb1ddfe87a767379
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 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6073 && OMP_CLAUSE_LINEAR_STMT (c))
6075 gimplify_omp_ctxp = ctx;
6076 push_gimplify_context ();
6077 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6079 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6080 NULL, NULL);
6081 TREE_SIDE_EFFECTS (bind) = 1;
6082 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6083 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6085 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6086 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6087 pop_gimplify_context
6088 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6089 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6091 gimplify_omp_ctxp = outer_ctx;
6093 if (notice_outer)
6094 goto do_notice;
6095 break;
6097 case OMP_CLAUSE_COPYIN:
6098 case OMP_CLAUSE_COPYPRIVATE:
6099 decl = OMP_CLAUSE_DECL (c);
6100 if (error_operand_p (decl))
6102 remove = true;
6103 break;
6105 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6106 && !remove
6107 && !omp_check_private (ctx, decl, true))
6109 remove = true;
6110 if (is_global_var (decl))
6112 if (DECL_THREAD_LOCAL_P (decl))
6113 remove = false;
6114 else if (DECL_HAS_VALUE_EXPR_P (decl))
6116 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6118 if (value
6119 && DECL_P (value)
6120 && DECL_THREAD_LOCAL_P (value))
6121 remove = false;
6124 if (remove)
6125 error_at (OMP_CLAUSE_LOCATION (c),
6126 "copyprivate variable %qE is not threadprivate"
6127 " or private in outer context", DECL_NAME (decl));
6129 do_notice:
6130 if (outer_ctx)
6131 omp_notice_variable (outer_ctx, decl, true);
6132 if (check_non_private
6133 && region_type == ORT_WORKSHARE
6134 && omp_check_private (ctx, decl, false))
6136 error ("%s variable %qE is private in outer context",
6137 check_non_private, DECL_NAME (decl));
6138 remove = true;
6140 break;
6142 case OMP_CLAUSE_FINAL:
6143 case OMP_CLAUSE_IF:
6144 OMP_CLAUSE_OPERAND (c, 0)
6145 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6146 /* Fall through. */
6148 case OMP_CLAUSE_SCHEDULE:
6149 case OMP_CLAUSE_NUM_THREADS:
6150 case OMP_CLAUSE_NUM_TEAMS:
6151 case OMP_CLAUSE_THREAD_LIMIT:
6152 case OMP_CLAUSE_DIST_SCHEDULE:
6153 case OMP_CLAUSE_DEVICE:
6154 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6155 is_gimple_val, fb_rvalue) == GS_ERROR)
6156 remove = true;
6157 break;
6159 case OMP_CLAUSE_NOWAIT:
6160 case OMP_CLAUSE_ORDERED:
6161 case OMP_CLAUSE_UNTIED:
6162 case OMP_CLAUSE_COLLAPSE:
6163 case OMP_CLAUSE_MERGEABLE:
6164 case OMP_CLAUSE_PROC_BIND:
6165 case OMP_CLAUSE_SAFELEN:
6166 break;
6168 case OMP_CLAUSE_ALIGNED:
6169 decl = OMP_CLAUSE_DECL (c);
6170 if (error_operand_p (decl))
6172 remove = true;
6173 break;
6175 if (!is_global_var (decl)
6176 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6177 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6178 break;
6180 case OMP_CLAUSE_DEFAULT:
6181 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6182 break;
6184 default:
6185 gcc_unreachable ();
6188 if (remove)
6189 *list_p = OMP_CLAUSE_CHAIN (c);
6190 else
6191 list_p = &OMP_CLAUSE_CHAIN (c);
6194 gimplify_omp_ctxp = ctx;
6197 /* For all variables that were not actually used within the context,
6198 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6200 static int
6201 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6203 tree *list_p = (tree *) data;
6204 tree decl = (tree) n->key;
6205 unsigned flags = n->value;
6206 enum omp_clause_code code;
6207 tree clause;
6208 bool private_debug;
6210 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6211 return 0;
6212 if ((flags & GOVD_SEEN) == 0)
6213 return 0;
6214 if (flags & GOVD_DEBUG_PRIVATE)
6216 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6217 private_debug = true;
6219 else if (flags & GOVD_MAP)
6220 private_debug = false;
6221 else
6222 private_debug
6223 = lang_hooks.decls.omp_private_debug_clause (decl,
6224 !!(flags & GOVD_SHARED));
6225 if (private_debug)
6226 code = OMP_CLAUSE_PRIVATE;
6227 else if (flags & GOVD_MAP)
6228 code = OMP_CLAUSE_MAP;
6229 else if (flags & GOVD_SHARED)
6231 if (is_global_var (decl))
6233 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6234 while (ctx != NULL)
6236 splay_tree_node on
6237 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6238 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6239 | GOVD_PRIVATE | GOVD_REDUCTION
6240 | GOVD_LINEAR)) != 0)
6241 break;
6242 ctx = ctx->outer_context;
6244 if (ctx == NULL)
6245 return 0;
6247 code = OMP_CLAUSE_SHARED;
6249 else if (flags & GOVD_PRIVATE)
6250 code = OMP_CLAUSE_PRIVATE;
6251 else if (flags & GOVD_FIRSTPRIVATE)
6252 code = OMP_CLAUSE_FIRSTPRIVATE;
6253 else if (flags & GOVD_LASTPRIVATE)
6254 code = OMP_CLAUSE_LASTPRIVATE;
6255 else if (flags & GOVD_ALIGNED)
6256 return 0;
6257 else
6258 gcc_unreachable ();
6260 clause = build_omp_clause (input_location, code);
6261 OMP_CLAUSE_DECL (clause) = decl;
6262 OMP_CLAUSE_CHAIN (clause) = *list_p;
6263 if (private_debug)
6264 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6265 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6266 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6267 else if (code == OMP_CLAUSE_MAP)
6269 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6270 ? OMP_CLAUSE_MAP_TO
6271 : OMP_CLAUSE_MAP_TOFROM;
6272 if (DECL_SIZE (decl)
6273 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6275 tree decl2 = DECL_VALUE_EXPR (decl);
6276 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6277 decl2 = TREE_OPERAND (decl2, 0);
6278 gcc_assert (DECL_P (decl2));
6279 tree mem = build_simple_mem_ref (decl2);
6280 OMP_CLAUSE_DECL (clause) = mem;
6281 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6282 if (gimplify_omp_ctxp->outer_context)
6284 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6285 omp_notice_variable (ctx, decl2, true);
6286 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6288 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6289 OMP_CLAUSE_MAP);
6290 OMP_CLAUSE_DECL (nc) = decl;
6291 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6292 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6293 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6294 OMP_CLAUSE_CHAIN (clause) = nc;
6297 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6299 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6300 OMP_CLAUSE_DECL (nc) = decl;
6301 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6302 OMP_CLAUSE_CHAIN (nc) = *list_p;
6303 OMP_CLAUSE_CHAIN (clause) = nc;
6304 lang_hooks.decls.omp_finish_clause (nc);
6306 *list_p = clause;
6307 lang_hooks.decls.omp_finish_clause (clause);
6308 return 0;
6311 static void
6312 gimplify_adjust_omp_clauses (tree *list_p)
6314 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6315 tree c, decl;
6317 while ((c = *list_p) != NULL)
6319 splay_tree_node n;
6320 bool remove = false;
6322 switch (OMP_CLAUSE_CODE (c))
6324 case OMP_CLAUSE_PRIVATE:
6325 case OMP_CLAUSE_SHARED:
6326 case OMP_CLAUSE_FIRSTPRIVATE:
6327 case OMP_CLAUSE_LINEAR:
6328 decl = OMP_CLAUSE_DECL (c);
6329 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6330 remove = !(n->value & GOVD_SEEN);
6331 if (! remove)
6333 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6334 if ((n->value & GOVD_DEBUG_PRIVATE)
6335 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6337 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6338 || ((n->value & GOVD_DATA_SHARE_CLASS)
6339 == GOVD_PRIVATE));
6340 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6341 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6343 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6344 && ctx->outer_context
6345 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6346 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6348 if (ctx->outer_context->combined_loop
6349 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6351 n = splay_tree_lookup (ctx->outer_context->variables,
6352 (splay_tree_key) decl);
6353 if (n == NULL
6354 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6356 int flags = GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE;
6357 if (n == NULL)
6358 omp_add_variable (ctx->outer_context, decl,
6359 flags | GOVD_SEEN);
6360 else
6361 n->value |= flags | GOVD_SEEN;
6364 else if (!is_global_var (decl))
6365 omp_notice_variable (ctx->outer_context, decl, true);
6368 break;
6370 case OMP_CLAUSE_LASTPRIVATE:
6371 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6372 accurately reflect the presence of a FIRSTPRIVATE clause. */
6373 decl = OMP_CLAUSE_DECL (c);
6374 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6375 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6376 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6377 break;
6379 case OMP_CLAUSE_ALIGNED:
6380 decl = OMP_CLAUSE_DECL (c);
6381 if (!is_global_var (decl))
6383 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6384 remove = n == NULL || !(n->value & GOVD_SEEN);
6385 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6387 struct gimplify_omp_ctx *octx;
6388 if (n != NULL
6389 && (n->value & (GOVD_DATA_SHARE_CLASS
6390 & ~GOVD_FIRSTPRIVATE)))
6391 remove = true;
6392 else
6393 for (octx = ctx->outer_context; octx;
6394 octx = octx->outer_context)
6396 n = splay_tree_lookup (octx->variables,
6397 (splay_tree_key) decl);
6398 if (n == NULL)
6399 continue;
6400 if (n->value & GOVD_LOCAL)
6401 break;
6402 /* We have to avoid assigning a shared variable
6403 to itself when trying to add
6404 __builtin_assume_aligned. */
6405 if (n->value & GOVD_SHARED)
6407 remove = true;
6408 break;
6413 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6415 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6416 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6417 remove = true;
6419 break;
6421 case OMP_CLAUSE_MAP:
6422 decl = OMP_CLAUSE_DECL (c);
6423 if (!DECL_P (decl))
6424 break;
6425 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6426 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6427 remove = true;
6428 else if (DECL_SIZE (decl)
6429 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6430 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6432 tree decl2 = DECL_VALUE_EXPR (decl);
6433 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6434 decl2 = TREE_OPERAND (decl2, 0);
6435 gcc_assert (DECL_P (decl2));
6436 tree mem = build_simple_mem_ref (decl2);
6437 OMP_CLAUSE_DECL (c) = mem;
6438 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6439 if (ctx->outer_context)
6441 omp_notice_variable (ctx->outer_context, decl2, true);
6442 omp_notice_variable (ctx->outer_context,
6443 OMP_CLAUSE_SIZE (c), true);
6445 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6446 OMP_CLAUSE_MAP);
6447 OMP_CLAUSE_DECL (nc) = decl;
6448 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6449 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6450 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6451 OMP_CLAUSE_CHAIN (c) = nc;
6452 c = nc;
6454 break;
6456 case OMP_CLAUSE_TO:
6457 case OMP_CLAUSE_FROM:
6458 decl = OMP_CLAUSE_DECL (c);
6459 if (!DECL_P (decl))
6460 break;
6461 if (DECL_SIZE (decl)
6462 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6464 tree decl2 = DECL_VALUE_EXPR (decl);
6465 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6466 decl2 = TREE_OPERAND (decl2, 0);
6467 gcc_assert (DECL_P (decl2));
6468 tree mem = build_simple_mem_ref (decl2);
6469 OMP_CLAUSE_DECL (c) = mem;
6470 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6471 if (ctx->outer_context)
6473 omp_notice_variable (ctx->outer_context, decl2, true);
6474 omp_notice_variable (ctx->outer_context,
6475 OMP_CLAUSE_SIZE (c), true);
6478 break;
6480 case OMP_CLAUSE_REDUCTION:
6481 case OMP_CLAUSE_COPYIN:
6482 case OMP_CLAUSE_COPYPRIVATE:
6483 case OMP_CLAUSE_IF:
6484 case OMP_CLAUSE_NUM_THREADS:
6485 case OMP_CLAUSE_NUM_TEAMS:
6486 case OMP_CLAUSE_THREAD_LIMIT:
6487 case OMP_CLAUSE_DIST_SCHEDULE:
6488 case OMP_CLAUSE_DEVICE:
6489 case OMP_CLAUSE_SCHEDULE:
6490 case OMP_CLAUSE_NOWAIT:
6491 case OMP_CLAUSE_ORDERED:
6492 case OMP_CLAUSE_DEFAULT:
6493 case OMP_CLAUSE_UNTIED:
6494 case OMP_CLAUSE_COLLAPSE:
6495 case OMP_CLAUSE_FINAL:
6496 case OMP_CLAUSE_MERGEABLE:
6497 case OMP_CLAUSE_PROC_BIND:
6498 case OMP_CLAUSE_SAFELEN:
6499 case OMP_CLAUSE_DEPEND:
6500 break;
6502 default:
6503 gcc_unreachable ();
6506 if (remove)
6507 *list_p = OMP_CLAUSE_CHAIN (c);
6508 else
6509 list_p = &OMP_CLAUSE_CHAIN (c);
6512 /* Add in any implicit data sharing. */
6513 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6515 gimplify_omp_ctxp = ctx->outer_context;
6516 delete_omp_context (ctx);
6519 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6520 gimplification of the body, as well as scanning the body for used
6521 variables. We need to do this scan now, because variable-sized
6522 decls will be decomposed during gimplification. */
6524 static void
6525 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6527 tree expr = *expr_p;
6528 gimple g;
6529 gimple_seq body = NULL;
6531 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6532 OMP_PARALLEL_COMBINED (expr)
6533 ? ORT_COMBINED_PARALLEL
6534 : ORT_PARALLEL);
6536 push_gimplify_context ();
6538 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6539 if (gimple_code (g) == GIMPLE_BIND)
6540 pop_gimplify_context (g);
6541 else
6542 pop_gimplify_context (NULL);
6544 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6546 g = gimple_build_omp_parallel (body,
6547 OMP_PARALLEL_CLAUSES (expr),
6548 NULL_TREE, NULL_TREE);
6549 if (OMP_PARALLEL_COMBINED (expr))
6550 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6551 gimplify_seq_add_stmt (pre_p, g);
6552 *expr_p = NULL_TREE;
6555 /* Gimplify the contents of an OMP_TASK statement. This involves
6556 gimplification of the body, as well as scanning the body for used
6557 variables. We need to do this scan now, because variable-sized
6558 decls will be decomposed during gimplification. */
6560 static void
6561 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6563 tree expr = *expr_p;
6564 gimple g;
6565 gimple_seq body = NULL;
6567 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6568 find_omp_clause (OMP_TASK_CLAUSES (expr),
6569 OMP_CLAUSE_UNTIED)
6570 ? ORT_UNTIED_TASK : ORT_TASK);
6572 push_gimplify_context ();
6574 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6575 if (gimple_code (g) == GIMPLE_BIND)
6576 pop_gimplify_context (g);
6577 else
6578 pop_gimplify_context (NULL);
6580 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6582 g = gimple_build_omp_task (body,
6583 OMP_TASK_CLAUSES (expr),
6584 NULL_TREE, NULL_TREE,
6585 NULL_TREE, NULL_TREE, NULL_TREE);
6586 gimplify_seq_add_stmt (pre_p, g);
6587 *expr_p = NULL_TREE;
6590 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6591 with non-NULL OMP_FOR_INIT. */
6593 static tree
6594 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6596 *walk_subtrees = 0;
6597 switch (TREE_CODE (*tp))
6599 case OMP_FOR:
6600 *walk_subtrees = 1;
6601 /* FALLTHRU */
6602 case OMP_SIMD:
6603 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6604 return *tp;
6605 break;
6606 case BIND_EXPR:
6607 case STATEMENT_LIST:
6608 case OMP_PARALLEL:
6609 *walk_subtrees = 1;
6610 break;
6611 default:
6612 break;
6614 return NULL_TREE;
6617 /* Gimplify the gross structure of an OMP_FOR statement. */
6619 static enum gimplify_status
6620 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6622 tree for_stmt, orig_for_stmt, decl, var, t;
6623 enum gimplify_status ret = GS_ALL_DONE;
6624 enum gimplify_status tret;
6625 gimple gfor;
6626 gimple_seq for_body, for_pre_body;
6627 int i;
6628 bool simd;
6629 bitmap has_decl_expr = NULL;
6631 orig_for_stmt = for_stmt = *expr_p;
6633 simd = (TREE_CODE (for_stmt) == OMP_SIMD
6634 || TREE_CODE (for_stmt) == CILK_SIMD);
6635 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6636 simd ? ORT_SIMD : ORT_WORKSHARE);
6638 /* Handle OMP_FOR_INIT. */
6639 for_pre_body = NULL;
6640 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6642 has_decl_expr = BITMAP_ALLOC (NULL);
6643 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6644 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6645 == VAR_DECL)
6647 t = OMP_FOR_PRE_BODY (for_stmt);
6648 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6650 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6652 tree_stmt_iterator si;
6653 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6654 tsi_next (&si))
6656 t = tsi_stmt (si);
6657 if (TREE_CODE (t) == DECL_EXPR
6658 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6659 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6663 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6664 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6666 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6668 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6669 NULL, NULL);
6670 gcc_assert (for_stmt != NULL_TREE);
6671 gimplify_omp_ctxp->combined_loop = true;
6674 for_body = NULL;
6675 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6676 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6677 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6678 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6679 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6681 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6682 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6683 decl = TREE_OPERAND (t, 0);
6684 gcc_assert (DECL_P (decl));
6685 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6686 || POINTER_TYPE_P (TREE_TYPE (decl)));
6688 /* Make sure the iteration variable is private. */
6689 tree c = NULL_TREE;
6690 tree c2 = NULL_TREE;
6691 if (orig_for_stmt != for_stmt)
6692 /* Do this only on innermost construct for combined ones. */;
6693 else if (simd)
6695 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6696 (splay_tree_key)decl);
6697 omp_is_private (gimplify_omp_ctxp, decl,
6698 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6699 != 1));
6700 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6701 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6702 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6704 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6705 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6706 if (has_decl_expr
6707 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6708 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6709 OMP_CLAUSE_DECL (c) = decl;
6710 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6711 OMP_FOR_CLAUSES (for_stmt) = c;
6712 omp_add_variable (gimplify_omp_ctxp, decl,
6713 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6715 else
6717 bool lastprivate
6718 = (!has_decl_expr
6719 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6720 c = build_omp_clause (input_location,
6721 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6722 : OMP_CLAUSE_PRIVATE);
6723 OMP_CLAUSE_DECL (c) = decl;
6724 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6725 OMP_FOR_CLAUSES (for_stmt) = c;
6726 omp_add_variable (gimplify_omp_ctxp, decl,
6727 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6728 | GOVD_EXPLICIT | GOVD_SEEN);
6729 c = NULL_TREE;
6732 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6733 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6734 else
6735 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6737 /* If DECL is not a gimple register, create a temporary variable to act
6738 as an iteration counter. This is valid, since DECL cannot be
6739 modified in the body of the loop. */
6740 if (orig_for_stmt != for_stmt)
6741 var = decl;
6742 else if (!is_gimple_reg (decl))
6744 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6745 TREE_OPERAND (t, 0) = var;
6747 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6749 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6751 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6752 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
6753 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
6754 OMP_CLAUSE_DECL (c2) = var;
6755 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
6756 OMP_FOR_CLAUSES (for_stmt) = c2;
6757 omp_add_variable (gimplify_omp_ctxp, var,
6758 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6759 if (c == NULL_TREE)
6761 c = c2;
6762 c2 = NULL_TREE;
6765 else
6766 omp_add_variable (gimplify_omp_ctxp, var,
6767 GOVD_PRIVATE | GOVD_SEEN);
6769 else
6770 var = decl;
6772 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6773 is_gimple_val, fb_rvalue);
6774 ret = MIN (ret, tret);
6775 if (ret == GS_ERROR)
6776 return ret;
6778 /* Handle OMP_FOR_COND. */
6779 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6780 gcc_assert (COMPARISON_CLASS_P (t));
6781 gcc_assert (TREE_OPERAND (t, 0) == decl);
6783 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6784 is_gimple_val, fb_rvalue);
6785 ret = MIN (ret, tret);
6787 /* Handle OMP_FOR_INCR. */
6788 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6789 switch (TREE_CODE (t))
6791 case PREINCREMENT_EXPR:
6792 case POSTINCREMENT_EXPR:
6794 tree decl = TREE_OPERAND (t, 0);
6795 // c_omp_for_incr_canonicalize_ptr() should have been
6796 // called to massage things appropriately.
6797 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6799 if (orig_for_stmt != for_stmt)
6800 break;
6801 t = build_int_cst (TREE_TYPE (decl), 1);
6802 if (c)
6803 OMP_CLAUSE_LINEAR_STEP (c) = t;
6804 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6805 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6806 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6807 break;
6810 case PREDECREMENT_EXPR:
6811 case POSTDECREMENT_EXPR:
6812 if (orig_for_stmt != for_stmt)
6813 break;
6814 t = build_int_cst (TREE_TYPE (decl), -1);
6815 if (c)
6816 OMP_CLAUSE_LINEAR_STEP (c) = t;
6817 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6818 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6819 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6820 break;
6822 case MODIFY_EXPR:
6823 gcc_assert (TREE_OPERAND (t, 0) == decl);
6824 TREE_OPERAND (t, 0) = var;
6826 t = TREE_OPERAND (t, 1);
6827 switch (TREE_CODE (t))
6829 case PLUS_EXPR:
6830 if (TREE_OPERAND (t, 1) == decl)
6832 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6833 TREE_OPERAND (t, 0) = var;
6834 break;
6837 /* Fallthru. */
6838 case MINUS_EXPR:
6839 case POINTER_PLUS_EXPR:
6840 gcc_assert (TREE_OPERAND (t, 0) == decl);
6841 TREE_OPERAND (t, 0) = var;
6842 break;
6843 default:
6844 gcc_unreachable ();
6847 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6848 is_gimple_val, fb_rvalue);
6849 ret = MIN (ret, tret);
6850 if (c)
6852 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
6853 if (TREE_CODE (t) == MINUS_EXPR)
6855 t = TREE_OPERAND (t, 1);
6856 OMP_CLAUSE_LINEAR_STEP (c)
6857 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
6858 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6859 &for_pre_body, NULL,
6860 is_gimple_val, fb_rvalue);
6861 ret = MIN (ret, tret);
6864 break;
6866 default:
6867 gcc_unreachable ();
6870 if (c2)
6872 gcc_assert (c);
6873 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
6876 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6877 && orig_for_stmt == for_stmt)
6879 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6880 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6881 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6882 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6883 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
6884 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
6885 && OMP_CLAUSE_DECL (c) == decl)
6887 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6888 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6889 gcc_assert (TREE_OPERAND (t, 0) == var);
6890 t = TREE_OPERAND (t, 1);
6891 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6892 || TREE_CODE (t) == MINUS_EXPR
6893 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6894 gcc_assert (TREE_OPERAND (t, 0) == var);
6895 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6896 TREE_OPERAND (t, 1));
6897 gimple_seq *seq;
6898 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
6899 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
6900 else
6901 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
6902 gimplify_assign (decl, t, seq);
6907 BITMAP_FREE (has_decl_expr);
6909 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
6911 if (orig_for_stmt != for_stmt)
6912 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6914 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6915 decl = TREE_OPERAND (t, 0);
6916 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6917 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6918 TREE_OPERAND (t, 0) = var;
6919 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6920 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
6921 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
6924 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
6926 int kind;
6927 switch (TREE_CODE (orig_for_stmt))
6929 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
6930 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
6931 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
6932 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
6933 default:
6934 gcc_unreachable ();
6936 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
6937 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6938 for_pre_body);
6939 if (orig_for_stmt != for_stmt)
6940 gimple_omp_for_set_combined_p (gfor, true);
6941 if (gimplify_omp_ctxp
6942 && (gimplify_omp_ctxp->combined_loop
6943 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
6944 && gimplify_omp_ctxp->outer_context
6945 && gimplify_omp_ctxp->outer_context->combined_loop)))
6947 gimple_omp_for_set_combined_into_p (gfor, true);
6948 if (gimplify_omp_ctxp->combined_loop)
6949 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
6950 else
6951 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
6954 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6956 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6957 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6958 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6959 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6960 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6961 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6962 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6963 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6966 gimplify_seq_add_stmt (pre_p, gfor);
6967 if (ret != GS_ALL_DONE)
6968 return GS_ERROR;
6969 *expr_p = NULL_TREE;
6970 return GS_ALL_DONE;
6973 /* Gimplify the gross structure of other OpenMP constructs.
6974 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
6975 and OMP_TEAMS. */
6977 static void
6978 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6980 tree expr = *expr_p;
6981 gimple stmt;
6982 gimple_seq body = NULL;
6983 enum omp_region_type ort = ORT_WORKSHARE;
6985 switch (TREE_CODE (expr))
6987 case OMP_SECTIONS:
6988 case OMP_SINGLE:
6989 break;
6990 case OMP_TARGET:
6991 ort = ORT_TARGET;
6992 break;
6993 case OMP_TARGET_DATA:
6994 ort = ORT_TARGET_DATA;
6995 break;
6996 case OMP_TEAMS:
6997 ort = ORT_TEAMS;
6998 break;
6999 default:
7000 gcc_unreachable ();
7002 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7003 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7005 push_gimplify_context ();
7006 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7007 if (gimple_code (g) == GIMPLE_BIND)
7008 pop_gimplify_context (g);
7009 else
7010 pop_gimplify_context (NULL);
7011 if (ort == ORT_TARGET_DATA)
7013 gimple_seq cleanup = NULL;
7014 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
7015 g = gimple_build_call (fn, 0);
7016 gimple_seq_add_stmt (&cleanup, g);
7017 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7018 body = NULL;
7019 gimple_seq_add_stmt (&body, g);
7022 else
7023 gimplify_and_add (OMP_BODY (expr), &body);
7024 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
7026 switch (TREE_CODE (expr))
7028 case OMP_SECTIONS:
7029 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7030 break;
7031 case OMP_SINGLE:
7032 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7033 break;
7034 case OMP_TARGET:
7035 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7036 OMP_CLAUSES (expr));
7037 break;
7038 case OMP_TARGET_DATA:
7039 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7040 OMP_CLAUSES (expr));
7041 break;
7042 case OMP_TEAMS:
7043 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7044 break;
7045 default:
7046 gcc_unreachable ();
7049 gimplify_seq_add_stmt (pre_p, stmt);
7050 *expr_p = NULL_TREE;
7053 /* Gimplify the gross structure of OpenMP target update construct. */
7055 static void
7056 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7058 tree expr = *expr_p;
7059 gimple stmt;
7061 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7062 ORT_WORKSHARE);
7063 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
7064 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7065 OMP_TARGET_UPDATE_CLAUSES (expr));
7067 gimplify_seq_add_stmt (pre_p, stmt);
7068 *expr_p = NULL_TREE;
7071 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7072 stabilized the lhs of the atomic operation as *ADDR. Return true if
7073 EXPR is this stabilized form. */
7075 static bool
7076 goa_lhs_expr_p (tree expr, tree addr)
7078 /* Also include casts to other type variants. The C front end is fond
7079 of adding these for e.g. volatile variables. This is like
7080 STRIP_TYPE_NOPS but includes the main variant lookup. */
7081 STRIP_USELESS_TYPE_CONVERSION (expr);
7083 if (TREE_CODE (expr) == INDIRECT_REF)
7085 expr = TREE_OPERAND (expr, 0);
7086 while (expr != addr
7087 && (CONVERT_EXPR_P (expr)
7088 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7089 && TREE_CODE (expr) == TREE_CODE (addr)
7090 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7092 expr = TREE_OPERAND (expr, 0);
7093 addr = TREE_OPERAND (addr, 0);
7095 if (expr == addr)
7096 return true;
7097 return (TREE_CODE (addr) == ADDR_EXPR
7098 && TREE_CODE (expr) == ADDR_EXPR
7099 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7101 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7102 return true;
7103 return false;
7106 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7107 expression does not involve the lhs, evaluate it into a temporary.
7108 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7109 or -1 if an error was encountered. */
7111 static int
7112 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7113 tree lhs_var)
7115 tree expr = *expr_p;
7116 int saw_lhs;
7118 if (goa_lhs_expr_p (expr, lhs_addr))
7120 *expr_p = lhs_var;
7121 return 1;
7123 if (is_gimple_val (expr))
7124 return 0;
7126 saw_lhs = 0;
7127 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7129 case tcc_binary:
7130 case tcc_comparison:
7131 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7132 lhs_var);
7133 case tcc_unary:
7134 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7135 lhs_var);
7136 break;
7137 case tcc_expression:
7138 switch (TREE_CODE (expr))
7140 case TRUTH_ANDIF_EXPR:
7141 case TRUTH_ORIF_EXPR:
7142 case TRUTH_AND_EXPR:
7143 case TRUTH_OR_EXPR:
7144 case TRUTH_XOR_EXPR:
7145 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7146 lhs_addr, lhs_var);
7147 case TRUTH_NOT_EXPR:
7148 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7149 lhs_addr, lhs_var);
7150 break;
7151 case COMPOUND_EXPR:
7152 /* Break out any preevaluations from cp_build_modify_expr. */
7153 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7154 expr = TREE_OPERAND (expr, 1))
7155 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7156 *expr_p = expr;
7157 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7158 default:
7159 break;
7161 break;
7162 default:
7163 break;
7166 if (saw_lhs == 0)
7168 enum gimplify_status gs;
7169 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7170 if (gs != GS_ALL_DONE)
7171 saw_lhs = -1;
7174 return saw_lhs;
7177 /* Gimplify an OMP_ATOMIC statement. */
7179 static enum gimplify_status
7180 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7182 tree addr = TREE_OPERAND (*expr_p, 0);
7183 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7184 ? NULL : TREE_OPERAND (*expr_p, 1);
7185 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7186 tree tmp_load;
7187 gimple loadstmt, storestmt;
7189 tmp_load = create_tmp_reg (type, NULL);
7190 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7191 return GS_ERROR;
7193 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7194 != GS_ALL_DONE)
7195 return GS_ERROR;
7197 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7198 gimplify_seq_add_stmt (pre_p, loadstmt);
7199 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7200 != GS_ALL_DONE)
7201 return GS_ERROR;
7203 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7204 rhs = tmp_load;
7205 storestmt = gimple_build_omp_atomic_store (rhs);
7206 gimplify_seq_add_stmt (pre_p, storestmt);
7207 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7209 gimple_omp_atomic_set_seq_cst (loadstmt);
7210 gimple_omp_atomic_set_seq_cst (storestmt);
7212 switch (TREE_CODE (*expr_p))
7214 case OMP_ATOMIC_READ:
7215 case OMP_ATOMIC_CAPTURE_OLD:
7216 *expr_p = tmp_load;
7217 gimple_omp_atomic_set_need_value (loadstmt);
7218 break;
7219 case OMP_ATOMIC_CAPTURE_NEW:
7220 *expr_p = rhs;
7221 gimple_omp_atomic_set_need_value (storestmt);
7222 break;
7223 default:
7224 *expr_p = NULL;
7225 break;
7228 return GS_ALL_DONE;
7231 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7232 body, and adding some EH bits. */
7234 static enum gimplify_status
7235 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7237 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7238 gimple g;
7239 gimple_seq body = NULL;
7240 int subcode = 0;
7242 /* Wrap the transaction body in a BIND_EXPR so we have a context
7243 where to put decls for OpenMP. */
7244 if (TREE_CODE (tbody) != BIND_EXPR)
7246 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7247 TREE_SIDE_EFFECTS (bind) = 1;
7248 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7249 TRANSACTION_EXPR_BODY (expr) = bind;
7252 push_gimplify_context ();
7253 temp = voidify_wrapper_expr (*expr_p, NULL);
7255 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7256 pop_gimplify_context (g);
7258 g = gimple_build_transaction (body, NULL);
7259 if (TRANSACTION_EXPR_OUTER (expr))
7260 subcode = GTMA_IS_OUTER;
7261 else if (TRANSACTION_EXPR_RELAXED (expr))
7262 subcode = GTMA_IS_RELAXED;
7263 gimple_transaction_set_subcode (g, subcode);
7265 gimplify_seq_add_stmt (pre_p, g);
7267 if (temp)
7269 *expr_p = temp;
7270 return GS_OK;
7273 *expr_p = NULL_TREE;
7274 return GS_ALL_DONE;
7277 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7278 expression produces a value to be used as an operand inside a GIMPLE
7279 statement, the value will be stored back in *EXPR_P. This value will
7280 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7281 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7282 emitted in PRE_P and POST_P.
7284 Additionally, this process may overwrite parts of the input
7285 expression during gimplification. Ideally, it should be
7286 possible to do non-destructive gimplification.
7288 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7289 the expression needs to evaluate to a value to be used as
7290 an operand in a GIMPLE statement, this value will be stored in
7291 *EXPR_P on exit. This happens when the caller specifies one
7292 of fb_lvalue or fb_rvalue fallback flags.
7294 PRE_P will contain the sequence of GIMPLE statements corresponding
7295 to the evaluation of EXPR and all the side-effects that must
7296 be executed before the main expression. On exit, the last
7297 statement of PRE_P is the core statement being gimplified. For
7298 instance, when gimplifying 'if (++a)' the last statement in
7299 PRE_P will be 'if (t.1)' where t.1 is the result of
7300 pre-incrementing 'a'.
7302 POST_P will contain the sequence of GIMPLE statements corresponding
7303 to the evaluation of all the side-effects that must be executed
7304 after the main expression. If this is NULL, the post
7305 side-effects are stored at the end of PRE_P.
7307 The reason why the output is split in two is to handle post
7308 side-effects explicitly. In some cases, an expression may have
7309 inner and outer post side-effects which need to be emitted in
7310 an order different from the one given by the recursive
7311 traversal. For instance, for the expression (*p--)++ the post
7312 side-effects of '--' must actually occur *after* the post
7313 side-effects of '++'. However, gimplification will first visit
7314 the inner expression, so if a separate POST sequence was not
7315 used, the resulting sequence would be:
7317 1 t.1 = *p
7318 2 p = p - 1
7319 3 t.2 = t.1 + 1
7320 4 *p = t.2
7322 However, the post-decrement operation in line #2 must not be
7323 evaluated until after the store to *p at line #4, so the
7324 correct sequence should be:
7326 1 t.1 = *p
7327 2 t.2 = t.1 + 1
7328 3 *p = t.2
7329 4 p = p - 1
7331 So, by specifying a separate post queue, it is possible
7332 to emit the post side-effects in the correct order.
7333 If POST_P is NULL, an internal queue will be used. Before
7334 returning to the caller, the sequence POST_P is appended to
7335 the main output sequence PRE_P.
7337 GIMPLE_TEST_F points to a function that takes a tree T and
7338 returns nonzero if T is in the GIMPLE form requested by the
7339 caller. The GIMPLE predicates are in gimple.c.
7341 FALLBACK tells the function what sort of a temporary we want if
7342 gimplification cannot produce an expression that complies with
7343 GIMPLE_TEST_F.
7345 fb_none means that no temporary should be generated
7346 fb_rvalue means that an rvalue is OK to generate
7347 fb_lvalue means that an lvalue is OK to generate
7348 fb_either means that either is OK, but an lvalue is preferable.
7349 fb_mayfail means that gimplification may fail (in which case
7350 GS_ERROR will be returned)
7352 The return value is either GS_ERROR or GS_ALL_DONE, since this
7353 function iterates until EXPR is completely gimplified or an error
7354 occurs. */
7356 enum gimplify_status
7357 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7358 bool (*gimple_test_f) (tree), fallback_t fallback)
7360 tree tmp;
7361 gimple_seq internal_pre = NULL;
7362 gimple_seq internal_post = NULL;
7363 tree save_expr;
7364 bool is_statement;
7365 location_t saved_location;
7366 enum gimplify_status ret;
7367 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7369 save_expr = *expr_p;
7370 if (save_expr == NULL_TREE)
7371 return GS_ALL_DONE;
7373 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7374 is_statement = gimple_test_f == is_gimple_stmt;
7375 if (is_statement)
7376 gcc_assert (pre_p);
7378 /* Consistency checks. */
7379 if (gimple_test_f == is_gimple_reg)
7380 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7381 else if (gimple_test_f == is_gimple_val
7382 || gimple_test_f == is_gimple_call_addr
7383 || gimple_test_f == is_gimple_condexpr
7384 || gimple_test_f == is_gimple_mem_rhs
7385 || gimple_test_f == is_gimple_mem_rhs_or_call
7386 || gimple_test_f == is_gimple_reg_rhs
7387 || gimple_test_f == is_gimple_reg_rhs_or_call
7388 || gimple_test_f == is_gimple_asm_val
7389 || gimple_test_f == is_gimple_mem_ref_addr)
7390 gcc_assert (fallback & fb_rvalue);
7391 else if (gimple_test_f == is_gimple_min_lval
7392 || gimple_test_f == is_gimple_lvalue)
7393 gcc_assert (fallback & fb_lvalue);
7394 else if (gimple_test_f == is_gimple_addressable)
7395 gcc_assert (fallback & fb_either);
7396 else if (gimple_test_f == is_gimple_stmt)
7397 gcc_assert (fallback == fb_none);
7398 else
7400 /* We should have recognized the GIMPLE_TEST_F predicate to
7401 know what kind of fallback to use in case a temporary is
7402 needed to hold the value or address of *EXPR_P. */
7403 gcc_unreachable ();
7406 /* We used to check the predicate here and return immediately if it
7407 succeeds. This is wrong; the design is for gimplification to be
7408 idempotent, and for the predicates to only test for valid forms, not
7409 whether they are fully simplified. */
7410 if (pre_p == NULL)
7411 pre_p = &internal_pre;
7413 if (post_p == NULL)
7414 post_p = &internal_post;
7416 /* Remember the last statements added to PRE_P and POST_P. Every
7417 new statement added by the gimplification helpers needs to be
7418 annotated with location information. To centralize the
7419 responsibility, we remember the last statement that had been
7420 added to both queues before gimplifying *EXPR_P. If
7421 gimplification produces new statements in PRE_P and POST_P, those
7422 statements will be annotated with the same location information
7423 as *EXPR_P. */
7424 pre_last_gsi = gsi_last (*pre_p);
7425 post_last_gsi = gsi_last (*post_p);
7427 saved_location = input_location;
7428 if (save_expr != error_mark_node
7429 && EXPR_HAS_LOCATION (*expr_p))
7430 input_location = EXPR_LOCATION (*expr_p);
7432 /* Loop over the specific gimplifiers until the toplevel node
7433 remains the same. */
7436 /* Strip away as many useless type conversions as possible
7437 at the toplevel. */
7438 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7440 /* Remember the expr. */
7441 save_expr = *expr_p;
7443 /* Die, die, die, my darling. */
7444 if (save_expr == error_mark_node
7445 || (TREE_TYPE (save_expr)
7446 && TREE_TYPE (save_expr) == error_mark_node))
7448 ret = GS_ERROR;
7449 break;
7452 /* Do any language-specific gimplification. */
7453 ret = ((enum gimplify_status)
7454 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7455 if (ret == GS_OK)
7457 if (*expr_p == NULL_TREE)
7458 break;
7459 if (*expr_p != save_expr)
7460 continue;
7462 else if (ret != GS_UNHANDLED)
7463 break;
7465 /* Make sure that all the cases set 'ret' appropriately. */
7466 ret = GS_UNHANDLED;
7467 switch (TREE_CODE (*expr_p))
7469 /* First deal with the special cases. */
7471 case POSTINCREMENT_EXPR:
7472 case POSTDECREMENT_EXPR:
7473 case PREINCREMENT_EXPR:
7474 case PREDECREMENT_EXPR:
7475 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7476 fallback != fb_none,
7477 TREE_TYPE (*expr_p));
7478 break;
7480 case VIEW_CONVERT_EXPR:
7481 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7482 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7484 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7485 post_p, is_gimple_val, fb_rvalue);
7486 recalculate_side_effects (*expr_p);
7487 break;
7489 /* Fallthru. */
7491 case ARRAY_REF:
7492 case ARRAY_RANGE_REF:
7493 case REALPART_EXPR:
7494 case IMAGPART_EXPR:
7495 case COMPONENT_REF:
7496 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7497 fallback ? fallback : fb_rvalue);
7498 break;
7500 case COND_EXPR:
7501 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7503 /* C99 code may assign to an array in a structure value of a
7504 conditional expression, and this has undefined behavior
7505 only on execution, so create a temporary if an lvalue is
7506 required. */
7507 if (fallback == fb_lvalue)
7509 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7510 mark_addressable (*expr_p);
7511 ret = GS_OK;
7513 break;
7515 case CALL_EXPR:
7516 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7518 /* C99 code may assign to an array in a structure returned
7519 from a function, and this has undefined behavior only on
7520 execution, so create a temporary if an lvalue is
7521 required. */
7522 if (fallback == fb_lvalue)
7524 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7525 mark_addressable (*expr_p);
7526 ret = GS_OK;
7528 break;
7530 case TREE_LIST:
7531 gcc_unreachable ();
7533 case COMPOUND_EXPR:
7534 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7535 break;
7537 case COMPOUND_LITERAL_EXPR:
7538 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7539 gimple_test_f, fallback);
7540 break;
7542 case MODIFY_EXPR:
7543 case INIT_EXPR:
7544 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7545 fallback != fb_none);
7546 break;
7548 case TRUTH_ANDIF_EXPR:
7549 case TRUTH_ORIF_EXPR:
7551 /* Preserve the original type of the expression and the
7552 source location of the outer expression. */
7553 tree org_type = TREE_TYPE (*expr_p);
7554 *expr_p = gimple_boolify (*expr_p);
7555 *expr_p = build3_loc (input_location, COND_EXPR,
7556 org_type, *expr_p,
7557 fold_convert_loc
7558 (input_location,
7559 org_type, boolean_true_node),
7560 fold_convert_loc
7561 (input_location,
7562 org_type, boolean_false_node));
7563 ret = GS_OK;
7564 break;
7567 case TRUTH_NOT_EXPR:
7569 tree type = TREE_TYPE (*expr_p);
7570 /* The parsers are careful to generate TRUTH_NOT_EXPR
7571 only with operands that are always zero or one.
7572 We do not fold here but handle the only interesting case
7573 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7574 *expr_p = gimple_boolify (*expr_p);
7575 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7576 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7577 TREE_TYPE (*expr_p),
7578 TREE_OPERAND (*expr_p, 0));
7579 else
7580 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7581 TREE_TYPE (*expr_p),
7582 TREE_OPERAND (*expr_p, 0),
7583 build_int_cst (TREE_TYPE (*expr_p), 1));
7584 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7585 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7586 ret = GS_OK;
7587 break;
7590 case ADDR_EXPR:
7591 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7592 break;
7594 case ANNOTATE_EXPR:
7596 tree cond = TREE_OPERAND (*expr_p, 0);
7597 tree kind = TREE_OPERAND (*expr_p, 1);
7598 tree type = TREE_TYPE (cond);
7599 if (!INTEGRAL_TYPE_P (type))
7601 *expr_p = cond;
7602 ret = GS_OK;
7603 break;
7605 tree tmp = create_tmp_var (type, NULL);
7606 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7607 gimple call
7608 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7609 gimple_call_set_lhs (call, tmp);
7610 gimplify_seq_add_stmt (pre_p, call);
7611 *expr_p = tmp;
7612 ret = GS_ALL_DONE;
7613 break;
7616 case VA_ARG_EXPR:
7617 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7618 break;
7620 CASE_CONVERT:
7621 if (IS_EMPTY_STMT (*expr_p))
7623 ret = GS_ALL_DONE;
7624 break;
7627 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7628 || fallback == fb_none)
7630 /* Just strip a conversion to void (or in void context) and
7631 try again. */
7632 *expr_p = TREE_OPERAND (*expr_p, 0);
7633 ret = GS_OK;
7634 break;
7637 ret = gimplify_conversion (expr_p);
7638 if (ret == GS_ERROR)
7639 break;
7640 if (*expr_p != save_expr)
7641 break;
7642 /* FALLTHRU */
7644 case FIX_TRUNC_EXPR:
7645 /* unary_expr: ... | '(' cast ')' val | ... */
7646 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7647 is_gimple_val, fb_rvalue);
7648 recalculate_side_effects (*expr_p);
7649 break;
7651 case INDIRECT_REF:
7653 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7654 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7655 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7657 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7658 if (*expr_p != save_expr)
7660 ret = GS_OK;
7661 break;
7664 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7665 is_gimple_reg, fb_rvalue);
7666 if (ret == GS_ERROR)
7667 break;
7669 recalculate_side_effects (*expr_p);
7670 *expr_p = fold_build2_loc (input_location, MEM_REF,
7671 TREE_TYPE (*expr_p),
7672 TREE_OPERAND (*expr_p, 0),
7673 build_int_cst (saved_ptr_type, 0));
7674 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7675 TREE_THIS_NOTRAP (*expr_p) = notrap;
7676 ret = GS_OK;
7677 break;
7680 /* We arrive here through the various re-gimplifcation paths. */
7681 case MEM_REF:
7682 /* First try re-folding the whole thing. */
7683 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7684 TREE_OPERAND (*expr_p, 0),
7685 TREE_OPERAND (*expr_p, 1));
7686 if (tmp)
7688 *expr_p = tmp;
7689 recalculate_side_effects (*expr_p);
7690 ret = GS_OK;
7691 break;
7693 /* Avoid re-gimplifying the address operand if it is already
7694 in suitable form. Re-gimplifying would mark the address
7695 operand addressable. Always gimplify when not in SSA form
7696 as we still may have to gimplify decls with value-exprs. */
7697 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7698 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7700 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7701 is_gimple_mem_ref_addr, fb_rvalue);
7702 if (ret == GS_ERROR)
7703 break;
7705 recalculate_side_effects (*expr_p);
7706 ret = GS_ALL_DONE;
7707 break;
7709 /* Constants need not be gimplified. */
7710 case INTEGER_CST:
7711 case REAL_CST:
7712 case FIXED_CST:
7713 case STRING_CST:
7714 case COMPLEX_CST:
7715 case VECTOR_CST:
7716 /* Drop the overflow flag on constants, we do not want
7717 that in the GIMPLE IL. */
7718 if (TREE_OVERFLOW_P (*expr_p))
7719 *expr_p = drop_tree_overflow (*expr_p);
7720 ret = GS_ALL_DONE;
7721 break;
7723 case CONST_DECL:
7724 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7725 CONST_DECL node. Otherwise the decl is replaceable by its
7726 value. */
7727 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7728 if (fallback & fb_lvalue)
7729 ret = GS_ALL_DONE;
7730 else
7732 *expr_p = DECL_INITIAL (*expr_p);
7733 ret = GS_OK;
7735 break;
7737 case DECL_EXPR:
7738 ret = gimplify_decl_expr (expr_p, pre_p);
7739 break;
7741 case BIND_EXPR:
7742 ret = gimplify_bind_expr (expr_p, pre_p);
7743 break;
7745 case LOOP_EXPR:
7746 ret = gimplify_loop_expr (expr_p, pre_p);
7747 break;
7749 case SWITCH_EXPR:
7750 ret = gimplify_switch_expr (expr_p, pre_p);
7751 break;
7753 case EXIT_EXPR:
7754 ret = gimplify_exit_expr (expr_p);
7755 break;
7757 case GOTO_EXPR:
7758 /* If the target is not LABEL, then it is a computed jump
7759 and the target needs to be gimplified. */
7760 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7762 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7763 NULL, is_gimple_val, fb_rvalue);
7764 if (ret == GS_ERROR)
7765 break;
7767 gimplify_seq_add_stmt (pre_p,
7768 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7769 ret = GS_ALL_DONE;
7770 break;
7772 case PREDICT_EXPR:
7773 gimplify_seq_add_stmt (pre_p,
7774 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7775 PREDICT_EXPR_OUTCOME (*expr_p)));
7776 ret = GS_ALL_DONE;
7777 break;
7779 case LABEL_EXPR:
7780 ret = GS_ALL_DONE;
7781 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7782 == current_function_decl);
7783 gimplify_seq_add_stmt (pre_p,
7784 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7785 break;
7787 case CASE_LABEL_EXPR:
7788 ret = gimplify_case_label_expr (expr_p, pre_p);
7789 break;
7791 case RETURN_EXPR:
7792 ret = gimplify_return_expr (*expr_p, pre_p);
7793 break;
7795 case CONSTRUCTOR:
7796 /* Don't reduce this in place; let gimplify_init_constructor work its
7797 magic. Buf if we're just elaborating this for side effects, just
7798 gimplify any element that has side-effects. */
7799 if (fallback == fb_none)
7801 unsigned HOST_WIDE_INT ix;
7802 tree val;
7803 tree temp = NULL_TREE;
7804 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7805 if (TREE_SIDE_EFFECTS (val))
7806 append_to_statement_list (val, &temp);
7808 *expr_p = temp;
7809 ret = temp ? GS_OK : GS_ALL_DONE;
7811 /* C99 code may assign to an array in a constructed
7812 structure or union, and this has undefined behavior only
7813 on execution, so create a temporary if an lvalue is
7814 required. */
7815 else if (fallback == fb_lvalue)
7817 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7818 mark_addressable (*expr_p);
7819 ret = GS_OK;
7821 else
7822 ret = GS_ALL_DONE;
7823 break;
7825 /* The following are special cases that are not handled by the
7826 original GIMPLE grammar. */
7828 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7829 eliminated. */
7830 case SAVE_EXPR:
7831 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7832 break;
7834 case BIT_FIELD_REF:
7835 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7836 post_p, is_gimple_lvalue, fb_either);
7837 recalculate_side_effects (*expr_p);
7838 break;
7840 case TARGET_MEM_REF:
7842 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7844 if (TMR_BASE (*expr_p))
7845 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7846 post_p, is_gimple_mem_ref_addr, fb_either);
7847 if (TMR_INDEX (*expr_p))
7848 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7849 post_p, is_gimple_val, fb_rvalue);
7850 if (TMR_INDEX2 (*expr_p))
7851 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7852 post_p, is_gimple_val, fb_rvalue);
7853 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7854 ret = MIN (r0, r1);
7856 break;
7858 case NON_LVALUE_EXPR:
7859 /* This should have been stripped above. */
7860 gcc_unreachable ();
7862 case ASM_EXPR:
7863 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7864 break;
7866 case TRY_FINALLY_EXPR:
7867 case TRY_CATCH_EXPR:
7869 gimple_seq eval, cleanup;
7870 gimple try_;
7872 /* Calls to destructors are generated automatically in FINALLY/CATCH
7873 block. They should have location as UNKNOWN_LOCATION. However,
7874 gimplify_call_expr will reset these call stmts to input_location
7875 if it finds stmt's location is unknown. To prevent resetting for
7876 destructors, we set the input_location to unknown.
7877 Note that this only affects the destructor calls in FINALLY/CATCH
7878 block, and will automatically reset to its original value by the
7879 end of gimplify_expr. */
7880 input_location = UNKNOWN_LOCATION;
7881 eval = cleanup = NULL;
7882 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7883 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7884 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7885 if (gimple_seq_empty_p (cleanup))
7887 gimple_seq_add_seq (pre_p, eval);
7888 ret = GS_ALL_DONE;
7889 break;
7891 try_ = gimple_build_try (eval, cleanup,
7892 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7893 ? GIMPLE_TRY_FINALLY
7894 : GIMPLE_TRY_CATCH);
7895 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7896 gimple_set_location (try_, saved_location);
7897 else
7898 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7899 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7900 gimple_try_set_catch_is_cleanup (try_,
7901 TRY_CATCH_IS_CLEANUP (*expr_p));
7902 gimplify_seq_add_stmt (pre_p, try_);
7903 ret = GS_ALL_DONE;
7904 break;
7907 case CLEANUP_POINT_EXPR:
7908 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7909 break;
7911 case TARGET_EXPR:
7912 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7913 break;
7915 case CATCH_EXPR:
7917 gimple c;
7918 gimple_seq handler = NULL;
7919 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7920 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7921 gimplify_seq_add_stmt (pre_p, c);
7922 ret = GS_ALL_DONE;
7923 break;
7926 case EH_FILTER_EXPR:
7928 gimple ehf;
7929 gimple_seq failure = NULL;
7931 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7932 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7933 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7934 gimplify_seq_add_stmt (pre_p, ehf);
7935 ret = GS_ALL_DONE;
7936 break;
7939 case OBJ_TYPE_REF:
7941 enum gimplify_status r0, r1;
7942 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7943 post_p, is_gimple_val, fb_rvalue);
7944 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7945 post_p, is_gimple_val, fb_rvalue);
7946 TREE_SIDE_EFFECTS (*expr_p) = 0;
7947 ret = MIN (r0, r1);
7949 break;
7951 case LABEL_DECL:
7952 /* We get here when taking the address of a label. We mark
7953 the label as "forced"; meaning it can never be removed and
7954 it is a potential target for any computed goto. */
7955 FORCED_LABEL (*expr_p) = 1;
7956 ret = GS_ALL_DONE;
7957 break;
7959 case STATEMENT_LIST:
7960 ret = gimplify_statement_list (expr_p, pre_p);
7961 break;
7963 case WITH_SIZE_EXPR:
7965 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7966 post_p == &internal_post ? NULL : post_p,
7967 gimple_test_f, fallback);
7968 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7969 is_gimple_val, fb_rvalue);
7970 ret = GS_ALL_DONE;
7972 break;
7974 case VAR_DECL:
7975 case PARM_DECL:
7976 ret = gimplify_var_or_parm_decl (expr_p);
7977 break;
7979 case RESULT_DECL:
7980 /* When within an OpenMP context, notice uses of variables. */
7981 if (gimplify_omp_ctxp)
7982 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7983 ret = GS_ALL_DONE;
7984 break;
7986 case SSA_NAME:
7987 /* Allow callbacks into the gimplifier during optimization. */
7988 ret = GS_ALL_DONE;
7989 break;
7991 case OMP_PARALLEL:
7992 gimplify_omp_parallel (expr_p, pre_p);
7993 ret = GS_ALL_DONE;
7994 break;
7996 case OMP_TASK:
7997 gimplify_omp_task (expr_p, pre_p);
7998 ret = GS_ALL_DONE;
7999 break;
8001 case OMP_FOR:
8002 case OMP_SIMD:
8003 case CILK_SIMD:
8004 case OMP_DISTRIBUTE:
8005 ret = gimplify_omp_for (expr_p, pre_p);
8006 break;
8008 case OMP_SECTIONS:
8009 case OMP_SINGLE:
8010 case OMP_TARGET:
8011 case OMP_TARGET_DATA:
8012 case OMP_TEAMS:
8013 gimplify_omp_workshare (expr_p, pre_p);
8014 ret = GS_ALL_DONE;
8015 break;
8017 case OMP_TARGET_UPDATE:
8018 gimplify_omp_target_update (expr_p, pre_p);
8019 ret = GS_ALL_DONE;
8020 break;
8022 case OMP_SECTION:
8023 case OMP_MASTER:
8024 case OMP_TASKGROUP:
8025 case OMP_ORDERED:
8026 case OMP_CRITICAL:
8028 gimple_seq body = NULL;
8029 gimple g;
8031 gimplify_and_add (OMP_BODY (*expr_p), &body);
8032 switch (TREE_CODE (*expr_p))
8034 case OMP_SECTION:
8035 g = gimple_build_omp_section (body);
8036 break;
8037 case OMP_MASTER:
8038 g = gimple_build_omp_master (body);
8039 break;
8040 case OMP_TASKGROUP:
8042 gimple_seq cleanup = NULL;
8043 tree fn
8044 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8045 g = gimple_build_call (fn, 0);
8046 gimple_seq_add_stmt (&cleanup, g);
8047 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8048 body = NULL;
8049 gimple_seq_add_stmt (&body, g);
8050 g = gimple_build_omp_taskgroup (body);
8052 break;
8053 case OMP_ORDERED:
8054 g = gimple_build_omp_ordered (body);
8055 break;
8056 case OMP_CRITICAL:
8057 g = gimple_build_omp_critical (body,
8058 OMP_CRITICAL_NAME (*expr_p));
8059 break;
8060 default:
8061 gcc_unreachable ();
8063 gimplify_seq_add_stmt (pre_p, g);
8064 ret = GS_ALL_DONE;
8065 break;
8068 case OMP_ATOMIC:
8069 case OMP_ATOMIC_READ:
8070 case OMP_ATOMIC_CAPTURE_OLD:
8071 case OMP_ATOMIC_CAPTURE_NEW:
8072 ret = gimplify_omp_atomic (expr_p, pre_p);
8073 break;
8075 case TRANSACTION_EXPR:
8076 ret = gimplify_transaction (expr_p, pre_p);
8077 break;
8079 case TRUTH_AND_EXPR:
8080 case TRUTH_OR_EXPR:
8081 case TRUTH_XOR_EXPR:
8083 tree orig_type = TREE_TYPE (*expr_p);
8084 tree new_type, xop0, xop1;
8085 *expr_p = gimple_boolify (*expr_p);
8086 new_type = TREE_TYPE (*expr_p);
8087 if (!useless_type_conversion_p (orig_type, new_type))
8089 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8090 ret = GS_OK;
8091 break;
8094 /* Boolified binary truth expressions are semantically equivalent
8095 to bitwise binary expressions. Canonicalize them to the
8096 bitwise variant. */
8097 switch (TREE_CODE (*expr_p))
8099 case TRUTH_AND_EXPR:
8100 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8101 break;
8102 case TRUTH_OR_EXPR:
8103 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8104 break;
8105 case TRUTH_XOR_EXPR:
8106 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8107 break;
8108 default:
8109 break;
8111 /* Now make sure that operands have compatible type to
8112 expression's new_type. */
8113 xop0 = TREE_OPERAND (*expr_p, 0);
8114 xop1 = TREE_OPERAND (*expr_p, 1);
8115 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8116 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8117 new_type,
8118 xop0);
8119 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8120 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8121 new_type,
8122 xop1);
8123 /* Continue classified as tcc_binary. */
8124 goto expr_2;
8127 case FMA_EXPR:
8128 case VEC_COND_EXPR:
8129 case VEC_PERM_EXPR:
8130 /* Classified as tcc_expression. */
8131 goto expr_3;
8133 case POINTER_PLUS_EXPR:
8135 enum gimplify_status r0, r1;
8136 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8137 post_p, is_gimple_val, fb_rvalue);
8138 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8139 post_p, is_gimple_val, fb_rvalue);
8140 recalculate_side_effects (*expr_p);
8141 ret = MIN (r0, r1);
8142 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8143 after gimplifying operands - this is similar to how
8144 it would be folding all gimplified stmts on creation
8145 to have them canonicalized, which is what we eventually
8146 should do anyway. */
8147 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8148 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8150 *expr_p = build_fold_addr_expr_with_type_loc
8151 (input_location,
8152 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8153 TREE_OPERAND (*expr_p, 0),
8154 fold_convert (ptr_type_node,
8155 TREE_OPERAND (*expr_p, 1))),
8156 TREE_TYPE (*expr_p));
8157 ret = MIN (ret, GS_OK);
8159 break;
8162 case CILK_SYNC_STMT:
8164 if (!fn_contains_cilk_spawn_p (cfun))
8166 error_at (EXPR_LOCATION (*expr_p),
8167 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8168 ret = GS_ERROR;
8170 else
8172 gimplify_cilk_sync (expr_p, pre_p);
8173 ret = GS_ALL_DONE;
8175 break;
8178 default:
8179 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8181 case tcc_comparison:
8182 /* Handle comparison of objects of non scalar mode aggregates
8183 with a call to memcmp. It would be nice to only have to do
8184 this for variable-sized objects, but then we'd have to allow
8185 the same nest of reference nodes we allow for MODIFY_EXPR and
8186 that's too complex.
8188 Compare scalar mode aggregates as scalar mode values. Using
8189 memcmp for them would be very inefficient at best, and is
8190 plain wrong if bitfields are involved. */
8192 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8194 /* Vector comparisons need no boolification. */
8195 if (TREE_CODE (type) == VECTOR_TYPE)
8196 goto expr_2;
8197 else if (!AGGREGATE_TYPE_P (type))
8199 tree org_type = TREE_TYPE (*expr_p);
8200 *expr_p = gimple_boolify (*expr_p);
8201 if (!useless_type_conversion_p (org_type,
8202 TREE_TYPE (*expr_p)))
8204 *expr_p = fold_convert_loc (input_location,
8205 org_type, *expr_p);
8206 ret = GS_OK;
8208 else
8209 goto expr_2;
8211 else if (TYPE_MODE (type) != BLKmode)
8212 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8213 else
8214 ret = gimplify_variable_sized_compare (expr_p);
8216 break;
8219 /* If *EXPR_P does not need to be special-cased, handle it
8220 according to its class. */
8221 case tcc_unary:
8222 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8223 post_p, is_gimple_val, fb_rvalue);
8224 break;
8226 case tcc_binary:
8227 expr_2:
8229 enum gimplify_status r0, r1;
8231 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8232 post_p, is_gimple_val, fb_rvalue);
8233 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8234 post_p, is_gimple_val, fb_rvalue);
8236 ret = MIN (r0, r1);
8237 break;
8240 expr_3:
8242 enum gimplify_status r0, r1, r2;
8244 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8245 post_p, is_gimple_val, fb_rvalue);
8246 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8247 post_p, is_gimple_val, fb_rvalue);
8248 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8249 post_p, is_gimple_val, fb_rvalue);
8251 ret = MIN (MIN (r0, r1), r2);
8252 break;
8255 case tcc_declaration:
8256 case tcc_constant:
8257 ret = GS_ALL_DONE;
8258 goto dont_recalculate;
8260 default:
8261 gcc_unreachable ();
8264 recalculate_side_effects (*expr_p);
8266 dont_recalculate:
8267 break;
8270 gcc_assert (*expr_p || ret != GS_OK);
8272 while (ret == GS_OK);
8274 /* If we encountered an error_mark somewhere nested inside, either
8275 stub out the statement or propagate the error back out. */
8276 if (ret == GS_ERROR)
8278 if (is_statement)
8279 *expr_p = NULL;
8280 goto out;
8283 /* This was only valid as a return value from the langhook, which
8284 we handled. Make sure it doesn't escape from any other context. */
8285 gcc_assert (ret != GS_UNHANDLED);
8287 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8289 /* We aren't looking for a value, and we don't have a valid
8290 statement. If it doesn't have side-effects, throw it away. */
8291 if (!TREE_SIDE_EFFECTS (*expr_p))
8292 *expr_p = NULL;
8293 else if (!TREE_THIS_VOLATILE (*expr_p))
8295 /* This is probably a _REF that contains something nested that
8296 has side effects. Recurse through the operands to find it. */
8297 enum tree_code code = TREE_CODE (*expr_p);
8299 switch (code)
8301 case COMPONENT_REF:
8302 case REALPART_EXPR:
8303 case IMAGPART_EXPR:
8304 case VIEW_CONVERT_EXPR:
8305 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8306 gimple_test_f, fallback);
8307 break;
8309 case ARRAY_REF:
8310 case ARRAY_RANGE_REF:
8311 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8312 gimple_test_f, fallback);
8313 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8314 gimple_test_f, fallback);
8315 break;
8317 default:
8318 /* Anything else with side-effects must be converted to
8319 a valid statement before we get here. */
8320 gcc_unreachable ();
8323 *expr_p = NULL;
8325 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8326 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8328 /* Historically, the compiler has treated a bare reference
8329 to a non-BLKmode volatile lvalue as forcing a load. */
8330 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8332 /* Normally, we do not want to create a temporary for a
8333 TREE_ADDRESSABLE type because such a type should not be
8334 copied by bitwise-assignment. However, we make an
8335 exception here, as all we are doing here is ensuring that
8336 we read the bytes that make up the type. We use
8337 create_tmp_var_raw because create_tmp_var will abort when
8338 given a TREE_ADDRESSABLE type. */
8339 tree tmp = create_tmp_var_raw (type, "vol");
8340 gimple_add_tmp_var (tmp);
8341 gimplify_assign (tmp, *expr_p, pre_p);
8342 *expr_p = NULL;
8344 else
8345 /* We can't do anything useful with a volatile reference to
8346 an incomplete type, so just throw it away. Likewise for
8347 a BLKmode type, since any implicit inner load should
8348 already have been turned into an explicit one by the
8349 gimplification process. */
8350 *expr_p = NULL;
8353 /* If we are gimplifying at the statement level, we're done. Tack
8354 everything together and return. */
8355 if (fallback == fb_none || is_statement)
8357 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8358 it out for GC to reclaim it. */
8359 *expr_p = NULL_TREE;
8361 if (!gimple_seq_empty_p (internal_pre)
8362 || !gimple_seq_empty_p (internal_post))
8364 gimplify_seq_add_seq (&internal_pre, internal_post);
8365 gimplify_seq_add_seq (pre_p, internal_pre);
8368 /* The result of gimplifying *EXPR_P is going to be the last few
8369 statements in *PRE_P and *POST_P. Add location information
8370 to all the statements that were added by the gimplification
8371 helpers. */
8372 if (!gimple_seq_empty_p (*pre_p))
8373 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8375 if (!gimple_seq_empty_p (*post_p))
8376 annotate_all_with_location_after (*post_p, post_last_gsi,
8377 input_location);
8379 goto out;
8382 #ifdef ENABLE_GIMPLE_CHECKING
8383 if (*expr_p)
8385 enum tree_code code = TREE_CODE (*expr_p);
8386 /* These expressions should already be in gimple IR form. */
8387 gcc_assert (code != MODIFY_EXPR
8388 && code != ASM_EXPR
8389 && code != BIND_EXPR
8390 && code != CATCH_EXPR
8391 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8392 && code != EH_FILTER_EXPR
8393 && code != GOTO_EXPR
8394 && code != LABEL_EXPR
8395 && code != LOOP_EXPR
8396 && code != SWITCH_EXPR
8397 && code != TRY_FINALLY_EXPR
8398 && code != OMP_CRITICAL
8399 && code != OMP_FOR
8400 && code != OMP_MASTER
8401 && code != OMP_TASKGROUP
8402 && code != OMP_ORDERED
8403 && code != OMP_PARALLEL
8404 && code != OMP_SECTIONS
8405 && code != OMP_SECTION
8406 && code != OMP_SINGLE);
8408 #endif
8410 /* Otherwise we're gimplifying a subexpression, so the resulting
8411 value is interesting. If it's a valid operand that matches
8412 GIMPLE_TEST_F, we're done. Unless we are handling some
8413 post-effects internally; if that's the case, we need to copy into
8414 a temporary before adding the post-effects to POST_P. */
8415 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8416 goto out;
8418 /* Otherwise, we need to create a new temporary for the gimplified
8419 expression. */
8421 /* We can't return an lvalue if we have an internal postqueue. The
8422 object the lvalue refers to would (probably) be modified by the
8423 postqueue; we need to copy the value out first, which means an
8424 rvalue. */
8425 if ((fallback & fb_lvalue)
8426 && gimple_seq_empty_p (internal_post)
8427 && is_gimple_addressable (*expr_p))
8429 /* An lvalue will do. Take the address of the expression, store it
8430 in a temporary, and replace the expression with an INDIRECT_REF of
8431 that temporary. */
8432 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8433 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8434 *expr_p = build_simple_mem_ref (tmp);
8436 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8438 /* An rvalue will do. Assign the gimplified expression into a
8439 new temporary TMP and replace the original expression with
8440 TMP. First, make sure that the expression has a type so that
8441 it can be assigned into a temporary. */
8442 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8443 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8445 else
8447 #ifdef ENABLE_GIMPLE_CHECKING
8448 if (!(fallback & fb_mayfail))
8450 fprintf (stderr, "gimplification failed:\n");
8451 print_generic_expr (stderr, *expr_p, 0);
8452 debug_tree (*expr_p);
8453 internal_error ("gimplification failed");
8455 #endif
8456 gcc_assert (fallback & fb_mayfail);
8458 /* If this is an asm statement, and the user asked for the
8459 impossible, don't die. Fail and let gimplify_asm_expr
8460 issue an error. */
8461 ret = GS_ERROR;
8462 goto out;
8465 /* Make sure the temporary matches our predicate. */
8466 gcc_assert ((*gimple_test_f) (*expr_p));
8468 if (!gimple_seq_empty_p (internal_post))
8470 annotate_all_with_location (internal_post, input_location);
8471 gimplify_seq_add_seq (pre_p, internal_post);
8474 out:
8475 input_location = saved_location;
8476 return ret;
8479 /* Look through TYPE for variable-sized objects and gimplify each such
8480 size that we find. Add to LIST_P any statements generated. */
8482 void
8483 gimplify_type_sizes (tree type, gimple_seq *list_p)
8485 tree field, t;
8487 if (type == NULL || type == error_mark_node)
8488 return;
8490 /* We first do the main variant, then copy into any other variants. */
8491 type = TYPE_MAIN_VARIANT (type);
8493 /* Avoid infinite recursion. */
8494 if (TYPE_SIZES_GIMPLIFIED (type))
8495 return;
8497 TYPE_SIZES_GIMPLIFIED (type) = 1;
8499 switch (TREE_CODE (type))
8501 case INTEGER_TYPE:
8502 case ENUMERAL_TYPE:
8503 case BOOLEAN_TYPE:
8504 case REAL_TYPE:
8505 case FIXED_POINT_TYPE:
8506 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8507 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8509 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8511 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8512 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8514 break;
8516 case ARRAY_TYPE:
8517 /* These types may not have declarations, so handle them here. */
8518 gimplify_type_sizes (TREE_TYPE (type), list_p);
8519 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8520 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8521 with assigned stack slots, for -O1+ -g they should be tracked
8522 by VTA. */
8523 if (!(TYPE_NAME (type)
8524 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8525 && DECL_IGNORED_P (TYPE_NAME (type)))
8526 && TYPE_DOMAIN (type)
8527 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8529 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8530 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8531 DECL_IGNORED_P (t) = 0;
8532 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8533 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8534 DECL_IGNORED_P (t) = 0;
8536 break;
8538 case RECORD_TYPE:
8539 case UNION_TYPE:
8540 case QUAL_UNION_TYPE:
8541 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8542 if (TREE_CODE (field) == FIELD_DECL)
8544 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8545 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8546 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8547 gimplify_type_sizes (TREE_TYPE (field), list_p);
8549 break;
8551 case POINTER_TYPE:
8552 case REFERENCE_TYPE:
8553 /* We used to recurse on the pointed-to type here, which turned out to
8554 be incorrect because its definition might refer to variables not
8555 yet initialized at this point if a forward declaration is involved.
8557 It was actually useful for anonymous pointed-to types to ensure
8558 that the sizes evaluation dominates every possible later use of the
8559 values. Restricting to such types here would be safe since there
8560 is no possible forward declaration around, but would introduce an
8561 undesirable middle-end semantic to anonymity. We then defer to
8562 front-ends the responsibility of ensuring that the sizes are
8563 evaluated both early and late enough, e.g. by attaching artificial
8564 type declarations to the tree. */
8565 break;
8567 default:
8568 break;
8571 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8572 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8574 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8576 TYPE_SIZE (t) = TYPE_SIZE (type);
8577 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8578 TYPE_SIZES_GIMPLIFIED (t) = 1;
8582 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8583 a size or position, has had all of its SAVE_EXPRs evaluated.
8584 We add any required statements to *STMT_P. */
8586 void
8587 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8589 tree expr = *expr_p;
8591 /* We don't do anything if the value isn't there, is constant, or contains
8592 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8593 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8594 will want to replace it with a new variable, but that will cause problems
8595 if this type is from outside the function. It's OK to have that here. */
8596 if (is_gimple_sizepos (expr))
8597 return;
8599 *expr_p = unshare_expr (expr);
8601 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8604 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8605 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8606 is true, also gimplify the parameters. */
8608 gimple
8609 gimplify_body (tree fndecl, bool do_parms)
8611 location_t saved_location = input_location;
8612 gimple_seq parm_stmts, seq;
8613 gimple outer_bind;
8614 struct cgraph_node *cgn;
8616 timevar_push (TV_TREE_GIMPLIFY);
8618 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8619 gimplification. */
8620 default_rtl_profile ();
8622 gcc_assert (gimplify_ctxp == NULL);
8623 push_gimplify_context ();
8625 if (flag_openmp)
8627 gcc_assert (gimplify_omp_ctxp == NULL);
8628 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8629 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8632 /* Unshare most shared trees in the body and in that of any nested functions.
8633 It would seem we don't have to do this for nested functions because
8634 they are supposed to be output and then the outer function gimplified
8635 first, but the g++ front end doesn't always do it that way. */
8636 unshare_body (fndecl);
8637 unvisit_body (fndecl);
8639 cgn = cgraph_get_node (fndecl);
8640 if (cgn && cgn->origin)
8641 nonlocal_vlas = pointer_set_create ();
8643 /* Make sure input_location isn't set to something weird. */
8644 input_location = DECL_SOURCE_LOCATION (fndecl);
8646 /* Resolve callee-copies. This has to be done before processing
8647 the body so that DECL_VALUE_EXPR gets processed correctly. */
8648 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8650 /* Gimplify the function's body. */
8651 seq = NULL;
8652 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8653 outer_bind = gimple_seq_first_stmt (seq);
8654 if (!outer_bind)
8656 outer_bind = gimple_build_nop ();
8657 gimplify_seq_add_stmt (&seq, outer_bind);
8660 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8661 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8662 if (gimple_code (outer_bind) == GIMPLE_BIND
8663 && gimple_seq_first (seq) == gimple_seq_last (seq))
8665 else
8666 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8668 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8670 /* If we had callee-copies statements, insert them at the beginning
8671 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8672 if (!gimple_seq_empty_p (parm_stmts))
8674 tree parm;
8676 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8677 gimple_bind_set_body (outer_bind, parm_stmts);
8679 for (parm = DECL_ARGUMENTS (current_function_decl);
8680 parm; parm = DECL_CHAIN (parm))
8681 if (DECL_HAS_VALUE_EXPR_P (parm))
8683 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8684 DECL_IGNORED_P (parm) = 0;
8688 if (nonlocal_vlas)
8690 if (nonlocal_vla_vars)
8692 /* tree-nested.c may later on call declare_vars (..., true);
8693 which relies on BLOCK_VARS chain to be the tail of the
8694 gimple_bind_vars chain. Ensure we don't violate that
8695 assumption. */
8696 if (gimple_bind_block (outer_bind)
8697 == DECL_INITIAL (current_function_decl))
8698 declare_vars (nonlocal_vla_vars, outer_bind, true);
8699 else
8700 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8701 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8702 nonlocal_vla_vars);
8703 nonlocal_vla_vars = NULL_TREE;
8705 pointer_set_destroy (nonlocal_vlas);
8706 nonlocal_vlas = NULL;
8709 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8711 delete_omp_context (gimplify_omp_ctxp);
8712 gimplify_omp_ctxp = NULL;
8715 pop_gimplify_context (outer_bind);
8716 gcc_assert (gimplify_ctxp == NULL);
8718 #ifdef ENABLE_CHECKING
8719 if (!seen_error ())
8720 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8721 #endif
8723 timevar_pop (TV_TREE_GIMPLIFY);
8724 input_location = saved_location;
8726 return outer_bind;
8729 typedef char *char_p; /* For DEF_VEC_P. */
8731 /* Return whether we should exclude FNDECL from instrumentation. */
8733 static bool
8734 flag_instrument_functions_exclude_p (tree fndecl)
8736 vec<char_p> *v;
8738 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8739 if (v && v->length () > 0)
8741 const char *name;
8742 int i;
8743 char *s;
8745 name = lang_hooks.decl_printable_name (fndecl, 0);
8746 FOR_EACH_VEC_ELT (*v, i, s)
8747 if (strstr (name, s) != NULL)
8748 return true;
8751 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8752 if (v && v->length () > 0)
8754 const char *name;
8755 int i;
8756 char *s;
8758 name = DECL_SOURCE_FILE (fndecl);
8759 FOR_EACH_VEC_ELT (*v, i, s)
8760 if (strstr (name, s) != NULL)
8761 return true;
8764 return false;
8767 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8768 node for the function we want to gimplify.
8770 Return the sequence of GIMPLE statements corresponding to the body
8771 of FNDECL. */
8773 void
8774 gimplify_function_tree (tree fndecl)
8776 tree parm, ret;
8777 gimple_seq seq;
8778 gimple bind;
8780 gcc_assert (!gimple_body (fndecl));
8782 if (DECL_STRUCT_FUNCTION (fndecl))
8783 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8784 else
8785 push_struct_function (fndecl);
8787 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8789 /* Preliminarily mark non-addressed complex variables as eligible
8790 for promotion to gimple registers. We'll transform their uses
8791 as we find them. */
8792 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8793 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8794 && !TREE_THIS_VOLATILE (parm)
8795 && !needs_to_live_in_memory (parm))
8796 DECL_GIMPLE_REG_P (parm) = 1;
8799 ret = DECL_RESULT (fndecl);
8800 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8801 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8802 && !needs_to_live_in_memory (ret))
8803 DECL_GIMPLE_REG_P (ret) = 1;
8805 bind = gimplify_body (fndecl, true);
8807 /* The tree body of the function is no longer needed, replace it
8808 with the new GIMPLE body. */
8809 seq = NULL;
8810 gimple_seq_add_stmt (&seq, bind);
8811 gimple_set_body (fndecl, seq);
8813 /* If we're instrumenting function entry/exit, then prepend the call to
8814 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8815 catch the exit hook. */
8816 /* ??? Add some way to ignore exceptions for this TFE. */
8817 if (flag_instrument_function_entry_exit
8818 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8819 && !flag_instrument_functions_exclude_p (fndecl))
8821 tree x;
8822 gimple new_bind;
8823 gimple tf;
8824 gimple_seq cleanup = NULL, body = NULL;
8825 tree tmp_var;
8826 gimple call;
8828 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8829 call = gimple_build_call (x, 1, integer_zero_node);
8830 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8831 gimple_call_set_lhs (call, tmp_var);
8832 gimplify_seq_add_stmt (&cleanup, call);
8833 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8834 call = gimple_build_call (x, 2,
8835 build_fold_addr_expr (current_function_decl),
8836 tmp_var);
8837 gimplify_seq_add_stmt (&cleanup, call);
8838 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8840 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8841 call = gimple_build_call (x, 1, integer_zero_node);
8842 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8843 gimple_call_set_lhs (call, tmp_var);
8844 gimplify_seq_add_stmt (&body, call);
8845 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8846 call = gimple_build_call (x, 2,
8847 build_fold_addr_expr (current_function_decl),
8848 tmp_var);
8849 gimplify_seq_add_stmt (&body, call);
8850 gimplify_seq_add_stmt (&body, tf);
8851 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8852 /* Clear the block for BIND, since it is no longer directly inside
8853 the function, but within a try block. */
8854 gimple_bind_set_block (bind, NULL);
8856 /* Replace the current function body with the body
8857 wrapped in the try/finally TF. */
8858 seq = NULL;
8859 gimple_seq_add_stmt (&seq, new_bind);
8860 gimple_set_body (fndecl, seq);
8863 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8864 cfun->curr_properties = PROP_gimple_any;
8866 pop_cfun ();
8869 /* Return a dummy expression of type TYPE in order to keep going after an
8870 error. */
8872 static tree
8873 dummy_object (tree type)
8875 tree t = build_int_cst (build_pointer_type (type), 0);
8876 return build2 (MEM_REF, type, t, t);
8879 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
8880 builtin function, but a very special sort of operator. */
8882 enum gimplify_status
8883 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8885 tree promoted_type, have_va_type;
8886 tree valist = TREE_OPERAND (*expr_p, 0);
8887 tree type = TREE_TYPE (*expr_p);
8888 tree t;
8889 location_t loc = EXPR_LOCATION (*expr_p);
8891 /* Verify that valist is of the proper type. */
8892 have_va_type = TREE_TYPE (valist);
8893 if (have_va_type == error_mark_node)
8894 return GS_ERROR;
8895 have_va_type = targetm.canonical_va_list_type (have_va_type);
8897 if (have_va_type == NULL_TREE)
8899 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
8900 return GS_ERROR;
8903 /* Generate a diagnostic for requesting data of a type that cannot
8904 be passed through `...' due to type promotion at the call site. */
8905 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
8906 != type)
8908 static bool gave_help;
8909 bool warned;
8911 /* Unfortunately, this is merely undefined, rather than a constraint
8912 violation, so we cannot make this an error. If this call is never
8913 executed, the program is still strictly conforming. */
8914 warned = warning_at (loc, 0,
8915 "%qT is promoted to %qT when passed through %<...%>",
8916 type, promoted_type);
8917 if (!gave_help && warned)
8919 gave_help = true;
8920 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
8921 promoted_type, type);
8924 /* We can, however, treat "undefined" any way we please.
8925 Call abort to encourage the user to fix the program. */
8926 if (warned)
8927 inform (loc, "if this code is reached, the program will abort");
8928 /* Before the abort, allow the evaluation of the va_list
8929 expression to exit or longjmp. */
8930 gimplify_and_add (valist, pre_p);
8931 t = build_call_expr_loc (loc,
8932 builtin_decl_implicit (BUILT_IN_TRAP), 0);
8933 gimplify_and_add (t, pre_p);
8935 /* This is dead code, but go ahead and finish so that the
8936 mode of the result comes out right. */
8937 *expr_p = dummy_object (type);
8938 return GS_ALL_DONE;
8940 else
8942 /* Make it easier for the backends by protecting the valist argument
8943 from multiple evaluations. */
8944 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
8946 /* For this case, the backends will be expecting a pointer to
8947 TREE_TYPE (abi), but it's possible we've
8948 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
8949 So fix it. */
8950 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
8952 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
8953 valist = fold_convert_loc (loc, p1,
8954 build_fold_addr_expr_loc (loc, valist));
8957 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
8959 else
8960 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
8962 if (!targetm.gimplify_va_arg_expr)
8963 /* FIXME: Once most targets are converted we should merely
8964 assert this is non-null. */
8965 return GS_ALL_DONE;
8967 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
8968 return GS_OK;
8972 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
8974 DST/SRC are the destination and source respectively. You can pass
8975 ungimplified trees in DST or SRC, in which case they will be
8976 converted to a gimple operand if necessary.
8978 This function returns the newly created GIMPLE_ASSIGN tuple. */
8980 gimple
8981 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
8983 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
8984 gimplify_and_add (t, seq_p);
8985 ggc_free (t);
8986 return gimple_seq_last_stmt (*seq_p);
8989 inline hashval_t
8990 gimplify_hasher::hash (const value_type *p)
8992 tree t = p->val;
8993 return iterative_hash_expr (t, 0);
8996 inline bool
8997 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
8999 tree t1 = p1->val;
9000 tree t2 = p2->val;
9001 enum tree_code code = TREE_CODE (t1);
9003 if (TREE_CODE (t2) != code
9004 || TREE_TYPE (t1) != TREE_TYPE (t2))
9005 return false;
9007 if (!operand_equal_p (t1, t2, 0))
9008 return false;
9010 #ifdef ENABLE_CHECKING
9011 /* Only allow them to compare equal if they also hash equal; otherwise
9012 results are nondeterminate, and we fail bootstrap comparison. */
9013 gcc_assert (hash (p1) == hash (p2));
9014 #endif
9016 return true;