Enabled Usage of _Cilk_spawn and _Cilk_sync in Cilk Runtime (libcilkrts).
[official-gcc.git] / gcc / gimplify.c
blob1ca847ac7597b9ced2ed2014d2638a78aa9b5df2
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 "pointer-set.h"
29 #include "hash-table.h"
30 #include "basic-block.h"
31 #include "tree-ssa-alias.h"
32 #include "internal-fn.h"
33 #include "gimple-fold.h"
34 #include "tree-eh.h"
35 #include "gimple-expr.h"
36 #include "is-a.h"
37 #include "gimple.h"
38 #include "gimplify.h"
39 #include "gimple-iterator.h"
40 #include "stringpool.h"
41 #include "calls.h"
42 #include "varasm.h"
43 #include "stor-layout.h"
44 #include "stmt.h"
45 #include "print-tree.h"
46 #include "tree-iterator.h"
47 #include "tree-inline.h"
48 #include "tree-pretty-print.h"
49 #include "langhooks.h"
50 #include "bitmap.h"
51 #include "gimple-ssa.h"
52 #include "cgraph.h"
53 #include "tree-cfg.h"
54 #include "tree-ssanames.h"
55 #include "tree-ssa.h"
56 #include "diagnostic-core.h"
57 #include "target.h"
58 #include "splay-tree.h"
59 #include "omp-low.h"
60 #include "gimple-low.h"
61 #include "cilk.h"
63 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
64 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
66 enum gimplify_omp_var_data
68 GOVD_SEEN = 1,
69 GOVD_EXPLICIT = 2,
70 GOVD_SHARED = 4,
71 GOVD_PRIVATE = 8,
72 GOVD_FIRSTPRIVATE = 16,
73 GOVD_LASTPRIVATE = 32,
74 GOVD_REDUCTION = 64,
75 GOVD_LOCAL = 128,
76 GOVD_MAP = 256,
77 GOVD_DEBUG_PRIVATE = 512,
78 GOVD_PRIVATE_OUTER_REF = 1024,
79 GOVD_LINEAR = 2048,
80 GOVD_ALIGNED = 4096,
81 GOVD_MAP_TO_ONLY = 8192,
82 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
83 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
84 | GOVD_LOCAL)
88 enum omp_region_type
90 ORT_WORKSHARE = 0,
91 ORT_SIMD = 1,
92 ORT_PARALLEL = 2,
93 ORT_COMBINED_PARALLEL = 3,
94 ORT_TASK = 4,
95 ORT_UNTIED_TASK = 5,
96 ORT_TEAMS = 8,
97 ORT_TARGET_DATA = 16,
98 ORT_TARGET = 32
101 /* Gimplify hashtable helper. */
103 struct gimplify_hasher : typed_free_remove <elt_t>
105 typedef elt_t value_type;
106 typedef elt_t compare_type;
107 static inline hashval_t hash (const value_type *);
108 static inline bool equal (const value_type *, const compare_type *);
111 struct gimplify_ctx
113 struct gimplify_ctx *prev_context;
115 vec<gimple> bind_expr_stack;
116 tree temps;
117 gimple_seq conditional_cleanups;
118 tree exit_label;
119 tree return_temp;
121 vec<tree> case_labels;
122 /* The formal temporary table. Should this be persistent? */
123 hash_table <gimplify_hasher> temp_htab;
125 int conditions;
126 bool save_stack;
127 bool into_ssa;
128 bool allow_rhs_cond_expr;
129 bool in_cleanup_point_expr;
132 struct gimplify_omp_ctx
134 struct gimplify_omp_ctx *outer_context;
135 splay_tree variables;
136 struct pointer_set_t *privatized_types;
137 location_t location;
138 enum omp_clause_default_kind default_kind;
139 enum omp_region_type region_type;
140 bool combined_loop;
143 static struct gimplify_ctx *gimplify_ctxp;
144 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
146 /* Forward declaration. */
147 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
149 /* Shorter alias name for the above function for use in gimplify.c
150 only. */
152 static inline void
153 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
155 gimple_seq_add_stmt_without_update (seq_p, gs);
158 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
159 NULL, a new sequence is allocated. This function is
160 similar to gimple_seq_add_seq, but does not scan the operands.
161 During gimplification, we need to manipulate statement sequences
162 before the def/use vectors have been constructed. */
164 static void
165 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
167 gimple_stmt_iterator si;
169 if (src == NULL)
170 return;
172 si = gsi_last (*dst_p);
173 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
177 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
178 and popping gimplify contexts. */
180 static struct gimplify_ctx *ctx_pool = NULL;
182 /* Return a gimplify context struct from the pool. */
184 static inline struct gimplify_ctx *
185 ctx_alloc (void)
187 struct gimplify_ctx * c = ctx_pool;
189 if (c)
190 ctx_pool = c->prev_context;
191 else
192 c = XNEW (struct gimplify_ctx);
194 memset (c, '\0', sizeof (*c));
195 return c;
198 /* Put gimplify context C back into the pool. */
200 static inline void
201 ctx_free (struct gimplify_ctx *c)
203 c->prev_context = ctx_pool;
204 ctx_pool = c;
207 /* Free allocated ctx stack memory. */
209 void
210 free_gimplify_stack (void)
212 struct gimplify_ctx *c;
214 while ((c = ctx_pool))
216 ctx_pool = c->prev_context;
217 free (c);
222 /* Set up a context for the gimplifier. */
224 void
225 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
227 struct gimplify_ctx *c = ctx_alloc ();
229 c->prev_context = gimplify_ctxp;
230 gimplify_ctxp = c;
231 gimplify_ctxp->into_ssa = in_ssa;
232 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
235 /* Tear down a context for the gimplifier. If BODY is non-null, then
236 put the temporaries into the outer BIND_EXPR. Otherwise, put them
237 in the local_decls.
239 BODY is not a sequence, but the first tuple in a sequence. */
241 void
242 pop_gimplify_context (gimple body)
244 struct gimplify_ctx *c = gimplify_ctxp;
246 gcc_assert (c
247 && (!c->bind_expr_stack.exists ()
248 || c->bind_expr_stack.is_empty ()));
249 c->bind_expr_stack.release ();
250 gimplify_ctxp = c->prev_context;
252 if (body)
253 declare_vars (c->temps, body, false);
254 else
255 record_vars (c->temps);
257 if (c->temp_htab.is_created ())
258 c->temp_htab.dispose ();
259 ctx_free (c);
262 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
264 static void
265 gimple_push_bind_expr (gimple gimple_bind)
267 gimplify_ctxp->bind_expr_stack.reserve (8);
268 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
271 /* Pop the first element off the stack of bindings. */
273 static void
274 gimple_pop_bind_expr (void)
276 gimplify_ctxp->bind_expr_stack.pop ();
279 /* Return the first element of the stack of bindings. */
281 gimple
282 gimple_current_bind_expr (void)
284 return gimplify_ctxp->bind_expr_stack.last ();
287 /* Return the stack of bindings created during gimplification. */
289 vec<gimple>
290 gimple_bind_expr_stack (void)
292 return gimplify_ctxp->bind_expr_stack;
295 /* Return true iff there is a COND_EXPR between us and the innermost
296 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
298 static bool
299 gimple_conditional_context (void)
301 return gimplify_ctxp->conditions > 0;
304 /* Note that we've entered a COND_EXPR. */
306 static void
307 gimple_push_condition (void)
309 #ifdef ENABLE_GIMPLE_CHECKING
310 if (gimplify_ctxp->conditions == 0)
311 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
312 #endif
313 ++(gimplify_ctxp->conditions);
316 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
317 now, add any conditional cleanups we've seen to the prequeue. */
319 static void
320 gimple_pop_condition (gimple_seq *pre_p)
322 int conds = --(gimplify_ctxp->conditions);
324 gcc_assert (conds >= 0);
325 if (conds == 0)
327 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
328 gimplify_ctxp->conditional_cleanups = NULL;
332 /* A stable comparison routine for use with splay trees and DECLs. */
334 static int
335 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
337 tree a = (tree) xa;
338 tree b = (tree) xb;
340 return DECL_UID (a) - DECL_UID (b);
343 /* Create a new omp construct that deals with variable remapping. */
345 static struct gimplify_omp_ctx *
346 new_omp_context (enum omp_region_type region_type)
348 struct gimplify_omp_ctx *c;
350 c = XCNEW (struct gimplify_omp_ctx);
351 c->outer_context = gimplify_omp_ctxp;
352 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
353 c->privatized_types = pointer_set_create ();
354 c->location = input_location;
355 c->region_type = region_type;
356 if ((region_type & ORT_TASK) == 0)
357 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
358 else
359 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
361 return c;
364 /* Destroy an omp construct that deals with variable remapping. */
366 static void
367 delete_omp_context (struct gimplify_omp_ctx *c)
369 splay_tree_delete (c->variables);
370 pointer_set_destroy (c->privatized_types);
371 XDELETE (c);
374 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
375 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
377 /* Both gimplify the statement T and append it to *SEQ_P. This function
378 behaves exactly as gimplify_stmt, but you don't have to pass T as a
379 reference. */
381 void
382 gimplify_and_add (tree t, gimple_seq *seq_p)
384 gimplify_stmt (&t, seq_p);
387 /* Gimplify statement T into sequence *SEQ_P, and return the first
388 tuple in the sequence of generated tuples for this statement.
389 Return NULL if gimplifying T produced no tuples. */
391 static gimple
392 gimplify_and_return_first (tree t, gimple_seq *seq_p)
394 gimple_stmt_iterator last = gsi_last (*seq_p);
396 gimplify_and_add (t, seq_p);
398 if (!gsi_end_p (last))
400 gsi_next (&last);
401 return gsi_stmt (last);
403 else
404 return gimple_seq_first_stmt (*seq_p);
407 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
408 LHS, or for a call argument. */
410 static bool
411 is_gimple_mem_rhs (tree t)
413 /* If we're dealing with a renamable type, either source or dest must be
414 a renamed variable. */
415 if (is_gimple_reg_type (TREE_TYPE (t)))
416 return is_gimple_val (t);
417 else
418 return is_gimple_val (t) || is_gimple_lvalue (t);
421 /* Return true if T is a CALL_EXPR or an expression that can be
422 assigned to a temporary. Note that this predicate should only be
423 used during gimplification. See the rationale for this in
424 gimplify_modify_expr. */
426 static bool
427 is_gimple_reg_rhs_or_call (tree t)
429 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
430 || TREE_CODE (t) == CALL_EXPR);
433 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
434 this predicate should only be used during gimplification. See the
435 rationale for this in gimplify_modify_expr. */
437 static bool
438 is_gimple_mem_rhs_or_call (tree t)
440 /* If we're dealing with a renamable type, either source or dest must be
441 a renamed variable. */
442 if (is_gimple_reg_type (TREE_TYPE (t)))
443 return is_gimple_val (t);
444 else
445 return (is_gimple_val (t) || is_gimple_lvalue (t)
446 || TREE_CODE (t) == CALL_EXPR);
449 /* Create a temporary with a name derived from VAL. Subroutine of
450 lookup_tmp_var; nobody else should call this function. */
452 static inline tree
453 create_tmp_from_val (tree val, bool is_formal)
455 /* Drop all qualifiers and address-space information from the value type. */
456 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
457 tree var = create_tmp_var (type, get_name (val));
458 if (is_formal
459 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
460 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE))
461 DECL_GIMPLE_REG_P (var) = 1;
462 return var;
465 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
466 an existing expression temporary. */
468 static tree
469 lookup_tmp_var (tree val, bool is_formal)
471 tree ret;
473 /* If not optimizing, never really reuse a temporary. local-alloc
474 won't allocate any variable that is used in more than one basic
475 block, which means it will go into memory, causing much extra
476 work in reload and final and poorer code generation, outweighing
477 the extra memory allocation here. */
478 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
479 ret = create_tmp_from_val (val, is_formal);
480 else
482 elt_t elt, *elt_p;
483 elt_t **slot;
485 elt.val = val;
486 if (!gimplify_ctxp->temp_htab.is_created ())
487 gimplify_ctxp->temp_htab.create (1000);
488 slot = gimplify_ctxp->temp_htab.find_slot (&elt, INSERT);
489 if (*slot == NULL)
491 elt_p = XNEW (elt_t);
492 elt_p->val = val;
493 elt_p->temp = ret = create_tmp_from_val (val, is_formal);
494 *slot = elt_p;
496 else
498 elt_p = *slot;
499 ret = elt_p->temp;
503 return ret;
506 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
508 static tree
509 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
510 bool is_formal)
512 tree t, mod;
514 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
515 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
516 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
517 fb_rvalue);
519 if (gimplify_ctxp->into_ssa
520 && is_gimple_reg_type (TREE_TYPE (val)))
521 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
522 else
523 t = lookup_tmp_var (val, is_formal);
525 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
527 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
529 /* gimplify_modify_expr might want to reduce this further. */
530 gimplify_and_add (mod, pre_p);
531 ggc_free (mod);
533 return t;
536 /* Return a formal temporary variable initialized with VAL. PRE_P is as
537 in gimplify_expr. Only use this function if:
539 1) The value of the unfactored expression represented by VAL will not
540 change between the initialization and use of the temporary, and
541 2) The temporary will not be otherwise modified.
543 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
544 and #2 means it is inappropriate for && temps.
546 For other cases, use get_initialized_tmp_var instead. */
548 tree
549 get_formal_tmp_var (tree val, gimple_seq *pre_p)
551 return internal_get_tmp_var (val, pre_p, NULL, true);
554 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
555 are as in gimplify_expr. */
557 tree
558 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
560 return internal_get_tmp_var (val, pre_p, post_p, false);
563 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
564 generate debug info for them; otherwise don't. */
566 void
567 declare_vars (tree vars, gimple scope, bool debug_info)
569 tree last = vars;
570 if (last)
572 tree temps, block;
574 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
576 temps = nreverse (last);
578 block = gimple_bind_block (scope);
579 gcc_assert (!block || TREE_CODE (block) == BLOCK);
580 if (!block || !debug_info)
582 DECL_CHAIN (last) = gimple_bind_vars (scope);
583 gimple_bind_set_vars (scope, temps);
585 else
587 /* We need to attach the nodes both to the BIND_EXPR and to its
588 associated BLOCK for debugging purposes. The key point here
589 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
590 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
591 if (BLOCK_VARS (block))
592 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
593 else
595 gimple_bind_set_vars (scope,
596 chainon (gimple_bind_vars (scope), temps));
597 BLOCK_VARS (block) = temps;
603 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
604 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
605 no such upper bound can be obtained. */
607 static void
608 force_constant_size (tree var)
610 /* The only attempt we make is by querying the maximum size of objects
611 of the variable's type. */
613 HOST_WIDE_INT max_size;
615 gcc_assert (TREE_CODE (var) == VAR_DECL);
617 max_size = max_int_size_in_bytes (TREE_TYPE (var));
619 gcc_assert (max_size >= 0);
621 DECL_SIZE_UNIT (var)
622 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
623 DECL_SIZE (var)
624 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
627 /* Push the temporary variable TMP into the current binding. */
629 void
630 gimple_add_tmp_var (tree tmp)
632 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
634 /* Later processing assumes that the object size is constant, which might
635 not be true at this point. Force the use of a constant upper bound in
636 this case. */
637 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
638 force_constant_size (tmp);
640 DECL_CONTEXT (tmp) = current_function_decl;
641 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
643 if (gimplify_ctxp)
645 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
646 gimplify_ctxp->temps = tmp;
648 /* Mark temporaries local within the nearest enclosing parallel. */
649 if (gimplify_omp_ctxp)
651 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
652 while (ctx
653 && (ctx->region_type == ORT_WORKSHARE
654 || ctx->region_type == ORT_SIMD))
655 ctx = ctx->outer_context;
656 if (ctx)
657 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
660 else if (cfun)
661 record_vars (tmp);
662 else
664 gimple_seq body_seq;
666 /* This case is for nested functions. We need to expose the locals
667 they create. */
668 body_seq = gimple_body (current_function_decl);
669 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
675 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
676 nodes that are referenced more than once in GENERIC functions. This is
677 necessary because gimplification (translation into GIMPLE) is performed
678 by modifying tree nodes in-place, so gimplication of a shared node in a
679 first context could generate an invalid GIMPLE form in a second context.
681 This is achieved with a simple mark/copy/unmark algorithm that walks the
682 GENERIC representation top-down, marks nodes with TREE_VISITED the first
683 time it encounters them, duplicates them if they already have TREE_VISITED
684 set, and finally removes the TREE_VISITED marks it has set.
686 The algorithm works only at the function level, i.e. it generates a GENERIC
687 representation of a function with no nodes shared within the function when
688 passed a GENERIC function (except for nodes that are allowed to be shared).
690 At the global level, it is also necessary to unshare tree nodes that are
691 referenced in more than one function, for the same aforementioned reason.
692 This requires some cooperation from the front-end. There are 2 strategies:
694 1. Manual unsharing. The front-end needs to call unshare_expr on every
695 expression that might end up being shared across functions.
697 2. Deep unsharing. This is an extension of regular unsharing. Instead
698 of calling unshare_expr on expressions that might be shared across
699 functions, the front-end pre-marks them with TREE_VISITED. This will
700 ensure that they are unshared on the first reference within functions
701 when the regular unsharing algorithm runs. The counterpart is that
702 this algorithm must look deeper than for manual unsharing, which is
703 specified by LANG_HOOKS_DEEP_UNSHARING.
705 If there are only few specific cases of node sharing across functions, it is
706 probably easier for a front-end to unshare the expressions manually. On the
707 contrary, if the expressions generated at the global level are as widespread
708 as expressions generated within functions, deep unsharing is very likely the
709 way to go. */
711 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
712 These nodes model computations that must be done once. If we were to
713 unshare something like SAVE_EXPR(i++), the gimplification process would
714 create wrong code. However, if DATA is non-null, it must hold a pointer
715 set that is used to unshare the subtrees of these nodes. */
717 static tree
718 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
720 tree t = *tp;
721 enum tree_code code = TREE_CODE (t);
723 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
724 copy their subtrees if we can make sure to do it only once. */
725 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
727 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
729 else
730 *walk_subtrees = 0;
733 /* Stop at types, decls, constants like copy_tree_r. */
734 else if (TREE_CODE_CLASS (code) == tcc_type
735 || TREE_CODE_CLASS (code) == tcc_declaration
736 || TREE_CODE_CLASS (code) == tcc_constant
737 /* We can't do anything sensible with a BLOCK used as an
738 expression, but we also can't just die when we see it
739 because of non-expression uses. So we avert our eyes
740 and cross our fingers. Silly Java. */
741 || code == BLOCK)
742 *walk_subtrees = 0;
744 /* Cope with the statement expression extension. */
745 else if (code == STATEMENT_LIST)
748 /* Leave the bulk of the work to copy_tree_r itself. */
749 else
750 copy_tree_r (tp, walk_subtrees, NULL);
752 return NULL_TREE;
755 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
756 If *TP has been visited already, then *TP is deeply copied by calling
757 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
759 static tree
760 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
762 tree t = *tp;
763 enum tree_code code = TREE_CODE (t);
765 /* Skip types, decls, and constants. But we do want to look at their
766 types and the bounds of types. Mark them as visited so we properly
767 unmark their subtrees on the unmark pass. If we've already seen them,
768 don't look down further. */
769 if (TREE_CODE_CLASS (code) == tcc_type
770 || TREE_CODE_CLASS (code) == tcc_declaration
771 || TREE_CODE_CLASS (code) == tcc_constant)
773 if (TREE_VISITED (t))
774 *walk_subtrees = 0;
775 else
776 TREE_VISITED (t) = 1;
779 /* If this node has been visited already, unshare it and don't look
780 any deeper. */
781 else if (TREE_VISITED (t))
783 walk_tree (tp, mostly_copy_tree_r, data, NULL);
784 *walk_subtrees = 0;
787 /* Otherwise, mark the node as visited and keep looking. */
788 else
789 TREE_VISITED (t) = 1;
791 return NULL_TREE;
794 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
795 copy_if_shared_r callback unmodified. */
797 static inline void
798 copy_if_shared (tree *tp, void *data)
800 walk_tree (tp, copy_if_shared_r, data, NULL);
803 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
804 any nested functions. */
806 static void
807 unshare_body (tree fndecl)
809 struct cgraph_node *cgn = cgraph_get_node (fndecl);
810 /* If the language requires deep unsharing, we need a pointer set to make
811 sure we don't repeatedly unshare subtrees of unshareable nodes. */
812 struct pointer_set_t *visited
813 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
815 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
816 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
817 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
819 if (visited)
820 pointer_set_destroy (visited);
822 if (cgn)
823 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
824 unshare_body (cgn->decl);
827 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
828 Subtrees are walked until the first unvisited node is encountered. */
830 static tree
831 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
833 tree t = *tp;
835 /* If this node has been visited, unmark it and keep looking. */
836 if (TREE_VISITED (t))
837 TREE_VISITED (t) = 0;
839 /* Otherwise, don't look any deeper. */
840 else
841 *walk_subtrees = 0;
843 return NULL_TREE;
846 /* Unmark the visited trees rooted at *TP. */
848 static inline void
849 unmark_visited (tree *tp)
851 walk_tree (tp, unmark_visited_r, NULL, NULL);
854 /* Likewise, but mark all trees as not visited. */
856 static void
857 unvisit_body (tree fndecl)
859 struct cgraph_node *cgn = cgraph_get_node (fndecl);
861 unmark_visited (&DECL_SAVED_TREE (fndecl));
862 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
863 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
865 if (cgn)
866 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
867 unvisit_body (cgn->decl);
870 /* Unconditionally make an unshared copy of EXPR. This is used when using
871 stored expressions which span multiple functions, such as BINFO_VTABLE,
872 as the normal unsharing process can't tell that they're shared. */
874 tree
875 unshare_expr (tree expr)
877 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
878 return expr;
881 /* Worker for unshare_expr_without_location. */
883 static tree
884 prune_expr_location (tree *tp, int *walk_subtrees, void *)
886 if (EXPR_P (*tp))
887 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
888 else
889 *walk_subtrees = 0;
890 return NULL_TREE;
893 /* Similar to unshare_expr but also prune all expression locations
894 from EXPR. */
896 tree
897 unshare_expr_without_location (tree expr)
899 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
900 if (EXPR_P (expr))
901 walk_tree (&expr, prune_expr_location, NULL, NULL);
902 return expr;
905 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
906 contain statements and have a value. Assign its value to a temporary
907 and give it void_type_node. Return the temporary, or NULL_TREE if
908 WRAPPER was already void. */
910 tree
911 voidify_wrapper_expr (tree wrapper, tree temp)
913 tree type = TREE_TYPE (wrapper);
914 if (type && !VOID_TYPE_P (type))
916 tree *p;
918 /* Set p to point to the body of the wrapper. Loop until we find
919 something that isn't a wrapper. */
920 for (p = &wrapper; p && *p; )
922 switch (TREE_CODE (*p))
924 case BIND_EXPR:
925 TREE_SIDE_EFFECTS (*p) = 1;
926 TREE_TYPE (*p) = void_type_node;
927 /* For a BIND_EXPR, the body is operand 1. */
928 p = &BIND_EXPR_BODY (*p);
929 break;
931 case CLEANUP_POINT_EXPR:
932 case TRY_FINALLY_EXPR:
933 case TRY_CATCH_EXPR:
934 TREE_SIDE_EFFECTS (*p) = 1;
935 TREE_TYPE (*p) = void_type_node;
936 p = &TREE_OPERAND (*p, 0);
937 break;
939 case STATEMENT_LIST:
941 tree_stmt_iterator i = tsi_last (*p);
942 TREE_SIDE_EFFECTS (*p) = 1;
943 TREE_TYPE (*p) = void_type_node;
944 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
946 break;
948 case COMPOUND_EXPR:
949 /* Advance to the last statement. Set all container types to
950 void. */
951 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
953 TREE_SIDE_EFFECTS (*p) = 1;
954 TREE_TYPE (*p) = void_type_node;
956 break;
958 case TRANSACTION_EXPR:
959 TREE_SIDE_EFFECTS (*p) = 1;
960 TREE_TYPE (*p) = void_type_node;
961 p = &TRANSACTION_EXPR_BODY (*p);
962 break;
964 default:
965 /* Assume that any tree upon which voidify_wrapper_expr is
966 directly called is a wrapper, and that its body is op0. */
967 if (p == &wrapper)
969 TREE_SIDE_EFFECTS (*p) = 1;
970 TREE_TYPE (*p) = void_type_node;
971 p = &TREE_OPERAND (*p, 0);
972 break;
974 goto out;
978 out:
979 if (p == NULL || IS_EMPTY_STMT (*p))
980 temp = NULL_TREE;
981 else if (temp)
983 /* The wrapper is on the RHS of an assignment that we're pushing
984 down. */
985 gcc_assert (TREE_CODE (temp) == INIT_EXPR
986 || TREE_CODE (temp) == MODIFY_EXPR);
987 TREE_OPERAND (temp, 1) = *p;
988 *p = temp;
990 else
992 temp = create_tmp_var (type, "retval");
993 *p = build2 (INIT_EXPR, type, temp, *p);
996 return temp;
999 return NULL_TREE;
1002 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1003 a temporary through which they communicate. */
1005 static void
1006 build_stack_save_restore (gimple *save, gimple *restore)
1008 tree tmp_var;
1010 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1011 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1012 gimple_call_set_lhs (*save, tmp_var);
1014 *restore
1015 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1016 1, tmp_var);
1019 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1021 static enum gimplify_status
1022 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1024 tree bind_expr = *expr_p;
1025 bool old_save_stack = gimplify_ctxp->save_stack;
1026 tree t;
1027 gimple gimple_bind;
1028 gimple_seq body, cleanup;
1029 gimple stack_save;
1031 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1033 /* Mark variables seen in this bind expr. */
1034 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1036 if (TREE_CODE (t) == VAR_DECL)
1038 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1040 /* Mark variable as local. */
1041 if (ctx && !DECL_EXTERNAL (t)
1042 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1043 || splay_tree_lookup (ctx->variables,
1044 (splay_tree_key) t) == NULL))
1045 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1047 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1049 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1050 cfun->has_local_explicit_reg_vars = true;
1053 /* Preliminarily mark non-addressed complex variables as eligible
1054 for promotion to gimple registers. We'll transform their uses
1055 as we find them. */
1056 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1057 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1058 && !TREE_THIS_VOLATILE (t)
1059 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1060 && !needs_to_live_in_memory (t))
1061 DECL_GIMPLE_REG_P (t) = 1;
1064 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1065 BIND_EXPR_BLOCK (bind_expr));
1066 gimple_push_bind_expr (gimple_bind);
1068 gimplify_ctxp->save_stack = false;
1070 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1071 body = NULL;
1072 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1073 gimple_bind_set_body (gimple_bind, body);
1075 cleanup = NULL;
1076 stack_save = NULL;
1077 if (gimplify_ctxp->save_stack)
1079 gimple stack_restore;
1081 /* Save stack on entry and restore it on exit. Add a try_finally
1082 block to achieve this. */
1083 build_stack_save_restore (&stack_save, &stack_restore);
1085 gimplify_seq_add_stmt (&cleanup, stack_restore);
1088 /* Add clobbers for all variables that go out of scope. */
1089 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1091 if (TREE_CODE (t) == VAR_DECL
1092 && !is_global_var (t)
1093 && DECL_CONTEXT (t) == current_function_decl
1094 && !DECL_HARD_REGISTER (t)
1095 && !TREE_THIS_VOLATILE (t)
1096 && !DECL_HAS_VALUE_EXPR_P (t)
1097 /* Only care for variables that have to be in memory. Others
1098 will be rewritten into SSA names, hence moved to the top-level. */
1099 && !is_gimple_reg (t)
1100 && flag_stack_reuse != SR_NONE)
1102 tree clobber = build_constructor (TREE_TYPE (t),
1103 NULL);
1104 TREE_THIS_VOLATILE (clobber) = 1;
1105 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1109 if (cleanup)
1111 gimple gs;
1112 gimple_seq new_body;
1114 new_body = NULL;
1115 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1116 GIMPLE_TRY_FINALLY);
1118 if (stack_save)
1119 gimplify_seq_add_stmt (&new_body, stack_save);
1120 gimplify_seq_add_stmt (&new_body, gs);
1121 gimple_bind_set_body (gimple_bind, new_body);
1124 gimplify_ctxp->save_stack = old_save_stack;
1125 gimple_pop_bind_expr ();
1127 gimplify_seq_add_stmt (pre_p, gimple_bind);
1129 if (temp)
1131 *expr_p = temp;
1132 return GS_OK;
1135 *expr_p = NULL_TREE;
1136 return GS_ALL_DONE;
1139 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1140 GIMPLE value, it is assigned to a new temporary and the statement is
1141 re-written to return the temporary.
1143 PRE_P points to the sequence where side effects that must happen before
1144 STMT should be stored. */
1146 static enum gimplify_status
1147 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1149 gimple ret;
1150 tree ret_expr = TREE_OPERAND (stmt, 0);
1151 tree result_decl, result;
1153 if (ret_expr == error_mark_node)
1154 return GS_ERROR;
1156 /* Implicit _Cilk_sync must be inserted right before any return statement
1157 if there is a _Cilk_spawn in the function. If the user has provided a
1158 _Cilk_sync, the optimizer should remove this duplicate one. */
1159 if (fn_contains_cilk_spawn_p (cfun))
1161 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1162 gimplify_and_add (impl_sync, pre_p);
1165 if (!ret_expr
1166 || TREE_CODE (ret_expr) == RESULT_DECL
1167 || ret_expr == error_mark_node)
1169 gimple ret = gimple_build_return (ret_expr);
1170 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1171 gimplify_seq_add_stmt (pre_p, ret);
1172 return GS_ALL_DONE;
1175 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1176 result_decl = NULL_TREE;
1177 else
1179 result_decl = TREE_OPERAND (ret_expr, 0);
1181 /* See through a return by reference. */
1182 if (TREE_CODE (result_decl) == INDIRECT_REF)
1183 result_decl = TREE_OPERAND (result_decl, 0);
1185 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1186 || TREE_CODE (ret_expr) == INIT_EXPR)
1187 && TREE_CODE (result_decl) == RESULT_DECL);
1190 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1191 Recall that aggregate_value_p is FALSE for any aggregate type that is
1192 returned in registers. If we're returning values in registers, then
1193 we don't want to extend the lifetime of the RESULT_DECL, particularly
1194 across another call. In addition, for those aggregates for which
1195 hard_function_value generates a PARALLEL, we'll die during normal
1196 expansion of structure assignments; there's special code in expand_return
1197 to handle this case that does not exist in expand_expr. */
1198 if (!result_decl)
1199 result = NULL_TREE;
1200 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1202 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1204 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1205 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1206 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1207 should be effectively allocated by the caller, i.e. all calls to
1208 this function must be subject to the Return Slot Optimization. */
1209 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1210 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1212 result = result_decl;
1214 else if (gimplify_ctxp->return_temp)
1215 result = gimplify_ctxp->return_temp;
1216 else
1218 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1220 /* ??? With complex control flow (usually involving abnormal edges),
1221 we can wind up warning about an uninitialized value for this. Due
1222 to how this variable is constructed and initialized, this is never
1223 true. Give up and never warn. */
1224 TREE_NO_WARNING (result) = 1;
1226 gimplify_ctxp->return_temp = result;
1229 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1230 Then gimplify the whole thing. */
1231 if (result != result_decl)
1232 TREE_OPERAND (ret_expr, 0) = result;
1234 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1236 ret = gimple_build_return (result);
1237 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1238 gimplify_seq_add_stmt (pre_p, ret);
1240 return GS_ALL_DONE;
1243 /* Gimplify a variable-length array DECL. */
1245 static void
1246 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1248 /* This is a variable-sized decl. Simplify its size and mark it
1249 for deferred expansion. */
1250 tree t, addr, ptr_type;
1252 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1253 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1255 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1256 if (DECL_HAS_VALUE_EXPR_P (decl))
1257 return;
1259 /* All occurrences of this decl in final gimplified code will be
1260 replaced by indirection. Setting DECL_VALUE_EXPR does two
1261 things: First, it lets the rest of the gimplifier know what
1262 replacement to use. Second, it lets the debug info know
1263 where to find the value. */
1264 ptr_type = build_pointer_type (TREE_TYPE (decl));
1265 addr = create_tmp_var (ptr_type, get_name (decl));
1266 DECL_IGNORED_P (addr) = 0;
1267 t = build_fold_indirect_ref (addr);
1268 TREE_THIS_NOTRAP (t) = 1;
1269 SET_DECL_VALUE_EXPR (decl, t);
1270 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1272 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1273 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1274 size_int (DECL_ALIGN (decl)));
1275 /* The call has been built for a variable-sized object. */
1276 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1277 t = fold_convert (ptr_type, t);
1278 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1280 gimplify_and_add (t, seq_p);
1282 /* Indicate that we need to restore the stack level when the
1283 enclosing BIND_EXPR is exited. */
1284 gimplify_ctxp->save_stack = true;
1287 /* A helper function to be called via walk_tree. Mark all labels under *TP
1288 as being forced. To be called for DECL_INITIAL of static variables. */
1290 static tree
1291 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1293 if (TYPE_P (*tp))
1294 *walk_subtrees = 0;
1295 if (TREE_CODE (*tp) == LABEL_DECL)
1296 FORCED_LABEL (*tp) = 1;
1298 return NULL_TREE;
1301 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1302 and initialization explicit. */
1304 static enum gimplify_status
1305 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1307 tree stmt = *stmt_p;
1308 tree decl = DECL_EXPR_DECL (stmt);
1310 *stmt_p = NULL_TREE;
1312 if (TREE_TYPE (decl) == error_mark_node)
1313 return GS_ERROR;
1315 if ((TREE_CODE (decl) == TYPE_DECL
1316 || TREE_CODE (decl) == VAR_DECL)
1317 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1318 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1320 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1321 in case its size expressions contain problematic nodes like CALL_EXPR. */
1322 if (TREE_CODE (decl) == TYPE_DECL
1323 && DECL_ORIGINAL_TYPE (decl)
1324 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1325 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1327 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1329 tree init = DECL_INITIAL (decl);
1331 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1332 || (!TREE_STATIC (decl)
1333 && flag_stack_check == GENERIC_STACK_CHECK
1334 && compare_tree_int (DECL_SIZE_UNIT (decl),
1335 STACK_CHECK_MAX_VAR_SIZE) > 0))
1336 gimplify_vla_decl (decl, seq_p);
1338 /* Some front ends do not explicitly declare all anonymous
1339 artificial variables. We compensate here by declaring the
1340 variables, though it would be better if the front ends would
1341 explicitly declare them. */
1342 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1343 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1344 gimple_add_tmp_var (decl);
1346 if (init && init != error_mark_node)
1348 if (!TREE_STATIC (decl))
1350 DECL_INITIAL (decl) = NULL_TREE;
1351 init = build2 (INIT_EXPR, void_type_node, decl, init);
1352 gimplify_and_add (init, seq_p);
1353 ggc_free (init);
1355 else
1356 /* We must still examine initializers for static variables
1357 as they may contain a label address. */
1358 walk_tree (&init, force_labels_r, NULL, NULL);
1362 return GS_ALL_DONE;
1365 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1366 and replacing the LOOP_EXPR with goto, but if the loop contains an
1367 EXIT_EXPR, we need to append a label for it to jump to. */
1369 static enum gimplify_status
1370 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1372 tree saved_label = gimplify_ctxp->exit_label;
1373 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1375 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1377 gimplify_ctxp->exit_label = NULL_TREE;
1379 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1381 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1383 if (gimplify_ctxp->exit_label)
1384 gimplify_seq_add_stmt (pre_p,
1385 gimple_build_label (gimplify_ctxp->exit_label));
1387 gimplify_ctxp->exit_label = saved_label;
1389 *expr_p = NULL;
1390 return GS_ALL_DONE;
1393 /* Gimplify a statement list onto a sequence. These may be created either
1394 by an enlightened front-end, or by shortcut_cond_expr. */
1396 static enum gimplify_status
1397 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1399 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1401 tree_stmt_iterator i = tsi_start (*expr_p);
1403 while (!tsi_end_p (i))
1405 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1406 tsi_delink (&i);
1409 if (temp)
1411 *expr_p = temp;
1412 return GS_OK;
1415 return GS_ALL_DONE;
1419 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1420 branch to. */
1422 static enum gimplify_status
1423 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1425 tree switch_expr = *expr_p;
1426 gimple_seq switch_body_seq = NULL;
1427 enum gimplify_status ret;
1428 tree index_type = TREE_TYPE (switch_expr);
1429 if (index_type == NULL_TREE)
1430 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1432 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1433 fb_rvalue);
1434 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1435 return ret;
1437 if (SWITCH_BODY (switch_expr))
1439 vec<tree> labels;
1440 vec<tree> saved_labels;
1441 tree default_case = NULL_TREE;
1442 gimple gimple_switch;
1444 /* If someone can be bothered to fill in the labels, they can
1445 be bothered to null out the body too. */
1446 gcc_assert (!SWITCH_LABELS (switch_expr));
1448 /* Save old labels, get new ones from body, then restore the old
1449 labels. Save all the things from the switch body to append after. */
1450 saved_labels = gimplify_ctxp->case_labels;
1451 gimplify_ctxp->case_labels.create (8);
1453 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1454 labels = gimplify_ctxp->case_labels;
1455 gimplify_ctxp->case_labels = saved_labels;
1457 preprocess_case_label_vec_for_gimple (labels, index_type,
1458 &default_case);
1460 if (!default_case)
1462 gimple new_default;
1464 default_case
1465 = build_case_label (NULL_TREE, NULL_TREE,
1466 create_artificial_label (UNKNOWN_LOCATION));
1467 new_default = gimple_build_label (CASE_LABEL (default_case));
1468 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1471 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1472 default_case, labels);
1473 gimplify_seq_add_stmt (pre_p, gimple_switch);
1474 gimplify_seq_add_seq (pre_p, switch_body_seq);
1475 labels.release ();
1477 else
1478 gcc_assert (SWITCH_LABELS (switch_expr));
1480 return GS_ALL_DONE;
1483 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1485 static enum gimplify_status
1486 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1488 struct gimplify_ctx *ctxp;
1489 gimple gimple_label;
1491 /* Invalid OpenMP programs can play Duff's Device type games with
1492 #pragma omp parallel. At least in the C front end, we don't
1493 detect such invalid branches until after gimplification. */
1494 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1495 if (ctxp->case_labels.exists ())
1496 break;
1498 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1499 ctxp->case_labels.safe_push (*expr_p);
1500 gimplify_seq_add_stmt (pre_p, gimple_label);
1502 return GS_ALL_DONE;
1505 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1506 if necessary. */
1508 tree
1509 build_and_jump (tree *label_p)
1511 if (label_p == NULL)
1512 /* If there's nowhere to jump, just fall through. */
1513 return NULL_TREE;
1515 if (*label_p == NULL_TREE)
1517 tree label = create_artificial_label (UNKNOWN_LOCATION);
1518 *label_p = label;
1521 return build1 (GOTO_EXPR, void_type_node, *label_p);
1524 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1525 This also involves building a label to jump to and communicating it to
1526 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1528 static enum gimplify_status
1529 gimplify_exit_expr (tree *expr_p)
1531 tree cond = TREE_OPERAND (*expr_p, 0);
1532 tree expr;
1534 expr = build_and_jump (&gimplify_ctxp->exit_label);
1535 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1536 *expr_p = expr;
1538 return GS_OK;
1541 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1542 different from its canonical type, wrap the whole thing inside a
1543 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1544 type.
1546 The canonical type of a COMPONENT_REF is the type of the field being
1547 referenced--unless the field is a bit-field which can be read directly
1548 in a smaller mode, in which case the canonical type is the
1549 sign-appropriate type corresponding to that mode. */
1551 static void
1552 canonicalize_component_ref (tree *expr_p)
1554 tree expr = *expr_p;
1555 tree type;
1557 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1559 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1560 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1561 else
1562 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1564 /* One could argue that all the stuff below is not necessary for
1565 the non-bitfield case and declare it a FE error if type
1566 adjustment would be needed. */
1567 if (TREE_TYPE (expr) != type)
1569 #ifdef ENABLE_TYPES_CHECKING
1570 tree old_type = TREE_TYPE (expr);
1571 #endif
1572 int type_quals;
1574 /* We need to preserve qualifiers and propagate them from
1575 operand 0. */
1576 type_quals = TYPE_QUALS (type)
1577 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1578 if (TYPE_QUALS (type) != type_quals)
1579 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1581 /* Set the type of the COMPONENT_REF to the underlying type. */
1582 TREE_TYPE (expr) = type;
1584 #ifdef ENABLE_TYPES_CHECKING
1585 /* It is now a FE error, if the conversion from the canonical
1586 type to the original expression type is not useless. */
1587 gcc_assert (useless_type_conversion_p (old_type, type));
1588 #endif
1592 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1593 to foo, embed that change in the ADDR_EXPR by converting
1594 T array[U];
1595 (T *)&array
1597 &array[L]
1598 where L is the lower bound. For simplicity, only do this for constant
1599 lower bound.
1600 The constraint is that the type of &array[L] is trivially convertible
1601 to T *. */
1603 static void
1604 canonicalize_addr_expr (tree *expr_p)
1606 tree expr = *expr_p;
1607 tree addr_expr = TREE_OPERAND (expr, 0);
1608 tree datype, ddatype, pddatype;
1610 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1611 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1612 || TREE_CODE (addr_expr) != ADDR_EXPR)
1613 return;
1615 /* The addr_expr type should be a pointer to an array. */
1616 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1617 if (TREE_CODE (datype) != ARRAY_TYPE)
1618 return;
1620 /* The pointer to element type shall be trivially convertible to
1621 the expression pointer type. */
1622 ddatype = TREE_TYPE (datype);
1623 pddatype = build_pointer_type (ddatype);
1624 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1625 pddatype))
1626 return;
1628 /* The lower bound and element sizes must be constant. */
1629 if (!TYPE_SIZE_UNIT (ddatype)
1630 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1631 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1632 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1633 return;
1635 /* All checks succeeded. Build a new node to merge the cast. */
1636 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1637 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1638 NULL_TREE, NULL_TREE);
1639 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1641 /* We can have stripped a required restrict qualifier above. */
1642 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1643 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1646 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1647 underneath as appropriate. */
1649 static enum gimplify_status
1650 gimplify_conversion (tree *expr_p)
1652 location_t loc = EXPR_LOCATION (*expr_p);
1653 gcc_assert (CONVERT_EXPR_P (*expr_p));
1655 /* Then strip away all but the outermost conversion. */
1656 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1658 /* And remove the outermost conversion if it's useless. */
1659 if (tree_ssa_useless_type_conversion (*expr_p))
1660 *expr_p = TREE_OPERAND (*expr_p, 0);
1662 /* If we still have a conversion at the toplevel,
1663 then canonicalize some constructs. */
1664 if (CONVERT_EXPR_P (*expr_p))
1666 tree sub = TREE_OPERAND (*expr_p, 0);
1668 /* If a NOP conversion is changing the type of a COMPONENT_REF
1669 expression, then canonicalize its type now in order to expose more
1670 redundant conversions. */
1671 if (TREE_CODE (sub) == COMPONENT_REF)
1672 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1674 /* If a NOP conversion is changing a pointer to array of foo
1675 to a pointer to foo, embed that change in the ADDR_EXPR. */
1676 else if (TREE_CODE (sub) == ADDR_EXPR)
1677 canonicalize_addr_expr (expr_p);
1680 /* If we have a conversion to a non-register type force the
1681 use of a VIEW_CONVERT_EXPR instead. */
1682 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1683 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1684 TREE_OPERAND (*expr_p, 0));
1686 return GS_OK;
1689 /* Nonlocal VLAs seen in the current function. */
1690 static struct pointer_set_t *nonlocal_vlas;
1692 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1693 static tree nonlocal_vla_vars;
1695 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1696 DECL_VALUE_EXPR, and it's worth re-examining things. */
1698 static enum gimplify_status
1699 gimplify_var_or_parm_decl (tree *expr_p)
1701 tree decl = *expr_p;
1703 /* ??? If this is a local variable, and it has not been seen in any
1704 outer BIND_EXPR, then it's probably the result of a duplicate
1705 declaration, for which we've already issued an error. It would
1706 be really nice if the front end wouldn't leak these at all.
1707 Currently the only known culprit is C++ destructors, as seen
1708 in g++.old-deja/g++.jason/binding.C. */
1709 if (TREE_CODE (decl) == VAR_DECL
1710 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1711 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1712 && decl_function_context (decl) == current_function_decl)
1714 gcc_assert (seen_error ());
1715 return GS_ERROR;
1718 /* When within an OpenMP context, notice uses of variables. */
1719 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1720 return GS_ALL_DONE;
1722 /* If the decl is an alias for another expression, substitute it now. */
1723 if (DECL_HAS_VALUE_EXPR_P (decl))
1725 tree value_expr = DECL_VALUE_EXPR (decl);
1727 /* For referenced nonlocal VLAs add a decl for debugging purposes
1728 to the current function. */
1729 if (TREE_CODE (decl) == VAR_DECL
1730 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1731 && nonlocal_vlas != NULL
1732 && TREE_CODE (value_expr) == INDIRECT_REF
1733 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1734 && decl_function_context (decl) != current_function_decl)
1736 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1737 while (ctx
1738 && (ctx->region_type == ORT_WORKSHARE
1739 || ctx->region_type == ORT_SIMD))
1740 ctx = ctx->outer_context;
1741 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
1743 tree copy = copy_node (decl);
1745 lang_hooks.dup_lang_specific_decl (copy);
1746 SET_DECL_RTL (copy, 0);
1747 TREE_USED (copy) = 1;
1748 DECL_CHAIN (copy) = nonlocal_vla_vars;
1749 nonlocal_vla_vars = copy;
1750 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1751 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1755 *expr_p = unshare_expr (value_expr);
1756 return GS_OK;
1759 return GS_ALL_DONE;
1762 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1764 static void
1765 recalculate_side_effects (tree t)
1767 enum tree_code code = TREE_CODE (t);
1768 int len = TREE_OPERAND_LENGTH (t);
1769 int i;
1771 switch (TREE_CODE_CLASS (code))
1773 case tcc_expression:
1774 switch (code)
1776 case INIT_EXPR:
1777 case MODIFY_EXPR:
1778 case VA_ARG_EXPR:
1779 case PREDECREMENT_EXPR:
1780 case PREINCREMENT_EXPR:
1781 case POSTDECREMENT_EXPR:
1782 case POSTINCREMENT_EXPR:
1783 /* All of these have side-effects, no matter what their
1784 operands are. */
1785 return;
1787 default:
1788 break;
1790 /* Fall through. */
1792 case tcc_comparison: /* a comparison expression */
1793 case tcc_unary: /* a unary arithmetic expression */
1794 case tcc_binary: /* a binary arithmetic expression */
1795 case tcc_reference: /* a reference */
1796 case tcc_vl_exp: /* a function call */
1797 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1798 for (i = 0; i < len; ++i)
1800 tree op = TREE_OPERAND (t, i);
1801 if (op && TREE_SIDE_EFFECTS (op))
1802 TREE_SIDE_EFFECTS (t) = 1;
1804 break;
1806 case tcc_constant:
1807 /* No side-effects. */
1808 return;
1810 default:
1811 gcc_unreachable ();
1815 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1816 node *EXPR_P.
1818 compound_lval
1819 : min_lval '[' val ']'
1820 | min_lval '.' ID
1821 | compound_lval '[' val ']'
1822 | compound_lval '.' ID
1824 This is not part of the original SIMPLE definition, which separates
1825 array and member references, but it seems reasonable to handle them
1826 together. Also, this way we don't run into problems with union
1827 aliasing; gcc requires that for accesses through a union to alias, the
1828 union reference must be explicit, which was not always the case when we
1829 were splitting up array and member refs.
1831 PRE_P points to the sequence where side effects that must happen before
1832 *EXPR_P should be stored.
1834 POST_P points to the sequence where side effects that must happen after
1835 *EXPR_P should be stored. */
1837 static enum gimplify_status
1838 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1839 fallback_t fallback)
1841 tree *p;
1842 enum gimplify_status ret = GS_ALL_DONE, tret;
1843 int i;
1844 location_t loc = EXPR_LOCATION (*expr_p);
1845 tree expr = *expr_p;
1847 /* Create a stack of the subexpressions so later we can walk them in
1848 order from inner to outer. */
1849 stack_vec<tree, 10> expr_stack;
1851 /* We can handle anything that get_inner_reference can deal with. */
1852 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1854 restart:
1855 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1856 if (TREE_CODE (*p) == INDIRECT_REF)
1857 *p = fold_indirect_ref_loc (loc, *p);
1859 if (handled_component_p (*p))
1861 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1862 additional COMPONENT_REFs. */
1863 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1864 && gimplify_var_or_parm_decl (p) == GS_OK)
1865 goto restart;
1866 else
1867 break;
1869 expr_stack.safe_push (*p);
1872 gcc_assert (expr_stack.length ());
1874 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1875 walked through and P points to the innermost expression.
1877 Java requires that we elaborated nodes in source order. That
1878 means we must gimplify the inner expression followed by each of
1879 the indices, in order. But we can't gimplify the inner
1880 expression until we deal with any variable bounds, sizes, or
1881 positions in order to deal with PLACEHOLDER_EXPRs.
1883 So we do this in three steps. First we deal with the annotations
1884 for any variables in the components, then we gimplify the base,
1885 then we gimplify any indices, from left to right. */
1886 for (i = expr_stack.length () - 1; i >= 0; i--)
1888 tree t = expr_stack[i];
1890 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1892 /* Gimplify the low bound and element type size and put them into
1893 the ARRAY_REF. If these values are set, they have already been
1894 gimplified. */
1895 if (TREE_OPERAND (t, 2) == NULL_TREE)
1897 tree low = unshare_expr (array_ref_low_bound (t));
1898 if (!is_gimple_min_invariant (low))
1900 TREE_OPERAND (t, 2) = low;
1901 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1902 post_p, is_gimple_reg,
1903 fb_rvalue);
1904 ret = MIN (ret, tret);
1907 else
1909 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1910 is_gimple_reg, fb_rvalue);
1911 ret = MIN (ret, tret);
1914 if (TREE_OPERAND (t, 3) == NULL_TREE)
1916 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1917 tree elmt_size = unshare_expr (array_ref_element_size (t));
1918 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1920 /* Divide the element size by the alignment of the element
1921 type (above). */
1922 elmt_size
1923 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1925 if (!is_gimple_min_invariant (elmt_size))
1927 TREE_OPERAND (t, 3) = elmt_size;
1928 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1929 post_p, is_gimple_reg,
1930 fb_rvalue);
1931 ret = MIN (ret, tret);
1934 else
1936 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1937 is_gimple_reg, fb_rvalue);
1938 ret = MIN (ret, tret);
1941 else if (TREE_CODE (t) == COMPONENT_REF)
1943 /* Set the field offset into T and gimplify it. */
1944 if (TREE_OPERAND (t, 2) == NULL_TREE)
1946 tree offset = unshare_expr (component_ref_field_offset (t));
1947 tree field = TREE_OPERAND (t, 1);
1948 tree factor
1949 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1951 /* Divide the offset by its alignment. */
1952 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
1954 if (!is_gimple_min_invariant (offset))
1956 TREE_OPERAND (t, 2) = offset;
1957 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1958 post_p, is_gimple_reg,
1959 fb_rvalue);
1960 ret = MIN (ret, tret);
1963 else
1965 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1966 is_gimple_reg, fb_rvalue);
1967 ret = MIN (ret, tret);
1972 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
1973 so as to match the min_lval predicate. Failure to do so may result
1974 in the creation of large aggregate temporaries. */
1975 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1976 fallback | fb_lvalue);
1977 ret = MIN (ret, tret);
1979 /* And finally, the indices and operands of ARRAY_REF. During this
1980 loop we also remove any useless conversions. */
1981 for (; expr_stack.length () > 0; )
1983 tree t = expr_stack.pop ();
1985 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1987 /* Gimplify the dimension. */
1988 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1990 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1991 is_gimple_val, fb_rvalue);
1992 ret = MIN (ret, tret);
1996 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
1998 /* The innermost expression P may have originally had
1999 TREE_SIDE_EFFECTS set which would have caused all the outer
2000 expressions in *EXPR_P leading to P to also have had
2001 TREE_SIDE_EFFECTS set. */
2002 recalculate_side_effects (t);
2005 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2006 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2008 canonicalize_component_ref (expr_p);
2011 expr_stack.release ();
2013 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2015 return ret;
2018 /* Gimplify the self modifying expression pointed to by EXPR_P
2019 (++, --, +=, -=).
2021 PRE_P points to the list where side effects that must happen before
2022 *EXPR_P should be stored.
2024 POST_P points to the list where side effects that must happen after
2025 *EXPR_P should be stored.
2027 WANT_VALUE is nonzero iff we want to use the value of this expression
2028 in another expression.
2030 ARITH_TYPE is the type the computation should be performed in. */
2032 enum gimplify_status
2033 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2034 bool want_value, tree arith_type)
2036 enum tree_code code;
2037 tree lhs, lvalue, rhs, t1;
2038 gimple_seq post = NULL, *orig_post_p = post_p;
2039 bool postfix;
2040 enum tree_code arith_code;
2041 enum gimplify_status ret;
2042 location_t loc = EXPR_LOCATION (*expr_p);
2044 code = TREE_CODE (*expr_p);
2046 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2047 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2049 /* Prefix or postfix? */
2050 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2051 /* Faster to treat as prefix if result is not used. */
2052 postfix = want_value;
2053 else
2054 postfix = false;
2056 /* For postfix, make sure the inner expression's post side effects
2057 are executed after side effects from this expression. */
2058 if (postfix)
2059 post_p = &post;
2061 /* Add or subtract? */
2062 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2063 arith_code = PLUS_EXPR;
2064 else
2065 arith_code = MINUS_EXPR;
2067 /* Gimplify the LHS into a GIMPLE lvalue. */
2068 lvalue = TREE_OPERAND (*expr_p, 0);
2069 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2070 if (ret == GS_ERROR)
2071 return ret;
2073 /* Extract the operands to the arithmetic operation. */
2074 lhs = lvalue;
2075 rhs = TREE_OPERAND (*expr_p, 1);
2077 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2078 that as the result value and in the postqueue operation. */
2079 if (postfix)
2081 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2082 if (ret == GS_ERROR)
2083 return ret;
2085 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2088 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2089 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2091 rhs = convert_to_ptrofftype_loc (loc, rhs);
2092 if (arith_code == MINUS_EXPR)
2093 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2094 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2096 else
2097 t1 = fold_convert (TREE_TYPE (*expr_p),
2098 fold_build2 (arith_code, arith_type,
2099 fold_convert (arith_type, lhs),
2100 fold_convert (arith_type, rhs)));
2102 if (postfix)
2104 gimplify_assign (lvalue, t1, pre_p);
2105 gimplify_seq_add_seq (orig_post_p, post);
2106 *expr_p = lhs;
2107 return GS_ALL_DONE;
2109 else
2111 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2112 return GS_OK;
2116 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2118 static void
2119 maybe_with_size_expr (tree *expr_p)
2121 tree expr = *expr_p;
2122 tree type = TREE_TYPE (expr);
2123 tree size;
2125 /* If we've already wrapped this or the type is error_mark_node, we can't do
2126 anything. */
2127 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2128 || type == error_mark_node)
2129 return;
2131 /* If the size isn't known or is a constant, we have nothing to do. */
2132 size = TYPE_SIZE_UNIT (type);
2133 if (!size || TREE_CODE (size) == INTEGER_CST)
2134 return;
2136 /* Otherwise, make a WITH_SIZE_EXPR. */
2137 size = unshare_expr (size);
2138 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2139 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2142 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2143 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2144 the CALL_EXPR. */
2146 static enum gimplify_status
2147 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2149 bool (*test) (tree);
2150 fallback_t fb;
2152 /* In general, we allow lvalues for function arguments to avoid
2153 extra overhead of copying large aggregates out of even larger
2154 aggregates into temporaries only to copy the temporaries to
2155 the argument list. Make optimizers happy by pulling out to
2156 temporaries those types that fit in registers. */
2157 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2158 test = is_gimple_val, fb = fb_rvalue;
2159 else
2161 test = is_gimple_lvalue, fb = fb_either;
2162 /* Also strip a TARGET_EXPR that would force an extra copy. */
2163 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2165 tree init = TARGET_EXPR_INITIAL (*arg_p);
2166 if (init
2167 && !VOID_TYPE_P (TREE_TYPE (init)))
2168 *arg_p = init;
2172 /* If this is a variable sized type, we must remember the size. */
2173 maybe_with_size_expr (arg_p);
2175 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2176 /* Make sure arguments have the same location as the function call
2177 itself. */
2178 protected_set_expr_location (*arg_p, call_location);
2180 /* There is a sequence point before a function call. Side effects in
2181 the argument list must occur before the actual call. So, when
2182 gimplifying arguments, force gimplify_expr to use an internal
2183 post queue which is then appended to the end of PRE_P. */
2184 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2187 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2188 WANT_VALUE is true if the result of the call is desired. */
2190 static enum gimplify_status
2191 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2193 tree fndecl, parms, p, fnptrtype;
2194 enum gimplify_status ret;
2195 int i, nargs;
2196 gimple call;
2197 bool builtin_va_start_p = FALSE;
2198 location_t loc = EXPR_LOCATION (*expr_p);
2200 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2202 /* For reliable diagnostics during inlining, it is necessary that
2203 every call_expr be annotated with file and line. */
2204 if (! EXPR_HAS_LOCATION (*expr_p))
2205 SET_EXPR_LOCATION (*expr_p, input_location);
2207 /* This may be a call to a builtin function.
2209 Builtin function calls may be transformed into different
2210 (and more efficient) builtin function calls under certain
2211 circumstances. Unfortunately, gimplification can muck things
2212 up enough that the builtin expanders are not aware that certain
2213 transformations are still valid.
2215 So we attempt transformation/gimplification of the call before
2216 we gimplify the CALL_EXPR. At this time we do not manage to
2217 transform all calls in the same manner as the expanders do, but
2218 we do transform most of them. */
2219 fndecl = get_callee_fndecl (*expr_p);
2220 if (fndecl
2221 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2222 switch (DECL_FUNCTION_CODE (fndecl))
2224 case BUILT_IN_VA_START:
2226 builtin_va_start_p = TRUE;
2227 if (call_expr_nargs (*expr_p) < 2)
2229 error ("too few arguments to function %<va_start%>");
2230 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2231 return GS_OK;
2234 if (fold_builtin_next_arg (*expr_p, true))
2236 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2237 return GS_OK;
2239 break;
2241 case BUILT_IN_LINE:
2243 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2244 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2245 return GS_OK;
2247 case BUILT_IN_FILE:
2249 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2250 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2251 return GS_OK;
2253 case BUILT_IN_FUNCTION:
2255 const char *function;
2256 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2257 *expr_p = build_string_literal (strlen (function) + 1, function);
2258 return GS_OK;
2260 default:
2263 if (fndecl && DECL_BUILT_IN (fndecl))
2265 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2266 if (new_tree && new_tree != *expr_p)
2268 /* There was a transformation of this call which computes the
2269 same value, but in a more efficient way. Return and try
2270 again. */
2271 *expr_p = new_tree;
2272 return GS_OK;
2276 /* Remember the original function pointer type. */
2277 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2279 /* There is a sequence point before the call, so any side effects in
2280 the calling expression must occur before the actual call. Force
2281 gimplify_expr to use an internal post queue. */
2282 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2283 is_gimple_call_addr, fb_rvalue);
2285 nargs = call_expr_nargs (*expr_p);
2287 /* Get argument types for verification. */
2288 fndecl = get_callee_fndecl (*expr_p);
2289 parms = NULL_TREE;
2290 if (fndecl)
2291 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2292 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2293 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2295 if (fndecl && DECL_ARGUMENTS (fndecl))
2296 p = DECL_ARGUMENTS (fndecl);
2297 else if (parms)
2298 p = parms;
2299 else
2300 p = NULL_TREE;
2301 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2304 /* If the last argument is __builtin_va_arg_pack () and it is not
2305 passed as a named argument, decrease the number of CALL_EXPR
2306 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2307 if (!p
2308 && i < nargs
2309 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2311 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2312 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2314 if (last_arg_fndecl
2315 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2316 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2317 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2319 tree call = *expr_p;
2321 --nargs;
2322 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2323 CALL_EXPR_FN (call),
2324 nargs, CALL_EXPR_ARGP (call));
2326 /* Copy all CALL_EXPR flags, location and block, except
2327 CALL_EXPR_VA_ARG_PACK flag. */
2328 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2329 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2330 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2331 = CALL_EXPR_RETURN_SLOT_OPT (call);
2332 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2333 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2335 /* Set CALL_EXPR_VA_ARG_PACK. */
2336 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2340 /* Finally, gimplify the function arguments. */
2341 if (nargs > 0)
2343 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2344 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2345 PUSH_ARGS_REVERSED ? i-- : i++)
2347 enum gimplify_status t;
2349 /* Avoid gimplifying the second argument to va_start, which needs to
2350 be the plain PARM_DECL. */
2351 if ((i != 1) || !builtin_va_start_p)
2353 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2354 EXPR_LOCATION (*expr_p));
2356 if (t == GS_ERROR)
2357 ret = GS_ERROR;
2362 /* Verify the function result. */
2363 if (want_value && fndecl
2364 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2366 error_at (loc, "using result of function returning %<void%>");
2367 ret = GS_ERROR;
2370 /* Try this again in case gimplification exposed something. */
2371 if (ret != GS_ERROR)
2373 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2375 if (new_tree && new_tree != *expr_p)
2377 /* There was a transformation of this call which computes the
2378 same value, but in a more efficient way. Return and try
2379 again. */
2380 *expr_p = new_tree;
2381 return GS_OK;
2384 else
2386 *expr_p = error_mark_node;
2387 return GS_ERROR;
2390 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2391 decl. This allows us to eliminate redundant or useless
2392 calls to "const" functions. */
2393 if (TREE_CODE (*expr_p) == CALL_EXPR)
2395 int flags = call_expr_flags (*expr_p);
2396 if (flags & (ECF_CONST | ECF_PURE)
2397 /* An infinite loop is considered a side effect. */
2398 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2399 TREE_SIDE_EFFECTS (*expr_p) = 0;
2402 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2403 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2404 form and delegate the creation of a GIMPLE_CALL to
2405 gimplify_modify_expr. This is always possible because when
2406 WANT_VALUE is true, the caller wants the result of this call into
2407 a temporary, which means that we will emit an INIT_EXPR in
2408 internal_get_tmp_var which will then be handled by
2409 gimplify_modify_expr. */
2410 if (!want_value)
2412 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2413 have to do is replicate it as a GIMPLE_CALL tuple. */
2414 gimple_stmt_iterator gsi;
2415 call = gimple_build_call_from_tree (*expr_p);
2416 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2417 notice_special_calls (call);
2418 gimplify_seq_add_stmt (pre_p, call);
2419 gsi = gsi_last (*pre_p);
2420 /* Don't fold stmts inside of target construct. We'll do it
2421 during omplower pass instead. */
2422 struct gimplify_omp_ctx *ctx;
2423 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2424 if (ctx->region_type == ORT_TARGET)
2425 break;
2426 if (ctx == NULL)
2427 fold_stmt (&gsi);
2428 *expr_p = NULL_TREE;
2430 else
2431 /* Remember the original function type. */
2432 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2433 CALL_EXPR_FN (*expr_p));
2435 return ret;
2438 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2439 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2441 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2442 condition is true or false, respectively. If null, we should generate
2443 our own to skip over the evaluation of this specific expression.
2445 LOCUS is the source location of the COND_EXPR.
2447 This function is the tree equivalent of do_jump.
2449 shortcut_cond_r should only be called by shortcut_cond_expr. */
2451 static tree
2452 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2453 location_t locus)
2455 tree local_label = NULL_TREE;
2456 tree t, expr = NULL;
2458 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2459 retain the shortcut semantics. Just insert the gotos here;
2460 shortcut_cond_expr will append the real blocks later. */
2461 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2463 location_t new_locus;
2465 /* Turn if (a && b) into
2467 if (a); else goto no;
2468 if (b) goto yes; else goto no;
2469 (no:) */
2471 if (false_label_p == NULL)
2472 false_label_p = &local_label;
2474 /* Keep the original source location on the first 'if'. */
2475 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2476 append_to_statement_list (t, &expr);
2478 /* Set the source location of the && on the second 'if'. */
2479 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2480 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2481 new_locus);
2482 append_to_statement_list (t, &expr);
2484 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2486 location_t new_locus;
2488 /* Turn if (a || b) into
2490 if (a) goto yes;
2491 if (b) goto yes; else goto no;
2492 (yes:) */
2494 if (true_label_p == NULL)
2495 true_label_p = &local_label;
2497 /* Keep the original source location on the first 'if'. */
2498 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2499 append_to_statement_list (t, &expr);
2501 /* Set the source location of the || on the second 'if'. */
2502 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2503 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2504 new_locus);
2505 append_to_statement_list (t, &expr);
2507 else if (TREE_CODE (pred) == COND_EXPR
2508 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2509 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2511 location_t new_locus;
2513 /* As long as we're messing with gotos, turn if (a ? b : c) into
2514 if (a)
2515 if (b) goto yes; else goto no;
2516 else
2517 if (c) goto yes; else goto no;
2519 Don't do this if one of the arms has void type, which can happen
2520 in C++ when the arm is throw. */
2522 /* Keep the original source location on the first 'if'. Set the source
2523 location of the ? on the second 'if'. */
2524 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2525 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2526 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2527 false_label_p, locus),
2528 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2529 false_label_p, new_locus));
2531 else
2533 expr = build3 (COND_EXPR, void_type_node, pred,
2534 build_and_jump (true_label_p),
2535 build_and_jump (false_label_p));
2536 SET_EXPR_LOCATION (expr, locus);
2539 if (local_label)
2541 t = build1 (LABEL_EXPR, void_type_node, local_label);
2542 append_to_statement_list (t, &expr);
2545 return expr;
2548 /* Given a conditional expression EXPR with short-circuit boolean
2549 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2550 predicate apart into the equivalent sequence of conditionals. */
2552 static tree
2553 shortcut_cond_expr (tree expr)
2555 tree pred = TREE_OPERAND (expr, 0);
2556 tree then_ = TREE_OPERAND (expr, 1);
2557 tree else_ = TREE_OPERAND (expr, 2);
2558 tree true_label, false_label, end_label, t;
2559 tree *true_label_p;
2560 tree *false_label_p;
2561 bool emit_end, emit_false, jump_over_else;
2562 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2563 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2565 /* First do simple transformations. */
2566 if (!else_se)
2568 /* If there is no 'else', turn
2569 if (a && b) then c
2570 into
2571 if (a) if (b) then c. */
2572 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2574 /* Keep the original source location on the first 'if'. */
2575 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2576 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2577 /* Set the source location of the && on the second 'if'. */
2578 if (EXPR_HAS_LOCATION (pred))
2579 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2580 then_ = shortcut_cond_expr (expr);
2581 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2582 pred = TREE_OPERAND (pred, 0);
2583 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2584 SET_EXPR_LOCATION (expr, locus);
2588 if (!then_se)
2590 /* If there is no 'then', turn
2591 if (a || b); else d
2592 into
2593 if (a); else if (b); else d. */
2594 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2596 /* Keep the original source location on the first 'if'. */
2597 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2598 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2599 /* Set the source location of the || on the second 'if'. */
2600 if (EXPR_HAS_LOCATION (pred))
2601 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2602 else_ = shortcut_cond_expr (expr);
2603 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2604 pred = TREE_OPERAND (pred, 0);
2605 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2606 SET_EXPR_LOCATION (expr, locus);
2610 /* If we're done, great. */
2611 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2612 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2613 return expr;
2615 /* Otherwise we need to mess with gotos. Change
2616 if (a) c; else d;
2618 if (a); else goto no;
2619 c; goto end;
2620 no: d; end:
2621 and recursively gimplify the condition. */
2623 true_label = false_label = end_label = NULL_TREE;
2625 /* If our arms just jump somewhere, hijack those labels so we don't
2626 generate jumps to jumps. */
2628 if (then_
2629 && TREE_CODE (then_) == GOTO_EXPR
2630 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2632 true_label = GOTO_DESTINATION (then_);
2633 then_ = NULL;
2634 then_se = false;
2637 if (else_
2638 && TREE_CODE (else_) == GOTO_EXPR
2639 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2641 false_label = GOTO_DESTINATION (else_);
2642 else_ = NULL;
2643 else_se = false;
2646 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2647 if (true_label)
2648 true_label_p = &true_label;
2649 else
2650 true_label_p = NULL;
2652 /* The 'else' branch also needs a label if it contains interesting code. */
2653 if (false_label || else_se)
2654 false_label_p = &false_label;
2655 else
2656 false_label_p = NULL;
2658 /* If there was nothing else in our arms, just forward the label(s). */
2659 if (!then_se && !else_se)
2660 return shortcut_cond_r (pred, true_label_p, false_label_p,
2661 EXPR_LOC_OR_LOC (expr, input_location));
2663 /* If our last subexpression already has a terminal label, reuse it. */
2664 if (else_se)
2665 t = expr_last (else_);
2666 else if (then_se)
2667 t = expr_last (then_);
2668 else
2669 t = NULL;
2670 if (t && TREE_CODE (t) == LABEL_EXPR)
2671 end_label = LABEL_EXPR_LABEL (t);
2673 /* If we don't care about jumping to the 'else' branch, jump to the end
2674 if the condition is false. */
2675 if (!false_label_p)
2676 false_label_p = &end_label;
2678 /* We only want to emit these labels if we aren't hijacking them. */
2679 emit_end = (end_label == NULL_TREE);
2680 emit_false = (false_label == NULL_TREE);
2682 /* We only emit the jump over the else clause if we have to--if the
2683 then clause may fall through. Otherwise we can wind up with a
2684 useless jump and a useless label at the end of gimplified code,
2685 which will cause us to think that this conditional as a whole
2686 falls through even if it doesn't. If we then inline a function
2687 which ends with such a condition, that can cause us to issue an
2688 inappropriate warning about control reaching the end of a
2689 non-void function. */
2690 jump_over_else = block_may_fallthru (then_);
2692 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2693 EXPR_LOC_OR_LOC (expr, input_location));
2695 expr = NULL;
2696 append_to_statement_list (pred, &expr);
2698 append_to_statement_list (then_, &expr);
2699 if (else_se)
2701 if (jump_over_else)
2703 tree last = expr_last (expr);
2704 t = build_and_jump (&end_label);
2705 if (EXPR_HAS_LOCATION (last))
2706 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2707 append_to_statement_list (t, &expr);
2709 if (emit_false)
2711 t = build1 (LABEL_EXPR, void_type_node, false_label);
2712 append_to_statement_list (t, &expr);
2714 append_to_statement_list (else_, &expr);
2716 if (emit_end && end_label)
2718 t = build1 (LABEL_EXPR, void_type_node, end_label);
2719 append_to_statement_list (t, &expr);
2722 return expr;
2725 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2727 tree
2728 gimple_boolify (tree expr)
2730 tree type = TREE_TYPE (expr);
2731 location_t loc = EXPR_LOCATION (expr);
2733 if (TREE_CODE (expr) == NE_EXPR
2734 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2735 && integer_zerop (TREE_OPERAND (expr, 1)))
2737 tree call = TREE_OPERAND (expr, 0);
2738 tree fn = get_callee_fndecl (call);
2740 /* For __builtin_expect ((long) (x), y) recurse into x as well
2741 if x is truth_value_p. */
2742 if (fn
2743 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2744 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2745 && call_expr_nargs (call) == 2)
2747 tree arg = CALL_EXPR_ARG (call, 0);
2748 if (arg)
2750 if (TREE_CODE (arg) == NOP_EXPR
2751 && TREE_TYPE (arg) == TREE_TYPE (call))
2752 arg = TREE_OPERAND (arg, 0);
2753 if (truth_value_p (TREE_CODE (arg)))
2755 arg = gimple_boolify (arg);
2756 CALL_EXPR_ARG (call, 0)
2757 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2763 switch (TREE_CODE (expr))
2765 case TRUTH_AND_EXPR:
2766 case TRUTH_OR_EXPR:
2767 case TRUTH_XOR_EXPR:
2768 case TRUTH_ANDIF_EXPR:
2769 case TRUTH_ORIF_EXPR:
2770 /* Also boolify the arguments of truth exprs. */
2771 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2772 /* FALLTHRU */
2774 case TRUTH_NOT_EXPR:
2775 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2777 /* These expressions always produce boolean results. */
2778 if (TREE_CODE (type) != BOOLEAN_TYPE)
2779 TREE_TYPE (expr) = boolean_type_node;
2780 return expr;
2782 case ANNOTATE_EXPR:
2783 if ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1))
2784 == annot_expr_ivdep_kind)
2786 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2787 if (TREE_CODE (type) != BOOLEAN_TYPE)
2788 TREE_TYPE (expr) = boolean_type_node;
2789 return expr;
2791 /* FALLTHRU */
2793 default:
2794 if (COMPARISON_CLASS_P (expr))
2796 /* There expressions always prduce boolean results. */
2797 if (TREE_CODE (type) != BOOLEAN_TYPE)
2798 TREE_TYPE (expr) = boolean_type_node;
2799 return expr;
2801 /* Other expressions that get here must have boolean values, but
2802 might need to be converted to the appropriate mode. */
2803 if (TREE_CODE (type) == BOOLEAN_TYPE)
2804 return expr;
2805 return fold_convert_loc (loc, boolean_type_node, expr);
2809 /* Given a conditional expression *EXPR_P without side effects, gimplify
2810 its operands. New statements are inserted to PRE_P. */
2812 static enum gimplify_status
2813 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2815 tree expr = *expr_p, cond;
2816 enum gimplify_status ret, tret;
2817 enum tree_code code;
2819 cond = gimple_boolify (COND_EXPR_COND (expr));
2821 /* We need to handle && and || specially, as their gimplification
2822 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2823 code = TREE_CODE (cond);
2824 if (code == TRUTH_ANDIF_EXPR)
2825 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2826 else if (code == TRUTH_ORIF_EXPR)
2827 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2828 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2829 COND_EXPR_COND (*expr_p) = cond;
2831 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2832 is_gimple_val, fb_rvalue);
2833 ret = MIN (ret, tret);
2834 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2835 is_gimple_val, fb_rvalue);
2837 return MIN (ret, tret);
2840 /* Return true if evaluating EXPR could trap.
2841 EXPR is GENERIC, while tree_could_trap_p can be called
2842 only on GIMPLE. */
2844 static bool
2845 generic_expr_could_trap_p (tree expr)
2847 unsigned i, n;
2849 if (!expr || is_gimple_val (expr))
2850 return false;
2852 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2853 return true;
2855 n = TREE_OPERAND_LENGTH (expr);
2856 for (i = 0; i < n; i++)
2857 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2858 return true;
2860 return false;
2863 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2864 into
2866 if (p) if (p)
2867 t1 = a; a;
2868 else or else
2869 t1 = b; b;
2872 The second form is used when *EXPR_P is of type void.
2874 PRE_P points to the list where side effects that must happen before
2875 *EXPR_P should be stored. */
2877 static enum gimplify_status
2878 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2880 tree expr = *expr_p;
2881 tree type = TREE_TYPE (expr);
2882 location_t loc = EXPR_LOCATION (expr);
2883 tree tmp, arm1, arm2;
2884 enum gimplify_status ret;
2885 tree label_true, label_false, label_cont;
2886 bool have_then_clause_p, have_else_clause_p;
2887 gimple gimple_cond;
2888 enum tree_code pred_code;
2889 gimple_seq seq = NULL;
2891 /* If this COND_EXPR has a value, copy the values into a temporary within
2892 the arms. */
2893 if (!VOID_TYPE_P (type))
2895 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2896 tree result;
2898 /* If either an rvalue is ok or we do not require an lvalue, create the
2899 temporary. But we cannot do that if the type is addressable. */
2900 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
2901 && !TREE_ADDRESSABLE (type))
2903 if (gimplify_ctxp->allow_rhs_cond_expr
2904 /* If either branch has side effects or could trap, it can't be
2905 evaluated unconditionally. */
2906 && !TREE_SIDE_EFFECTS (then_)
2907 && !generic_expr_could_trap_p (then_)
2908 && !TREE_SIDE_EFFECTS (else_)
2909 && !generic_expr_could_trap_p (else_))
2910 return gimplify_pure_cond_expr (expr_p, pre_p);
2912 tmp = create_tmp_var (type, "iftmp");
2913 result = tmp;
2916 /* Otherwise, only create and copy references to the values. */
2917 else
2919 type = build_pointer_type (type);
2921 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2922 then_ = build_fold_addr_expr_loc (loc, then_);
2924 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2925 else_ = build_fold_addr_expr_loc (loc, else_);
2927 expr
2928 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
2930 tmp = create_tmp_var (type, "iftmp");
2931 result = build_simple_mem_ref_loc (loc, tmp);
2934 /* Build the new then clause, `tmp = then_;'. But don't build the
2935 assignment if the value is void; in C++ it can be if it's a throw. */
2936 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2937 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
2939 /* Similarly, build the new else clause, `tmp = else_;'. */
2940 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2941 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
2943 TREE_TYPE (expr) = void_type_node;
2944 recalculate_side_effects (expr);
2946 /* Move the COND_EXPR to the prequeue. */
2947 gimplify_stmt (&expr, pre_p);
2949 *expr_p = result;
2950 return GS_ALL_DONE;
2953 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
2954 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
2955 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
2956 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
2958 /* Make sure the condition has BOOLEAN_TYPE. */
2959 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2961 /* Break apart && and || conditions. */
2962 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2963 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2965 expr = shortcut_cond_expr (expr);
2967 if (expr != *expr_p)
2969 *expr_p = expr;
2971 /* We can't rely on gimplify_expr to re-gimplify the expanded
2972 form properly, as cleanups might cause the target labels to be
2973 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2974 set up a conditional context. */
2975 gimple_push_condition ();
2976 gimplify_stmt (expr_p, &seq);
2977 gimple_pop_condition (pre_p);
2978 gimple_seq_add_seq (pre_p, seq);
2980 return GS_ALL_DONE;
2984 /* Now do the normal gimplification. */
2986 /* Gimplify condition. */
2987 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
2988 fb_rvalue);
2989 if (ret == GS_ERROR)
2990 return GS_ERROR;
2991 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
2993 gimple_push_condition ();
2995 have_then_clause_p = have_else_clause_p = false;
2996 if (TREE_OPERAND (expr, 1) != NULL
2997 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
2998 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
2999 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3000 == current_function_decl)
3001 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3002 have different locations, otherwise we end up with incorrect
3003 location information on the branches. */
3004 && (optimize
3005 || !EXPR_HAS_LOCATION (expr)
3006 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3007 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3009 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3010 have_then_clause_p = true;
3012 else
3013 label_true = create_artificial_label (UNKNOWN_LOCATION);
3014 if (TREE_OPERAND (expr, 2) != NULL
3015 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3016 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3017 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3018 == current_function_decl)
3019 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3020 have different locations, otherwise we end up with incorrect
3021 location information on the branches. */
3022 && (optimize
3023 || !EXPR_HAS_LOCATION (expr)
3024 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3025 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3027 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3028 have_else_clause_p = true;
3030 else
3031 label_false = create_artificial_label (UNKNOWN_LOCATION);
3033 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3034 &arm2);
3036 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3037 label_false);
3039 gimplify_seq_add_stmt (&seq, gimple_cond);
3040 label_cont = NULL_TREE;
3041 if (!have_then_clause_p)
3043 /* For if (...) {} else { code; } put label_true after
3044 the else block. */
3045 if (TREE_OPERAND (expr, 1) == NULL_TREE
3046 && !have_else_clause_p
3047 && TREE_OPERAND (expr, 2) != NULL_TREE)
3048 label_cont = label_true;
3049 else
3051 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3052 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3053 /* For if (...) { code; } else {} or
3054 if (...) { code; } else goto label; or
3055 if (...) { code; return; } else { ... }
3056 label_cont isn't needed. */
3057 if (!have_else_clause_p
3058 && TREE_OPERAND (expr, 2) != NULL_TREE
3059 && gimple_seq_may_fallthru (seq))
3061 gimple g;
3062 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3064 g = gimple_build_goto (label_cont);
3066 /* GIMPLE_COND's are very low level; they have embedded
3067 gotos. This particular embedded goto should not be marked
3068 with the location of the original COND_EXPR, as it would
3069 correspond to the COND_EXPR's condition, not the ELSE or the
3070 THEN arms. To avoid marking it with the wrong location, flag
3071 it as "no location". */
3072 gimple_set_do_not_emit_location (g);
3074 gimplify_seq_add_stmt (&seq, g);
3078 if (!have_else_clause_p)
3080 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3081 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3083 if (label_cont)
3084 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3086 gimple_pop_condition (pre_p);
3087 gimple_seq_add_seq (pre_p, seq);
3089 if (ret == GS_ERROR)
3090 ; /* Do nothing. */
3091 else if (have_then_clause_p || have_else_clause_p)
3092 ret = GS_ALL_DONE;
3093 else
3095 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3096 expr = TREE_OPERAND (expr, 0);
3097 gimplify_stmt (&expr, pre_p);
3100 *expr_p = NULL;
3101 return ret;
3104 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3105 to be marked addressable.
3107 We cannot rely on such an expression being directly markable if a temporary
3108 has been created by the gimplification. In this case, we create another
3109 temporary and initialize it with a copy, which will become a store after we
3110 mark it addressable. This can happen if the front-end passed us something
3111 that it could not mark addressable yet, like a Fortran pass-by-reference
3112 parameter (int) floatvar. */
3114 static void
3115 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3117 while (handled_component_p (*expr_p))
3118 expr_p = &TREE_OPERAND (*expr_p, 0);
3119 if (is_gimple_reg (*expr_p))
3120 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3123 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3124 a call to __builtin_memcpy. */
3126 static enum gimplify_status
3127 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3128 gimple_seq *seq_p)
3130 tree t, to, to_ptr, from, from_ptr;
3131 gimple gs;
3132 location_t loc = EXPR_LOCATION (*expr_p);
3134 to = TREE_OPERAND (*expr_p, 0);
3135 from = TREE_OPERAND (*expr_p, 1);
3137 /* Mark the RHS addressable. Beware that it may not be possible to do so
3138 directly if a temporary has been created by the gimplification. */
3139 prepare_gimple_addressable (&from, seq_p);
3141 mark_addressable (from);
3142 from_ptr = build_fold_addr_expr_loc (loc, from);
3143 gimplify_arg (&from_ptr, seq_p, loc);
3145 mark_addressable (to);
3146 to_ptr = build_fold_addr_expr_loc (loc, to);
3147 gimplify_arg (&to_ptr, seq_p, loc);
3149 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3151 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3153 if (want_value)
3155 /* tmp = memcpy() */
3156 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3157 gimple_call_set_lhs (gs, t);
3158 gimplify_seq_add_stmt (seq_p, gs);
3160 *expr_p = build_simple_mem_ref (t);
3161 return GS_ALL_DONE;
3164 gimplify_seq_add_stmt (seq_p, gs);
3165 *expr_p = NULL;
3166 return GS_ALL_DONE;
3169 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3170 a call to __builtin_memset. In this case we know that the RHS is
3171 a CONSTRUCTOR with an empty element list. */
3173 static enum gimplify_status
3174 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3175 gimple_seq *seq_p)
3177 tree t, from, to, to_ptr;
3178 gimple gs;
3179 location_t loc = EXPR_LOCATION (*expr_p);
3181 /* Assert our assumptions, to abort instead of producing wrong code
3182 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3183 not be immediately exposed. */
3184 from = TREE_OPERAND (*expr_p, 1);
3185 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3186 from = TREE_OPERAND (from, 0);
3188 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3189 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3191 /* Now proceed. */
3192 to = TREE_OPERAND (*expr_p, 0);
3194 to_ptr = build_fold_addr_expr_loc (loc, to);
3195 gimplify_arg (&to_ptr, seq_p, loc);
3196 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3198 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3200 if (want_value)
3202 /* tmp = memset() */
3203 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3204 gimple_call_set_lhs (gs, t);
3205 gimplify_seq_add_stmt (seq_p, gs);
3207 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3208 return GS_ALL_DONE;
3211 gimplify_seq_add_stmt (seq_p, gs);
3212 *expr_p = NULL;
3213 return GS_ALL_DONE;
3216 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3217 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3218 assignment. Return non-null if we detect a potential overlap. */
3220 struct gimplify_init_ctor_preeval_data
3222 /* The base decl of the lhs object. May be NULL, in which case we
3223 have to assume the lhs is indirect. */
3224 tree lhs_base_decl;
3226 /* The alias set of the lhs object. */
3227 alias_set_type lhs_alias_set;
3230 static tree
3231 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3233 struct gimplify_init_ctor_preeval_data *data
3234 = (struct gimplify_init_ctor_preeval_data *) xdata;
3235 tree t = *tp;
3237 /* If we find the base object, obviously we have overlap. */
3238 if (data->lhs_base_decl == t)
3239 return t;
3241 /* If the constructor component is indirect, determine if we have a
3242 potential overlap with the lhs. The only bits of information we
3243 have to go on at this point are addressability and alias sets. */
3244 if ((INDIRECT_REF_P (t)
3245 || TREE_CODE (t) == MEM_REF)
3246 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3247 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3248 return t;
3250 /* If the constructor component is a call, determine if it can hide a
3251 potential overlap with the lhs through an INDIRECT_REF like above.
3252 ??? Ugh - this is completely broken. In fact this whole analysis
3253 doesn't look conservative. */
3254 if (TREE_CODE (t) == CALL_EXPR)
3256 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3258 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3259 if (POINTER_TYPE_P (TREE_VALUE (type))
3260 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3261 && alias_sets_conflict_p (data->lhs_alias_set,
3262 get_alias_set
3263 (TREE_TYPE (TREE_VALUE (type)))))
3264 return t;
3267 if (IS_TYPE_OR_DECL_P (t))
3268 *walk_subtrees = 0;
3269 return NULL;
3272 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3273 force values that overlap with the lhs (as described by *DATA)
3274 into temporaries. */
3276 static void
3277 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3278 struct gimplify_init_ctor_preeval_data *data)
3280 enum gimplify_status one;
3282 /* If the value is constant, then there's nothing to pre-evaluate. */
3283 if (TREE_CONSTANT (*expr_p))
3285 /* Ensure it does not have side effects, it might contain a reference to
3286 the object we're initializing. */
3287 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3288 return;
3291 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3292 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3293 return;
3295 /* Recurse for nested constructors. */
3296 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3298 unsigned HOST_WIDE_INT ix;
3299 constructor_elt *ce;
3300 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3302 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3303 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3305 return;
3308 /* If this is a variable sized type, we must remember the size. */
3309 maybe_with_size_expr (expr_p);
3311 /* Gimplify the constructor element to something appropriate for the rhs
3312 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3313 the gimplifier will consider this a store to memory. Doing this
3314 gimplification now means that we won't have to deal with complicated
3315 language-specific trees, nor trees like SAVE_EXPR that can induce
3316 exponential search behavior. */
3317 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3318 if (one == GS_ERROR)
3320 *expr_p = NULL;
3321 return;
3324 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3325 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3326 always be true for all scalars, since is_gimple_mem_rhs insists on a
3327 temporary variable for them. */
3328 if (DECL_P (*expr_p))
3329 return;
3331 /* If this is of variable size, we have no choice but to assume it doesn't
3332 overlap since we can't make a temporary for it. */
3333 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3334 return;
3336 /* Otherwise, we must search for overlap ... */
3337 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3338 return;
3340 /* ... and if found, force the value into a temporary. */
3341 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3344 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3345 a RANGE_EXPR in a CONSTRUCTOR for an array.
3347 var = lower;
3348 loop_entry:
3349 object[var] = value;
3350 if (var == upper)
3351 goto loop_exit;
3352 var = var + 1;
3353 goto loop_entry;
3354 loop_exit:
3356 We increment var _after_ the loop exit check because we might otherwise
3357 fail if upper == TYPE_MAX_VALUE (type for upper).
3359 Note that we never have to deal with SAVE_EXPRs here, because this has
3360 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3362 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3363 gimple_seq *, bool);
3365 static void
3366 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3367 tree value, tree array_elt_type,
3368 gimple_seq *pre_p, bool cleared)
3370 tree loop_entry_label, loop_exit_label, fall_thru_label;
3371 tree var, var_type, cref, tmp;
3373 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3374 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3375 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3377 /* Create and initialize the index variable. */
3378 var_type = TREE_TYPE (upper);
3379 var = create_tmp_var (var_type, NULL);
3380 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3382 /* Add the loop entry label. */
3383 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3385 /* Build the reference. */
3386 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3387 var, NULL_TREE, NULL_TREE);
3389 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3390 the store. Otherwise just assign value to the reference. */
3392 if (TREE_CODE (value) == CONSTRUCTOR)
3393 /* NB we might have to call ourself recursively through
3394 gimplify_init_ctor_eval if the value is a constructor. */
3395 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3396 pre_p, cleared);
3397 else
3398 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3400 /* We exit the loop when the index var is equal to the upper bound. */
3401 gimplify_seq_add_stmt (pre_p,
3402 gimple_build_cond (EQ_EXPR, var, upper,
3403 loop_exit_label, fall_thru_label));
3405 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3407 /* Otherwise, increment the index var... */
3408 tmp = build2 (PLUS_EXPR, var_type, var,
3409 fold_convert (var_type, integer_one_node));
3410 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3412 /* ...and jump back to the loop entry. */
3413 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3415 /* Add the loop exit label. */
3416 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3419 /* Return true if FDECL is accessing a field that is zero sized. */
3421 static bool
3422 zero_sized_field_decl (const_tree fdecl)
3424 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3425 && integer_zerop (DECL_SIZE (fdecl)))
3426 return true;
3427 return false;
3430 /* Return true if TYPE is zero sized. */
3432 static bool
3433 zero_sized_type (const_tree type)
3435 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3436 && integer_zerop (TYPE_SIZE (type)))
3437 return true;
3438 return false;
3441 /* A subroutine of gimplify_init_constructor. Generate individual
3442 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3443 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3444 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3445 zeroed first. */
3447 static void
3448 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3449 gimple_seq *pre_p, bool cleared)
3451 tree array_elt_type = NULL;
3452 unsigned HOST_WIDE_INT ix;
3453 tree purpose, value;
3455 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3456 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3458 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3460 tree cref;
3462 /* NULL values are created above for gimplification errors. */
3463 if (value == NULL)
3464 continue;
3466 if (cleared && initializer_zerop (value))
3467 continue;
3469 /* ??? Here's to hoping the front end fills in all of the indices,
3470 so we don't have to figure out what's missing ourselves. */
3471 gcc_assert (purpose);
3473 /* Skip zero-sized fields, unless value has side-effects. This can
3474 happen with calls to functions returning a zero-sized type, which
3475 we shouldn't discard. As a number of downstream passes don't
3476 expect sets of zero-sized fields, we rely on the gimplification of
3477 the MODIFY_EXPR we make below to drop the assignment statement. */
3478 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3479 continue;
3481 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3482 whole range. */
3483 if (TREE_CODE (purpose) == RANGE_EXPR)
3485 tree lower = TREE_OPERAND (purpose, 0);
3486 tree upper = TREE_OPERAND (purpose, 1);
3488 /* If the lower bound is equal to upper, just treat it as if
3489 upper was the index. */
3490 if (simple_cst_equal (lower, upper))
3491 purpose = upper;
3492 else
3494 gimplify_init_ctor_eval_range (object, lower, upper, value,
3495 array_elt_type, pre_p, cleared);
3496 continue;
3500 if (array_elt_type)
3502 /* Do not use bitsizetype for ARRAY_REF indices. */
3503 if (TYPE_DOMAIN (TREE_TYPE (object)))
3504 purpose
3505 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3506 purpose);
3507 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3508 purpose, NULL_TREE, NULL_TREE);
3510 else
3512 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3513 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3514 unshare_expr (object), purpose, NULL_TREE);
3517 if (TREE_CODE (value) == CONSTRUCTOR
3518 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3519 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3520 pre_p, cleared);
3521 else
3523 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3524 gimplify_and_add (init, pre_p);
3525 ggc_free (init);
3530 /* Return the appropriate RHS predicate for this LHS. */
3532 gimple_predicate
3533 rhs_predicate_for (tree lhs)
3535 if (is_gimple_reg (lhs))
3536 return is_gimple_reg_rhs_or_call;
3537 else
3538 return is_gimple_mem_rhs_or_call;
3541 /* Gimplify a C99 compound literal expression. This just means adding
3542 the DECL_EXPR before the current statement and using its anonymous
3543 decl instead. */
3545 static enum gimplify_status
3546 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3547 bool (*gimple_test_f) (tree),
3548 fallback_t fallback)
3550 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3551 tree decl = DECL_EXPR_DECL (decl_s);
3552 tree init = DECL_INITIAL (decl);
3553 /* Mark the decl as addressable if the compound literal
3554 expression is addressable now, otherwise it is marked too late
3555 after we gimplify the initialization expression. */
3556 if (TREE_ADDRESSABLE (*expr_p))
3557 TREE_ADDRESSABLE (decl) = 1;
3558 /* Otherwise, if we don't need an lvalue and have a literal directly
3559 substitute it. Check if it matches the gimple predicate, as
3560 otherwise we'd generate a new temporary, and we can as well just
3561 use the decl we already have. */
3562 else if (!TREE_ADDRESSABLE (decl)
3563 && init
3564 && (fallback & fb_lvalue) == 0
3565 && gimple_test_f (init))
3567 *expr_p = init;
3568 return GS_OK;
3571 /* Preliminarily mark non-addressed complex variables as eligible
3572 for promotion to gimple registers. We'll transform their uses
3573 as we find them. */
3574 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3575 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3576 && !TREE_THIS_VOLATILE (decl)
3577 && !needs_to_live_in_memory (decl))
3578 DECL_GIMPLE_REG_P (decl) = 1;
3580 /* If the decl is not addressable, then it is being used in some
3581 expression or on the right hand side of a statement, and it can
3582 be put into a readonly data section. */
3583 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3584 TREE_READONLY (decl) = 1;
3586 /* This decl isn't mentioned in the enclosing block, so add it to the
3587 list of temps. FIXME it seems a bit of a kludge to say that
3588 anonymous artificial vars aren't pushed, but everything else is. */
3589 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3590 gimple_add_tmp_var (decl);
3592 gimplify_and_add (decl_s, pre_p);
3593 *expr_p = decl;
3594 return GS_OK;
3597 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3598 return a new CONSTRUCTOR if something changed. */
3600 static tree
3601 optimize_compound_literals_in_ctor (tree orig_ctor)
3603 tree ctor = orig_ctor;
3604 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3605 unsigned int idx, num = vec_safe_length (elts);
3607 for (idx = 0; idx < num; idx++)
3609 tree value = (*elts)[idx].value;
3610 tree newval = value;
3611 if (TREE_CODE (value) == CONSTRUCTOR)
3612 newval = optimize_compound_literals_in_ctor (value);
3613 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3615 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3616 tree decl = DECL_EXPR_DECL (decl_s);
3617 tree init = DECL_INITIAL (decl);
3619 if (!TREE_ADDRESSABLE (value)
3620 && !TREE_ADDRESSABLE (decl)
3621 && init
3622 && TREE_CODE (init) == CONSTRUCTOR)
3623 newval = optimize_compound_literals_in_ctor (init);
3625 if (newval == value)
3626 continue;
3628 if (ctor == orig_ctor)
3630 ctor = copy_node (orig_ctor);
3631 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3632 elts = CONSTRUCTOR_ELTS (ctor);
3634 (*elts)[idx].value = newval;
3636 return ctor;
3639 /* A subroutine of gimplify_modify_expr. Break out elements of a
3640 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3642 Note that we still need to clear any elements that don't have explicit
3643 initializers, so if not all elements are initialized we keep the
3644 original MODIFY_EXPR, we just remove all of the constructor elements.
3646 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3647 GS_ERROR if we would have to create a temporary when gimplifying
3648 this constructor. Otherwise, return GS_OK.
3650 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3652 static enum gimplify_status
3653 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3654 bool want_value, bool notify_temp_creation)
3656 tree object, ctor, type;
3657 enum gimplify_status ret;
3658 vec<constructor_elt, va_gc> *elts;
3660 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3662 if (!notify_temp_creation)
3664 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3665 is_gimple_lvalue, fb_lvalue);
3666 if (ret == GS_ERROR)
3667 return ret;
3670 object = TREE_OPERAND (*expr_p, 0);
3671 ctor = TREE_OPERAND (*expr_p, 1) =
3672 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3673 type = TREE_TYPE (ctor);
3674 elts = CONSTRUCTOR_ELTS (ctor);
3675 ret = GS_ALL_DONE;
3677 switch (TREE_CODE (type))
3679 case RECORD_TYPE:
3680 case UNION_TYPE:
3681 case QUAL_UNION_TYPE:
3682 case ARRAY_TYPE:
3684 struct gimplify_init_ctor_preeval_data preeval_data;
3685 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3686 bool cleared, complete_p, valid_const_initializer;
3688 /* Aggregate types must lower constructors to initialization of
3689 individual elements. The exception is that a CONSTRUCTOR node
3690 with no elements indicates zero-initialization of the whole. */
3691 if (vec_safe_is_empty (elts))
3693 if (notify_temp_creation)
3694 return GS_OK;
3695 break;
3698 /* Fetch information about the constructor to direct later processing.
3699 We might want to make static versions of it in various cases, and
3700 can only do so if it known to be a valid constant initializer. */
3701 valid_const_initializer
3702 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3703 &num_ctor_elements, &complete_p);
3705 /* If a const aggregate variable is being initialized, then it
3706 should never be a lose to promote the variable to be static. */
3707 if (valid_const_initializer
3708 && num_nonzero_elements > 1
3709 && TREE_READONLY (object)
3710 && TREE_CODE (object) == VAR_DECL
3711 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3713 if (notify_temp_creation)
3714 return GS_ERROR;
3715 DECL_INITIAL (object) = ctor;
3716 TREE_STATIC (object) = 1;
3717 if (!DECL_NAME (object))
3718 DECL_NAME (object) = create_tmp_var_name ("C");
3719 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3721 /* ??? C++ doesn't automatically append a .<number> to the
3722 assembler name, and even when it does, it looks at FE private
3723 data structures to figure out what that number should be,
3724 which are not set for this variable. I suppose this is
3725 important for local statics for inline functions, which aren't
3726 "local" in the object file sense. So in order to get a unique
3727 TU-local symbol, we must invoke the lhd version now. */
3728 lhd_set_decl_assembler_name (object);
3730 *expr_p = NULL_TREE;
3731 break;
3734 /* If there are "lots" of initialized elements, even discounting
3735 those that are not address constants (and thus *must* be
3736 computed at runtime), then partition the constructor into
3737 constant and non-constant parts. Block copy the constant
3738 parts in, then generate code for the non-constant parts. */
3739 /* TODO. There's code in cp/typeck.c to do this. */
3741 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3742 /* store_constructor will ignore the clearing of variable-sized
3743 objects. Initializers for such objects must explicitly set
3744 every field that needs to be set. */
3745 cleared = false;
3746 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3747 /* If the constructor isn't complete, clear the whole object
3748 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3750 ??? This ought not to be needed. For any element not present
3751 in the initializer, we should simply set them to zero. Except
3752 we'd need to *find* the elements that are not present, and that
3753 requires trickery to avoid quadratic compile-time behavior in
3754 large cases or excessive memory use in small cases. */
3755 cleared = true;
3756 else if (num_ctor_elements - num_nonzero_elements
3757 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3758 && num_nonzero_elements < num_ctor_elements / 4)
3759 /* If there are "lots" of zeros, it's more efficient to clear
3760 the memory and then set the nonzero elements. */
3761 cleared = true;
3762 else
3763 cleared = false;
3765 /* If there are "lots" of initialized elements, and all of them
3766 are valid address constants, then the entire initializer can
3767 be dropped to memory, and then memcpy'd out. Don't do this
3768 for sparse arrays, though, as it's more efficient to follow
3769 the standard CONSTRUCTOR behavior of memset followed by
3770 individual element initialization. Also don't do this for small
3771 all-zero initializers (which aren't big enough to merit
3772 clearing), and don't try to make bitwise copies of
3773 TREE_ADDRESSABLE types. */
3774 if (valid_const_initializer
3775 && !(cleared || num_nonzero_elements == 0)
3776 && !TREE_ADDRESSABLE (type))
3778 HOST_WIDE_INT size = int_size_in_bytes (type);
3779 unsigned int align;
3781 /* ??? We can still get unbounded array types, at least
3782 from the C++ front end. This seems wrong, but attempt
3783 to work around it for now. */
3784 if (size < 0)
3786 size = int_size_in_bytes (TREE_TYPE (object));
3787 if (size >= 0)
3788 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3791 /* Find the maximum alignment we can assume for the object. */
3792 /* ??? Make use of DECL_OFFSET_ALIGN. */
3793 if (DECL_P (object))
3794 align = DECL_ALIGN (object);
3795 else
3796 align = TYPE_ALIGN (type);
3798 /* Do a block move either if the size is so small as to make
3799 each individual move a sub-unit move on average, or if it
3800 is so large as to make individual moves inefficient. */
3801 if (size > 0
3802 && num_nonzero_elements > 1
3803 && (size < num_nonzero_elements
3804 || !can_move_by_pieces (size, align)))
3806 if (notify_temp_creation)
3807 return GS_ERROR;
3809 walk_tree (&ctor, force_labels_r, NULL, NULL);
3810 ctor = tree_output_constant_def (ctor);
3811 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3812 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3813 TREE_OPERAND (*expr_p, 1) = ctor;
3815 /* This is no longer an assignment of a CONSTRUCTOR, but
3816 we still may have processing to do on the LHS. So
3817 pretend we didn't do anything here to let that happen. */
3818 return GS_UNHANDLED;
3822 /* If the target is volatile, we have non-zero elements and more than
3823 one field to assign, initialize the target from a temporary. */
3824 if (TREE_THIS_VOLATILE (object)
3825 && !TREE_ADDRESSABLE (type)
3826 && num_nonzero_elements > 0
3827 && vec_safe_length (elts) > 1)
3829 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3830 TREE_OPERAND (*expr_p, 0) = temp;
3831 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3832 *expr_p,
3833 build2 (MODIFY_EXPR, void_type_node,
3834 object, temp));
3835 return GS_OK;
3838 if (notify_temp_creation)
3839 return GS_OK;
3841 /* If there are nonzero elements and if needed, pre-evaluate to capture
3842 elements overlapping with the lhs into temporaries. We must do this
3843 before clearing to fetch the values before they are zeroed-out. */
3844 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3846 preeval_data.lhs_base_decl = get_base_address (object);
3847 if (!DECL_P (preeval_data.lhs_base_decl))
3848 preeval_data.lhs_base_decl = NULL;
3849 preeval_data.lhs_alias_set = get_alias_set (object);
3851 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3852 pre_p, post_p, &preeval_data);
3855 if (cleared)
3857 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3858 Note that we still have to gimplify, in order to handle the
3859 case of variable sized types. Avoid shared tree structures. */
3860 CONSTRUCTOR_ELTS (ctor) = NULL;
3861 TREE_SIDE_EFFECTS (ctor) = 0;
3862 object = unshare_expr (object);
3863 gimplify_stmt (expr_p, pre_p);
3866 /* If we have not block cleared the object, or if there are nonzero
3867 elements in the constructor, add assignments to the individual
3868 scalar fields of the object. */
3869 if (!cleared || num_nonzero_elements > 0)
3870 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3872 *expr_p = NULL_TREE;
3874 break;
3876 case COMPLEX_TYPE:
3878 tree r, i;
3880 if (notify_temp_creation)
3881 return GS_OK;
3883 /* Extract the real and imaginary parts out of the ctor. */
3884 gcc_assert (elts->length () == 2);
3885 r = (*elts)[0].value;
3886 i = (*elts)[1].value;
3887 if (r == NULL || i == NULL)
3889 tree zero = build_zero_cst (TREE_TYPE (type));
3890 if (r == NULL)
3891 r = zero;
3892 if (i == NULL)
3893 i = zero;
3896 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3897 represent creation of a complex value. */
3898 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3900 ctor = build_complex (type, r, i);
3901 TREE_OPERAND (*expr_p, 1) = ctor;
3903 else
3905 ctor = build2 (COMPLEX_EXPR, type, r, i);
3906 TREE_OPERAND (*expr_p, 1) = ctor;
3907 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
3908 pre_p,
3909 post_p,
3910 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3911 fb_rvalue);
3914 break;
3916 case VECTOR_TYPE:
3918 unsigned HOST_WIDE_INT ix;
3919 constructor_elt *ce;
3921 if (notify_temp_creation)
3922 return GS_OK;
3924 /* Go ahead and simplify constant constructors to VECTOR_CST. */
3925 if (TREE_CONSTANT (ctor))
3927 bool constant_p = true;
3928 tree value;
3930 /* Even when ctor is constant, it might contain non-*_CST
3931 elements, such as addresses or trapping values like
3932 1.0/0.0 - 1.0/0.0. Such expressions don't belong
3933 in VECTOR_CST nodes. */
3934 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
3935 if (!CONSTANT_CLASS_P (value))
3937 constant_p = false;
3938 break;
3941 if (constant_p)
3943 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
3944 break;
3947 /* Don't reduce an initializer constant even if we can't
3948 make a VECTOR_CST. It won't do anything for us, and it'll
3949 prevent us from representing it as a single constant. */
3950 if (initializer_constant_valid_p (ctor, type))
3951 break;
3953 TREE_CONSTANT (ctor) = 0;
3956 /* Vector types use CONSTRUCTOR all the way through gimple
3957 compilation as a general initializer. */
3958 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
3960 enum gimplify_status tret;
3961 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
3962 fb_rvalue);
3963 if (tret == GS_ERROR)
3964 ret = GS_ERROR;
3966 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
3967 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
3969 break;
3971 default:
3972 /* So how did we get a CONSTRUCTOR for a scalar type? */
3973 gcc_unreachable ();
3976 if (ret == GS_ERROR)
3977 return GS_ERROR;
3978 else if (want_value)
3980 *expr_p = object;
3981 return GS_OK;
3983 else
3985 /* If we have gimplified both sides of the initializer but have
3986 not emitted an assignment, do so now. */
3987 if (*expr_p)
3989 tree lhs = TREE_OPERAND (*expr_p, 0);
3990 tree rhs = TREE_OPERAND (*expr_p, 1);
3991 gimple init = gimple_build_assign (lhs, rhs);
3992 gimplify_seq_add_stmt (pre_p, init);
3993 *expr_p = NULL;
3996 return GS_ALL_DONE;
4000 /* Given a pointer value OP0, return a simplified version of an
4001 indirection through OP0, or NULL_TREE if no simplification is
4002 possible. This may only be applied to a rhs of an expression.
4003 Note that the resulting type may be different from the type pointed
4004 to in the sense that it is still compatible from the langhooks
4005 point of view. */
4007 static tree
4008 gimple_fold_indirect_ref_rhs (tree t)
4010 return gimple_fold_indirect_ref (t);
4013 /* Subroutine of gimplify_modify_expr to do simplifications of
4014 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4015 something changes. */
4017 static enum gimplify_status
4018 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4019 gimple_seq *pre_p, gimple_seq *post_p,
4020 bool want_value)
4022 enum gimplify_status ret = GS_UNHANDLED;
4023 bool changed;
4027 changed = false;
4028 switch (TREE_CODE (*from_p))
4030 case VAR_DECL:
4031 /* If we're assigning from a read-only variable initialized with
4032 a constructor, do the direct assignment from the constructor,
4033 but only if neither source nor target are volatile since this
4034 latter assignment might end up being done on a per-field basis. */
4035 if (DECL_INITIAL (*from_p)
4036 && TREE_READONLY (*from_p)
4037 && !TREE_THIS_VOLATILE (*from_p)
4038 && !TREE_THIS_VOLATILE (*to_p)
4039 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4041 tree old_from = *from_p;
4042 enum gimplify_status subret;
4044 /* Move the constructor into the RHS. */
4045 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4047 /* Let's see if gimplify_init_constructor will need to put
4048 it in memory. */
4049 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4050 false, true);
4051 if (subret == GS_ERROR)
4053 /* If so, revert the change. */
4054 *from_p = old_from;
4056 else
4058 ret = GS_OK;
4059 changed = true;
4062 break;
4063 case INDIRECT_REF:
4065 /* If we have code like
4067 *(const A*)(A*)&x
4069 where the type of "x" is a (possibly cv-qualified variant
4070 of "A"), treat the entire expression as identical to "x".
4071 This kind of code arises in C++ when an object is bound
4072 to a const reference, and if "x" is a TARGET_EXPR we want
4073 to take advantage of the optimization below. */
4074 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4075 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4076 if (t)
4078 if (TREE_THIS_VOLATILE (t) != volatile_p)
4080 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4081 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4082 build_fold_addr_expr (t));
4083 if (REFERENCE_CLASS_P (t))
4084 TREE_THIS_VOLATILE (t) = volatile_p;
4086 *from_p = t;
4087 ret = GS_OK;
4088 changed = true;
4090 break;
4093 case TARGET_EXPR:
4095 /* If we are initializing something from a TARGET_EXPR, strip the
4096 TARGET_EXPR and initialize it directly, if possible. This can't
4097 be done if the initializer is void, since that implies that the
4098 temporary is set in some non-trivial way.
4100 ??? What about code that pulls out the temp and uses it
4101 elsewhere? I think that such code never uses the TARGET_EXPR as
4102 an initializer. If I'm wrong, we'll die because the temp won't
4103 have any RTL. In that case, I guess we'll need to replace
4104 references somehow. */
4105 tree init = TARGET_EXPR_INITIAL (*from_p);
4107 if (init
4108 && !VOID_TYPE_P (TREE_TYPE (init)))
4110 *from_p = init;
4111 ret = GS_OK;
4112 changed = true;
4115 break;
4117 case COMPOUND_EXPR:
4118 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4119 caught. */
4120 gimplify_compound_expr (from_p, pre_p, true);
4121 ret = GS_OK;
4122 changed = true;
4123 break;
4125 case CONSTRUCTOR:
4126 /* If we already made some changes, let the front end have a
4127 crack at this before we break it down. */
4128 if (ret != GS_UNHANDLED)
4129 break;
4130 /* If we're initializing from a CONSTRUCTOR, break this into
4131 individual MODIFY_EXPRs. */
4132 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4133 false);
4135 case COND_EXPR:
4136 /* If we're assigning to a non-register type, push the assignment
4137 down into the branches. This is mandatory for ADDRESSABLE types,
4138 since we cannot generate temporaries for such, but it saves a
4139 copy in other cases as well. */
4140 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4142 /* This code should mirror the code in gimplify_cond_expr. */
4143 enum tree_code code = TREE_CODE (*expr_p);
4144 tree cond = *from_p;
4145 tree result = *to_p;
4147 ret = gimplify_expr (&result, pre_p, post_p,
4148 is_gimple_lvalue, fb_lvalue);
4149 if (ret != GS_ERROR)
4150 ret = GS_OK;
4152 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4153 TREE_OPERAND (cond, 1)
4154 = build2 (code, void_type_node, result,
4155 TREE_OPERAND (cond, 1));
4156 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4157 TREE_OPERAND (cond, 2)
4158 = build2 (code, void_type_node, unshare_expr (result),
4159 TREE_OPERAND (cond, 2));
4161 TREE_TYPE (cond) = void_type_node;
4162 recalculate_side_effects (cond);
4164 if (want_value)
4166 gimplify_and_add (cond, pre_p);
4167 *expr_p = unshare_expr (result);
4169 else
4170 *expr_p = cond;
4171 return ret;
4173 break;
4175 case CALL_EXPR:
4176 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4177 return slot so that we don't generate a temporary. */
4178 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4179 && aggregate_value_p (*from_p, *from_p))
4181 bool use_target;
4183 if (!(rhs_predicate_for (*to_p))(*from_p))
4184 /* If we need a temporary, *to_p isn't accurate. */
4185 use_target = false;
4186 /* It's OK to use the return slot directly unless it's an NRV. */
4187 else if (TREE_CODE (*to_p) == RESULT_DECL
4188 && DECL_NAME (*to_p) == NULL_TREE
4189 && needs_to_live_in_memory (*to_p))
4190 use_target = true;
4191 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4192 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4193 /* Don't force regs into memory. */
4194 use_target = false;
4195 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4196 /* It's OK to use the target directly if it's being
4197 initialized. */
4198 use_target = true;
4199 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4200 /* Always use the target and thus RSO for variable-sized types.
4201 GIMPLE cannot deal with a variable-sized assignment
4202 embedded in a call statement. */
4203 use_target = true;
4204 else if (TREE_CODE (*to_p) != SSA_NAME
4205 && (!is_gimple_variable (*to_p)
4206 || needs_to_live_in_memory (*to_p)))
4207 /* Don't use the original target if it's already addressable;
4208 if its address escapes, and the called function uses the
4209 NRV optimization, a conforming program could see *to_p
4210 change before the called function returns; see c++/19317.
4211 When optimizing, the return_slot pass marks more functions
4212 as safe after we have escape info. */
4213 use_target = false;
4214 else
4215 use_target = true;
4217 if (use_target)
4219 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4220 mark_addressable (*to_p);
4223 break;
4225 case WITH_SIZE_EXPR:
4226 /* Likewise for calls that return an aggregate of non-constant size,
4227 since we would not be able to generate a temporary at all. */
4228 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4230 *from_p = TREE_OPERAND (*from_p, 0);
4231 /* We don't change ret in this case because the
4232 WITH_SIZE_EXPR might have been added in
4233 gimplify_modify_expr, so returning GS_OK would lead to an
4234 infinite loop. */
4235 changed = true;
4237 break;
4239 /* If we're initializing from a container, push the initialization
4240 inside it. */
4241 case CLEANUP_POINT_EXPR:
4242 case BIND_EXPR:
4243 case STATEMENT_LIST:
4245 tree wrap = *from_p;
4246 tree t;
4248 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4249 fb_lvalue);
4250 if (ret != GS_ERROR)
4251 ret = GS_OK;
4253 t = voidify_wrapper_expr (wrap, *expr_p);
4254 gcc_assert (t == *expr_p);
4256 if (want_value)
4258 gimplify_and_add (wrap, pre_p);
4259 *expr_p = unshare_expr (*to_p);
4261 else
4262 *expr_p = wrap;
4263 return GS_OK;
4266 case COMPOUND_LITERAL_EXPR:
4268 tree complit = TREE_OPERAND (*expr_p, 1);
4269 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4270 tree decl = DECL_EXPR_DECL (decl_s);
4271 tree init = DECL_INITIAL (decl);
4273 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4274 into struct T x = { 0, 1, 2 } if the address of the
4275 compound literal has never been taken. */
4276 if (!TREE_ADDRESSABLE (complit)
4277 && !TREE_ADDRESSABLE (decl)
4278 && init)
4280 *expr_p = copy_node (*expr_p);
4281 TREE_OPERAND (*expr_p, 1) = init;
4282 return GS_OK;
4286 default:
4287 break;
4290 while (changed);
4292 return ret;
4296 /* Return true if T looks like a valid GIMPLE statement. */
4298 static bool
4299 is_gimple_stmt (tree t)
4301 const enum tree_code code = TREE_CODE (t);
4303 switch (code)
4305 case NOP_EXPR:
4306 /* The only valid NOP_EXPR is the empty statement. */
4307 return IS_EMPTY_STMT (t);
4309 case BIND_EXPR:
4310 case COND_EXPR:
4311 /* These are only valid if they're void. */
4312 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4314 case SWITCH_EXPR:
4315 case GOTO_EXPR:
4316 case RETURN_EXPR:
4317 case LABEL_EXPR:
4318 case CASE_LABEL_EXPR:
4319 case TRY_CATCH_EXPR:
4320 case TRY_FINALLY_EXPR:
4321 case EH_FILTER_EXPR:
4322 case CATCH_EXPR:
4323 case ASM_EXPR:
4324 case STATEMENT_LIST:
4325 case OMP_PARALLEL:
4326 case OMP_FOR:
4327 case OMP_SIMD:
4328 case CILK_SIMD:
4329 case OMP_DISTRIBUTE:
4330 case OMP_SECTIONS:
4331 case OMP_SECTION:
4332 case OMP_SINGLE:
4333 case OMP_MASTER:
4334 case OMP_TASKGROUP:
4335 case OMP_ORDERED:
4336 case OMP_CRITICAL:
4337 case OMP_TASK:
4338 /* These are always void. */
4339 return true;
4341 case CALL_EXPR:
4342 case MODIFY_EXPR:
4343 case PREDICT_EXPR:
4344 /* These are valid regardless of their type. */
4345 return true;
4347 default:
4348 return false;
4353 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4354 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4355 DECL_GIMPLE_REG_P set.
4357 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4358 other, unmodified part of the complex object just before the total store.
4359 As a consequence, if the object is still uninitialized, an undefined value
4360 will be loaded into a register, which may result in a spurious exception
4361 if the register is floating-point and the value happens to be a signaling
4362 NaN for example. Then the fully-fledged complex operations lowering pass
4363 followed by a DCE pass are necessary in order to fix things up. */
4365 static enum gimplify_status
4366 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4367 bool want_value)
4369 enum tree_code code, ocode;
4370 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4372 lhs = TREE_OPERAND (*expr_p, 0);
4373 rhs = TREE_OPERAND (*expr_p, 1);
4374 code = TREE_CODE (lhs);
4375 lhs = TREE_OPERAND (lhs, 0);
4377 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4378 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4379 TREE_NO_WARNING (other) = 1;
4380 other = get_formal_tmp_var (other, pre_p);
4382 realpart = code == REALPART_EXPR ? rhs : other;
4383 imagpart = code == REALPART_EXPR ? other : rhs;
4385 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4386 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4387 else
4388 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4390 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4391 *expr_p = (want_value) ? rhs : NULL_TREE;
4393 return GS_ALL_DONE;
4396 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4398 modify_expr
4399 : varname '=' rhs
4400 | '*' ID '=' rhs
4402 PRE_P points to the list where side effects that must happen before
4403 *EXPR_P should be stored.
4405 POST_P points to the list where side effects that must happen after
4406 *EXPR_P should be stored.
4408 WANT_VALUE is nonzero iff we want to use the value of this expression
4409 in another expression. */
4411 static enum gimplify_status
4412 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4413 bool want_value)
4415 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4416 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4417 enum gimplify_status ret = GS_UNHANDLED;
4418 gimple assign;
4419 location_t loc = EXPR_LOCATION (*expr_p);
4420 gimple_stmt_iterator gsi;
4422 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4423 || TREE_CODE (*expr_p) == INIT_EXPR);
4425 /* Trying to simplify a clobber using normal logic doesn't work,
4426 so handle it here. */
4427 if (TREE_CLOBBER_P (*from_p))
4429 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4430 if (ret == GS_ERROR)
4431 return ret;
4432 gcc_assert (!want_value
4433 && (TREE_CODE (*to_p) == VAR_DECL
4434 || TREE_CODE (*to_p) == MEM_REF));
4435 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4436 *expr_p = NULL;
4437 return GS_ALL_DONE;
4440 /* Insert pointer conversions required by the middle-end that are not
4441 required by the frontend. This fixes middle-end type checking for
4442 for example gcc.dg/redecl-6.c. */
4443 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4445 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4446 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4447 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4450 /* See if any simplifications can be done based on what the RHS is. */
4451 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4452 want_value);
4453 if (ret != GS_UNHANDLED)
4454 return ret;
4456 /* For zero sized types only gimplify the left hand side and right hand
4457 side as statements and throw away the assignment. Do this after
4458 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4459 types properly. */
4460 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4462 gimplify_stmt (from_p, pre_p);
4463 gimplify_stmt (to_p, pre_p);
4464 *expr_p = NULL_TREE;
4465 return GS_ALL_DONE;
4468 /* If the value being copied is of variable width, compute the length
4469 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4470 before gimplifying any of the operands so that we can resolve any
4471 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4472 the size of the expression to be copied, not of the destination, so
4473 that is what we must do here. */
4474 maybe_with_size_expr (from_p);
4476 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4477 if (ret == GS_ERROR)
4478 return ret;
4480 /* As a special case, we have to temporarily allow for assignments
4481 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4482 a toplevel statement, when gimplifying the GENERIC expression
4483 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4484 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4486 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4487 prevent gimplify_expr from trying to create a new temporary for
4488 foo's LHS, we tell it that it should only gimplify until it
4489 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4490 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4491 and all we need to do here is set 'a' to be its LHS. */
4492 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4493 fb_rvalue);
4494 if (ret == GS_ERROR)
4495 return ret;
4497 /* Now see if the above changed *from_p to something we handle specially. */
4498 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4499 want_value);
4500 if (ret != GS_UNHANDLED)
4501 return ret;
4503 /* If we've got a variable sized assignment between two lvalues (i.e. does
4504 not involve a call), then we can make things a bit more straightforward
4505 by converting the assignment to memcpy or memset. */
4506 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4508 tree from = TREE_OPERAND (*from_p, 0);
4509 tree size = TREE_OPERAND (*from_p, 1);
4511 if (TREE_CODE (from) == CONSTRUCTOR)
4512 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4514 if (is_gimple_addressable (from))
4516 *from_p = from;
4517 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4518 pre_p);
4522 /* Transform partial stores to non-addressable complex variables into
4523 total stores. This allows us to use real instead of virtual operands
4524 for these variables, which improves optimization. */
4525 if ((TREE_CODE (*to_p) == REALPART_EXPR
4526 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4527 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4528 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4530 /* Try to alleviate the effects of the gimplification creating artificial
4531 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4532 if (!gimplify_ctxp->into_ssa
4533 && TREE_CODE (*from_p) == VAR_DECL
4534 && DECL_IGNORED_P (*from_p)
4535 && DECL_P (*to_p)
4536 && !DECL_IGNORED_P (*to_p))
4538 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4539 DECL_NAME (*from_p)
4540 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4541 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4542 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4545 if (want_value && TREE_THIS_VOLATILE (*to_p))
4546 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4548 if (TREE_CODE (*from_p) == CALL_EXPR)
4550 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4551 instead of a GIMPLE_ASSIGN. */
4552 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4553 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4554 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4555 assign = gimple_build_call_from_tree (*from_p);
4556 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4557 notice_special_calls (assign);
4558 if (!gimple_call_noreturn_p (assign))
4559 gimple_call_set_lhs (assign, *to_p);
4561 else
4563 assign = gimple_build_assign (*to_p, *from_p);
4564 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4567 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4569 /* We should have got an SSA name from the start. */
4570 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4573 gimplify_seq_add_stmt (pre_p, assign);
4574 gsi = gsi_last (*pre_p);
4575 /* Don't fold stmts inside of target construct. We'll do it
4576 during omplower pass instead. */
4577 struct gimplify_omp_ctx *ctx;
4578 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
4579 if (ctx->region_type == ORT_TARGET)
4580 break;
4581 if (ctx == NULL)
4582 fold_stmt (&gsi);
4584 if (want_value)
4586 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4587 return GS_OK;
4589 else
4590 *expr_p = NULL;
4592 return GS_ALL_DONE;
4595 /* Gimplify a comparison between two variable-sized objects. Do this
4596 with a call to BUILT_IN_MEMCMP. */
4598 static enum gimplify_status
4599 gimplify_variable_sized_compare (tree *expr_p)
4601 location_t loc = EXPR_LOCATION (*expr_p);
4602 tree op0 = TREE_OPERAND (*expr_p, 0);
4603 tree op1 = TREE_OPERAND (*expr_p, 1);
4604 tree t, arg, dest, src, expr;
4606 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4607 arg = unshare_expr (arg);
4608 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4609 src = build_fold_addr_expr_loc (loc, op1);
4610 dest = build_fold_addr_expr_loc (loc, op0);
4611 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4612 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4614 expr
4615 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4616 SET_EXPR_LOCATION (expr, loc);
4617 *expr_p = expr;
4619 return GS_OK;
4622 /* Gimplify a comparison between two aggregate objects of integral scalar
4623 mode as a comparison between the bitwise equivalent scalar values. */
4625 static enum gimplify_status
4626 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4628 location_t loc = EXPR_LOCATION (*expr_p);
4629 tree op0 = TREE_OPERAND (*expr_p, 0);
4630 tree op1 = TREE_OPERAND (*expr_p, 1);
4632 tree type = TREE_TYPE (op0);
4633 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4635 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4636 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4638 *expr_p
4639 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4641 return GS_OK;
4644 /* Gimplify an expression sequence. This function gimplifies each
4645 expression and rewrites the original expression with the last
4646 expression of the sequence in GIMPLE form.
4648 PRE_P points to the list where the side effects for all the
4649 expressions in the sequence will be emitted.
4651 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4653 static enum gimplify_status
4654 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4656 tree t = *expr_p;
4660 tree *sub_p = &TREE_OPERAND (t, 0);
4662 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4663 gimplify_compound_expr (sub_p, pre_p, false);
4664 else
4665 gimplify_stmt (sub_p, pre_p);
4667 t = TREE_OPERAND (t, 1);
4669 while (TREE_CODE (t) == COMPOUND_EXPR);
4671 *expr_p = t;
4672 if (want_value)
4673 return GS_OK;
4674 else
4676 gimplify_stmt (expr_p, pre_p);
4677 return GS_ALL_DONE;
4681 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4682 gimplify. After gimplification, EXPR_P will point to a new temporary
4683 that holds the original value of the SAVE_EXPR node.
4685 PRE_P points to the list where side effects that must happen before
4686 *EXPR_P should be stored. */
4688 static enum gimplify_status
4689 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4691 enum gimplify_status ret = GS_ALL_DONE;
4692 tree val;
4694 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4695 val = TREE_OPERAND (*expr_p, 0);
4697 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4698 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4700 /* The operand may be a void-valued expression such as SAVE_EXPRs
4701 generated by the Java frontend for class initialization. It is
4702 being executed only for its side-effects. */
4703 if (TREE_TYPE (val) == void_type_node)
4705 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4706 is_gimple_stmt, fb_none);
4707 val = NULL;
4709 else
4710 val = get_initialized_tmp_var (val, pre_p, post_p);
4712 TREE_OPERAND (*expr_p, 0) = val;
4713 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4716 *expr_p = val;
4718 return ret;
4721 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4723 unary_expr
4724 : ...
4725 | '&' varname
4728 PRE_P points to the list where side effects that must happen before
4729 *EXPR_P should be stored.
4731 POST_P points to the list where side effects that must happen after
4732 *EXPR_P should be stored. */
4734 static enum gimplify_status
4735 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4737 tree expr = *expr_p;
4738 tree op0 = TREE_OPERAND (expr, 0);
4739 enum gimplify_status ret;
4740 location_t loc = EXPR_LOCATION (*expr_p);
4742 switch (TREE_CODE (op0))
4744 case INDIRECT_REF:
4745 do_indirect_ref:
4746 /* Check if we are dealing with an expression of the form '&*ptr'.
4747 While the front end folds away '&*ptr' into 'ptr', these
4748 expressions may be generated internally by the compiler (e.g.,
4749 builtins like __builtin_va_end). */
4750 /* Caution: the silent array decomposition semantics we allow for
4751 ADDR_EXPR means we can't always discard the pair. */
4752 /* Gimplification of the ADDR_EXPR operand may drop
4753 cv-qualification conversions, so make sure we add them if
4754 needed. */
4756 tree op00 = TREE_OPERAND (op0, 0);
4757 tree t_expr = TREE_TYPE (expr);
4758 tree t_op00 = TREE_TYPE (op00);
4760 if (!useless_type_conversion_p (t_expr, t_op00))
4761 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4762 *expr_p = op00;
4763 ret = GS_OK;
4765 break;
4767 case VIEW_CONVERT_EXPR:
4768 /* Take the address of our operand and then convert it to the type of
4769 this ADDR_EXPR.
4771 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4772 all clear. The impact of this transformation is even less clear. */
4774 /* If the operand is a useless conversion, look through it. Doing so
4775 guarantees that the ADDR_EXPR and its operand will remain of the
4776 same type. */
4777 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4778 op0 = TREE_OPERAND (op0, 0);
4780 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4781 build_fold_addr_expr_loc (loc,
4782 TREE_OPERAND (op0, 0)));
4783 ret = GS_OK;
4784 break;
4786 default:
4787 /* We use fb_either here because the C frontend sometimes takes
4788 the address of a call that returns a struct; see
4789 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4790 the implied temporary explicit. */
4792 /* Make the operand addressable. */
4793 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4794 is_gimple_addressable, fb_either);
4795 if (ret == GS_ERROR)
4796 break;
4798 /* Then mark it. Beware that it may not be possible to do so directly
4799 if a temporary has been created by the gimplification. */
4800 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4802 op0 = TREE_OPERAND (expr, 0);
4804 /* For various reasons, the gimplification of the expression
4805 may have made a new INDIRECT_REF. */
4806 if (TREE_CODE (op0) == INDIRECT_REF)
4807 goto do_indirect_ref;
4809 mark_addressable (TREE_OPERAND (expr, 0));
4811 /* The FEs may end up building ADDR_EXPRs early on a decl with
4812 an incomplete type. Re-build ADDR_EXPRs in canonical form
4813 here. */
4814 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4815 *expr_p = build_fold_addr_expr (op0);
4817 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4818 recompute_tree_invariant_for_addr_expr (*expr_p);
4820 /* If we re-built the ADDR_EXPR add a conversion to the original type
4821 if required. */
4822 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4823 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4825 break;
4828 return ret;
4831 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4832 value; output operands should be a gimple lvalue. */
4834 static enum gimplify_status
4835 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4837 tree expr;
4838 int noutputs;
4839 const char **oconstraints;
4840 int i;
4841 tree link;
4842 const char *constraint;
4843 bool allows_mem, allows_reg, is_inout;
4844 enum gimplify_status ret, tret;
4845 gimple stmt;
4846 vec<tree, va_gc> *inputs;
4847 vec<tree, va_gc> *outputs;
4848 vec<tree, va_gc> *clobbers;
4849 vec<tree, va_gc> *labels;
4850 tree link_next;
4852 expr = *expr_p;
4853 noutputs = list_length (ASM_OUTPUTS (expr));
4854 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4856 inputs = NULL;
4857 outputs = NULL;
4858 clobbers = NULL;
4859 labels = NULL;
4861 ret = GS_ALL_DONE;
4862 link_next = NULL_TREE;
4863 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4865 bool ok;
4866 size_t constraint_len;
4868 link_next = TREE_CHAIN (link);
4870 oconstraints[i]
4871 = constraint
4872 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4873 constraint_len = strlen (constraint);
4874 if (constraint_len == 0)
4875 continue;
4877 ok = parse_output_constraint (&constraint, i, 0, 0,
4878 &allows_mem, &allows_reg, &is_inout);
4879 if (!ok)
4881 ret = GS_ERROR;
4882 is_inout = false;
4885 if (!allows_reg && allows_mem)
4886 mark_addressable (TREE_VALUE (link));
4888 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4889 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4890 fb_lvalue | fb_mayfail);
4891 if (tret == GS_ERROR)
4893 error ("invalid lvalue in asm output %d", i);
4894 ret = tret;
4897 vec_safe_push (outputs, link);
4898 TREE_CHAIN (link) = NULL_TREE;
4900 if (is_inout)
4902 /* An input/output operand. To give the optimizers more
4903 flexibility, split it into separate input and output
4904 operands. */
4905 tree input;
4906 char buf[10];
4908 /* Turn the in/out constraint into an output constraint. */
4909 char *p = xstrdup (constraint);
4910 p[0] = '=';
4911 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4913 /* And add a matching input constraint. */
4914 if (allows_reg)
4916 sprintf (buf, "%d", i);
4918 /* If there are multiple alternatives in the constraint,
4919 handle each of them individually. Those that allow register
4920 will be replaced with operand number, the others will stay
4921 unchanged. */
4922 if (strchr (p, ',') != NULL)
4924 size_t len = 0, buflen = strlen (buf);
4925 char *beg, *end, *str, *dst;
4927 for (beg = p + 1;;)
4929 end = strchr (beg, ',');
4930 if (end == NULL)
4931 end = strchr (beg, '\0');
4932 if ((size_t) (end - beg) < buflen)
4933 len += buflen + 1;
4934 else
4935 len += end - beg + 1;
4936 if (*end)
4937 beg = end + 1;
4938 else
4939 break;
4942 str = (char *) alloca (len);
4943 for (beg = p + 1, dst = str;;)
4945 const char *tem;
4946 bool mem_p, reg_p, inout_p;
4948 end = strchr (beg, ',');
4949 if (end)
4950 *end = '\0';
4951 beg[-1] = '=';
4952 tem = beg - 1;
4953 parse_output_constraint (&tem, i, 0, 0,
4954 &mem_p, &reg_p, &inout_p);
4955 if (dst != str)
4956 *dst++ = ',';
4957 if (reg_p)
4959 memcpy (dst, buf, buflen);
4960 dst += buflen;
4962 else
4964 if (end)
4965 len = end - beg;
4966 else
4967 len = strlen (beg);
4968 memcpy (dst, beg, len);
4969 dst += len;
4971 if (end)
4972 beg = end + 1;
4973 else
4974 break;
4976 *dst = '\0';
4977 input = build_string (dst - str, str);
4979 else
4980 input = build_string (strlen (buf), buf);
4982 else
4983 input = build_string (constraint_len - 1, constraint + 1);
4985 free (p);
4987 input = build_tree_list (build_tree_list (NULL_TREE, input),
4988 unshare_expr (TREE_VALUE (link)));
4989 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
4993 link_next = NULL_TREE;
4994 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
4996 link_next = TREE_CHAIN (link);
4997 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4998 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
4999 oconstraints, &allows_mem, &allows_reg);
5001 /* If we can't make copies, we can only accept memory. */
5002 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5004 if (allows_mem)
5005 allows_reg = 0;
5006 else
5008 error ("impossible constraint in %<asm%>");
5009 error ("non-memory input %d must stay in memory", i);
5010 return GS_ERROR;
5014 /* If the operand is a memory input, it should be an lvalue. */
5015 if (!allows_reg && allows_mem)
5017 tree inputv = TREE_VALUE (link);
5018 STRIP_NOPS (inputv);
5019 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5020 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5021 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5022 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5023 TREE_VALUE (link) = error_mark_node;
5024 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5025 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5026 mark_addressable (TREE_VALUE (link));
5027 if (tret == GS_ERROR)
5029 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5030 input_location = EXPR_LOCATION (TREE_VALUE (link));
5031 error ("memory input %d is not directly addressable", i);
5032 ret = tret;
5035 else
5037 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5038 is_gimple_asm_val, fb_rvalue);
5039 if (tret == GS_ERROR)
5040 ret = tret;
5043 TREE_CHAIN (link) = NULL_TREE;
5044 vec_safe_push (inputs, link);
5047 link_next = NULL_TREE;
5048 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5050 link_next = TREE_CHAIN (link);
5051 TREE_CHAIN (link) = NULL_TREE;
5052 vec_safe_push (clobbers, link);
5055 link_next = NULL_TREE;
5056 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5058 link_next = TREE_CHAIN (link);
5059 TREE_CHAIN (link) = NULL_TREE;
5060 vec_safe_push (labels, link);
5063 /* Do not add ASMs with errors to the gimple IL stream. */
5064 if (ret != GS_ERROR)
5066 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5067 inputs, outputs, clobbers, labels);
5069 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5070 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5072 gimplify_seq_add_stmt (pre_p, stmt);
5075 return ret;
5078 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5079 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5080 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5081 return to this function.
5083 FIXME should we complexify the prequeue handling instead? Or use flags
5084 for all the cleanups and let the optimizer tighten them up? The current
5085 code seems pretty fragile; it will break on a cleanup within any
5086 non-conditional nesting. But any such nesting would be broken, anyway;
5087 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5088 and continues out of it. We can do that at the RTL level, though, so
5089 having an optimizer to tighten up try/finally regions would be a Good
5090 Thing. */
5092 static enum gimplify_status
5093 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5095 gimple_stmt_iterator iter;
5096 gimple_seq body_sequence = NULL;
5098 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5100 /* We only care about the number of conditions between the innermost
5101 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5102 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5103 int old_conds = gimplify_ctxp->conditions;
5104 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5105 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5106 gimplify_ctxp->conditions = 0;
5107 gimplify_ctxp->conditional_cleanups = NULL;
5108 gimplify_ctxp->in_cleanup_point_expr = true;
5110 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5112 gimplify_ctxp->conditions = old_conds;
5113 gimplify_ctxp->conditional_cleanups = old_cleanups;
5114 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5116 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5118 gimple wce = gsi_stmt (iter);
5120 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5122 if (gsi_one_before_end_p (iter))
5124 /* Note that gsi_insert_seq_before and gsi_remove do not
5125 scan operands, unlike some other sequence mutators. */
5126 if (!gimple_wce_cleanup_eh_only (wce))
5127 gsi_insert_seq_before_without_update (&iter,
5128 gimple_wce_cleanup (wce),
5129 GSI_SAME_STMT);
5130 gsi_remove (&iter, true);
5131 break;
5133 else
5135 gimple_statement_try *gtry;
5136 gimple_seq seq;
5137 enum gimple_try_flags kind;
5139 if (gimple_wce_cleanup_eh_only (wce))
5140 kind = GIMPLE_TRY_CATCH;
5141 else
5142 kind = GIMPLE_TRY_FINALLY;
5143 seq = gsi_split_seq_after (iter);
5145 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5146 /* Do not use gsi_replace here, as it may scan operands.
5147 We want to do a simple structural modification only. */
5148 gsi_set_stmt (&iter, gtry);
5149 iter = gsi_start (gtry->eval);
5152 else
5153 gsi_next (&iter);
5156 gimplify_seq_add_seq (pre_p, body_sequence);
5157 if (temp)
5159 *expr_p = temp;
5160 return GS_OK;
5162 else
5164 *expr_p = NULL;
5165 return GS_ALL_DONE;
5169 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5170 is the cleanup action required. EH_ONLY is true if the cleanup should
5171 only be executed if an exception is thrown, not on normal exit. */
5173 static void
5174 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5176 gimple wce;
5177 gimple_seq cleanup_stmts = NULL;
5179 /* Errors can result in improperly nested cleanups. Which results in
5180 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5181 if (seen_error ())
5182 return;
5184 if (gimple_conditional_context ())
5186 /* If we're in a conditional context, this is more complex. We only
5187 want to run the cleanup if we actually ran the initialization that
5188 necessitates it, but we want to run it after the end of the
5189 conditional context. So we wrap the try/finally around the
5190 condition and use a flag to determine whether or not to actually
5191 run the destructor. Thus
5193 test ? f(A()) : 0
5195 becomes (approximately)
5197 flag = 0;
5198 try {
5199 if (test) { A::A(temp); flag = 1; val = f(temp); }
5200 else { val = 0; }
5201 } finally {
5202 if (flag) A::~A(temp);
5206 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5207 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5208 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5210 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5211 gimplify_stmt (&cleanup, &cleanup_stmts);
5212 wce = gimple_build_wce (cleanup_stmts);
5214 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5215 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5216 gimplify_seq_add_stmt (pre_p, ftrue);
5218 /* Because of this manipulation, and the EH edges that jump
5219 threading cannot redirect, the temporary (VAR) will appear
5220 to be used uninitialized. Don't warn. */
5221 TREE_NO_WARNING (var) = 1;
5223 else
5225 gimplify_stmt (&cleanup, &cleanup_stmts);
5226 wce = gimple_build_wce (cleanup_stmts);
5227 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5228 gimplify_seq_add_stmt (pre_p, wce);
5232 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5234 static enum gimplify_status
5235 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5237 tree targ = *expr_p;
5238 tree temp = TARGET_EXPR_SLOT (targ);
5239 tree init = TARGET_EXPR_INITIAL (targ);
5240 enum gimplify_status ret;
5242 if (init)
5244 tree cleanup = NULL_TREE;
5246 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5247 to the temps list. Handle also variable length TARGET_EXPRs. */
5248 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5250 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5251 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5252 gimplify_vla_decl (temp, pre_p);
5254 else
5255 gimple_add_tmp_var (temp);
5257 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5258 expression is supposed to initialize the slot. */
5259 if (VOID_TYPE_P (TREE_TYPE (init)))
5260 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5261 else
5263 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5264 init = init_expr;
5265 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5266 init = NULL;
5267 ggc_free (init_expr);
5269 if (ret == GS_ERROR)
5271 /* PR c++/28266 Make sure this is expanded only once. */
5272 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5273 return GS_ERROR;
5275 if (init)
5276 gimplify_and_add (init, pre_p);
5278 /* If needed, push the cleanup for the temp. */
5279 if (TARGET_EXPR_CLEANUP (targ))
5281 if (CLEANUP_EH_ONLY (targ))
5282 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5283 CLEANUP_EH_ONLY (targ), pre_p);
5284 else
5285 cleanup = TARGET_EXPR_CLEANUP (targ);
5288 /* Add a clobber for the temporary going out of scope, like
5289 gimplify_bind_expr. */
5290 if (gimplify_ctxp->in_cleanup_point_expr
5291 && needs_to_live_in_memory (temp)
5292 && flag_stack_reuse == SR_ALL)
5294 tree clobber = build_constructor (TREE_TYPE (temp),
5295 NULL);
5296 TREE_THIS_VOLATILE (clobber) = true;
5297 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5298 if (cleanup)
5299 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5300 clobber);
5301 else
5302 cleanup = clobber;
5305 if (cleanup)
5306 gimple_push_cleanup (temp, cleanup, false, pre_p);
5308 /* Only expand this once. */
5309 TREE_OPERAND (targ, 3) = init;
5310 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5312 else
5313 /* We should have expanded this before. */
5314 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5316 *expr_p = temp;
5317 return GS_OK;
5320 /* Gimplification of expression trees. */
5322 /* Gimplify an expression which appears at statement context. The
5323 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5324 NULL, a new sequence is allocated.
5326 Return true if we actually added a statement to the queue. */
5328 bool
5329 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5331 gimple_seq_node last;
5333 last = gimple_seq_last (*seq_p);
5334 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5335 return last != gimple_seq_last (*seq_p);
5338 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5339 to CTX. If entries already exist, force them to be some flavor of private.
5340 If there is no enclosing parallel, do nothing. */
5342 void
5343 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5345 splay_tree_node n;
5347 if (decl == NULL || !DECL_P (decl))
5348 return;
5352 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5353 if (n != NULL)
5355 if (n->value & GOVD_SHARED)
5356 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5357 else if (n->value & GOVD_MAP)
5358 n->value |= GOVD_MAP_TO_ONLY;
5359 else
5360 return;
5362 else if (ctx->region_type == ORT_TARGET)
5363 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5364 else if (ctx->region_type != ORT_WORKSHARE
5365 && ctx->region_type != ORT_SIMD
5366 && ctx->region_type != ORT_TARGET_DATA)
5367 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5369 ctx = ctx->outer_context;
5371 while (ctx);
5374 /* Similarly for each of the type sizes of TYPE. */
5376 static void
5377 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5379 if (type == NULL || type == error_mark_node)
5380 return;
5381 type = TYPE_MAIN_VARIANT (type);
5383 if (pointer_set_insert (ctx->privatized_types, type))
5384 return;
5386 switch (TREE_CODE (type))
5388 case INTEGER_TYPE:
5389 case ENUMERAL_TYPE:
5390 case BOOLEAN_TYPE:
5391 case REAL_TYPE:
5392 case FIXED_POINT_TYPE:
5393 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5394 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5395 break;
5397 case ARRAY_TYPE:
5398 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5399 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5400 break;
5402 case RECORD_TYPE:
5403 case UNION_TYPE:
5404 case QUAL_UNION_TYPE:
5406 tree field;
5407 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5408 if (TREE_CODE (field) == FIELD_DECL)
5410 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5411 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5414 break;
5416 case POINTER_TYPE:
5417 case REFERENCE_TYPE:
5418 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5419 break;
5421 default:
5422 break;
5425 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5426 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5427 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5430 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5432 static void
5433 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5435 splay_tree_node n;
5436 unsigned int nflags;
5437 tree t;
5439 if (error_operand_p (decl))
5440 return;
5442 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5443 there are constructors involved somewhere. */
5444 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5445 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5446 flags |= GOVD_SEEN;
5448 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5449 if (n != NULL && n->value != GOVD_ALIGNED)
5451 /* We shouldn't be re-adding the decl with the same data
5452 sharing class. */
5453 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5454 /* The only combination of data sharing classes we should see is
5455 FIRSTPRIVATE and LASTPRIVATE. */
5456 nflags = n->value | flags;
5457 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5458 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5459 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5460 n->value = nflags;
5461 return;
5464 /* When adding a variable-sized variable, we have to handle all sorts
5465 of additional bits of data: the pointer replacement variable, and
5466 the parameters of the type. */
5467 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5469 /* Add the pointer replacement variable as PRIVATE if the variable
5470 replacement is private, else FIRSTPRIVATE since we'll need the
5471 address of the original variable either for SHARED, or for the
5472 copy into or out of the context. */
5473 if (!(flags & GOVD_LOCAL))
5475 nflags = flags & GOVD_MAP
5476 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5477 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5478 nflags |= flags & GOVD_SEEN;
5479 t = DECL_VALUE_EXPR (decl);
5480 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5481 t = TREE_OPERAND (t, 0);
5482 gcc_assert (DECL_P (t));
5483 omp_add_variable (ctx, t, nflags);
5486 /* Add all of the variable and type parameters (which should have
5487 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5488 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5489 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5490 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5492 /* The variable-sized variable itself is never SHARED, only some form
5493 of PRIVATE. The sharing would take place via the pointer variable
5494 which we remapped above. */
5495 if (flags & GOVD_SHARED)
5496 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5497 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5499 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5500 alloca statement we generate for the variable, so make sure it
5501 is available. This isn't automatically needed for the SHARED
5502 case, since we won't be allocating local storage then.
5503 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5504 in this case omp_notice_variable will be called later
5505 on when it is gimplified. */
5506 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5507 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5508 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5510 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5511 && lang_hooks.decls.omp_privatize_by_reference (decl))
5513 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5515 /* Similar to the direct variable sized case above, we'll need the
5516 size of references being privatized. */
5517 if ((flags & GOVD_SHARED) == 0)
5519 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5520 if (TREE_CODE (t) != INTEGER_CST)
5521 omp_notice_variable (ctx, t, true);
5525 if (n != NULL)
5526 n->value |= flags;
5527 else
5528 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5531 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5532 This just prints out diagnostics about threadprivate variable uses
5533 in untied tasks. If DECL2 is non-NULL, prevent this warning
5534 on that variable. */
5536 static bool
5537 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5538 tree decl2)
5540 splay_tree_node n;
5541 struct gimplify_omp_ctx *octx;
5543 for (octx = ctx; octx; octx = octx->outer_context)
5544 if (octx->region_type == ORT_TARGET)
5546 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5547 if (n == NULL)
5549 error ("threadprivate variable %qE used in target region",
5550 DECL_NAME (decl));
5551 error_at (octx->location, "enclosing target region");
5552 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5554 if (decl2)
5555 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5558 if (ctx->region_type != ORT_UNTIED_TASK)
5559 return false;
5560 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5561 if (n == NULL)
5563 error ("threadprivate variable %qE used in untied task",
5564 DECL_NAME (decl));
5565 error_at (ctx->location, "enclosing task");
5566 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5568 if (decl2)
5569 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5570 return false;
5573 /* Record the fact that DECL was used within the OpenMP context CTX.
5574 IN_CODE is true when real code uses DECL, and false when we should
5575 merely emit default(none) errors. Return true if DECL is going to
5576 be remapped and thus DECL shouldn't be gimplified into its
5577 DECL_VALUE_EXPR (if any). */
5579 static bool
5580 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5582 splay_tree_node n;
5583 unsigned flags = in_code ? GOVD_SEEN : 0;
5584 bool ret = false, shared;
5586 if (error_operand_p (decl))
5587 return false;
5589 /* Threadprivate variables are predetermined. */
5590 if (is_global_var (decl))
5592 if (DECL_THREAD_LOCAL_P (decl))
5593 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5595 if (DECL_HAS_VALUE_EXPR_P (decl))
5597 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5599 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5600 return omp_notice_threadprivate_variable (ctx, decl, value);
5604 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5605 if (ctx->region_type == ORT_TARGET)
5607 if (n == NULL)
5609 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5611 error ("%qD referenced in target region does not have "
5612 "a mappable type", decl);
5613 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5615 else
5616 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5618 else
5619 n->value |= flags;
5620 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5621 goto do_outer;
5624 if (n == NULL)
5626 enum omp_clause_default_kind default_kind, kind;
5627 struct gimplify_omp_ctx *octx;
5629 if (ctx->region_type == ORT_WORKSHARE
5630 || ctx->region_type == ORT_SIMD
5631 || ctx->region_type == ORT_TARGET_DATA)
5632 goto do_outer;
5634 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5635 remapped firstprivate instead of shared. To some extent this is
5636 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5637 default_kind = ctx->default_kind;
5638 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5639 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5640 default_kind = kind;
5642 switch (default_kind)
5644 case OMP_CLAUSE_DEFAULT_NONE:
5645 if ((ctx->region_type & ORT_TASK) != 0)
5647 error ("%qE not specified in enclosing task",
5648 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5649 error_at (ctx->location, "enclosing task");
5651 else if (ctx->region_type == ORT_TEAMS)
5653 error ("%qE not specified in enclosing teams construct",
5654 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5655 error_at (ctx->location, "enclosing teams construct");
5657 else
5659 error ("%qE not specified in enclosing parallel",
5660 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5661 error_at (ctx->location, "enclosing parallel");
5663 /* FALLTHRU */
5664 case OMP_CLAUSE_DEFAULT_SHARED:
5665 flags |= GOVD_SHARED;
5666 break;
5667 case OMP_CLAUSE_DEFAULT_PRIVATE:
5668 flags |= GOVD_PRIVATE;
5669 break;
5670 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5671 flags |= GOVD_FIRSTPRIVATE;
5672 break;
5673 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5674 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5675 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5676 if (ctx->outer_context)
5677 omp_notice_variable (ctx->outer_context, decl, in_code);
5678 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5680 splay_tree_node n2;
5682 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5683 continue;
5684 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5685 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5687 flags |= GOVD_FIRSTPRIVATE;
5688 break;
5690 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5691 break;
5693 if (flags & GOVD_FIRSTPRIVATE)
5694 break;
5695 if (octx == NULL
5696 && (TREE_CODE (decl) == PARM_DECL
5697 || (!is_global_var (decl)
5698 && DECL_CONTEXT (decl) == current_function_decl)))
5700 flags |= GOVD_FIRSTPRIVATE;
5701 break;
5703 flags |= GOVD_SHARED;
5704 break;
5705 default:
5706 gcc_unreachable ();
5709 if ((flags & GOVD_PRIVATE)
5710 && lang_hooks.decls.omp_private_outer_ref (decl))
5711 flags |= GOVD_PRIVATE_OUTER_REF;
5713 omp_add_variable (ctx, decl, flags);
5715 shared = (flags & GOVD_SHARED) != 0;
5716 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5717 goto do_outer;
5720 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5721 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5722 && DECL_SIZE (decl)
5723 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5725 splay_tree_node n2;
5726 tree t = DECL_VALUE_EXPR (decl);
5727 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5728 t = TREE_OPERAND (t, 0);
5729 gcc_assert (DECL_P (t));
5730 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5731 n2->value |= GOVD_SEEN;
5734 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5735 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5737 /* If nothing changed, there's nothing left to do. */
5738 if ((n->value & flags) == flags)
5739 return ret;
5740 flags |= n->value;
5741 n->value = flags;
5743 do_outer:
5744 /* If the variable is private in the current context, then we don't
5745 need to propagate anything to an outer context. */
5746 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5747 return ret;
5748 if (ctx->outer_context
5749 && omp_notice_variable (ctx->outer_context, decl, in_code))
5750 return true;
5751 return ret;
5754 /* Verify that DECL is private within CTX. If there's specific information
5755 to the contrary in the innermost scope, generate an error. */
5757 static bool
5758 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, bool simd)
5760 splay_tree_node n;
5762 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5763 if (n != NULL)
5765 if (n->value & GOVD_SHARED)
5767 if (ctx == gimplify_omp_ctxp)
5769 if (simd)
5770 error ("iteration variable %qE is predetermined linear",
5771 DECL_NAME (decl));
5772 else
5773 error ("iteration variable %qE should be private",
5774 DECL_NAME (decl));
5775 n->value = GOVD_PRIVATE;
5776 return true;
5778 else
5779 return false;
5781 else if ((n->value & GOVD_EXPLICIT) != 0
5782 && (ctx == gimplify_omp_ctxp
5783 || (ctx->region_type == ORT_COMBINED_PARALLEL
5784 && gimplify_omp_ctxp->outer_context == ctx)))
5786 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5787 error ("iteration variable %qE should not be firstprivate",
5788 DECL_NAME (decl));
5789 else if ((n->value & GOVD_REDUCTION) != 0)
5790 error ("iteration variable %qE should not be reduction",
5791 DECL_NAME (decl));
5792 else if (simd && (n->value & GOVD_LASTPRIVATE) != 0)
5793 error ("iteration variable %qE should not be lastprivate",
5794 DECL_NAME (decl));
5795 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5796 error ("iteration variable %qE should not be private",
5797 DECL_NAME (decl));
5798 else if (simd && (n->value & GOVD_LINEAR) != 0)
5799 error ("iteration variable %qE is predetermined linear",
5800 DECL_NAME (decl));
5802 return (ctx == gimplify_omp_ctxp
5803 || (ctx->region_type == ORT_COMBINED_PARALLEL
5804 && gimplify_omp_ctxp->outer_context == ctx));
5807 if (ctx->region_type != ORT_WORKSHARE
5808 && ctx->region_type != ORT_SIMD)
5809 return false;
5810 else if (ctx->outer_context)
5811 return omp_is_private (ctx->outer_context, decl, simd);
5812 return false;
5815 /* Return true if DECL is private within a parallel region
5816 that binds to the current construct's context or in parallel
5817 region's REDUCTION clause. */
5819 static bool
5820 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5822 splay_tree_node n;
5826 ctx = ctx->outer_context;
5827 if (ctx == NULL)
5828 return !(is_global_var (decl)
5829 /* References might be private, but might be shared too,
5830 when checking for copyprivate, assume they might be
5831 private, otherwise assume they might be shared. */
5832 || (!copyprivate
5833 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5835 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5836 continue;
5838 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5839 if (n != NULL)
5840 return (n->value & GOVD_SHARED) == 0;
5842 while (ctx->region_type == ORT_WORKSHARE
5843 || ctx->region_type == ORT_SIMD);
5844 return false;
5847 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5848 and previous omp contexts. */
5850 static void
5851 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5852 enum omp_region_type region_type)
5854 struct gimplify_omp_ctx *ctx, *outer_ctx;
5855 tree c;
5857 ctx = new_omp_context (region_type);
5858 outer_ctx = ctx->outer_context;
5860 while ((c = *list_p) != NULL)
5862 bool remove = false;
5863 bool notice_outer = true;
5864 const char *check_non_private = NULL;
5865 unsigned int flags;
5866 tree decl;
5868 switch (OMP_CLAUSE_CODE (c))
5870 case OMP_CLAUSE_PRIVATE:
5871 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5872 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5874 flags |= GOVD_PRIVATE_OUTER_REF;
5875 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5877 else
5878 notice_outer = false;
5879 goto do_add;
5880 case OMP_CLAUSE_SHARED:
5881 flags = GOVD_SHARED | GOVD_EXPLICIT;
5882 goto do_add;
5883 case OMP_CLAUSE_FIRSTPRIVATE:
5884 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5885 check_non_private = "firstprivate";
5886 goto do_add;
5887 case OMP_CLAUSE_LASTPRIVATE:
5888 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5889 check_non_private = "lastprivate";
5890 goto do_add;
5891 case OMP_CLAUSE_REDUCTION:
5892 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5893 check_non_private = "reduction";
5894 goto do_add;
5895 case OMP_CLAUSE_LINEAR:
5896 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5897 is_gimple_val, fb_rvalue) == GS_ERROR)
5899 remove = true;
5900 break;
5902 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5903 goto do_add;
5905 case OMP_CLAUSE_MAP:
5906 if (OMP_CLAUSE_SIZE (c)
5907 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5908 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5910 remove = true;
5911 break;
5913 decl = OMP_CLAUSE_DECL (c);
5914 if (!DECL_P (decl))
5916 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
5917 NULL, is_gimple_lvalue, fb_lvalue)
5918 == GS_ERROR)
5920 remove = true;
5921 break;
5923 break;
5925 flags = GOVD_MAP | GOVD_EXPLICIT;
5926 goto do_add;
5928 case OMP_CLAUSE_DEPEND:
5929 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
5931 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
5932 NULL, is_gimple_val, fb_rvalue);
5933 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
5935 if (error_operand_p (OMP_CLAUSE_DECL (c)))
5937 remove = true;
5938 break;
5940 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
5941 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
5942 is_gimple_val, fb_rvalue) == GS_ERROR)
5944 remove = true;
5945 break;
5947 break;
5949 case OMP_CLAUSE_TO:
5950 case OMP_CLAUSE_FROM:
5951 if (OMP_CLAUSE_SIZE (c)
5952 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5953 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5955 remove = true;
5956 break;
5958 decl = OMP_CLAUSE_DECL (c);
5959 if (error_operand_p (decl))
5961 remove = true;
5962 break;
5964 if (!DECL_P (decl))
5966 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
5967 NULL, is_gimple_lvalue, fb_lvalue)
5968 == GS_ERROR)
5970 remove = true;
5971 break;
5973 break;
5975 goto do_notice;
5977 do_add:
5978 decl = OMP_CLAUSE_DECL (c);
5979 if (error_operand_p (decl))
5981 remove = true;
5982 break;
5984 omp_add_variable (ctx, decl, flags);
5985 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5986 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5988 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
5989 GOVD_LOCAL | GOVD_SEEN);
5990 gimplify_omp_ctxp = ctx;
5991 push_gimplify_context ();
5993 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
5994 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
5996 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
5997 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
5998 pop_gimplify_context
5999 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6000 push_gimplify_context ();
6001 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6002 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6003 pop_gimplify_context
6004 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6005 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6006 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6008 gimplify_omp_ctxp = outer_ctx;
6010 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6011 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6013 gimplify_omp_ctxp = ctx;
6014 push_gimplify_context ();
6015 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6017 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6018 NULL, NULL);
6019 TREE_SIDE_EFFECTS (bind) = 1;
6020 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6021 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6023 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6024 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6025 pop_gimplify_context
6026 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6027 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6029 gimplify_omp_ctxp = outer_ctx;
6031 if (notice_outer)
6032 goto do_notice;
6033 break;
6035 case OMP_CLAUSE_COPYIN:
6036 case OMP_CLAUSE_COPYPRIVATE:
6037 decl = OMP_CLAUSE_DECL (c);
6038 if (error_operand_p (decl))
6040 remove = true;
6041 break;
6043 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6044 && !remove
6045 && !omp_check_private (ctx, decl, true))
6047 remove = true;
6048 if (is_global_var (decl))
6050 if (DECL_THREAD_LOCAL_P (decl))
6051 remove = false;
6052 else if (DECL_HAS_VALUE_EXPR_P (decl))
6054 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6056 if (value
6057 && DECL_P (value)
6058 && DECL_THREAD_LOCAL_P (value))
6059 remove = false;
6062 if (remove)
6063 error_at (OMP_CLAUSE_LOCATION (c),
6064 "copyprivate variable %qE is not threadprivate"
6065 " or private in outer context", DECL_NAME (decl));
6067 do_notice:
6068 if (outer_ctx)
6069 omp_notice_variable (outer_ctx, decl, true);
6070 if (check_non_private
6071 && region_type == ORT_WORKSHARE
6072 && omp_check_private (ctx, decl, false))
6074 error ("%s variable %qE is private in outer context",
6075 check_non_private, DECL_NAME (decl));
6076 remove = true;
6078 break;
6080 case OMP_CLAUSE_FINAL:
6081 case OMP_CLAUSE_IF:
6082 OMP_CLAUSE_OPERAND (c, 0)
6083 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6084 /* Fall through. */
6086 case OMP_CLAUSE_SCHEDULE:
6087 case OMP_CLAUSE_NUM_THREADS:
6088 case OMP_CLAUSE_NUM_TEAMS:
6089 case OMP_CLAUSE_THREAD_LIMIT:
6090 case OMP_CLAUSE_DIST_SCHEDULE:
6091 case OMP_CLAUSE_DEVICE:
6092 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6093 is_gimple_val, fb_rvalue) == GS_ERROR)
6094 remove = true;
6095 break;
6097 case OMP_CLAUSE_NOWAIT:
6098 case OMP_CLAUSE_ORDERED:
6099 case OMP_CLAUSE_UNTIED:
6100 case OMP_CLAUSE_COLLAPSE:
6101 case OMP_CLAUSE_MERGEABLE:
6102 case OMP_CLAUSE_PROC_BIND:
6103 case OMP_CLAUSE_SAFELEN:
6104 break;
6106 case OMP_CLAUSE_ALIGNED:
6107 decl = OMP_CLAUSE_DECL (c);
6108 if (error_operand_p (decl))
6110 remove = true;
6111 break;
6113 if (!is_global_var (decl)
6114 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6115 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6116 break;
6118 case OMP_CLAUSE_DEFAULT:
6119 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6120 break;
6122 default:
6123 gcc_unreachable ();
6126 if (remove)
6127 *list_p = OMP_CLAUSE_CHAIN (c);
6128 else
6129 list_p = &OMP_CLAUSE_CHAIN (c);
6132 gimplify_omp_ctxp = ctx;
6135 /* For all variables that were not actually used within the context,
6136 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6138 static int
6139 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6141 tree *list_p = (tree *) data;
6142 tree decl = (tree) n->key;
6143 unsigned flags = n->value;
6144 enum omp_clause_code code;
6145 tree clause;
6146 bool private_debug;
6148 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6149 return 0;
6150 if ((flags & GOVD_SEEN) == 0)
6151 return 0;
6152 if (flags & GOVD_DEBUG_PRIVATE)
6154 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6155 private_debug = true;
6157 else if (flags & GOVD_MAP)
6158 private_debug = false;
6159 else
6160 private_debug
6161 = lang_hooks.decls.omp_private_debug_clause (decl,
6162 !!(flags & GOVD_SHARED));
6163 if (private_debug)
6164 code = OMP_CLAUSE_PRIVATE;
6165 else if (flags & GOVD_MAP)
6166 code = OMP_CLAUSE_MAP;
6167 else if (flags & GOVD_SHARED)
6169 if (is_global_var (decl))
6171 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6172 while (ctx != NULL)
6174 splay_tree_node on
6175 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6176 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6177 | GOVD_PRIVATE | GOVD_REDUCTION
6178 | GOVD_LINEAR)) != 0)
6179 break;
6180 ctx = ctx->outer_context;
6182 if (ctx == NULL)
6183 return 0;
6185 code = OMP_CLAUSE_SHARED;
6187 else if (flags & GOVD_PRIVATE)
6188 code = OMP_CLAUSE_PRIVATE;
6189 else if (flags & GOVD_FIRSTPRIVATE)
6190 code = OMP_CLAUSE_FIRSTPRIVATE;
6191 else if (flags & GOVD_LASTPRIVATE)
6192 code = OMP_CLAUSE_LASTPRIVATE;
6193 else if (flags & GOVD_ALIGNED)
6194 return 0;
6195 else
6196 gcc_unreachable ();
6198 clause = build_omp_clause (input_location, code);
6199 OMP_CLAUSE_DECL (clause) = decl;
6200 OMP_CLAUSE_CHAIN (clause) = *list_p;
6201 if (private_debug)
6202 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6203 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6204 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6205 else if (code == OMP_CLAUSE_MAP)
6207 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6208 ? OMP_CLAUSE_MAP_TO
6209 : OMP_CLAUSE_MAP_TOFROM;
6210 if (DECL_SIZE (decl)
6211 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6213 tree decl2 = DECL_VALUE_EXPR (decl);
6214 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6215 decl2 = TREE_OPERAND (decl2, 0);
6216 gcc_assert (DECL_P (decl2));
6217 tree mem = build_simple_mem_ref (decl2);
6218 OMP_CLAUSE_DECL (clause) = mem;
6219 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6220 if (gimplify_omp_ctxp->outer_context)
6222 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6223 omp_notice_variable (ctx, decl2, true);
6224 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6226 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6227 OMP_CLAUSE_MAP);
6228 OMP_CLAUSE_DECL (nc) = decl;
6229 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6230 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6231 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6232 OMP_CLAUSE_CHAIN (clause) = nc;
6235 *list_p = clause;
6236 lang_hooks.decls.omp_finish_clause (clause);
6238 return 0;
6241 static void
6242 gimplify_adjust_omp_clauses (tree *list_p)
6244 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6245 tree c, decl;
6247 while ((c = *list_p) != NULL)
6249 splay_tree_node n;
6250 bool remove = false;
6252 switch (OMP_CLAUSE_CODE (c))
6254 case OMP_CLAUSE_PRIVATE:
6255 case OMP_CLAUSE_SHARED:
6256 case OMP_CLAUSE_FIRSTPRIVATE:
6257 case OMP_CLAUSE_LINEAR:
6258 decl = OMP_CLAUSE_DECL (c);
6259 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6260 remove = !(n->value & GOVD_SEEN);
6261 if (! remove)
6263 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6264 if ((n->value & GOVD_DEBUG_PRIVATE)
6265 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6267 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6268 || ((n->value & GOVD_DATA_SHARE_CLASS)
6269 == GOVD_PRIVATE));
6270 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6271 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6273 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6274 && ctx->outer_context
6275 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6276 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6277 && !is_global_var (decl))
6279 if (ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
6281 n = splay_tree_lookup (ctx->outer_context->variables,
6282 (splay_tree_key) decl);
6283 if (n == NULL
6284 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6286 int flags = OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6287 ? GOVD_LASTPRIVATE : GOVD_SHARED;
6288 if (n == NULL)
6289 omp_add_variable (ctx->outer_context, decl,
6290 flags | GOVD_SEEN);
6291 else
6292 n->value |= flags | GOVD_SEEN;
6295 else
6296 omp_notice_variable (ctx->outer_context, decl, true);
6299 break;
6301 case OMP_CLAUSE_LASTPRIVATE:
6302 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6303 accurately reflect the presence of a FIRSTPRIVATE clause. */
6304 decl = OMP_CLAUSE_DECL (c);
6305 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6306 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6307 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6308 break;
6310 case OMP_CLAUSE_ALIGNED:
6311 decl = OMP_CLAUSE_DECL (c);
6312 if (!is_global_var (decl))
6314 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6315 remove = n == NULL || !(n->value & GOVD_SEEN);
6316 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6318 struct gimplify_omp_ctx *octx;
6319 if (n != NULL
6320 && (n->value & (GOVD_DATA_SHARE_CLASS
6321 & ~GOVD_FIRSTPRIVATE)))
6322 remove = true;
6323 else
6324 for (octx = ctx->outer_context; octx;
6325 octx = octx->outer_context)
6327 n = splay_tree_lookup (octx->variables,
6328 (splay_tree_key) decl);
6329 if (n == NULL)
6330 continue;
6331 if (n->value & GOVD_LOCAL)
6332 break;
6333 /* We have to avoid assigning a shared variable
6334 to itself when trying to add
6335 __builtin_assume_aligned. */
6336 if (n->value & GOVD_SHARED)
6338 remove = true;
6339 break;
6344 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6346 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6347 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6348 remove = true;
6350 break;
6352 case OMP_CLAUSE_MAP:
6353 decl = OMP_CLAUSE_DECL (c);
6354 if (!DECL_P (decl))
6355 break;
6356 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6357 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6358 remove = true;
6359 else if (DECL_SIZE (decl)
6360 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6361 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6363 tree decl2 = DECL_VALUE_EXPR (decl);
6364 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6365 decl2 = TREE_OPERAND (decl2, 0);
6366 gcc_assert (DECL_P (decl2));
6367 tree mem = build_simple_mem_ref (decl2);
6368 OMP_CLAUSE_DECL (c) = mem;
6369 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6370 if (ctx->outer_context)
6372 omp_notice_variable (ctx->outer_context, decl2, true);
6373 omp_notice_variable (ctx->outer_context,
6374 OMP_CLAUSE_SIZE (c), true);
6376 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6377 OMP_CLAUSE_MAP);
6378 OMP_CLAUSE_DECL (nc) = decl;
6379 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6380 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6381 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6382 OMP_CLAUSE_CHAIN (c) = nc;
6383 c = nc;
6385 break;
6387 case OMP_CLAUSE_TO:
6388 case OMP_CLAUSE_FROM:
6389 decl = OMP_CLAUSE_DECL (c);
6390 if (!DECL_P (decl))
6391 break;
6392 if (DECL_SIZE (decl)
6393 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6395 tree decl2 = DECL_VALUE_EXPR (decl);
6396 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6397 decl2 = TREE_OPERAND (decl2, 0);
6398 gcc_assert (DECL_P (decl2));
6399 tree mem = build_simple_mem_ref (decl2);
6400 OMP_CLAUSE_DECL (c) = mem;
6401 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6402 if (ctx->outer_context)
6404 omp_notice_variable (ctx->outer_context, decl2, true);
6405 omp_notice_variable (ctx->outer_context,
6406 OMP_CLAUSE_SIZE (c), true);
6409 break;
6411 case OMP_CLAUSE_REDUCTION:
6412 case OMP_CLAUSE_COPYIN:
6413 case OMP_CLAUSE_COPYPRIVATE:
6414 case OMP_CLAUSE_IF:
6415 case OMP_CLAUSE_NUM_THREADS:
6416 case OMP_CLAUSE_NUM_TEAMS:
6417 case OMP_CLAUSE_THREAD_LIMIT:
6418 case OMP_CLAUSE_DIST_SCHEDULE:
6419 case OMP_CLAUSE_DEVICE:
6420 case OMP_CLAUSE_SCHEDULE:
6421 case OMP_CLAUSE_NOWAIT:
6422 case OMP_CLAUSE_ORDERED:
6423 case OMP_CLAUSE_DEFAULT:
6424 case OMP_CLAUSE_UNTIED:
6425 case OMP_CLAUSE_COLLAPSE:
6426 case OMP_CLAUSE_FINAL:
6427 case OMP_CLAUSE_MERGEABLE:
6428 case OMP_CLAUSE_PROC_BIND:
6429 case OMP_CLAUSE_SAFELEN:
6430 case OMP_CLAUSE_DEPEND:
6431 break;
6433 default:
6434 gcc_unreachable ();
6437 if (remove)
6438 *list_p = OMP_CLAUSE_CHAIN (c);
6439 else
6440 list_p = &OMP_CLAUSE_CHAIN (c);
6443 /* Add in any implicit data sharing. */
6444 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6446 gimplify_omp_ctxp = ctx->outer_context;
6447 delete_omp_context (ctx);
6450 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6451 gimplification of the body, as well as scanning the body for used
6452 variables. We need to do this scan now, because variable-sized
6453 decls will be decomposed during gimplification. */
6455 static void
6456 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6458 tree expr = *expr_p;
6459 gimple g;
6460 gimple_seq body = NULL;
6462 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6463 OMP_PARALLEL_COMBINED (expr)
6464 ? ORT_COMBINED_PARALLEL
6465 : ORT_PARALLEL);
6467 push_gimplify_context ();
6469 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6470 if (gimple_code (g) == GIMPLE_BIND)
6471 pop_gimplify_context (g);
6472 else
6473 pop_gimplify_context (NULL);
6475 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6477 g = gimple_build_omp_parallel (body,
6478 OMP_PARALLEL_CLAUSES (expr),
6479 NULL_TREE, NULL_TREE);
6480 if (OMP_PARALLEL_COMBINED (expr))
6481 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6482 gimplify_seq_add_stmt (pre_p, g);
6483 *expr_p = NULL_TREE;
6486 /* Gimplify the contents of an OMP_TASK statement. This involves
6487 gimplification of the body, as well as scanning the body for used
6488 variables. We need to do this scan now, because variable-sized
6489 decls will be decomposed during gimplification. */
6491 static void
6492 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6494 tree expr = *expr_p;
6495 gimple g;
6496 gimple_seq body = NULL;
6498 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6499 find_omp_clause (OMP_TASK_CLAUSES (expr),
6500 OMP_CLAUSE_UNTIED)
6501 ? ORT_UNTIED_TASK : ORT_TASK);
6503 push_gimplify_context ();
6505 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6506 if (gimple_code (g) == GIMPLE_BIND)
6507 pop_gimplify_context (g);
6508 else
6509 pop_gimplify_context (NULL);
6511 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6513 g = gimple_build_omp_task (body,
6514 OMP_TASK_CLAUSES (expr),
6515 NULL_TREE, NULL_TREE,
6516 NULL_TREE, NULL_TREE, NULL_TREE);
6517 gimplify_seq_add_stmt (pre_p, g);
6518 *expr_p = NULL_TREE;
6521 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6522 with non-NULL OMP_FOR_INIT. */
6524 static tree
6525 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6527 *walk_subtrees = 0;
6528 switch (TREE_CODE (*tp))
6530 case OMP_FOR:
6531 *walk_subtrees = 1;
6532 /* FALLTHRU */
6533 case OMP_SIMD:
6534 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6535 return *tp;
6536 break;
6537 case BIND_EXPR:
6538 case STATEMENT_LIST:
6539 case OMP_PARALLEL:
6540 *walk_subtrees = 1;
6541 break;
6542 default:
6543 break;
6545 return NULL_TREE;
6548 /* Gimplify the gross structure of an OMP_FOR statement. */
6550 static enum gimplify_status
6551 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6553 tree for_stmt, orig_for_stmt, decl, var, t;
6554 enum gimplify_status ret = GS_ALL_DONE;
6555 enum gimplify_status tret;
6556 gimple gfor;
6557 gimple_seq for_body, for_pre_body;
6558 int i;
6559 bool simd;
6560 bitmap has_decl_expr = NULL;
6562 orig_for_stmt = for_stmt = *expr_p;
6564 simd = TREE_CODE (for_stmt) == OMP_SIMD
6565 || TREE_CODE (for_stmt) == CILK_SIMD;
6566 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6567 simd ? ORT_SIMD : ORT_WORKSHARE);
6569 /* Handle OMP_FOR_INIT. */
6570 for_pre_body = NULL;
6571 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6573 has_decl_expr = BITMAP_ALLOC (NULL);
6574 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6575 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6576 == VAR_DECL)
6578 t = OMP_FOR_PRE_BODY (for_stmt);
6579 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6581 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6583 tree_stmt_iterator si;
6584 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6585 tsi_next (&si))
6587 t = tsi_stmt (si);
6588 if (TREE_CODE (t) == DECL_EXPR
6589 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6590 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6594 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6595 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6597 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6599 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6600 NULL, NULL);
6601 gcc_assert (for_stmt != NULL_TREE);
6602 gimplify_omp_ctxp->combined_loop = true;
6605 for_body = NULL;
6606 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6607 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6608 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6609 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6610 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6612 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6613 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6614 decl = TREE_OPERAND (t, 0);
6615 gcc_assert (DECL_P (decl));
6616 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6617 || POINTER_TYPE_P (TREE_TYPE (decl)));
6619 /* Make sure the iteration variable is private. */
6620 tree c = NULL_TREE;
6621 if (orig_for_stmt != for_stmt)
6622 /* Do this only on innermost construct for combined ones. */;
6623 else if (simd)
6625 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6626 (splay_tree_key)decl);
6627 omp_is_private (gimplify_omp_ctxp, decl, simd);
6628 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6629 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6630 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6632 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6633 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6634 if (has_decl_expr
6635 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6636 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6637 OMP_CLAUSE_DECL (c) = decl;
6638 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6639 OMP_FOR_CLAUSES (for_stmt) = c;
6640 omp_add_variable (gimplify_omp_ctxp, decl,
6641 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6643 else
6645 bool lastprivate
6646 = (!has_decl_expr
6647 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6648 c = build_omp_clause (input_location,
6649 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6650 : OMP_CLAUSE_PRIVATE);
6651 OMP_CLAUSE_DECL (c) = decl;
6652 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6653 omp_add_variable (gimplify_omp_ctxp, decl,
6654 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6655 | GOVD_SEEN);
6656 c = NULL_TREE;
6659 else if (omp_is_private (gimplify_omp_ctxp, decl, simd))
6660 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6661 else
6662 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6664 /* If DECL is not a gimple register, create a temporary variable to act
6665 as an iteration counter. This is valid, since DECL cannot be
6666 modified in the body of the loop. */
6667 if (orig_for_stmt != for_stmt)
6668 var = decl;
6669 else if (!is_gimple_reg (decl))
6671 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6672 TREE_OPERAND (t, 0) = var;
6674 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6676 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6678 else
6679 var = decl;
6681 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6682 is_gimple_val, fb_rvalue);
6683 ret = MIN (ret, tret);
6684 if (ret == GS_ERROR)
6685 return ret;
6687 /* Handle OMP_FOR_COND. */
6688 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6689 gcc_assert (COMPARISON_CLASS_P (t));
6690 gcc_assert (TREE_OPERAND (t, 0) == decl);
6692 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6693 is_gimple_val, fb_rvalue);
6694 ret = MIN (ret, tret);
6696 /* Handle OMP_FOR_INCR. */
6697 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6698 switch (TREE_CODE (t))
6700 case PREINCREMENT_EXPR:
6701 case POSTINCREMENT_EXPR:
6703 tree decl = TREE_OPERAND (t, 0);
6704 // c_omp_for_incr_canonicalize_ptr() should have been
6705 // called to massage things appropriately.
6706 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6708 if (orig_for_stmt != for_stmt)
6709 break;
6710 t = build_int_cst (TREE_TYPE (decl), 1);
6711 if (c)
6712 OMP_CLAUSE_LINEAR_STEP (c) = t;
6713 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6714 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6715 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6716 break;
6719 case PREDECREMENT_EXPR:
6720 case POSTDECREMENT_EXPR:
6721 if (orig_for_stmt != for_stmt)
6722 break;
6723 t = build_int_cst (TREE_TYPE (decl), -1);
6724 if (c)
6725 OMP_CLAUSE_LINEAR_STEP (c) = t;
6726 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6727 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6728 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6729 break;
6731 case MODIFY_EXPR:
6732 gcc_assert (TREE_OPERAND (t, 0) == decl);
6733 TREE_OPERAND (t, 0) = var;
6735 t = TREE_OPERAND (t, 1);
6736 switch (TREE_CODE (t))
6738 case PLUS_EXPR:
6739 if (TREE_OPERAND (t, 1) == decl)
6741 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6742 TREE_OPERAND (t, 0) = var;
6743 break;
6746 /* Fallthru. */
6747 case MINUS_EXPR:
6748 case POINTER_PLUS_EXPR:
6749 gcc_assert (TREE_OPERAND (t, 0) == decl);
6750 TREE_OPERAND (t, 0) = var;
6751 break;
6752 default:
6753 gcc_unreachable ();
6756 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6757 is_gimple_val, fb_rvalue);
6758 ret = MIN (ret, tret);
6759 if (c)
6761 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
6762 if (TREE_CODE (t) == MINUS_EXPR)
6764 t = TREE_OPERAND (t, 1);
6765 OMP_CLAUSE_LINEAR_STEP (c)
6766 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
6767 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6768 &for_pre_body, NULL,
6769 is_gimple_val, fb_rvalue);
6770 ret = MIN (ret, tret);
6773 break;
6775 default:
6776 gcc_unreachable ();
6779 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6780 && orig_for_stmt == for_stmt)
6782 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6783 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6784 && OMP_CLAUSE_DECL (c) == decl
6785 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6787 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6788 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6789 gcc_assert (TREE_OPERAND (t, 0) == var);
6790 t = TREE_OPERAND (t, 1);
6791 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6792 || TREE_CODE (t) == MINUS_EXPR
6793 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6794 gcc_assert (TREE_OPERAND (t, 0) == var);
6795 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6796 TREE_OPERAND (t, 1));
6797 gimplify_assign (decl, t,
6798 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6803 BITMAP_FREE (has_decl_expr);
6805 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
6807 if (orig_for_stmt != for_stmt)
6808 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6810 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6811 decl = TREE_OPERAND (t, 0);
6812 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6813 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6814 TREE_OPERAND (t, 0) = var;
6815 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6816 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
6817 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
6820 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
6822 int kind;
6823 switch (TREE_CODE (orig_for_stmt))
6825 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
6826 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
6827 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
6828 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
6829 default:
6830 gcc_unreachable ();
6832 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
6833 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6834 for_pre_body);
6835 if (orig_for_stmt != for_stmt)
6836 gimple_omp_for_set_combined_p (gfor, true);
6837 if (gimplify_omp_ctxp
6838 && (gimplify_omp_ctxp->combined_loop
6839 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
6840 && gimplify_omp_ctxp->outer_context
6841 && gimplify_omp_ctxp->outer_context->combined_loop)))
6843 gimple_omp_for_set_combined_into_p (gfor, true);
6844 if (gimplify_omp_ctxp->combined_loop)
6845 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
6846 else
6847 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
6850 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6852 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6853 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6854 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6855 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6856 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6857 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6858 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6859 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6862 gimplify_seq_add_stmt (pre_p, gfor);
6863 if (ret != GS_ALL_DONE)
6864 return GS_ERROR;
6865 *expr_p = NULL_TREE;
6866 return GS_ALL_DONE;
6869 /* Gimplify the gross structure of other OpenMP constructs.
6870 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
6871 and OMP_TEAMS. */
6873 static void
6874 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6876 tree expr = *expr_p;
6877 gimple stmt;
6878 gimple_seq body = NULL;
6879 enum omp_region_type ort = ORT_WORKSHARE;
6881 switch (TREE_CODE (expr))
6883 case OMP_SECTIONS:
6884 case OMP_SINGLE:
6885 break;
6886 case OMP_TARGET:
6887 ort = ORT_TARGET;
6888 break;
6889 case OMP_TARGET_DATA:
6890 ort = ORT_TARGET_DATA;
6891 break;
6892 case OMP_TEAMS:
6893 ort = ORT_TEAMS;
6894 break;
6895 default:
6896 gcc_unreachable ();
6898 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
6899 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
6901 push_gimplify_context ();
6902 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
6903 if (gimple_code (g) == GIMPLE_BIND)
6904 pop_gimplify_context (g);
6905 else
6906 pop_gimplify_context (NULL);
6907 if (ort == ORT_TARGET_DATA)
6909 gimple_seq cleanup = NULL;
6910 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
6911 g = gimple_build_call (fn, 0);
6912 gimple_seq_add_stmt (&cleanup, g);
6913 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
6914 body = NULL;
6915 gimple_seq_add_stmt (&body, g);
6918 else
6919 gimplify_and_add (OMP_BODY (expr), &body);
6920 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6922 switch (TREE_CODE (expr))
6924 case OMP_SECTIONS:
6925 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6926 break;
6927 case OMP_SINGLE:
6928 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6929 break;
6930 case OMP_TARGET:
6931 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
6932 OMP_CLAUSES (expr));
6933 break;
6934 case OMP_TARGET_DATA:
6935 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
6936 OMP_CLAUSES (expr));
6937 break;
6938 case OMP_TEAMS:
6939 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
6940 break;
6941 default:
6942 gcc_unreachable ();
6945 gimplify_seq_add_stmt (pre_p, stmt);
6946 *expr_p = NULL_TREE;
6949 /* Gimplify the gross structure of OpenMP target update construct. */
6951 static void
6952 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
6954 tree expr = *expr_p;
6955 gimple stmt;
6957 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
6958 ORT_WORKSHARE);
6959 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
6960 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
6961 OMP_TARGET_UPDATE_CLAUSES (expr));
6963 gimplify_seq_add_stmt (pre_p, stmt);
6964 *expr_p = NULL_TREE;
6967 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6968 stabilized the lhs of the atomic operation as *ADDR. Return true if
6969 EXPR is this stabilized form. */
6971 static bool
6972 goa_lhs_expr_p (tree expr, tree addr)
6974 /* Also include casts to other type variants. The C front end is fond
6975 of adding these for e.g. volatile variables. This is like
6976 STRIP_TYPE_NOPS but includes the main variant lookup. */
6977 STRIP_USELESS_TYPE_CONVERSION (expr);
6979 if (TREE_CODE (expr) == INDIRECT_REF)
6981 expr = TREE_OPERAND (expr, 0);
6982 while (expr != addr
6983 && (CONVERT_EXPR_P (expr)
6984 || TREE_CODE (expr) == NON_LVALUE_EXPR)
6985 && TREE_CODE (expr) == TREE_CODE (addr)
6986 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
6988 expr = TREE_OPERAND (expr, 0);
6989 addr = TREE_OPERAND (addr, 0);
6991 if (expr == addr)
6992 return true;
6993 return (TREE_CODE (addr) == ADDR_EXPR
6994 && TREE_CODE (expr) == ADDR_EXPR
6995 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
6997 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
6998 return true;
6999 return false;
7002 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7003 expression does not involve the lhs, evaluate it into a temporary.
7004 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7005 or -1 if an error was encountered. */
7007 static int
7008 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7009 tree lhs_var)
7011 tree expr = *expr_p;
7012 int saw_lhs;
7014 if (goa_lhs_expr_p (expr, lhs_addr))
7016 *expr_p = lhs_var;
7017 return 1;
7019 if (is_gimple_val (expr))
7020 return 0;
7022 saw_lhs = 0;
7023 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7025 case tcc_binary:
7026 case tcc_comparison:
7027 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7028 lhs_var);
7029 case tcc_unary:
7030 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7031 lhs_var);
7032 break;
7033 case tcc_expression:
7034 switch (TREE_CODE (expr))
7036 case TRUTH_ANDIF_EXPR:
7037 case TRUTH_ORIF_EXPR:
7038 case TRUTH_AND_EXPR:
7039 case TRUTH_OR_EXPR:
7040 case TRUTH_XOR_EXPR:
7041 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7042 lhs_addr, lhs_var);
7043 case TRUTH_NOT_EXPR:
7044 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7045 lhs_addr, lhs_var);
7046 break;
7047 case COMPOUND_EXPR:
7048 /* Break out any preevaluations from cp_build_modify_expr. */
7049 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7050 expr = TREE_OPERAND (expr, 1))
7051 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7052 *expr_p = expr;
7053 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7054 default:
7055 break;
7057 break;
7058 default:
7059 break;
7062 if (saw_lhs == 0)
7064 enum gimplify_status gs;
7065 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7066 if (gs != GS_ALL_DONE)
7067 saw_lhs = -1;
7070 return saw_lhs;
7073 /* Gimplify an OMP_ATOMIC statement. */
7075 static enum gimplify_status
7076 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7078 tree addr = TREE_OPERAND (*expr_p, 0);
7079 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7080 ? NULL : TREE_OPERAND (*expr_p, 1);
7081 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7082 tree tmp_load;
7083 gimple loadstmt, storestmt;
7085 tmp_load = create_tmp_reg (type, NULL);
7086 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7087 return GS_ERROR;
7089 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7090 != GS_ALL_DONE)
7091 return GS_ERROR;
7093 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7094 gimplify_seq_add_stmt (pre_p, loadstmt);
7095 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7096 != GS_ALL_DONE)
7097 return GS_ERROR;
7099 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7100 rhs = tmp_load;
7101 storestmt = gimple_build_omp_atomic_store (rhs);
7102 gimplify_seq_add_stmt (pre_p, storestmt);
7103 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7105 gimple_omp_atomic_set_seq_cst (loadstmt);
7106 gimple_omp_atomic_set_seq_cst (storestmt);
7108 switch (TREE_CODE (*expr_p))
7110 case OMP_ATOMIC_READ:
7111 case OMP_ATOMIC_CAPTURE_OLD:
7112 *expr_p = tmp_load;
7113 gimple_omp_atomic_set_need_value (loadstmt);
7114 break;
7115 case OMP_ATOMIC_CAPTURE_NEW:
7116 *expr_p = rhs;
7117 gimple_omp_atomic_set_need_value (storestmt);
7118 break;
7119 default:
7120 *expr_p = NULL;
7121 break;
7124 return GS_ALL_DONE;
7127 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7128 body, and adding some EH bits. */
7130 static enum gimplify_status
7131 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7133 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7134 gimple g;
7135 gimple_seq body = NULL;
7136 int subcode = 0;
7138 /* Wrap the transaction body in a BIND_EXPR so we have a context
7139 where to put decls for OpenMP. */
7140 if (TREE_CODE (tbody) != BIND_EXPR)
7142 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7143 TREE_SIDE_EFFECTS (bind) = 1;
7144 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7145 TRANSACTION_EXPR_BODY (expr) = bind;
7148 push_gimplify_context ();
7149 temp = voidify_wrapper_expr (*expr_p, NULL);
7151 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7152 pop_gimplify_context (g);
7154 g = gimple_build_transaction (body, NULL);
7155 if (TRANSACTION_EXPR_OUTER (expr))
7156 subcode = GTMA_IS_OUTER;
7157 else if (TRANSACTION_EXPR_RELAXED (expr))
7158 subcode = GTMA_IS_RELAXED;
7159 gimple_transaction_set_subcode (g, subcode);
7161 gimplify_seq_add_stmt (pre_p, g);
7163 if (temp)
7165 *expr_p = temp;
7166 return GS_OK;
7169 *expr_p = NULL_TREE;
7170 return GS_ALL_DONE;
7173 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7174 expression produces a value to be used as an operand inside a GIMPLE
7175 statement, the value will be stored back in *EXPR_P. This value will
7176 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7177 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7178 emitted in PRE_P and POST_P.
7180 Additionally, this process may overwrite parts of the input
7181 expression during gimplification. Ideally, it should be
7182 possible to do non-destructive gimplification.
7184 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7185 the expression needs to evaluate to a value to be used as
7186 an operand in a GIMPLE statement, this value will be stored in
7187 *EXPR_P on exit. This happens when the caller specifies one
7188 of fb_lvalue or fb_rvalue fallback flags.
7190 PRE_P will contain the sequence of GIMPLE statements corresponding
7191 to the evaluation of EXPR and all the side-effects that must
7192 be executed before the main expression. On exit, the last
7193 statement of PRE_P is the core statement being gimplified. For
7194 instance, when gimplifying 'if (++a)' the last statement in
7195 PRE_P will be 'if (t.1)' where t.1 is the result of
7196 pre-incrementing 'a'.
7198 POST_P will contain the sequence of GIMPLE statements corresponding
7199 to the evaluation of all the side-effects that must be executed
7200 after the main expression. If this is NULL, the post
7201 side-effects are stored at the end of PRE_P.
7203 The reason why the output is split in two is to handle post
7204 side-effects explicitly. In some cases, an expression may have
7205 inner and outer post side-effects which need to be emitted in
7206 an order different from the one given by the recursive
7207 traversal. For instance, for the expression (*p--)++ the post
7208 side-effects of '--' must actually occur *after* the post
7209 side-effects of '++'. However, gimplification will first visit
7210 the inner expression, so if a separate POST sequence was not
7211 used, the resulting sequence would be:
7213 1 t.1 = *p
7214 2 p = p - 1
7215 3 t.2 = t.1 + 1
7216 4 *p = t.2
7218 However, the post-decrement operation in line #2 must not be
7219 evaluated until after the store to *p at line #4, so the
7220 correct sequence should be:
7222 1 t.1 = *p
7223 2 t.2 = t.1 + 1
7224 3 *p = t.2
7225 4 p = p - 1
7227 So, by specifying a separate post queue, it is possible
7228 to emit the post side-effects in the correct order.
7229 If POST_P is NULL, an internal queue will be used. Before
7230 returning to the caller, the sequence POST_P is appended to
7231 the main output sequence PRE_P.
7233 GIMPLE_TEST_F points to a function that takes a tree T and
7234 returns nonzero if T is in the GIMPLE form requested by the
7235 caller. The GIMPLE predicates are in gimple.c.
7237 FALLBACK tells the function what sort of a temporary we want if
7238 gimplification cannot produce an expression that complies with
7239 GIMPLE_TEST_F.
7241 fb_none means that no temporary should be generated
7242 fb_rvalue means that an rvalue is OK to generate
7243 fb_lvalue means that an lvalue is OK to generate
7244 fb_either means that either is OK, but an lvalue is preferable.
7245 fb_mayfail means that gimplification may fail (in which case
7246 GS_ERROR will be returned)
7248 The return value is either GS_ERROR or GS_ALL_DONE, since this
7249 function iterates until EXPR is completely gimplified or an error
7250 occurs. */
7252 enum gimplify_status
7253 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7254 bool (*gimple_test_f) (tree), fallback_t fallback)
7256 tree tmp;
7257 gimple_seq internal_pre = NULL;
7258 gimple_seq internal_post = NULL;
7259 tree save_expr;
7260 bool is_statement;
7261 location_t saved_location;
7262 enum gimplify_status ret;
7263 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7265 save_expr = *expr_p;
7266 if (save_expr == NULL_TREE)
7267 return GS_ALL_DONE;
7269 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7270 is_statement = gimple_test_f == is_gimple_stmt;
7271 if (is_statement)
7272 gcc_assert (pre_p);
7274 /* Consistency checks. */
7275 if (gimple_test_f == is_gimple_reg)
7276 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7277 else if (gimple_test_f == is_gimple_val
7278 || gimple_test_f == is_gimple_call_addr
7279 || gimple_test_f == is_gimple_condexpr
7280 || gimple_test_f == is_gimple_mem_rhs
7281 || gimple_test_f == is_gimple_mem_rhs_or_call
7282 || gimple_test_f == is_gimple_reg_rhs
7283 || gimple_test_f == is_gimple_reg_rhs_or_call
7284 || gimple_test_f == is_gimple_asm_val
7285 || gimple_test_f == is_gimple_mem_ref_addr)
7286 gcc_assert (fallback & fb_rvalue);
7287 else if (gimple_test_f == is_gimple_min_lval
7288 || gimple_test_f == is_gimple_lvalue)
7289 gcc_assert (fallback & fb_lvalue);
7290 else if (gimple_test_f == is_gimple_addressable)
7291 gcc_assert (fallback & fb_either);
7292 else if (gimple_test_f == is_gimple_stmt)
7293 gcc_assert (fallback == fb_none);
7294 else
7296 /* We should have recognized the GIMPLE_TEST_F predicate to
7297 know what kind of fallback to use in case a temporary is
7298 needed to hold the value or address of *EXPR_P. */
7299 gcc_unreachable ();
7302 /* We used to check the predicate here and return immediately if it
7303 succeeds. This is wrong; the design is for gimplification to be
7304 idempotent, and for the predicates to only test for valid forms, not
7305 whether they are fully simplified. */
7306 if (pre_p == NULL)
7307 pre_p = &internal_pre;
7309 if (post_p == NULL)
7310 post_p = &internal_post;
7312 /* Remember the last statements added to PRE_P and POST_P. Every
7313 new statement added by the gimplification helpers needs to be
7314 annotated with location information. To centralize the
7315 responsibility, we remember the last statement that had been
7316 added to both queues before gimplifying *EXPR_P. If
7317 gimplification produces new statements in PRE_P and POST_P, those
7318 statements will be annotated with the same location information
7319 as *EXPR_P. */
7320 pre_last_gsi = gsi_last (*pre_p);
7321 post_last_gsi = gsi_last (*post_p);
7323 saved_location = input_location;
7324 if (save_expr != error_mark_node
7325 && EXPR_HAS_LOCATION (*expr_p))
7326 input_location = EXPR_LOCATION (*expr_p);
7328 /* Loop over the specific gimplifiers until the toplevel node
7329 remains the same. */
7332 /* Strip away as many useless type conversions as possible
7333 at the toplevel. */
7334 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7336 /* Remember the expr. */
7337 save_expr = *expr_p;
7339 /* Die, die, die, my darling. */
7340 if (save_expr == error_mark_node
7341 || (TREE_TYPE (save_expr)
7342 && TREE_TYPE (save_expr) == error_mark_node))
7344 ret = GS_ERROR;
7345 break;
7348 /* Do any language-specific gimplification. */
7349 ret = ((enum gimplify_status)
7350 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7351 if (ret == GS_OK)
7353 if (*expr_p == NULL_TREE)
7354 break;
7355 if (*expr_p != save_expr)
7356 continue;
7358 else if (ret != GS_UNHANDLED)
7359 break;
7361 /* Make sure that all the cases set 'ret' appropriately. */
7362 ret = GS_UNHANDLED;
7363 switch (TREE_CODE (*expr_p))
7365 /* First deal with the special cases. */
7367 case POSTINCREMENT_EXPR:
7368 case POSTDECREMENT_EXPR:
7369 case PREINCREMENT_EXPR:
7370 case PREDECREMENT_EXPR:
7371 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7372 fallback != fb_none,
7373 TREE_TYPE (*expr_p));
7374 break;
7376 case ARRAY_REF:
7377 case ARRAY_RANGE_REF:
7378 case REALPART_EXPR:
7379 case IMAGPART_EXPR:
7380 case COMPONENT_REF:
7381 case VIEW_CONVERT_EXPR:
7382 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7383 fallback ? fallback : fb_rvalue);
7384 break;
7386 case COND_EXPR:
7387 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7389 /* C99 code may assign to an array in a structure value of a
7390 conditional expression, and this has undefined behavior
7391 only on execution, so create a temporary if an lvalue is
7392 required. */
7393 if (fallback == fb_lvalue)
7395 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7396 mark_addressable (*expr_p);
7397 ret = GS_OK;
7399 break;
7401 case CALL_EXPR:
7402 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7404 /* C99 code may assign to an array in a structure returned
7405 from a function, and this has undefined behavior only on
7406 execution, so create a temporary if an lvalue is
7407 required. */
7408 if (fallback == fb_lvalue)
7410 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7411 mark_addressable (*expr_p);
7412 ret = GS_OK;
7414 break;
7416 case TREE_LIST:
7417 gcc_unreachable ();
7419 case COMPOUND_EXPR:
7420 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7421 break;
7423 case COMPOUND_LITERAL_EXPR:
7424 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7425 gimple_test_f, fallback);
7426 break;
7428 case MODIFY_EXPR:
7429 case INIT_EXPR:
7430 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7431 fallback != fb_none);
7432 break;
7434 case TRUTH_ANDIF_EXPR:
7435 case TRUTH_ORIF_EXPR:
7437 /* Preserve the original type of the expression and the
7438 source location of the outer expression. */
7439 tree org_type = TREE_TYPE (*expr_p);
7440 *expr_p = gimple_boolify (*expr_p);
7441 *expr_p = build3_loc (input_location, COND_EXPR,
7442 org_type, *expr_p,
7443 fold_convert_loc
7444 (input_location,
7445 org_type, boolean_true_node),
7446 fold_convert_loc
7447 (input_location,
7448 org_type, boolean_false_node));
7449 ret = GS_OK;
7450 break;
7453 case TRUTH_NOT_EXPR:
7455 tree type = TREE_TYPE (*expr_p);
7456 /* The parsers are careful to generate TRUTH_NOT_EXPR
7457 only with operands that are always zero or one.
7458 We do not fold here but handle the only interesting case
7459 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7460 *expr_p = gimple_boolify (*expr_p);
7461 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7462 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7463 TREE_TYPE (*expr_p),
7464 TREE_OPERAND (*expr_p, 0));
7465 else
7466 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7467 TREE_TYPE (*expr_p),
7468 TREE_OPERAND (*expr_p, 0),
7469 build_int_cst (TREE_TYPE (*expr_p), 1));
7470 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7471 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7472 ret = GS_OK;
7473 break;
7476 case ADDR_EXPR:
7477 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7478 break;
7480 case ANNOTATE_EXPR:
7482 tree cond = TREE_OPERAND (*expr_p, 0);
7483 tree id = TREE_OPERAND (*expr_p, 1);
7484 tree tmp = create_tmp_var_raw (TREE_TYPE(cond), NULL);
7485 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7486 gimple call = gimple_build_call_internal (IFN_ANNOTATE, 2,
7487 cond, id);
7488 gimple_call_set_lhs (call, tmp);
7489 gimplify_seq_add_stmt (pre_p, call);
7490 *expr_p = tmp;
7491 ret = GS_ALL_DONE;
7492 break;
7495 case VA_ARG_EXPR:
7496 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7497 break;
7499 CASE_CONVERT:
7500 if (IS_EMPTY_STMT (*expr_p))
7502 ret = GS_ALL_DONE;
7503 break;
7506 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7507 || fallback == fb_none)
7509 /* Just strip a conversion to void (or in void context) and
7510 try again. */
7511 *expr_p = TREE_OPERAND (*expr_p, 0);
7512 ret = GS_OK;
7513 break;
7516 ret = gimplify_conversion (expr_p);
7517 if (ret == GS_ERROR)
7518 break;
7519 if (*expr_p != save_expr)
7520 break;
7521 /* FALLTHRU */
7523 case FIX_TRUNC_EXPR:
7524 /* unary_expr: ... | '(' cast ')' val | ... */
7525 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7526 is_gimple_val, fb_rvalue);
7527 recalculate_side_effects (*expr_p);
7528 break;
7530 case INDIRECT_REF:
7532 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7533 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7534 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7536 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7537 if (*expr_p != save_expr)
7539 ret = GS_OK;
7540 break;
7543 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7544 is_gimple_reg, fb_rvalue);
7545 if (ret == GS_ERROR)
7546 break;
7548 recalculate_side_effects (*expr_p);
7549 *expr_p = fold_build2_loc (input_location, MEM_REF,
7550 TREE_TYPE (*expr_p),
7551 TREE_OPERAND (*expr_p, 0),
7552 build_int_cst (saved_ptr_type, 0));
7553 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7554 TREE_THIS_NOTRAP (*expr_p) = notrap;
7555 ret = GS_OK;
7556 break;
7559 /* We arrive here through the various re-gimplifcation paths. */
7560 case MEM_REF:
7561 /* First try re-folding the whole thing. */
7562 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7563 TREE_OPERAND (*expr_p, 0),
7564 TREE_OPERAND (*expr_p, 1));
7565 if (tmp)
7567 *expr_p = tmp;
7568 recalculate_side_effects (*expr_p);
7569 ret = GS_OK;
7570 break;
7572 /* Avoid re-gimplifying the address operand if it is already
7573 in suitable form. Re-gimplifying would mark the address
7574 operand addressable. Always gimplify when not in SSA form
7575 as we still may have to gimplify decls with value-exprs. */
7576 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7577 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7579 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7580 is_gimple_mem_ref_addr, fb_rvalue);
7581 if (ret == GS_ERROR)
7582 break;
7584 recalculate_side_effects (*expr_p);
7585 ret = GS_ALL_DONE;
7586 break;
7588 /* Constants need not be gimplified. */
7589 case INTEGER_CST:
7590 case REAL_CST:
7591 case FIXED_CST:
7592 case STRING_CST:
7593 case COMPLEX_CST:
7594 case VECTOR_CST:
7595 /* Drop the overflow flag on constants, we do not want
7596 that in the GIMPLE IL. */
7597 if (TREE_OVERFLOW_P (*expr_p))
7598 *expr_p = drop_tree_overflow (*expr_p);
7599 ret = GS_ALL_DONE;
7600 break;
7602 case CONST_DECL:
7603 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7604 CONST_DECL node. Otherwise the decl is replaceable by its
7605 value. */
7606 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7607 if (fallback & fb_lvalue)
7608 ret = GS_ALL_DONE;
7609 else
7611 *expr_p = DECL_INITIAL (*expr_p);
7612 ret = GS_OK;
7614 break;
7616 case DECL_EXPR:
7617 ret = gimplify_decl_expr (expr_p, pre_p);
7618 break;
7620 case BIND_EXPR:
7621 ret = gimplify_bind_expr (expr_p, pre_p);
7622 break;
7624 case LOOP_EXPR:
7625 ret = gimplify_loop_expr (expr_p, pre_p);
7626 break;
7628 case SWITCH_EXPR:
7629 ret = gimplify_switch_expr (expr_p, pre_p);
7630 break;
7632 case EXIT_EXPR:
7633 ret = gimplify_exit_expr (expr_p);
7634 break;
7636 case GOTO_EXPR:
7637 /* If the target is not LABEL, then it is a computed jump
7638 and the target needs to be gimplified. */
7639 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7641 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7642 NULL, is_gimple_val, fb_rvalue);
7643 if (ret == GS_ERROR)
7644 break;
7646 gimplify_seq_add_stmt (pre_p,
7647 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7648 ret = GS_ALL_DONE;
7649 break;
7651 case PREDICT_EXPR:
7652 gimplify_seq_add_stmt (pre_p,
7653 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7654 PREDICT_EXPR_OUTCOME (*expr_p)));
7655 ret = GS_ALL_DONE;
7656 break;
7658 case LABEL_EXPR:
7659 ret = GS_ALL_DONE;
7660 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7661 == current_function_decl);
7662 gimplify_seq_add_stmt (pre_p,
7663 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7664 break;
7666 case CASE_LABEL_EXPR:
7667 ret = gimplify_case_label_expr (expr_p, pre_p);
7668 break;
7670 case RETURN_EXPR:
7671 ret = gimplify_return_expr (*expr_p, pre_p);
7672 break;
7674 case CONSTRUCTOR:
7675 /* Don't reduce this in place; let gimplify_init_constructor work its
7676 magic. Buf if we're just elaborating this for side effects, just
7677 gimplify any element that has side-effects. */
7678 if (fallback == fb_none)
7680 unsigned HOST_WIDE_INT ix;
7681 tree val;
7682 tree temp = NULL_TREE;
7683 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7684 if (TREE_SIDE_EFFECTS (val))
7685 append_to_statement_list (val, &temp);
7687 *expr_p = temp;
7688 ret = temp ? GS_OK : GS_ALL_DONE;
7690 /* C99 code may assign to an array in a constructed
7691 structure or union, and this has undefined behavior only
7692 on execution, so create a temporary if an lvalue is
7693 required. */
7694 else if (fallback == fb_lvalue)
7696 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7697 mark_addressable (*expr_p);
7698 ret = GS_OK;
7700 else
7701 ret = GS_ALL_DONE;
7702 break;
7704 /* The following are special cases that are not handled by the
7705 original GIMPLE grammar. */
7707 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7708 eliminated. */
7709 case SAVE_EXPR:
7710 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7711 break;
7713 case BIT_FIELD_REF:
7714 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7715 post_p, is_gimple_lvalue, fb_either);
7716 recalculate_side_effects (*expr_p);
7717 break;
7719 case TARGET_MEM_REF:
7721 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7723 if (TMR_BASE (*expr_p))
7724 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7725 post_p, is_gimple_mem_ref_addr, fb_either);
7726 if (TMR_INDEX (*expr_p))
7727 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7728 post_p, is_gimple_val, fb_rvalue);
7729 if (TMR_INDEX2 (*expr_p))
7730 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7731 post_p, is_gimple_val, fb_rvalue);
7732 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7733 ret = MIN (r0, r1);
7735 break;
7737 case NON_LVALUE_EXPR:
7738 /* This should have been stripped above. */
7739 gcc_unreachable ();
7741 case ASM_EXPR:
7742 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7743 break;
7745 case TRY_FINALLY_EXPR:
7746 case TRY_CATCH_EXPR:
7748 gimple_seq eval, cleanup;
7749 gimple try_;
7751 /* Calls to destructors are generated automatically in FINALLY/CATCH
7752 block. They should have location as UNKNOWN_LOCATION. However,
7753 gimplify_call_expr will reset these call stmts to input_location
7754 if it finds stmt's location is unknown. To prevent resetting for
7755 destructors, we set the input_location to unknown.
7756 Note that this only affects the destructor calls in FINALLY/CATCH
7757 block, and will automatically reset to its original value by the
7758 end of gimplify_expr. */
7759 input_location = UNKNOWN_LOCATION;
7760 eval = cleanup = NULL;
7761 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7762 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7763 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7764 if (gimple_seq_empty_p (cleanup))
7766 gimple_seq_add_seq (pre_p, eval);
7767 ret = GS_ALL_DONE;
7768 break;
7770 try_ = gimple_build_try (eval, cleanup,
7771 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7772 ? GIMPLE_TRY_FINALLY
7773 : GIMPLE_TRY_CATCH);
7774 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7775 gimple_set_location (try_, saved_location);
7776 else
7777 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7778 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7779 gimple_try_set_catch_is_cleanup (try_,
7780 TRY_CATCH_IS_CLEANUP (*expr_p));
7781 gimplify_seq_add_stmt (pre_p, try_);
7782 ret = GS_ALL_DONE;
7783 break;
7786 case CLEANUP_POINT_EXPR:
7787 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7788 break;
7790 case TARGET_EXPR:
7791 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7792 break;
7794 case CATCH_EXPR:
7796 gimple c;
7797 gimple_seq handler = NULL;
7798 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7799 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7800 gimplify_seq_add_stmt (pre_p, c);
7801 ret = GS_ALL_DONE;
7802 break;
7805 case EH_FILTER_EXPR:
7807 gimple ehf;
7808 gimple_seq failure = NULL;
7810 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7811 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7812 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7813 gimplify_seq_add_stmt (pre_p, ehf);
7814 ret = GS_ALL_DONE;
7815 break;
7818 case OBJ_TYPE_REF:
7820 enum gimplify_status r0, r1;
7821 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7822 post_p, is_gimple_val, fb_rvalue);
7823 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7824 post_p, is_gimple_val, fb_rvalue);
7825 TREE_SIDE_EFFECTS (*expr_p) = 0;
7826 ret = MIN (r0, r1);
7828 break;
7830 case LABEL_DECL:
7831 /* We get here when taking the address of a label. We mark
7832 the label as "forced"; meaning it can never be removed and
7833 it is a potential target for any computed goto. */
7834 FORCED_LABEL (*expr_p) = 1;
7835 ret = GS_ALL_DONE;
7836 break;
7838 case STATEMENT_LIST:
7839 ret = gimplify_statement_list (expr_p, pre_p);
7840 break;
7842 case WITH_SIZE_EXPR:
7844 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7845 post_p == &internal_post ? NULL : post_p,
7846 gimple_test_f, fallback);
7847 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7848 is_gimple_val, fb_rvalue);
7849 ret = GS_ALL_DONE;
7851 break;
7853 case VAR_DECL:
7854 case PARM_DECL:
7855 ret = gimplify_var_or_parm_decl (expr_p);
7856 break;
7858 case RESULT_DECL:
7859 /* When within an OpenMP context, notice uses of variables. */
7860 if (gimplify_omp_ctxp)
7861 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7862 ret = GS_ALL_DONE;
7863 break;
7865 case SSA_NAME:
7866 /* Allow callbacks into the gimplifier during optimization. */
7867 ret = GS_ALL_DONE;
7868 break;
7870 case OMP_PARALLEL:
7871 gimplify_omp_parallel (expr_p, pre_p);
7872 ret = GS_ALL_DONE;
7873 break;
7875 case OMP_TASK:
7876 gimplify_omp_task (expr_p, pre_p);
7877 ret = GS_ALL_DONE;
7878 break;
7880 case OMP_FOR:
7881 case OMP_SIMD:
7882 case CILK_SIMD:
7883 case OMP_DISTRIBUTE:
7884 ret = gimplify_omp_for (expr_p, pre_p);
7885 break;
7887 case OMP_SECTIONS:
7888 case OMP_SINGLE:
7889 case OMP_TARGET:
7890 case OMP_TARGET_DATA:
7891 case OMP_TEAMS:
7892 gimplify_omp_workshare (expr_p, pre_p);
7893 ret = GS_ALL_DONE;
7894 break;
7896 case OMP_TARGET_UPDATE:
7897 gimplify_omp_target_update (expr_p, pre_p);
7898 ret = GS_ALL_DONE;
7899 break;
7901 case OMP_SECTION:
7902 case OMP_MASTER:
7903 case OMP_TASKGROUP:
7904 case OMP_ORDERED:
7905 case OMP_CRITICAL:
7907 gimple_seq body = NULL;
7908 gimple g;
7910 gimplify_and_add (OMP_BODY (*expr_p), &body);
7911 switch (TREE_CODE (*expr_p))
7913 case OMP_SECTION:
7914 g = gimple_build_omp_section (body);
7915 break;
7916 case OMP_MASTER:
7917 g = gimple_build_omp_master (body);
7918 break;
7919 case OMP_TASKGROUP:
7921 gimple_seq cleanup = NULL;
7922 tree fn
7923 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
7924 g = gimple_build_call (fn, 0);
7925 gimple_seq_add_stmt (&cleanup, g);
7926 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7927 body = NULL;
7928 gimple_seq_add_stmt (&body, g);
7929 g = gimple_build_omp_taskgroup (body);
7931 break;
7932 case OMP_ORDERED:
7933 g = gimple_build_omp_ordered (body);
7934 break;
7935 case OMP_CRITICAL:
7936 g = gimple_build_omp_critical (body,
7937 OMP_CRITICAL_NAME (*expr_p));
7938 break;
7939 default:
7940 gcc_unreachable ();
7942 gimplify_seq_add_stmt (pre_p, g);
7943 ret = GS_ALL_DONE;
7944 break;
7947 case OMP_ATOMIC:
7948 case OMP_ATOMIC_READ:
7949 case OMP_ATOMIC_CAPTURE_OLD:
7950 case OMP_ATOMIC_CAPTURE_NEW:
7951 ret = gimplify_omp_atomic (expr_p, pre_p);
7952 break;
7954 case TRANSACTION_EXPR:
7955 ret = gimplify_transaction (expr_p, pre_p);
7956 break;
7958 case TRUTH_AND_EXPR:
7959 case TRUTH_OR_EXPR:
7960 case TRUTH_XOR_EXPR:
7962 tree orig_type = TREE_TYPE (*expr_p);
7963 tree new_type, xop0, xop1;
7964 *expr_p = gimple_boolify (*expr_p);
7965 new_type = TREE_TYPE (*expr_p);
7966 if (!useless_type_conversion_p (orig_type, new_type))
7968 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
7969 ret = GS_OK;
7970 break;
7973 /* Boolified binary truth expressions are semantically equivalent
7974 to bitwise binary expressions. Canonicalize them to the
7975 bitwise variant. */
7976 switch (TREE_CODE (*expr_p))
7978 case TRUTH_AND_EXPR:
7979 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
7980 break;
7981 case TRUTH_OR_EXPR:
7982 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
7983 break;
7984 case TRUTH_XOR_EXPR:
7985 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
7986 break;
7987 default:
7988 break;
7990 /* Now make sure that operands have compatible type to
7991 expression's new_type. */
7992 xop0 = TREE_OPERAND (*expr_p, 0);
7993 xop1 = TREE_OPERAND (*expr_p, 1);
7994 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
7995 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
7996 new_type,
7997 xop0);
7998 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
7999 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8000 new_type,
8001 xop1);
8002 /* Continue classified as tcc_binary. */
8003 goto expr_2;
8006 case FMA_EXPR:
8007 case VEC_COND_EXPR:
8008 case VEC_PERM_EXPR:
8009 /* Classified as tcc_expression. */
8010 goto expr_3;
8012 case POINTER_PLUS_EXPR:
8014 enum gimplify_status r0, r1;
8015 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8016 post_p, is_gimple_val, fb_rvalue);
8017 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8018 post_p, is_gimple_val, fb_rvalue);
8019 recalculate_side_effects (*expr_p);
8020 ret = MIN (r0, r1);
8021 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8022 after gimplifying operands - this is similar to how
8023 it would be folding all gimplified stmts on creation
8024 to have them canonicalized, which is what we eventually
8025 should do anyway. */
8026 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8027 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8029 *expr_p = build_fold_addr_expr_with_type_loc
8030 (input_location,
8031 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8032 TREE_OPERAND (*expr_p, 0),
8033 fold_convert (ptr_type_node,
8034 TREE_OPERAND (*expr_p, 1))),
8035 TREE_TYPE (*expr_p));
8036 ret = MIN (ret, GS_OK);
8038 break;
8041 case CILK_SYNC_STMT:
8043 if (!fn_contains_cilk_spawn_p (cfun))
8045 error_at (EXPR_LOCATION (*expr_p),
8046 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8047 ret = GS_ERROR;
8049 else
8051 gimplify_cilk_sync (expr_p, pre_p);
8052 ret = GS_ALL_DONE;
8054 break;
8057 default:
8058 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8060 case tcc_comparison:
8061 /* Handle comparison of objects of non scalar mode aggregates
8062 with a call to memcmp. It would be nice to only have to do
8063 this for variable-sized objects, but then we'd have to allow
8064 the same nest of reference nodes we allow for MODIFY_EXPR and
8065 that's too complex.
8067 Compare scalar mode aggregates as scalar mode values. Using
8068 memcmp for them would be very inefficient at best, and is
8069 plain wrong if bitfields are involved. */
8071 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8073 /* Vector comparisons need no boolification. */
8074 if (TREE_CODE (type) == VECTOR_TYPE)
8075 goto expr_2;
8076 else if (!AGGREGATE_TYPE_P (type))
8078 tree org_type = TREE_TYPE (*expr_p);
8079 *expr_p = gimple_boolify (*expr_p);
8080 if (!useless_type_conversion_p (org_type,
8081 TREE_TYPE (*expr_p)))
8083 *expr_p = fold_convert_loc (input_location,
8084 org_type, *expr_p);
8085 ret = GS_OK;
8087 else
8088 goto expr_2;
8090 else if (TYPE_MODE (type) != BLKmode)
8091 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8092 else
8093 ret = gimplify_variable_sized_compare (expr_p);
8095 break;
8098 /* If *EXPR_P does not need to be special-cased, handle it
8099 according to its class. */
8100 case tcc_unary:
8101 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8102 post_p, is_gimple_val, fb_rvalue);
8103 break;
8105 case tcc_binary:
8106 expr_2:
8108 enum gimplify_status r0, r1;
8110 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8111 post_p, is_gimple_val, fb_rvalue);
8112 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8113 post_p, is_gimple_val, fb_rvalue);
8115 ret = MIN (r0, r1);
8116 break;
8119 expr_3:
8121 enum gimplify_status r0, r1, r2;
8123 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8124 post_p, is_gimple_val, fb_rvalue);
8125 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8126 post_p, is_gimple_val, fb_rvalue);
8127 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8128 post_p, is_gimple_val, fb_rvalue);
8130 ret = MIN (MIN (r0, r1), r2);
8131 break;
8134 case tcc_declaration:
8135 case tcc_constant:
8136 ret = GS_ALL_DONE;
8137 goto dont_recalculate;
8139 default:
8140 gcc_unreachable ();
8143 recalculate_side_effects (*expr_p);
8145 dont_recalculate:
8146 break;
8149 gcc_assert (*expr_p || ret != GS_OK);
8151 while (ret == GS_OK);
8153 /* If we encountered an error_mark somewhere nested inside, either
8154 stub out the statement or propagate the error back out. */
8155 if (ret == GS_ERROR)
8157 if (is_statement)
8158 *expr_p = NULL;
8159 goto out;
8162 /* This was only valid as a return value from the langhook, which
8163 we handled. Make sure it doesn't escape from any other context. */
8164 gcc_assert (ret != GS_UNHANDLED);
8166 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8168 /* We aren't looking for a value, and we don't have a valid
8169 statement. If it doesn't have side-effects, throw it away. */
8170 if (!TREE_SIDE_EFFECTS (*expr_p))
8171 *expr_p = NULL;
8172 else if (!TREE_THIS_VOLATILE (*expr_p))
8174 /* This is probably a _REF that contains something nested that
8175 has side effects. Recurse through the operands to find it. */
8176 enum tree_code code = TREE_CODE (*expr_p);
8178 switch (code)
8180 case COMPONENT_REF:
8181 case REALPART_EXPR:
8182 case IMAGPART_EXPR:
8183 case VIEW_CONVERT_EXPR:
8184 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8185 gimple_test_f, fallback);
8186 break;
8188 case ARRAY_REF:
8189 case ARRAY_RANGE_REF:
8190 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8191 gimple_test_f, fallback);
8192 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8193 gimple_test_f, fallback);
8194 break;
8196 default:
8197 /* Anything else with side-effects must be converted to
8198 a valid statement before we get here. */
8199 gcc_unreachable ();
8202 *expr_p = NULL;
8204 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8205 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8207 /* Historically, the compiler has treated a bare reference
8208 to a non-BLKmode volatile lvalue as forcing a load. */
8209 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8211 /* Normally, we do not want to create a temporary for a
8212 TREE_ADDRESSABLE type because such a type should not be
8213 copied by bitwise-assignment. However, we make an
8214 exception here, as all we are doing here is ensuring that
8215 we read the bytes that make up the type. We use
8216 create_tmp_var_raw because create_tmp_var will abort when
8217 given a TREE_ADDRESSABLE type. */
8218 tree tmp = create_tmp_var_raw (type, "vol");
8219 gimple_add_tmp_var (tmp);
8220 gimplify_assign (tmp, *expr_p, pre_p);
8221 *expr_p = NULL;
8223 else
8224 /* We can't do anything useful with a volatile reference to
8225 an incomplete type, so just throw it away. Likewise for
8226 a BLKmode type, since any implicit inner load should
8227 already have been turned into an explicit one by the
8228 gimplification process. */
8229 *expr_p = NULL;
8232 /* If we are gimplifying at the statement level, we're done. Tack
8233 everything together and return. */
8234 if (fallback == fb_none || is_statement)
8236 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8237 it out for GC to reclaim it. */
8238 *expr_p = NULL_TREE;
8240 if (!gimple_seq_empty_p (internal_pre)
8241 || !gimple_seq_empty_p (internal_post))
8243 gimplify_seq_add_seq (&internal_pre, internal_post);
8244 gimplify_seq_add_seq (pre_p, internal_pre);
8247 /* The result of gimplifying *EXPR_P is going to be the last few
8248 statements in *PRE_P and *POST_P. Add location information
8249 to all the statements that were added by the gimplification
8250 helpers. */
8251 if (!gimple_seq_empty_p (*pre_p))
8252 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8254 if (!gimple_seq_empty_p (*post_p))
8255 annotate_all_with_location_after (*post_p, post_last_gsi,
8256 input_location);
8258 goto out;
8261 #ifdef ENABLE_GIMPLE_CHECKING
8262 if (*expr_p)
8264 enum tree_code code = TREE_CODE (*expr_p);
8265 /* These expressions should already be in gimple IR form. */
8266 gcc_assert (code != MODIFY_EXPR
8267 && code != ASM_EXPR
8268 && code != BIND_EXPR
8269 && code != CATCH_EXPR
8270 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8271 && code != EH_FILTER_EXPR
8272 && code != GOTO_EXPR
8273 && code != LABEL_EXPR
8274 && code != LOOP_EXPR
8275 && code != SWITCH_EXPR
8276 && code != TRY_FINALLY_EXPR
8277 && code != OMP_CRITICAL
8278 && code != OMP_FOR
8279 && code != OMP_MASTER
8280 && code != OMP_TASKGROUP
8281 && code != OMP_ORDERED
8282 && code != OMP_PARALLEL
8283 && code != OMP_SECTIONS
8284 && code != OMP_SECTION
8285 && code != OMP_SINGLE);
8287 #endif
8289 /* Otherwise we're gimplifying a subexpression, so the resulting
8290 value is interesting. If it's a valid operand that matches
8291 GIMPLE_TEST_F, we're done. Unless we are handling some
8292 post-effects internally; if that's the case, we need to copy into
8293 a temporary before adding the post-effects to POST_P. */
8294 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8295 goto out;
8297 /* Otherwise, we need to create a new temporary for the gimplified
8298 expression. */
8300 /* We can't return an lvalue if we have an internal postqueue. The
8301 object the lvalue refers to would (probably) be modified by the
8302 postqueue; we need to copy the value out first, which means an
8303 rvalue. */
8304 if ((fallback & fb_lvalue)
8305 && gimple_seq_empty_p (internal_post)
8306 && is_gimple_addressable (*expr_p))
8308 /* An lvalue will do. Take the address of the expression, store it
8309 in a temporary, and replace the expression with an INDIRECT_REF of
8310 that temporary. */
8311 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8312 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8313 *expr_p = build_simple_mem_ref (tmp);
8315 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8317 /* An rvalue will do. Assign the gimplified expression into a
8318 new temporary TMP and replace the original expression with
8319 TMP. First, make sure that the expression has a type so that
8320 it can be assigned into a temporary. */
8321 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8322 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8324 else
8326 #ifdef ENABLE_GIMPLE_CHECKING
8327 if (!(fallback & fb_mayfail))
8329 fprintf (stderr, "gimplification failed:\n");
8330 print_generic_expr (stderr, *expr_p, 0);
8331 debug_tree (*expr_p);
8332 internal_error ("gimplification failed");
8334 #endif
8335 gcc_assert (fallback & fb_mayfail);
8337 /* If this is an asm statement, and the user asked for the
8338 impossible, don't die. Fail and let gimplify_asm_expr
8339 issue an error. */
8340 ret = GS_ERROR;
8341 goto out;
8344 /* Make sure the temporary matches our predicate. */
8345 gcc_assert ((*gimple_test_f) (*expr_p));
8347 if (!gimple_seq_empty_p (internal_post))
8349 annotate_all_with_location (internal_post, input_location);
8350 gimplify_seq_add_seq (pre_p, internal_post);
8353 out:
8354 input_location = saved_location;
8355 return ret;
8358 /* Look through TYPE for variable-sized objects and gimplify each such
8359 size that we find. Add to LIST_P any statements generated. */
8361 void
8362 gimplify_type_sizes (tree type, gimple_seq *list_p)
8364 tree field, t;
8366 if (type == NULL || type == error_mark_node)
8367 return;
8369 /* We first do the main variant, then copy into any other variants. */
8370 type = TYPE_MAIN_VARIANT (type);
8372 /* Avoid infinite recursion. */
8373 if (TYPE_SIZES_GIMPLIFIED (type))
8374 return;
8376 TYPE_SIZES_GIMPLIFIED (type) = 1;
8378 switch (TREE_CODE (type))
8380 case INTEGER_TYPE:
8381 case ENUMERAL_TYPE:
8382 case BOOLEAN_TYPE:
8383 case REAL_TYPE:
8384 case FIXED_POINT_TYPE:
8385 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8386 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8388 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8390 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8391 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8393 break;
8395 case ARRAY_TYPE:
8396 /* These types may not have declarations, so handle them here. */
8397 gimplify_type_sizes (TREE_TYPE (type), list_p);
8398 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8399 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8400 with assigned stack slots, for -O1+ -g they should be tracked
8401 by VTA. */
8402 if (!(TYPE_NAME (type)
8403 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8404 && DECL_IGNORED_P (TYPE_NAME (type)))
8405 && TYPE_DOMAIN (type)
8406 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8408 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8409 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8410 DECL_IGNORED_P (t) = 0;
8411 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8412 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8413 DECL_IGNORED_P (t) = 0;
8415 break;
8417 case RECORD_TYPE:
8418 case UNION_TYPE:
8419 case QUAL_UNION_TYPE:
8420 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8421 if (TREE_CODE (field) == FIELD_DECL)
8423 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8424 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8425 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8426 gimplify_type_sizes (TREE_TYPE (field), list_p);
8428 break;
8430 case POINTER_TYPE:
8431 case REFERENCE_TYPE:
8432 /* We used to recurse on the pointed-to type here, which turned out to
8433 be incorrect because its definition might refer to variables not
8434 yet initialized at this point if a forward declaration is involved.
8436 It was actually useful for anonymous pointed-to types to ensure
8437 that the sizes evaluation dominates every possible later use of the
8438 values. Restricting to such types here would be safe since there
8439 is no possible forward declaration around, but would introduce an
8440 undesirable middle-end semantic to anonymity. We then defer to
8441 front-ends the responsibility of ensuring that the sizes are
8442 evaluated both early and late enough, e.g. by attaching artificial
8443 type declarations to the tree. */
8444 break;
8446 default:
8447 break;
8450 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8451 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8453 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8455 TYPE_SIZE (t) = TYPE_SIZE (type);
8456 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8457 TYPE_SIZES_GIMPLIFIED (t) = 1;
8461 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8462 a size or position, has had all of its SAVE_EXPRs evaluated.
8463 We add any required statements to *STMT_P. */
8465 void
8466 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8468 tree expr = *expr_p;
8470 /* We don't do anything if the value isn't there, is constant, or contains
8471 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8472 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8473 will want to replace it with a new variable, but that will cause problems
8474 if this type is from outside the function. It's OK to have that here. */
8475 if (is_gimple_sizepos (expr))
8476 return;
8478 *expr_p = unshare_expr (expr);
8480 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8483 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8484 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8485 is true, also gimplify the parameters. */
8487 gimple
8488 gimplify_body (tree fndecl, bool do_parms)
8490 location_t saved_location = input_location;
8491 gimple_seq parm_stmts, seq;
8492 gimple outer_bind;
8493 struct cgraph_node *cgn;
8495 timevar_push (TV_TREE_GIMPLIFY);
8497 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8498 gimplification. */
8499 default_rtl_profile ();
8501 gcc_assert (gimplify_ctxp == NULL);
8502 push_gimplify_context ();
8504 if (flag_openmp)
8506 gcc_assert (gimplify_omp_ctxp == NULL);
8507 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8508 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8511 /* Unshare most shared trees in the body and in that of any nested functions.
8512 It would seem we don't have to do this for nested functions because
8513 they are supposed to be output and then the outer function gimplified
8514 first, but the g++ front end doesn't always do it that way. */
8515 unshare_body (fndecl);
8516 unvisit_body (fndecl);
8518 cgn = cgraph_get_node (fndecl);
8519 if (cgn && cgn->origin)
8520 nonlocal_vlas = pointer_set_create ();
8522 /* Make sure input_location isn't set to something weird. */
8523 input_location = DECL_SOURCE_LOCATION (fndecl);
8525 /* Resolve callee-copies. This has to be done before processing
8526 the body so that DECL_VALUE_EXPR gets processed correctly. */
8527 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8529 /* Gimplify the function's body. */
8530 seq = NULL;
8531 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8532 outer_bind = gimple_seq_first_stmt (seq);
8533 if (!outer_bind)
8535 outer_bind = gimple_build_nop ();
8536 gimplify_seq_add_stmt (&seq, outer_bind);
8539 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8540 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8541 if (gimple_code (outer_bind) == GIMPLE_BIND
8542 && gimple_seq_first (seq) == gimple_seq_last (seq))
8544 else
8545 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8547 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8549 /* If we had callee-copies statements, insert them at the beginning
8550 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8551 if (!gimple_seq_empty_p (parm_stmts))
8553 tree parm;
8555 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8556 gimple_bind_set_body (outer_bind, parm_stmts);
8558 for (parm = DECL_ARGUMENTS (current_function_decl);
8559 parm; parm = DECL_CHAIN (parm))
8560 if (DECL_HAS_VALUE_EXPR_P (parm))
8562 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8563 DECL_IGNORED_P (parm) = 0;
8567 if (nonlocal_vlas)
8569 if (nonlocal_vla_vars)
8571 /* tree-nested.c may later on call declare_vars (..., true);
8572 which relies on BLOCK_VARS chain to be the tail of the
8573 gimple_bind_vars chain. Ensure we don't violate that
8574 assumption. */
8575 if (gimple_bind_block (outer_bind)
8576 == DECL_INITIAL (current_function_decl))
8577 declare_vars (nonlocal_vla_vars, outer_bind, true);
8578 else
8579 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8580 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8581 nonlocal_vla_vars);
8582 nonlocal_vla_vars = NULL_TREE;
8584 pointer_set_destroy (nonlocal_vlas);
8585 nonlocal_vlas = NULL;
8588 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8590 delete_omp_context (gimplify_omp_ctxp);
8591 gimplify_omp_ctxp = NULL;
8594 pop_gimplify_context (outer_bind);
8595 gcc_assert (gimplify_ctxp == NULL);
8597 #ifdef ENABLE_CHECKING
8598 if (!seen_error ())
8599 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8600 #endif
8602 timevar_pop (TV_TREE_GIMPLIFY);
8603 input_location = saved_location;
8605 return outer_bind;
8608 typedef char *char_p; /* For DEF_VEC_P. */
8610 /* Return whether we should exclude FNDECL from instrumentation. */
8612 static bool
8613 flag_instrument_functions_exclude_p (tree fndecl)
8615 vec<char_p> *v;
8617 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8618 if (v && v->length () > 0)
8620 const char *name;
8621 int i;
8622 char *s;
8624 name = lang_hooks.decl_printable_name (fndecl, 0);
8625 FOR_EACH_VEC_ELT (*v, i, s)
8626 if (strstr (name, s) != NULL)
8627 return true;
8630 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8631 if (v && v->length () > 0)
8633 const char *name;
8634 int i;
8635 char *s;
8637 name = DECL_SOURCE_FILE (fndecl);
8638 FOR_EACH_VEC_ELT (*v, i, s)
8639 if (strstr (name, s) != NULL)
8640 return true;
8643 return false;
8646 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8647 node for the function we want to gimplify.
8649 Return the sequence of GIMPLE statements corresponding to the body
8650 of FNDECL. */
8652 void
8653 gimplify_function_tree (tree fndecl)
8655 tree parm, ret;
8656 gimple_seq seq;
8657 gimple bind;
8659 gcc_assert (!gimple_body (fndecl));
8661 if (DECL_STRUCT_FUNCTION (fndecl))
8662 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8663 else
8664 push_struct_function (fndecl);
8666 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8668 /* Preliminarily mark non-addressed complex variables as eligible
8669 for promotion to gimple registers. We'll transform their uses
8670 as we find them. */
8671 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8672 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8673 && !TREE_THIS_VOLATILE (parm)
8674 && !needs_to_live_in_memory (parm))
8675 DECL_GIMPLE_REG_P (parm) = 1;
8678 ret = DECL_RESULT (fndecl);
8679 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8680 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8681 && !needs_to_live_in_memory (ret))
8682 DECL_GIMPLE_REG_P (ret) = 1;
8684 bind = gimplify_body (fndecl, true);
8686 /* The tree body of the function is no longer needed, replace it
8687 with the new GIMPLE body. */
8688 seq = NULL;
8689 gimple_seq_add_stmt (&seq, bind);
8690 gimple_set_body (fndecl, seq);
8692 /* If we're instrumenting function entry/exit, then prepend the call to
8693 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8694 catch the exit hook. */
8695 /* ??? Add some way to ignore exceptions for this TFE. */
8696 if (flag_instrument_function_entry_exit
8697 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8698 && !flag_instrument_functions_exclude_p (fndecl))
8700 tree x;
8701 gimple new_bind;
8702 gimple tf;
8703 gimple_seq cleanup = NULL, body = NULL;
8704 tree tmp_var;
8705 gimple call;
8707 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8708 call = gimple_build_call (x, 1, integer_zero_node);
8709 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8710 gimple_call_set_lhs (call, tmp_var);
8711 gimplify_seq_add_stmt (&cleanup, call);
8712 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8713 call = gimple_build_call (x, 2,
8714 build_fold_addr_expr (current_function_decl),
8715 tmp_var);
8716 gimplify_seq_add_stmt (&cleanup, call);
8717 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8719 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8720 call = gimple_build_call (x, 1, integer_zero_node);
8721 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8722 gimple_call_set_lhs (call, tmp_var);
8723 gimplify_seq_add_stmt (&body, call);
8724 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8725 call = gimple_build_call (x, 2,
8726 build_fold_addr_expr (current_function_decl),
8727 tmp_var);
8728 gimplify_seq_add_stmt (&body, call);
8729 gimplify_seq_add_stmt (&body, tf);
8730 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8731 /* Clear the block for BIND, since it is no longer directly inside
8732 the function, but within a try block. */
8733 gimple_bind_set_block (bind, NULL);
8735 /* Replace the current function body with the body
8736 wrapped in the try/finally TF. */
8737 seq = NULL;
8738 gimple_seq_add_stmt (&seq, new_bind);
8739 gimple_set_body (fndecl, seq);
8742 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8743 cfun->curr_properties = PROP_gimple_any;
8745 pop_cfun ();
8748 /* Return a dummy expression of type TYPE in order to keep going after an
8749 error. */
8751 static tree
8752 dummy_object (tree type)
8754 tree t = build_int_cst (build_pointer_type (type), 0);
8755 return build2 (MEM_REF, type, t, t);
8758 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
8759 builtin function, but a very special sort of operator. */
8761 enum gimplify_status
8762 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8764 tree promoted_type, have_va_type;
8765 tree valist = TREE_OPERAND (*expr_p, 0);
8766 tree type = TREE_TYPE (*expr_p);
8767 tree t;
8768 location_t loc = EXPR_LOCATION (*expr_p);
8770 /* Verify that valist is of the proper type. */
8771 have_va_type = TREE_TYPE (valist);
8772 if (have_va_type == error_mark_node)
8773 return GS_ERROR;
8774 have_va_type = targetm.canonical_va_list_type (have_va_type);
8776 if (have_va_type == NULL_TREE)
8778 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
8779 return GS_ERROR;
8782 /* Generate a diagnostic for requesting data of a type that cannot
8783 be passed through `...' due to type promotion at the call site. */
8784 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
8785 != type)
8787 static bool gave_help;
8788 bool warned;
8790 /* Unfortunately, this is merely undefined, rather than a constraint
8791 violation, so we cannot make this an error. If this call is never
8792 executed, the program is still strictly conforming. */
8793 warned = warning_at (loc, 0,
8794 "%qT is promoted to %qT when passed through %<...%>",
8795 type, promoted_type);
8796 if (!gave_help && warned)
8798 gave_help = true;
8799 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
8800 promoted_type, type);
8803 /* We can, however, treat "undefined" any way we please.
8804 Call abort to encourage the user to fix the program. */
8805 if (warned)
8806 inform (loc, "if this code is reached, the program will abort");
8807 /* Before the abort, allow the evaluation of the va_list
8808 expression to exit or longjmp. */
8809 gimplify_and_add (valist, pre_p);
8810 t = build_call_expr_loc (loc,
8811 builtin_decl_implicit (BUILT_IN_TRAP), 0);
8812 gimplify_and_add (t, pre_p);
8814 /* This is dead code, but go ahead and finish so that the
8815 mode of the result comes out right. */
8816 *expr_p = dummy_object (type);
8817 return GS_ALL_DONE;
8819 else
8821 /* Make it easier for the backends by protecting the valist argument
8822 from multiple evaluations. */
8823 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
8825 /* For this case, the backends will be expecting a pointer to
8826 TREE_TYPE (abi), but it's possible we've
8827 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
8828 So fix it. */
8829 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
8831 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
8832 valist = fold_convert_loc (loc, p1,
8833 build_fold_addr_expr_loc (loc, valist));
8836 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
8838 else
8839 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
8841 if (!targetm.gimplify_va_arg_expr)
8842 /* FIXME: Once most targets are converted we should merely
8843 assert this is non-null. */
8844 return GS_ALL_DONE;
8846 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
8847 return GS_OK;
8851 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
8853 DST/SRC are the destination and source respectively. You can pass
8854 ungimplified trees in DST or SRC, in which case they will be
8855 converted to a gimple operand if necessary.
8857 This function returns the newly created GIMPLE_ASSIGN tuple. */
8859 gimple
8860 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
8862 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
8863 gimplify_and_add (t, seq_p);
8864 ggc_free (t);
8865 return gimple_seq_last_stmt (*seq_p);
8868 inline hashval_t
8869 gimplify_hasher::hash (const value_type *p)
8871 tree t = p->val;
8872 return iterative_hash_expr (t, 0);
8875 inline bool
8876 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
8878 tree t1 = p1->val;
8879 tree t2 = p2->val;
8880 enum tree_code code = TREE_CODE (t1);
8882 if (TREE_CODE (t2) != code
8883 || TREE_TYPE (t1) != TREE_TYPE (t2))
8884 return false;
8886 if (!operand_equal_p (t1, t2, 0))
8887 return false;
8889 #ifdef ENABLE_CHECKING
8890 /* Only allow them to compare equal if they also hash equal; otherwise
8891 results are nondeterminate, and we fail bootstrap comparison. */
8892 gcc_assert (hash (p1) == hash (p2));
8893 #endif
8895 return true;