2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / gimplify.c
blob2b6de3e173218c8cfe4e4b9ff1fbef6b1b40c55b
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2013 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 "gimple.h"
29 #include "gimplify.h"
30 #include "gimple-iterator.h"
31 #include "stringpool.h"
32 #include "calls.h"
33 #include "varasm.h"
34 #include "stor-layout.h"
35 #include "stmt.h"
36 #include "print-tree.h"
37 #include "tree-iterator.h"
38 #include "tree-inline.h"
39 #include "tree-pretty-print.h"
40 #include "langhooks.h"
41 #include "bitmap.h"
42 #include "gimple-ssa.h"
43 #include "cgraph.h"
44 #include "tree-cfg.h"
45 #include "tree-ssanames.h"
46 #include "tree-ssa.h"
47 #include "diagnostic-core.h"
48 #include "target.h"
49 #include "splay-tree.h"
50 #include "omp-low.h"
51 #include "gimple-low.h"
52 #include "cilk.h"
54 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
55 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
57 enum gimplify_omp_var_data
59 GOVD_SEEN = 1,
60 GOVD_EXPLICIT = 2,
61 GOVD_SHARED = 4,
62 GOVD_PRIVATE = 8,
63 GOVD_FIRSTPRIVATE = 16,
64 GOVD_LASTPRIVATE = 32,
65 GOVD_REDUCTION = 64,
66 GOVD_LOCAL = 128,
67 GOVD_MAP = 256,
68 GOVD_DEBUG_PRIVATE = 512,
69 GOVD_PRIVATE_OUTER_REF = 1024,
70 GOVD_LINEAR = 2048,
71 GOVD_ALIGNED = 4096,
72 GOVD_MAP_TO_ONLY = 8192,
73 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
74 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
75 | GOVD_LOCAL)
79 enum omp_region_type
81 ORT_WORKSHARE = 0,
82 ORT_SIMD = 1,
83 ORT_PARALLEL = 2,
84 ORT_COMBINED_PARALLEL = 3,
85 ORT_TASK = 4,
86 ORT_UNTIED_TASK = 5,
87 ORT_TEAMS = 8,
88 ORT_TARGET_DATA = 16,
89 ORT_TARGET = 32
92 /* Gimplify hashtable helper. */
94 struct gimplify_hasher : typed_free_remove <elt_t>
96 typedef elt_t value_type;
97 typedef elt_t compare_type;
98 static inline hashval_t hash (const value_type *);
99 static inline bool equal (const value_type *, const compare_type *);
102 struct gimplify_ctx
104 struct gimplify_ctx *prev_context;
106 vec<gimple> bind_expr_stack;
107 tree temps;
108 gimple_seq conditional_cleanups;
109 tree exit_label;
110 tree return_temp;
112 vec<tree> case_labels;
113 /* The formal temporary table. Should this be persistent? */
114 hash_table <gimplify_hasher> temp_htab;
116 int conditions;
117 bool save_stack;
118 bool into_ssa;
119 bool allow_rhs_cond_expr;
120 bool in_cleanup_point_expr;
123 struct gimplify_omp_ctx
125 struct gimplify_omp_ctx *outer_context;
126 splay_tree variables;
127 struct pointer_set_t *privatized_types;
128 location_t location;
129 enum omp_clause_default_kind default_kind;
130 enum omp_region_type region_type;
131 bool combined_loop;
134 static struct gimplify_ctx *gimplify_ctxp;
135 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
137 /* Forward declaration. */
138 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
140 /* Shorter alias name for the above function for use in gimplify.c
141 only. */
143 static inline void
144 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
146 gimple_seq_add_stmt_without_update (seq_p, gs);
149 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
150 NULL, a new sequence is allocated. This function is
151 similar to gimple_seq_add_seq, but does not scan the operands.
152 During gimplification, we need to manipulate statement sequences
153 before the def/use vectors have been constructed. */
155 static void
156 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
158 gimple_stmt_iterator si;
160 if (src == NULL)
161 return;
163 si = gsi_last (*dst_p);
164 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
168 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
169 and popping gimplify contexts. */
171 static struct gimplify_ctx *ctx_pool = NULL;
173 /* Return a gimplify context struct from the pool. */
175 static inline struct gimplify_ctx *
176 ctx_alloc (void)
178 struct gimplify_ctx * c = ctx_pool;
180 if (c)
181 ctx_pool = c->prev_context;
182 else
183 c = XNEW (struct gimplify_ctx);
185 memset (c, '\0', sizeof (*c));
186 return c;
189 /* Put gimplify context C back into the pool. */
191 static inline void
192 ctx_free (struct gimplify_ctx *c)
194 c->prev_context = ctx_pool;
195 ctx_pool = c;
198 /* Free allocated ctx stack memory. */
200 void
201 free_gimplify_stack (void)
203 struct gimplify_ctx *c;
205 while ((c = ctx_pool))
207 ctx_pool = c->prev_context;
208 free (c);
213 /* Set up a context for the gimplifier. */
215 void
216 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
218 struct gimplify_ctx *c = ctx_alloc ();
220 c->prev_context = gimplify_ctxp;
221 gimplify_ctxp = c;
222 gimplify_ctxp->into_ssa = in_ssa;
223 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
226 /* Tear down a context for the gimplifier. If BODY is non-null, then
227 put the temporaries into the outer BIND_EXPR. Otherwise, put them
228 in the local_decls.
230 BODY is not a sequence, but the first tuple in a sequence. */
232 void
233 pop_gimplify_context (gimple body)
235 struct gimplify_ctx *c = gimplify_ctxp;
237 gcc_assert (c
238 && (!c->bind_expr_stack.exists ()
239 || c->bind_expr_stack.is_empty ()));
240 c->bind_expr_stack.release ();
241 gimplify_ctxp = c->prev_context;
243 if (body)
244 declare_vars (c->temps, body, false);
245 else
246 record_vars (c->temps);
248 if (c->temp_htab.is_created ())
249 c->temp_htab.dispose ();
250 ctx_free (c);
253 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
255 static void
256 gimple_push_bind_expr (gimple gimple_bind)
258 gimplify_ctxp->bind_expr_stack.reserve (8);
259 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
262 /* Pop the first element off the stack of bindings. */
264 static void
265 gimple_pop_bind_expr (void)
267 gimplify_ctxp->bind_expr_stack.pop ();
270 /* Return the first element of the stack of bindings. */
272 gimple
273 gimple_current_bind_expr (void)
275 return gimplify_ctxp->bind_expr_stack.last ();
278 /* Return the stack of bindings created during gimplification. */
280 vec<gimple>
281 gimple_bind_expr_stack (void)
283 return gimplify_ctxp->bind_expr_stack;
286 /* Return true iff there is a COND_EXPR between us and the innermost
287 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
289 static bool
290 gimple_conditional_context (void)
292 return gimplify_ctxp->conditions > 0;
295 /* Note that we've entered a COND_EXPR. */
297 static void
298 gimple_push_condition (void)
300 #ifdef ENABLE_GIMPLE_CHECKING
301 if (gimplify_ctxp->conditions == 0)
302 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
303 #endif
304 ++(gimplify_ctxp->conditions);
307 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
308 now, add any conditional cleanups we've seen to the prequeue. */
310 static void
311 gimple_pop_condition (gimple_seq *pre_p)
313 int conds = --(gimplify_ctxp->conditions);
315 gcc_assert (conds >= 0);
316 if (conds == 0)
318 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
319 gimplify_ctxp->conditional_cleanups = NULL;
323 /* A stable comparison routine for use with splay trees and DECLs. */
325 static int
326 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
328 tree a = (tree) xa;
329 tree b = (tree) xb;
331 return DECL_UID (a) - DECL_UID (b);
334 /* Create a new omp construct that deals with variable remapping. */
336 static struct gimplify_omp_ctx *
337 new_omp_context (enum omp_region_type region_type)
339 struct gimplify_omp_ctx *c;
341 c = XCNEW (struct gimplify_omp_ctx);
342 c->outer_context = gimplify_omp_ctxp;
343 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
344 c->privatized_types = pointer_set_create ();
345 c->location = input_location;
346 c->region_type = region_type;
347 if ((region_type & ORT_TASK) == 0)
348 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
349 else
350 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
352 return c;
355 /* Destroy an omp construct that deals with variable remapping. */
357 static void
358 delete_omp_context (struct gimplify_omp_ctx *c)
360 splay_tree_delete (c->variables);
361 pointer_set_destroy (c->privatized_types);
362 XDELETE (c);
365 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
366 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
368 /* Both gimplify the statement T and append it to *SEQ_P. This function
369 behaves exactly as gimplify_stmt, but you don't have to pass T as a
370 reference. */
372 void
373 gimplify_and_add (tree t, gimple_seq *seq_p)
375 gimplify_stmt (&t, seq_p);
378 /* Gimplify statement T into sequence *SEQ_P, and return the first
379 tuple in the sequence of generated tuples for this statement.
380 Return NULL if gimplifying T produced no tuples. */
382 static gimple
383 gimplify_and_return_first (tree t, gimple_seq *seq_p)
385 gimple_stmt_iterator last = gsi_last (*seq_p);
387 gimplify_and_add (t, seq_p);
389 if (!gsi_end_p (last))
391 gsi_next (&last);
392 return gsi_stmt (last);
394 else
395 return gimple_seq_first_stmt (*seq_p);
398 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
399 LHS, or for a call argument. */
401 static bool
402 is_gimple_mem_rhs (tree t)
404 /* If we're dealing with a renamable type, either source or dest must be
405 a renamed variable. */
406 if (is_gimple_reg_type (TREE_TYPE (t)))
407 return is_gimple_val (t);
408 else
409 return is_gimple_val (t) || is_gimple_lvalue (t);
412 /* Return true if T is a CALL_EXPR or an expression that can be
413 assigned to a temporary. Note that this predicate should only be
414 used during gimplification. See the rationale for this in
415 gimplify_modify_expr. */
417 static bool
418 is_gimple_reg_rhs_or_call (tree t)
420 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
421 || TREE_CODE (t) == CALL_EXPR);
424 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
425 this predicate should only be used during gimplification. See the
426 rationale for this in gimplify_modify_expr. */
428 static bool
429 is_gimple_mem_rhs_or_call (tree t)
431 /* If we're dealing with a renamable type, either source or dest must be
432 a renamed variable. */
433 if (is_gimple_reg_type (TREE_TYPE (t)))
434 return is_gimple_val (t);
435 else
436 return (is_gimple_val (t) || is_gimple_lvalue (t)
437 || TREE_CODE (t) == CALL_EXPR);
440 /* Create a temporary with a name derived from VAL. Subroutine of
441 lookup_tmp_var; nobody else should call this function. */
443 static inline tree
444 create_tmp_from_val (tree val, bool is_formal)
446 /* Drop all qualifiers and address-space information from the value type. */
447 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
448 tree var = create_tmp_var (type, get_name (val));
449 if (is_formal
450 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
451 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE))
452 DECL_GIMPLE_REG_P (var) = 1;
453 return var;
456 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
457 an existing expression temporary. */
459 static tree
460 lookup_tmp_var (tree val, bool is_formal)
462 tree ret;
464 /* If not optimizing, never really reuse a temporary. local-alloc
465 won't allocate any variable that is used in more than one basic
466 block, which means it will go into memory, causing much extra
467 work in reload and final and poorer code generation, outweighing
468 the extra memory allocation here. */
469 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
470 ret = create_tmp_from_val (val, is_formal);
471 else
473 elt_t elt, *elt_p;
474 elt_t **slot;
476 elt.val = val;
477 if (!gimplify_ctxp->temp_htab.is_created ())
478 gimplify_ctxp->temp_htab.create (1000);
479 slot = gimplify_ctxp->temp_htab.find_slot (&elt, INSERT);
480 if (*slot == NULL)
482 elt_p = XNEW (elt_t);
483 elt_p->val = val;
484 elt_p->temp = ret = create_tmp_from_val (val, is_formal);
485 *slot = elt_p;
487 else
489 elt_p = *slot;
490 ret = elt_p->temp;
494 return ret;
497 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
499 static tree
500 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
501 bool is_formal)
503 tree t, mod;
505 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
506 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
507 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
508 fb_rvalue);
510 if (gimplify_ctxp->into_ssa
511 && is_gimple_reg_type (TREE_TYPE (val)))
512 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
513 else
514 t = lookup_tmp_var (val, is_formal);
516 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
518 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_HERE (val));
520 /* gimplify_modify_expr might want to reduce this further. */
521 gimplify_and_add (mod, pre_p);
522 ggc_free (mod);
524 return t;
527 /* Return a formal temporary variable initialized with VAL. PRE_P is as
528 in gimplify_expr. Only use this function if:
530 1) The value of the unfactored expression represented by VAL will not
531 change between the initialization and use of the temporary, and
532 2) The temporary will not be otherwise modified.
534 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
535 and #2 means it is inappropriate for && temps.
537 For other cases, use get_initialized_tmp_var instead. */
539 tree
540 get_formal_tmp_var (tree val, gimple_seq *pre_p)
542 return internal_get_tmp_var (val, pre_p, NULL, true);
545 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
546 are as in gimplify_expr. */
548 tree
549 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
551 return internal_get_tmp_var (val, pre_p, post_p, false);
554 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
555 generate debug info for them; otherwise don't. */
557 void
558 declare_vars (tree vars, gimple scope, bool debug_info)
560 tree last = vars;
561 if (last)
563 tree temps, block;
565 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
567 temps = nreverse (last);
569 block = gimple_bind_block (scope);
570 gcc_assert (!block || TREE_CODE (block) == BLOCK);
571 if (!block || !debug_info)
573 DECL_CHAIN (last) = gimple_bind_vars (scope);
574 gimple_bind_set_vars (scope, temps);
576 else
578 /* We need to attach the nodes both to the BIND_EXPR and to its
579 associated BLOCK for debugging purposes. The key point here
580 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
581 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
582 if (BLOCK_VARS (block))
583 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
584 else
586 gimple_bind_set_vars (scope,
587 chainon (gimple_bind_vars (scope), temps));
588 BLOCK_VARS (block) = temps;
594 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
595 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
596 no such upper bound can be obtained. */
598 static void
599 force_constant_size (tree var)
601 /* The only attempt we make is by querying the maximum size of objects
602 of the variable's type. */
604 HOST_WIDE_INT max_size;
606 gcc_assert (TREE_CODE (var) == VAR_DECL);
608 max_size = max_int_size_in_bytes (TREE_TYPE (var));
610 gcc_assert (max_size >= 0);
612 DECL_SIZE_UNIT (var)
613 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
614 DECL_SIZE (var)
615 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
618 /* Push the temporary variable TMP into the current binding. */
620 void
621 gimple_add_tmp_var (tree tmp)
623 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
625 /* Later processing assumes that the object size is constant, which might
626 not be true at this point. Force the use of a constant upper bound in
627 this case. */
628 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
629 force_constant_size (tmp);
631 DECL_CONTEXT (tmp) = current_function_decl;
632 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
634 if (gimplify_ctxp)
636 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
637 gimplify_ctxp->temps = tmp;
639 /* Mark temporaries local within the nearest enclosing parallel. */
640 if (gimplify_omp_ctxp)
642 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
643 while (ctx
644 && (ctx->region_type == ORT_WORKSHARE
645 || ctx->region_type == ORT_SIMD))
646 ctx = ctx->outer_context;
647 if (ctx)
648 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
651 else if (cfun)
652 record_vars (tmp);
653 else
655 gimple_seq body_seq;
657 /* This case is for nested functions. We need to expose the locals
658 they create. */
659 body_seq = gimple_body (current_function_decl);
660 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
666 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
667 nodes that are referenced more than once in GENERIC functions. This is
668 necessary because gimplification (translation into GIMPLE) is performed
669 by modifying tree nodes in-place, so gimplication of a shared node in a
670 first context could generate an invalid GIMPLE form in a second context.
672 This is achieved with a simple mark/copy/unmark algorithm that walks the
673 GENERIC representation top-down, marks nodes with TREE_VISITED the first
674 time it encounters them, duplicates them if they already have TREE_VISITED
675 set, and finally removes the TREE_VISITED marks it has set.
677 The algorithm works only at the function level, i.e. it generates a GENERIC
678 representation of a function with no nodes shared within the function when
679 passed a GENERIC function (except for nodes that are allowed to be shared).
681 At the global level, it is also necessary to unshare tree nodes that are
682 referenced in more than one function, for the same aforementioned reason.
683 This requires some cooperation from the front-end. There are 2 strategies:
685 1. Manual unsharing. The front-end needs to call unshare_expr on every
686 expression that might end up being shared across functions.
688 2. Deep unsharing. This is an extension of regular unsharing. Instead
689 of calling unshare_expr on expressions that might be shared across
690 functions, the front-end pre-marks them with TREE_VISITED. This will
691 ensure that they are unshared on the first reference within functions
692 when the regular unsharing algorithm runs. The counterpart is that
693 this algorithm must look deeper than for manual unsharing, which is
694 specified by LANG_HOOKS_DEEP_UNSHARING.
696 If there are only few specific cases of node sharing across functions, it is
697 probably easier for a front-end to unshare the expressions manually. On the
698 contrary, if the expressions generated at the global level are as widespread
699 as expressions generated within functions, deep unsharing is very likely the
700 way to go. */
702 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
703 These nodes model computations that must be done once. If we were to
704 unshare something like SAVE_EXPR(i++), the gimplification process would
705 create wrong code. However, if DATA is non-null, it must hold a pointer
706 set that is used to unshare the subtrees of these nodes. */
708 static tree
709 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
711 tree t = *tp;
712 enum tree_code code = TREE_CODE (t);
714 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
715 copy their subtrees if we can make sure to do it only once. */
716 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
718 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
720 else
721 *walk_subtrees = 0;
724 /* Stop at types, decls, constants like copy_tree_r. */
725 else if (TREE_CODE_CLASS (code) == tcc_type
726 || TREE_CODE_CLASS (code) == tcc_declaration
727 || TREE_CODE_CLASS (code) == tcc_constant
728 /* We can't do anything sensible with a BLOCK used as an
729 expression, but we also can't just die when we see it
730 because of non-expression uses. So we avert our eyes
731 and cross our fingers. Silly Java. */
732 || code == BLOCK)
733 *walk_subtrees = 0;
735 /* Cope with the statement expression extension. */
736 else if (code == STATEMENT_LIST)
739 /* Leave the bulk of the work to copy_tree_r itself. */
740 else
741 copy_tree_r (tp, walk_subtrees, NULL);
743 return NULL_TREE;
746 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
747 If *TP has been visited already, then *TP is deeply copied by calling
748 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
750 static tree
751 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
753 tree t = *tp;
754 enum tree_code code = TREE_CODE (t);
756 /* Skip types, decls, and constants. But we do want to look at their
757 types and the bounds of types. Mark them as visited so we properly
758 unmark their subtrees on the unmark pass. If we've already seen them,
759 don't look down further. */
760 if (TREE_CODE_CLASS (code) == tcc_type
761 || TREE_CODE_CLASS (code) == tcc_declaration
762 || TREE_CODE_CLASS (code) == tcc_constant)
764 if (TREE_VISITED (t))
765 *walk_subtrees = 0;
766 else
767 TREE_VISITED (t) = 1;
770 /* If this node has been visited already, unshare it and don't look
771 any deeper. */
772 else if (TREE_VISITED (t))
774 walk_tree (tp, mostly_copy_tree_r, data, NULL);
775 *walk_subtrees = 0;
778 /* Otherwise, mark the node as visited and keep looking. */
779 else
780 TREE_VISITED (t) = 1;
782 return NULL_TREE;
785 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
786 copy_if_shared_r callback unmodified. */
788 static inline void
789 copy_if_shared (tree *tp, void *data)
791 walk_tree (tp, copy_if_shared_r, data, NULL);
794 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
795 any nested functions. */
797 static void
798 unshare_body (tree fndecl)
800 struct cgraph_node *cgn = cgraph_get_node (fndecl);
801 /* If the language requires deep unsharing, we need a pointer set to make
802 sure we don't repeatedly unshare subtrees of unshareable nodes. */
803 struct pointer_set_t *visited
804 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
806 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
807 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
808 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
810 if (visited)
811 pointer_set_destroy (visited);
813 if (cgn)
814 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
815 unshare_body (cgn->decl);
818 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
819 Subtrees are walked until the first unvisited node is encountered. */
821 static tree
822 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
824 tree t = *tp;
826 /* If this node has been visited, unmark it and keep looking. */
827 if (TREE_VISITED (t))
828 TREE_VISITED (t) = 0;
830 /* Otherwise, don't look any deeper. */
831 else
832 *walk_subtrees = 0;
834 return NULL_TREE;
837 /* Unmark the visited trees rooted at *TP. */
839 static inline void
840 unmark_visited (tree *tp)
842 walk_tree (tp, unmark_visited_r, NULL, NULL);
845 /* Likewise, but mark all trees as not visited. */
847 static void
848 unvisit_body (tree fndecl)
850 struct cgraph_node *cgn = cgraph_get_node (fndecl);
852 unmark_visited (&DECL_SAVED_TREE (fndecl));
853 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
854 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
856 if (cgn)
857 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
858 unvisit_body (cgn->decl);
861 /* Unconditionally make an unshared copy of EXPR. This is used when using
862 stored expressions which span multiple functions, such as BINFO_VTABLE,
863 as the normal unsharing process can't tell that they're shared. */
865 tree
866 unshare_expr (tree expr)
868 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
869 return expr;
872 /* Worker for unshare_expr_without_location. */
874 static tree
875 prune_expr_location (tree *tp, int *walk_subtrees, void *)
877 if (EXPR_P (*tp))
878 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
879 else
880 *walk_subtrees = 0;
881 return NULL_TREE;
884 /* Similar to unshare_expr but also prune all expression locations
885 from EXPR. */
887 tree
888 unshare_expr_without_location (tree expr)
890 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
891 if (EXPR_P (expr))
892 walk_tree (&expr, prune_expr_location, NULL, NULL);
893 return expr;
896 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
897 contain statements and have a value. Assign its value to a temporary
898 and give it void_type_node. Return the temporary, or NULL_TREE if
899 WRAPPER was already void. */
901 tree
902 voidify_wrapper_expr (tree wrapper, tree temp)
904 tree type = TREE_TYPE (wrapper);
905 if (type && !VOID_TYPE_P (type))
907 tree *p;
909 /* Set p to point to the body of the wrapper. Loop until we find
910 something that isn't a wrapper. */
911 for (p = &wrapper; p && *p; )
913 switch (TREE_CODE (*p))
915 case BIND_EXPR:
916 TREE_SIDE_EFFECTS (*p) = 1;
917 TREE_TYPE (*p) = void_type_node;
918 /* For a BIND_EXPR, the body is operand 1. */
919 p = &BIND_EXPR_BODY (*p);
920 break;
922 case CLEANUP_POINT_EXPR:
923 case TRY_FINALLY_EXPR:
924 case TRY_CATCH_EXPR:
925 TREE_SIDE_EFFECTS (*p) = 1;
926 TREE_TYPE (*p) = void_type_node;
927 p = &TREE_OPERAND (*p, 0);
928 break;
930 case STATEMENT_LIST:
932 tree_stmt_iterator i = tsi_last (*p);
933 TREE_SIDE_EFFECTS (*p) = 1;
934 TREE_TYPE (*p) = void_type_node;
935 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
937 break;
939 case COMPOUND_EXPR:
940 /* Advance to the last statement. Set all container types to
941 void. */
942 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
944 TREE_SIDE_EFFECTS (*p) = 1;
945 TREE_TYPE (*p) = void_type_node;
947 break;
949 case TRANSACTION_EXPR:
950 TREE_SIDE_EFFECTS (*p) = 1;
951 TREE_TYPE (*p) = void_type_node;
952 p = &TRANSACTION_EXPR_BODY (*p);
953 break;
955 default:
956 /* Assume that any tree upon which voidify_wrapper_expr is
957 directly called is a wrapper, and that its body is op0. */
958 if (p == &wrapper)
960 TREE_SIDE_EFFECTS (*p) = 1;
961 TREE_TYPE (*p) = void_type_node;
962 p = &TREE_OPERAND (*p, 0);
963 break;
965 goto out;
969 out:
970 if (p == NULL || IS_EMPTY_STMT (*p))
971 temp = NULL_TREE;
972 else if (temp)
974 /* The wrapper is on the RHS of an assignment that we're pushing
975 down. */
976 gcc_assert (TREE_CODE (temp) == INIT_EXPR
977 || TREE_CODE (temp) == MODIFY_EXPR);
978 TREE_OPERAND (temp, 1) = *p;
979 *p = temp;
981 else
983 temp = create_tmp_var (type, "retval");
984 *p = build2 (INIT_EXPR, type, temp, *p);
987 return temp;
990 return NULL_TREE;
993 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
994 a temporary through which they communicate. */
996 static void
997 build_stack_save_restore (gimple *save, gimple *restore)
999 tree tmp_var;
1001 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1002 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1003 gimple_call_set_lhs (*save, tmp_var);
1005 *restore
1006 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1007 1, tmp_var);
1010 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1012 static enum gimplify_status
1013 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1015 tree bind_expr = *expr_p;
1016 bool old_save_stack = gimplify_ctxp->save_stack;
1017 tree t;
1018 gimple gimple_bind;
1019 gimple_seq body, cleanup;
1020 gimple stack_save;
1022 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1024 /* Mark variables seen in this bind expr. */
1025 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1027 if (TREE_CODE (t) == VAR_DECL)
1029 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1031 /* Mark variable as local. */
1032 if (ctx && !DECL_EXTERNAL (t)
1033 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1034 || splay_tree_lookup (ctx->variables,
1035 (splay_tree_key) t) == NULL))
1036 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1038 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1040 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1041 cfun->has_local_explicit_reg_vars = true;
1044 /* Preliminarily mark non-addressed complex variables as eligible
1045 for promotion to gimple registers. We'll transform their uses
1046 as we find them. */
1047 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1048 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1049 && !TREE_THIS_VOLATILE (t)
1050 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1051 && !needs_to_live_in_memory (t))
1052 DECL_GIMPLE_REG_P (t) = 1;
1055 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1056 BIND_EXPR_BLOCK (bind_expr));
1057 gimple_push_bind_expr (gimple_bind);
1059 gimplify_ctxp->save_stack = false;
1061 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1062 body = NULL;
1063 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1064 gimple_bind_set_body (gimple_bind, body);
1066 cleanup = NULL;
1067 stack_save = NULL;
1068 if (gimplify_ctxp->save_stack)
1070 gimple stack_restore;
1072 /* Save stack on entry and restore it on exit. Add a try_finally
1073 block to achieve this. */
1074 build_stack_save_restore (&stack_save, &stack_restore);
1076 gimplify_seq_add_stmt (&cleanup, stack_restore);
1079 /* Add clobbers for all variables that go out of scope. */
1080 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1082 if (TREE_CODE (t) == VAR_DECL
1083 && !is_global_var (t)
1084 && DECL_CONTEXT (t) == current_function_decl
1085 && !DECL_HARD_REGISTER (t)
1086 && !TREE_THIS_VOLATILE (t)
1087 && !DECL_HAS_VALUE_EXPR_P (t)
1088 /* Only care for variables that have to be in memory. Others
1089 will be rewritten into SSA names, hence moved to the top-level. */
1090 && !is_gimple_reg (t)
1091 && flag_stack_reuse != SR_NONE)
1093 tree clobber = build_constructor (TREE_TYPE (t),
1094 NULL);
1095 TREE_THIS_VOLATILE (clobber) = 1;
1096 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1100 if (cleanup)
1102 gimple gs;
1103 gimple_seq new_body;
1105 new_body = NULL;
1106 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1107 GIMPLE_TRY_FINALLY);
1109 if (stack_save)
1110 gimplify_seq_add_stmt (&new_body, stack_save);
1111 gimplify_seq_add_stmt (&new_body, gs);
1112 gimple_bind_set_body (gimple_bind, new_body);
1115 gimplify_ctxp->save_stack = old_save_stack;
1116 gimple_pop_bind_expr ();
1118 gimplify_seq_add_stmt (pre_p, gimple_bind);
1120 if (temp)
1122 *expr_p = temp;
1123 return GS_OK;
1126 *expr_p = NULL_TREE;
1127 return GS_ALL_DONE;
1130 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1131 GIMPLE value, it is assigned to a new temporary and the statement is
1132 re-written to return the temporary.
1134 PRE_P points to the sequence where side effects that must happen before
1135 STMT should be stored. */
1137 static enum gimplify_status
1138 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1140 gimple ret;
1141 tree ret_expr = TREE_OPERAND (stmt, 0);
1142 tree result_decl, result;
1144 if (ret_expr == error_mark_node)
1145 return GS_ERROR;
1147 /* Implicit _Cilk_sync must be inserted right before any return statement
1148 if there is a _Cilk_spawn in the function. If the user has provided a
1149 _Cilk_sync, the optimizer should remove this duplicate one. */
1150 if (fn_contains_cilk_spawn_p (cfun))
1152 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1153 gimplify_and_add (impl_sync, pre_p);
1156 if (!ret_expr
1157 || TREE_CODE (ret_expr) == RESULT_DECL
1158 || ret_expr == error_mark_node)
1160 gimple ret = gimple_build_return (ret_expr);
1161 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1162 gimplify_seq_add_stmt (pre_p, ret);
1163 return GS_ALL_DONE;
1166 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1167 result_decl = NULL_TREE;
1168 else
1170 result_decl = TREE_OPERAND (ret_expr, 0);
1172 /* See through a return by reference. */
1173 if (TREE_CODE (result_decl) == INDIRECT_REF)
1174 result_decl = TREE_OPERAND (result_decl, 0);
1176 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1177 || TREE_CODE (ret_expr) == INIT_EXPR)
1178 && TREE_CODE (result_decl) == RESULT_DECL);
1181 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1182 Recall that aggregate_value_p is FALSE for any aggregate type that is
1183 returned in registers. If we're returning values in registers, then
1184 we don't want to extend the lifetime of the RESULT_DECL, particularly
1185 across another call. In addition, for those aggregates for which
1186 hard_function_value generates a PARALLEL, we'll die during normal
1187 expansion of structure assignments; there's special code in expand_return
1188 to handle this case that does not exist in expand_expr. */
1189 if (!result_decl)
1190 result = NULL_TREE;
1191 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1193 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1195 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1196 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1197 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1198 should be effectively allocated by the caller, i.e. all calls to
1199 this function must be subject to the Return Slot Optimization. */
1200 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1201 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1203 result = result_decl;
1205 else if (gimplify_ctxp->return_temp)
1206 result = gimplify_ctxp->return_temp;
1207 else
1209 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1211 /* ??? With complex control flow (usually involving abnormal edges),
1212 we can wind up warning about an uninitialized value for this. Due
1213 to how this variable is constructed and initialized, this is never
1214 true. Give up and never warn. */
1215 TREE_NO_WARNING (result) = 1;
1217 gimplify_ctxp->return_temp = result;
1220 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1221 Then gimplify the whole thing. */
1222 if (result != result_decl)
1223 TREE_OPERAND (ret_expr, 0) = result;
1225 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1227 ret = gimple_build_return (result);
1228 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1229 gimplify_seq_add_stmt (pre_p, ret);
1231 return GS_ALL_DONE;
1234 /* Gimplify a variable-length array DECL. */
1236 static void
1237 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1239 /* This is a variable-sized decl. Simplify its size and mark it
1240 for deferred expansion. */
1241 tree t, addr, ptr_type;
1243 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1244 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1246 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1247 if (DECL_HAS_VALUE_EXPR_P (decl))
1248 return;
1250 /* All occurrences of this decl in final gimplified code will be
1251 replaced by indirection. Setting DECL_VALUE_EXPR does two
1252 things: First, it lets the rest of the gimplifier know what
1253 replacement to use. Second, it lets the debug info know
1254 where to find the value. */
1255 ptr_type = build_pointer_type (TREE_TYPE (decl));
1256 addr = create_tmp_var (ptr_type, get_name (decl));
1257 DECL_IGNORED_P (addr) = 0;
1258 t = build_fold_indirect_ref (addr);
1259 TREE_THIS_NOTRAP (t) = 1;
1260 SET_DECL_VALUE_EXPR (decl, t);
1261 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1263 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1264 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1265 size_int (DECL_ALIGN (decl)));
1266 /* The call has been built for a variable-sized object. */
1267 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1268 t = fold_convert (ptr_type, t);
1269 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1271 gimplify_and_add (t, seq_p);
1273 /* Indicate that we need to restore the stack level when the
1274 enclosing BIND_EXPR is exited. */
1275 gimplify_ctxp->save_stack = true;
1278 /* A helper function to be called via walk_tree. Mark all labels under *TP
1279 as being forced. To be called for DECL_INITIAL of static variables. */
1281 static tree
1282 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1284 if (TYPE_P (*tp))
1285 *walk_subtrees = 0;
1286 if (TREE_CODE (*tp) == LABEL_DECL)
1287 FORCED_LABEL (*tp) = 1;
1289 return NULL_TREE;
1292 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1293 and initialization explicit. */
1295 static enum gimplify_status
1296 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1298 tree stmt = *stmt_p;
1299 tree decl = DECL_EXPR_DECL (stmt);
1301 *stmt_p = NULL_TREE;
1303 if (TREE_TYPE (decl) == error_mark_node)
1304 return GS_ERROR;
1306 if ((TREE_CODE (decl) == TYPE_DECL
1307 || TREE_CODE (decl) == VAR_DECL)
1308 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1309 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1311 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1312 in case its size expressions contain problematic nodes like CALL_EXPR. */
1313 if (TREE_CODE (decl) == TYPE_DECL
1314 && DECL_ORIGINAL_TYPE (decl)
1315 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1316 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1318 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1320 tree init = DECL_INITIAL (decl);
1322 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1323 || (!TREE_STATIC (decl)
1324 && flag_stack_check == GENERIC_STACK_CHECK
1325 && compare_tree_int (DECL_SIZE_UNIT (decl),
1326 STACK_CHECK_MAX_VAR_SIZE) > 0))
1327 gimplify_vla_decl (decl, seq_p);
1329 /* Some front ends do not explicitly declare all anonymous
1330 artificial variables. We compensate here by declaring the
1331 variables, though it would be better if the front ends would
1332 explicitly declare them. */
1333 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1334 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1335 gimple_add_tmp_var (decl);
1337 if (init && init != error_mark_node)
1339 if (!TREE_STATIC (decl))
1341 DECL_INITIAL (decl) = NULL_TREE;
1342 init = build2 (INIT_EXPR, void_type_node, decl, init);
1343 gimplify_and_add (init, seq_p);
1344 ggc_free (init);
1346 else
1347 /* We must still examine initializers for static variables
1348 as they may contain a label address. */
1349 walk_tree (&init, force_labels_r, NULL, NULL);
1353 return GS_ALL_DONE;
1356 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1357 and replacing the LOOP_EXPR with goto, but if the loop contains an
1358 EXIT_EXPR, we need to append a label for it to jump to. */
1360 static enum gimplify_status
1361 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1363 tree saved_label = gimplify_ctxp->exit_label;
1364 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1366 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1368 gimplify_ctxp->exit_label = NULL_TREE;
1370 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1372 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1374 if (gimplify_ctxp->exit_label)
1375 gimplify_seq_add_stmt (pre_p,
1376 gimple_build_label (gimplify_ctxp->exit_label));
1378 gimplify_ctxp->exit_label = saved_label;
1380 *expr_p = NULL;
1381 return GS_ALL_DONE;
1384 /* Gimplify a statement list onto a sequence. These may be created either
1385 by an enlightened front-end, or by shortcut_cond_expr. */
1387 static enum gimplify_status
1388 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1390 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1392 tree_stmt_iterator i = tsi_start (*expr_p);
1394 while (!tsi_end_p (i))
1396 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1397 tsi_delink (&i);
1400 if (temp)
1402 *expr_p = temp;
1403 return GS_OK;
1406 return GS_ALL_DONE;
1410 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1411 branch to. */
1413 static enum gimplify_status
1414 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1416 tree switch_expr = *expr_p;
1417 gimple_seq switch_body_seq = NULL;
1418 enum gimplify_status ret;
1419 tree index_type = TREE_TYPE (switch_expr);
1420 if (index_type == NULL_TREE)
1421 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1423 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1424 fb_rvalue);
1425 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1426 return ret;
1428 if (SWITCH_BODY (switch_expr))
1430 vec<tree> labels;
1431 vec<tree> saved_labels;
1432 tree default_case = NULL_TREE;
1433 gimple gimple_switch;
1435 /* If someone can be bothered to fill in the labels, they can
1436 be bothered to null out the body too. */
1437 gcc_assert (!SWITCH_LABELS (switch_expr));
1439 /* Save old labels, get new ones from body, then restore the old
1440 labels. Save all the things from the switch body to append after. */
1441 saved_labels = gimplify_ctxp->case_labels;
1442 gimplify_ctxp->case_labels.create (8);
1444 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1445 labels = gimplify_ctxp->case_labels;
1446 gimplify_ctxp->case_labels = saved_labels;
1448 preprocess_case_label_vec_for_gimple (labels, index_type,
1449 &default_case);
1451 if (!default_case)
1453 gimple new_default;
1455 default_case
1456 = build_case_label (NULL_TREE, NULL_TREE,
1457 create_artificial_label (UNKNOWN_LOCATION));
1458 new_default = gimple_build_label (CASE_LABEL (default_case));
1459 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1462 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1463 default_case, labels);
1464 gimplify_seq_add_stmt (pre_p, gimple_switch);
1465 gimplify_seq_add_seq (pre_p, switch_body_seq);
1466 labels.release ();
1468 else
1469 gcc_assert (SWITCH_LABELS (switch_expr));
1471 return GS_ALL_DONE;
1474 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1476 static enum gimplify_status
1477 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1479 struct gimplify_ctx *ctxp;
1480 gimple gimple_label;
1482 /* Invalid OpenMP programs can play Duff's Device type games with
1483 #pragma omp parallel. At least in the C front end, we don't
1484 detect such invalid branches until after gimplification. */
1485 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1486 if (ctxp->case_labels.exists ())
1487 break;
1489 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1490 ctxp->case_labels.safe_push (*expr_p);
1491 gimplify_seq_add_stmt (pre_p, gimple_label);
1493 return GS_ALL_DONE;
1496 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1497 if necessary. */
1499 tree
1500 build_and_jump (tree *label_p)
1502 if (label_p == NULL)
1503 /* If there's nowhere to jump, just fall through. */
1504 return NULL_TREE;
1506 if (*label_p == NULL_TREE)
1508 tree label = create_artificial_label (UNKNOWN_LOCATION);
1509 *label_p = label;
1512 return build1 (GOTO_EXPR, void_type_node, *label_p);
1515 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1516 This also involves building a label to jump to and communicating it to
1517 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1519 static enum gimplify_status
1520 gimplify_exit_expr (tree *expr_p)
1522 tree cond = TREE_OPERAND (*expr_p, 0);
1523 tree expr;
1525 expr = build_and_jump (&gimplify_ctxp->exit_label);
1526 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1527 *expr_p = expr;
1529 return GS_OK;
1532 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1533 different from its canonical type, wrap the whole thing inside a
1534 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1535 type.
1537 The canonical type of a COMPONENT_REF is the type of the field being
1538 referenced--unless the field is a bit-field which can be read directly
1539 in a smaller mode, in which case the canonical type is the
1540 sign-appropriate type corresponding to that mode. */
1542 static void
1543 canonicalize_component_ref (tree *expr_p)
1545 tree expr = *expr_p;
1546 tree type;
1548 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1550 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1551 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1552 else
1553 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1555 /* One could argue that all the stuff below is not necessary for
1556 the non-bitfield case and declare it a FE error if type
1557 adjustment would be needed. */
1558 if (TREE_TYPE (expr) != type)
1560 #ifdef ENABLE_TYPES_CHECKING
1561 tree old_type = TREE_TYPE (expr);
1562 #endif
1563 int type_quals;
1565 /* We need to preserve qualifiers and propagate them from
1566 operand 0. */
1567 type_quals = TYPE_QUALS (type)
1568 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1569 if (TYPE_QUALS (type) != type_quals)
1570 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1572 /* Set the type of the COMPONENT_REF to the underlying type. */
1573 TREE_TYPE (expr) = type;
1575 #ifdef ENABLE_TYPES_CHECKING
1576 /* It is now a FE error, if the conversion from the canonical
1577 type to the original expression type is not useless. */
1578 gcc_assert (useless_type_conversion_p (old_type, type));
1579 #endif
1583 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1584 to foo, embed that change in the ADDR_EXPR by converting
1585 T array[U];
1586 (T *)&array
1588 &array[L]
1589 where L is the lower bound. For simplicity, only do this for constant
1590 lower bound.
1591 The constraint is that the type of &array[L] is trivially convertible
1592 to T *. */
1594 static void
1595 canonicalize_addr_expr (tree *expr_p)
1597 tree expr = *expr_p;
1598 tree addr_expr = TREE_OPERAND (expr, 0);
1599 tree datype, ddatype, pddatype;
1601 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1602 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1603 || TREE_CODE (addr_expr) != ADDR_EXPR)
1604 return;
1606 /* The addr_expr type should be a pointer to an array. */
1607 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1608 if (TREE_CODE (datype) != ARRAY_TYPE)
1609 return;
1611 /* The pointer to element type shall be trivially convertible to
1612 the expression pointer type. */
1613 ddatype = TREE_TYPE (datype);
1614 pddatype = build_pointer_type (ddatype);
1615 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1616 pddatype))
1617 return;
1619 /* The lower bound and element sizes must be constant. */
1620 if (!TYPE_SIZE_UNIT (ddatype)
1621 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1622 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1623 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1624 return;
1626 /* All checks succeeded. Build a new node to merge the cast. */
1627 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1628 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1629 NULL_TREE, NULL_TREE);
1630 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1632 /* We can have stripped a required restrict qualifier above. */
1633 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1634 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1637 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1638 underneath as appropriate. */
1640 static enum gimplify_status
1641 gimplify_conversion (tree *expr_p)
1643 location_t loc = EXPR_LOCATION (*expr_p);
1644 gcc_assert (CONVERT_EXPR_P (*expr_p));
1646 /* Then strip away all but the outermost conversion. */
1647 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1649 /* And remove the outermost conversion if it's useless. */
1650 if (tree_ssa_useless_type_conversion (*expr_p))
1651 *expr_p = TREE_OPERAND (*expr_p, 0);
1653 /* If we still have a conversion at the toplevel,
1654 then canonicalize some constructs. */
1655 if (CONVERT_EXPR_P (*expr_p))
1657 tree sub = TREE_OPERAND (*expr_p, 0);
1659 /* If a NOP conversion is changing the type of a COMPONENT_REF
1660 expression, then canonicalize its type now in order to expose more
1661 redundant conversions. */
1662 if (TREE_CODE (sub) == COMPONENT_REF)
1663 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1665 /* If a NOP conversion is changing a pointer to array of foo
1666 to a pointer to foo, embed that change in the ADDR_EXPR. */
1667 else if (TREE_CODE (sub) == ADDR_EXPR)
1668 canonicalize_addr_expr (expr_p);
1671 /* If we have a conversion to a non-register type force the
1672 use of a VIEW_CONVERT_EXPR instead. */
1673 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1674 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1675 TREE_OPERAND (*expr_p, 0));
1677 return GS_OK;
1680 /* Nonlocal VLAs seen in the current function. */
1681 static struct pointer_set_t *nonlocal_vlas;
1683 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1684 DECL_VALUE_EXPR, and it's worth re-examining things. */
1686 static enum gimplify_status
1687 gimplify_var_or_parm_decl (tree *expr_p)
1689 tree decl = *expr_p;
1691 /* ??? If this is a local variable, and it has not been seen in any
1692 outer BIND_EXPR, then it's probably the result of a duplicate
1693 declaration, for which we've already issued an error. It would
1694 be really nice if the front end wouldn't leak these at all.
1695 Currently the only known culprit is C++ destructors, as seen
1696 in g++.old-deja/g++.jason/binding.C. */
1697 if (TREE_CODE (decl) == VAR_DECL
1698 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1699 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1700 && decl_function_context (decl) == current_function_decl)
1702 gcc_assert (seen_error ());
1703 return GS_ERROR;
1706 /* When within an OpenMP context, notice uses of variables. */
1707 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1708 return GS_ALL_DONE;
1710 /* If the decl is an alias for another expression, substitute it now. */
1711 if (DECL_HAS_VALUE_EXPR_P (decl))
1713 tree value_expr = DECL_VALUE_EXPR (decl);
1715 /* For referenced nonlocal VLAs add a decl for debugging purposes
1716 to the current function. */
1717 if (TREE_CODE (decl) == VAR_DECL
1718 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1719 && nonlocal_vlas != NULL
1720 && TREE_CODE (value_expr) == INDIRECT_REF
1721 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1722 && decl_function_context (decl) != current_function_decl)
1724 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1725 while (ctx
1726 && (ctx->region_type == ORT_WORKSHARE
1727 || ctx->region_type == ORT_SIMD))
1728 ctx = ctx->outer_context;
1729 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
1731 tree copy = copy_node (decl), block;
1733 lang_hooks.dup_lang_specific_decl (copy);
1734 SET_DECL_RTL (copy, 0);
1735 TREE_USED (copy) = 1;
1736 block = DECL_INITIAL (current_function_decl);
1737 DECL_CHAIN (copy) = BLOCK_VARS (block);
1738 BLOCK_VARS (block) = copy;
1739 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1740 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1744 *expr_p = unshare_expr (value_expr);
1745 return GS_OK;
1748 return GS_ALL_DONE;
1751 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1752 node *EXPR_P.
1754 compound_lval
1755 : min_lval '[' val ']'
1756 | min_lval '.' ID
1757 | compound_lval '[' val ']'
1758 | compound_lval '.' ID
1760 This is not part of the original SIMPLE definition, which separates
1761 array and member references, but it seems reasonable to handle them
1762 together. Also, this way we don't run into problems with union
1763 aliasing; gcc requires that for accesses through a union to alias, the
1764 union reference must be explicit, which was not always the case when we
1765 were splitting up array and member refs.
1767 PRE_P points to the sequence where side effects that must happen before
1768 *EXPR_P should be stored.
1770 POST_P points to the sequence where side effects that must happen after
1771 *EXPR_P should be stored. */
1773 static enum gimplify_status
1774 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1775 fallback_t fallback)
1777 tree *p;
1778 enum gimplify_status ret = GS_ALL_DONE, tret;
1779 int i;
1780 location_t loc = EXPR_LOCATION (*expr_p);
1781 tree expr = *expr_p;
1783 /* Create a stack of the subexpressions so later we can walk them in
1784 order from inner to outer. */
1785 stack_vec<tree, 10> expr_stack;
1787 /* We can handle anything that get_inner_reference can deal with. */
1788 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1790 restart:
1791 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1792 if (TREE_CODE (*p) == INDIRECT_REF)
1793 *p = fold_indirect_ref_loc (loc, *p);
1795 if (handled_component_p (*p))
1797 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1798 additional COMPONENT_REFs. */
1799 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1800 && gimplify_var_or_parm_decl (p) == GS_OK)
1801 goto restart;
1802 else
1803 break;
1805 expr_stack.safe_push (*p);
1808 gcc_assert (expr_stack.length ());
1810 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1811 walked through and P points to the innermost expression.
1813 Java requires that we elaborated nodes in source order. That
1814 means we must gimplify the inner expression followed by each of
1815 the indices, in order. But we can't gimplify the inner
1816 expression until we deal with any variable bounds, sizes, or
1817 positions in order to deal with PLACEHOLDER_EXPRs.
1819 So we do this in three steps. First we deal with the annotations
1820 for any variables in the components, then we gimplify the base,
1821 then we gimplify any indices, from left to right. */
1822 for (i = expr_stack.length () - 1; i >= 0; i--)
1824 tree t = expr_stack[i];
1826 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1828 /* Gimplify the low bound and element type size and put them into
1829 the ARRAY_REF. If these values are set, they have already been
1830 gimplified. */
1831 if (TREE_OPERAND (t, 2) == NULL_TREE)
1833 tree low = unshare_expr (array_ref_low_bound (t));
1834 if (!is_gimple_min_invariant (low))
1836 TREE_OPERAND (t, 2) = low;
1837 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1838 post_p, is_gimple_reg,
1839 fb_rvalue);
1840 ret = MIN (ret, tret);
1843 else
1845 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1846 is_gimple_reg, fb_rvalue);
1847 ret = MIN (ret, tret);
1850 if (TREE_OPERAND (t, 3) == NULL_TREE)
1852 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1853 tree elmt_size = unshare_expr (array_ref_element_size (t));
1854 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1856 /* Divide the element size by the alignment of the element
1857 type (above). */
1858 elmt_size
1859 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1861 if (!is_gimple_min_invariant (elmt_size))
1863 TREE_OPERAND (t, 3) = elmt_size;
1864 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1865 post_p, is_gimple_reg,
1866 fb_rvalue);
1867 ret = MIN (ret, tret);
1870 else
1872 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1873 is_gimple_reg, fb_rvalue);
1874 ret = MIN (ret, tret);
1877 else if (TREE_CODE (t) == COMPONENT_REF)
1879 /* Set the field offset into T and gimplify it. */
1880 if (TREE_OPERAND (t, 2) == NULL_TREE)
1882 tree offset = unshare_expr (component_ref_field_offset (t));
1883 tree field = TREE_OPERAND (t, 1);
1884 tree factor
1885 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1887 /* Divide the offset by its alignment. */
1888 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
1890 if (!is_gimple_min_invariant (offset))
1892 TREE_OPERAND (t, 2) = offset;
1893 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1894 post_p, is_gimple_reg,
1895 fb_rvalue);
1896 ret = MIN (ret, tret);
1899 else
1901 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1902 is_gimple_reg, fb_rvalue);
1903 ret = MIN (ret, tret);
1908 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
1909 so as to match the min_lval predicate. Failure to do so may result
1910 in the creation of large aggregate temporaries. */
1911 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1912 fallback | fb_lvalue);
1913 ret = MIN (ret, tret);
1915 /* And finally, the indices and operands of ARRAY_REF. During this
1916 loop we also remove any useless conversions. */
1917 for (; expr_stack.length () > 0; )
1919 tree t = expr_stack.pop ();
1921 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1923 /* Gimplify the dimension. */
1924 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1926 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1927 is_gimple_val, fb_rvalue);
1928 ret = MIN (ret, tret);
1932 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
1934 /* The innermost expression P may have originally had
1935 TREE_SIDE_EFFECTS set which would have caused all the outer
1936 expressions in *EXPR_P leading to P to also have had
1937 TREE_SIDE_EFFECTS set. */
1938 recalculate_side_effects (t);
1941 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1942 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
1944 canonicalize_component_ref (expr_p);
1947 expr_stack.release ();
1949 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
1951 return ret;
1954 /* Gimplify the self modifying expression pointed to by EXPR_P
1955 (++, --, +=, -=).
1957 PRE_P points to the list where side effects that must happen before
1958 *EXPR_P should be stored.
1960 POST_P points to the list where side effects that must happen after
1961 *EXPR_P should be stored.
1963 WANT_VALUE is nonzero iff we want to use the value of this expression
1964 in another expression.
1966 ARITH_TYPE is the type the computation should be performed in. */
1968 enum gimplify_status
1969 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1970 bool want_value, tree arith_type)
1972 enum tree_code code;
1973 tree lhs, lvalue, rhs, t1;
1974 gimple_seq post = NULL, *orig_post_p = post_p;
1975 bool postfix;
1976 enum tree_code arith_code;
1977 enum gimplify_status ret;
1978 location_t loc = EXPR_LOCATION (*expr_p);
1980 code = TREE_CODE (*expr_p);
1982 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
1983 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
1985 /* Prefix or postfix? */
1986 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1987 /* Faster to treat as prefix if result is not used. */
1988 postfix = want_value;
1989 else
1990 postfix = false;
1992 /* For postfix, make sure the inner expression's post side effects
1993 are executed after side effects from this expression. */
1994 if (postfix)
1995 post_p = &post;
1997 /* Add or subtract? */
1998 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1999 arith_code = PLUS_EXPR;
2000 else
2001 arith_code = MINUS_EXPR;
2003 /* Gimplify the LHS into a GIMPLE lvalue. */
2004 lvalue = TREE_OPERAND (*expr_p, 0);
2005 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2006 if (ret == GS_ERROR)
2007 return ret;
2009 /* Extract the operands to the arithmetic operation. */
2010 lhs = lvalue;
2011 rhs = TREE_OPERAND (*expr_p, 1);
2013 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2014 that as the result value and in the postqueue operation. */
2015 if (postfix)
2017 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2018 if (ret == GS_ERROR)
2019 return ret;
2021 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2024 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2025 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2027 rhs = convert_to_ptrofftype_loc (loc, rhs);
2028 if (arith_code == MINUS_EXPR)
2029 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2030 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2032 else
2033 t1 = fold_convert (TREE_TYPE (*expr_p),
2034 fold_build2 (arith_code, arith_type,
2035 fold_convert (arith_type, lhs),
2036 fold_convert (arith_type, rhs)));
2038 if (postfix)
2040 gimplify_assign (lvalue, t1, pre_p);
2041 gimplify_seq_add_seq (orig_post_p, post);
2042 *expr_p = lhs;
2043 return GS_ALL_DONE;
2045 else
2047 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2048 return GS_OK;
2052 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2054 static void
2055 maybe_with_size_expr (tree *expr_p)
2057 tree expr = *expr_p;
2058 tree type = TREE_TYPE (expr);
2059 tree size;
2061 /* If we've already wrapped this or the type is error_mark_node, we can't do
2062 anything. */
2063 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2064 || type == error_mark_node)
2065 return;
2067 /* If the size isn't known or is a constant, we have nothing to do. */
2068 size = TYPE_SIZE_UNIT (type);
2069 if (!size || TREE_CODE (size) == INTEGER_CST)
2070 return;
2072 /* Otherwise, make a WITH_SIZE_EXPR. */
2073 size = unshare_expr (size);
2074 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2075 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2078 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2079 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2080 the CALL_EXPR. */
2082 static enum gimplify_status
2083 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2085 bool (*test) (tree);
2086 fallback_t fb;
2088 /* In general, we allow lvalues for function arguments to avoid
2089 extra overhead of copying large aggregates out of even larger
2090 aggregates into temporaries only to copy the temporaries to
2091 the argument list. Make optimizers happy by pulling out to
2092 temporaries those types that fit in registers. */
2093 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2094 test = is_gimple_val, fb = fb_rvalue;
2095 else
2097 test = is_gimple_lvalue, fb = fb_either;
2098 /* Also strip a TARGET_EXPR that would force an extra copy. */
2099 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2101 tree init = TARGET_EXPR_INITIAL (*arg_p);
2102 if (init
2103 && !VOID_TYPE_P (TREE_TYPE (init)))
2104 *arg_p = init;
2108 /* If this is a variable sized type, we must remember the size. */
2109 maybe_with_size_expr (arg_p);
2111 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2112 /* Make sure arguments have the same location as the function call
2113 itself. */
2114 protected_set_expr_location (*arg_p, call_location);
2116 /* There is a sequence point before a function call. Side effects in
2117 the argument list must occur before the actual call. So, when
2118 gimplifying arguments, force gimplify_expr to use an internal
2119 post queue which is then appended to the end of PRE_P. */
2120 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2123 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2124 WANT_VALUE is true if the result of the call is desired. */
2126 static enum gimplify_status
2127 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2129 tree fndecl, parms, p, fnptrtype;
2130 enum gimplify_status ret;
2131 int i, nargs;
2132 gimple call;
2133 bool builtin_va_start_p = FALSE;
2134 location_t loc = EXPR_LOCATION (*expr_p);
2136 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2138 /* For reliable diagnostics during inlining, it is necessary that
2139 every call_expr be annotated with file and line. */
2140 if (! EXPR_HAS_LOCATION (*expr_p))
2141 SET_EXPR_LOCATION (*expr_p, input_location);
2143 if (fn_contains_cilk_spawn_p (cfun)
2144 && lang_hooks.cilkplus.cilk_detect_spawn_and_unwrap (expr_p)
2145 && !seen_error ())
2146 return (enum gimplify_status)
2147 lang_hooks.cilkplus.gimplify_cilk_spawn (expr_p, pre_p, NULL);
2149 /* This may be a call to a builtin function.
2151 Builtin function calls may be transformed into different
2152 (and more efficient) builtin function calls under certain
2153 circumstances. Unfortunately, gimplification can muck things
2154 up enough that the builtin expanders are not aware that certain
2155 transformations are still valid.
2157 So we attempt transformation/gimplification of the call before
2158 we gimplify the CALL_EXPR. At this time we do not manage to
2159 transform all calls in the same manner as the expanders do, but
2160 we do transform most of them. */
2161 fndecl = get_callee_fndecl (*expr_p);
2162 if (fndecl
2163 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2164 switch (DECL_FUNCTION_CODE (fndecl))
2166 case BUILT_IN_VA_START:
2168 builtin_va_start_p = TRUE;
2169 if (call_expr_nargs (*expr_p) < 2)
2171 error ("too few arguments to function %<va_start%>");
2172 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2173 return GS_OK;
2176 if (fold_builtin_next_arg (*expr_p, true))
2178 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2179 return GS_OK;
2181 break;
2183 case BUILT_IN_LINE:
2185 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2186 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2187 return GS_OK;
2189 case BUILT_IN_FILE:
2191 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2192 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2193 return GS_OK;
2195 case BUILT_IN_FUNCTION:
2197 const char *function;
2198 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2199 *expr_p = build_string_literal (strlen (function) + 1, function);
2200 return GS_OK;
2202 default:
2205 if (fndecl && DECL_BUILT_IN (fndecl))
2207 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2208 if (new_tree && new_tree != *expr_p)
2210 /* There was a transformation of this call which computes the
2211 same value, but in a more efficient way. Return and try
2212 again. */
2213 *expr_p = new_tree;
2214 return GS_OK;
2218 /* Remember the original function pointer type. */
2219 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2221 /* There is a sequence point before the call, so any side effects in
2222 the calling expression must occur before the actual call. Force
2223 gimplify_expr to use an internal post queue. */
2224 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2225 is_gimple_call_addr, fb_rvalue);
2227 nargs = call_expr_nargs (*expr_p);
2229 /* Get argument types for verification. */
2230 fndecl = get_callee_fndecl (*expr_p);
2231 parms = NULL_TREE;
2232 if (fndecl)
2233 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2234 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2235 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2237 if (fndecl && DECL_ARGUMENTS (fndecl))
2238 p = DECL_ARGUMENTS (fndecl);
2239 else if (parms)
2240 p = parms;
2241 else
2242 p = NULL_TREE;
2243 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2246 /* If the last argument is __builtin_va_arg_pack () and it is not
2247 passed as a named argument, decrease the number of CALL_EXPR
2248 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2249 if (!p
2250 && i < nargs
2251 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2253 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2254 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2256 if (last_arg_fndecl
2257 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2258 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2259 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2261 tree call = *expr_p;
2263 --nargs;
2264 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2265 CALL_EXPR_FN (call),
2266 nargs, CALL_EXPR_ARGP (call));
2268 /* Copy all CALL_EXPR flags, location and block, except
2269 CALL_EXPR_VA_ARG_PACK flag. */
2270 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2271 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2272 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2273 = CALL_EXPR_RETURN_SLOT_OPT (call);
2274 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2275 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2277 /* Set CALL_EXPR_VA_ARG_PACK. */
2278 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2282 /* Finally, gimplify the function arguments. */
2283 if (nargs > 0)
2285 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2286 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2287 PUSH_ARGS_REVERSED ? i-- : i++)
2289 enum gimplify_status t;
2291 /* Avoid gimplifying the second argument to va_start, which needs to
2292 be the plain PARM_DECL. */
2293 if ((i != 1) || !builtin_va_start_p)
2295 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2296 EXPR_LOCATION (*expr_p));
2298 if (t == GS_ERROR)
2299 ret = GS_ERROR;
2304 /* Verify the function result. */
2305 if (want_value && fndecl
2306 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2308 error_at (loc, "using result of function returning %<void%>");
2309 ret = GS_ERROR;
2312 /* Try this again in case gimplification exposed something. */
2313 if (ret != GS_ERROR)
2315 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2317 if (new_tree && new_tree != *expr_p)
2319 /* There was a transformation of this call which computes the
2320 same value, but in a more efficient way. Return and try
2321 again. */
2322 *expr_p = new_tree;
2323 return GS_OK;
2326 else
2328 *expr_p = error_mark_node;
2329 return GS_ERROR;
2332 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2333 decl. This allows us to eliminate redundant or useless
2334 calls to "const" functions. */
2335 if (TREE_CODE (*expr_p) == CALL_EXPR)
2337 int flags = call_expr_flags (*expr_p);
2338 if (flags & (ECF_CONST | ECF_PURE)
2339 /* An infinite loop is considered a side effect. */
2340 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2341 TREE_SIDE_EFFECTS (*expr_p) = 0;
2344 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2345 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2346 form and delegate the creation of a GIMPLE_CALL to
2347 gimplify_modify_expr. This is always possible because when
2348 WANT_VALUE is true, the caller wants the result of this call into
2349 a temporary, which means that we will emit an INIT_EXPR in
2350 internal_get_tmp_var which will then be handled by
2351 gimplify_modify_expr. */
2352 if (!want_value)
2354 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2355 have to do is replicate it as a GIMPLE_CALL tuple. */
2356 gimple_stmt_iterator gsi;
2357 call = gimple_build_call_from_tree (*expr_p);
2358 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2359 notice_special_calls (call);
2360 gimplify_seq_add_stmt (pre_p, call);
2361 gsi = gsi_last (*pre_p);
2362 /* Don't fold stmts inside of target construct. We'll do it
2363 during omplower pass instead. */
2364 struct gimplify_omp_ctx *ctx;
2365 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2366 if (ctx->region_type == ORT_TARGET)
2367 break;
2368 if (ctx == NULL)
2369 fold_stmt (&gsi);
2370 *expr_p = NULL_TREE;
2372 else
2373 /* Remember the original function type. */
2374 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2375 CALL_EXPR_FN (*expr_p));
2377 return ret;
2380 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2381 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2383 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2384 condition is true or false, respectively. If null, we should generate
2385 our own to skip over the evaluation of this specific expression.
2387 LOCUS is the source location of the COND_EXPR.
2389 This function is the tree equivalent of do_jump.
2391 shortcut_cond_r should only be called by shortcut_cond_expr. */
2393 static tree
2394 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2395 location_t locus)
2397 tree local_label = NULL_TREE;
2398 tree t, expr = NULL;
2400 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2401 retain the shortcut semantics. Just insert the gotos here;
2402 shortcut_cond_expr will append the real blocks later. */
2403 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2405 location_t new_locus;
2407 /* Turn if (a && b) into
2409 if (a); else goto no;
2410 if (b) goto yes; else goto no;
2411 (no:) */
2413 if (false_label_p == NULL)
2414 false_label_p = &local_label;
2416 /* Keep the original source location on the first 'if'. */
2417 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2418 append_to_statement_list (t, &expr);
2420 /* Set the source location of the && on the second 'if'. */
2421 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2422 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2423 new_locus);
2424 append_to_statement_list (t, &expr);
2426 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2428 location_t new_locus;
2430 /* Turn if (a || b) into
2432 if (a) goto yes;
2433 if (b) goto yes; else goto no;
2434 (yes:) */
2436 if (true_label_p == NULL)
2437 true_label_p = &local_label;
2439 /* Keep the original source location on the first 'if'. */
2440 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2441 append_to_statement_list (t, &expr);
2443 /* Set the source location of the || on the second 'if'. */
2444 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2445 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2446 new_locus);
2447 append_to_statement_list (t, &expr);
2449 else if (TREE_CODE (pred) == COND_EXPR
2450 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2451 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2453 location_t new_locus;
2455 /* As long as we're messing with gotos, turn if (a ? b : c) into
2456 if (a)
2457 if (b) goto yes; else goto no;
2458 else
2459 if (c) goto yes; else goto no;
2461 Don't do this if one of the arms has void type, which can happen
2462 in C++ when the arm is throw. */
2464 /* Keep the original source location on the first 'if'. Set the source
2465 location of the ? on the second 'if'. */
2466 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2467 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2468 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2469 false_label_p, locus),
2470 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2471 false_label_p, new_locus));
2473 else
2475 expr = build3 (COND_EXPR, void_type_node, pred,
2476 build_and_jump (true_label_p),
2477 build_and_jump (false_label_p));
2478 SET_EXPR_LOCATION (expr, locus);
2481 if (local_label)
2483 t = build1 (LABEL_EXPR, void_type_node, local_label);
2484 append_to_statement_list (t, &expr);
2487 return expr;
2490 /* Given a conditional expression EXPR with short-circuit boolean
2491 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2492 predicate apart into the equivalent sequence of conditionals. */
2494 static tree
2495 shortcut_cond_expr (tree expr)
2497 tree pred = TREE_OPERAND (expr, 0);
2498 tree then_ = TREE_OPERAND (expr, 1);
2499 tree else_ = TREE_OPERAND (expr, 2);
2500 tree true_label, false_label, end_label, t;
2501 tree *true_label_p;
2502 tree *false_label_p;
2503 bool emit_end, emit_false, jump_over_else;
2504 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2505 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2507 /* First do simple transformations. */
2508 if (!else_se)
2510 /* If there is no 'else', turn
2511 if (a && b) then c
2512 into
2513 if (a) if (b) then c. */
2514 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2516 /* Keep the original source location on the first 'if'. */
2517 location_t locus = EXPR_LOC_OR_HERE (expr);
2518 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2519 /* Set the source location of the && on the second 'if'. */
2520 if (EXPR_HAS_LOCATION (pred))
2521 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2522 then_ = shortcut_cond_expr (expr);
2523 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2524 pred = TREE_OPERAND (pred, 0);
2525 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2526 SET_EXPR_LOCATION (expr, locus);
2530 if (!then_se)
2532 /* If there is no 'then', turn
2533 if (a || b); else d
2534 into
2535 if (a); else if (b); else d. */
2536 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2538 /* Keep the original source location on the first 'if'. */
2539 location_t locus = EXPR_LOC_OR_HERE (expr);
2540 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2541 /* Set the source location of the || on the second 'if'. */
2542 if (EXPR_HAS_LOCATION (pred))
2543 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2544 else_ = shortcut_cond_expr (expr);
2545 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2546 pred = TREE_OPERAND (pred, 0);
2547 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2548 SET_EXPR_LOCATION (expr, locus);
2552 /* If we're done, great. */
2553 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2554 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2555 return expr;
2557 /* Otherwise we need to mess with gotos. Change
2558 if (a) c; else d;
2560 if (a); else goto no;
2561 c; goto end;
2562 no: d; end:
2563 and recursively gimplify the condition. */
2565 true_label = false_label = end_label = NULL_TREE;
2567 /* If our arms just jump somewhere, hijack those labels so we don't
2568 generate jumps to jumps. */
2570 if (then_
2571 && TREE_CODE (then_) == GOTO_EXPR
2572 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2574 true_label = GOTO_DESTINATION (then_);
2575 then_ = NULL;
2576 then_se = false;
2579 if (else_
2580 && TREE_CODE (else_) == GOTO_EXPR
2581 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2583 false_label = GOTO_DESTINATION (else_);
2584 else_ = NULL;
2585 else_se = false;
2588 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2589 if (true_label)
2590 true_label_p = &true_label;
2591 else
2592 true_label_p = NULL;
2594 /* The 'else' branch also needs a label if it contains interesting code. */
2595 if (false_label || else_se)
2596 false_label_p = &false_label;
2597 else
2598 false_label_p = NULL;
2600 /* If there was nothing else in our arms, just forward the label(s). */
2601 if (!then_se && !else_se)
2602 return shortcut_cond_r (pred, true_label_p, false_label_p,
2603 EXPR_LOC_OR_HERE (expr));
2605 /* If our last subexpression already has a terminal label, reuse it. */
2606 if (else_se)
2607 t = expr_last (else_);
2608 else if (then_se)
2609 t = expr_last (then_);
2610 else
2611 t = NULL;
2612 if (t && TREE_CODE (t) == LABEL_EXPR)
2613 end_label = LABEL_EXPR_LABEL (t);
2615 /* If we don't care about jumping to the 'else' branch, jump to the end
2616 if the condition is false. */
2617 if (!false_label_p)
2618 false_label_p = &end_label;
2620 /* We only want to emit these labels if we aren't hijacking them. */
2621 emit_end = (end_label == NULL_TREE);
2622 emit_false = (false_label == NULL_TREE);
2624 /* We only emit the jump over the else clause if we have to--if the
2625 then clause may fall through. Otherwise we can wind up with a
2626 useless jump and a useless label at the end of gimplified code,
2627 which will cause us to think that this conditional as a whole
2628 falls through even if it doesn't. If we then inline a function
2629 which ends with such a condition, that can cause us to issue an
2630 inappropriate warning about control reaching the end of a
2631 non-void function. */
2632 jump_over_else = block_may_fallthru (then_);
2634 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2635 EXPR_LOC_OR_HERE (expr));
2637 expr = NULL;
2638 append_to_statement_list (pred, &expr);
2640 append_to_statement_list (then_, &expr);
2641 if (else_se)
2643 if (jump_over_else)
2645 tree last = expr_last (expr);
2646 t = build_and_jump (&end_label);
2647 if (EXPR_HAS_LOCATION (last))
2648 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2649 append_to_statement_list (t, &expr);
2651 if (emit_false)
2653 t = build1 (LABEL_EXPR, void_type_node, false_label);
2654 append_to_statement_list (t, &expr);
2656 append_to_statement_list (else_, &expr);
2658 if (emit_end && end_label)
2660 t = build1 (LABEL_EXPR, void_type_node, end_label);
2661 append_to_statement_list (t, &expr);
2664 return expr;
2667 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2669 tree
2670 gimple_boolify (tree expr)
2672 tree type = TREE_TYPE (expr);
2673 location_t loc = EXPR_LOCATION (expr);
2675 if (TREE_CODE (expr) == NE_EXPR
2676 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2677 && integer_zerop (TREE_OPERAND (expr, 1)))
2679 tree call = TREE_OPERAND (expr, 0);
2680 tree fn = get_callee_fndecl (call);
2682 /* For __builtin_expect ((long) (x), y) recurse into x as well
2683 if x is truth_value_p. */
2684 if (fn
2685 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2686 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2687 && call_expr_nargs (call) == 2)
2689 tree arg = CALL_EXPR_ARG (call, 0);
2690 if (arg)
2692 if (TREE_CODE (arg) == NOP_EXPR
2693 && TREE_TYPE (arg) == TREE_TYPE (call))
2694 arg = TREE_OPERAND (arg, 0);
2695 if (truth_value_p (TREE_CODE (arg)))
2697 arg = gimple_boolify (arg);
2698 CALL_EXPR_ARG (call, 0)
2699 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2705 switch (TREE_CODE (expr))
2707 case TRUTH_AND_EXPR:
2708 case TRUTH_OR_EXPR:
2709 case TRUTH_XOR_EXPR:
2710 case TRUTH_ANDIF_EXPR:
2711 case TRUTH_ORIF_EXPR:
2712 /* Also boolify the arguments of truth exprs. */
2713 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2714 /* FALLTHRU */
2716 case TRUTH_NOT_EXPR:
2717 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2719 /* These expressions always produce boolean results. */
2720 if (TREE_CODE (type) != BOOLEAN_TYPE)
2721 TREE_TYPE (expr) = boolean_type_node;
2722 return expr;
2724 case ANNOTATE_EXPR:
2725 if ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1))
2726 == annot_expr_ivdep_kind)
2728 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2729 if (TREE_CODE (type) != BOOLEAN_TYPE)
2730 TREE_TYPE (expr) = boolean_type_node;
2731 return expr;
2733 /* FALLTHRU */
2735 default:
2736 if (COMPARISON_CLASS_P (expr))
2738 /* There expressions always prduce boolean results. */
2739 if (TREE_CODE (type) != BOOLEAN_TYPE)
2740 TREE_TYPE (expr) = boolean_type_node;
2741 return expr;
2743 /* Other expressions that get here must have boolean values, but
2744 might need to be converted to the appropriate mode. */
2745 if (TREE_CODE (type) == BOOLEAN_TYPE)
2746 return expr;
2747 return fold_convert_loc (loc, boolean_type_node, expr);
2751 /* Given a conditional expression *EXPR_P without side effects, gimplify
2752 its operands. New statements are inserted to PRE_P. */
2754 static enum gimplify_status
2755 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2757 tree expr = *expr_p, cond;
2758 enum gimplify_status ret, tret;
2759 enum tree_code code;
2761 cond = gimple_boolify (COND_EXPR_COND (expr));
2763 /* We need to handle && and || specially, as their gimplification
2764 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2765 code = TREE_CODE (cond);
2766 if (code == TRUTH_ANDIF_EXPR)
2767 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2768 else if (code == TRUTH_ORIF_EXPR)
2769 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2770 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2771 COND_EXPR_COND (*expr_p) = cond;
2773 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2774 is_gimple_val, fb_rvalue);
2775 ret = MIN (ret, tret);
2776 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2777 is_gimple_val, fb_rvalue);
2779 return MIN (ret, tret);
2782 /* Return true if evaluating EXPR could trap.
2783 EXPR is GENERIC, while tree_could_trap_p can be called
2784 only on GIMPLE. */
2786 static bool
2787 generic_expr_could_trap_p (tree expr)
2789 unsigned i, n;
2791 if (!expr || is_gimple_val (expr))
2792 return false;
2794 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2795 return true;
2797 n = TREE_OPERAND_LENGTH (expr);
2798 for (i = 0; i < n; i++)
2799 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2800 return true;
2802 return false;
2805 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2806 into
2808 if (p) if (p)
2809 t1 = a; a;
2810 else or else
2811 t1 = b; b;
2814 The second form is used when *EXPR_P is of type void.
2816 PRE_P points to the list where side effects that must happen before
2817 *EXPR_P should be stored. */
2819 static enum gimplify_status
2820 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2822 tree expr = *expr_p;
2823 tree type = TREE_TYPE (expr);
2824 location_t loc = EXPR_LOCATION (expr);
2825 tree tmp, arm1, arm2;
2826 enum gimplify_status ret;
2827 tree label_true, label_false, label_cont;
2828 bool have_then_clause_p, have_else_clause_p;
2829 gimple gimple_cond;
2830 enum tree_code pred_code;
2831 gimple_seq seq = NULL;
2833 /* If this COND_EXPR has a value, copy the values into a temporary within
2834 the arms. */
2835 if (!VOID_TYPE_P (type))
2837 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2838 tree result;
2840 /* If either an rvalue is ok or we do not require an lvalue, create the
2841 temporary. But we cannot do that if the type is addressable. */
2842 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
2843 && !TREE_ADDRESSABLE (type))
2845 if (gimplify_ctxp->allow_rhs_cond_expr
2846 /* If either branch has side effects or could trap, it can't be
2847 evaluated unconditionally. */
2848 && !TREE_SIDE_EFFECTS (then_)
2849 && !generic_expr_could_trap_p (then_)
2850 && !TREE_SIDE_EFFECTS (else_)
2851 && !generic_expr_could_trap_p (else_))
2852 return gimplify_pure_cond_expr (expr_p, pre_p);
2854 tmp = create_tmp_var (type, "iftmp");
2855 result = tmp;
2858 /* Otherwise, only create and copy references to the values. */
2859 else
2861 type = build_pointer_type (type);
2863 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2864 then_ = build_fold_addr_expr_loc (loc, then_);
2866 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2867 else_ = build_fold_addr_expr_loc (loc, else_);
2869 expr
2870 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
2872 tmp = create_tmp_var (type, "iftmp");
2873 result = build_simple_mem_ref_loc (loc, tmp);
2876 /* Build the new then clause, `tmp = then_;'. But don't build the
2877 assignment if the value is void; in C++ it can be if it's a throw. */
2878 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2879 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
2881 /* Similarly, build the new else clause, `tmp = else_;'. */
2882 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2883 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
2885 TREE_TYPE (expr) = void_type_node;
2886 recalculate_side_effects (expr);
2888 /* Move the COND_EXPR to the prequeue. */
2889 gimplify_stmt (&expr, pre_p);
2891 *expr_p = result;
2892 return GS_ALL_DONE;
2895 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
2896 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
2897 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
2898 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
2900 /* Make sure the condition has BOOLEAN_TYPE. */
2901 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2903 /* Break apart && and || conditions. */
2904 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2905 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2907 expr = shortcut_cond_expr (expr);
2909 if (expr != *expr_p)
2911 *expr_p = expr;
2913 /* We can't rely on gimplify_expr to re-gimplify the expanded
2914 form properly, as cleanups might cause the target labels to be
2915 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2916 set up a conditional context. */
2917 gimple_push_condition ();
2918 gimplify_stmt (expr_p, &seq);
2919 gimple_pop_condition (pre_p);
2920 gimple_seq_add_seq (pre_p, seq);
2922 return GS_ALL_DONE;
2926 /* Now do the normal gimplification. */
2928 /* Gimplify condition. */
2929 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
2930 fb_rvalue);
2931 if (ret == GS_ERROR)
2932 return GS_ERROR;
2933 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
2935 gimple_push_condition ();
2937 have_then_clause_p = have_else_clause_p = false;
2938 if (TREE_OPERAND (expr, 1) != NULL
2939 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
2940 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
2941 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
2942 == current_function_decl)
2943 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
2944 have different locations, otherwise we end up with incorrect
2945 location information on the branches. */
2946 && (optimize
2947 || !EXPR_HAS_LOCATION (expr)
2948 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
2949 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
2951 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
2952 have_then_clause_p = true;
2954 else
2955 label_true = create_artificial_label (UNKNOWN_LOCATION);
2956 if (TREE_OPERAND (expr, 2) != NULL
2957 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
2958 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
2959 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
2960 == current_function_decl)
2961 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
2962 have different locations, otherwise we end up with incorrect
2963 location information on the branches. */
2964 && (optimize
2965 || !EXPR_HAS_LOCATION (expr)
2966 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
2967 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
2969 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
2970 have_else_clause_p = true;
2972 else
2973 label_false = create_artificial_label (UNKNOWN_LOCATION);
2975 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
2976 &arm2);
2978 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
2979 label_false);
2981 gimplify_seq_add_stmt (&seq, gimple_cond);
2982 label_cont = NULL_TREE;
2983 if (!have_then_clause_p)
2985 /* For if (...) {} else { code; } put label_true after
2986 the else block. */
2987 if (TREE_OPERAND (expr, 1) == NULL_TREE
2988 && !have_else_clause_p
2989 && TREE_OPERAND (expr, 2) != NULL_TREE)
2990 label_cont = label_true;
2991 else
2993 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
2994 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
2995 /* For if (...) { code; } else {} or
2996 if (...) { code; } else goto label; or
2997 if (...) { code; return; } else { ... }
2998 label_cont isn't needed. */
2999 if (!have_else_clause_p
3000 && TREE_OPERAND (expr, 2) != NULL_TREE
3001 && gimple_seq_may_fallthru (seq))
3003 gimple g;
3004 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3006 g = gimple_build_goto (label_cont);
3008 /* GIMPLE_COND's are very low level; they have embedded
3009 gotos. This particular embedded goto should not be marked
3010 with the location of the original COND_EXPR, as it would
3011 correspond to the COND_EXPR's condition, not the ELSE or the
3012 THEN arms. To avoid marking it with the wrong location, flag
3013 it as "no location". */
3014 gimple_set_do_not_emit_location (g);
3016 gimplify_seq_add_stmt (&seq, g);
3020 if (!have_else_clause_p)
3022 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3023 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3025 if (label_cont)
3026 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3028 gimple_pop_condition (pre_p);
3029 gimple_seq_add_seq (pre_p, seq);
3031 if (ret == GS_ERROR)
3032 ; /* Do nothing. */
3033 else if (have_then_clause_p || have_else_clause_p)
3034 ret = GS_ALL_DONE;
3035 else
3037 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3038 expr = TREE_OPERAND (expr, 0);
3039 gimplify_stmt (&expr, pre_p);
3042 *expr_p = NULL;
3043 return ret;
3046 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3047 to be marked addressable.
3049 We cannot rely on such an expression being directly markable if a temporary
3050 has been created by the gimplification. In this case, we create another
3051 temporary and initialize it with a copy, which will become a store after we
3052 mark it addressable. This can happen if the front-end passed us something
3053 that it could not mark addressable yet, like a Fortran pass-by-reference
3054 parameter (int) floatvar. */
3056 static void
3057 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3059 while (handled_component_p (*expr_p))
3060 expr_p = &TREE_OPERAND (*expr_p, 0);
3061 if (is_gimple_reg (*expr_p))
3062 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3065 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3066 a call to __builtin_memcpy. */
3068 static enum gimplify_status
3069 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3070 gimple_seq *seq_p)
3072 tree t, to, to_ptr, from, from_ptr;
3073 gimple gs;
3074 location_t loc = EXPR_LOCATION (*expr_p);
3076 to = TREE_OPERAND (*expr_p, 0);
3077 from = TREE_OPERAND (*expr_p, 1);
3079 /* Mark the RHS addressable. Beware that it may not be possible to do so
3080 directly if a temporary has been created by the gimplification. */
3081 prepare_gimple_addressable (&from, seq_p);
3083 mark_addressable (from);
3084 from_ptr = build_fold_addr_expr_loc (loc, from);
3085 gimplify_arg (&from_ptr, seq_p, loc);
3087 mark_addressable (to);
3088 to_ptr = build_fold_addr_expr_loc (loc, to);
3089 gimplify_arg (&to_ptr, seq_p, loc);
3091 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3093 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3095 if (want_value)
3097 /* tmp = memcpy() */
3098 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3099 gimple_call_set_lhs (gs, t);
3100 gimplify_seq_add_stmt (seq_p, gs);
3102 *expr_p = build_simple_mem_ref (t);
3103 return GS_ALL_DONE;
3106 gimplify_seq_add_stmt (seq_p, gs);
3107 *expr_p = NULL;
3108 return GS_ALL_DONE;
3111 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3112 a call to __builtin_memset. In this case we know that the RHS is
3113 a CONSTRUCTOR with an empty element list. */
3115 static enum gimplify_status
3116 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3117 gimple_seq *seq_p)
3119 tree t, from, to, to_ptr;
3120 gimple gs;
3121 location_t loc = EXPR_LOCATION (*expr_p);
3123 /* Assert our assumptions, to abort instead of producing wrong code
3124 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3125 not be immediately exposed. */
3126 from = TREE_OPERAND (*expr_p, 1);
3127 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3128 from = TREE_OPERAND (from, 0);
3130 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3131 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3133 /* Now proceed. */
3134 to = TREE_OPERAND (*expr_p, 0);
3136 to_ptr = build_fold_addr_expr_loc (loc, to);
3137 gimplify_arg (&to_ptr, seq_p, loc);
3138 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3140 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3142 if (want_value)
3144 /* tmp = memset() */
3145 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3146 gimple_call_set_lhs (gs, t);
3147 gimplify_seq_add_stmt (seq_p, gs);
3149 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3150 return GS_ALL_DONE;
3153 gimplify_seq_add_stmt (seq_p, gs);
3154 *expr_p = NULL;
3155 return GS_ALL_DONE;
3158 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3159 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3160 assignment. Return non-null if we detect a potential overlap. */
3162 struct gimplify_init_ctor_preeval_data
3164 /* The base decl of the lhs object. May be NULL, in which case we
3165 have to assume the lhs is indirect. */
3166 tree lhs_base_decl;
3168 /* The alias set of the lhs object. */
3169 alias_set_type lhs_alias_set;
3172 static tree
3173 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3175 struct gimplify_init_ctor_preeval_data *data
3176 = (struct gimplify_init_ctor_preeval_data *) xdata;
3177 tree t = *tp;
3179 /* If we find the base object, obviously we have overlap. */
3180 if (data->lhs_base_decl == t)
3181 return t;
3183 /* If the constructor component is indirect, determine if we have a
3184 potential overlap with the lhs. The only bits of information we
3185 have to go on at this point are addressability and alias sets. */
3186 if ((INDIRECT_REF_P (t)
3187 || TREE_CODE (t) == MEM_REF)
3188 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3189 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3190 return t;
3192 /* If the constructor component is a call, determine if it can hide a
3193 potential overlap with the lhs through an INDIRECT_REF like above.
3194 ??? Ugh - this is completely broken. In fact this whole analysis
3195 doesn't look conservative. */
3196 if (TREE_CODE (t) == CALL_EXPR)
3198 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3200 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3201 if (POINTER_TYPE_P (TREE_VALUE (type))
3202 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3203 && alias_sets_conflict_p (data->lhs_alias_set,
3204 get_alias_set
3205 (TREE_TYPE (TREE_VALUE (type)))))
3206 return t;
3209 if (IS_TYPE_OR_DECL_P (t))
3210 *walk_subtrees = 0;
3211 return NULL;
3214 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3215 force values that overlap with the lhs (as described by *DATA)
3216 into temporaries. */
3218 static void
3219 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3220 struct gimplify_init_ctor_preeval_data *data)
3222 enum gimplify_status one;
3224 /* If the value is constant, then there's nothing to pre-evaluate. */
3225 if (TREE_CONSTANT (*expr_p))
3227 /* Ensure it does not have side effects, it might contain a reference to
3228 the object we're initializing. */
3229 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3230 return;
3233 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3234 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3235 return;
3237 /* Recurse for nested constructors. */
3238 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3240 unsigned HOST_WIDE_INT ix;
3241 constructor_elt *ce;
3242 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3244 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3245 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3247 return;
3250 /* If this is a variable sized type, we must remember the size. */
3251 maybe_with_size_expr (expr_p);
3253 /* Gimplify the constructor element to something appropriate for the rhs
3254 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3255 the gimplifier will consider this a store to memory. Doing this
3256 gimplification now means that we won't have to deal with complicated
3257 language-specific trees, nor trees like SAVE_EXPR that can induce
3258 exponential search behavior. */
3259 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3260 if (one == GS_ERROR)
3262 *expr_p = NULL;
3263 return;
3266 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3267 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3268 always be true for all scalars, since is_gimple_mem_rhs insists on a
3269 temporary variable for them. */
3270 if (DECL_P (*expr_p))
3271 return;
3273 /* If this is of variable size, we have no choice but to assume it doesn't
3274 overlap since we can't make a temporary for it. */
3275 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3276 return;
3278 /* Otherwise, we must search for overlap ... */
3279 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3280 return;
3282 /* ... and if found, force the value into a temporary. */
3283 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3286 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3287 a RANGE_EXPR in a CONSTRUCTOR for an array.
3289 var = lower;
3290 loop_entry:
3291 object[var] = value;
3292 if (var == upper)
3293 goto loop_exit;
3294 var = var + 1;
3295 goto loop_entry;
3296 loop_exit:
3298 We increment var _after_ the loop exit check because we might otherwise
3299 fail if upper == TYPE_MAX_VALUE (type for upper).
3301 Note that we never have to deal with SAVE_EXPRs here, because this has
3302 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3304 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3305 gimple_seq *, bool);
3307 static void
3308 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3309 tree value, tree array_elt_type,
3310 gimple_seq *pre_p, bool cleared)
3312 tree loop_entry_label, loop_exit_label, fall_thru_label;
3313 tree var, var_type, cref, tmp;
3315 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3316 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3317 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3319 /* Create and initialize the index variable. */
3320 var_type = TREE_TYPE (upper);
3321 var = create_tmp_var (var_type, NULL);
3322 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3324 /* Add the loop entry label. */
3325 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3327 /* Build the reference. */
3328 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3329 var, NULL_TREE, NULL_TREE);
3331 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3332 the store. Otherwise just assign value to the reference. */
3334 if (TREE_CODE (value) == CONSTRUCTOR)
3335 /* NB we might have to call ourself recursively through
3336 gimplify_init_ctor_eval if the value is a constructor. */
3337 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3338 pre_p, cleared);
3339 else
3340 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3342 /* We exit the loop when the index var is equal to the upper bound. */
3343 gimplify_seq_add_stmt (pre_p,
3344 gimple_build_cond (EQ_EXPR, var, upper,
3345 loop_exit_label, fall_thru_label));
3347 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3349 /* Otherwise, increment the index var... */
3350 tmp = build2 (PLUS_EXPR, var_type, var,
3351 fold_convert (var_type, integer_one_node));
3352 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3354 /* ...and jump back to the loop entry. */
3355 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3357 /* Add the loop exit label. */
3358 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3361 /* Return true if FDECL is accessing a field that is zero sized. */
3363 static bool
3364 zero_sized_field_decl (const_tree fdecl)
3366 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3367 && integer_zerop (DECL_SIZE (fdecl)))
3368 return true;
3369 return false;
3372 /* Return true if TYPE is zero sized. */
3374 static bool
3375 zero_sized_type (const_tree type)
3377 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3378 && integer_zerop (TYPE_SIZE (type)))
3379 return true;
3380 return false;
3383 /* A subroutine of gimplify_init_constructor. Generate individual
3384 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3385 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3386 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3387 zeroed first. */
3389 static void
3390 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3391 gimple_seq *pre_p, bool cleared)
3393 tree array_elt_type = NULL;
3394 unsigned HOST_WIDE_INT ix;
3395 tree purpose, value;
3397 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3398 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3400 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3402 tree cref;
3404 /* NULL values are created above for gimplification errors. */
3405 if (value == NULL)
3406 continue;
3408 if (cleared && initializer_zerop (value))
3409 continue;
3411 /* ??? Here's to hoping the front end fills in all of the indices,
3412 so we don't have to figure out what's missing ourselves. */
3413 gcc_assert (purpose);
3415 /* Skip zero-sized fields, unless value has side-effects. This can
3416 happen with calls to functions returning a zero-sized type, which
3417 we shouldn't discard. As a number of downstream passes don't
3418 expect sets of zero-sized fields, we rely on the gimplification of
3419 the MODIFY_EXPR we make below to drop the assignment statement. */
3420 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3421 continue;
3423 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3424 whole range. */
3425 if (TREE_CODE (purpose) == RANGE_EXPR)
3427 tree lower = TREE_OPERAND (purpose, 0);
3428 tree upper = TREE_OPERAND (purpose, 1);
3430 /* If the lower bound is equal to upper, just treat it as if
3431 upper was the index. */
3432 if (simple_cst_equal (lower, upper))
3433 purpose = upper;
3434 else
3436 gimplify_init_ctor_eval_range (object, lower, upper, value,
3437 array_elt_type, pre_p, cleared);
3438 continue;
3442 if (array_elt_type)
3444 /* Do not use bitsizetype for ARRAY_REF indices. */
3445 if (TYPE_DOMAIN (TREE_TYPE (object)))
3446 purpose
3447 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3448 purpose);
3449 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3450 purpose, NULL_TREE, NULL_TREE);
3452 else
3454 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3455 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3456 unshare_expr (object), purpose, NULL_TREE);
3459 if (TREE_CODE (value) == CONSTRUCTOR
3460 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3461 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3462 pre_p, cleared);
3463 else
3465 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3466 gimplify_and_add (init, pre_p);
3467 ggc_free (init);
3472 /* Return the appropriate RHS predicate for this LHS. */
3474 gimple_predicate
3475 rhs_predicate_for (tree lhs)
3477 if (is_gimple_reg (lhs))
3478 return is_gimple_reg_rhs_or_call;
3479 else
3480 return is_gimple_mem_rhs_or_call;
3483 /* Gimplify a C99 compound literal expression. This just means adding
3484 the DECL_EXPR before the current statement and using its anonymous
3485 decl instead. */
3487 static enum gimplify_status
3488 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3489 bool (*gimple_test_f) (tree),
3490 fallback_t fallback)
3492 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3493 tree decl = DECL_EXPR_DECL (decl_s);
3494 tree init = DECL_INITIAL (decl);
3495 /* Mark the decl as addressable if the compound literal
3496 expression is addressable now, otherwise it is marked too late
3497 after we gimplify the initialization expression. */
3498 if (TREE_ADDRESSABLE (*expr_p))
3499 TREE_ADDRESSABLE (decl) = 1;
3500 /* Otherwise, if we don't need an lvalue and have a literal directly
3501 substitute it. Check if it matches the gimple predicate, as
3502 otherwise we'd generate a new temporary, and we can as well just
3503 use the decl we already have. */
3504 else if (!TREE_ADDRESSABLE (decl)
3505 && init
3506 && (fallback & fb_lvalue) == 0
3507 && gimple_test_f (init))
3509 *expr_p = init;
3510 return GS_OK;
3513 /* Preliminarily mark non-addressed complex variables as eligible
3514 for promotion to gimple registers. We'll transform their uses
3515 as we find them. */
3516 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3517 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3518 && !TREE_THIS_VOLATILE (decl)
3519 && !needs_to_live_in_memory (decl))
3520 DECL_GIMPLE_REG_P (decl) = 1;
3522 /* If the decl is not addressable, then it is being used in some
3523 expression or on the right hand side of a statement, and it can
3524 be put into a readonly data section. */
3525 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3526 TREE_READONLY (decl) = 1;
3528 /* This decl isn't mentioned in the enclosing block, so add it to the
3529 list of temps. FIXME it seems a bit of a kludge to say that
3530 anonymous artificial vars aren't pushed, but everything else is. */
3531 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3532 gimple_add_tmp_var (decl);
3534 gimplify_and_add (decl_s, pre_p);
3535 *expr_p = decl;
3536 return GS_OK;
3539 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3540 return a new CONSTRUCTOR if something changed. */
3542 static tree
3543 optimize_compound_literals_in_ctor (tree orig_ctor)
3545 tree ctor = orig_ctor;
3546 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3547 unsigned int idx, num = vec_safe_length (elts);
3549 for (idx = 0; idx < num; idx++)
3551 tree value = (*elts)[idx].value;
3552 tree newval = value;
3553 if (TREE_CODE (value) == CONSTRUCTOR)
3554 newval = optimize_compound_literals_in_ctor (value);
3555 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3557 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3558 tree decl = DECL_EXPR_DECL (decl_s);
3559 tree init = DECL_INITIAL (decl);
3561 if (!TREE_ADDRESSABLE (value)
3562 && !TREE_ADDRESSABLE (decl)
3563 && init
3564 && TREE_CODE (init) == CONSTRUCTOR)
3565 newval = optimize_compound_literals_in_ctor (init);
3567 if (newval == value)
3568 continue;
3570 if (ctor == orig_ctor)
3572 ctor = copy_node (orig_ctor);
3573 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3574 elts = CONSTRUCTOR_ELTS (ctor);
3576 (*elts)[idx].value = newval;
3578 return ctor;
3581 /* A subroutine of gimplify_modify_expr. Break out elements of a
3582 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3584 Note that we still need to clear any elements that don't have explicit
3585 initializers, so if not all elements are initialized we keep the
3586 original MODIFY_EXPR, we just remove all of the constructor elements.
3588 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3589 GS_ERROR if we would have to create a temporary when gimplifying
3590 this constructor. Otherwise, return GS_OK.
3592 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3594 static enum gimplify_status
3595 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3596 bool want_value, bool notify_temp_creation)
3598 tree object, ctor, type;
3599 enum gimplify_status ret;
3600 vec<constructor_elt, va_gc> *elts;
3602 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3604 if (!notify_temp_creation)
3606 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3607 is_gimple_lvalue, fb_lvalue);
3608 if (ret == GS_ERROR)
3609 return ret;
3612 object = TREE_OPERAND (*expr_p, 0);
3613 ctor = TREE_OPERAND (*expr_p, 1) =
3614 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3615 type = TREE_TYPE (ctor);
3616 elts = CONSTRUCTOR_ELTS (ctor);
3617 ret = GS_ALL_DONE;
3619 switch (TREE_CODE (type))
3621 case RECORD_TYPE:
3622 case UNION_TYPE:
3623 case QUAL_UNION_TYPE:
3624 case ARRAY_TYPE:
3626 struct gimplify_init_ctor_preeval_data preeval_data;
3627 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3628 bool cleared, complete_p, valid_const_initializer;
3630 /* Aggregate types must lower constructors to initialization of
3631 individual elements. The exception is that a CONSTRUCTOR node
3632 with no elements indicates zero-initialization of the whole. */
3633 if (vec_safe_is_empty (elts))
3635 if (notify_temp_creation)
3636 return GS_OK;
3637 break;
3640 /* Fetch information about the constructor to direct later processing.
3641 We might want to make static versions of it in various cases, and
3642 can only do so if it known to be a valid constant initializer. */
3643 valid_const_initializer
3644 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3645 &num_ctor_elements, &complete_p);
3647 /* If a const aggregate variable is being initialized, then it
3648 should never be a lose to promote the variable to be static. */
3649 if (valid_const_initializer
3650 && num_nonzero_elements > 1
3651 && TREE_READONLY (object)
3652 && TREE_CODE (object) == VAR_DECL
3653 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3655 if (notify_temp_creation)
3656 return GS_ERROR;
3657 DECL_INITIAL (object) = ctor;
3658 TREE_STATIC (object) = 1;
3659 if (!DECL_NAME (object))
3660 DECL_NAME (object) = create_tmp_var_name ("C");
3661 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3663 /* ??? C++ doesn't automatically append a .<number> to the
3664 assembler name, and even when it does, it looks at FE private
3665 data structures to figure out what that number should be,
3666 which are not set for this variable. I suppose this is
3667 important for local statics for inline functions, which aren't
3668 "local" in the object file sense. So in order to get a unique
3669 TU-local symbol, we must invoke the lhd version now. */
3670 lhd_set_decl_assembler_name (object);
3672 *expr_p = NULL_TREE;
3673 break;
3676 /* If there are "lots" of initialized elements, even discounting
3677 those that are not address constants (and thus *must* be
3678 computed at runtime), then partition the constructor into
3679 constant and non-constant parts. Block copy the constant
3680 parts in, then generate code for the non-constant parts. */
3681 /* TODO. There's code in cp/typeck.c to do this. */
3683 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3684 /* store_constructor will ignore the clearing of variable-sized
3685 objects. Initializers for such objects must explicitly set
3686 every field that needs to be set. */
3687 cleared = false;
3688 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3689 /* If the constructor isn't complete, clear the whole object
3690 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3692 ??? This ought not to be needed. For any element not present
3693 in the initializer, we should simply set them to zero. Except
3694 we'd need to *find* the elements that are not present, and that
3695 requires trickery to avoid quadratic compile-time behavior in
3696 large cases or excessive memory use in small cases. */
3697 cleared = true;
3698 else if (num_ctor_elements - num_nonzero_elements
3699 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3700 && num_nonzero_elements < num_ctor_elements / 4)
3701 /* If there are "lots" of zeros, it's more efficient to clear
3702 the memory and then set the nonzero elements. */
3703 cleared = true;
3704 else
3705 cleared = false;
3707 /* If there are "lots" of initialized elements, and all of them
3708 are valid address constants, then the entire initializer can
3709 be dropped to memory, and then memcpy'd out. Don't do this
3710 for sparse arrays, though, as it's more efficient to follow
3711 the standard CONSTRUCTOR behavior of memset followed by
3712 individual element initialization. Also don't do this for small
3713 all-zero initializers (which aren't big enough to merit
3714 clearing), and don't try to make bitwise copies of
3715 TREE_ADDRESSABLE types.
3717 We cannot apply such transformation when compiling chkp static
3718 initializer because creation of initializer image in the memory
3719 will require static initialization of bounds for it. It should
3720 result in another gimplification of similar initializer and we
3721 may fall into infinite loop. */
3722 if (valid_const_initializer
3723 && !(cleared || num_nonzero_elements == 0)
3724 && !TREE_ADDRESSABLE (type)
3725 && (!current_function_decl
3726 || !lookup_attribute ("chkp ctor",
3727 DECL_ATTRIBUTES (current_function_decl))))
3729 HOST_WIDE_INT size = int_size_in_bytes (type);
3730 unsigned int align;
3732 /* ??? We can still get unbounded array types, at least
3733 from the C++ front end. This seems wrong, but attempt
3734 to work around it for now. */
3735 if (size < 0)
3737 size = int_size_in_bytes (TREE_TYPE (object));
3738 if (size >= 0)
3739 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3742 /* Find the maximum alignment we can assume for the object. */
3743 /* ??? Make use of DECL_OFFSET_ALIGN. */
3744 if (DECL_P (object))
3745 align = DECL_ALIGN (object);
3746 else
3747 align = TYPE_ALIGN (type);
3749 /* Do a block move either if the size is so small as to make
3750 each individual move a sub-unit move on average, or if it
3751 is so large as to make individual moves inefficient. */
3752 if (size > 0
3753 && num_nonzero_elements > 1
3754 && (size < num_nonzero_elements
3755 || !can_move_by_pieces (size, align)))
3757 if (notify_temp_creation)
3758 return GS_ERROR;
3760 walk_tree (&ctor, force_labels_r, NULL, NULL);
3761 ctor = tree_output_constant_def (ctor);
3762 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3763 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3764 TREE_OPERAND (*expr_p, 1) = ctor;
3766 /* This is no longer an assignment of a CONSTRUCTOR, but
3767 we still may have processing to do on the LHS. So
3768 pretend we didn't do anything here to let that happen. */
3769 return GS_UNHANDLED;
3773 /* If the target is volatile, we have non-zero elements and more than
3774 one field to assign, initialize the target from a temporary. */
3775 if (TREE_THIS_VOLATILE (object)
3776 && !TREE_ADDRESSABLE (type)
3777 && num_nonzero_elements > 0
3778 && vec_safe_length (elts) > 1)
3780 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3781 TREE_OPERAND (*expr_p, 0) = temp;
3782 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3783 *expr_p,
3784 build2 (MODIFY_EXPR, void_type_node,
3785 object, temp));
3786 return GS_OK;
3789 if (notify_temp_creation)
3790 return GS_OK;
3792 /* If there are nonzero elements and if needed, pre-evaluate to capture
3793 elements overlapping with the lhs into temporaries. We must do this
3794 before clearing to fetch the values before they are zeroed-out. */
3795 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3797 preeval_data.lhs_base_decl = get_base_address (object);
3798 if (!DECL_P (preeval_data.lhs_base_decl))
3799 preeval_data.lhs_base_decl = NULL;
3800 preeval_data.lhs_alias_set = get_alias_set (object);
3802 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3803 pre_p, post_p, &preeval_data);
3806 if (cleared)
3808 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3809 Note that we still have to gimplify, in order to handle the
3810 case of variable sized types. Avoid shared tree structures. */
3811 CONSTRUCTOR_ELTS (ctor) = NULL;
3812 TREE_SIDE_EFFECTS (ctor) = 0;
3813 object = unshare_expr (object);
3814 gimplify_stmt (expr_p, pre_p);
3817 /* If we have not block cleared the object, or if there are nonzero
3818 elements in the constructor, add assignments to the individual
3819 scalar fields of the object. */
3820 if (!cleared || num_nonzero_elements > 0)
3821 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3823 *expr_p = NULL_TREE;
3825 break;
3827 case COMPLEX_TYPE:
3829 tree r, i;
3831 if (notify_temp_creation)
3832 return GS_OK;
3834 /* Extract the real and imaginary parts out of the ctor. */
3835 gcc_assert (elts->length () == 2);
3836 r = (*elts)[0].value;
3837 i = (*elts)[1].value;
3838 if (r == NULL || i == NULL)
3840 tree zero = build_zero_cst (TREE_TYPE (type));
3841 if (r == NULL)
3842 r = zero;
3843 if (i == NULL)
3844 i = zero;
3847 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3848 represent creation of a complex value. */
3849 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3851 ctor = build_complex (type, r, i);
3852 TREE_OPERAND (*expr_p, 1) = ctor;
3854 else
3856 ctor = build2 (COMPLEX_EXPR, type, r, i);
3857 TREE_OPERAND (*expr_p, 1) = ctor;
3858 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
3859 pre_p,
3860 post_p,
3861 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3862 fb_rvalue);
3865 break;
3867 case VECTOR_TYPE:
3869 unsigned HOST_WIDE_INT ix;
3870 constructor_elt *ce;
3872 if (notify_temp_creation)
3873 return GS_OK;
3875 /* Go ahead and simplify constant constructors to VECTOR_CST. */
3876 if (TREE_CONSTANT (ctor))
3878 bool constant_p = true;
3879 tree value;
3881 /* Even when ctor is constant, it might contain non-*_CST
3882 elements, such as addresses or trapping values like
3883 1.0/0.0 - 1.0/0.0. Such expressions don't belong
3884 in VECTOR_CST nodes. */
3885 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
3886 if (!CONSTANT_CLASS_P (value))
3888 constant_p = false;
3889 break;
3892 if (constant_p)
3894 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
3895 break;
3898 /* Don't reduce an initializer constant even if we can't
3899 make a VECTOR_CST. It won't do anything for us, and it'll
3900 prevent us from representing it as a single constant. */
3901 if (initializer_constant_valid_p (ctor, type))
3902 break;
3904 TREE_CONSTANT (ctor) = 0;
3907 /* Vector types use CONSTRUCTOR all the way through gimple
3908 compilation as a general initializer. */
3909 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
3911 enum gimplify_status tret;
3912 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
3913 fb_rvalue);
3914 if (tret == GS_ERROR)
3915 ret = GS_ERROR;
3917 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
3918 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
3920 break;
3922 default:
3923 /* So how did we get a CONSTRUCTOR for a scalar type? */
3924 gcc_unreachable ();
3927 if (ret == GS_ERROR)
3928 return GS_ERROR;
3929 else if (want_value)
3931 *expr_p = object;
3932 return GS_OK;
3934 else
3936 /* If we have gimplified both sides of the initializer but have
3937 not emitted an assignment, do so now. */
3938 if (*expr_p)
3940 tree lhs = TREE_OPERAND (*expr_p, 0);
3941 tree rhs = TREE_OPERAND (*expr_p, 1);
3942 gimple init = gimple_build_assign (lhs, rhs);
3943 gimplify_seq_add_stmt (pre_p, init);
3944 *expr_p = NULL;
3947 return GS_ALL_DONE;
3951 /* Given a pointer value OP0, return a simplified version of an
3952 indirection through OP0, or NULL_TREE if no simplification is
3953 possible. This may only be applied to a rhs of an expression.
3954 Note that the resulting type may be different from the type pointed
3955 to in the sense that it is still compatible from the langhooks
3956 point of view. */
3958 static tree
3959 gimple_fold_indirect_ref_rhs (tree t)
3961 return gimple_fold_indirect_ref (t);
3964 /* Subroutine of gimplify_modify_expr to do simplifications of
3965 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
3966 something changes. */
3968 static enum gimplify_status
3969 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
3970 gimple_seq *pre_p, gimple_seq *post_p,
3971 bool want_value)
3973 enum gimplify_status ret = GS_UNHANDLED;
3974 bool changed;
3978 changed = false;
3979 switch (TREE_CODE (*from_p))
3981 case VAR_DECL:
3982 /* If we're assigning from a read-only variable initialized with
3983 a constructor, do the direct assignment from the constructor,
3984 but only if neither source nor target are volatile since this
3985 latter assignment might end up being done on a per-field basis. */
3986 if (DECL_INITIAL (*from_p)
3987 && TREE_READONLY (*from_p)
3988 && !TREE_THIS_VOLATILE (*from_p)
3989 && !TREE_THIS_VOLATILE (*to_p)
3990 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
3992 tree old_from = *from_p;
3993 enum gimplify_status subret;
3995 /* Move the constructor into the RHS. */
3996 *from_p = unshare_expr (DECL_INITIAL (*from_p));
3998 /* Let's see if gimplify_init_constructor will need to put
3999 it in memory. */
4000 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4001 false, true);
4002 if (subret == GS_ERROR)
4004 /* If so, revert the change. */
4005 *from_p = old_from;
4007 else
4009 ret = GS_OK;
4010 changed = true;
4013 break;
4014 case INDIRECT_REF:
4016 /* If we have code like
4018 *(const A*)(A*)&x
4020 where the type of "x" is a (possibly cv-qualified variant
4021 of "A"), treat the entire expression as identical to "x".
4022 This kind of code arises in C++ when an object is bound
4023 to a const reference, and if "x" is a TARGET_EXPR we want
4024 to take advantage of the optimization below. */
4025 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4026 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4027 if (t)
4029 if (TREE_THIS_VOLATILE (t) != volatile_p)
4031 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4032 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4033 build_fold_addr_expr (t));
4034 if (REFERENCE_CLASS_P (t))
4035 TREE_THIS_VOLATILE (t) = volatile_p;
4037 *from_p = t;
4038 ret = GS_OK;
4039 changed = true;
4041 break;
4044 case TARGET_EXPR:
4046 /* If we are initializing something from a TARGET_EXPR, strip the
4047 TARGET_EXPR and initialize it directly, if possible. This can't
4048 be done if the initializer is void, since that implies that the
4049 temporary is set in some non-trivial way.
4051 ??? What about code that pulls out the temp and uses it
4052 elsewhere? I think that such code never uses the TARGET_EXPR as
4053 an initializer. If I'm wrong, we'll die because the temp won't
4054 have any RTL. In that case, I guess we'll need to replace
4055 references somehow. */
4056 tree init = TARGET_EXPR_INITIAL (*from_p);
4058 if (init
4059 && !VOID_TYPE_P (TREE_TYPE (init)))
4061 *from_p = init;
4062 ret = GS_OK;
4063 changed = true;
4066 break;
4068 case COMPOUND_EXPR:
4069 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4070 caught. */
4071 gimplify_compound_expr (from_p, pre_p, true);
4072 ret = GS_OK;
4073 changed = true;
4074 break;
4076 case CONSTRUCTOR:
4077 /* If we already made some changes, let the front end have a
4078 crack at this before we break it down. */
4079 if (ret != GS_UNHANDLED)
4080 break;
4081 /* If we're initializing from a CONSTRUCTOR, break this into
4082 individual MODIFY_EXPRs. */
4083 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4084 false);
4086 case COND_EXPR:
4087 /* If we're assigning to a non-register type, push the assignment
4088 down into the branches. This is mandatory for ADDRESSABLE types,
4089 since we cannot generate temporaries for such, but it saves a
4090 copy in other cases as well. */
4091 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4093 /* This code should mirror the code in gimplify_cond_expr. */
4094 enum tree_code code = TREE_CODE (*expr_p);
4095 tree cond = *from_p;
4096 tree result = *to_p;
4098 ret = gimplify_expr (&result, pre_p, post_p,
4099 is_gimple_lvalue, fb_lvalue);
4100 if (ret != GS_ERROR)
4101 ret = GS_OK;
4103 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4104 TREE_OPERAND (cond, 1)
4105 = build2 (code, void_type_node, result,
4106 TREE_OPERAND (cond, 1));
4107 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4108 TREE_OPERAND (cond, 2)
4109 = build2 (code, void_type_node, unshare_expr (result),
4110 TREE_OPERAND (cond, 2));
4112 TREE_TYPE (cond) = void_type_node;
4113 recalculate_side_effects (cond);
4115 if (want_value)
4117 gimplify_and_add (cond, pre_p);
4118 *expr_p = unshare_expr (result);
4120 else
4121 *expr_p = cond;
4122 return ret;
4124 break;
4126 case CALL_EXPR:
4127 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4128 return slot so that we don't generate a temporary. */
4129 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4130 && aggregate_value_p (*from_p, *from_p))
4132 bool use_target;
4134 if (!(rhs_predicate_for (*to_p))(*from_p))
4135 /* If we need a temporary, *to_p isn't accurate. */
4136 use_target = false;
4137 /* It's OK to use the return slot directly unless it's an NRV. */
4138 else if (TREE_CODE (*to_p) == RESULT_DECL
4139 && DECL_NAME (*to_p) == NULL_TREE
4140 && needs_to_live_in_memory (*to_p))
4141 use_target = true;
4142 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4143 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4144 /* Don't force regs into memory. */
4145 use_target = false;
4146 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4147 /* It's OK to use the target directly if it's being
4148 initialized. */
4149 use_target = true;
4150 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4151 /* Always use the target and thus RSO for variable-sized types.
4152 GIMPLE cannot deal with a variable-sized assignment
4153 embedded in a call statement. */
4154 use_target = true;
4155 else if (TREE_CODE (*to_p) != SSA_NAME
4156 && (!is_gimple_variable (*to_p)
4157 || needs_to_live_in_memory (*to_p)))
4158 /* Don't use the original target if it's already addressable;
4159 if its address escapes, and the called function uses the
4160 NRV optimization, a conforming program could see *to_p
4161 change before the called function returns; see c++/19317.
4162 When optimizing, the return_slot pass marks more functions
4163 as safe after we have escape info. */
4164 use_target = false;
4165 else
4166 use_target = true;
4168 if (use_target)
4170 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4171 mark_addressable (*to_p);
4174 break;
4176 case WITH_SIZE_EXPR:
4177 /* Likewise for calls that return an aggregate of non-constant size,
4178 since we would not be able to generate a temporary at all. */
4179 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4181 *from_p = TREE_OPERAND (*from_p, 0);
4182 /* We don't change ret in this case because the
4183 WITH_SIZE_EXPR might have been added in
4184 gimplify_modify_expr, so returning GS_OK would lead to an
4185 infinite loop. */
4186 changed = true;
4188 break;
4190 /* If we're initializing from a container, push the initialization
4191 inside it. */
4192 case CLEANUP_POINT_EXPR:
4193 case BIND_EXPR:
4194 case STATEMENT_LIST:
4196 tree wrap = *from_p;
4197 tree t;
4199 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4200 fb_lvalue);
4201 if (ret != GS_ERROR)
4202 ret = GS_OK;
4204 t = voidify_wrapper_expr (wrap, *expr_p);
4205 gcc_assert (t == *expr_p);
4207 if (want_value)
4209 gimplify_and_add (wrap, pre_p);
4210 *expr_p = unshare_expr (*to_p);
4212 else
4213 *expr_p = wrap;
4214 return GS_OK;
4217 case COMPOUND_LITERAL_EXPR:
4219 tree complit = TREE_OPERAND (*expr_p, 1);
4220 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4221 tree decl = DECL_EXPR_DECL (decl_s);
4222 tree init = DECL_INITIAL (decl);
4224 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4225 into struct T x = { 0, 1, 2 } if the address of the
4226 compound literal has never been taken. */
4227 if (!TREE_ADDRESSABLE (complit)
4228 && !TREE_ADDRESSABLE (decl)
4229 && init)
4231 *expr_p = copy_node (*expr_p);
4232 TREE_OPERAND (*expr_p, 1) = init;
4233 return GS_OK;
4237 default:
4238 break;
4241 while (changed);
4243 return ret;
4247 /* Return true if T looks like a valid GIMPLE statement. */
4249 static bool
4250 is_gimple_stmt (tree t)
4252 const enum tree_code code = TREE_CODE (t);
4254 switch (code)
4256 case NOP_EXPR:
4257 /* The only valid NOP_EXPR is the empty statement. */
4258 return IS_EMPTY_STMT (t);
4260 case BIND_EXPR:
4261 case COND_EXPR:
4262 /* These are only valid if they're void. */
4263 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4265 case SWITCH_EXPR:
4266 case GOTO_EXPR:
4267 case RETURN_EXPR:
4268 case LABEL_EXPR:
4269 case CASE_LABEL_EXPR:
4270 case TRY_CATCH_EXPR:
4271 case TRY_FINALLY_EXPR:
4272 case EH_FILTER_EXPR:
4273 case CATCH_EXPR:
4274 case ASM_EXPR:
4275 case STATEMENT_LIST:
4276 case OMP_PARALLEL:
4277 case OMP_FOR:
4278 case OMP_SIMD:
4279 case CILK_SIMD:
4280 case OMP_DISTRIBUTE:
4281 case OMP_SECTIONS:
4282 case OMP_SECTION:
4283 case OMP_SINGLE:
4284 case OMP_MASTER:
4285 case OMP_TASKGROUP:
4286 case OMP_ORDERED:
4287 case OMP_CRITICAL:
4288 case OMP_TASK:
4289 /* These are always void. */
4290 return true;
4292 case CALL_EXPR:
4293 case MODIFY_EXPR:
4294 case PREDICT_EXPR:
4295 /* These are valid regardless of their type. */
4296 return true;
4298 default:
4299 return false;
4304 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4305 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4306 DECL_GIMPLE_REG_P set.
4308 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4309 other, unmodified part of the complex object just before the total store.
4310 As a consequence, if the object is still uninitialized, an undefined value
4311 will be loaded into a register, which may result in a spurious exception
4312 if the register is floating-point and the value happens to be a signaling
4313 NaN for example. Then the fully-fledged complex operations lowering pass
4314 followed by a DCE pass are necessary in order to fix things up. */
4316 static enum gimplify_status
4317 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4318 bool want_value)
4320 enum tree_code code, ocode;
4321 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4323 lhs = TREE_OPERAND (*expr_p, 0);
4324 rhs = TREE_OPERAND (*expr_p, 1);
4325 code = TREE_CODE (lhs);
4326 lhs = TREE_OPERAND (lhs, 0);
4328 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4329 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4330 TREE_NO_WARNING (other) = 1;
4331 other = get_formal_tmp_var (other, pre_p);
4333 realpart = code == REALPART_EXPR ? rhs : other;
4334 imagpart = code == REALPART_EXPR ? other : rhs;
4336 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4337 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4338 else
4339 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4341 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4342 *expr_p = (want_value) ? rhs : NULL_TREE;
4344 return GS_ALL_DONE;
4347 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4349 modify_expr
4350 : varname '=' rhs
4351 | '*' ID '=' rhs
4353 PRE_P points to the list where side effects that must happen before
4354 *EXPR_P should be stored.
4356 POST_P points to the list where side effects that must happen after
4357 *EXPR_P should be stored.
4359 WANT_VALUE is nonzero iff we want to use the value of this expression
4360 in another expression. */
4362 static enum gimplify_status
4363 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4364 bool want_value)
4366 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4367 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4368 enum gimplify_status ret = GS_UNHANDLED;
4369 gimple assign;
4370 location_t loc = EXPR_LOCATION (*expr_p);
4371 gimple_stmt_iterator gsi;
4373 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4374 || TREE_CODE (*expr_p) == INIT_EXPR);
4376 if (fn_contains_cilk_spawn_p (cfun)
4377 && lang_hooks.cilkplus.cilk_detect_spawn_and_unwrap (expr_p)
4378 && !seen_error ())
4379 return (enum gimplify_status)
4380 lang_hooks.cilkplus.gimplify_cilk_spawn (expr_p, pre_p, post_p);
4382 /* Trying to simplify a clobber using normal logic doesn't work,
4383 so handle it here. */
4384 if (TREE_CLOBBER_P (*from_p))
4386 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4387 if (ret == GS_ERROR)
4388 return ret;
4389 gcc_assert (!want_value
4390 && (TREE_CODE (*to_p) == VAR_DECL
4391 || TREE_CODE (*to_p) == MEM_REF));
4392 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4393 *expr_p = NULL;
4394 return GS_ALL_DONE;
4397 /* Insert pointer conversions required by the middle-end that are not
4398 required by the frontend. This fixes middle-end type checking for
4399 for example gcc.dg/redecl-6.c. */
4400 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4402 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4403 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4404 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4407 /* See if any simplifications can be done based on what the RHS is. */
4408 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4409 want_value);
4410 if (ret != GS_UNHANDLED)
4411 return ret;
4413 /* For zero sized types only gimplify the left hand side and right hand
4414 side as statements and throw away the assignment. Do this after
4415 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4416 types properly. */
4417 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4419 gimplify_stmt (from_p, pre_p);
4420 gimplify_stmt (to_p, pre_p);
4421 *expr_p = NULL_TREE;
4422 return GS_ALL_DONE;
4425 /* If the value being copied is of variable width, compute the length
4426 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4427 before gimplifying any of the operands so that we can resolve any
4428 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4429 the size of the expression to be copied, not of the destination, so
4430 that is what we must do here. */
4431 maybe_with_size_expr (from_p);
4433 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4434 if (ret == GS_ERROR)
4435 return ret;
4437 /* As a special case, we have to temporarily allow for assignments
4438 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4439 a toplevel statement, when gimplifying the GENERIC expression
4440 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4441 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4443 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4444 prevent gimplify_expr from trying to create a new temporary for
4445 foo's LHS, we tell it that it should only gimplify until it
4446 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4447 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4448 and all we need to do here is set 'a' to be its LHS. */
4449 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4450 fb_rvalue);
4451 if (ret == GS_ERROR)
4452 return ret;
4454 /* Now see if the above changed *from_p to something we handle specially. */
4455 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4456 want_value);
4457 if (ret != GS_UNHANDLED)
4458 return ret;
4460 /* If we've got a variable sized assignment between two lvalues (i.e. does
4461 not involve a call), then we can make things a bit more straightforward
4462 by converting the assignment to memcpy or memset. */
4463 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4465 tree from = TREE_OPERAND (*from_p, 0);
4466 tree size = TREE_OPERAND (*from_p, 1);
4468 if (TREE_CODE (from) == CONSTRUCTOR)
4469 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4471 if (is_gimple_addressable (from))
4473 *from_p = from;
4474 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4475 pre_p);
4479 /* Transform partial stores to non-addressable complex variables into
4480 total stores. This allows us to use real instead of virtual operands
4481 for these variables, which improves optimization. */
4482 if ((TREE_CODE (*to_p) == REALPART_EXPR
4483 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4484 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4485 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4487 /* Try to alleviate the effects of the gimplification creating artificial
4488 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4489 if (!gimplify_ctxp->into_ssa
4490 && TREE_CODE (*from_p) == VAR_DECL
4491 && DECL_IGNORED_P (*from_p)
4492 && DECL_P (*to_p)
4493 && !DECL_IGNORED_P (*to_p))
4495 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4496 DECL_NAME (*from_p)
4497 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4498 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4499 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4502 if (want_value && TREE_THIS_VOLATILE (*to_p))
4503 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4505 if (TREE_CODE (*from_p) == CALL_EXPR)
4507 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4508 instead of a GIMPLE_ASSIGN. */
4509 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4510 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4511 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4512 assign = gimple_build_call_from_tree (*from_p);
4513 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4514 notice_special_calls (assign);
4515 if (!gimple_call_noreturn_p (assign))
4516 gimple_call_set_lhs (assign, *to_p);
4518 else
4520 assign = gimple_build_assign (*to_p, *from_p);
4521 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4524 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4526 /* We should have got an SSA name from the start. */
4527 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4530 gimplify_seq_add_stmt (pre_p, assign);
4531 gsi = gsi_last (*pre_p);
4532 /* Don't fold stmts inside of target construct. We'll do it
4533 during omplower pass instead. */
4534 struct gimplify_omp_ctx *ctx;
4535 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
4536 if (ctx->region_type == ORT_TARGET)
4537 break;
4538 if (ctx == NULL)
4539 fold_stmt (&gsi);
4541 if (want_value)
4543 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4544 return GS_OK;
4546 else
4547 *expr_p = NULL;
4549 return GS_ALL_DONE;
4552 /* Gimplify a comparison between two variable-sized objects. Do this
4553 with a call to BUILT_IN_MEMCMP. */
4555 static enum gimplify_status
4556 gimplify_variable_sized_compare (tree *expr_p)
4558 location_t loc = EXPR_LOCATION (*expr_p);
4559 tree op0 = TREE_OPERAND (*expr_p, 0);
4560 tree op1 = TREE_OPERAND (*expr_p, 1);
4561 tree t, arg, dest, src, expr;
4563 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4564 arg = unshare_expr (arg);
4565 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4566 src = build_fold_addr_expr_loc (loc, op1);
4567 dest = build_fold_addr_expr_loc (loc, op0);
4568 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4569 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4571 expr
4572 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4573 SET_EXPR_LOCATION (expr, loc);
4574 *expr_p = expr;
4576 return GS_OK;
4579 /* Gimplify a comparison between two aggregate objects of integral scalar
4580 mode as a comparison between the bitwise equivalent scalar values. */
4582 static enum gimplify_status
4583 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4585 location_t loc = EXPR_LOCATION (*expr_p);
4586 tree op0 = TREE_OPERAND (*expr_p, 0);
4587 tree op1 = TREE_OPERAND (*expr_p, 1);
4589 tree type = TREE_TYPE (op0);
4590 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4592 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4593 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4595 *expr_p
4596 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4598 return GS_OK;
4601 /* Gimplify an expression sequence. This function gimplifies each
4602 expression and rewrites the original expression with the last
4603 expression of the sequence in GIMPLE form.
4605 PRE_P points to the list where the side effects for all the
4606 expressions in the sequence will be emitted.
4608 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4610 static enum gimplify_status
4611 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4613 tree t = *expr_p;
4617 tree *sub_p = &TREE_OPERAND (t, 0);
4619 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4620 gimplify_compound_expr (sub_p, pre_p, false);
4621 else
4622 gimplify_stmt (sub_p, pre_p);
4624 t = TREE_OPERAND (t, 1);
4626 while (TREE_CODE (t) == COMPOUND_EXPR);
4628 *expr_p = t;
4629 if (want_value)
4630 return GS_OK;
4631 else
4633 gimplify_stmt (expr_p, pre_p);
4634 return GS_ALL_DONE;
4638 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4639 gimplify. After gimplification, EXPR_P will point to a new temporary
4640 that holds the original value of the SAVE_EXPR node.
4642 PRE_P points to the list where side effects that must happen before
4643 *EXPR_P should be stored. */
4645 static enum gimplify_status
4646 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4648 enum gimplify_status ret = GS_ALL_DONE;
4649 tree val;
4651 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4652 val = TREE_OPERAND (*expr_p, 0);
4654 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4655 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4657 /* The operand may be a void-valued expression such as SAVE_EXPRs
4658 generated by the Java frontend for class initialization. It is
4659 being executed only for its side-effects. */
4660 if (TREE_TYPE (val) == void_type_node)
4662 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4663 is_gimple_stmt, fb_none);
4664 val = NULL;
4666 else
4667 val = get_initialized_tmp_var (val, pre_p, post_p);
4669 TREE_OPERAND (*expr_p, 0) = val;
4670 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4673 *expr_p = val;
4675 return ret;
4678 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4680 unary_expr
4681 : ...
4682 | '&' varname
4685 PRE_P points to the list where side effects that must happen before
4686 *EXPR_P should be stored.
4688 POST_P points to the list where side effects that must happen after
4689 *EXPR_P should be stored. */
4691 static enum gimplify_status
4692 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4694 tree expr = *expr_p;
4695 tree op0 = TREE_OPERAND (expr, 0);
4696 enum gimplify_status ret;
4697 location_t loc = EXPR_LOCATION (*expr_p);
4699 switch (TREE_CODE (op0))
4701 case INDIRECT_REF:
4702 do_indirect_ref:
4703 /* Check if we are dealing with an expression of the form '&*ptr'.
4704 While the front end folds away '&*ptr' into 'ptr', these
4705 expressions may be generated internally by the compiler (e.g.,
4706 builtins like __builtin_va_end). */
4707 /* Caution: the silent array decomposition semantics we allow for
4708 ADDR_EXPR means we can't always discard the pair. */
4709 /* Gimplification of the ADDR_EXPR operand may drop
4710 cv-qualification conversions, so make sure we add them if
4711 needed. */
4713 tree op00 = TREE_OPERAND (op0, 0);
4714 tree t_expr = TREE_TYPE (expr);
4715 tree t_op00 = TREE_TYPE (op00);
4717 if (!useless_type_conversion_p (t_expr, t_op00))
4718 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4719 *expr_p = op00;
4720 ret = GS_OK;
4722 break;
4724 case VIEW_CONVERT_EXPR:
4725 /* Take the address of our operand and then convert it to the type of
4726 this ADDR_EXPR.
4728 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4729 all clear. The impact of this transformation is even less clear. */
4731 /* If the operand is a useless conversion, look through it. Doing so
4732 guarantees that the ADDR_EXPR and its operand will remain of the
4733 same type. */
4734 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4735 op0 = TREE_OPERAND (op0, 0);
4737 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4738 build_fold_addr_expr_loc (loc,
4739 TREE_OPERAND (op0, 0)));
4740 ret = GS_OK;
4741 break;
4743 default:
4744 /* We use fb_either here because the C frontend sometimes takes
4745 the address of a call that returns a struct; see
4746 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4747 the implied temporary explicit. */
4749 /* Make the operand addressable. */
4750 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4751 is_gimple_addressable, fb_either);
4752 if (ret == GS_ERROR)
4753 break;
4755 /* Then mark it. Beware that it may not be possible to do so directly
4756 if a temporary has been created by the gimplification. */
4757 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4759 op0 = TREE_OPERAND (expr, 0);
4761 /* For various reasons, the gimplification of the expression
4762 may have made a new INDIRECT_REF. */
4763 if (TREE_CODE (op0) == INDIRECT_REF)
4764 goto do_indirect_ref;
4766 mark_addressable (TREE_OPERAND (expr, 0));
4768 /* The FEs may end up building ADDR_EXPRs early on a decl with
4769 an incomplete type. Re-build ADDR_EXPRs in canonical form
4770 here. */
4771 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4772 *expr_p = build_fold_addr_expr (op0);
4774 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4775 recompute_tree_invariant_for_addr_expr (*expr_p);
4777 /* If we re-built the ADDR_EXPR add a conversion to the original type
4778 if required. */
4779 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4780 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4782 break;
4785 return ret;
4788 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4789 value; output operands should be a gimple lvalue. */
4791 static enum gimplify_status
4792 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4794 tree expr;
4795 int noutputs;
4796 const char **oconstraints;
4797 int i;
4798 tree link;
4799 const char *constraint;
4800 bool allows_mem, allows_reg, is_inout;
4801 enum gimplify_status ret, tret;
4802 gimple stmt;
4803 vec<tree, va_gc> *inputs;
4804 vec<tree, va_gc> *outputs;
4805 vec<tree, va_gc> *clobbers;
4806 vec<tree, va_gc> *labels;
4807 tree link_next;
4809 expr = *expr_p;
4810 noutputs = list_length (ASM_OUTPUTS (expr));
4811 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4813 inputs = NULL;
4814 outputs = NULL;
4815 clobbers = NULL;
4816 labels = NULL;
4818 ret = GS_ALL_DONE;
4819 link_next = NULL_TREE;
4820 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4822 bool ok;
4823 size_t constraint_len;
4825 link_next = TREE_CHAIN (link);
4827 oconstraints[i]
4828 = constraint
4829 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4830 constraint_len = strlen (constraint);
4831 if (constraint_len == 0)
4832 continue;
4834 ok = parse_output_constraint (&constraint, i, 0, 0,
4835 &allows_mem, &allows_reg, &is_inout);
4836 if (!ok)
4838 ret = GS_ERROR;
4839 is_inout = false;
4842 if (!allows_reg && allows_mem)
4843 mark_addressable (TREE_VALUE (link));
4845 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4846 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4847 fb_lvalue | fb_mayfail);
4848 if (tret == GS_ERROR)
4850 error ("invalid lvalue in asm output %d", i);
4851 ret = tret;
4854 vec_safe_push (outputs, link);
4855 TREE_CHAIN (link) = NULL_TREE;
4857 if (is_inout)
4859 /* An input/output operand. To give the optimizers more
4860 flexibility, split it into separate input and output
4861 operands. */
4862 tree input;
4863 char buf[10];
4865 /* Turn the in/out constraint into an output constraint. */
4866 char *p = xstrdup (constraint);
4867 p[0] = '=';
4868 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4870 /* And add a matching input constraint. */
4871 if (allows_reg)
4873 sprintf (buf, "%d", i);
4875 /* If there are multiple alternatives in the constraint,
4876 handle each of them individually. Those that allow register
4877 will be replaced with operand number, the others will stay
4878 unchanged. */
4879 if (strchr (p, ',') != NULL)
4881 size_t len = 0, buflen = strlen (buf);
4882 char *beg, *end, *str, *dst;
4884 for (beg = p + 1;;)
4886 end = strchr (beg, ',');
4887 if (end == NULL)
4888 end = strchr (beg, '\0');
4889 if ((size_t) (end - beg) < buflen)
4890 len += buflen + 1;
4891 else
4892 len += end - beg + 1;
4893 if (*end)
4894 beg = end + 1;
4895 else
4896 break;
4899 str = (char *) alloca (len);
4900 for (beg = p + 1, dst = str;;)
4902 const char *tem;
4903 bool mem_p, reg_p, inout_p;
4905 end = strchr (beg, ',');
4906 if (end)
4907 *end = '\0';
4908 beg[-1] = '=';
4909 tem = beg - 1;
4910 parse_output_constraint (&tem, i, 0, 0,
4911 &mem_p, &reg_p, &inout_p);
4912 if (dst != str)
4913 *dst++ = ',';
4914 if (reg_p)
4916 memcpy (dst, buf, buflen);
4917 dst += buflen;
4919 else
4921 if (end)
4922 len = end - beg;
4923 else
4924 len = strlen (beg);
4925 memcpy (dst, beg, len);
4926 dst += len;
4928 if (end)
4929 beg = end + 1;
4930 else
4931 break;
4933 *dst = '\0';
4934 input = build_string (dst - str, str);
4936 else
4937 input = build_string (strlen (buf), buf);
4939 else
4940 input = build_string (constraint_len - 1, constraint + 1);
4942 free (p);
4944 input = build_tree_list (build_tree_list (NULL_TREE, input),
4945 unshare_expr (TREE_VALUE (link)));
4946 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
4950 link_next = NULL_TREE;
4951 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
4953 link_next = TREE_CHAIN (link);
4954 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4955 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
4956 oconstraints, &allows_mem, &allows_reg);
4958 /* If we can't make copies, we can only accept memory. */
4959 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
4961 if (allows_mem)
4962 allows_reg = 0;
4963 else
4965 error ("impossible constraint in %<asm%>");
4966 error ("non-memory input %d must stay in memory", i);
4967 return GS_ERROR;
4971 /* If the operand is a memory input, it should be an lvalue. */
4972 if (!allows_reg && allows_mem)
4974 tree inputv = TREE_VALUE (link);
4975 STRIP_NOPS (inputv);
4976 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
4977 || TREE_CODE (inputv) == PREINCREMENT_EXPR
4978 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
4979 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
4980 TREE_VALUE (link) = error_mark_node;
4981 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4982 is_gimple_lvalue, fb_lvalue | fb_mayfail);
4983 mark_addressable (TREE_VALUE (link));
4984 if (tret == GS_ERROR)
4986 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
4987 input_location = EXPR_LOCATION (TREE_VALUE (link));
4988 error ("memory input %d is not directly addressable", i);
4989 ret = tret;
4992 else
4994 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4995 is_gimple_asm_val, fb_rvalue);
4996 if (tret == GS_ERROR)
4997 ret = tret;
5000 TREE_CHAIN (link) = NULL_TREE;
5001 vec_safe_push (inputs, link);
5004 link_next = NULL_TREE;
5005 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5007 link_next = TREE_CHAIN (link);
5008 TREE_CHAIN (link) = NULL_TREE;
5009 vec_safe_push (clobbers, link);
5012 link_next = NULL_TREE;
5013 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5015 link_next = TREE_CHAIN (link);
5016 TREE_CHAIN (link) = NULL_TREE;
5017 vec_safe_push (labels, link);
5020 /* Do not add ASMs with errors to the gimple IL stream. */
5021 if (ret != GS_ERROR)
5023 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5024 inputs, outputs, clobbers, labels);
5026 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5027 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5029 gimplify_seq_add_stmt (pre_p, stmt);
5032 return ret;
5035 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5036 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5037 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5038 return to this function.
5040 FIXME should we complexify the prequeue handling instead? Or use flags
5041 for all the cleanups and let the optimizer tighten them up? The current
5042 code seems pretty fragile; it will break on a cleanup within any
5043 non-conditional nesting. But any such nesting would be broken, anyway;
5044 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5045 and continues out of it. We can do that at the RTL level, though, so
5046 having an optimizer to tighten up try/finally regions would be a Good
5047 Thing. */
5049 static enum gimplify_status
5050 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5052 gimple_stmt_iterator iter;
5053 gimple_seq body_sequence = NULL;
5055 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5057 /* We only care about the number of conditions between the innermost
5058 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5059 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5060 int old_conds = gimplify_ctxp->conditions;
5061 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5062 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5063 gimplify_ctxp->conditions = 0;
5064 gimplify_ctxp->conditional_cleanups = NULL;
5065 gimplify_ctxp->in_cleanup_point_expr = true;
5067 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5069 gimplify_ctxp->conditions = old_conds;
5070 gimplify_ctxp->conditional_cleanups = old_cleanups;
5071 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5073 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5075 gimple wce = gsi_stmt (iter);
5077 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5079 if (gsi_one_before_end_p (iter))
5081 /* Note that gsi_insert_seq_before and gsi_remove do not
5082 scan operands, unlike some other sequence mutators. */
5083 if (!gimple_wce_cleanup_eh_only (wce))
5084 gsi_insert_seq_before_without_update (&iter,
5085 gimple_wce_cleanup (wce),
5086 GSI_SAME_STMT);
5087 gsi_remove (&iter, true);
5088 break;
5090 else
5092 gimple_statement_try *gtry;
5093 gimple_seq seq;
5094 enum gimple_try_flags kind;
5096 if (gimple_wce_cleanup_eh_only (wce))
5097 kind = GIMPLE_TRY_CATCH;
5098 else
5099 kind = GIMPLE_TRY_FINALLY;
5100 seq = gsi_split_seq_after (iter);
5102 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5103 /* Do not use gsi_replace here, as it may scan operands.
5104 We want to do a simple structural modification only. */
5105 gsi_set_stmt (&iter, gtry);
5106 iter = gsi_start (gtry->eval);
5109 else
5110 gsi_next (&iter);
5113 gimplify_seq_add_seq (pre_p, body_sequence);
5114 if (temp)
5116 *expr_p = temp;
5117 return GS_OK;
5119 else
5121 *expr_p = NULL;
5122 return GS_ALL_DONE;
5126 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5127 is the cleanup action required. EH_ONLY is true if the cleanup should
5128 only be executed if an exception is thrown, not on normal exit. */
5130 static void
5131 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5133 gimple wce;
5134 gimple_seq cleanup_stmts = NULL;
5136 /* Errors can result in improperly nested cleanups. Which results in
5137 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5138 if (seen_error ())
5139 return;
5141 if (gimple_conditional_context ())
5143 /* If we're in a conditional context, this is more complex. We only
5144 want to run the cleanup if we actually ran the initialization that
5145 necessitates it, but we want to run it after the end of the
5146 conditional context. So we wrap the try/finally around the
5147 condition and use a flag to determine whether or not to actually
5148 run the destructor. Thus
5150 test ? f(A()) : 0
5152 becomes (approximately)
5154 flag = 0;
5155 try {
5156 if (test) { A::A(temp); flag = 1; val = f(temp); }
5157 else { val = 0; }
5158 } finally {
5159 if (flag) A::~A(temp);
5163 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5164 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5165 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5167 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5168 gimplify_stmt (&cleanup, &cleanup_stmts);
5169 wce = gimple_build_wce (cleanup_stmts);
5171 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5172 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5173 gimplify_seq_add_stmt (pre_p, ftrue);
5175 /* Because of this manipulation, and the EH edges that jump
5176 threading cannot redirect, the temporary (VAR) will appear
5177 to be used uninitialized. Don't warn. */
5178 TREE_NO_WARNING (var) = 1;
5180 else
5182 gimplify_stmt (&cleanup, &cleanup_stmts);
5183 wce = gimple_build_wce (cleanup_stmts);
5184 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5185 gimplify_seq_add_stmt (pre_p, wce);
5189 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5191 static enum gimplify_status
5192 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5194 tree targ = *expr_p;
5195 tree temp = TARGET_EXPR_SLOT (targ);
5196 tree init = TARGET_EXPR_INITIAL (targ);
5197 enum gimplify_status ret;
5199 if (init)
5201 tree cleanup = NULL_TREE;
5203 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5204 to the temps list. Handle also variable length TARGET_EXPRs. */
5205 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5207 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5208 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5209 gimplify_vla_decl (temp, pre_p);
5211 else
5212 gimple_add_tmp_var (temp);
5214 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5215 expression is supposed to initialize the slot. */
5216 if (VOID_TYPE_P (TREE_TYPE (init)))
5217 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5218 else
5220 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5221 init = init_expr;
5222 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5223 init = NULL;
5224 ggc_free (init_expr);
5226 if (ret == GS_ERROR)
5228 /* PR c++/28266 Make sure this is expanded only once. */
5229 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5230 return GS_ERROR;
5232 if (init)
5233 gimplify_and_add (init, pre_p);
5235 /* If needed, push the cleanup for the temp. */
5236 if (TARGET_EXPR_CLEANUP (targ))
5238 if (CLEANUP_EH_ONLY (targ))
5239 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5240 CLEANUP_EH_ONLY (targ), pre_p);
5241 else
5242 cleanup = TARGET_EXPR_CLEANUP (targ);
5245 /* Add a clobber for the temporary going out of scope, like
5246 gimplify_bind_expr. */
5247 if (gimplify_ctxp->in_cleanup_point_expr
5248 && needs_to_live_in_memory (temp)
5249 && flag_stack_reuse == SR_ALL)
5251 tree clobber = build_constructor (TREE_TYPE (temp),
5252 NULL);
5253 TREE_THIS_VOLATILE (clobber) = true;
5254 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5255 if (cleanup)
5256 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5257 clobber);
5258 else
5259 cleanup = clobber;
5262 if (cleanup)
5263 gimple_push_cleanup (temp, cleanup, false, pre_p);
5265 /* Only expand this once. */
5266 TREE_OPERAND (targ, 3) = init;
5267 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5269 else
5270 /* We should have expanded this before. */
5271 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5273 *expr_p = temp;
5274 return GS_OK;
5277 /* Gimplification of expression trees. */
5279 /* Gimplify an expression which appears at statement context. The
5280 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5281 NULL, a new sequence is allocated.
5283 Return true if we actually added a statement to the queue. */
5285 bool
5286 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5288 gimple_seq_node last;
5290 last = gimple_seq_last (*seq_p);
5291 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5292 return last != gimple_seq_last (*seq_p);
5295 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5296 to CTX. If entries already exist, force them to be some flavor of private.
5297 If there is no enclosing parallel, do nothing. */
5299 void
5300 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5302 splay_tree_node n;
5304 if (decl == NULL || !DECL_P (decl))
5305 return;
5309 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5310 if (n != NULL)
5312 if (n->value & GOVD_SHARED)
5313 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5314 else if (n->value & GOVD_MAP)
5315 n->value |= GOVD_MAP_TO_ONLY;
5316 else
5317 return;
5319 else if (ctx->region_type == ORT_TARGET)
5320 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5321 else if (ctx->region_type != ORT_WORKSHARE
5322 && ctx->region_type != ORT_SIMD
5323 && ctx->region_type != ORT_TARGET_DATA)
5324 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5326 ctx = ctx->outer_context;
5328 while (ctx);
5331 /* Similarly for each of the type sizes of TYPE. */
5333 static void
5334 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5336 if (type == NULL || type == error_mark_node)
5337 return;
5338 type = TYPE_MAIN_VARIANT (type);
5340 if (pointer_set_insert (ctx->privatized_types, type))
5341 return;
5343 switch (TREE_CODE (type))
5345 case INTEGER_TYPE:
5346 case ENUMERAL_TYPE:
5347 case BOOLEAN_TYPE:
5348 case REAL_TYPE:
5349 case FIXED_POINT_TYPE:
5350 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5351 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5352 break;
5354 case ARRAY_TYPE:
5355 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5356 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5357 break;
5359 case RECORD_TYPE:
5360 case UNION_TYPE:
5361 case QUAL_UNION_TYPE:
5363 tree field;
5364 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5365 if (TREE_CODE (field) == FIELD_DECL)
5367 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5368 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5371 break;
5373 case POINTER_TYPE:
5374 case REFERENCE_TYPE:
5375 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5376 break;
5378 default:
5379 break;
5382 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5383 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5384 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5387 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5389 static void
5390 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5392 splay_tree_node n;
5393 unsigned int nflags;
5394 tree t;
5396 if (error_operand_p (decl))
5397 return;
5399 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5400 there are constructors involved somewhere. */
5401 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5402 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5403 flags |= GOVD_SEEN;
5405 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5406 if (n != NULL && n->value != GOVD_ALIGNED)
5408 /* We shouldn't be re-adding the decl with the same data
5409 sharing class. */
5410 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5411 /* The only combination of data sharing classes we should see is
5412 FIRSTPRIVATE and LASTPRIVATE. */
5413 nflags = n->value | flags;
5414 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5415 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5416 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5417 n->value = nflags;
5418 return;
5421 /* When adding a variable-sized variable, we have to handle all sorts
5422 of additional bits of data: the pointer replacement variable, and
5423 the parameters of the type. */
5424 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5426 /* Add the pointer replacement variable as PRIVATE if the variable
5427 replacement is private, else FIRSTPRIVATE since we'll need the
5428 address of the original variable either for SHARED, or for the
5429 copy into or out of the context. */
5430 if (!(flags & GOVD_LOCAL))
5432 nflags = flags & GOVD_MAP
5433 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5434 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5435 nflags |= flags & GOVD_SEEN;
5436 t = DECL_VALUE_EXPR (decl);
5437 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5438 t = TREE_OPERAND (t, 0);
5439 gcc_assert (DECL_P (t));
5440 omp_add_variable (ctx, t, nflags);
5443 /* Add all of the variable and type parameters (which should have
5444 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5445 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5446 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5447 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5449 /* The variable-sized variable itself is never SHARED, only some form
5450 of PRIVATE. The sharing would take place via the pointer variable
5451 which we remapped above. */
5452 if (flags & GOVD_SHARED)
5453 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5454 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5456 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5457 alloca statement we generate for the variable, so make sure it
5458 is available. This isn't automatically needed for the SHARED
5459 case, since we won't be allocating local storage then.
5460 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5461 in this case omp_notice_variable will be called later
5462 on when it is gimplified. */
5463 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5464 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5465 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5467 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5468 && lang_hooks.decls.omp_privatize_by_reference (decl))
5470 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5472 /* Similar to the direct variable sized case above, we'll need the
5473 size of references being privatized. */
5474 if ((flags & GOVD_SHARED) == 0)
5476 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5477 if (TREE_CODE (t) != INTEGER_CST)
5478 omp_notice_variable (ctx, t, true);
5482 if (n != NULL)
5483 n->value |= flags;
5484 else
5485 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5488 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5489 This just prints out diagnostics about threadprivate variable uses
5490 in untied tasks. If DECL2 is non-NULL, prevent this warning
5491 on that variable. */
5493 static bool
5494 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5495 tree decl2)
5497 splay_tree_node n;
5498 struct gimplify_omp_ctx *octx;
5500 for (octx = ctx; octx; octx = octx->outer_context)
5501 if (octx->region_type == ORT_TARGET)
5503 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5504 if (n == NULL)
5506 error ("threadprivate variable %qE used in target region",
5507 DECL_NAME (decl));
5508 error_at (octx->location, "enclosing target region");
5509 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5511 if (decl2)
5512 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5515 if (ctx->region_type != ORT_UNTIED_TASK)
5516 return false;
5517 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5518 if (n == NULL)
5520 error ("threadprivate variable %qE used in untied task",
5521 DECL_NAME (decl));
5522 error_at (ctx->location, "enclosing task");
5523 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5525 if (decl2)
5526 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5527 return false;
5530 /* Record the fact that DECL was used within the OpenMP context CTX.
5531 IN_CODE is true when real code uses DECL, and false when we should
5532 merely emit default(none) errors. Return true if DECL is going to
5533 be remapped and thus DECL shouldn't be gimplified into its
5534 DECL_VALUE_EXPR (if any). */
5536 static bool
5537 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5539 splay_tree_node n;
5540 unsigned flags = in_code ? GOVD_SEEN : 0;
5541 bool ret = false, shared;
5543 if (error_operand_p (decl))
5544 return false;
5546 /* Threadprivate variables are predetermined. */
5547 if (is_global_var (decl))
5549 if (DECL_THREAD_LOCAL_P (decl))
5550 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5552 if (DECL_HAS_VALUE_EXPR_P (decl))
5554 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5556 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5557 return omp_notice_threadprivate_variable (ctx, decl, value);
5561 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5562 if (ctx->region_type == ORT_TARGET)
5564 if (n == NULL)
5566 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5568 error ("%qD referenced in target region does not have "
5569 "a mappable type", decl);
5570 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5572 else
5573 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5575 else
5576 n->value |= flags;
5577 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5578 goto do_outer;
5581 if (n == NULL)
5583 enum omp_clause_default_kind default_kind, kind;
5584 struct gimplify_omp_ctx *octx;
5586 if (ctx->region_type == ORT_WORKSHARE
5587 || ctx->region_type == ORT_SIMD
5588 || ctx->region_type == ORT_TARGET_DATA)
5589 goto do_outer;
5591 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5592 remapped firstprivate instead of shared. To some extent this is
5593 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5594 default_kind = ctx->default_kind;
5595 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5596 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5597 default_kind = kind;
5599 switch (default_kind)
5601 case OMP_CLAUSE_DEFAULT_NONE:
5602 if ((ctx->region_type & ORT_TASK) != 0)
5604 error ("%qE not specified in enclosing task",
5605 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5606 error_at (ctx->location, "enclosing task");
5608 else if (ctx->region_type == ORT_TEAMS)
5610 error ("%qE not specified in enclosing teams construct",
5611 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5612 error_at (ctx->location, "enclosing teams construct");
5614 else
5616 error ("%qE not specified in enclosing parallel",
5617 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5618 error_at (ctx->location, "enclosing parallel");
5620 /* FALLTHRU */
5621 case OMP_CLAUSE_DEFAULT_SHARED:
5622 flags |= GOVD_SHARED;
5623 break;
5624 case OMP_CLAUSE_DEFAULT_PRIVATE:
5625 flags |= GOVD_PRIVATE;
5626 break;
5627 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5628 flags |= GOVD_FIRSTPRIVATE;
5629 break;
5630 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5631 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5632 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5633 if (ctx->outer_context)
5634 omp_notice_variable (ctx->outer_context, decl, in_code);
5635 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5637 splay_tree_node n2;
5639 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5640 continue;
5641 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5642 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5644 flags |= GOVD_FIRSTPRIVATE;
5645 break;
5647 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5648 break;
5650 if (flags & GOVD_FIRSTPRIVATE)
5651 break;
5652 if (octx == NULL
5653 && (TREE_CODE (decl) == PARM_DECL
5654 || (!is_global_var (decl)
5655 && DECL_CONTEXT (decl) == current_function_decl)))
5657 flags |= GOVD_FIRSTPRIVATE;
5658 break;
5660 flags |= GOVD_SHARED;
5661 break;
5662 default:
5663 gcc_unreachable ();
5666 if ((flags & GOVD_PRIVATE)
5667 && lang_hooks.decls.omp_private_outer_ref (decl))
5668 flags |= GOVD_PRIVATE_OUTER_REF;
5670 omp_add_variable (ctx, decl, flags);
5672 shared = (flags & GOVD_SHARED) != 0;
5673 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5674 goto do_outer;
5677 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5678 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5679 && DECL_SIZE (decl)
5680 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5682 splay_tree_node n2;
5683 tree t = DECL_VALUE_EXPR (decl);
5684 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5685 t = TREE_OPERAND (t, 0);
5686 gcc_assert (DECL_P (t));
5687 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5688 n2->value |= GOVD_SEEN;
5691 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5692 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5694 /* If nothing changed, there's nothing left to do. */
5695 if ((n->value & flags) == flags)
5696 return ret;
5697 flags |= n->value;
5698 n->value = flags;
5700 do_outer:
5701 /* If the variable is private in the current context, then we don't
5702 need to propagate anything to an outer context. */
5703 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5704 return ret;
5705 if (ctx->outer_context
5706 && omp_notice_variable (ctx->outer_context, decl, in_code))
5707 return true;
5708 return ret;
5711 /* Verify that DECL is private within CTX. If there's specific information
5712 to the contrary in the innermost scope, generate an error. */
5714 static bool
5715 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, bool simd)
5717 splay_tree_node n;
5719 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5720 if (n != NULL)
5722 if (n->value & GOVD_SHARED)
5724 if (ctx == gimplify_omp_ctxp)
5726 if (simd)
5727 error ("iteration variable %qE is predetermined linear",
5728 DECL_NAME (decl));
5729 else
5730 error ("iteration variable %qE should be private",
5731 DECL_NAME (decl));
5732 n->value = GOVD_PRIVATE;
5733 return true;
5735 else
5736 return false;
5738 else if ((n->value & GOVD_EXPLICIT) != 0
5739 && (ctx == gimplify_omp_ctxp
5740 || (ctx->region_type == ORT_COMBINED_PARALLEL
5741 && gimplify_omp_ctxp->outer_context == ctx)))
5743 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5744 error ("iteration variable %qE should not be firstprivate",
5745 DECL_NAME (decl));
5746 else if ((n->value & GOVD_REDUCTION) != 0)
5747 error ("iteration variable %qE should not be reduction",
5748 DECL_NAME (decl));
5749 else if (simd && (n->value & GOVD_LASTPRIVATE) != 0)
5750 error ("iteration variable %qE should not be lastprivate",
5751 DECL_NAME (decl));
5752 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5753 error ("iteration variable %qE should not be private",
5754 DECL_NAME (decl));
5755 else if (simd && (n->value & GOVD_LINEAR) != 0)
5756 error ("iteration variable %qE is predetermined linear",
5757 DECL_NAME (decl));
5759 return (ctx == gimplify_omp_ctxp
5760 || (ctx->region_type == ORT_COMBINED_PARALLEL
5761 && gimplify_omp_ctxp->outer_context == ctx));
5764 if (ctx->region_type != ORT_WORKSHARE
5765 && ctx->region_type != ORT_SIMD)
5766 return false;
5767 else if (ctx->outer_context)
5768 return omp_is_private (ctx->outer_context, decl, simd);
5769 return false;
5772 /* Return true if DECL is private within a parallel region
5773 that binds to the current construct's context or in parallel
5774 region's REDUCTION clause. */
5776 static bool
5777 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
5779 splay_tree_node n;
5783 ctx = ctx->outer_context;
5784 if (ctx == NULL)
5785 return !(is_global_var (decl)
5786 /* References might be private, but might be shared too. */
5787 || lang_hooks.decls.omp_privatize_by_reference (decl));
5789 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5790 continue;
5792 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5793 if (n != NULL)
5794 return (n->value & GOVD_SHARED) == 0;
5796 while (ctx->region_type == ORT_WORKSHARE
5797 || ctx->region_type == ORT_SIMD);
5798 return false;
5801 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5802 and previous omp contexts. */
5804 static void
5805 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5806 enum omp_region_type region_type)
5808 struct gimplify_omp_ctx *ctx, *outer_ctx;
5809 tree c;
5811 ctx = new_omp_context (region_type);
5812 outer_ctx = ctx->outer_context;
5814 while ((c = *list_p) != NULL)
5816 bool remove = false;
5817 bool notice_outer = true;
5818 const char *check_non_private = NULL;
5819 unsigned int flags;
5820 tree decl;
5822 switch (OMP_CLAUSE_CODE (c))
5824 case OMP_CLAUSE_PRIVATE:
5825 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5826 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5828 flags |= GOVD_PRIVATE_OUTER_REF;
5829 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5831 else
5832 notice_outer = false;
5833 goto do_add;
5834 case OMP_CLAUSE_SHARED:
5835 flags = GOVD_SHARED | GOVD_EXPLICIT;
5836 goto do_add;
5837 case OMP_CLAUSE_FIRSTPRIVATE:
5838 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5839 check_non_private = "firstprivate";
5840 goto do_add;
5841 case OMP_CLAUSE_LASTPRIVATE:
5842 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5843 check_non_private = "lastprivate";
5844 goto do_add;
5845 case OMP_CLAUSE_REDUCTION:
5846 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5847 check_non_private = "reduction";
5848 goto do_add;
5849 case OMP_CLAUSE_LINEAR:
5850 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5851 is_gimple_val, fb_rvalue) == GS_ERROR)
5853 remove = true;
5854 break;
5856 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5857 goto do_add;
5859 case OMP_CLAUSE_MAP:
5860 if (OMP_CLAUSE_SIZE (c)
5861 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5862 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5864 remove = true;
5865 break;
5867 decl = OMP_CLAUSE_DECL (c);
5868 if (!DECL_P (decl))
5870 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
5871 NULL, is_gimple_lvalue, fb_lvalue)
5872 == GS_ERROR)
5874 remove = true;
5875 break;
5877 break;
5879 flags = GOVD_MAP | GOVD_EXPLICIT;
5880 goto do_add;
5882 case OMP_CLAUSE_DEPEND:
5883 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
5885 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
5886 NULL, is_gimple_val, fb_rvalue);
5887 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
5889 if (error_operand_p (OMP_CLAUSE_DECL (c)))
5891 remove = true;
5892 break;
5894 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
5895 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
5896 is_gimple_val, fb_rvalue) == GS_ERROR)
5898 remove = true;
5899 break;
5901 break;
5903 case OMP_CLAUSE_TO:
5904 case OMP_CLAUSE_FROM:
5905 if (OMP_CLAUSE_SIZE (c)
5906 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5907 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5909 remove = true;
5910 break;
5912 decl = OMP_CLAUSE_DECL (c);
5913 if (error_operand_p (decl))
5915 remove = true;
5916 break;
5918 if (!DECL_P (decl))
5920 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
5921 NULL, is_gimple_lvalue, fb_lvalue)
5922 == GS_ERROR)
5924 remove = true;
5925 break;
5927 break;
5929 goto do_notice;
5931 do_add:
5932 decl = OMP_CLAUSE_DECL (c);
5933 if (error_operand_p (decl))
5935 remove = true;
5936 break;
5938 omp_add_variable (ctx, decl, flags);
5939 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5940 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5942 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
5943 GOVD_LOCAL | GOVD_SEEN);
5944 gimplify_omp_ctxp = ctx;
5945 push_gimplify_context ();
5947 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
5948 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
5950 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
5951 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
5952 pop_gimplify_context
5953 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
5954 push_gimplify_context ();
5955 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
5956 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
5957 pop_gimplify_context
5958 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
5959 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
5960 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
5962 gimplify_omp_ctxp = outer_ctx;
5964 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5965 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
5967 gimplify_omp_ctxp = ctx;
5968 push_gimplify_context ();
5969 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
5971 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
5972 NULL, NULL);
5973 TREE_SIDE_EFFECTS (bind) = 1;
5974 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
5975 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
5977 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
5978 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
5979 pop_gimplify_context
5980 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
5981 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
5983 gimplify_omp_ctxp = outer_ctx;
5985 if (notice_outer)
5986 goto do_notice;
5987 break;
5989 case OMP_CLAUSE_COPYIN:
5990 case OMP_CLAUSE_COPYPRIVATE:
5991 decl = OMP_CLAUSE_DECL (c);
5992 if (error_operand_p (decl))
5994 remove = true;
5995 break;
5997 do_notice:
5998 if (outer_ctx)
5999 omp_notice_variable (outer_ctx, decl, true);
6000 if (check_non_private
6001 && region_type == ORT_WORKSHARE
6002 && omp_check_private (ctx, decl))
6004 error ("%s variable %qE is private in outer context",
6005 check_non_private, DECL_NAME (decl));
6006 remove = true;
6008 break;
6010 case OMP_CLAUSE_FINAL:
6011 case OMP_CLAUSE_IF:
6012 OMP_CLAUSE_OPERAND (c, 0)
6013 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6014 /* Fall through. */
6016 case OMP_CLAUSE_SCHEDULE:
6017 case OMP_CLAUSE_NUM_THREADS:
6018 case OMP_CLAUSE_NUM_TEAMS:
6019 case OMP_CLAUSE_THREAD_LIMIT:
6020 case OMP_CLAUSE_DIST_SCHEDULE:
6021 case OMP_CLAUSE_DEVICE:
6022 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6023 is_gimple_val, fb_rvalue) == GS_ERROR)
6024 remove = true;
6025 break;
6027 case OMP_CLAUSE_NOWAIT:
6028 case OMP_CLAUSE_ORDERED:
6029 case OMP_CLAUSE_UNTIED:
6030 case OMP_CLAUSE_COLLAPSE:
6031 case OMP_CLAUSE_MERGEABLE:
6032 case OMP_CLAUSE_PROC_BIND:
6033 case OMP_CLAUSE_SAFELEN:
6034 break;
6036 case OMP_CLAUSE_ALIGNED:
6037 decl = OMP_CLAUSE_DECL (c);
6038 if (error_operand_p (decl))
6040 remove = true;
6041 break;
6043 if (!is_global_var (decl)
6044 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6045 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6046 break;
6048 case OMP_CLAUSE_DEFAULT:
6049 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6050 break;
6052 default:
6053 gcc_unreachable ();
6056 if (remove)
6057 *list_p = OMP_CLAUSE_CHAIN (c);
6058 else
6059 list_p = &OMP_CLAUSE_CHAIN (c);
6062 gimplify_omp_ctxp = ctx;
6065 /* For all variables that were not actually used within the context,
6066 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6068 static int
6069 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6071 tree *list_p = (tree *) data;
6072 tree decl = (tree) n->key;
6073 unsigned flags = n->value;
6074 enum omp_clause_code code;
6075 tree clause;
6076 bool private_debug;
6078 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6079 return 0;
6080 if ((flags & GOVD_SEEN) == 0)
6081 return 0;
6082 if (flags & GOVD_DEBUG_PRIVATE)
6084 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6085 private_debug = true;
6087 else if (flags & GOVD_MAP)
6088 private_debug = false;
6089 else
6090 private_debug
6091 = lang_hooks.decls.omp_private_debug_clause (decl,
6092 !!(flags & GOVD_SHARED));
6093 if (private_debug)
6094 code = OMP_CLAUSE_PRIVATE;
6095 else if (flags & GOVD_MAP)
6096 code = OMP_CLAUSE_MAP;
6097 else if (flags & GOVD_SHARED)
6099 if (is_global_var (decl))
6101 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6102 while (ctx != NULL)
6104 splay_tree_node on
6105 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6106 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6107 | GOVD_PRIVATE | GOVD_REDUCTION
6108 | GOVD_LINEAR)) != 0)
6109 break;
6110 ctx = ctx->outer_context;
6112 if (ctx == NULL)
6113 return 0;
6115 code = OMP_CLAUSE_SHARED;
6117 else if (flags & GOVD_PRIVATE)
6118 code = OMP_CLAUSE_PRIVATE;
6119 else if (flags & GOVD_FIRSTPRIVATE)
6120 code = OMP_CLAUSE_FIRSTPRIVATE;
6121 else if (flags & GOVD_LASTPRIVATE)
6122 code = OMP_CLAUSE_LASTPRIVATE;
6123 else if (flags & GOVD_ALIGNED)
6124 return 0;
6125 else
6126 gcc_unreachable ();
6128 clause = build_omp_clause (input_location, code);
6129 OMP_CLAUSE_DECL (clause) = decl;
6130 OMP_CLAUSE_CHAIN (clause) = *list_p;
6131 if (private_debug)
6132 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6133 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6134 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6135 else if (code == OMP_CLAUSE_MAP)
6137 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6138 ? OMP_CLAUSE_MAP_TO
6139 : OMP_CLAUSE_MAP_TOFROM;
6140 if (DECL_SIZE (decl)
6141 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6143 tree decl2 = DECL_VALUE_EXPR (decl);
6144 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6145 decl2 = TREE_OPERAND (decl2, 0);
6146 gcc_assert (DECL_P (decl2));
6147 tree mem = build_simple_mem_ref (decl2);
6148 OMP_CLAUSE_DECL (clause) = mem;
6149 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6150 if (gimplify_omp_ctxp->outer_context)
6152 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6153 omp_notice_variable (ctx, decl2, true);
6154 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6156 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6157 OMP_CLAUSE_MAP);
6158 OMP_CLAUSE_DECL (nc) = decl;
6159 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6160 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6161 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6162 OMP_CLAUSE_CHAIN (clause) = nc;
6165 *list_p = clause;
6166 lang_hooks.decls.omp_finish_clause (clause);
6168 return 0;
6171 static void
6172 gimplify_adjust_omp_clauses (tree *list_p)
6174 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6175 tree c, decl;
6177 while ((c = *list_p) != NULL)
6179 splay_tree_node n;
6180 bool remove = false;
6182 switch (OMP_CLAUSE_CODE (c))
6184 case OMP_CLAUSE_PRIVATE:
6185 case OMP_CLAUSE_SHARED:
6186 case OMP_CLAUSE_FIRSTPRIVATE:
6187 case OMP_CLAUSE_LINEAR:
6188 decl = OMP_CLAUSE_DECL (c);
6189 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6190 remove = !(n->value & GOVD_SEEN);
6191 if (! remove)
6193 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6194 if ((n->value & GOVD_DEBUG_PRIVATE)
6195 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6197 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6198 || ((n->value & GOVD_DATA_SHARE_CLASS)
6199 == GOVD_PRIVATE));
6200 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6201 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6203 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6204 && ctx->outer_context
6205 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6206 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6207 && !is_global_var (decl))
6209 if (ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
6211 n = splay_tree_lookup (ctx->outer_context->variables,
6212 (splay_tree_key) decl);
6213 if (n == NULL
6214 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6216 int flags = OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6217 ? GOVD_LASTPRIVATE : GOVD_SHARED;
6218 if (n == NULL)
6219 omp_add_variable (ctx->outer_context, decl,
6220 flags | GOVD_SEEN);
6221 else
6222 n->value |= flags | GOVD_SEEN;
6225 else
6226 omp_notice_variable (ctx->outer_context, decl, true);
6229 break;
6231 case OMP_CLAUSE_LASTPRIVATE:
6232 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6233 accurately reflect the presence of a FIRSTPRIVATE clause. */
6234 decl = OMP_CLAUSE_DECL (c);
6235 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6236 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6237 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6238 break;
6240 case OMP_CLAUSE_ALIGNED:
6241 decl = OMP_CLAUSE_DECL (c);
6242 if (!is_global_var (decl))
6244 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6245 remove = n == NULL || !(n->value & GOVD_SEEN);
6246 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6248 struct gimplify_omp_ctx *octx;
6249 if (n != NULL
6250 && (n->value & (GOVD_DATA_SHARE_CLASS
6251 & ~GOVD_FIRSTPRIVATE)))
6252 remove = true;
6253 else
6254 for (octx = ctx->outer_context; octx;
6255 octx = octx->outer_context)
6257 n = splay_tree_lookup (octx->variables,
6258 (splay_tree_key) decl);
6259 if (n == NULL)
6260 continue;
6261 if (n->value & GOVD_LOCAL)
6262 break;
6263 /* We have to avoid assigning a shared variable
6264 to itself when trying to add
6265 __builtin_assume_aligned. */
6266 if (n->value & GOVD_SHARED)
6268 remove = true;
6269 break;
6274 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6276 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6277 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6278 remove = true;
6280 break;
6282 case OMP_CLAUSE_MAP:
6283 decl = OMP_CLAUSE_DECL (c);
6284 if (!DECL_P (decl))
6285 break;
6286 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6287 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6288 remove = true;
6289 else if (DECL_SIZE (decl)
6290 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6291 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6293 tree decl2 = DECL_VALUE_EXPR (decl);
6294 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6295 decl2 = TREE_OPERAND (decl2, 0);
6296 gcc_assert (DECL_P (decl2));
6297 tree mem = build_simple_mem_ref (decl2);
6298 OMP_CLAUSE_DECL (c) = mem;
6299 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6300 if (ctx->outer_context)
6302 omp_notice_variable (ctx->outer_context, decl2, true);
6303 omp_notice_variable (ctx->outer_context,
6304 OMP_CLAUSE_SIZE (c), true);
6306 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6307 OMP_CLAUSE_MAP);
6308 OMP_CLAUSE_DECL (nc) = decl;
6309 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6310 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6311 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6312 OMP_CLAUSE_CHAIN (c) = nc;
6313 c = nc;
6315 break;
6317 case OMP_CLAUSE_TO:
6318 case OMP_CLAUSE_FROM:
6319 decl = OMP_CLAUSE_DECL (c);
6320 if (!DECL_P (decl))
6321 break;
6322 if (DECL_SIZE (decl)
6323 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6325 tree decl2 = DECL_VALUE_EXPR (decl);
6326 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6327 decl2 = TREE_OPERAND (decl2, 0);
6328 gcc_assert (DECL_P (decl2));
6329 tree mem = build_simple_mem_ref (decl2);
6330 OMP_CLAUSE_DECL (c) = mem;
6331 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6332 if (ctx->outer_context)
6334 omp_notice_variable (ctx->outer_context, decl2, true);
6335 omp_notice_variable (ctx->outer_context,
6336 OMP_CLAUSE_SIZE (c), true);
6339 break;
6341 case OMP_CLAUSE_REDUCTION:
6342 case OMP_CLAUSE_COPYIN:
6343 case OMP_CLAUSE_COPYPRIVATE:
6344 case OMP_CLAUSE_IF:
6345 case OMP_CLAUSE_NUM_THREADS:
6346 case OMP_CLAUSE_NUM_TEAMS:
6347 case OMP_CLAUSE_THREAD_LIMIT:
6348 case OMP_CLAUSE_DIST_SCHEDULE:
6349 case OMP_CLAUSE_DEVICE:
6350 case OMP_CLAUSE_SCHEDULE:
6351 case OMP_CLAUSE_NOWAIT:
6352 case OMP_CLAUSE_ORDERED:
6353 case OMP_CLAUSE_DEFAULT:
6354 case OMP_CLAUSE_UNTIED:
6355 case OMP_CLAUSE_COLLAPSE:
6356 case OMP_CLAUSE_FINAL:
6357 case OMP_CLAUSE_MERGEABLE:
6358 case OMP_CLAUSE_PROC_BIND:
6359 case OMP_CLAUSE_SAFELEN:
6360 case OMP_CLAUSE_DEPEND:
6361 break;
6363 default:
6364 gcc_unreachable ();
6367 if (remove)
6368 *list_p = OMP_CLAUSE_CHAIN (c);
6369 else
6370 list_p = &OMP_CLAUSE_CHAIN (c);
6373 /* Add in any implicit data sharing. */
6374 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6376 gimplify_omp_ctxp = ctx->outer_context;
6377 delete_omp_context (ctx);
6380 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6381 gimplification of the body, as well as scanning the body for used
6382 variables. We need to do this scan now, because variable-sized
6383 decls will be decomposed during gimplification. */
6385 static void
6386 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6388 tree expr = *expr_p;
6389 gimple g;
6390 gimple_seq body = NULL;
6392 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6393 OMP_PARALLEL_COMBINED (expr)
6394 ? ORT_COMBINED_PARALLEL
6395 : ORT_PARALLEL);
6397 push_gimplify_context ();
6399 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6400 if (gimple_code (g) == GIMPLE_BIND)
6401 pop_gimplify_context (g);
6402 else
6403 pop_gimplify_context (NULL);
6405 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6407 g = gimple_build_omp_parallel (body,
6408 OMP_PARALLEL_CLAUSES (expr),
6409 NULL_TREE, NULL_TREE);
6410 if (OMP_PARALLEL_COMBINED (expr))
6411 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6412 gimplify_seq_add_stmt (pre_p, g);
6413 *expr_p = NULL_TREE;
6416 /* Gimplify the contents of an OMP_TASK statement. This involves
6417 gimplification of the body, as well as scanning the body for used
6418 variables. We need to do this scan now, because variable-sized
6419 decls will be decomposed during gimplification. */
6421 static void
6422 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6424 tree expr = *expr_p;
6425 gimple g;
6426 gimple_seq body = NULL;
6428 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6429 find_omp_clause (OMP_TASK_CLAUSES (expr),
6430 OMP_CLAUSE_UNTIED)
6431 ? ORT_UNTIED_TASK : ORT_TASK);
6433 push_gimplify_context ();
6435 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6436 if (gimple_code (g) == GIMPLE_BIND)
6437 pop_gimplify_context (g);
6438 else
6439 pop_gimplify_context (NULL);
6441 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6443 g = gimple_build_omp_task (body,
6444 OMP_TASK_CLAUSES (expr),
6445 NULL_TREE, NULL_TREE,
6446 NULL_TREE, NULL_TREE, NULL_TREE);
6447 gimplify_seq_add_stmt (pre_p, g);
6448 *expr_p = NULL_TREE;
6451 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6452 with non-NULL OMP_FOR_INIT. */
6454 static tree
6455 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6457 *walk_subtrees = 0;
6458 switch (TREE_CODE (*tp))
6460 case OMP_FOR:
6461 *walk_subtrees = 1;
6462 /* FALLTHRU */
6463 case OMP_SIMD:
6464 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6465 return *tp;
6466 break;
6467 case BIND_EXPR:
6468 case STATEMENT_LIST:
6469 case OMP_PARALLEL:
6470 *walk_subtrees = 1;
6471 break;
6472 default:
6473 break;
6475 return NULL_TREE;
6478 /* Gimplify the gross structure of an OMP_FOR statement. */
6480 static enum gimplify_status
6481 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6483 tree for_stmt, orig_for_stmt, decl, var, t;
6484 enum gimplify_status ret = GS_ALL_DONE;
6485 enum gimplify_status tret;
6486 gimple gfor;
6487 gimple_seq for_body, for_pre_body;
6488 int i;
6489 bool simd;
6490 bitmap has_decl_expr = NULL;
6492 orig_for_stmt = for_stmt = *expr_p;
6494 simd = TREE_CODE (for_stmt) == OMP_SIMD
6495 || TREE_CODE (for_stmt) == CILK_SIMD;
6496 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6497 simd ? ORT_SIMD : ORT_WORKSHARE);
6499 /* Handle OMP_FOR_INIT. */
6500 for_pre_body = NULL;
6501 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6503 has_decl_expr = BITMAP_ALLOC (NULL);
6504 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6505 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6506 == VAR_DECL)
6508 t = OMP_FOR_PRE_BODY (for_stmt);
6509 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6511 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6513 tree_stmt_iterator si;
6514 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6515 tsi_next (&si))
6517 t = tsi_stmt (si);
6518 if (TREE_CODE (t) == DECL_EXPR
6519 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6520 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6524 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6525 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6527 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6529 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6530 NULL, NULL);
6531 gcc_assert (for_stmt != NULL_TREE);
6532 gimplify_omp_ctxp->combined_loop = true;
6535 for_body = NULL;
6536 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6537 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6538 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6539 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6540 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6542 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6543 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6544 decl = TREE_OPERAND (t, 0);
6545 gcc_assert (DECL_P (decl));
6546 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6547 || POINTER_TYPE_P (TREE_TYPE (decl)));
6549 /* Make sure the iteration variable is private. */
6550 tree c = NULL_TREE;
6551 if (orig_for_stmt != for_stmt)
6552 /* Do this only on innermost construct for combined ones. */;
6553 else if (simd)
6555 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6556 (splay_tree_key)decl);
6557 omp_is_private (gimplify_omp_ctxp, decl, simd);
6558 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6559 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6560 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6562 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6563 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6564 if (has_decl_expr
6565 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6566 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6567 OMP_CLAUSE_DECL (c) = decl;
6568 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6569 OMP_FOR_CLAUSES (for_stmt) = c;
6570 omp_add_variable (gimplify_omp_ctxp, decl,
6571 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6573 else
6575 bool lastprivate
6576 = (!has_decl_expr
6577 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6578 c = build_omp_clause (input_location,
6579 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6580 : OMP_CLAUSE_PRIVATE);
6581 OMP_CLAUSE_DECL (c) = decl;
6582 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6583 omp_add_variable (gimplify_omp_ctxp, decl,
6584 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6585 | GOVD_SEEN);
6586 c = NULL_TREE;
6589 else if (omp_is_private (gimplify_omp_ctxp, decl, simd))
6590 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6591 else
6592 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6594 /* If DECL is not a gimple register, create a temporary variable to act
6595 as an iteration counter. This is valid, since DECL cannot be
6596 modified in the body of the loop. */
6597 if (orig_for_stmt != for_stmt)
6598 var = decl;
6599 else if (!is_gimple_reg (decl))
6601 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6602 TREE_OPERAND (t, 0) = var;
6604 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6606 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6608 else
6609 var = decl;
6611 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6612 is_gimple_val, fb_rvalue);
6613 ret = MIN (ret, tret);
6614 if (ret == GS_ERROR)
6615 return ret;
6617 /* Handle OMP_FOR_COND. */
6618 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6619 gcc_assert (COMPARISON_CLASS_P (t));
6620 gcc_assert (TREE_OPERAND (t, 0) == decl);
6622 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6623 is_gimple_val, fb_rvalue);
6624 ret = MIN (ret, tret);
6626 /* Handle OMP_FOR_INCR. */
6627 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6628 switch (TREE_CODE (t))
6630 case PREINCREMENT_EXPR:
6631 case POSTINCREMENT_EXPR:
6633 tree decl = TREE_OPERAND (t, 0);
6634 // c_omp_for_incr_canonicalize_ptr() should have been
6635 // called to massage things appropriately.
6636 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6638 if (orig_for_stmt != for_stmt)
6639 break;
6640 t = build_int_cst (TREE_TYPE (decl), 1);
6641 if (c)
6642 OMP_CLAUSE_LINEAR_STEP (c) = t;
6643 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6644 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6645 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6646 break;
6649 case PREDECREMENT_EXPR:
6650 case POSTDECREMENT_EXPR:
6651 if (orig_for_stmt != for_stmt)
6652 break;
6653 t = build_int_cst (TREE_TYPE (decl), -1);
6654 if (c)
6655 OMP_CLAUSE_LINEAR_STEP (c) = t;
6656 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6657 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6658 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6659 break;
6661 case MODIFY_EXPR:
6662 gcc_assert (TREE_OPERAND (t, 0) == decl);
6663 TREE_OPERAND (t, 0) = var;
6665 t = TREE_OPERAND (t, 1);
6666 switch (TREE_CODE (t))
6668 case PLUS_EXPR:
6669 if (TREE_OPERAND (t, 1) == decl)
6671 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6672 TREE_OPERAND (t, 0) = var;
6673 break;
6676 /* Fallthru. */
6677 case MINUS_EXPR:
6678 case POINTER_PLUS_EXPR:
6679 gcc_assert (TREE_OPERAND (t, 0) == decl);
6680 TREE_OPERAND (t, 0) = var;
6681 break;
6682 default:
6683 gcc_unreachable ();
6686 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6687 is_gimple_val, fb_rvalue);
6688 ret = MIN (ret, tret);
6689 if (c)
6691 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
6692 if (TREE_CODE (t) == MINUS_EXPR)
6694 t = TREE_OPERAND (t, 1);
6695 OMP_CLAUSE_LINEAR_STEP (c)
6696 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
6697 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6698 &for_pre_body, NULL,
6699 is_gimple_val, fb_rvalue);
6700 ret = MIN (ret, tret);
6703 break;
6705 default:
6706 gcc_unreachable ();
6709 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6710 && orig_for_stmt == for_stmt)
6712 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6713 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6714 && OMP_CLAUSE_DECL (c) == decl
6715 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6717 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6718 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6719 gcc_assert (TREE_OPERAND (t, 0) == var);
6720 t = TREE_OPERAND (t, 1);
6721 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6722 || TREE_CODE (t) == MINUS_EXPR
6723 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6724 gcc_assert (TREE_OPERAND (t, 0) == var);
6725 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6726 TREE_OPERAND (t, 1));
6727 gimplify_assign (decl, t,
6728 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6733 BITMAP_FREE (has_decl_expr);
6735 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
6737 if (orig_for_stmt != for_stmt)
6738 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6740 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6741 decl = TREE_OPERAND (t, 0);
6742 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6743 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6744 TREE_OPERAND (t, 0) = var;
6745 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6746 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
6747 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
6750 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
6752 int kind;
6753 switch (TREE_CODE (orig_for_stmt))
6755 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
6756 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
6757 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
6758 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
6759 default:
6760 gcc_unreachable ();
6762 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
6763 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6764 for_pre_body);
6765 if (orig_for_stmt != for_stmt)
6766 gimple_omp_for_set_combined_p (gfor, true);
6767 if (gimplify_omp_ctxp
6768 && (gimplify_omp_ctxp->combined_loop
6769 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
6770 && gimplify_omp_ctxp->outer_context
6771 && gimplify_omp_ctxp->outer_context->combined_loop)))
6773 gimple_omp_for_set_combined_into_p (gfor, true);
6774 if (gimplify_omp_ctxp->combined_loop)
6775 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
6776 else
6777 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
6780 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6782 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6783 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6784 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6785 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6786 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6787 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6788 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6789 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6792 gimplify_seq_add_stmt (pre_p, gfor);
6793 if (ret != GS_ALL_DONE)
6794 return GS_ERROR;
6795 *expr_p = NULL_TREE;
6796 return GS_ALL_DONE;
6799 /* Gimplify the gross structure of other OpenMP constructs.
6800 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
6801 and OMP_TEAMS. */
6803 static void
6804 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6806 tree expr = *expr_p;
6807 gimple stmt;
6808 gimple_seq body = NULL;
6809 enum omp_region_type ort = ORT_WORKSHARE;
6811 switch (TREE_CODE (expr))
6813 case OMP_SECTIONS:
6814 case OMP_SINGLE:
6815 break;
6816 case OMP_TARGET:
6817 ort = ORT_TARGET;
6818 break;
6819 case OMP_TARGET_DATA:
6820 ort = ORT_TARGET_DATA;
6821 break;
6822 case OMP_TEAMS:
6823 ort = ORT_TEAMS;
6824 break;
6825 default:
6826 gcc_unreachable ();
6828 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
6829 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
6831 push_gimplify_context ();
6832 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
6833 if (gimple_code (g) == GIMPLE_BIND)
6834 pop_gimplify_context (g);
6835 else
6836 pop_gimplify_context (NULL);
6837 if (ort == ORT_TARGET_DATA)
6839 gimple_seq cleanup = NULL;
6840 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
6841 g = gimple_build_call (fn, 0);
6842 gimple_seq_add_stmt (&cleanup, g);
6843 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
6844 body = NULL;
6845 gimple_seq_add_stmt (&body, g);
6848 else
6849 gimplify_and_add (OMP_BODY (expr), &body);
6850 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6852 switch (TREE_CODE (expr))
6854 case OMP_SECTIONS:
6855 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6856 break;
6857 case OMP_SINGLE:
6858 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6859 break;
6860 case OMP_TARGET:
6861 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
6862 OMP_CLAUSES (expr));
6863 break;
6864 case OMP_TARGET_DATA:
6865 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
6866 OMP_CLAUSES (expr));
6867 break;
6868 case OMP_TEAMS:
6869 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
6870 break;
6871 default:
6872 gcc_unreachable ();
6875 gimplify_seq_add_stmt (pre_p, stmt);
6876 *expr_p = NULL_TREE;
6879 /* Gimplify the gross structure of OpenMP target update construct. */
6881 static void
6882 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
6884 tree expr = *expr_p;
6885 gimple stmt;
6887 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
6888 ORT_WORKSHARE);
6889 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
6890 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
6891 OMP_TARGET_UPDATE_CLAUSES (expr));
6893 gimplify_seq_add_stmt (pre_p, stmt);
6894 *expr_p = NULL_TREE;
6897 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6898 stabilized the lhs of the atomic operation as *ADDR. Return true if
6899 EXPR is this stabilized form. */
6901 static bool
6902 goa_lhs_expr_p (tree expr, tree addr)
6904 /* Also include casts to other type variants. The C front end is fond
6905 of adding these for e.g. volatile variables. This is like
6906 STRIP_TYPE_NOPS but includes the main variant lookup. */
6907 STRIP_USELESS_TYPE_CONVERSION (expr);
6909 if (TREE_CODE (expr) == INDIRECT_REF)
6911 expr = TREE_OPERAND (expr, 0);
6912 while (expr != addr
6913 && (CONVERT_EXPR_P (expr)
6914 || TREE_CODE (expr) == NON_LVALUE_EXPR)
6915 && TREE_CODE (expr) == TREE_CODE (addr)
6916 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
6918 expr = TREE_OPERAND (expr, 0);
6919 addr = TREE_OPERAND (addr, 0);
6921 if (expr == addr)
6922 return true;
6923 return (TREE_CODE (addr) == ADDR_EXPR
6924 && TREE_CODE (expr) == ADDR_EXPR
6925 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
6927 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
6928 return true;
6929 return false;
6932 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
6933 expression does not involve the lhs, evaluate it into a temporary.
6934 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
6935 or -1 if an error was encountered. */
6937 static int
6938 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
6939 tree lhs_var)
6941 tree expr = *expr_p;
6942 int saw_lhs;
6944 if (goa_lhs_expr_p (expr, lhs_addr))
6946 *expr_p = lhs_var;
6947 return 1;
6949 if (is_gimple_val (expr))
6950 return 0;
6952 saw_lhs = 0;
6953 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
6955 case tcc_binary:
6956 case tcc_comparison:
6957 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
6958 lhs_var);
6959 case tcc_unary:
6960 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
6961 lhs_var);
6962 break;
6963 case tcc_expression:
6964 switch (TREE_CODE (expr))
6966 case TRUTH_ANDIF_EXPR:
6967 case TRUTH_ORIF_EXPR:
6968 case TRUTH_AND_EXPR:
6969 case TRUTH_OR_EXPR:
6970 case TRUTH_XOR_EXPR:
6971 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
6972 lhs_addr, lhs_var);
6973 case TRUTH_NOT_EXPR:
6974 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
6975 lhs_addr, lhs_var);
6976 break;
6977 case COMPOUND_EXPR:
6978 /* Break out any preevaluations from cp_build_modify_expr. */
6979 for (; TREE_CODE (expr) == COMPOUND_EXPR;
6980 expr = TREE_OPERAND (expr, 1))
6981 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
6982 *expr_p = expr;
6983 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
6984 default:
6985 break;
6987 break;
6988 default:
6989 break;
6992 if (saw_lhs == 0)
6994 enum gimplify_status gs;
6995 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
6996 if (gs != GS_ALL_DONE)
6997 saw_lhs = -1;
7000 return saw_lhs;
7003 /* Gimplify an OMP_ATOMIC statement. */
7005 static enum gimplify_status
7006 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7008 tree addr = TREE_OPERAND (*expr_p, 0);
7009 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7010 ? NULL : TREE_OPERAND (*expr_p, 1);
7011 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7012 tree tmp_load;
7013 gimple loadstmt, storestmt;
7015 tmp_load = create_tmp_reg (type, NULL);
7016 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7017 return GS_ERROR;
7019 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7020 != GS_ALL_DONE)
7021 return GS_ERROR;
7023 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7024 gimplify_seq_add_stmt (pre_p, loadstmt);
7025 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7026 != GS_ALL_DONE)
7027 return GS_ERROR;
7029 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7030 rhs = tmp_load;
7031 storestmt = gimple_build_omp_atomic_store (rhs);
7032 gimplify_seq_add_stmt (pre_p, storestmt);
7033 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7035 gimple_omp_atomic_set_seq_cst (loadstmt);
7036 gimple_omp_atomic_set_seq_cst (storestmt);
7038 switch (TREE_CODE (*expr_p))
7040 case OMP_ATOMIC_READ:
7041 case OMP_ATOMIC_CAPTURE_OLD:
7042 *expr_p = tmp_load;
7043 gimple_omp_atomic_set_need_value (loadstmt);
7044 break;
7045 case OMP_ATOMIC_CAPTURE_NEW:
7046 *expr_p = rhs;
7047 gimple_omp_atomic_set_need_value (storestmt);
7048 break;
7049 default:
7050 *expr_p = NULL;
7051 break;
7054 return GS_ALL_DONE;
7057 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7058 body, and adding some EH bits. */
7060 static enum gimplify_status
7061 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7063 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7064 gimple g;
7065 gimple_seq body = NULL;
7066 int subcode = 0;
7068 /* Wrap the transaction body in a BIND_EXPR so we have a context
7069 where to put decls for OpenMP. */
7070 if (TREE_CODE (tbody) != BIND_EXPR)
7072 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7073 TREE_SIDE_EFFECTS (bind) = 1;
7074 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7075 TRANSACTION_EXPR_BODY (expr) = bind;
7078 push_gimplify_context ();
7079 temp = voidify_wrapper_expr (*expr_p, NULL);
7081 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7082 pop_gimplify_context (g);
7084 g = gimple_build_transaction (body, NULL);
7085 if (TRANSACTION_EXPR_OUTER (expr))
7086 subcode = GTMA_IS_OUTER;
7087 else if (TRANSACTION_EXPR_RELAXED (expr))
7088 subcode = GTMA_IS_RELAXED;
7089 gimple_transaction_set_subcode (g, subcode);
7091 gimplify_seq_add_stmt (pre_p, g);
7093 if (temp)
7095 *expr_p = temp;
7096 return GS_OK;
7099 *expr_p = NULL_TREE;
7100 return GS_ALL_DONE;
7103 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7104 expression produces a value to be used as an operand inside a GIMPLE
7105 statement, the value will be stored back in *EXPR_P. This value will
7106 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7107 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7108 emitted in PRE_P and POST_P.
7110 Additionally, this process may overwrite parts of the input
7111 expression during gimplification. Ideally, it should be
7112 possible to do non-destructive gimplification.
7114 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7115 the expression needs to evaluate to a value to be used as
7116 an operand in a GIMPLE statement, this value will be stored in
7117 *EXPR_P on exit. This happens when the caller specifies one
7118 of fb_lvalue or fb_rvalue fallback flags.
7120 PRE_P will contain the sequence of GIMPLE statements corresponding
7121 to the evaluation of EXPR and all the side-effects that must
7122 be executed before the main expression. On exit, the last
7123 statement of PRE_P is the core statement being gimplified. For
7124 instance, when gimplifying 'if (++a)' the last statement in
7125 PRE_P will be 'if (t.1)' where t.1 is the result of
7126 pre-incrementing 'a'.
7128 POST_P will contain the sequence of GIMPLE statements corresponding
7129 to the evaluation of all the side-effects that must be executed
7130 after the main expression. If this is NULL, the post
7131 side-effects are stored at the end of PRE_P.
7133 The reason why the output is split in two is to handle post
7134 side-effects explicitly. In some cases, an expression may have
7135 inner and outer post side-effects which need to be emitted in
7136 an order different from the one given by the recursive
7137 traversal. For instance, for the expression (*p--)++ the post
7138 side-effects of '--' must actually occur *after* the post
7139 side-effects of '++'. However, gimplification will first visit
7140 the inner expression, so if a separate POST sequence was not
7141 used, the resulting sequence would be:
7143 1 t.1 = *p
7144 2 p = p - 1
7145 3 t.2 = t.1 + 1
7146 4 *p = t.2
7148 However, the post-decrement operation in line #2 must not be
7149 evaluated until after the store to *p at line #4, so the
7150 correct sequence should be:
7152 1 t.1 = *p
7153 2 t.2 = t.1 + 1
7154 3 *p = t.2
7155 4 p = p - 1
7157 So, by specifying a separate post queue, it is possible
7158 to emit the post side-effects in the correct order.
7159 If POST_P is NULL, an internal queue will be used. Before
7160 returning to the caller, the sequence POST_P is appended to
7161 the main output sequence PRE_P.
7163 GIMPLE_TEST_F points to a function that takes a tree T and
7164 returns nonzero if T is in the GIMPLE form requested by the
7165 caller. The GIMPLE predicates are in gimple.c.
7167 FALLBACK tells the function what sort of a temporary we want if
7168 gimplification cannot produce an expression that complies with
7169 GIMPLE_TEST_F.
7171 fb_none means that no temporary should be generated
7172 fb_rvalue means that an rvalue is OK to generate
7173 fb_lvalue means that an lvalue is OK to generate
7174 fb_either means that either is OK, but an lvalue is preferable.
7175 fb_mayfail means that gimplification may fail (in which case
7176 GS_ERROR will be returned)
7178 The return value is either GS_ERROR or GS_ALL_DONE, since this
7179 function iterates until EXPR is completely gimplified or an error
7180 occurs. */
7182 enum gimplify_status
7183 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7184 bool (*gimple_test_f) (tree), fallback_t fallback)
7186 tree tmp;
7187 gimple_seq internal_pre = NULL;
7188 gimple_seq internal_post = NULL;
7189 tree save_expr;
7190 bool is_statement;
7191 location_t saved_location;
7192 enum gimplify_status ret;
7193 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7195 save_expr = *expr_p;
7196 if (save_expr == NULL_TREE)
7197 return GS_ALL_DONE;
7199 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7200 is_statement = gimple_test_f == is_gimple_stmt;
7201 if (is_statement)
7202 gcc_assert (pre_p);
7204 /* Consistency checks. */
7205 if (gimple_test_f == is_gimple_reg)
7206 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7207 else if (gimple_test_f == is_gimple_val
7208 || gimple_test_f == is_gimple_call_addr
7209 || gimple_test_f == is_gimple_condexpr
7210 || gimple_test_f == is_gimple_mem_rhs
7211 || gimple_test_f == is_gimple_mem_rhs_or_call
7212 || gimple_test_f == is_gimple_reg_rhs
7213 || gimple_test_f == is_gimple_reg_rhs_or_call
7214 || gimple_test_f == is_gimple_asm_val
7215 || gimple_test_f == is_gimple_mem_ref_addr)
7216 gcc_assert (fallback & fb_rvalue);
7217 else if (gimple_test_f == is_gimple_min_lval
7218 || gimple_test_f == is_gimple_lvalue)
7219 gcc_assert (fallback & fb_lvalue);
7220 else if (gimple_test_f == is_gimple_addressable)
7221 gcc_assert (fallback & fb_either);
7222 else if (gimple_test_f == is_gimple_stmt)
7223 gcc_assert (fallback == fb_none);
7224 else
7226 /* We should have recognized the GIMPLE_TEST_F predicate to
7227 know what kind of fallback to use in case a temporary is
7228 needed to hold the value or address of *EXPR_P. */
7229 gcc_unreachable ();
7232 /* We used to check the predicate here and return immediately if it
7233 succeeds. This is wrong; the design is for gimplification to be
7234 idempotent, and for the predicates to only test for valid forms, not
7235 whether they are fully simplified. */
7236 if (pre_p == NULL)
7237 pre_p = &internal_pre;
7239 if (post_p == NULL)
7240 post_p = &internal_post;
7242 /* Remember the last statements added to PRE_P and POST_P. Every
7243 new statement added by the gimplification helpers needs to be
7244 annotated with location information. To centralize the
7245 responsibility, we remember the last statement that had been
7246 added to both queues before gimplifying *EXPR_P. If
7247 gimplification produces new statements in PRE_P and POST_P, those
7248 statements will be annotated with the same location information
7249 as *EXPR_P. */
7250 pre_last_gsi = gsi_last (*pre_p);
7251 post_last_gsi = gsi_last (*post_p);
7253 saved_location = input_location;
7254 if (save_expr != error_mark_node
7255 && EXPR_HAS_LOCATION (*expr_p))
7256 input_location = EXPR_LOCATION (*expr_p);
7258 /* Loop over the specific gimplifiers until the toplevel node
7259 remains the same. */
7262 /* Strip away as many useless type conversions as possible
7263 at the toplevel. */
7264 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7266 /* Remember the expr. */
7267 save_expr = *expr_p;
7269 /* Die, die, die, my darling. */
7270 if (save_expr == error_mark_node
7271 || (TREE_TYPE (save_expr)
7272 && TREE_TYPE (save_expr) == error_mark_node))
7274 ret = GS_ERROR;
7275 break;
7278 /* Do any language-specific gimplification. */
7279 ret = ((enum gimplify_status)
7280 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7281 if (ret == GS_OK)
7283 if (*expr_p == NULL_TREE)
7284 break;
7285 if (*expr_p != save_expr)
7286 continue;
7288 else if (ret != GS_UNHANDLED)
7289 break;
7291 /* Make sure that all the cases set 'ret' appropriately. */
7292 ret = GS_UNHANDLED;
7293 switch (TREE_CODE (*expr_p))
7295 /* First deal with the special cases. */
7297 case POSTINCREMENT_EXPR:
7298 case POSTDECREMENT_EXPR:
7299 case PREINCREMENT_EXPR:
7300 case PREDECREMENT_EXPR:
7301 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7302 fallback != fb_none,
7303 TREE_TYPE (*expr_p));
7304 break;
7306 case ARRAY_REF:
7307 case ARRAY_RANGE_REF:
7308 case REALPART_EXPR:
7309 case IMAGPART_EXPR:
7310 case COMPONENT_REF:
7311 case VIEW_CONVERT_EXPR:
7312 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7313 fallback ? fallback : fb_rvalue);
7314 break;
7316 case COND_EXPR:
7317 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7319 /* C99 code may assign to an array in a structure value of a
7320 conditional expression, and this has undefined behavior
7321 only on execution, so create a temporary if an lvalue is
7322 required. */
7323 if (fallback == fb_lvalue)
7325 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7326 mark_addressable (*expr_p);
7327 ret = GS_OK;
7329 break;
7331 case CILK_SPAWN_STMT:
7332 gcc_assert
7333 (fn_contains_cilk_spawn_p (cfun)
7334 && lang_hooks.cilkplus.cilk_detect_spawn_and_unwrap (expr_p));
7335 if (!seen_error ())
7337 ret = (enum gimplify_status)
7338 lang_hooks.cilkplus.gimplify_cilk_spawn (expr_p, pre_p,
7339 post_p);
7340 break;
7342 /* If errors are seen, then just process it as a CALL_EXPR. */
7344 case CALL_EXPR:
7345 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7347 /* C99 code may assign to an array in a structure returned
7348 from a function, and this has undefined behavior only on
7349 execution, so create a temporary if an lvalue is
7350 required. */
7351 if (fallback == fb_lvalue)
7353 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7354 mark_addressable (*expr_p);
7355 ret = GS_OK;
7357 break;
7359 case TREE_LIST:
7360 gcc_unreachable ();
7362 case COMPOUND_EXPR:
7363 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7364 break;
7366 case COMPOUND_LITERAL_EXPR:
7367 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7368 gimple_test_f, fallback);
7369 break;
7371 case MODIFY_EXPR:
7372 case INIT_EXPR:
7373 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7374 fallback != fb_none);
7375 break;
7377 case TRUTH_ANDIF_EXPR:
7378 case TRUTH_ORIF_EXPR:
7380 /* Preserve the original type of the expression and the
7381 source location of the outer expression. */
7382 tree org_type = TREE_TYPE (*expr_p);
7383 *expr_p = gimple_boolify (*expr_p);
7384 *expr_p = build3_loc (input_location, COND_EXPR,
7385 org_type, *expr_p,
7386 fold_convert_loc
7387 (input_location,
7388 org_type, boolean_true_node),
7389 fold_convert_loc
7390 (input_location,
7391 org_type, boolean_false_node));
7392 ret = GS_OK;
7393 break;
7396 case TRUTH_NOT_EXPR:
7398 tree type = TREE_TYPE (*expr_p);
7399 /* The parsers are careful to generate TRUTH_NOT_EXPR
7400 only with operands that are always zero or one.
7401 We do not fold here but handle the only interesting case
7402 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7403 *expr_p = gimple_boolify (*expr_p);
7404 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7405 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7406 TREE_TYPE (*expr_p),
7407 TREE_OPERAND (*expr_p, 0));
7408 else
7409 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7410 TREE_TYPE (*expr_p),
7411 TREE_OPERAND (*expr_p, 0),
7412 build_int_cst (TREE_TYPE (*expr_p), 1));
7413 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7414 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7415 ret = GS_OK;
7416 break;
7419 case ADDR_EXPR:
7420 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7421 break;
7423 case ANNOTATE_EXPR:
7425 tree cond = TREE_OPERAND (*expr_p, 0);
7426 tree id = TREE_OPERAND (*expr_p, 1);
7427 tree tmp = create_tmp_var_raw (TREE_TYPE(cond), NULL);
7428 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7429 gimple call = gimple_build_call_internal (IFN_ANNOTATE, 2,
7430 cond, id);
7431 gimple_call_set_lhs (call, tmp);
7432 gimplify_seq_add_stmt (pre_p, call);
7433 *expr_p = tmp;
7434 ret = GS_ALL_DONE;
7435 break;
7438 case VA_ARG_EXPR:
7439 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7440 break;
7442 CASE_CONVERT:
7443 if (IS_EMPTY_STMT (*expr_p))
7445 ret = GS_ALL_DONE;
7446 break;
7449 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7450 || fallback == fb_none)
7452 /* Just strip a conversion to void (or in void context) and
7453 try again. */
7454 *expr_p = TREE_OPERAND (*expr_p, 0);
7455 ret = GS_OK;
7456 break;
7459 ret = gimplify_conversion (expr_p);
7460 if (ret == GS_ERROR)
7461 break;
7462 if (*expr_p != save_expr)
7463 break;
7464 /* FALLTHRU */
7466 case FIX_TRUNC_EXPR:
7467 /* unary_expr: ... | '(' cast ')' val | ... */
7468 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7469 is_gimple_val, fb_rvalue);
7470 recalculate_side_effects (*expr_p);
7471 break;
7473 case INDIRECT_REF:
7475 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7476 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7477 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7479 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7480 if (*expr_p != save_expr)
7482 ret = GS_OK;
7483 break;
7486 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7487 is_gimple_reg, fb_rvalue);
7488 if (ret == GS_ERROR)
7489 break;
7491 recalculate_side_effects (*expr_p);
7492 *expr_p = fold_build2_loc (input_location, MEM_REF,
7493 TREE_TYPE (*expr_p),
7494 TREE_OPERAND (*expr_p, 0),
7495 build_int_cst (saved_ptr_type, 0));
7496 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7497 TREE_THIS_NOTRAP (*expr_p) = notrap;
7498 ret = GS_OK;
7499 break;
7502 /* We arrive here through the various re-gimplifcation paths. */
7503 case MEM_REF:
7504 /* First try re-folding the whole thing. */
7505 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7506 TREE_OPERAND (*expr_p, 0),
7507 TREE_OPERAND (*expr_p, 1));
7508 if (tmp)
7510 *expr_p = tmp;
7511 recalculate_side_effects (*expr_p);
7512 ret = GS_OK;
7513 break;
7515 /* Avoid re-gimplifying the address operand if it is already
7516 in suitable form. Re-gimplifying would mark the address
7517 operand addressable. Always gimplify when not in SSA form
7518 as we still may have to gimplify decls with value-exprs. */
7519 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7520 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7522 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7523 is_gimple_mem_ref_addr, fb_rvalue);
7524 if (ret == GS_ERROR)
7525 break;
7527 recalculate_side_effects (*expr_p);
7528 ret = GS_ALL_DONE;
7529 break;
7531 /* Constants need not be gimplified. */
7532 case INTEGER_CST:
7533 case REAL_CST:
7534 case FIXED_CST:
7535 case STRING_CST:
7536 case COMPLEX_CST:
7537 case VECTOR_CST:
7538 /* Drop the overflow flag on constants, we do not want
7539 that in the GIMPLE IL. */
7540 if (TREE_OVERFLOW_P (*expr_p))
7541 *expr_p = drop_tree_overflow (*expr_p);
7542 ret = GS_ALL_DONE;
7543 break;
7545 case CONST_DECL:
7546 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7547 CONST_DECL node. Otherwise the decl is replaceable by its
7548 value. */
7549 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7550 if (fallback & fb_lvalue)
7551 ret = GS_ALL_DONE;
7552 else
7554 *expr_p = DECL_INITIAL (*expr_p);
7555 ret = GS_OK;
7557 break;
7559 case DECL_EXPR:
7560 ret = gimplify_decl_expr (expr_p, pre_p);
7561 break;
7563 case BIND_EXPR:
7564 ret = gimplify_bind_expr (expr_p, pre_p);
7565 break;
7567 case LOOP_EXPR:
7568 ret = gimplify_loop_expr (expr_p, pre_p);
7569 break;
7571 case SWITCH_EXPR:
7572 ret = gimplify_switch_expr (expr_p, pre_p);
7573 break;
7575 case EXIT_EXPR:
7576 ret = gimplify_exit_expr (expr_p);
7577 break;
7579 case GOTO_EXPR:
7580 /* If the target is not LABEL, then it is a computed jump
7581 and the target needs to be gimplified. */
7582 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7584 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7585 NULL, is_gimple_val, fb_rvalue);
7586 if (ret == GS_ERROR)
7587 break;
7589 gimplify_seq_add_stmt (pre_p,
7590 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7591 ret = GS_ALL_DONE;
7592 break;
7594 case PREDICT_EXPR:
7595 gimplify_seq_add_stmt (pre_p,
7596 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7597 PREDICT_EXPR_OUTCOME (*expr_p)));
7598 ret = GS_ALL_DONE;
7599 break;
7601 case LABEL_EXPR:
7602 ret = GS_ALL_DONE;
7603 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7604 == current_function_decl);
7605 gimplify_seq_add_stmt (pre_p,
7606 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7607 break;
7609 case CASE_LABEL_EXPR:
7610 ret = gimplify_case_label_expr (expr_p, pre_p);
7611 break;
7613 case RETURN_EXPR:
7614 ret = gimplify_return_expr (*expr_p, pre_p);
7615 break;
7617 case CONSTRUCTOR:
7618 /* Don't reduce this in place; let gimplify_init_constructor work its
7619 magic. Buf if we're just elaborating this for side effects, just
7620 gimplify any element that has side-effects. */
7621 if (fallback == fb_none)
7623 unsigned HOST_WIDE_INT ix;
7624 tree val;
7625 tree temp = NULL_TREE;
7626 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7627 if (TREE_SIDE_EFFECTS (val))
7628 append_to_statement_list (val, &temp);
7630 *expr_p = temp;
7631 ret = temp ? GS_OK : GS_ALL_DONE;
7633 /* C99 code may assign to an array in a constructed
7634 structure or union, and this has undefined behavior only
7635 on execution, so create a temporary if an lvalue is
7636 required. */
7637 else if (fallback == fb_lvalue)
7639 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7640 mark_addressable (*expr_p);
7641 ret = GS_OK;
7643 else
7644 ret = GS_ALL_DONE;
7645 break;
7647 /* The following are special cases that are not handled by the
7648 original GIMPLE grammar. */
7650 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7651 eliminated. */
7652 case SAVE_EXPR:
7653 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7654 break;
7656 case BIT_FIELD_REF:
7657 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7658 post_p, is_gimple_lvalue, fb_either);
7659 recalculate_side_effects (*expr_p);
7660 break;
7662 case TARGET_MEM_REF:
7664 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7666 if (TMR_BASE (*expr_p))
7667 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7668 post_p, is_gimple_mem_ref_addr, fb_either);
7669 if (TMR_INDEX (*expr_p))
7670 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7671 post_p, is_gimple_val, fb_rvalue);
7672 if (TMR_INDEX2 (*expr_p))
7673 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7674 post_p, is_gimple_val, fb_rvalue);
7675 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7676 ret = MIN (r0, r1);
7678 break;
7680 case NON_LVALUE_EXPR:
7681 /* This should have been stripped above. */
7682 gcc_unreachable ();
7684 case ASM_EXPR:
7685 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7686 break;
7688 case TRY_FINALLY_EXPR:
7689 case TRY_CATCH_EXPR:
7691 gimple_seq eval, cleanup;
7692 gimple try_;
7694 /* Calls to destructors are generated automatically in FINALLY/CATCH
7695 block. They should have location as UNKNOWN_LOCATION. However,
7696 gimplify_call_expr will reset these call stmts to input_location
7697 if it finds stmt's location is unknown. To prevent resetting for
7698 destructors, we set the input_location to unknown.
7699 Note that this only affects the destructor calls in FINALLY/CATCH
7700 block, and will automatically reset to its original value by the
7701 end of gimplify_expr. */
7702 input_location = UNKNOWN_LOCATION;
7703 eval = cleanup = NULL;
7704 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7705 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7706 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7707 if (gimple_seq_empty_p (cleanup))
7709 gimple_seq_add_seq (pre_p, eval);
7710 ret = GS_ALL_DONE;
7711 break;
7713 try_ = gimple_build_try (eval, cleanup,
7714 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7715 ? GIMPLE_TRY_FINALLY
7716 : GIMPLE_TRY_CATCH);
7717 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7718 gimple_set_location (try_, saved_location);
7719 else
7720 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7721 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7722 gimple_try_set_catch_is_cleanup (try_,
7723 TRY_CATCH_IS_CLEANUP (*expr_p));
7724 gimplify_seq_add_stmt (pre_p, try_);
7725 ret = GS_ALL_DONE;
7726 break;
7729 case CLEANUP_POINT_EXPR:
7730 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7731 break;
7733 case TARGET_EXPR:
7734 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7735 break;
7737 case CATCH_EXPR:
7739 gimple c;
7740 gimple_seq handler = NULL;
7741 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7742 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7743 gimplify_seq_add_stmt (pre_p, c);
7744 ret = GS_ALL_DONE;
7745 break;
7748 case EH_FILTER_EXPR:
7750 gimple ehf;
7751 gimple_seq failure = NULL;
7753 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7754 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7755 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7756 gimplify_seq_add_stmt (pre_p, ehf);
7757 ret = GS_ALL_DONE;
7758 break;
7761 case OBJ_TYPE_REF:
7763 enum gimplify_status r0, r1;
7764 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7765 post_p, is_gimple_val, fb_rvalue);
7766 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7767 post_p, is_gimple_val, fb_rvalue);
7768 TREE_SIDE_EFFECTS (*expr_p) = 0;
7769 ret = MIN (r0, r1);
7771 break;
7773 case LABEL_DECL:
7774 /* We get here when taking the address of a label. We mark
7775 the label as "forced"; meaning it can never be removed and
7776 it is a potential target for any computed goto. */
7777 FORCED_LABEL (*expr_p) = 1;
7778 ret = GS_ALL_DONE;
7779 break;
7781 case STATEMENT_LIST:
7782 ret = gimplify_statement_list (expr_p, pre_p);
7783 break;
7785 case WITH_SIZE_EXPR:
7787 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7788 post_p == &internal_post ? NULL : post_p,
7789 gimple_test_f, fallback);
7790 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7791 is_gimple_val, fb_rvalue);
7792 ret = GS_ALL_DONE;
7794 break;
7796 case VAR_DECL:
7797 case PARM_DECL:
7798 ret = gimplify_var_or_parm_decl (expr_p);
7799 break;
7801 case RESULT_DECL:
7802 /* When within an OpenMP context, notice uses of variables. */
7803 if (gimplify_omp_ctxp)
7804 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7805 ret = GS_ALL_DONE;
7806 break;
7808 case SSA_NAME:
7809 /* Allow callbacks into the gimplifier during optimization. */
7810 ret = GS_ALL_DONE;
7811 break;
7813 case OMP_PARALLEL:
7814 gimplify_omp_parallel (expr_p, pre_p);
7815 ret = GS_ALL_DONE;
7816 break;
7818 case OMP_TASK:
7819 gimplify_omp_task (expr_p, pre_p);
7820 ret = GS_ALL_DONE;
7821 break;
7823 case OMP_FOR:
7824 case OMP_SIMD:
7825 case CILK_SIMD:
7826 case OMP_DISTRIBUTE:
7827 ret = gimplify_omp_for (expr_p, pre_p);
7828 break;
7830 case OMP_SECTIONS:
7831 case OMP_SINGLE:
7832 case OMP_TARGET:
7833 case OMP_TARGET_DATA:
7834 case OMP_TEAMS:
7835 gimplify_omp_workshare (expr_p, pre_p);
7836 ret = GS_ALL_DONE;
7837 break;
7839 case OMP_TARGET_UPDATE:
7840 gimplify_omp_target_update (expr_p, pre_p);
7841 ret = GS_ALL_DONE;
7842 break;
7844 case OMP_SECTION:
7845 case OMP_MASTER:
7846 case OMP_TASKGROUP:
7847 case OMP_ORDERED:
7848 case OMP_CRITICAL:
7850 gimple_seq body = NULL;
7851 gimple g;
7853 gimplify_and_add (OMP_BODY (*expr_p), &body);
7854 switch (TREE_CODE (*expr_p))
7856 case OMP_SECTION:
7857 g = gimple_build_omp_section (body);
7858 break;
7859 case OMP_MASTER:
7860 g = gimple_build_omp_master (body);
7861 break;
7862 case OMP_TASKGROUP:
7864 gimple_seq cleanup = NULL;
7865 tree fn
7866 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
7867 g = gimple_build_call (fn, 0);
7868 gimple_seq_add_stmt (&cleanup, g);
7869 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7870 body = NULL;
7871 gimple_seq_add_stmt (&body, g);
7872 g = gimple_build_omp_taskgroup (body);
7874 break;
7875 case OMP_ORDERED:
7876 g = gimple_build_omp_ordered (body);
7877 break;
7878 case OMP_CRITICAL:
7879 g = gimple_build_omp_critical (body,
7880 OMP_CRITICAL_NAME (*expr_p));
7881 break;
7882 default:
7883 gcc_unreachable ();
7885 gimplify_seq_add_stmt (pre_p, g);
7886 ret = GS_ALL_DONE;
7887 break;
7890 case OMP_ATOMIC:
7891 case OMP_ATOMIC_READ:
7892 case OMP_ATOMIC_CAPTURE_OLD:
7893 case OMP_ATOMIC_CAPTURE_NEW:
7894 ret = gimplify_omp_atomic (expr_p, pre_p);
7895 break;
7897 case TRANSACTION_EXPR:
7898 ret = gimplify_transaction (expr_p, pre_p);
7899 break;
7901 case TRUTH_AND_EXPR:
7902 case TRUTH_OR_EXPR:
7903 case TRUTH_XOR_EXPR:
7905 tree orig_type = TREE_TYPE (*expr_p);
7906 tree new_type, xop0, xop1;
7907 *expr_p = gimple_boolify (*expr_p);
7908 new_type = TREE_TYPE (*expr_p);
7909 if (!useless_type_conversion_p (orig_type, new_type))
7911 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
7912 ret = GS_OK;
7913 break;
7916 /* Boolified binary truth expressions are semantically equivalent
7917 to bitwise binary expressions. Canonicalize them to the
7918 bitwise variant. */
7919 switch (TREE_CODE (*expr_p))
7921 case TRUTH_AND_EXPR:
7922 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
7923 break;
7924 case TRUTH_OR_EXPR:
7925 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
7926 break;
7927 case TRUTH_XOR_EXPR:
7928 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
7929 break;
7930 default:
7931 break;
7933 /* Now make sure that operands have compatible type to
7934 expression's new_type. */
7935 xop0 = TREE_OPERAND (*expr_p, 0);
7936 xop1 = TREE_OPERAND (*expr_p, 1);
7937 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
7938 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
7939 new_type,
7940 xop0);
7941 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
7942 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
7943 new_type,
7944 xop1);
7945 /* Continue classified as tcc_binary. */
7946 goto expr_2;
7949 case FMA_EXPR:
7950 case VEC_COND_EXPR:
7951 case VEC_PERM_EXPR:
7952 /* Classified as tcc_expression. */
7953 goto expr_3;
7955 case POINTER_PLUS_EXPR:
7957 enum gimplify_status r0, r1;
7958 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7959 post_p, is_gimple_val, fb_rvalue);
7960 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7961 post_p, is_gimple_val, fb_rvalue);
7962 recalculate_side_effects (*expr_p);
7963 ret = MIN (r0, r1);
7964 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
7965 after gimplifying operands - this is similar to how
7966 it would be folding all gimplified stmts on creation
7967 to have them canonicalized, which is what we eventually
7968 should do anyway. */
7969 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
7970 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
7972 *expr_p = build_fold_addr_expr_with_type_loc
7973 (input_location,
7974 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
7975 TREE_OPERAND (*expr_p, 0),
7976 fold_convert (ptr_type_node,
7977 TREE_OPERAND (*expr_p, 1))),
7978 TREE_TYPE (*expr_p));
7979 ret = MIN (ret, GS_OK);
7981 break;
7984 case CILK_SYNC_STMT:
7986 if (!fn_contains_cilk_spawn_p (cfun))
7988 error_at (EXPR_LOCATION (*expr_p),
7989 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
7990 ret = GS_ERROR;
7992 else
7994 gimplify_cilk_sync (expr_p, pre_p);
7995 ret = GS_ALL_DONE;
7997 break;
8000 default:
8001 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8003 case tcc_comparison:
8004 /* Handle comparison of objects of non scalar mode aggregates
8005 with a call to memcmp. It would be nice to only have to do
8006 this for variable-sized objects, but then we'd have to allow
8007 the same nest of reference nodes we allow for MODIFY_EXPR and
8008 that's too complex.
8010 Compare scalar mode aggregates as scalar mode values. Using
8011 memcmp for them would be very inefficient at best, and is
8012 plain wrong if bitfields are involved. */
8014 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8016 /* Vector comparisons need no boolification. */
8017 if (TREE_CODE (type) == VECTOR_TYPE)
8018 goto expr_2;
8019 else if (!AGGREGATE_TYPE_P (type))
8021 tree org_type = TREE_TYPE (*expr_p);
8022 *expr_p = gimple_boolify (*expr_p);
8023 if (!useless_type_conversion_p (org_type,
8024 TREE_TYPE (*expr_p)))
8026 *expr_p = fold_convert_loc (input_location,
8027 org_type, *expr_p);
8028 ret = GS_OK;
8030 else
8031 goto expr_2;
8033 else if (TYPE_MODE (type) != BLKmode)
8034 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8035 else
8036 ret = gimplify_variable_sized_compare (expr_p);
8038 break;
8041 /* If *EXPR_P does not need to be special-cased, handle it
8042 according to its class. */
8043 case tcc_unary:
8044 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8045 post_p, is_gimple_val, fb_rvalue);
8046 break;
8048 case tcc_binary:
8049 expr_2:
8051 enum gimplify_status r0, r1;
8053 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8054 post_p, is_gimple_val, fb_rvalue);
8055 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8056 post_p, is_gimple_val, fb_rvalue);
8058 ret = MIN (r0, r1);
8059 break;
8062 expr_3:
8064 enum gimplify_status r0, r1, r2;
8066 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8067 post_p, is_gimple_val, fb_rvalue);
8068 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8069 post_p, is_gimple_val, fb_rvalue);
8070 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8071 post_p, is_gimple_val, fb_rvalue);
8073 ret = MIN (MIN (r0, r1), r2);
8074 break;
8077 case tcc_declaration:
8078 case tcc_constant:
8079 ret = GS_ALL_DONE;
8080 goto dont_recalculate;
8082 default:
8083 gcc_unreachable ();
8086 recalculate_side_effects (*expr_p);
8088 dont_recalculate:
8089 break;
8092 gcc_assert (*expr_p || ret != GS_OK);
8094 while (ret == GS_OK);
8096 /* If we encountered an error_mark somewhere nested inside, either
8097 stub out the statement or propagate the error back out. */
8098 if (ret == GS_ERROR)
8100 if (is_statement)
8101 *expr_p = NULL;
8102 goto out;
8105 /* This was only valid as a return value from the langhook, which
8106 we handled. Make sure it doesn't escape from any other context. */
8107 gcc_assert (ret != GS_UNHANDLED);
8109 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8111 /* We aren't looking for a value, and we don't have a valid
8112 statement. If it doesn't have side-effects, throw it away. */
8113 if (!TREE_SIDE_EFFECTS (*expr_p))
8114 *expr_p = NULL;
8115 else if (!TREE_THIS_VOLATILE (*expr_p))
8117 /* This is probably a _REF that contains something nested that
8118 has side effects. Recurse through the operands to find it. */
8119 enum tree_code code = TREE_CODE (*expr_p);
8121 switch (code)
8123 case COMPONENT_REF:
8124 case REALPART_EXPR:
8125 case IMAGPART_EXPR:
8126 case VIEW_CONVERT_EXPR:
8127 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8128 gimple_test_f, fallback);
8129 break;
8131 case ARRAY_REF:
8132 case ARRAY_RANGE_REF:
8133 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8134 gimple_test_f, fallback);
8135 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8136 gimple_test_f, fallback);
8137 break;
8139 default:
8140 /* Anything else with side-effects must be converted to
8141 a valid statement before we get here. */
8142 gcc_unreachable ();
8145 *expr_p = NULL;
8147 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8148 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8150 /* Historically, the compiler has treated a bare reference
8151 to a non-BLKmode volatile lvalue as forcing a load. */
8152 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8154 /* Normally, we do not want to create a temporary for a
8155 TREE_ADDRESSABLE type because such a type should not be
8156 copied by bitwise-assignment. However, we make an
8157 exception here, as all we are doing here is ensuring that
8158 we read the bytes that make up the type. We use
8159 create_tmp_var_raw because create_tmp_var will abort when
8160 given a TREE_ADDRESSABLE type. */
8161 tree tmp = create_tmp_var_raw (type, "vol");
8162 gimple_add_tmp_var (tmp);
8163 gimplify_assign (tmp, *expr_p, pre_p);
8164 *expr_p = NULL;
8166 else
8167 /* We can't do anything useful with a volatile reference to
8168 an incomplete type, so just throw it away. Likewise for
8169 a BLKmode type, since any implicit inner load should
8170 already have been turned into an explicit one by the
8171 gimplification process. */
8172 *expr_p = NULL;
8175 /* If we are gimplifying at the statement level, we're done. Tack
8176 everything together and return. */
8177 if (fallback == fb_none || is_statement)
8179 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8180 it out for GC to reclaim it. */
8181 *expr_p = NULL_TREE;
8183 if (!gimple_seq_empty_p (internal_pre)
8184 || !gimple_seq_empty_p (internal_post))
8186 gimplify_seq_add_seq (&internal_pre, internal_post);
8187 gimplify_seq_add_seq (pre_p, internal_pre);
8190 /* The result of gimplifying *EXPR_P is going to be the last few
8191 statements in *PRE_P and *POST_P. Add location information
8192 to all the statements that were added by the gimplification
8193 helpers. */
8194 if (!gimple_seq_empty_p (*pre_p))
8195 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8197 if (!gimple_seq_empty_p (*post_p))
8198 annotate_all_with_location_after (*post_p, post_last_gsi,
8199 input_location);
8201 goto out;
8204 #ifdef ENABLE_GIMPLE_CHECKING
8205 if (*expr_p)
8207 enum tree_code code = TREE_CODE (*expr_p);
8208 /* These expressions should already be in gimple IR form. */
8209 gcc_assert (code != MODIFY_EXPR
8210 && code != ASM_EXPR
8211 && code != BIND_EXPR
8212 && code != CATCH_EXPR
8213 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8214 && code != EH_FILTER_EXPR
8215 && code != GOTO_EXPR
8216 && code != LABEL_EXPR
8217 && code != LOOP_EXPR
8218 && code != SWITCH_EXPR
8219 && code != TRY_FINALLY_EXPR
8220 && code != OMP_CRITICAL
8221 && code != OMP_FOR
8222 && code != OMP_MASTER
8223 && code != OMP_TASKGROUP
8224 && code != OMP_ORDERED
8225 && code != OMP_PARALLEL
8226 && code != OMP_SECTIONS
8227 && code != OMP_SECTION
8228 && code != OMP_SINGLE);
8230 #endif
8232 /* Otherwise we're gimplifying a subexpression, so the resulting
8233 value is interesting. If it's a valid operand that matches
8234 GIMPLE_TEST_F, we're done. Unless we are handling some
8235 post-effects internally; if that's the case, we need to copy into
8236 a temporary before adding the post-effects to POST_P. */
8237 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8238 goto out;
8240 /* Otherwise, we need to create a new temporary for the gimplified
8241 expression. */
8243 /* We can't return an lvalue if we have an internal postqueue. The
8244 object the lvalue refers to would (probably) be modified by the
8245 postqueue; we need to copy the value out first, which means an
8246 rvalue. */
8247 if ((fallback & fb_lvalue)
8248 && gimple_seq_empty_p (internal_post)
8249 && is_gimple_addressable (*expr_p))
8251 /* An lvalue will do. Take the address of the expression, store it
8252 in a temporary, and replace the expression with an INDIRECT_REF of
8253 that temporary. */
8254 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8255 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8256 *expr_p = build_simple_mem_ref (tmp);
8258 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8260 /* An rvalue will do. Assign the gimplified expression into a
8261 new temporary TMP and replace the original expression with
8262 TMP. First, make sure that the expression has a type so that
8263 it can be assigned into a temporary. */
8264 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8265 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8267 else
8269 #ifdef ENABLE_GIMPLE_CHECKING
8270 if (!(fallback & fb_mayfail))
8272 fprintf (stderr, "gimplification failed:\n");
8273 print_generic_expr (stderr, *expr_p, 0);
8274 debug_tree (*expr_p);
8275 internal_error ("gimplification failed");
8277 #endif
8278 gcc_assert (fallback & fb_mayfail);
8280 /* If this is an asm statement, and the user asked for the
8281 impossible, don't die. Fail and let gimplify_asm_expr
8282 issue an error. */
8283 ret = GS_ERROR;
8284 goto out;
8287 /* Make sure the temporary matches our predicate. */
8288 gcc_assert ((*gimple_test_f) (*expr_p));
8290 if (!gimple_seq_empty_p (internal_post))
8292 annotate_all_with_location (internal_post, input_location);
8293 gimplify_seq_add_seq (pre_p, internal_post);
8296 out:
8297 input_location = saved_location;
8298 return ret;
8301 /* Look through TYPE for variable-sized objects and gimplify each such
8302 size that we find. Add to LIST_P any statements generated. */
8304 void
8305 gimplify_type_sizes (tree type, gimple_seq *list_p)
8307 tree field, t;
8309 if (type == NULL || type == error_mark_node)
8310 return;
8312 /* We first do the main variant, then copy into any other variants. */
8313 type = TYPE_MAIN_VARIANT (type);
8315 /* Avoid infinite recursion. */
8316 if (TYPE_SIZES_GIMPLIFIED (type))
8317 return;
8319 TYPE_SIZES_GIMPLIFIED (type) = 1;
8321 switch (TREE_CODE (type))
8323 case INTEGER_TYPE:
8324 case ENUMERAL_TYPE:
8325 case BOOLEAN_TYPE:
8326 case REAL_TYPE:
8327 case FIXED_POINT_TYPE:
8328 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8329 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8331 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8333 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8334 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8336 break;
8338 case ARRAY_TYPE:
8339 /* These types may not have declarations, so handle them here. */
8340 gimplify_type_sizes (TREE_TYPE (type), list_p);
8341 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8342 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8343 with assigned stack slots, for -O1+ -g they should be tracked
8344 by VTA. */
8345 if (!(TYPE_NAME (type)
8346 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8347 && DECL_IGNORED_P (TYPE_NAME (type)))
8348 && TYPE_DOMAIN (type)
8349 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8351 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8352 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8353 DECL_IGNORED_P (t) = 0;
8354 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8355 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8356 DECL_IGNORED_P (t) = 0;
8358 break;
8360 case RECORD_TYPE:
8361 case UNION_TYPE:
8362 case QUAL_UNION_TYPE:
8363 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8364 if (TREE_CODE (field) == FIELD_DECL)
8366 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8367 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8368 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8369 gimplify_type_sizes (TREE_TYPE (field), list_p);
8371 break;
8373 case POINTER_TYPE:
8374 case REFERENCE_TYPE:
8375 /* We used to recurse on the pointed-to type here, which turned out to
8376 be incorrect because its definition might refer to variables not
8377 yet initialized at this point if a forward declaration is involved.
8379 It was actually useful for anonymous pointed-to types to ensure
8380 that the sizes evaluation dominates every possible later use of the
8381 values. Restricting to such types here would be safe since there
8382 is no possible forward declaration around, but would introduce an
8383 undesirable middle-end semantic to anonymity. We then defer to
8384 front-ends the responsibility of ensuring that the sizes are
8385 evaluated both early and late enough, e.g. by attaching artificial
8386 type declarations to the tree. */
8387 break;
8389 default:
8390 break;
8393 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8394 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8396 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8398 TYPE_SIZE (t) = TYPE_SIZE (type);
8399 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8400 TYPE_SIZES_GIMPLIFIED (t) = 1;
8404 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8405 a size or position, has had all of its SAVE_EXPRs evaluated.
8406 We add any required statements to *STMT_P. */
8408 void
8409 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8411 tree expr = *expr_p;
8413 /* We don't do anything if the value isn't there, is constant, or contains
8414 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8415 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8416 will want to replace it with a new variable, but that will cause problems
8417 if this type is from outside the function. It's OK to have that here. */
8418 if (is_gimple_sizepos (expr))
8419 return;
8421 *expr_p = unshare_expr (expr);
8423 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8426 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8427 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8428 is true, also gimplify the parameters. */
8430 gimple
8431 gimplify_body (tree fndecl, bool do_parms)
8433 location_t saved_location = input_location;
8434 gimple_seq parm_stmts, seq;
8435 gimple outer_bind;
8436 struct cgraph_node *cgn;
8438 timevar_push (TV_TREE_GIMPLIFY);
8440 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8441 gimplification. */
8442 default_rtl_profile ();
8444 gcc_assert (gimplify_ctxp == NULL);
8445 push_gimplify_context ();
8447 if (flag_openmp)
8449 gcc_assert (gimplify_omp_ctxp == NULL);
8450 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8451 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8454 /* Unshare most shared trees in the body and in that of any nested functions.
8455 It would seem we don't have to do this for nested functions because
8456 they are supposed to be output and then the outer function gimplified
8457 first, but the g++ front end doesn't always do it that way. */
8458 unshare_body (fndecl);
8459 unvisit_body (fndecl);
8461 cgn = cgraph_get_node (fndecl);
8462 if (cgn && cgn->origin)
8463 nonlocal_vlas = pointer_set_create ();
8465 /* Make sure input_location isn't set to something weird. */
8466 input_location = DECL_SOURCE_LOCATION (fndecl);
8468 /* Resolve callee-copies. This has to be done before processing
8469 the body so that DECL_VALUE_EXPR gets processed correctly. */
8470 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8472 /* Gimplify the function's body. */
8473 seq = NULL;
8474 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8475 outer_bind = gimple_seq_first_stmt (seq);
8476 if (!outer_bind)
8478 outer_bind = gimple_build_nop ();
8479 gimplify_seq_add_stmt (&seq, outer_bind);
8482 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8483 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8484 if (gimple_code (outer_bind) == GIMPLE_BIND
8485 && gimple_seq_first (seq) == gimple_seq_last (seq))
8487 else
8488 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8490 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8492 /* If we had callee-copies statements, insert them at the beginning
8493 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8494 if (!gimple_seq_empty_p (parm_stmts))
8496 tree parm;
8498 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8499 gimple_bind_set_body (outer_bind, parm_stmts);
8501 for (parm = DECL_ARGUMENTS (current_function_decl);
8502 parm; parm = DECL_CHAIN (parm))
8503 if (DECL_HAS_VALUE_EXPR_P (parm))
8505 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8506 DECL_IGNORED_P (parm) = 0;
8510 if (nonlocal_vlas)
8512 pointer_set_destroy (nonlocal_vlas);
8513 nonlocal_vlas = NULL;
8516 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8518 delete_omp_context (gimplify_omp_ctxp);
8519 gimplify_omp_ctxp = NULL;
8522 pop_gimplify_context (outer_bind);
8523 gcc_assert (gimplify_ctxp == NULL);
8525 #ifdef ENABLE_CHECKING
8526 if (!seen_error ())
8527 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8528 #endif
8530 timevar_pop (TV_TREE_GIMPLIFY);
8531 input_location = saved_location;
8533 return outer_bind;
8536 typedef char *char_p; /* For DEF_VEC_P. */
8538 /* Return whether we should exclude FNDECL from instrumentation. */
8540 static bool
8541 flag_instrument_functions_exclude_p (tree fndecl)
8543 vec<char_p> *v;
8545 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8546 if (v && v->length () > 0)
8548 const char *name;
8549 int i;
8550 char *s;
8552 name = lang_hooks.decl_printable_name (fndecl, 0);
8553 FOR_EACH_VEC_ELT (*v, i, s)
8554 if (strstr (name, s) != NULL)
8555 return true;
8558 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8559 if (v && v->length () > 0)
8561 const char *name;
8562 int i;
8563 char *s;
8565 name = DECL_SOURCE_FILE (fndecl);
8566 FOR_EACH_VEC_ELT (*v, i, s)
8567 if (strstr (name, s) != NULL)
8568 return true;
8571 return false;
8574 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8575 node for the function we want to gimplify.
8577 Return the sequence of GIMPLE statements corresponding to the body
8578 of FNDECL. */
8580 void
8581 gimplify_function_tree (tree fndecl)
8583 tree parm, ret;
8584 gimple_seq seq;
8585 gimple bind;
8587 gcc_assert (!gimple_body (fndecl));
8589 if (DECL_STRUCT_FUNCTION (fndecl))
8590 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8591 else
8592 push_struct_function (fndecl);
8594 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8596 /* Preliminarily mark non-addressed complex variables as eligible
8597 for promotion to gimple registers. We'll transform their uses
8598 as we find them. */
8599 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8600 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8601 && !TREE_THIS_VOLATILE (parm)
8602 && !needs_to_live_in_memory (parm))
8603 DECL_GIMPLE_REG_P (parm) = 1;
8606 ret = DECL_RESULT (fndecl);
8607 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8608 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8609 && !needs_to_live_in_memory (ret))
8610 DECL_GIMPLE_REG_P (ret) = 1;
8612 bind = gimplify_body (fndecl, true);
8614 /* The tree body of the function is no longer needed, replace it
8615 with the new GIMPLE body. */
8616 seq = NULL;
8617 gimple_seq_add_stmt (&seq, bind);
8618 gimple_set_body (fndecl, seq);
8620 /* If we're instrumenting function entry/exit, then prepend the call to
8621 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8622 catch the exit hook. */
8623 /* ??? Add some way to ignore exceptions for this TFE. */
8624 if (flag_instrument_function_entry_exit
8625 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8626 && !flag_instrument_functions_exclude_p (fndecl))
8628 tree x;
8629 gimple new_bind;
8630 gimple tf;
8631 gimple_seq cleanup = NULL, body = NULL;
8632 tree tmp_var;
8633 gimple call;
8635 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8636 call = gimple_build_call (x, 1, integer_zero_node);
8637 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8638 gimple_call_set_lhs (call, tmp_var);
8639 gimplify_seq_add_stmt (&cleanup, call);
8640 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8641 call = gimple_build_call (x, 2,
8642 build_fold_addr_expr (current_function_decl),
8643 tmp_var);
8644 gimplify_seq_add_stmt (&cleanup, call);
8645 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8647 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8648 call = gimple_build_call (x, 1, integer_zero_node);
8649 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8650 gimple_call_set_lhs (call, tmp_var);
8651 gimplify_seq_add_stmt (&body, call);
8652 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8653 call = gimple_build_call (x, 2,
8654 build_fold_addr_expr (current_function_decl),
8655 tmp_var);
8656 gimplify_seq_add_stmt (&body, call);
8657 gimplify_seq_add_stmt (&body, tf);
8658 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8659 /* Clear the block for BIND, since it is no longer directly inside
8660 the function, but within a try block. */
8661 gimple_bind_set_block (bind, NULL);
8663 /* Replace the current function body with the body
8664 wrapped in the try/finally TF. */
8665 seq = NULL;
8666 gimple_seq_add_stmt (&seq, new_bind);
8667 gimple_set_body (fndecl, seq);
8670 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8671 cfun->curr_properties = PROP_gimple_any;
8673 pop_cfun ();
8676 /* Return a dummy expression of type TYPE in order to keep going after an
8677 error. */
8679 static tree
8680 dummy_object (tree type)
8682 tree t = build_int_cst (build_pointer_type (type), 0);
8683 return build2 (MEM_REF, type, t, t);
8686 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
8687 builtin function, but a very special sort of operator. */
8689 enum gimplify_status
8690 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8692 tree promoted_type, have_va_type;
8693 tree valist = TREE_OPERAND (*expr_p, 0);
8694 tree type = TREE_TYPE (*expr_p);
8695 tree t;
8696 location_t loc = EXPR_LOCATION (*expr_p);
8698 /* Verify that valist is of the proper type. */
8699 have_va_type = TREE_TYPE (valist);
8700 if (have_va_type == error_mark_node)
8701 return GS_ERROR;
8702 have_va_type = targetm.canonical_va_list_type (have_va_type);
8704 if (have_va_type == NULL_TREE)
8706 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
8707 return GS_ERROR;
8710 /* Generate a diagnostic for requesting data of a type that cannot
8711 be passed through `...' due to type promotion at the call site. */
8712 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
8713 != type)
8715 static bool gave_help;
8716 bool warned;
8718 /* Unfortunately, this is merely undefined, rather than a constraint
8719 violation, so we cannot make this an error. If this call is never
8720 executed, the program is still strictly conforming. */
8721 warned = warning_at (loc, 0,
8722 "%qT is promoted to %qT when passed through %<...%>",
8723 type, promoted_type);
8724 if (!gave_help && warned)
8726 gave_help = true;
8727 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
8728 promoted_type, type);
8731 /* We can, however, treat "undefined" any way we please.
8732 Call abort to encourage the user to fix the program. */
8733 if (warned)
8734 inform (loc, "if this code is reached, the program will abort");
8735 /* Before the abort, allow the evaluation of the va_list
8736 expression to exit or longjmp. */
8737 gimplify_and_add (valist, pre_p);
8738 t = build_call_expr_loc (loc,
8739 builtin_decl_implicit (BUILT_IN_TRAP), 0);
8740 gimplify_and_add (t, pre_p);
8742 /* This is dead code, but go ahead and finish so that the
8743 mode of the result comes out right. */
8744 *expr_p = dummy_object (type);
8745 return GS_ALL_DONE;
8747 else
8749 /* Make it easier for the backends by protecting the valist argument
8750 from multiple evaluations. */
8751 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
8753 /* For this case, the backends will be expecting a pointer to
8754 TREE_TYPE (abi), but it's possible we've
8755 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
8756 So fix it. */
8757 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
8759 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
8760 valist = fold_convert_loc (loc, p1,
8761 build_fold_addr_expr_loc (loc, valist));
8764 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
8766 else
8767 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
8769 if (!targetm.gimplify_va_arg_expr)
8770 /* FIXME: Once most targets are converted we should merely
8771 assert this is non-null. */
8772 return GS_ALL_DONE;
8774 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
8775 return GS_OK;
8779 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
8781 DST/SRC are the destination and source respectively. You can pass
8782 ungimplified trees in DST or SRC, in which case they will be
8783 converted to a gimple operand if necessary.
8785 This function returns the newly created GIMPLE_ASSIGN tuple. */
8787 gimple
8788 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
8790 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
8791 gimplify_and_add (t, seq_p);
8792 ggc_free (t);
8793 return gimple_seq_last_stmt (*seq_p);
8796 inline hashval_t
8797 gimplify_hasher::hash (const value_type *p)
8799 tree t = p->val;
8800 return iterative_hash_expr (t, 0);
8803 inline bool
8804 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
8806 tree t1 = p1->val;
8807 tree t2 = p2->val;
8808 enum tree_code code = TREE_CODE (t1);
8810 if (TREE_CODE (t2) != code
8811 || TREE_TYPE (t1) != TREE_TYPE (t2))
8812 return false;
8814 if (!operand_equal_p (t1, t2, 0))
8815 return false;
8817 #ifdef ENABLE_CHECKING
8818 /* Only allow them to compare equal if they also hash equal; otherwise
8819 results are nondeterminate, and we fail bootstrap comparison. */
8820 gcc_assert (hash (p1) == hash (p2));
8821 #endif
8823 return true;