Tweak to gimplify_modify_expr
[official-gcc.git] / gcc / gimplify.c
blob037fb2bb719ff1021977701f5a09a2549f4e10b4
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2014 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "expr.h"
28 #include "hash-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 */
65 #include "builtins.h"
67 enum gimplify_omp_var_data
69 GOVD_SEEN = 1,
70 GOVD_EXPLICIT = 2,
71 GOVD_SHARED = 4,
72 GOVD_PRIVATE = 8,
73 GOVD_FIRSTPRIVATE = 16,
74 GOVD_LASTPRIVATE = 32,
75 GOVD_REDUCTION = 64,
76 GOVD_LOCAL = 128,
77 GOVD_MAP = 256,
78 GOVD_DEBUG_PRIVATE = 512,
79 GOVD_PRIVATE_OUTER_REF = 1024,
80 GOVD_LINEAR = 2048,
81 GOVD_ALIGNED = 4096,
82 GOVD_MAP_TO_ONLY = 8192,
83 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
84 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
85 | GOVD_LOCAL)
89 enum omp_region_type
91 ORT_WORKSHARE = 0,
92 ORT_SIMD = 1,
93 ORT_PARALLEL = 2,
94 ORT_COMBINED_PARALLEL = 3,
95 ORT_TASK = 4,
96 ORT_UNTIED_TASK = 5,
97 ORT_TEAMS = 8,
98 ORT_TARGET_DATA = 16,
99 ORT_TARGET = 32
102 /* Gimplify hashtable helper. */
104 struct gimplify_hasher : typed_free_remove <elt_t>
106 typedef elt_t value_type;
107 typedef elt_t compare_type;
108 static inline hashval_t hash (const value_type *);
109 static inline bool equal (const value_type *, const compare_type *);
112 struct gimplify_ctx
114 struct gimplify_ctx *prev_context;
116 vec<gimple_bind> bind_expr_stack;
117 tree temps;
118 gimple_seq conditional_cleanups;
119 tree exit_label;
120 tree return_temp;
122 vec<tree> case_labels;
123 /* The formal temporary table. Should this be persistent? */
124 hash_table<gimplify_hasher> *temp_htab;
126 int conditions;
127 bool save_stack;
128 bool into_ssa;
129 bool allow_rhs_cond_expr;
130 bool in_cleanup_point_expr;
133 struct gimplify_omp_ctx
135 struct gimplify_omp_ctx *outer_context;
136 splay_tree variables;
137 hash_set<tree> *privatized_types;
138 location_t location;
139 enum omp_clause_default_kind default_kind;
140 enum omp_region_type region_type;
141 bool combined_loop;
142 bool distribute;
145 static struct gimplify_ctx *gimplify_ctxp;
146 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
148 /* Forward declaration. */
149 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
151 /* Shorter alias name for the above function for use in gimplify.c
152 only. */
154 static inline void
155 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
157 gimple_seq_add_stmt_without_update (seq_p, gs);
160 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
161 NULL, a new sequence is allocated. This function is
162 similar to gimple_seq_add_seq, but does not scan the operands.
163 During gimplification, we need to manipulate statement sequences
164 before the def/use vectors have been constructed. */
166 static void
167 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
169 gimple_stmt_iterator si;
171 if (src == NULL)
172 return;
174 si = gsi_last (*dst_p);
175 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
179 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
180 and popping gimplify contexts. */
182 static struct gimplify_ctx *ctx_pool = NULL;
184 /* Return a gimplify context struct from the pool. */
186 static inline struct gimplify_ctx *
187 ctx_alloc (void)
189 struct gimplify_ctx * c = ctx_pool;
191 if (c)
192 ctx_pool = c->prev_context;
193 else
194 c = XNEW (struct gimplify_ctx);
196 memset (c, '\0', sizeof (*c));
197 return c;
200 /* Put gimplify context C back into the pool. */
202 static inline void
203 ctx_free (struct gimplify_ctx *c)
205 c->prev_context = ctx_pool;
206 ctx_pool = c;
209 /* Free allocated ctx stack memory. */
211 void
212 free_gimplify_stack (void)
214 struct gimplify_ctx *c;
216 while ((c = ctx_pool))
218 ctx_pool = c->prev_context;
219 free (c);
224 /* Set up a context for the gimplifier. */
226 void
227 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
229 struct gimplify_ctx *c = ctx_alloc ();
231 c->prev_context = gimplify_ctxp;
232 gimplify_ctxp = c;
233 gimplify_ctxp->into_ssa = in_ssa;
234 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
237 /* Tear down a context for the gimplifier. If BODY is non-null, then
238 put the temporaries into the outer BIND_EXPR. Otherwise, put them
239 in the local_decls.
241 BODY is not a sequence, but the first tuple in a sequence. */
243 void
244 pop_gimplify_context (gimple body)
246 struct gimplify_ctx *c = gimplify_ctxp;
248 gcc_assert (c
249 && (!c->bind_expr_stack.exists ()
250 || c->bind_expr_stack.is_empty ()));
251 c->bind_expr_stack.release ();
252 gimplify_ctxp = c->prev_context;
254 if (body)
255 declare_vars (c->temps, body, false);
256 else
257 record_vars (c->temps);
259 delete c->temp_htab;
260 c->temp_htab = NULL;
261 ctx_free (c);
264 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
266 static void
267 gimple_push_bind_expr (gimple_bind bind_stmt)
269 gimplify_ctxp->bind_expr_stack.reserve (8);
270 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
273 /* Pop the first element off the stack of bindings. */
275 static void
276 gimple_pop_bind_expr (void)
278 gimplify_ctxp->bind_expr_stack.pop ();
281 /* Return the first element of the stack of bindings. */
283 gimple_bind
284 gimple_current_bind_expr (void)
286 return gimplify_ctxp->bind_expr_stack.last ();
289 /* Return the stack of bindings created during gimplification. */
291 vec<gimple_bind>
292 gimple_bind_expr_stack (void)
294 return gimplify_ctxp->bind_expr_stack;
297 /* Return true iff there is a COND_EXPR between us and the innermost
298 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
300 static bool
301 gimple_conditional_context (void)
303 return gimplify_ctxp->conditions > 0;
306 /* Note that we've entered a COND_EXPR. */
308 static void
309 gimple_push_condition (void)
311 #ifdef ENABLE_GIMPLE_CHECKING
312 if (gimplify_ctxp->conditions == 0)
313 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
314 #endif
315 ++(gimplify_ctxp->conditions);
318 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
319 now, add any conditional cleanups we've seen to the prequeue. */
321 static void
322 gimple_pop_condition (gimple_seq *pre_p)
324 int conds = --(gimplify_ctxp->conditions);
326 gcc_assert (conds >= 0);
327 if (conds == 0)
329 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
330 gimplify_ctxp->conditional_cleanups = NULL;
334 /* A stable comparison routine for use with splay trees and DECLs. */
336 static int
337 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
339 tree a = (tree) xa;
340 tree b = (tree) xb;
342 return DECL_UID (a) - DECL_UID (b);
345 /* Create a new omp construct that deals with variable remapping. */
347 static struct gimplify_omp_ctx *
348 new_omp_context (enum omp_region_type region_type)
350 struct gimplify_omp_ctx *c;
352 c = XCNEW (struct gimplify_omp_ctx);
353 c->outer_context = gimplify_omp_ctxp;
354 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
355 c->privatized_types = new hash_set<tree>;
356 c->location = input_location;
357 c->region_type = region_type;
358 if ((region_type & ORT_TASK) == 0)
359 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
360 else
361 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
363 return c;
366 /* Destroy an omp construct that deals with variable remapping. */
368 static void
369 delete_omp_context (struct gimplify_omp_ctx *c)
371 splay_tree_delete (c->variables);
372 delete c->privatized_types;
373 XDELETE (c);
376 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
377 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
379 /* Both gimplify the statement T and append it to *SEQ_P. This function
380 behaves exactly as gimplify_stmt, but you don't have to pass T as a
381 reference. */
383 void
384 gimplify_and_add (tree t, gimple_seq *seq_p)
386 gimplify_stmt (&t, seq_p);
389 /* Gimplify statement T into sequence *SEQ_P, and return the first
390 tuple in the sequence of generated tuples for this statement.
391 Return NULL if gimplifying T produced no tuples. */
393 static gimple
394 gimplify_and_return_first (tree t, gimple_seq *seq_p)
396 gimple_stmt_iterator last = gsi_last (*seq_p);
398 gimplify_and_add (t, seq_p);
400 if (!gsi_end_p (last))
402 gsi_next (&last);
403 return gsi_stmt (last);
405 else
406 return gimple_seq_first_stmt (*seq_p);
409 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
410 LHS, or for a call argument. */
412 static bool
413 is_gimple_mem_rhs (tree t)
415 /* If we're dealing with a renamable type, either source or dest must be
416 a renamed variable. */
417 if (is_gimple_reg_type (TREE_TYPE (t)))
418 return is_gimple_val (t);
419 else
420 return is_gimple_val (t) || is_gimple_lvalue (t);
423 /* Return true if T is a CALL_EXPR or an expression that can be
424 assigned to a temporary. Note that this predicate should only be
425 used during gimplification. See the rationale for this in
426 gimplify_modify_expr. */
428 static bool
429 is_gimple_reg_rhs_or_call (tree t)
431 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
432 || TREE_CODE (t) == CALL_EXPR);
435 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
436 this predicate should only be used during gimplification. See the
437 rationale for this in gimplify_modify_expr. */
439 static bool
440 is_gimple_mem_rhs_or_call (tree t)
442 /* If we're dealing with a renamable type, either source or dest must be
443 a renamed variable. */
444 if (is_gimple_reg_type (TREE_TYPE (t)))
445 return is_gimple_val (t);
446 else
447 return (is_gimple_val (t) || is_gimple_lvalue (t)
448 || TREE_CODE (t) == CALL_EXPR);
451 /* Create a temporary with a name derived from VAL. Subroutine of
452 lookup_tmp_var; nobody else should call this function. */
454 static inline tree
455 create_tmp_from_val (tree val)
457 /* Drop all qualifiers and address-space information from the value type. */
458 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
459 tree var = create_tmp_var (type, get_name (val));
460 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
461 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
462 DECL_GIMPLE_REG_P (var) = 1;
463 return var;
466 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
467 an existing expression temporary. */
469 static tree
470 lookup_tmp_var (tree val, bool is_formal)
472 tree ret;
474 /* If not optimizing, never really reuse a temporary. local-alloc
475 won't allocate any variable that is used in more than one basic
476 block, which means it will go into memory, causing much extra
477 work in reload and final and poorer code generation, outweighing
478 the extra memory allocation here. */
479 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
480 ret = create_tmp_from_val (val);
481 else
483 elt_t elt, *elt_p;
484 elt_t **slot;
486 elt.val = val;
487 if (!gimplify_ctxp->temp_htab)
488 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
489 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
490 if (*slot == NULL)
492 elt_p = XNEW (elt_t);
493 elt_p->val = val;
494 elt_p->temp = ret = create_tmp_from_val (val);
495 *slot = elt_p;
497 else
499 elt_p = *slot;
500 ret = elt_p->temp;
504 return ret;
507 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
509 static tree
510 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
511 bool is_formal)
513 tree t, mod;
515 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
516 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
517 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
518 fb_rvalue);
520 if (gimplify_ctxp->into_ssa
521 && is_gimple_reg_type (TREE_TYPE (val)))
522 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
523 else
524 t = lookup_tmp_var (val, is_formal);
526 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
528 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
530 /* gimplify_modify_expr might want to reduce this further. */
531 gimplify_and_add (mod, pre_p);
532 ggc_free (mod);
534 return t;
537 /* Return a formal temporary variable initialized with VAL. PRE_P is as
538 in gimplify_expr. Only use this function if:
540 1) The value of the unfactored expression represented by VAL will not
541 change between the initialization and use of the temporary, and
542 2) The temporary will not be otherwise modified.
544 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
545 and #2 means it is inappropriate for && temps.
547 For other cases, use get_initialized_tmp_var instead. */
549 tree
550 get_formal_tmp_var (tree val, gimple_seq *pre_p)
552 return internal_get_tmp_var (val, pre_p, NULL, true);
555 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
556 are as in gimplify_expr. */
558 tree
559 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
561 return internal_get_tmp_var (val, pre_p, post_p, false);
564 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
565 generate debug info for them; otherwise don't. */
567 void
568 declare_vars (tree vars, gimple gs, bool debug_info)
570 tree last = vars;
571 if (last)
573 tree temps, block;
575 gimple_bind scope = as_a <gimple_bind> (gs);
577 temps = nreverse (last);
579 block = gimple_bind_block (scope);
580 gcc_assert (!block || TREE_CODE (block) == BLOCK);
581 if (!block || !debug_info)
583 DECL_CHAIN (last) = gimple_bind_vars (scope);
584 gimple_bind_set_vars (scope, temps);
586 else
588 /* We need to attach the nodes both to the BIND_EXPR and to its
589 associated BLOCK for debugging purposes. The key point here
590 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
591 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
592 if (BLOCK_VARS (block))
593 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
594 else
596 gimple_bind_set_vars (scope,
597 chainon (gimple_bind_vars (scope), temps));
598 BLOCK_VARS (block) = temps;
604 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
605 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
606 no such upper bound can be obtained. */
608 static void
609 force_constant_size (tree var)
611 /* The only attempt we make is by querying the maximum size of objects
612 of the variable's type. */
614 HOST_WIDE_INT max_size;
616 gcc_assert (TREE_CODE (var) == VAR_DECL);
618 max_size = max_int_size_in_bytes (TREE_TYPE (var));
620 gcc_assert (max_size >= 0);
622 DECL_SIZE_UNIT (var)
623 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
624 DECL_SIZE (var)
625 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
628 /* Push the temporary variable TMP into the current binding. */
630 void
631 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
633 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
635 /* Later processing assumes that the object size is constant, which might
636 not be true at this point. Force the use of a constant upper bound in
637 this case. */
638 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
639 force_constant_size (tmp);
641 DECL_CONTEXT (tmp) = fn->decl;
642 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
644 record_vars_into (tmp, fn->decl);
647 /* Push the temporary variable TMP into the current binding. */
649 void
650 gimple_add_tmp_var (tree tmp)
652 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
654 /* Later processing assumes that the object size is constant, which might
655 not be true at this point. Force the use of a constant upper bound in
656 this case. */
657 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
658 force_constant_size (tmp);
660 DECL_CONTEXT (tmp) = current_function_decl;
661 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
663 if (gimplify_ctxp)
665 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
666 gimplify_ctxp->temps = tmp;
668 /* Mark temporaries local within the nearest enclosing parallel. */
669 if (gimplify_omp_ctxp)
671 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
672 while (ctx
673 && (ctx->region_type == ORT_WORKSHARE
674 || ctx->region_type == ORT_SIMD))
675 ctx = ctx->outer_context;
676 if (ctx)
677 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
680 else if (cfun)
681 record_vars (tmp);
682 else
684 gimple_seq body_seq;
686 /* This case is for nested functions. We need to expose the locals
687 they create. */
688 body_seq = gimple_body (current_function_decl);
689 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
695 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
696 nodes that are referenced more than once in GENERIC functions. This is
697 necessary because gimplification (translation into GIMPLE) is performed
698 by modifying tree nodes in-place, so gimplication of a shared node in a
699 first context could generate an invalid GIMPLE form in a second context.
701 This is achieved with a simple mark/copy/unmark algorithm that walks the
702 GENERIC representation top-down, marks nodes with TREE_VISITED the first
703 time it encounters them, duplicates them if they already have TREE_VISITED
704 set, and finally removes the TREE_VISITED marks it has set.
706 The algorithm works only at the function level, i.e. it generates a GENERIC
707 representation of a function with no nodes shared within the function when
708 passed a GENERIC function (except for nodes that are allowed to be shared).
710 At the global level, it is also necessary to unshare tree nodes that are
711 referenced in more than one function, for the same aforementioned reason.
712 This requires some cooperation from the front-end. There are 2 strategies:
714 1. Manual unsharing. The front-end needs to call unshare_expr on every
715 expression that might end up being shared across functions.
717 2. Deep unsharing. This is an extension of regular unsharing. Instead
718 of calling unshare_expr on expressions that might be shared across
719 functions, the front-end pre-marks them with TREE_VISITED. This will
720 ensure that they are unshared on the first reference within functions
721 when the regular unsharing algorithm runs. The counterpart is that
722 this algorithm must look deeper than for manual unsharing, which is
723 specified by LANG_HOOKS_DEEP_UNSHARING.
725 If there are only few specific cases of node sharing across functions, it is
726 probably easier for a front-end to unshare the expressions manually. On the
727 contrary, if the expressions generated at the global level are as widespread
728 as expressions generated within functions, deep unsharing is very likely the
729 way to go. */
731 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
732 These nodes model computations that must be done once. If we were to
733 unshare something like SAVE_EXPR(i++), the gimplification process would
734 create wrong code. However, if DATA is non-null, it must hold a pointer
735 set that is used to unshare the subtrees of these nodes. */
737 static tree
738 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
740 tree t = *tp;
741 enum tree_code code = TREE_CODE (t);
743 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
744 copy their subtrees if we can make sure to do it only once. */
745 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
747 if (data && !((hash_set<tree> *)data)->add (t))
749 else
750 *walk_subtrees = 0;
753 /* Stop at types, decls, constants like copy_tree_r. */
754 else if (TREE_CODE_CLASS (code) == tcc_type
755 || TREE_CODE_CLASS (code) == tcc_declaration
756 || TREE_CODE_CLASS (code) == tcc_constant
757 /* We can't do anything sensible with a BLOCK used as an
758 expression, but we also can't just die when we see it
759 because of non-expression uses. So we avert our eyes
760 and cross our fingers. Silly Java. */
761 || code == BLOCK)
762 *walk_subtrees = 0;
764 /* Cope with the statement expression extension. */
765 else if (code == STATEMENT_LIST)
768 /* Leave the bulk of the work to copy_tree_r itself. */
769 else
770 copy_tree_r (tp, walk_subtrees, NULL);
772 return NULL_TREE;
775 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
776 If *TP has been visited already, then *TP is deeply copied by calling
777 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
779 static tree
780 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
782 tree t = *tp;
783 enum tree_code code = TREE_CODE (t);
785 /* Skip types, decls, and constants. But we do want to look at their
786 types and the bounds of types. Mark them as visited so we properly
787 unmark their subtrees on the unmark pass. If we've already seen them,
788 don't look down further. */
789 if (TREE_CODE_CLASS (code) == tcc_type
790 || TREE_CODE_CLASS (code) == tcc_declaration
791 || TREE_CODE_CLASS (code) == tcc_constant)
793 if (TREE_VISITED (t))
794 *walk_subtrees = 0;
795 else
796 TREE_VISITED (t) = 1;
799 /* If this node has been visited already, unshare it and don't look
800 any deeper. */
801 else if (TREE_VISITED (t))
803 walk_tree (tp, mostly_copy_tree_r, data, NULL);
804 *walk_subtrees = 0;
807 /* Otherwise, mark the node as visited and keep looking. */
808 else
809 TREE_VISITED (t) = 1;
811 return NULL_TREE;
814 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
815 copy_if_shared_r callback unmodified. */
817 static inline void
818 copy_if_shared (tree *tp, void *data)
820 walk_tree (tp, copy_if_shared_r, data, NULL);
823 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
824 any nested functions. */
826 static void
827 unshare_body (tree fndecl)
829 struct cgraph_node *cgn = cgraph_node::get (fndecl);
830 /* If the language requires deep unsharing, we need a pointer set to make
831 sure we don't repeatedly unshare subtrees of unshareable nodes. */
832 hash_set<tree> *visited
833 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
835 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
836 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
837 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
839 delete visited;
841 if (cgn)
842 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
843 unshare_body (cgn->decl);
846 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
847 Subtrees are walked until the first unvisited node is encountered. */
849 static tree
850 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
852 tree t = *tp;
854 /* If this node has been visited, unmark it and keep looking. */
855 if (TREE_VISITED (t))
856 TREE_VISITED (t) = 0;
858 /* Otherwise, don't look any deeper. */
859 else
860 *walk_subtrees = 0;
862 return NULL_TREE;
865 /* Unmark the visited trees rooted at *TP. */
867 static inline void
868 unmark_visited (tree *tp)
870 walk_tree (tp, unmark_visited_r, NULL, NULL);
873 /* Likewise, but mark all trees as not visited. */
875 static void
876 unvisit_body (tree fndecl)
878 struct cgraph_node *cgn = cgraph_node::get (fndecl);
880 unmark_visited (&DECL_SAVED_TREE (fndecl));
881 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
882 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
884 if (cgn)
885 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
886 unvisit_body (cgn->decl);
889 /* Unconditionally make an unshared copy of EXPR. This is used when using
890 stored expressions which span multiple functions, such as BINFO_VTABLE,
891 as the normal unsharing process can't tell that they're shared. */
893 tree
894 unshare_expr (tree expr)
896 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
897 return expr;
900 /* Worker for unshare_expr_without_location. */
902 static tree
903 prune_expr_location (tree *tp, int *walk_subtrees, void *)
905 if (EXPR_P (*tp))
906 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
907 else
908 *walk_subtrees = 0;
909 return NULL_TREE;
912 /* Similar to unshare_expr but also prune all expression locations
913 from EXPR. */
915 tree
916 unshare_expr_without_location (tree expr)
918 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
919 if (EXPR_P (expr))
920 walk_tree (&expr, prune_expr_location, NULL, NULL);
921 return expr;
924 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
925 contain statements and have a value. Assign its value to a temporary
926 and give it void_type_node. Return the temporary, or NULL_TREE if
927 WRAPPER was already void. */
929 tree
930 voidify_wrapper_expr (tree wrapper, tree temp)
932 tree type = TREE_TYPE (wrapper);
933 if (type && !VOID_TYPE_P (type))
935 tree *p;
937 /* Set p to point to the body of the wrapper. Loop until we find
938 something that isn't a wrapper. */
939 for (p = &wrapper; p && *p; )
941 switch (TREE_CODE (*p))
943 case BIND_EXPR:
944 TREE_SIDE_EFFECTS (*p) = 1;
945 TREE_TYPE (*p) = void_type_node;
946 /* For a BIND_EXPR, the body is operand 1. */
947 p = &BIND_EXPR_BODY (*p);
948 break;
950 case CLEANUP_POINT_EXPR:
951 case TRY_FINALLY_EXPR:
952 case TRY_CATCH_EXPR:
953 TREE_SIDE_EFFECTS (*p) = 1;
954 TREE_TYPE (*p) = void_type_node;
955 p = &TREE_OPERAND (*p, 0);
956 break;
958 case STATEMENT_LIST:
960 tree_stmt_iterator i = tsi_last (*p);
961 TREE_SIDE_EFFECTS (*p) = 1;
962 TREE_TYPE (*p) = void_type_node;
963 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
965 break;
967 case COMPOUND_EXPR:
968 /* Advance to the last statement. Set all container types to
969 void. */
970 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
972 TREE_SIDE_EFFECTS (*p) = 1;
973 TREE_TYPE (*p) = void_type_node;
975 break;
977 case TRANSACTION_EXPR:
978 TREE_SIDE_EFFECTS (*p) = 1;
979 TREE_TYPE (*p) = void_type_node;
980 p = &TRANSACTION_EXPR_BODY (*p);
981 break;
983 default:
984 /* Assume that any tree upon which voidify_wrapper_expr is
985 directly called is a wrapper, and that its body is op0. */
986 if (p == &wrapper)
988 TREE_SIDE_EFFECTS (*p) = 1;
989 TREE_TYPE (*p) = void_type_node;
990 p = &TREE_OPERAND (*p, 0);
991 break;
993 goto out;
997 out:
998 if (p == NULL || IS_EMPTY_STMT (*p))
999 temp = NULL_TREE;
1000 else if (temp)
1002 /* The wrapper is on the RHS of an assignment that we're pushing
1003 down. */
1004 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1005 || TREE_CODE (temp) == MODIFY_EXPR);
1006 TREE_OPERAND (temp, 1) = *p;
1007 *p = temp;
1009 else
1011 temp = create_tmp_var (type, "retval");
1012 *p = build2 (INIT_EXPR, type, temp, *p);
1015 return temp;
1018 return NULL_TREE;
1021 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1022 a temporary through which they communicate. */
1024 static void
1025 build_stack_save_restore (gimple_call *save, gimple_call *restore)
1027 tree tmp_var;
1029 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1030 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1031 gimple_call_set_lhs (*save, tmp_var);
1033 *restore
1034 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1035 1, tmp_var);
1038 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1040 static enum gimplify_status
1041 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1043 tree bind_expr = *expr_p;
1044 bool old_save_stack = gimplify_ctxp->save_stack;
1045 tree t;
1046 gimple_bind gimple_bind;
1047 gimple_seq body, cleanup;
1048 gimple_call stack_save;
1049 location_t start_locus = 0, end_locus = 0;
1051 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1053 /* Mark variables seen in this bind expr. */
1054 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1056 if (TREE_CODE (t) == VAR_DECL)
1058 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1060 /* Mark variable as local. */
1061 if (ctx && !DECL_EXTERNAL (t)
1062 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1063 || splay_tree_lookup (ctx->variables,
1064 (splay_tree_key) t) == NULL))
1066 if (ctx->region_type == ORT_SIMD
1067 && TREE_ADDRESSABLE (t)
1068 && !TREE_STATIC (t))
1069 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1070 else
1071 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1074 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1076 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1077 cfun->has_local_explicit_reg_vars = true;
1080 /* Preliminarily mark non-addressed complex variables as eligible
1081 for promotion to gimple registers. We'll transform their uses
1082 as we find them. */
1083 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1084 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1085 && !TREE_THIS_VOLATILE (t)
1086 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1087 && !needs_to_live_in_memory (t))
1088 DECL_GIMPLE_REG_P (t) = 1;
1091 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1092 BIND_EXPR_BLOCK (bind_expr));
1093 gimple_push_bind_expr (gimple_bind);
1095 gimplify_ctxp->save_stack = false;
1097 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1098 body = NULL;
1099 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1100 gimple_bind_set_body (gimple_bind, body);
1102 /* Source location wise, the cleanup code (stack_restore and clobbers)
1103 belongs to the end of the block, so propagate what we have. The
1104 stack_save operation belongs to the beginning of block, which we can
1105 infer from the bind_expr directly if the block has no explicit
1106 assignment. */
1107 if (BIND_EXPR_BLOCK (bind_expr))
1109 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1110 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1112 if (start_locus == 0)
1113 start_locus = EXPR_LOCATION (bind_expr);
1115 cleanup = NULL;
1116 stack_save = NULL;
1117 if (gimplify_ctxp->save_stack)
1119 gimple_call stack_restore;
1121 /* Save stack on entry and restore it on exit. Add a try_finally
1122 block to achieve this. */
1123 build_stack_save_restore (&stack_save, &stack_restore);
1125 gimple_set_location (stack_save, start_locus);
1126 gimple_set_location (stack_restore, end_locus);
1128 gimplify_seq_add_stmt (&cleanup, stack_restore);
1131 /* Add clobbers for all variables that go out of scope. */
1132 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1134 if (TREE_CODE (t) == VAR_DECL
1135 && !is_global_var (t)
1136 && DECL_CONTEXT (t) == current_function_decl
1137 && !DECL_HARD_REGISTER (t)
1138 && !TREE_THIS_VOLATILE (t)
1139 && !DECL_HAS_VALUE_EXPR_P (t)
1140 /* Only care for variables that have to be in memory. Others
1141 will be rewritten into SSA names, hence moved to the top-level. */
1142 && !is_gimple_reg (t)
1143 && flag_stack_reuse != SR_NONE)
1145 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1146 gimple clobber_stmt;
1147 TREE_THIS_VOLATILE (clobber) = 1;
1148 clobber_stmt = gimple_build_assign (t, clobber);
1149 gimple_set_location (clobber_stmt, end_locus);
1150 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1154 if (cleanup)
1156 gimple_try gs;
1157 gimple_seq new_body;
1159 new_body = NULL;
1160 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1161 GIMPLE_TRY_FINALLY);
1163 if (stack_save)
1164 gimplify_seq_add_stmt (&new_body, stack_save);
1165 gimplify_seq_add_stmt (&new_body, gs);
1166 gimple_bind_set_body (gimple_bind, new_body);
1169 gimplify_ctxp->save_stack = old_save_stack;
1170 gimple_pop_bind_expr ();
1172 gimplify_seq_add_stmt (pre_p, gimple_bind);
1174 if (temp)
1176 *expr_p = temp;
1177 return GS_OK;
1180 *expr_p = NULL_TREE;
1181 return GS_ALL_DONE;
1184 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1185 GIMPLE value, it is assigned to a new temporary and the statement is
1186 re-written to return the temporary.
1188 PRE_P points to the sequence where side effects that must happen before
1189 STMT should be stored. */
1191 static enum gimplify_status
1192 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1194 gimple_return ret;
1195 tree ret_expr = TREE_OPERAND (stmt, 0);
1196 tree result_decl, result;
1198 if (ret_expr == error_mark_node)
1199 return GS_ERROR;
1201 /* Implicit _Cilk_sync must be inserted right before any return statement
1202 if there is a _Cilk_spawn in the function. If the user has provided a
1203 _Cilk_sync, the optimizer should remove this duplicate one. */
1204 if (fn_contains_cilk_spawn_p (cfun))
1206 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1207 gimplify_and_add (impl_sync, pre_p);
1210 if (!ret_expr
1211 || TREE_CODE (ret_expr) == RESULT_DECL
1212 || ret_expr == error_mark_node)
1214 gimple_return ret = gimple_build_return (ret_expr);
1215 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1216 gimplify_seq_add_stmt (pre_p, ret);
1217 return GS_ALL_DONE;
1220 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1221 result_decl = NULL_TREE;
1222 else
1224 result_decl = TREE_OPERAND (ret_expr, 0);
1226 /* See through a return by reference. */
1227 if (TREE_CODE (result_decl) == INDIRECT_REF)
1228 result_decl = TREE_OPERAND (result_decl, 0);
1230 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1231 || TREE_CODE (ret_expr) == INIT_EXPR)
1232 && TREE_CODE (result_decl) == RESULT_DECL);
1235 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1236 Recall that aggregate_value_p is FALSE for any aggregate type that is
1237 returned in registers. If we're returning values in registers, then
1238 we don't want to extend the lifetime of the RESULT_DECL, particularly
1239 across another call. In addition, for those aggregates for which
1240 hard_function_value generates a PARALLEL, we'll die during normal
1241 expansion of structure assignments; there's special code in expand_return
1242 to handle this case that does not exist in expand_expr. */
1243 if (!result_decl)
1244 result = NULL_TREE;
1245 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1247 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1249 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1250 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1251 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1252 should be effectively allocated by the caller, i.e. all calls to
1253 this function must be subject to the Return Slot Optimization. */
1254 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1255 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1257 result = result_decl;
1259 else if (gimplify_ctxp->return_temp)
1260 result = gimplify_ctxp->return_temp;
1261 else
1263 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1265 /* ??? With complex control flow (usually involving abnormal edges),
1266 we can wind up warning about an uninitialized value for this. Due
1267 to how this variable is constructed and initialized, this is never
1268 true. Give up and never warn. */
1269 TREE_NO_WARNING (result) = 1;
1271 gimplify_ctxp->return_temp = result;
1274 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1275 Then gimplify the whole thing. */
1276 if (result != result_decl)
1277 TREE_OPERAND (ret_expr, 0) = result;
1279 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1281 ret = gimple_build_return (result);
1282 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1283 gimplify_seq_add_stmt (pre_p, ret);
1285 return GS_ALL_DONE;
1288 /* Gimplify a variable-length array DECL. */
1290 static void
1291 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1293 /* This is a variable-sized decl. Simplify its size and mark it
1294 for deferred expansion. */
1295 tree t, addr, ptr_type;
1297 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1298 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1300 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1301 if (DECL_HAS_VALUE_EXPR_P (decl))
1302 return;
1304 /* All occurrences of this decl in final gimplified code will be
1305 replaced by indirection. Setting DECL_VALUE_EXPR does two
1306 things: First, it lets the rest of the gimplifier know what
1307 replacement to use. Second, it lets the debug info know
1308 where to find the value. */
1309 ptr_type = build_pointer_type (TREE_TYPE (decl));
1310 addr = create_tmp_var (ptr_type, get_name (decl));
1311 DECL_IGNORED_P (addr) = 0;
1312 t = build_fold_indirect_ref (addr);
1313 TREE_THIS_NOTRAP (t) = 1;
1314 SET_DECL_VALUE_EXPR (decl, t);
1315 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1317 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1318 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1319 size_int (DECL_ALIGN (decl)));
1320 /* The call has been built for a variable-sized object. */
1321 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1322 t = fold_convert (ptr_type, t);
1323 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1325 gimplify_and_add (t, seq_p);
1327 /* Indicate that we need to restore the stack level when the
1328 enclosing BIND_EXPR is exited. */
1329 gimplify_ctxp->save_stack = true;
1332 /* A helper function to be called via walk_tree. Mark all labels under *TP
1333 as being forced. To be called for DECL_INITIAL of static variables. */
1335 static tree
1336 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1338 if (TYPE_P (*tp))
1339 *walk_subtrees = 0;
1340 if (TREE_CODE (*tp) == LABEL_DECL)
1341 FORCED_LABEL (*tp) = 1;
1343 return NULL_TREE;
1346 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1347 and initialization explicit. */
1349 static enum gimplify_status
1350 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1352 tree stmt = *stmt_p;
1353 tree decl = DECL_EXPR_DECL (stmt);
1355 *stmt_p = NULL_TREE;
1357 if (TREE_TYPE (decl) == error_mark_node)
1358 return GS_ERROR;
1360 if ((TREE_CODE (decl) == TYPE_DECL
1361 || TREE_CODE (decl) == VAR_DECL)
1362 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1363 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1365 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1366 in case its size expressions contain problematic nodes like CALL_EXPR. */
1367 if (TREE_CODE (decl) == TYPE_DECL
1368 && DECL_ORIGINAL_TYPE (decl)
1369 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1370 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1372 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1374 tree init = DECL_INITIAL (decl);
1376 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1377 || (!TREE_STATIC (decl)
1378 && flag_stack_check == GENERIC_STACK_CHECK
1379 && compare_tree_int (DECL_SIZE_UNIT (decl),
1380 STACK_CHECK_MAX_VAR_SIZE) > 0))
1381 gimplify_vla_decl (decl, seq_p);
1383 /* Some front ends do not explicitly declare all anonymous
1384 artificial variables. We compensate here by declaring the
1385 variables, though it would be better if the front ends would
1386 explicitly declare them. */
1387 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1388 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1389 gimple_add_tmp_var (decl);
1391 if (init && init != error_mark_node)
1393 if (!TREE_STATIC (decl))
1395 DECL_INITIAL (decl) = NULL_TREE;
1396 init = build2 (INIT_EXPR, void_type_node, decl, init);
1397 gimplify_and_add (init, seq_p);
1398 ggc_free (init);
1400 else
1401 /* We must still examine initializers for static variables
1402 as they may contain a label address. */
1403 walk_tree (&init, force_labels_r, NULL, NULL);
1407 return GS_ALL_DONE;
1410 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1411 and replacing the LOOP_EXPR with goto, but if the loop contains an
1412 EXIT_EXPR, we need to append a label for it to jump to. */
1414 static enum gimplify_status
1415 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1417 tree saved_label = gimplify_ctxp->exit_label;
1418 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1420 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1422 gimplify_ctxp->exit_label = NULL_TREE;
1424 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1426 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1428 if (gimplify_ctxp->exit_label)
1429 gimplify_seq_add_stmt (pre_p,
1430 gimple_build_label (gimplify_ctxp->exit_label));
1432 gimplify_ctxp->exit_label = saved_label;
1434 *expr_p = NULL;
1435 return GS_ALL_DONE;
1438 /* Gimplify a statement list onto a sequence. These may be created either
1439 by an enlightened front-end, or by shortcut_cond_expr. */
1441 static enum gimplify_status
1442 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1444 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1446 tree_stmt_iterator i = tsi_start (*expr_p);
1448 while (!tsi_end_p (i))
1450 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1451 tsi_delink (&i);
1454 if (temp)
1456 *expr_p = temp;
1457 return GS_OK;
1460 return GS_ALL_DONE;
1464 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1465 branch to. */
1467 static enum gimplify_status
1468 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1470 tree switch_expr = *expr_p;
1471 gimple_seq switch_body_seq = NULL;
1472 enum gimplify_status ret;
1473 tree index_type = TREE_TYPE (switch_expr);
1474 if (index_type == NULL_TREE)
1475 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1477 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1478 fb_rvalue);
1479 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1480 return ret;
1482 if (SWITCH_BODY (switch_expr))
1484 vec<tree> labels;
1485 vec<tree> saved_labels;
1486 tree default_case = NULL_TREE;
1487 gimple_switch gimple_switch;
1489 /* If someone can be bothered to fill in the labels, they can
1490 be bothered to null out the body too. */
1491 gcc_assert (!SWITCH_LABELS (switch_expr));
1493 /* Save old labels, get new ones from body, then restore the old
1494 labels. Save all the things from the switch body to append after. */
1495 saved_labels = gimplify_ctxp->case_labels;
1496 gimplify_ctxp->case_labels.create (8);
1498 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1499 labels = gimplify_ctxp->case_labels;
1500 gimplify_ctxp->case_labels = saved_labels;
1502 preprocess_case_label_vec_for_gimple (labels, index_type,
1503 &default_case);
1505 if (!default_case)
1507 gimple_label new_default;
1509 default_case
1510 = build_case_label (NULL_TREE, NULL_TREE,
1511 create_artificial_label (UNKNOWN_LOCATION));
1512 new_default = gimple_build_label (CASE_LABEL (default_case));
1513 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1516 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1517 default_case, labels);
1518 gimplify_seq_add_stmt (pre_p, gimple_switch);
1519 gimplify_seq_add_seq (pre_p, switch_body_seq);
1520 labels.release ();
1522 else
1523 gcc_assert (SWITCH_LABELS (switch_expr));
1525 return GS_ALL_DONE;
1528 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1530 static enum gimplify_status
1531 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1533 struct gimplify_ctx *ctxp;
1534 gimple_label label_stmt;
1536 /* Invalid OpenMP programs can play Duff's Device type games with
1537 #pragma omp parallel. At least in the C front end, we don't
1538 detect such invalid branches until after gimplification. */
1539 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1540 if (ctxp->case_labels.exists ())
1541 break;
1543 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
1544 ctxp->case_labels.safe_push (*expr_p);
1545 gimplify_seq_add_stmt (pre_p, label_stmt);
1547 return GS_ALL_DONE;
1550 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1551 if necessary. */
1553 tree
1554 build_and_jump (tree *label_p)
1556 if (label_p == NULL)
1557 /* If there's nowhere to jump, just fall through. */
1558 return NULL_TREE;
1560 if (*label_p == NULL_TREE)
1562 tree label = create_artificial_label (UNKNOWN_LOCATION);
1563 *label_p = label;
1566 return build1 (GOTO_EXPR, void_type_node, *label_p);
1569 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1570 This also involves building a label to jump to and communicating it to
1571 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1573 static enum gimplify_status
1574 gimplify_exit_expr (tree *expr_p)
1576 tree cond = TREE_OPERAND (*expr_p, 0);
1577 tree expr;
1579 expr = build_and_jump (&gimplify_ctxp->exit_label);
1580 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1581 *expr_p = expr;
1583 return GS_OK;
1586 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1587 different from its canonical type, wrap the whole thing inside a
1588 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1589 type.
1591 The canonical type of a COMPONENT_REF is the type of the field being
1592 referenced--unless the field is a bit-field which can be read directly
1593 in a smaller mode, in which case the canonical type is the
1594 sign-appropriate type corresponding to that mode. */
1596 static void
1597 canonicalize_component_ref (tree *expr_p)
1599 tree expr = *expr_p;
1600 tree type;
1602 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1604 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1605 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1606 else
1607 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1609 /* One could argue that all the stuff below is not necessary for
1610 the non-bitfield case and declare it a FE error if type
1611 adjustment would be needed. */
1612 if (TREE_TYPE (expr) != type)
1614 #ifdef ENABLE_TYPES_CHECKING
1615 tree old_type = TREE_TYPE (expr);
1616 #endif
1617 int type_quals;
1619 /* We need to preserve qualifiers and propagate them from
1620 operand 0. */
1621 type_quals = TYPE_QUALS (type)
1622 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1623 if (TYPE_QUALS (type) != type_quals)
1624 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1626 /* Set the type of the COMPONENT_REF to the underlying type. */
1627 TREE_TYPE (expr) = type;
1629 #ifdef ENABLE_TYPES_CHECKING
1630 /* It is now a FE error, if the conversion from the canonical
1631 type to the original expression type is not useless. */
1632 gcc_assert (useless_type_conversion_p (old_type, type));
1633 #endif
1637 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1638 to foo, embed that change in the ADDR_EXPR by converting
1639 T array[U];
1640 (T *)&array
1642 &array[L]
1643 where L is the lower bound. For simplicity, only do this for constant
1644 lower bound.
1645 The constraint is that the type of &array[L] is trivially convertible
1646 to T *. */
1648 static void
1649 canonicalize_addr_expr (tree *expr_p)
1651 tree expr = *expr_p;
1652 tree addr_expr = TREE_OPERAND (expr, 0);
1653 tree datype, ddatype, pddatype;
1655 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1656 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1657 || TREE_CODE (addr_expr) != ADDR_EXPR)
1658 return;
1660 /* The addr_expr type should be a pointer to an array. */
1661 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1662 if (TREE_CODE (datype) != ARRAY_TYPE)
1663 return;
1665 /* The pointer to element type shall be trivially convertible to
1666 the expression pointer type. */
1667 ddatype = TREE_TYPE (datype);
1668 pddatype = build_pointer_type (ddatype);
1669 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1670 pddatype))
1671 return;
1673 /* The lower bound and element sizes must be constant. */
1674 if (!TYPE_SIZE_UNIT (ddatype)
1675 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1676 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1677 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1678 return;
1680 /* All checks succeeded. Build a new node to merge the cast. */
1681 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1682 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1683 NULL_TREE, NULL_TREE);
1684 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1686 /* We can have stripped a required restrict qualifier above. */
1687 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1688 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1691 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1692 underneath as appropriate. */
1694 static enum gimplify_status
1695 gimplify_conversion (tree *expr_p)
1697 location_t loc = EXPR_LOCATION (*expr_p);
1698 gcc_assert (CONVERT_EXPR_P (*expr_p));
1700 /* Then strip away all but the outermost conversion. */
1701 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1703 /* And remove the outermost conversion if it's useless. */
1704 if (tree_ssa_useless_type_conversion (*expr_p))
1705 *expr_p = TREE_OPERAND (*expr_p, 0);
1707 /* If we still have a conversion at the toplevel,
1708 then canonicalize some constructs. */
1709 if (CONVERT_EXPR_P (*expr_p))
1711 tree sub = TREE_OPERAND (*expr_p, 0);
1713 /* If a NOP conversion is changing the type of a COMPONENT_REF
1714 expression, then canonicalize its type now in order to expose more
1715 redundant conversions. */
1716 if (TREE_CODE (sub) == COMPONENT_REF)
1717 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1719 /* If a NOP conversion is changing a pointer to array of foo
1720 to a pointer to foo, embed that change in the ADDR_EXPR. */
1721 else if (TREE_CODE (sub) == ADDR_EXPR)
1722 canonicalize_addr_expr (expr_p);
1725 /* If we have a conversion to a non-register type force the
1726 use of a VIEW_CONVERT_EXPR instead. */
1727 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1728 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1729 TREE_OPERAND (*expr_p, 0));
1731 return GS_OK;
1734 /* Nonlocal VLAs seen in the current function. */
1735 static hash_set<tree> *nonlocal_vlas;
1737 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1738 static tree nonlocal_vla_vars;
1740 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1741 DECL_VALUE_EXPR, and it's worth re-examining things. */
1743 static enum gimplify_status
1744 gimplify_var_or_parm_decl (tree *expr_p)
1746 tree decl = *expr_p;
1748 /* ??? If this is a local variable, and it has not been seen in any
1749 outer BIND_EXPR, then it's probably the result of a duplicate
1750 declaration, for which we've already issued an error. It would
1751 be really nice if the front end wouldn't leak these at all.
1752 Currently the only known culprit is C++ destructors, as seen
1753 in g++.old-deja/g++.jason/binding.C. */
1754 if (TREE_CODE (decl) == VAR_DECL
1755 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1756 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1757 && decl_function_context (decl) == current_function_decl)
1759 gcc_assert (seen_error ());
1760 return GS_ERROR;
1763 /* When within an OpenMP context, notice uses of variables. */
1764 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1765 return GS_ALL_DONE;
1767 /* If the decl is an alias for another expression, substitute it now. */
1768 if (DECL_HAS_VALUE_EXPR_P (decl))
1770 tree value_expr = DECL_VALUE_EXPR (decl);
1772 /* For referenced nonlocal VLAs add a decl for debugging purposes
1773 to the current function. */
1774 if (TREE_CODE (decl) == VAR_DECL
1775 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1776 && nonlocal_vlas != NULL
1777 && TREE_CODE (value_expr) == INDIRECT_REF
1778 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1779 && decl_function_context (decl) != current_function_decl)
1781 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1782 while (ctx
1783 && (ctx->region_type == ORT_WORKSHARE
1784 || ctx->region_type == ORT_SIMD))
1785 ctx = ctx->outer_context;
1786 if (!ctx && !nonlocal_vlas->add (decl))
1788 tree copy = copy_node (decl);
1790 lang_hooks.dup_lang_specific_decl (copy);
1791 SET_DECL_RTL (copy, 0);
1792 TREE_USED (copy) = 1;
1793 DECL_CHAIN (copy) = nonlocal_vla_vars;
1794 nonlocal_vla_vars = copy;
1795 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1796 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1800 *expr_p = unshare_expr (value_expr);
1801 return GS_OK;
1804 return GS_ALL_DONE;
1807 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1809 static void
1810 recalculate_side_effects (tree t)
1812 enum tree_code code = TREE_CODE (t);
1813 int len = TREE_OPERAND_LENGTH (t);
1814 int i;
1816 switch (TREE_CODE_CLASS (code))
1818 case tcc_expression:
1819 switch (code)
1821 case INIT_EXPR:
1822 case MODIFY_EXPR:
1823 case VA_ARG_EXPR:
1824 case PREDECREMENT_EXPR:
1825 case PREINCREMENT_EXPR:
1826 case POSTDECREMENT_EXPR:
1827 case POSTINCREMENT_EXPR:
1828 /* All of these have side-effects, no matter what their
1829 operands are. */
1830 return;
1832 default:
1833 break;
1835 /* Fall through. */
1837 case tcc_comparison: /* a comparison expression */
1838 case tcc_unary: /* a unary arithmetic expression */
1839 case tcc_binary: /* a binary arithmetic expression */
1840 case tcc_reference: /* a reference */
1841 case tcc_vl_exp: /* a function call */
1842 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1843 for (i = 0; i < len; ++i)
1845 tree op = TREE_OPERAND (t, i);
1846 if (op && TREE_SIDE_EFFECTS (op))
1847 TREE_SIDE_EFFECTS (t) = 1;
1849 break;
1851 case tcc_constant:
1852 /* No side-effects. */
1853 return;
1855 default:
1856 gcc_unreachable ();
1860 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1861 node *EXPR_P.
1863 compound_lval
1864 : min_lval '[' val ']'
1865 | min_lval '.' ID
1866 | compound_lval '[' val ']'
1867 | compound_lval '.' ID
1869 This is not part of the original SIMPLE definition, which separates
1870 array and member references, but it seems reasonable to handle them
1871 together. Also, this way we don't run into problems with union
1872 aliasing; gcc requires that for accesses through a union to alias, the
1873 union reference must be explicit, which was not always the case when we
1874 were splitting up array and member refs.
1876 PRE_P points to the sequence where side effects that must happen before
1877 *EXPR_P should be stored.
1879 POST_P points to the sequence where side effects that must happen after
1880 *EXPR_P should be stored. */
1882 static enum gimplify_status
1883 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1884 fallback_t fallback)
1886 tree *p;
1887 enum gimplify_status ret = GS_ALL_DONE, tret;
1888 int i;
1889 location_t loc = EXPR_LOCATION (*expr_p);
1890 tree expr = *expr_p;
1892 /* Create a stack of the subexpressions so later we can walk them in
1893 order from inner to outer. */
1894 auto_vec<tree, 10> expr_stack;
1896 /* We can handle anything that get_inner_reference can deal with. */
1897 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1899 restart:
1900 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1901 if (TREE_CODE (*p) == INDIRECT_REF)
1902 *p = fold_indirect_ref_loc (loc, *p);
1904 if (handled_component_p (*p))
1906 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1907 additional COMPONENT_REFs. */
1908 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1909 && gimplify_var_or_parm_decl (p) == GS_OK)
1910 goto restart;
1911 else
1912 break;
1914 expr_stack.safe_push (*p);
1917 gcc_assert (expr_stack.length ());
1919 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1920 walked through and P points to the innermost expression.
1922 Java requires that we elaborated nodes in source order. That
1923 means we must gimplify the inner expression followed by each of
1924 the indices, in order. But we can't gimplify the inner
1925 expression until we deal with any variable bounds, sizes, or
1926 positions in order to deal with PLACEHOLDER_EXPRs.
1928 So we do this in three steps. First we deal with the annotations
1929 for any variables in the components, then we gimplify the base,
1930 then we gimplify any indices, from left to right. */
1931 for (i = expr_stack.length () - 1; i >= 0; i--)
1933 tree t = expr_stack[i];
1935 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1937 /* Gimplify the low bound and element type size and put them into
1938 the ARRAY_REF. If these values are set, they have already been
1939 gimplified. */
1940 if (TREE_OPERAND (t, 2) == NULL_TREE)
1942 tree low = unshare_expr (array_ref_low_bound (t));
1943 if (!is_gimple_min_invariant (low))
1945 TREE_OPERAND (t, 2) = low;
1946 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1947 post_p, is_gimple_reg,
1948 fb_rvalue);
1949 ret = MIN (ret, tret);
1952 else
1954 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1955 is_gimple_reg, fb_rvalue);
1956 ret = MIN (ret, tret);
1959 if (TREE_OPERAND (t, 3) == NULL_TREE)
1961 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1962 tree elmt_size = unshare_expr (array_ref_element_size (t));
1963 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1965 /* Divide the element size by the alignment of the element
1966 type (above). */
1967 elmt_size
1968 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1970 if (!is_gimple_min_invariant (elmt_size))
1972 TREE_OPERAND (t, 3) = elmt_size;
1973 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1974 post_p, is_gimple_reg,
1975 fb_rvalue);
1976 ret = MIN (ret, tret);
1979 else
1981 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1982 is_gimple_reg, fb_rvalue);
1983 ret = MIN (ret, tret);
1986 else if (TREE_CODE (t) == COMPONENT_REF)
1988 /* Set the field offset into T and gimplify it. */
1989 if (TREE_OPERAND (t, 2) == NULL_TREE)
1991 tree offset = unshare_expr (component_ref_field_offset (t));
1992 tree field = TREE_OPERAND (t, 1);
1993 tree factor
1994 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1996 /* Divide the offset by its alignment. */
1997 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
1999 if (!is_gimple_min_invariant (offset))
2001 TREE_OPERAND (t, 2) = offset;
2002 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2003 post_p, is_gimple_reg,
2004 fb_rvalue);
2005 ret = MIN (ret, tret);
2008 else
2010 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2011 is_gimple_reg, fb_rvalue);
2012 ret = MIN (ret, tret);
2017 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2018 so as to match the min_lval predicate. Failure to do so may result
2019 in the creation of large aggregate temporaries. */
2020 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2021 fallback | fb_lvalue);
2022 ret = MIN (ret, tret);
2024 /* And finally, the indices and operands of ARRAY_REF. During this
2025 loop we also remove any useless conversions. */
2026 for (; expr_stack.length () > 0; )
2028 tree t = expr_stack.pop ();
2030 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2032 /* Gimplify the dimension. */
2033 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2035 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2036 is_gimple_val, fb_rvalue);
2037 ret = MIN (ret, tret);
2041 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2043 /* The innermost expression P may have originally had
2044 TREE_SIDE_EFFECTS set which would have caused all the outer
2045 expressions in *EXPR_P leading to P to also have had
2046 TREE_SIDE_EFFECTS set. */
2047 recalculate_side_effects (t);
2050 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2051 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2053 canonicalize_component_ref (expr_p);
2056 expr_stack.release ();
2058 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2060 return ret;
2063 /* Gimplify the self modifying expression pointed to by EXPR_P
2064 (++, --, +=, -=).
2066 PRE_P points to the list where side effects that must happen before
2067 *EXPR_P should be stored.
2069 POST_P points to the list where side effects that must happen after
2070 *EXPR_P should be stored.
2072 WANT_VALUE is nonzero iff we want to use the value of this expression
2073 in another expression.
2075 ARITH_TYPE is the type the computation should be performed in. */
2077 enum gimplify_status
2078 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2079 bool want_value, tree arith_type)
2081 enum tree_code code;
2082 tree lhs, lvalue, rhs, t1;
2083 gimple_seq post = NULL, *orig_post_p = post_p;
2084 bool postfix;
2085 enum tree_code arith_code;
2086 enum gimplify_status ret;
2087 location_t loc = EXPR_LOCATION (*expr_p);
2089 code = TREE_CODE (*expr_p);
2091 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2092 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2094 /* Prefix or postfix? */
2095 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2096 /* Faster to treat as prefix if result is not used. */
2097 postfix = want_value;
2098 else
2099 postfix = false;
2101 /* For postfix, make sure the inner expression's post side effects
2102 are executed after side effects from this expression. */
2103 if (postfix)
2104 post_p = &post;
2106 /* Add or subtract? */
2107 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2108 arith_code = PLUS_EXPR;
2109 else
2110 arith_code = MINUS_EXPR;
2112 /* Gimplify the LHS into a GIMPLE lvalue. */
2113 lvalue = TREE_OPERAND (*expr_p, 0);
2114 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2115 if (ret == GS_ERROR)
2116 return ret;
2118 /* Extract the operands to the arithmetic operation. */
2119 lhs = lvalue;
2120 rhs = TREE_OPERAND (*expr_p, 1);
2122 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2123 that as the result value and in the postqueue operation. */
2124 if (postfix)
2126 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2127 if (ret == GS_ERROR)
2128 return ret;
2130 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2133 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2134 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2136 rhs = convert_to_ptrofftype_loc (loc, rhs);
2137 if (arith_code == MINUS_EXPR)
2138 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2139 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2141 else
2142 t1 = fold_convert (TREE_TYPE (*expr_p),
2143 fold_build2 (arith_code, arith_type,
2144 fold_convert (arith_type, lhs),
2145 fold_convert (arith_type, rhs)));
2147 if (postfix)
2149 gimplify_assign (lvalue, t1, pre_p);
2150 gimplify_seq_add_seq (orig_post_p, post);
2151 *expr_p = lhs;
2152 return GS_ALL_DONE;
2154 else
2156 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2157 return GS_OK;
2161 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2163 static void
2164 maybe_with_size_expr (tree *expr_p)
2166 tree expr = *expr_p;
2167 tree type = TREE_TYPE (expr);
2168 tree size;
2170 /* If we've already wrapped this or the type is error_mark_node, we can't do
2171 anything. */
2172 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2173 || type == error_mark_node)
2174 return;
2176 /* If the size isn't known or is a constant, we have nothing to do. */
2177 size = TYPE_SIZE_UNIT (type);
2178 if (!size || TREE_CODE (size) == INTEGER_CST)
2179 return;
2181 /* Otherwise, make a WITH_SIZE_EXPR. */
2182 size = unshare_expr (size);
2183 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2184 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2187 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2188 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2189 the CALL_EXPR. */
2191 enum gimplify_status
2192 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2194 bool (*test) (tree);
2195 fallback_t fb;
2197 /* In general, we allow lvalues for function arguments to avoid
2198 extra overhead of copying large aggregates out of even larger
2199 aggregates into temporaries only to copy the temporaries to
2200 the argument list. Make optimizers happy by pulling out to
2201 temporaries those types that fit in registers. */
2202 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2203 test = is_gimple_val, fb = fb_rvalue;
2204 else
2206 test = is_gimple_lvalue, fb = fb_either;
2207 /* Also strip a TARGET_EXPR that would force an extra copy. */
2208 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2210 tree init = TARGET_EXPR_INITIAL (*arg_p);
2211 if (init
2212 && !VOID_TYPE_P (TREE_TYPE (init)))
2213 *arg_p = init;
2217 /* If this is a variable sized type, we must remember the size. */
2218 maybe_with_size_expr (arg_p);
2220 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2221 /* Make sure arguments have the same location as the function call
2222 itself. */
2223 protected_set_expr_location (*arg_p, call_location);
2225 /* There is a sequence point before a function call. Side effects in
2226 the argument list must occur before the actual call. So, when
2227 gimplifying arguments, force gimplify_expr to use an internal
2228 post queue which is then appended to the end of PRE_P. */
2229 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2232 /* Don't fold STMT inside ORT_TARGET, because it can break code by adding decl
2233 references that weren't in the source. We'll do it during omplower pass
2234 instead. */
2236 static bool
2237 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2239 struct gimplify_omp_ctx *ctx;
2240 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2241 if (ctx->region_type == ORT_TARGET)
2242 return false;
2243 return fold_stmt (gsi);
2246 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2247 WANT_VALUE is true if the result of the call is desired. */
2249 static enum gimplify_status
2250 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2252 tree fndecl, parms, p, fnptrtype;
2253 enum gimplify_status ret;
2254 int i, nargs;
2255 gimple_call call;
2256 bool builtin_va_start_p = false;
2257 location_t loc = EXPR_LOCATION (*expr_p);
2259 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2261 /* For reliable diagnostics during inlining, it is necessary that
2262 every call_expr be annotated with file and line. */
2263 if (! EXPR_HAS_LOCATION (*expr_p))
2264 SET_EXPR_LOCATION (*expr_p, input_location);
2266 /* Gimplify internal functions created in the FEs. */
2267 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2269 nargs = call_expr_nargs (*expr_p);
2270 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2271 auto_vec<tree> vargs (nargs);
2273 for (i = 0; i < nargs; i++)
2275 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2276 EXPR_LOCATION (*expr_p));
2277 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2279 gimple call = gimple_build_call_internal_vec (ifn, vargs);
2280 gimplify_seq_add_stmt (pre_p, call);
2281 return GS_ALL_DONE;
2284 /* This may be a call to a builtin function.
2286 Builtin function calls may be transformed into different
2287 (and more efficient) builtin function calls under certain
2288 circumstances. Unfortunately, gimplification can muck things
2289 up enough that the builtin expanders are not aware that certain
2290 transformations are still valid.
2292 So we attempt transformation/gimplification of the call before
2293 we gimplify the CALL_EXPR. At this time we do not manage to
2294 transform all calls in the same manner as the expanders do, but
2295 we do transform most of them. */
2296 fndecl = get_callee_fndecl (*expr_p);
2297 if (fndecl
2298 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2299 switch (DECL_FUNCTION_CODE (fndecl))
2301 case BUILT_IN_VA_START:
2303 builtin_va_start_p = TRUE;
2304 if (call_expr_nargs (*expr_p) < 2)
2306 error ("too few arguments to function %<va_start%>");
2307 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2308 return GS_OK;
2311 if (fold_builtin_next_arg (*expr_p, true))
2313 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2314 return GS_OK;
2316 break;
2318 case BUILT_IN_LINE:
2320 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2321 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2322 return GS_OK;
2324 case BUILT_IN_FILE:
2326 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2327 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2328 return GS_OK;
2330 case BUILT_IN_FUNCTION:
2332 const char *function;
2333 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2334 *expr_p = build_string_literal (strlen (function) + 1, function);
2335 return GS_OK;
2337 default:
2340 if (fndecl && DECL_BUILT_IN (fndecl))
2342 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2343 if (new_tree && new_tree != *expr_p)
2345 /* There was a transformation of this call which computes the
2346 same value, but in a more efficient way. Return and try
2347 again. */
2348 *expr_p = new_tree;
2349 return GS_OK;
2353 /* Remember the original function pointer type. */
2354 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2356 /* There is a sequence point before the call, so any side effects in
2357 the calling expression must occur before the actual call. Force
2358 gimplify_expr to use an internal post queue. */
2359 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2360 is_gimple_call_addr, fb_rvalue);
2362 nargs = call_expr_nargs (*expr_p);
2364 /* Get argument types for verification. */
2365 fndecl = get_callee_fndecl (*expr_p);
2366 parms = NULL_TREE;
2367 if (fndecl)
2368 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2369 else
2370 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2372 if (fndecl && DECL_ARGUMENTS (fndecl))
2373 p = DECL_ARGUMENTS (fndecl);
2374 else if (parms)
2375 p = parms;
2376 else
2377 p = NULL_TREE;
2378 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2381 /* If the last argument is __builtin_va_arg_pack () and it is not
2382 passed as a named argument, decrease the number of CALL_EXPR
2383 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2384 if (!p
2385 && i < nargs
2386 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2388 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2389 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2391 if (last_arg_fndecl
2392 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2393 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2394 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2396 tree call = *expr_p;
2398 --nargs;
2399 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2400 CALL_EXPR_FN (call),
2401 nargs, CALL_EXPR_ARGP (call));
2403 /* Copy all CALL_EXPR flags, location and block, except
2404 CALL_EXPR_VA_ARG_PACK flag. */
2405 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2406 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2407 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2408 = CALL_EXPR_RETURN_SLOT_OPT (call);
2409 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2410 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2412 /* Set CALL_EXPR_VA_ARG_PACK. */
2413 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2417 /* Finally, gimplify the function arguments. */
2418 if (nargs > 0)
2420 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2421 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2422 PUSH_ARGS_REVERSED ? i-- : i++)
2424 enum gimplify_status t;
2426 /* Avoid gimplifying the second argument to va_start, which needs to
2427 be the plain PARM_DECL. */
2428 if ((i != 1) || !builtin_va_start_p)
2430 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2431 EXPR_LOCATION (*expr_p));
2433 if (t == GS_ERROR)
2434 ret = GS_ERROR;
2439 /* Verify the function result. */
2440 if (want_value && fndecl
2441 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2443 error_at (loc, "using result of function returning %<void%>");
2444 ret = GS_ERROR;
2447 /* Try this again in case gimplification exposed something. */
2448 if (ret != GS_ERROR)
2450 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2452 if (new_tree && new_tree != *expr_p)
2454 /* There was a transformation of this call which computes the
2455 same value, but in a more efficient way. Return and try
2456 again. */
2457 *expr_p = new_tree;
2458 return GS_OK;
2461 else
2463 *expr_p = error_mark_node;
2464 return GS_ERROR;
2467 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2468 decl. This allows us to eliminate redundant or useless
2469 calls to "const" functions. */
2470 if (TREE_CODE (*expr_p) == CALL_EXPR)
2472 int flags = call_expr_flags (*expr_p);
2473 if (flags & (ECF_CONST | ECF_PURE)
2474 /* An infinite loop is considered a side effect. */
2475 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2476 TREE_SIDE_EFFECTS (*expr_p) = 0;
2479 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2480 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2481 form and delegate the creation of a GIMPLE_CALL to
2482 gimplify_modify_expr. This is always possible because when
2483 WANT_VALUE is true, the caller wants the result of this call into
2484 a temporary, which means that we will emit an INIT_EXPR in
2485 internal_get_tmp_var which will then be handled by
2486 gimplify_modify_expr. */
2487 if (!want_value)
2489 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2490 have to do is replicate it as a GIMPLE_CALL tuple. */
2491 gimple_stmt_iterator gsi;
2492 call = gimple_build_call_from_tree (*expr_p);
2493 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2494 notice_special_calls (call);
2495 gimplify_seq_add_stmt (pre_p, call);
2496 gsi = gsi_last (*pre_p);
2497 maybe_fold_stmt (&gsi);
2498 *expr_p = NULL_TREE;
2500 else
2501 /* Remember the original function type. */
2502 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2503 CALL_EXPR_FN (*expr_p));
2505 return ret;
2508 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2509 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2511 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2512 condition is true or false, respectively. If null, we should generate
2513 our own to skip over the evaluation of this specific expression.
2515 LOCUS is the source location of the COND_EXPR.
2517 This function is the tree equivalent of do_jump.
2519 shortcut_cond_r should only be called by shortcut_cond_expr. */
2521 static tree
2522 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2523 location_t locus)
2525 tree local_label = NULL_TREE;
2526 tree t, expr = NULL;
2528 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2529 retain the shortcut semantics. Just insert the gotos here;
2530 shortcut_cond_expr will append the real blocks later. */
2531 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2533 location_t new_locus;
2535 /* Turn if (a && b) into
2537 if (a); else goto no;
2538 if (b) goto yes; else goto no;
2539 (no:) */
2541 if (false_label_p == NULL)
2542 false_label_p = &local_label;
2544 /* Keep the original source location on the first 'if'. */
2545 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2546 append_to_statement_list (t, &expr);
2548 /* Set the source location of the && on the second 'if'. */
2549 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2550 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2551 new_locus);
2552 append_to_statement_list (t, &expr);
2554 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2556 location_t new_locus;
2558 /* Turn if (a || b) into
2560 if (a) goto yes;
2561 if (b) goto yes; else goto no;
2562 (yes:) */
2564 if (true_label_p == NULL)
2565 true_label_p = &local_label;
2567 /* Keep the original source location on the first 'if'. */
2568 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2569 append_to_statement_list (t, &expr);
2571 /* Set the source location of the || on the second 'if'. */
2572 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2573 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2574 new_locus);
2575 append_to_statement_list (t, &expr);
2577 else if (TREE_CODE (pred) == COND_EXPR
2578 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2579 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2581 location_t new_locus;
2583 /* As long as we're messing with gotos, turn if (a ? b : c) into
2584 if (a)
2585 if (b) goto yes; else goto no;
2586 else
2587 if (c) goto yes; else goto no;
2589 Don't do this if one of the arms has void type, which can happen
2590 in C++ when the arm is throw. */
2592 /* Keep the original source location on the first 'if'. Set the source
2593 location of the ? on the second 'if'. */
2594 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2595 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2596 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2597 false_label_p, locus),
2598 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2599 false_label_p, new_locus));
2601 else
2603 expr = build3 (COND_EXPR, void_type_node, pred,
2604 build_and_jump (true_label_p),
2605 build_and_jump (false_label_p));
2606 SET_EXPR_LOCATION (expr, locus);
2609 if (local_label)
2611 t = build1 (LABEL_EXPR, void_type_node, local_label);
2612 append_to_statement_list (t, &expr);
2615 return expr;
2618 /* Given a conditional expression EXPR with short-circuit boolean
2619 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2620 predicate apart into the equivalent sequence of conditionals. */
2622 static tree
2623 shortcut_cond_expr (tree expr)
2625 tree pred = TREE_OPERAND (expr, 0);
2626 tree then_ = TREE_OPERAND (expr, 1);
2627 tree else_ = TREE_OPERAND (expr, 2);
2628 tree true_label, false_label, end_label, t;
2629 tree *true_label_p;
2630 tree *false_label_p;
2631 bool emit_end, emit_false, jump_over_else;
2632 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2633 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2635 /* First do simple transformations. */
2636 if (!else_se)
2638 /* If there is no 'else', turn
2639 if (a && b) then c
2640 into
2641 if (a) if (b) then c. */
2642 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2644 /* Keep the original source location on the first 'if'. */
2645 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2646 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2647 /* Set the source location of the && on the second 'if'. */
2648 if (EXPR_HAS_LOCATION (pred))
2649 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2650 then_ = shortcut_cond_expr (expr);
2651 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2652 pred = TREE_OPERAND (pred, 0);
2653 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2654 SET_EXPR_LOCATION (expr, locus);
2658 if (!then_se)
2660 /* If there is no 'then', turn
2661 if (a || b); else d
2662 into
2663 if (a); else if (b); else d. */
2664 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2666 /* Keep the original source location on the first 'if'. */
2667 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2668 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2669 /* Set the source location of the || on the second 'if'. */
2670 if (EXPR_HAS_LOCATION (pred))
2671 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2672 else_ = shortcut_cond_expr (expr);
2673 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2674 pred = TREE_OPERAND (pred, 0);
2675 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2676 SET_EXPR_LOCATION (expr, locus);
2680 /* If we're done, great. */
2681 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2682 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2683 return expr;
2685 /* Otherwise we need to mess with gotos. Change
2686 if (a) c; else d;
2688 if (a); else goto no;
2689 c; goto end;
2690 no: d; end:
2691 and recursively gimplify the condition. */
2693 true_label = false_label = end_label = NULL_TREE;
2695 /* If our arms just jump somewhere, hijack those labels so we don't
2696 generate jumps to jumps. */
2698 if (then_
2699 && TREE_CODE (then_) == GOTO_EXPR
2700 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2702 true_label = GOTO_DESTINATION (then_);
2703 then_ = NULL;
2704 then_se = false;
2707 if (else_
2708 && TREE_CODE (else_) == GOTO_EXPR
2709 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2711 false_label = GOTO_DESTINATION (else_);
2712 else_ = NULL;
2713 else_se = false;
2716 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2717 if (true_label)
2718 true_label_p = &true_label;
2719 else
2720 true_label_p = NULL;
2722 /* The 'else' branch also needs a label if it contains interesting code. */
2723 if (false_label || else_se)
2724 false_label_p = &false_label;
2725 else
2726 false_label_p = NULL;
2728 /* If there was nothing else in our arms, just forward the label(s). */
2729 if (!then_se && !else_se)
2730 return shortcut_cond_r (pred, true_label_p, false_label_p,
2731 EXPR_LOC_OR_LOC (expr, input_location));
2733 /* If our last subexpression already has a terminal label, reuse it. */
2734 if (else_se)
2735 t = expr_last (else_);
2736 else if (then_se)
2737 t = expr_last (then_);
2738 else
2739 t = NULL;
2740 if (t && TREE_CODE (t) == LABEL_EXPR)
2741 end_label = LABEL_EXPR_LABEL (t);
2743 /* If we don't care about jumping to the 'else' branch, jump to the end
2744 if the condition is false. */
2745 if (!false_label_p)
2746 false_label_p = &end_label;
2748 /* We only want to emit these labels if we aren't hijacking them. */
2749 emit_end = (end_label == NULL_TREE);
2750 emit_false = (false_label == NULL_TREE);
2752 /* We only emit the jump over the else clause if we have to--if the
2753 then clause may fall through. Otherwise we can wind up with a
2754 useless jump and a useless label at the end of gimplified code,
2755 which will cause us to think that this conditional as a whole
2756 falls through even if it doesn't. If we then inline a function
2757 which ends with such a condition, that can cause us to issue an
2758 inappropriate warning about control reaching the end of a
2759 non-void function. */
2760 jump_over_else = block_may_fallthru (then_);
2762 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2763 EXPR_LOC_OR_LOC (expr, input_location));
2765 expr = NULL;
2766 append_to_statement_list (pred, &expr);
2768 append_to_statement_list (then_, &expr);
2769 if (else_se)
2771 if (jump_over_else)
2773 tree last = expr_last (expr);
2774 t = build_and_jump (&end_label);
2775 if (EXPR_HAS_LOCATION (last))
2776 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2777 append_to_statement_list (t, &expr);
2779 if (emit_false)
2781 t = build1 (LABEL_EXPR, void_type_node, false_label);
2782 append_to_statement_list (t, &expr);
2784 append_to_statement_list (else_, &expr);
2786 if (emit_end && end_label)
2788 t = build1 (LABEL_EXPR, void_type_node, end_label);
2789 append_to_statement_list (t, &expr);
2792 return expr;
2795 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2797 tree
2798 gimple_boolify (tree expr)
2800 tree type = TREE_TYPE (expr);
2801 location_t loc = EXPR_LOCATION (expr);
2803 if (TREE_CODE (expr) == NE_EXPR
2804 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2805 && integer_zerop (TREE_OPERAND (expr, 1)))
2807 tree call = TREE_OPERAND (expr, 0);
2808 tree fn = get_callee_fndecl (call);
2810 /* For __builtin_expect ((long) (x), y) recurse into x as well
2811 if x is truth_value_p. */
2812 if (fn
2813 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2814 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2815 && call_expr_nargs (call) == 2)
2817 tree arg = CALL_EXPR_ARG (call, 0);
2818 if (arg)
2820 if (TREE_CODE (arg) == NOP_EXPR
2821 && TREE_TYPE (arg) == TREE_TYPE (call))
2822 arg = TREE_OPERAND (arg, 0);
2823 if (truth_value_p (TREE_CODE (arg)))
2825 arg = gimple_boolify (arg);
2826 CALL_EXPR_ARG (call, 0)
2827 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2833 switch (TREE_CODE (expr))
2835 case TRUTH_AND_EXPR:
2836 case TRUTH_OR_EXPR:
2837 case TRUTH_XOR_EXPR:
2838 case TRUTH_ANDIF_EXPR:
2839 case TRUTH_ORIF_EXPR:
2840 /* Also boolify the arguments of truth exprs. */
2841 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2842 /* FALLTHRU */
2844 case TRUTH_NOT_EXPR:
2845 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2847 /* These expressions always produce boolean results. */
2848 if (TREE_CODE (type) != BOOLEAN_TYPE)
2849 TREE_TYPE (expr) = boolean_type_node;
2850 return expr;
2852 case ANNOTATE_EXPR:
2853 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2855 case annot_expr_ivdep_kind:
2856 case annot_expr_no_vector_kind:
2857 case annot_expr_vector_kind:
2858 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2859 if (TREE_CODE (type) != BOOLEAN_TYPE)
2860 TREE_TYPE (expr) = boolean_type_node;
2861 return expr;
2862 default:
2863 gcc_unreachable ();
2866 default:
2867 if (COMPARISON_CLASS_P (expr))
2869 /* There expressions always prduce boolean results. */
2870 if (TREE_CODE (type) != BOOLEAN_TYPE)
2871 TREE_TYPE (expr) = boolean_type_node;
2872 return expr;
2874 /* Other expressions that get here must have boolean values, but
2875 might need to be converted to the appropriate mode. */
2876 if (TREE_CODE (type) == BOOLEAN_TYPE)
2877 return expr;
2878 return fold_convert_loc (loc, boolean_type_node, expr);
2882 /* Given a conditional expression *EXPR_P without side effects, gimplify
2883 its operands. New statements are inserted to PRE_P. */
2885 static enum gimplify_status
2886 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2888 tree expr = *expr_p, cond;
2889 enum gimplify_status ret, tret;
2890 enum tree_code code;
2892 cond = gimple_boolify (COND_EXPR_COND (expr));
2894 /* We need to handle && and || specially, as their gimplification
2895 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2896 code = TREE_CODE (cond);
2897 if (code == TRUTH_ANDIF_EXPR)
2898 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2899 else if (code == TRUTH_ORIF_EXPR)
2900 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2901 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2902 COND_EXPR_COND (*expr_p) = cond;
2904 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2905 is_gimple_val, fb_rvalue);
2906 ret = MIN (ret, tret);
2907 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2908 is_gimple_val, fb_rvalue);
2910 return MIN (ret, tret);
2913 /* Return true if evaluating EXPR could trap.
2914 EXPR is GENERIC, while tree_could_trap_p can be called
2915 only on GIMPLE. */
2917 static bool
2918 generic_expr_could_trap_p (tree expr)
2920 unsigned i, n;
2922 if (!expr || is_gimple_val (expr))
2923 return false;
2925 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2926 return true;
2928 n = TREE_OPERAND_LENGTH (expr);
2929 for (i = 0; i < n; i++)
2930 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2931 return true;
2933 return false;
2936 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2937 into
2939 if (p) if (p)
2940 t1 = a; a;
2941 else or else
2942 t1 = b; b;
2945 The second form is used when *EXPR_P is of type void.
2947 PRE_P points to the list where side effects that must happen before
2948 *EXPR_P should be stored. */
2950 static enum gimplify_status
2951 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2953 tree expr = *expr_p;
2954 tree type = TREE_TYPE (expr);
2955 location_t loc = EXPR_LOCATION (expr);
2956 tree tmp, arm1, arm2;
2957 enum gimplify_status ret;
2958 tree label_true, label_false, label_cont;
2959 bool have_then_clause_p, have_else_clause_p;
2960 gimple_cond gimple_cond;
2961 enum tree_code pred_code;
2962 gimple_seq seq = NULL;
2964 /* If this COND_EXPR has a value, copy the values into a temporary within
2965 the arms. */
2966 if (!VOID_TYPE_P (type))
2968 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2969 tree result;
2971 /* If either an rvalue is ok or we do not require an lvalue, create the
2972 temporary. But we cannot do that if the type is addressable. */
2973 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
2974 && !TREE_ADDRESSABLE (type))
2976 if (gimplify_ctxp->allow_rhs_cond_expr
2977 /* If either branch has side effects or could trap, it can't be
2978 evaluated unconditionally. */
2979 && !TREE_SIDE_EFFECTS (then_)
2980 && !generic_expr_could_trap_p (then_)
2981 && !TREE_SIDE_EFFECTS (else_)
2982 && !generic_expr_could_trap_p (else_))
2983 return gimplify_pure_cond_expr (expr_p, pre_p);
2985 tmp = create_tmp_var (type, "iftmp");
2986 result = tmp;
2989 /* Otherwise, only create and copy references to the values. */
2990 else
2992 type = build_pointer_type (type);
2994 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2995 then_ = build_fold_addr_expr_loc (loc, then_);
2997 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2998 else_ = build_fold_addr_expr_loc (loc, else_);
3000 expr
3001 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3003 tmp = create_tmp_var (type, "iftmp");
3004 result = build_simple_mem_ref_loc (loc, tmp);
3007 /* Build the new then clause, `tmp = then_;'. But don't build the
3008 assignment if the value is void; in C++ it can be if it's a throw. */
3009 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3010 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3012 /* Similarly, build the new else clause, `tmp = else_;'. */
3013 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3014 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3016 TREE_TYPE (expr) = void_type_node;
3017 recalculate_side_effects (expr);
3019 /* Move the COND_EXPR to the prequeue. */
3020 gimplify_stmt (&expr, pre_p);
3022 *expr_p = result;
3023 return GS_ALL_DONE;
3026 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3027 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3028 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3029 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3031 /* Make sure the condition has BOOLEAN_TYPE. */
3032 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3034 /* Break apart && and || conditions. */
3035 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3036 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3038 expr = shortcut_cond_expr (expr);
3040 if (expr != *expr_p)
3042 *expr_p = expr;
3044 /* We can't rely on gimplify_expr to re-gimplify the expanded
3045 form properly, as cleanups might cause the target labels to be
3046 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3047 set up a conditional context. */
3048 gimple_push_condition ();
3049 gimplify_stmt (expr_p, &seq);
3050 gimple_pop_condition (pre_p);
3051 gimple_seq_add_seq (pre_p, seq);
3053 return GS_ALL_DONE;
3057 /* Now do the normal gimplification. */
3059 /* Gimplify condition. */
3060 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3061 fb_rvalue);
3062 if (ret == GS_ERROR)
3063 return GS_ERROR;
3064 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3066 gimple_push_condition ();
3068 have_then_clause_p = have_else_clause_p = false;
3069 if (TREE_OPERAND (expr, 1) != NULL
3070 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3071 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3072 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3073 == current_function_decl)
3074 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3075 have different locations, otherwise we end up with incorrect
3076 location information on the branches. */
3077 && (optimize
3078 || !EXPR_HAS_LOCATION (expr)
3079 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3080 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3082 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3083 have_then_clause_p = true;
3085 else
3086 label_true = create_artificial_label (UNKNOWN_LOCATION);
3087 if (TREE_OPERAND (expr, 2) != NULL
3088 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3089 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3090 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3091 == current_function_decl)
3092 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3093 have different locations, otherwise we end up with incorrect
3094 location information on the branches. */
3095 && (optimize
3096 || !EXPR_HAS_LOCATION (expr)
3097 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3098 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3100 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3101 have_else_clause_p = true;
3103 else
3104 label_false = create_artificial_label (UNKNOWN_LOCATION);
3106 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3107 &arm2);
3109 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3110 label_false);
3112 gimplify_seq_add_stmt (&seq, gimple_cond);
3113 label_cont = NULL_TREE;
3114 if (!have_then_clause_p)
3116 /* For if (...) {} else { code; } put label_true after
3117 the else block. */
3118 if (TREE_OPERAND (expr, 1) == NULL_TREE
3119 && !have_else_clause_p
3120 && TREE_OPERAND (expr, 2) != NULL_TREE)
3121 label_cont = label_true;
3122 else
3124 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3125 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3126 /* For if (...) { code; } else {} or
3127 if (...) { code; } else goto label; or
3128 if (...) { code; return; } else { ... }
3129 label_cont isn't needed. */
3130 if (!have_else_clause_p
3131 && TREE_OPERAND (expr, 2) != NULL_TREE
3132 && gimple_seq_may_fallthru (seq))
3134 gimple g;
3135 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3137 g = gimple_build_goto (label_cont);
3139 /* GIMPLE_COND's are very low level; they have embedded
3140 gotos. This particular embedded goto should not be marked
3141 with the location of the original COND_EXPR, as it would
3142 correspond to the COND_EXPR's condition, not the ELSE or the
3143 THEN arms. To avoid marking it with the wrong location, flag
3144 it as "no location". */
3145 gimple_set_do_not_emit_location (g);
3147 gimplify_seq_add_stmt (&seq, g);
3151 if (!have_else_clause_p)
3153 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3154 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3156 if (label_cont)
3157 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3159 gimple_pop_condition (pre_p);
3160 gimple_seq_add_seq (pre_p, seq);
3162 if (ret == GS_ERROR)
3163 ; /* Do nothing. */
3164 else if (have_then_clause_p || have_else_clause_p)
3165 ret = GS_ALL_DONE;
3166 else
3168 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3169 expr = TREE_OPERAND (expr, 0);
3170 gimplify_stmt (&expr, pre_p);
3173 *expr_p = NULL;
3174 return ret;
3177 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3178 to be marked addressable.
3180 We cannot rely on such an expression being directly markable if a temporary
3181 has been created by the gimplification. In this case, we create another
3182 temporary and initialize it with a copy, which will become a store after we
3183 mark it addressable. This can happen if the front-end passed us something
3184 that it could not mark addressable yet, like a Fortran pass-by-reference
3185 parameter (int) floatvar. */
3187 static void
3188 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3190 while (handled_component_p (*expr_p))
3191 expr_p = &TREE_OPERAND (*expr_p, 0);
3192 if (is_gimple_reg (*expr_p))
3194 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3195 DECL_GIMPLE_REG_P (var) = 0;
3196 *expr_p = var;
3200 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3201 a call to __builtin_memcpy. */
3203 static enum gimplify_status
3204 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3205 gimple_seq *seq_p)
3207 tree t, to, to_ptr, from, from_ptr;
3208 gimple_call gs;
3209 location_t loc = EXPR_LOCATION (*expr_p);
3211 to = TREE_OPERAND (*expr_p, 0);
3212 from = TREE_OPERAND (*expr_p, 1);
3214 /* Mark the RHS addressable. Beware that it may not be possible to do so
3215 directly if a temporary has been created by the gimplification. */
3216 prepare_gimple_addressable (&from, seq_p);
3218 mark_addressable (from);
3219 from_ptr = build_fold_addr_expr_loc (loc, from);
3220 gimplify_arg (&from_ptr, seq_p, loc);
3222 mark_addressable (to);
3223 to_ptr = build_fold_addr_expr_loc (loc, to);
3224 gimplify_arg (&to_ptr, seq_p, loc);
3226 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3228 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3230 if (want_value)
3232 /* tmp = memcpy() */
3233 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3234 gimple_call_set_lhs (gs, t);
3235 gimplify_seq_add_stmt (seq_p, gs);
3237 *expr_p = build_simple_mem_ref (t);
3238 return GS_ALL_DONE;
3241 gimplify_seq_add_stmt (seq_p, gs);
3242 *expr_p = NULL;
3243 return GS_ALL_DONE;
3246 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3247 a call to __builtin_memset. In this case we know that the RHS is
3248 a CONSTRUCTOR with an empty element list. */
3250 static enum gimplify_status
3251 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3252 gimple_seq *seq_p)
3254 tree t, from, to, to_ptr;
3255 gimple_call gs;
3256 location_t loc = EXPR_LOCATION (*expr_p);
3258 /* Assert our assumptions, to abort instead of producing wrong code
3259 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3260 not be immediately exposed. */
3261 from = TREE_OPERAND (*expr_p, 1);
3262 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3263 from = TREE_OPERAND (from, 0);
3265 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3266 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3268 /* Now proceed. */
3269 to = TREE_OPERAND (*expr_p, 0);
3271 to_ptr = build_fold_addr_expr_loc (loc, to);
3272 gimplify_arg (&to_ptr, seq_p, loc);
3273 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3275 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3277 if (want_value)
3279 /* tmp = memset() */
3280 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3281 gimple_call_set_lhs (gs, t);
3282 gimplify_seq_add_stmt (seq_p, gs);
3284 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3285 return GS_ALL_DONE;
3288 gimplify_seq_add_stmt (seq_p, gs);
3289 *expr_p = NULL;
3290 return GS_ALL_DONE;
3293 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3294 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3295 assignment. Return non-null if we detect a potential overlap. */
3297 struct gimplify_init_ctor_preeval_data
3299 /* The base decl of the lhs object. May be NULL, in which case we
3300 have to assume the lhs is indirect. */
3301 tree lhs_base_decl;
3303 /* The alias set of the lhs object. */
3304 alias_set_type lhs_alias_set;
3307 static tree
3308 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3310 struct gimplify_init_ctor_preeval_data *data
3311 = (struct gimplify_init_ctor_preeval_data *) xdata;
3312 tree t = *tp;
3314 /* If we find the base object, obviously we have overlap. */
3315 if (data->lhs_base_decl == t)
3316 return t;
3318 /* If the constructor component is indirect, determine if we have a
3319 potential overlap with the lhs. The only bits of information we
3320 have to go on at this point are addressability and alias sets. */
3321 if ((INDIRECT_REF_P (t)
3322 || TREE_CODE (t) == MEM_REF)
3323 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3324 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3325 return t;
3327 /* If the constructor component is a call, determine if it can hide a
3328 potential overlap with the lhs through an INDIRECT_REF like above.
3329 ??? Ugh - this is completely broken. In fact this whole analysis
3330 doesn't look conservative. */
3331 if (TREE_CODE (t) == CALL_EXPR)
3333 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3335 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3336 if (POINTER_TYPE_P (TREE_VALUE (type))
3337 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3338 && alias_sets_conflict_p (data->lhs_alias_set,
3339 get_alias_set
3340 (TREE_TYPE (TREE_VALUE (type)))))
3341 return t;
3344 if (IS_TYPE_OR_DECL_P (t))
3345 *walk_subtrees = 0;
3346 return NULL;
3349 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3350 force values that overlap with the lhs (as described by *DATA)
3351 into temporaries. */
3353 static void
3354 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3355 struct gimplify_init_ctor_preeval_data *data)
3357 enum gimplify_status one;
3359 /* If the value is constant, then there's nothing to pre-evaluate. */
3360 if (TREE_CONSTANT (*expr_p))
3362 /* Ensure it does not have side effects, it might contain a reference to
3363 the object we're initializing. */
3364 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3365 return;
3368 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3369 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3370 return;
3372 /* Recurse for nested constructors. */
3373 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3375 unsigned HOST_WIDE_INT ix;
3376 constructor_elt *ce;
3377 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3379 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3380 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3382 return;
3385 /* If this is a variable sized type, we must remember the size. */
3386 maybe_with_size_expr (expr_p);
3388 /* Gimplify the constructor element to something appropriate for the rhs
3389 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3390 the gimplifier will consider this a store to memory. Doing this
3391 gimplification now means that we won't have to deal with complicated
3392 language-specific trees, nor trees like SAVE_EXPR that can induce
3393 exponential search behavior. */
3394 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3395 if (one == GS_ERROR)
3397 *expr_p = NULL;
3398 return;
3401 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3402 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3403 always be true for all scalars, since is_gimple_mem_rhs insists on a
3404 temporary variable for them. */
3405 if (DECL_P (*expr_p))
3406 return;
3408 /* If this is of variable size, we have no choice but to assume it doesn't
3409 overlap since we can't make a temporary for it. */
3410 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3411 return;
3413 /* Otherwise, we must search for overlap ... */
3414 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3415 return;
3417 /* ... and if found, force the value into a temporary. */
3418 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3421 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3422 a RANGE_EXPR in a CONSTRUCTOR for an array.
3424 var = lower;
3425 loop_entry:
3426 object[var] = value;
3427 if (var == upper)
3428 goto loop_exit;
3429 var = var + 1;
3430 goto loop_entry;
3431 loop_exit:
3433 We increment var _after_ the loop exit check because we might otherwise
3434 fail if upper == TYPE_MAX_VALUE (type for upper).
3436 Note that we never have to deal with SAVE_EXPRs here, because this has
3437 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3439 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3440 gimple_seq *, bool);
3442 static void
3443 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3444 tree value, tree array_elt_type,
3445 gimple_seq *pre_p, bool cleared)
3447 tree loop_entry_label, loop_exit_label, fall_thru_label;
3448 tree var, var_type, cref, tmp;
3450 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3451 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3452 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3454 /* Create and initialize the index variable. */
3455 var_type = TREE_TYPE (upper);
3456 var = create_tmp_var (var_type, NULL);
3457 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3459 /* Add the loop entry label. */
3460 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3462 /* Build the reference. */
3463 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3464 var, NULL_TREE, NULL_TREE);
3466 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3467 the store. Otherwise just assign value to the reference. */
3469 if (TREE_CODE (value) == CONSTRUCTOR)
3470 /* NB we might have to call ourself recursively through
3471 gimplify_init_ctor_eval if the value is a constructor. */
3472 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3473 pre_p, cleared);
3474 else
3475 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3477 /* We exit the loop when the index var is equal to the upper bound. */
3478 gimplify_seq_add_stmt (pre_p,
3479 gimple_build_cond (EQ_EXPR, var, upper,
3480 loop_exit_label, fall_thru_label));
3482 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3484 /* Otherwise, increment the index var... */
3485 tmp = build2 (PLUS_EXPR, var_type, var,
3486 fold_convert (var_type, integer_one_node));
3487 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3489 /* ...and jump back to the loop entry. */
3490 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3492 /* Add the loop exit label. */
3493 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3496 /* Return true if FDECL is accessing a field that is zero sized. */
3498 static bool
3499 zero_sized_field_decl (const_tree fdecl)
3501 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3502 && integer_zerop (DECL_SIZE (fdecl)))
3503 return true;
3504 return false;
3507 /* Return true if TYPE is zero sized. */
3509 static bool
3510 zero_sized_type (const_tree type)
3512 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3513 && integer_zerop (TYPE_SIZE (type)))
3514 return true;
3515 return false;
3518 /* A subroutine of gimplify_init_constructor. Generate individual
3519 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3520 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3521 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3522 zeroed first. */
3524 static void
3525 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3526 gimple_seq *pre_p, bool cleared)
3528 tree array_elt_type = NULL;
3529 unsigned HOST_WIDE_INT ix;
3530 tree purpose, value;
3532 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3533 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3535 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3537 tree cref;
3539 /* NULL values are created above for gimplification errors. */
3540 if (value == NULL)
3541 continue;
3543 if (cleared && initializer_zerop (value))
3544 continue;
3546 /* ??? Here's to hoping the front end fills in all of the indices,
3547 so we don't have to figure out what's missing ourselves. */
3548 gcc_assert (purpose);
3550 /* Skip zero-sized fields, unless value has side-effects. This can
3551 happen with calls to functions returning a zero-sized type, which
3552 we shouldn't discard. As a number of downstream passes don't
3553 expect sets of zero-sized fields, we rely on the gimplification of
3554 the MODIFY_EXPR we make below to drop the assignment statement. */
3555 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3556 continue;
3558 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3559 whole range. */
3560 if (TREE_CODE (purpose) == RANGE_EXPR)
3562 tree lower = TREE_OPERAND (purpose, 0);
3563 tree upper = TREE_OPERAND (purpose, 1);
3565 /* If the lower bound is equal to upper, just treat it as if
3566 upper was the index. */
3567 if (simple_cst_equal (lower, upper))
3568 purpose = upper;
3569 else
3571 gimplify_init_ctor_eval_range (object, lower, upper, value,
3572 array_elt_type, pre_p, cleared);
3573 continue;
3577 if (array_elt_type)
3579 /* Do not use bitsizetype for ARRAY_REF indices. */
3580 if (TYPE_DOMAIN (TREE_TYPE (object)))
3581 purpose
3582 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3583 purpose);
3584 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3585 purpose, NULL_TREE, NULL_TREE);
3587 else
3589 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3590 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3591 unshare_expr (object), purpose, NULL_TREE);
3594 if (TREE_CODE (value) == CONSTRUCTOR
3595 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3596 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3597 pre_p, cleared);
3598 else
3600 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3601 gimplify_and_add (init, pre_p);
3602 ggc_free (init);
3607 /* Return the appropriate RHS predicate for this LHS. */
3609 gimple_predicate
3610 rhs_predicate_for (tree lhs)
3612 if (is_gimple_reg (lhs))
3613 return is_gimple_reg_rhs_or_call;
3614 else
3615 return is_gimple_mem_rhs_or_call;
3618 /* Gimplify a C99 compound literal expression. This just means adding
3619 the DECL_EXPR before the current statement and using its anonymous
3620 decl instead. */
3622 static enum gimplify_status
3623 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3624 bool (*gimple_test_f) (tree),
3625 fallback_t fallback)
3627 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3628 tree decl = DECL_EXPR_DECL (decl_s);
3629 tree init = DECL_INITIAL (decl);
3630 /* Mark the decl as addressable if the compound literal
3631 expression is addressable now, otherwise it is marked too late
3632 after we gimplify the initialization expression. */
3633 if (TREE_ADDRESSABLE (*expr_p))
3634 TREE_ADDRESSABLE (decl) = 1;
3635 /* Otherwise, if we don't need an lvalue and have a literal directly
3636 substitute it. Check if it matches the gimple predicate, as
3637 otherwise we'd generate a new temporary, and we can as well just
3638 use the decl we already have. */
3639 else if (!TREE_ADDRESSABLE (decl)
3640 && init
3641 && (fallback & fb_lvalue) == 0
3642 && gimple_test_f (init))
3644 *expr_p = init;
3645 return GS_OK;
3648 /* Preliminarily mark non-addressed complex variables as eligible
3649 for promotion to gimple registers. We'll transform their uses
3650 as we find them. */
3651 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3652 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3653 && !TREE_THIS_VOLATILE (decl)
3654 && !needs_to_live_in_memory (decl))
3655 DECL_GIMPLE_REG_P (decl) = 1;
3657 /* If the decl is not addressable, then it is being used in some
3658 expression or on the right hand side of a statement, and it can
3659 be put into a readonly data section. */
3660 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3661 TREE_READONLY (decl) = 1;
3663 /* This decl isn't mentioned in the enclosing block, so add it to the
3664 list of temps. FIXME it seems a bit of a kludge to say that
3665 anonymous artificial vars aren't pushed, but everything else is. */
3666 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3667 gimple_add_tmp_var (decl);
3669 gimplify_and_add (decl_s, pre_p);
3670 *expr_p = decl;
3671 return GS_OK;
3674 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3675 return a new CONSTRUCTOR if something changed. */
3677 static tree
3678 optimize_compound_literals_in_ctor (tree orig_ctor)
3680 tree ctor = orig_ctor;
3681 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3682 unsigned int idx, num = vec_safe_length (elts);
3684 for (idx = 0; idx < num; idx++)
3686 tree value = (*elts)[idx].value;
3687 tree newval = value;
3688 if (TREE_CODE (value) == CONSTRUCTOR)
3689 newval = optimize_compound_literals_in_ctor (value);
3690 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3692 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3693 tree decl = DECL_EXPR_DECL (decl_s);
3694 tree init = DECL_INITIAL (decl);
3696 if (!TREE_ADDRESSABLE (value)
3697 && !TREE_ADDRESSABLE (decl)
3698 && init
3699 && TREE_CODE (init) == CONSTRUCTOR)
3700 newval = optimize_compound_literals_in_ctor (init);
3702 if (newval == value)
3703 continue;
3705 if (ctor == orig_ctor)
3707 ctor = copy_node (orig_ctor);
3708 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3709 elts = CONSTRUCTOR_ELTS (ctor);
3711 (*elts)[idx].value = newval;
3713 return ctor;
3716 /* A subroutine of gimplify_modify_expr. Break out elements of a
3717 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3719 Note that we still need to clear any elements that don't have explicit
3720 initializers, so if not all elements are initialized we keep the
3721 original MODIFY_EXPR, we just remove all of the constructor elements.
3723 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3724 GS_ERROR if we would have to create a temporary when gimplifying
3725 this constructor. Otherwise, return GS_OK.
3727 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3729 static enum gimplify_status
3730 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3731 bool want_value, bool notify_temp_creation)
3733 tree object, ctor, type;
3734 enum gimplify_status ret;
3735 vec<constructor_elt, va_gc> *elts;
3737 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3739 if (!notify_temp_creation)
3741 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3742 is_gimple_lvalue, fb_lvalue);
3743 if (ret == GS_ERROR)
3744 return ret;
3747 object = TREE_OPERAND (*expr_p, 0);
3748 ctor = TREE_OPERAND (*expr_p, 1) =
3749 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3750 type = TREE_TYPE (ctor);
3751 elts = CONSTRUCTOR_ELTS (ctor);
3752 ret = GS_ALL_DONE;
3754 switch (TREE_CODE (type))
3756 case RECORD_TYPE:
3757 case UNION_TYPE:
3758 case QUAL_UNION_TYPE:
3759 case ARRAY_TYPE:
3761 struct gimplify_init_ctor_preeval_data preeval_data;
3762 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3763 bool cleared, complete_p, valid_const_initializer;
3765 /* Aggregate types must lower constructors to initialization of
3766 individual elements. The exception is that a CONSTRUCTOR node
3767 with no elements indicates zero-initialization of the whole. */
3768 if (vec_safe_is_empty (elts))
3770 if (notify_temp_creation)
3771 return GS_OK;
3772 break;
3775 /* Fetch information about the constructor to direct later processing.
3776 We might want to make static versions of it in various cases, and
3777 can only do so if it known to be a valid constant initializer. */
3778 valid_const_initializer
3779 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3780 &num_ctor_elements, &complete_p);
3782 /* If a const aggregate variable is being initialized, then it
3783 should never be a lose to promote the variable to be static. */
3784 if (valid_const_initializer
3785 && num_nonzero_elements > 1
3786 && TREE_READONLY (object)
3787 && TREE_CODE (object) == VAR_DECL
3788 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3790 if (notify_temp_creation)
3791 return GS_ERROR;
3792 DECL_INITIAL (object) = ctor;
3793 TREE_STATIC (object) = 1;
3794 if (!DECL_NAME (object))
3795 DECL_NAME (object) = create_tmp_var_name ("C");
3796 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3798 /* ??? C++ doesn't automatically append a .<number> to the
3799 assembler name, and even when it does, it looks at FE private
3800 data structures to figure out what that number should be,
3801 which are not set for this variable. I suppose this is
3802 important for local statics for inline functions, which aren't
3803 "local" in the object file sense. So in order to get a unique
3804 TU-local symbol, we must invoke the lhd version now. */
3805 lhd_set_decl_assembler_name (object);
3807 *expr_p = NULL_TREE;
3808 break;
3811 /* If there are "lots" of initialized elements, even discounting
3812 those that are not address constants (and thus *must* be
3813 computed at runtime), then partition the constructor into
3814 constant and non-constant parts. Block copy the constant
3815 parts in, then generate code for the non-constant parts. */
3816 /* TODO. There's code in cp/typeck.c to do this. */
3818 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3819 /* store_constructor will ignore the clearing of variable-sized
3820 objects. Initializers for such objects must explicitly set
3821 every field that needs to be set. */
3822 cleared = false;
3823 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3824 /* If the constructor isn't complete, clear the whole object
3825 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3827 ??? This ought not to be needed. For any element not present
3828 in the initializer, we should simply set them to zero. Except
3829 we'd need to *find* the elements that are not present, and that
3830 requires trickery to avoid quadratic compile-time behavior in
3831 large cases or excessive memory use in small cases. */
3832 cleared = true;
3833 else if (num_ctor_elements - num_nonzero_elements
3834 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3835 && num_nonzero_elements < num_ctor_elements / 4)
3836 /* If there are "lots" of zeros, it's more efficient to clear
3837 the memory and then set the nonzero elements. */
3838 cleared = true;
3839 else
3840 cleared = false;
3842 /* If there are "lots" of initialized elements, and all of them
3843 are valid address constants, then the entire initializer can
3844 be dropped to memory, and then memcpy'd out. Don't do this
3845 for sparse arrays, though, as it's more efficient to follow
3846 the standard CONSTRUCTOR behavior of memset followed by
3847 individual element initialization. Also don't do this for small
3848 all-zero initializers (which aren't big enough to merit
3849 clearing), and don't try to make bitwise copies of
3850 TREE_ADDRESSABLE types. */
3851 if (valid_const_initializer
3852 && !(cleared || num_nonzero_elements == 0)
3853 && !TREE_ADDRESSABLE (type))
3855 HOST_WIDE_INT size = int_size_in_bytes (type);
3856 unsigned int align;
3858 /* ??? We can still get unbounded array types, at least
3859 from the C++ front end. This seems wrong, but attempt
3860 to work around it for now. */
3861 if (size < 0)
3863 size = int_size_in_bytes (TREE_TYPE (object));
3864 if (size >= 0)
3865 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3868 /* Find the maximum alignment we can assume for the object. */
3869 /* ??? Make use of DECL_OFFSET_ALIGN. */
3870 if (DECL_P (object))
3871 align = DECL_ALIGN (object);
3872 else
3873 align = TYPE_ALIGN (type);
3875 /* Do a block move either if the size is so small as to make
3876 each individual move a sub-unit move on average, or if it
3877 is so large as to make individual moves inefficient. */
3878 if (size > 0
3879 && num_nonzero_elements > 1
3880 && (size < num_nonzero_elements
3881 || !can_move_by_pieces (size, align)))
3883 if (notify_temp_creation)
3884 return GS_ERROR;
3886 walk_tree (&ctor, force_labels_r, NULL, NULL);
3887 ctor = tree_output_constant_def (ctor);
3888 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3889 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3890 TREE_OPERAND (*expr_p, 1) = ctor;
3892 /* This is no longer an assignment of a CONSTRUCTOR, but
3893 we still may have processing to do on the LHS. So
3894 pretend we didn't do anything here to let that happen. */
3895 return GS_UNHANDLED;
3899 /* If the target is volatile, we have non-zero elements and more than
3900 one field to assign, initialize the target from a temporary. */
3901 if (TREE_THIS_VOLATILE (object)
3902 && !TREE_ADDRESSABLE (type)
3903 && num_nonzero_elements > 0
3904 && vec_safe_length (elts) > 1)
3906 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3907 TREE_OPERAND (*expr_p, 0) = temp;
3908 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3909 *expr_p,
3910 build2 (MODIFY_EXPR, void_type_node,
3911 object, temp));
3912 return GS_OK;
3915 if (notify_temp_creation)
3916 return GS_OK;
3918 /* If there are nonzero elements and if needed, pre-evaluate to capture
3919 elements overlapping with the lhs into temporaries. We must do this
3920 before clearing to fetch the values before they are zeroed-out. */
3921 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3923 preeval_data.lhs_base_decl = get_base_address (object);
3924 if (!DECL_P (preeval_data.lhs_base_decl))
3925 preeval_data.lhs_base_decl = NULL;
3926 preeval_data.lhs_alias_set = get_alias_set (object);
3928 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3929 pre_p, post_p, &preeval_data);
3932 if (cleared)
3934 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3935 Note that we still have to gimplify, in order to handle the
3936 case of variable sized types. Avoid shared tree structures. */
3937 CONSTRUCTOR_ELTS (ctor) = NULL;
3938 TREE_SIDE_EFFECTS (ctor) = 0;
3939 object = unshare_expr (object);
3940 gimplify_stmt (expr_p, pre_p);
3943 /* If we have not block cleared the object, or if there are nonzero
3944 elements in the constructor, add assignments to the individual
3945 scalar fields of the object. */
3946 if (!cleared || num_nonzero_elements > 0)
3947 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3949 *expr_p = NULL_TREE;
3951 break;
3953 case COMPLEX_TYPE:
3955 tree r, i;
3957 if (notify_temp_creation)
3958 return GS_OK;
3960 /* Extract the real and imaginary parts out of the ctor. */
3961 gcc_assert (elts->length () == 2);
3962 r = (*elts)[0].value;
3963 i = (*elts)[1].value;
3964 if (r == NULL || i == NULL)
3966 tree zero = build_zero_cst (TREE_TYPE (type));
3967 if (r == NULL)
3968 r = zero;
3969 if (i == NULL)
3970 i = zero;
3973 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3974 represent creation of a complex value. */
3975 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3977 ctor = build_complex (type, r, i);
3978 TREE_OPERAND (*expr_p, 1) = ctor;
3980 else
3982 ctor = build2 (COMPLEX_EXPR, type, r, i);
3983 TREE_OPERAND (*expr_p, 1) = ctor;
3984 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
3985 pre_p,
3986 post_p,
3987 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3988 fb_rvalue);
3991 break;
3993 case VECTOR_TYPE:
3995 unsigned HOST_WIDE_INT ix;
3996 constructor_elt *ce;
3998 if (notify_temp_creation)
3999 return GS_OK;
4001 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4002 if (TREE_CONSTANT (ctor))
4004 bool constant_p = true;
4005 tree value;
4007 /* Even when ctor is constant, it might contain non-*_CST
4008 elements, such as addresses or trapping values like
4009 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4010 in VECTOR_CST nodes. */
4011 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4012 if (!CONSTANT_CLASS_P (value))
4014 constant_p = false;
4015 break;
4018 if (constant_p)
4020 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4021 break;
4024 TREE_CONSTANT (ctor) = 0;
4027 /* Vector types use CONSTRUCTOR all the way through gimple
4028 compilation as a general initializer. */
4029 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4031 enum gimplify_status tret;
4032 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4033 fb_rvalue);
4034 if (tret == GS_ERROR)
4035 ret = GS_ERROR;
4037 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4038 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4040 break;
4042 default:
4043 /* So how did we get a CONSTRUCTOR for a scalar type? */
4044 gcc_unreachable ();
4047 if (ret == GS_ERROR)
4048 return GS_ERROR;
4049 else if (want_value)
4051 *expr_p = object;
4052 return GS_OK;
4054 else
4056 /* If we have gimplified both sides of the initializer but have
4057 not emitted an assignment, do so now. */
4058 if (*expr_p)
4060 tree lhs = TREE_OPERAND (*expr_p, 0);
4061 tree rhs = TREE_OPERAND (*expr_p, 1);
4062 gimple_assign init = gimple_build_assign (lhs, rhs);
4063 gimplify_seq_add_stmt (pre_p, init);
4064 *expr_p = NULL;
4067 return GS_ALL_DONE;
4071 /* Given a pointer value OP0, return a simplified version of an
4072 indirection through OP0, or NULL_TREE if no simplification is
4073 possible. This may only be applied to a rhs of an expression.
4074 Note that the resulting type may be different from the type pointed
4075 to in the sense that it is still compatible from the langhooks
4076 point of view. */
4078 static tree
4079 gimple_fold_indirect_ref_rhs (tree t)
4081 return gimple_fold_indirect_ref (t);
4084 /* Subroutine of gimplify_modify_expr to do simplifications of
4085 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4086 something changes. */
4088 static enum gimplify_status
4089 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4090 gimple_seq *pre_p, gimple_seq *post_p,
4091 bool want_value)
4093 enum gimplify_status ret = GS_UNHANDLED;
4094 bool changed;
4098 changed = false;
4099 switch (TREE_CODE (*from_p))
4101 case VAR_DECL:
4102 /* If we're assigning from a read-only variable initialized with
4103 a constructor, do the direct assignment from the constructor,
4104 but only if neither source nor target are volatile since this
4105 latter assignment might end up being done on a per-field basis. */
4106 if (DECL_INITIAL (*from_p)
4107 && TREE_READONLY (*from_p)
4108 && !TREE_THIS_VOLATILE (*from_p)
4109 && !TREE_THIS_VOLATILE (*to_p)
4110 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4112 tree old_from = *from_p;
4113 enum gimplify_status subret;
4115 /* Move the constructor into the RHS. */
4116 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4118 /* Let's see if gimplify_init_constructor will need to put
4119 it in memory. */
4120 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4121 false, true);
4122 if (subret == GS_ERROR)
4124 /* If so, revert the change. */
4125 *from_p = old_from;
4127 else
4129 ret = GS_OK;
4130 changed = true;
4133 break;
4134 case INDIRECT_REF:
4136 /* If we have code like
4138 *(const A*)(A*)&x
4140 where the type of "x" is a (possibly cv-qualified variant
4141 of "A"), treat the entire expression as identical to "x".
4142 This kind of code arises in C++ when an object is bound
4143 to a const reference, and if "x" is a TARGET_EXPR we want
4144 to take advantage of the optimization below. */
4145 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4146 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4147 if (t)
4149 if (TREE_THIS_VOLATILE (t) != volatile_p)
4151 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4152 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4153 build_fold_addr_expr (t));
4154 if (REFERENCE_CLASS_P (t))
4155 TREE_THIS_VOLATILE (t) = volatile_p;
4157 *from_p = t;
4158 ret = GS_OK;
4159 changed = true;
4161 break;
4164 case TARGET_EXPR:
4166 /* If we are initializing something from a TARGET_EXPR, strip the
4167 TARGET_EXPR and initialize it directly, if possible. This can't
4168 be done if the initializer is void, since that implies that the
4169 temporary is set in some non-trivial way.
4171 ??? What about code that pulls out the temp and uses it
4172 elsewhere? I think that such code never uses the TARGET_EXPR as
4173 an initializer. If I'm wrong, we'll die because the temp won't
4174 have any RTL. In that case, I guess we'll need to replace
4175 references somehow. */
4176 tree init = TARGET_EXPR_INITIAL (*from_p);
4178 if (init
4179 && !VOID_TYPE_P (TREE_TYPE (init)))
4181 *from_p = init;
4182 ret = GS_OK;
4183 changed = true;
4186 break;
4188 case COMPOUND_EXPR:
4189 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4190 caught. */
4191 gimplify_compound_expr (from_p, pre_p, true);
4192 ret = GS_OK;
4193 changed = true;
4194 break;
4196 case CONSTRUCTOR:
4197 /* If we already made some changes, let the front end have a
4198 crack at this before we break it down. */
4199 if (ret != GS_UNHANDLED)
4200 break;
4201 /* If we're initializing from a CONSTRUCTOR, break this into
4202 individual MODIFY_EXPRs. */
4203 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4204 false);
4206 case COND_EXPR:
4207 /* If we're assigning to a non-register type, push the assignment
4208 down into the branches. This is mandatory for ADDRESSABLE types,
4209 since we cannot generate temporaries for such, but it saves a
4210 copy in other cases as well. */
4211 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4213 /* This code should mirror the code in gimplify_cond_expr. */
4214 enum tree_code code = TREE_CODE (*expr_p);
4215 tree cond = *from_p;
4216 tree result = *to_p;
4218 ret = gimplify_expr (&result, pre_p, post_p,
4219 is_gimple_lvalue, fb_lvalue);
4220 if (ret != GS_ERROR)
4221 ret = GS_OK;
4223 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4224 TREE_OPERAND (cond, 1)
4225 = build2 (code, void_type_node, result,
4226 TREE_OPERAND (cond, 1));
4227 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4228 TREE_OPERAND (cond, 2)
4229 = build2 (code, void_type_node, unshare_expr (result),
4230 TREE_OPERAND (cond, 2));
4232 TREE_TYPE (cond) = void_type_node;
4233 recalculate_side_effects (cond);
4235 if (want_value)
4237 gimplify_and_add (cond, pre_p);
4238 *expr_p = unshare_expr (result);
4240 else
4241 *expr_p = cond;
4242 return ret;
4244 break;
4246 case CALL_EXPR:
4247 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4248 return slot so that we don't generate a temporary. */
4249 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4250 && aggregate_value_p (*from_p, *from_p))
4252 bool use_target;
4254 if (!(rhs_predicate_for (*to_p))(*from_p))
4255 /* If we need a temporary, *to_p isn't accurate. */
4256 use_target = false;
4257 /* It's OK to use the return slot directly unless it's an NRV. */
4258 else if (TREE_CODE (*to_p) == RESULT_DECL
4259 && DECL_NAME (*to_p) == NULL_TREE
4260 && needs_to_live_in_memory (*to_p))
4261 use_target = true;
4262 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4263 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4264 /* Don't force regs into memory. */
4265 use_target = false;
4266 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4267 /* It's OK to use the target directly if it's being
4268 initialized. */
4269 use_target = true;
4270 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4271 /* Always use the target and thus RSO for variable-sized types.
4272 GIMPLE cannot deal with a variable-sized assignment
4273 embedded in a call statement. */
4274 use_target = true;
4275 else if (TREE_CODE (*to_p) != SSA_NAME
4276 && (!is_gimple_variable (*to_p)
4277 || needs_to_live_in_memory (*to_p)))
4278 /* Don't use the original target if it's already addressable;
4279 if its address escapes, and the called function uses the
4280 NRV optimization, a conforming program could see *to_p
4281 change before the called function returns; see c++/19317.
4282 When optimizing, the return_slot pass marks more functions
4283 as safe after we have escape info. */
4284 use_target = false;
4285 else
4286 use_target = true;
4288 if (use_target)
4290 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4291 mark_addressable (*to_p);
4294 break;
4296 case WITH_SIZE_EXPR:
4297 /* Likewise for calls that return an aggregate of non-constant size,
4298 since we would not be able to generate a temporary at all. */
4299 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4301 *from_p = TREE_OPERAND (*from_p, 0);
4302 /* We don't change ret in this case because the
4303 WITH_SIZE_EXPR might have been added in
4304 gimplify_modify_expr, so returning GS_OK would lead to an
4305 infinite loop. */
4306 changed = true;
4308 break;
4310 /* If we're initializing from a container, push the initialization
4311 inside it. */
4312 case CLEANUP_POINT_EXPR:
4313 case BIND_EXPR:
4314 case STATEMENT_LIST:
4316 tree wrap = *from_p;
4317 tree t;
4319 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4320 fb_lvalue);
4321 if (ret != GS_ERROR)
4322 ret = GS_OK;
4324 t = voidify_wrapper_expr (wrap, *expr_p);
4325 gcc_assert (t == *expr_p);
4327 if (want_value)
4329 gimplify_and_add (wrap, pre_p);
4330 *expr_p = unshare_expr (*to_p);
4332 else
4333 *expr_p = wrap;
4334 return GS_OK;
4337 case COMPOUND_LITERAL_EXPR:
4339 tree complit = TREE_OPERAND (*expr_p, 1);
4340 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4341 tree decl = DECL_EXPR_DECL (decl_s);
4342 tree init = DECL_INITIAL (decl);
4344 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4345 into struct T x = { 0, 1, 2 } if the address of the
4346 compound literal has never been taken. */
4347 if (!TREE_ADDRESSABLE (complit)
4348 && !TREE_ADDRESSABLE (decl)
4349 && init)
4351 *expr_p = copy_node (*expr_p);
4352 TREE_OPERAND (*expr_p, 1) = init;
4353 return GS_OK;
4357 default:
4358 break;
4361 while (changed);
4363 return ret;
4367 /* Return true if T looks like a valid GIMPLE statement. */
4369 static bool
4370 is_gimple_stmt (tree t)
4372 const enum tree_code code = TREE_CODE (t);
4374 switch (code)
4376 case NOP_EXPR:
4377 /* The only valid NOP_EXPR is the empty statement. */
4378 return IS_EMPTY_STMT (t);
4380 case BIND_EXPR:
4381 case COND_EXPR:
4382 /* These are only valid if they're void. */
4383 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4385 case SWITCH_EXPR:
4386 case GOTO_EXPR:
4387 case RETURN_EXPR:
4388 case LABEL_EXPR:
4389 case CASE_LABEL_EXPR:
4390 case TRY_CATCH_EXPR:
4391 case TRY_FINALLY_EXPR:
4392 case EH_FILTER_EXPR:
4393 case CATCH_EXPR:
4394 case ASM_EXPR:
4395 case STATEMENT_LIST:
4396 case OMP_PARALLEL:
4397 case OMP_FOR:
4398 case OMP_SIMD:
4399 case CILK_SIMD:
4400 case OMP_DISTRIBUTE:
4401 case OMP_SECTIONS:
4402 case OMP_SECTION:
4403 case OMP_SINGLE:
4404 case OMP_MASTER:
4405 case OMP_TASKGROUP:
4406 case OMP_ORDERED:
4407 case OMP_CRITICAL:
4408 case OMP_TASK:
4409 /* These are always void. */
4410 return true;
4412 case CALL_EXPR:
4413 case MODIFY_EXPR:
4414 case PREDICT_EXPR:
4415 /* These are valid regardless of their type. */
4416 return true;
4418 default:
4419 return false;
4424 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4425 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4426 DECL_GIMPLE_REG_P set.
4428 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4429 other, unmodified part of the complex object just before the total store.
4430 As a consequence, if the object is still uninitialized, an undefined value
4431 will be loaded into a register, which may result in a spurious exception
4432 if the register is floating-point and the value happens to be a signaling
4433 NaN for example. Then the fully-fledged complex operations lowering pass
4434 followed by a DCE pass are necessary in order to fix things up. */
4436 static enum gimplify_status
4437 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4438 bool want_value)
4440 enum tree_code code, ocode;
4441 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4443 lhs = TREE_OPERAND (*expr_p, 0);
4444 rhs = TREE_OPERAND (*expr_p, 1);
4445 code = TREE_CODE (lhs);
4446 lhs = TREE_OPERAND (lhs, 0);
4448 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4449 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4450 TREE_NO_WARNING (other) = 1;
4451 other = get_formal_tmp_var (other, pre_p);
4453 realpart = code == REALPART_EXPR ? rhs : other;
4454 imagpart = code == REALPART_EXPR ? other : rhs;
4456 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4457 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4458 else
4459 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4461 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4462 *expr_p = (want_value) ? rhs : NULL_TREE;
4464 return GS_ALL_DONE;
4467 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4469 modify_expr
4470 : varname '=' rhs
4471 | '*' ID '=' rhs
4473 PRE_P points to the list where side effects that must happen before
4474 *EXPR_P should be stored.
4476 POST_P points to the list where side effects that must happen after
4477 *EXPR_P should be stored.
4479 WANT_VALUE is nonzero iff we want to use the value of this expression
4480 in another expression. */
4482 static enum gimplify_status
4483 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4484 bool want_value)
4486 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4487 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4488 enum gimplify_status ret = GS_UNHANDLED;
4489 gimple assign;
4490 location_t loc = EXPR_LOCATION (*expr_p);
4491 gimple_stmt_iterator gsi;
4493 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4494 || TREE_CODE (*expr_p) == INIT_EXPR);
4496 /* Trying to simplify a clobber using normal logic doesn't work,
4497 so handle it here. */
4498 if (TREE_CLOBBER_P (*from_p))
4500 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4501 if (ret == GS_ERROR)
4502 return ret;
4503 gcc_assert (!want_value
4504 && (TREE_CODE (*to_p) == VAR_DECL
4505 || TREE_CODE (*to_p) == MEM_REF));
4506 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4507 *expr_p = NULL;
4508 return GS_ALL_DONE;
4511 /* Insert pointer conversions required by the middle-end that are not
4512 required by the frontend. This fixes middle-end type checking for
4513 for example gcc.dg/redecl-6.c. */
4514 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4516 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4517 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4518 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4521 /* See if any simplifications can be done based on what the RHS is. */
4522 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4523 want_value);
4524 if (ret != GS_UNHANDLED)
4525 return ret;
4527 /* For zero sized types only gimplify the left hand side and right hand
4528 side as statements and throw away the assignment. Do this after
4529 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4530 types properly. */
4531 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4533 gimplify_stmt (from_p, pre_p);
4534 gimplify_stmt (to_p, pre_p);
4535 *expr_p = NULL_TREE;
4536 return GS_ALL_DONE;
4539 /* If the value being copied is of variable width, compute the length
4540 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4541 before gimplifying any of the operands so that we can resolve any
4542 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4543 the size of the expression to be copied, not of the destination, so
4544 that is what we must do here. */
4545 maybe_with_size_expr (from_p);
4547 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4548 if (ret == GS_ERROR)
4549 return ret;
4551 /* As a special case, we have to temporarily allow for assignments
4552 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4553 a toplevel statement, when gimplifying the GENERIC expression
4554 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4555 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4557 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4558 prevent gimplify_expr from trying to create a new temporary for
4559 foo's LHS, we tell it that it should only gimplify until it
4560 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4561 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4562 and all we need to do here is set 'a' to be its LHS. */
4563 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4564 fb_rvalue);
4565 if (ret == GS_ERROR)
4566 return ret;
4568 /* Now see if the above changed *from_p to something we handle specially. */
4569 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4570 want_value);
4571 if (ret != GS_UNHANDLED)
4572 return ret;
4574 /* If we've got a variable sized assignment between two lvalues (i.e. does
4575 not involve a call), then we can make things a bit more straightforward
4576 by converting the assignment to memcpy or memset. */
4577 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4579 tree from = TREE_OPERAND (*from_p, 0);
4580 tree size = TREE_OPERAND (*from_p, 1);
4582 if (TREE_CODE (from) == CONSTRUCTOR)
4583 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4585 if (is_gimple_addressable (from))
4587 *from_p = from;
4588 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4589 pre_p);
4593 /* Transform partial stores to non-addressable complex variables into
4594 total stores. This allows us to use real instead of virtual operands
4595 for these variables, which improves optimization. */
4596 if ((TREE_CODE (*to_p) == REALPART_EXPR
4597 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4598 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4599 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4601 /* Try to alleviate the effects of the gimplification creating artificial
4602 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4603 if (!gimplify_ctxp->into_ssa
4604 && TREE_CODE (*from_p) == VAR_DECL
4605 && DECL_IGNORED_P (*from_p)
4606 && DECL_P (*to_p)
4607 && !DECL_IGNORED_P (*to_p))
4609 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4610 DECL_NAME (*from_p)
4611 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4612 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4613 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4616 if (want_value && TREE_THIS_VOLATILE (*to_p))
4617 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4619 if (TREE_CODE (*from_p) == CALL_EXPR)
4621 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4622 instead of a GIMPLE_ASSIGN. */
4623 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4624 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4625 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4626 tree fndecl = get_callee_fndecl (*from_p);
4627 gimple_call call_stmt;
4628 if (fndecl
4629 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4630 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4631 && call_expr_nargs (*from_p) == 3)
4632 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4633 CALL_EXPR_ARG (*from_p, 0),
4634 CALL_EXPR_ARG (*from_p, 1),
4635 CALL_EXPR_ARG (*from_p, 2));
4636 else
4638 call_stmt = gimple_build_call_from_tree (*from_p);
4639 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
4641 notice_special_calls (call_stmt);
4642 if (!gimple_call_noreturn_p (call_stmt))
4643 gimple_call_set_lhs (call_stmt, *to_p);
4644 assign = call_stmt;
4646 else
4648 assign = gimple_build_assign (*to_p, *from_p);
4649 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4652 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4654 /* We should have got an SSA name from the start. */
4655 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4658 gimplify_seq_add_stmt (pre_p, assign);
4659 gsi = gsi_last (*pre_p);
4660 maybe_fold_stmt (&gsi);
4662 if (want_value)
4664 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4665 return GS_OK;
4667 else
4668 *expr_p = NULL;
4670 return GS_ALL_DONE;
4673 /* Gimplify a comparison between two variable-sized objects. Do this
4674 with a call to BUILT_IN_MEMCMP. */
4676 static enum gimplify_status
4677 gimplify_variable_sized_compare (tree *expr_p)
4679 location_t loc = EXPR_LOCATION (*expr_p);
4680 tree op0 = TREE_OPERAND (*expr_p, 0);
4681 tree op1 = TREE_OPERAND (*expr_p, 1);
4682 tree t, arg, dest, src, expr;
4684 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4685 arg = unshare_expr (arg);
4686 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4687 src = build_fold_addr_expr_loc (loc, op1);
4688 dest = build_fold_addr_expr_loc (loc, op0);
4689 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4690 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4692 expr
4693 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4694 SET_EXPR_LOCATION (expr, loc);
4695 *expr_p = expr;
4697 return GS_OK;
4700 /* Gimplify a comparison between two aggregate objects of integral scalar
4701 mode as a comparison between the bitwise equivalent scalar values. */
4703 static enum gimplify_status
4704 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4706 location_t loc = EXPR_LOCATION (*expr_p);
4707 tree op0 = TREE_OPERAND (*expr_p, 0);
4708 tree op1 = TREE_OPERAND (*expr_p, 1);
4710 tree type = TREE_TYPE (op0);
4711 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4713 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4714 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4716 *expr_p
4717 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4719 return GS_OK;
4722 /* Gimplify an expression sequence. This function gimplifies each
4723 expression and rewrites the original expression with the last
4724 expression of the sequence in GIMPLE form.
4726 PRE_P points to the list where the side effects for all the
4727 expressions in the sequence will be emitted.
4729 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4731 static enum gimplify_status
4732 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4734 tree t = *expr_p;
4738 tree *sub_p = &TREE_OPERAND (t, 0);
4740 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4741 gimplify_compound_expr (sub_p, pre_p, false);
4742 else
4743 gimplify_stmt (sub_p, pre_p);
4745 t = TREE_OPERAND (t, 1);
4747 while (TREE_CODE (t) == COMPOUND_EXPR);
4749 *expr_p = t;
4750 if (want_value)
4751 return GS_OK;
4752 else
4754 gimplify_stmt (expr_p, pre_p);
4755 return GS_ALL_DONE;
4759 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4760 gimplify. After gimplification, EXPR_P will point to a new temporary
4761 that holds the original value of the SAVE_EXPR node.
4763 PRE_P points to the list where side effects that must happen before
4764 *EXPR_P should be stored. */
4766 static enum gimplify_status
4767 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4769 enum gimplify_status ret = GS_ALL_DONE;
4770 tree val;
4772 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4773 val = TREE_OPERAND (*expr_p, 0);
4775 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4776 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4778 /* The operand may be a void-valued expression such as SAVE_EXPRs
4779 generated by the Java frontend for class initialization. It is
4780 being executed only for its side-effects. */
4781 if (TREE_TYPE (val) == void_type_node)
4783 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4784 is_gimple_stmt, fb_none);
4785 val = NULL;
4787 else
4788 val = get_initialized_tmp_var (val, pre_p, post_p);
4790 TREE_OPERAND (*expr_p, 0) = val;
4791 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4794 *expr_p = val;
4796 return ret;
4799 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4801 unary_expr
4802 : ...
4803 | '&' varname
4806 PRE_P points to the list where side effects that must happen before
4807 *EXPR_P should be stored.
4809 POST_P points to the list where side effects that must happen after
4810 *EXPR_P should be stored. */
4812 static enum gimplify_status
4813 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4815 tree expr = *expr_p;
4816 tree op0 = TREE_OPERAND (expr, 0);
4817 enum gimplify_status ret;
4818 location_t loc = EXPR_LOCATION (*expr_p);
4820 switch (TREE_CODE (op0))
4822 case INDIRECT_REF:
4823 do_indirect_ref:
4824 /* Check if we are dealing with an expression of the form '&*ptr'.
4825 While the front end folds away '&*ptr' into 'ptr', these
4826 expressions may be generated internally by the compiler (e.g.,
4827 builtins like __builtin_va_end). */
4828 /* Caution: the silent array decomposition semantics we allow for
4829 ADDR_EXPR means we can't always discard the pair. */
4830 /* Gimplification of the ADDR_EXPR operand may drop
4831 cv-qualification conversions, so make sure we add them if
4832 needed. */
4834 tree op00 = TREE_OPERAND (op0, 0);
4835 tree t_expr = TREE_TYPE (expr);
4836 tree t_op00 = TREE_TYPE (op00);
4838 if (!useless_type_conversion_p (t_expr, t_op00))
4839 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4840 *expr_p = op00;
4841 ret = GS_OK;
4843 break;
4845 case VIEW_CONVERT_EXPR:
4846 /* Take the address of our operand and then convert it to the type of
4847 this ADDR_EXPR.
4849 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4850 all clear. The impact of this transformation is even less clear. */
4852 /* If the operand is a useless conversion, look through it. Doing so
4853 guarantees that the ADDR_EXPR and its operand will remain of the
4854 same type. */
4855 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4856 op0 = TREE_OPERAND (op0, 0);
4858 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4859 build_fold_addr_expr_loc (loc,
4860 TREE_OPERAND (op0, 0)));
4861 ret = GS_OK;
4862 break;
4864 default:
4865 /* We use fb_either here because the C frontend sometimes takes
4866 the address of a call that returns a struct; see
4867 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4868 the implied temporary explicit. */
4870 /* Make the operand addressable. */
4871 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4872 is_gimple_addressable, fb_either);
4873 if (ret == GS_ERROR)
4874 break;
4876 /* Then mark it. Beware that it may not be possible to do so directly
4877 if a temporary has been created by the gimplification. */
4878 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4880 op0 = TREE_OPERAND (expr, 0);
4882 /* For various reasons, the gimplification of the expression
4883 may have made a new INDIRECT_REF. */
4884 if (TREE_CODE (op0) == INDIRECT_REF)
4885 goto do_indirect_ref;
4887 mark_addressable (TREE_OPERAND (expr, 0));
4889 /* The FEs may end up building ADDR_EXPRs early on a decl with
4890 an incomplete type. Re-build ADDR_EXPRs in canonical form
4891 here. */
4892 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4893 *expr_p = build_fold_addr_expr (op0);
4895 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4896 recompute_tree_invariant_for_addr_expr (*expr_p);
4898 /* If we re-built the ADDR_EXPR add a conversion to the original type
4899 if required. */
4900 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4901 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4903 break;
4906 return ret;
4909 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4910 value; output operands should be a gimple lvalue. */
4912 static enum gimplify_status
4913 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4915 tree expr;
4916 int noutputs;
4917 const char **oconstraints;
4918 int i;
4919 tree link;
4920 const char *constraint;
4921 bool allows_mem, allows_reg, is_inout;
4922 enum gimplify_status ret, tret;
4923 gimple_asm stmt;
4924 vec<tree, va_gc> *inputs;
4925 vec<tree, va_gc> *outputs;
4926 vec<tree, va_gc> *clobbers;
4927 vec<tree, va_gc> *labels;
4928 tree link_next;
4930 expr = *expr_p;
4931 noutputs = list_length (ASM_OUTPUTS (expr));
4932 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4934 inputs = NULL;
4935 outputs = NULL;
4936 clobbers = NULL;
4937 labels = NULL;
4939 ret = GS_ALL_DONE;
4940 link_next = NULL_TREE;
4941 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4943 bool ok;
4944 size_t constraint_len;
4946 link_next = TREE_CHAIN (link);
4948 oconstraints[i]
4949 = constraint
4950 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4951 constraint_len = strlen (constraint);
4952 if (constraint_len == 0)
4953 continue;
4955 ok = parse_output_constraint (&constraint, i, 0, 0,
4956 &allows_mem, &allows_reg, &is_inout);
4957 if (!ok)
4959 ret = GS_ERROR;
4960 is_inout = false;
4963 if (!allows_reg && allows_mem)
4964 mark_addressable (TREE_VALUE (link));
4966 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4967 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4968 fb_lvalue | fb_mayfail);
4969 if (tret == GS_ERROR)
4971 error ("invalid lvalue in asm output %d", i);
4972 ret = tret;
4975 vec_safe_push (outputs, link);
4976 TREE_CHAIN (link) = NULL_TREE;
4978 if (is_inout)
4980 /* An input/output operand. To give the optimizers more
4981 flexibility, split it into separate input and output
4982 operands. */
4983 tree input;
4984 char buf[10];
4986 /* Turn the in/out constraint into an output constraint. */
4987 char *p = xstrdup (constraint);
4988 p[0] = '=';
4989 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4991 /* And add a matching input constraint. */
4992 if (allows_reg)
4994 sprintf (buf, "%d", i);
4996 /* If there are multiple alternatives in the constraint,
4997 handle each of them individually. Those that allow register
4998 will be replaced with operand number, the others will stay
4999 unchanged. */
5000 if (strchr (p, ',') != NULL)
5002 size_t len = 0, buflen = strlen (buf);
5003 char *beg, *end, *str, *dst;
5005 for (beg = p + 1;;)
5007 end = strchr (beg, ',');
5008 if (end == NULL)
5009 end = strchr (beg, '\0');
5010 if ((size_t) (end - beg) < buflen)
5011 len += buflen + 1;
5012 else
5013 len += end - beg + 1;
5014 if (*end)
5015 beg = end + 1;
5016 else
5017 break;
5020 str = (char *) alloca (len);
5021 for (beg = p + 1, dst = str;;)
5023 const char *tem;
5024 bool mem_p, reg_p, inout_p;
5026 end = strchr (beg, ',');
5027 if (end)
5028 *end = '\0';
5029 beg[-1] = '=';
5030 tem = beg - 1;
5031 parse_output_constraint (&tem, i, 0, 0,
5032 &mem_p, &reg_p, &inout_p);
5033 if (dst != str)
5034 *dst++ = ',';
5035 if (reg_p)
5037 memcpy (dst, buf, buflen);
5038 dst += buflen;
5040 else
5042 if (end)
5043 len = end - beg;
5044 else
5045 len = strlen (beg);
5046 memcpy (dst, beg, len);
5047 dst += len;
5049 if (end)
5050 beg = end + 1;
5051 else
5052 break;
5054 *dst = '\0';
5055 input = build_string (dst - str, str);
5057 else
5058 input = build_string (strlen (buf), buf);
5060 else
5061 input = build_string (constraint_len - 1, constraint + 1);
5063 free (p);
5065 input = build_tree_list (build_tree_list (NULL_TREE, input),
5066 unshare_expr (TREE_VALUE (link)));
5067 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5071 link_next = NULL_TREE;
5072 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5074 link_next = TREE_CHAIN (link);
5075 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5076 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5077 oconstraints, &allows_mem, &allows_reg);
5079 /* If we can't make copies, we can only accept memory. */
5080 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5082 if (allows_mem)
5083 allows_reg = 0;
5084 else
5086 error ("impossible constraint in %<asm%>");
5087 error ("non-memory input %d must stay in memory", i);
5088 return GS_ERROR;
5092 /* If the operand is a memory input, it should be an lvalue. */
5093 if (!allows_reg && allows_mem)
5095 tree inputv = TREE_VALUE (link);
5096 STRIP_NOPS (inputv);
5097 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5098 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5099 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5100 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5101 TREE_VALUE (link) = error_mark_node;
5102 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5103 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5104 mark_addressable (TREE_VALUE (link));
5105 if (tret == GS_ERROR)
5107 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5108 input_location = EXPR_LOCATION (TREE_VALUE (link));
5109 error ("memory input %d is not directly addressable", i);
5110 ret = tret;
5113 else
5115 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5116 is_gimple_asm_val, fb_rvalue);
5117 if (tret == GS_ERROR)
5118 ret = tret;
5121 TREE_CHAIN (link) = NULL_TREE;
5122 vec_safe_push (inputs, link);
5125 link_next = NULL_TREE;
5126 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5128 link_next = TREE_CHAIN (link);
5129 TREE_CHAIN (link) = NULL_TREE;
5130 vec_safe_push (clobbers, link);
5133 link_next = NULL_TREE;
5134 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5136 link_next = TREE_CHAIN (link);
5137 TREE_CHAIN (link) = NULL_TREE;
5138 vec_safe_push (labels, link);
5141 /* Do not add ASMs with errors to the gimple IL stream. */
5142 if (ret != GS_ERROR)
5144 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5145 inputs, outputs, clobbers, labels);
5147 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5148 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5150 gimplify_seq_add_stmt (pre_p, stmt);
5153 return ret;
5156 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5157 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5158 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5159 return to this function.
5161 FIXME should we complexify the prequeue handling instead? Or use flags
5162 for all the cleanups and let the optimizer tighten them up? The current
5163 code seems pretty fragile; it will break on a cleanup within any
5164 non-conditional nesting. But any such nesting would be broken, anyway;
5165 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5166 and continues out of it. We can do that at the RTL level, though, so
5167 having an optimizer to tighten up try/finally regions would be a Good
5168 Thing. */
5170 static enum gimplify_status
5171 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5173 gimple_stmt_iterator iter;
5174 gimple_seq body_sequence = NULL;
5176 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5178 /* We only care about the number of conditions between the innermost
5179 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5180 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5181 int old_conds = gimplify_ctxp->conditions;
5182 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5183 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5184 gimplify_ctxp->conditions = 0;
5185 gimplify_ctxp->conditional_cleanups = NULL;
5186 gimplify_ctxp->in_cleanup_point_expr = true;
5188 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5190 gimplify_ctxp->conditions = old_conds;
5191 gimplify_ctxp->conditional_cleanups = old_cleanups;
5192 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5194 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5196 gimple wce = gsi_stmt (iter);
5198 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5200 if (gsi_one_before_end_p (iter))
5202 /* Note that gsi_insert_seq_before and gsi_remove do not
5203 scan operands, unlike some other sequence mutators. */
5204 if (!gimple_wce_cleanup_eh_only (wce))
5205 gsi_insert_seq_before_without_update (&iter,
5206 gimple_wce_cleanup (wce),
5207 GSI_SAME_STMT);
5208 gsi_remove (&iter, true);
5209 break;
5211 else
5213 gimple_try gtry;
5214 gimple_seq seq;
5215 enum gimple_try_flags kind;
5217 if (gimple_wce_cleanup_eh_only (wce))
5218 kind = GIMPLE_TRY_CATCH;
5219 else
5220 kind = GIMPLE_TRY_FINALLY;
5221 seq = gsi_split_seq_after (iter);
5223 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5224 /* Do not use gsi_replace here, as it may scan operands.
5225 We want to do a simple structural modification only. */
5226 gsi_set_stmt (&iter, gtry);
5227 iter = gsi_start (gtry->eval);
5230 else
5231 gsi_next (&iter);
5234 gimplify_seq_add_seq (pre_p, body_sequence);
5235 if (temp)
5237 *expr_p = temp;
5238 return GS_OK;
5240 else
5242 *expr_p = NULL;
5243 return GS_ALL_DONE;
5247 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5248 is the cleanup action required. EH_ONLY is true if the cleanup should
5249 only be executed if an exception is thrown, not on normal exit. */
5251 static void
5252 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5254 gimple wce;
5255 gimple_seq cleanup_stmts = NULL;
5257 /* Errors can result in improperly nested cleanups. Which results in
5258 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5259 if (seen_error ())
5260 return;
5262 if (gimple_conditional_context ())
5264 /* If we're in a conditional context, this is more complex. We only
5265 want to run the cleanup if we actually ran the initialization that
5266 necessitates it, but we want to run it after the end of the
5267 conditional context. So we wrap the try/finally around the
5268 condition and use a flag to determine whether or not to actually
5269 run the destructor. Thus
5271 test ? f(A()) : 0
5273 becomes (approximately)
5275 flag = 0;
5276 try {
5277 if (test) { A::A(temp); flag = 1; val = f(temp); }
5278 else { val = 0; }
5279 } finally {
5280 if (flag) A::~A(temp);
5284 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5285 gimple_assign ffalse = gimple_build_assign (flag, boolean_false_node);
5286 gimple_assign ftrue = gimple_build_assign (flag, boolean_true_node);
5288 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5289 gimplify_stmt (&cleanup, &cleanup_stmts);
5290 wce = gimple_build_wce (cleanup_stmts);
5292 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5293 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5294 gimplify_seq_add_stmt (pre_p, ftrue);
5296 /* Because of this manipulation, and the EH edges that jump
5297 threading cannot redirect, the temporary (VAR) will appear
5298 to be used uninitialized. Don't warn. */
5299 TREE_NO_WARNING (var) = 1;
5301 else
5303 gimplify_stmt (&cleanup, &cleanup_stmts);
5304 wce = gimple_build_wce (cleanup_stmts);
5305 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5306 gimplify_seq_add_stmt (pre_p, wce);
5310 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5312 static enum gimplify_status
5313 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5315 tree targ = *expr_p;
5316 tree temp = TARGET_EXPR_SLOT (targ);
5317 tree init = TARGET_EXPR_INITIAL (targ);
5318 enum gimplify_status ret;
5320 if (init)
5322 tree cleanup = NULL_TREE;
5324 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5325 to the temps list. Handle also variable length TARGET_EXPRs. */
5326 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5328 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5329 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5330 gimplify_vla_decl (temp, pre_p);
5332 else
5333 gimple_add_tmp_var (temp);
5335 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5336 expression is supposed to initialize the slot. */
5337 if (VOID_TYPE_P (TREE_TYPE (init)))
5338 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5339 else
5341 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5342 init = init_expr;
5343 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5344 init = NULL;
5345 ggc_free (init_expr);
5347 if (ret == GS_ERROR)
5349 /* PR c++/28266 Make sure this is expanded only once. */
5350 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5351 return GS_ERROR;
5353 if (init)
5354 gimplify_and_add (init, pre_p);
5356 /* If needed, push the cleanup for the temp. */
5357 if (TARGET_EXPR_CLEANUP (targ))
5359 if (CLEANUP_EH_ONLY (targ))
5360 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5361 CLEANUP_EH_ONLY (targ), pre_p);
5362 else
5363 cleanup = TARGET_EXPR_CLEANUP (targ);
5366 /* Add a clobber for the temporary going out of scope, like
5367 gimplify_bind_expr. */
5368 if (gimplify_ctxp->in_cleanup_point_expr
5369 && needs_to_live_in_memory (temp)
5370 && flag_stack_reuse == SR_ALL)
5372 tree clobber = build_constructor (TREE_TYPE (temp),
5373 NULL);
5374 TREE_THIS_VOLATILE (clobber) = true;
5375 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5376 if (cleanup)
5377 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5378 clobber);
5379 else
5380 cleanup = clobber;
5383 if (cleanup)
5384 gimple_push_cleanup (temp, cleanup, false, pre_p);
5386 /* Only expand this once. */
5387 TREE_OPERAND (targ, 3) = init;
5388 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5390 else
5391 /* We should have expanded this before. */
5392 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5394 *expr_p = temp;
5395 return GS_OK;
5398 /* Gimplification of expression trees. */
5400 /* Gimplify an expression which appears at statement context. The
5401 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5402 NULL, a new sequence is allocated.
5404 Return true if we actually added a statement to the queue. */
5406 bool
5407 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5409 gimple_seq_node last;
5411 last = gimple_seq_last (*seq_p);
5412 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5413 return last != gimple_seq_last (*seq_p);
5416 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5417 to CTX. If entries already exist, force them to be some flavor of private.
5418 If there is no enclosing parallel, do nothing. */
5420 void
5421 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5423 splay_tree_node n;
5425 if (decl == NULL || !DECL_P (decl))
5426 return;
5430 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5431 if (n != NULL)
5433 if (n->value & GOVD_SHARED)
5434 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5435 else if (n->value & GOVD_MAP)
5436 n->value |= GOVD_MAP_TO_ONLY;
5437 else
5438 return;
5440 else if (ctx->region_type == ORT_TARGET)
5441 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5442 else if (ctx->region_type != ORT_WORKSHARE
5443 && ctx->region_type != ORT_SIMD
5444 && ctx->region_type != ORT_TARGET_DATA)
5445 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5447 ctx = ctx->outer_context;
5449 while (ctx);
5452 /* Similarly for each of the type sizes of TYPE. */
5454 static void
5455 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5457 if (type == NULL || type == error_mark_node)
5458 return;
5459 type = TYPE_MAIN_VARIANT (type);
5461 if (ctx->privatized_types->add (type))
5462 return;
5464 switch (TREE_CODE (type))
5466 case INTEGER_TYPE:
5467 case ENUMERAL_TYPE:
5468 case BOOLEAN_TYPE:
5469 case REAL_TYPE:
5470 case FIXED_POINT_TYPE:
5471 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5472 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5473 break;
5475 case ARRAY_TYPE:
5476 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5477 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5478 break;
5480 case RECORD_TYPE:
5481 case UNION_TYPE:
5482 case QUAL_UNION_TYPE:
5484 tree field;
5485 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5486 if (TREE_CODE (field) == FIELD_DECL)
5488 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5489 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5492 break;
5494 case POINTER_TYPE:
5495 case REFERENCE_TYPE:
5496 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5497 break;
5499 default:
5500 break;
5503 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5504 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5505 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5508 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5510 static void
5511 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5513 splay_tree_node n;
5514 unsigned int nflags;
5515 tree t;
5517 if (error_operand_p (decl))
5518 return;
5520 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5521 there are constructors involved somewhere. */
5522 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5523 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5524 flags |= GOVD_SEEN;
5526 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5527 if (n != NULL && n->value != GOVD_ALIGNED)
5529 /* We shouldn't be re-adding the decl with the same data
5530 sharing class. */
5531 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5532 /* The only combination of data sharing classes we should see is
5533 FIRSTPRIVATE and LASTPRIVATE. */
5534 nflags = n->value | flags;
5535 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5536 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5537 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5538 n->value = nflags;
5539 return;
5542 /* When adding a variable-sized variable, we have to handle all sorts
5543 of additional bits of data: the pointer replacement variable, and
5544 the parameters of the type. */
5545 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5547 /* Add the pointer replacement variable as PRIVATE if the variable
5548 replacement is private, else FIRSTPRIVATE since we'll need the
5549 address of the original variable either for SHARED, or for the
5550 copy into or out of the context. */
5551 if (!(flags & GOVD_LOCAL))
5553 nflags = flags & GOVD_MAP
5554 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5555 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5556 nflags |= flags & GOVD_SEEN;
5557 t = DECL_VALUE_EXPR (decl);
5558 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5559 t = TREE_OPERAND (t, 0);
5560 gcc_assert (DECL_P (t));
5561 omp_add_variable (ctx, t, nflags);
5564 /* Add all of the variable and type parameters (which should have
5565 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5566 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5567 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5568 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5570 /* The variable-sized variable itself is never SHARED, only some form
5571 of PRIVATE. The sharing would take place via the pointer variable
5572 which we remapped above. */
5573 if (flags & GOVD_SHARED)
5574 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5575 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5577 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5578 alloca statement we generate for the variable, so make sure it
5579 is available. This isn't automatically needed for the SHARED
5580 case, since we won't be allocating local storage then.
5581 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5582 in this case omp_notice_variable will be called later
5583 on when it is gimplified. */
5584 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5585 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5586 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5588 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5589 && lang_hooks.decls.omp_privatize_by_reference (decl))
5591 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5593 /* Similar to the direct variable sized case above, we'll need the
5594 size of references being privatized. */
5595 if ((flags & GOVD_SHARED) == 0)
5597 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5598 if (TREE_CODE (t) != INTEGER_CST)
5599 omp_notice_variable (ctx, t, true);
5603 if (n != NULL)
5604 n->value |= flags;
5605 else
5606 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5609 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5610 This just prints out diagnostics about threadprivate variable uses
5611 in untied tasks. If DECL2 is non-NULL, prevent this warning
5612 on that variable. */
5614 static bool
5615 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5616 tree decl2)
5618 splay_tree_node n;
5619 struct gimplify_omp_ctx *octx;
5621 for (octx = ctx; octx; octx = octx->outer_context)
5622 if (octx->region_type == ORT_TARGET)
5624 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5625 if (n == NULL)
5627 error ("threadprivate variable %qE used in target region",
5628 DECL_NAME (decl));
5629 error_at (octx->location, "enclosing target region");
5630 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5632 if (decl2)
5633 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5636 if (ctx->region_type != ORT_UNTIED_TASK)
5637 return false;
5638 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5639 if (n == NULL)
5641 error ("threadprivate variable %qE used in untied task",
5642 DECL_NAME (decl));
5643 error_at (ctx->location, "enclosing task");
5644 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5646 if (decl2)
5647 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5648 return false;
5651 /* Record the fact that DECL was used within the OpenMP context CTX.
5652 IN_CODE is true when real code uses DECL, and false when we should
5653 merely emit default(none) errors. Return true if DECL is going to
5654 be remapped and thus DECL shouldn't be gimplified into its
5655 DECL_VALUE_EXPR (if any). */
5657 static bool
5658 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5660 splay_tree_node n;
5661 unsigned flags = in_code ? GOVD_SEEN : 0;
5662 bool ret = false, shared;
5664 if (error_operand_p (decl))
5665 return false;
5667 /* Threadprivate variables are predetermined. */
5668 if (is_global_var (decl))
5670 if (DECL_THREAD_LOCAL_P (decl))
5671 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5673 if (DECL_HAS_VALUE_EXPR_P (decl))
5675 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5677 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5678 return omp_notice_threadprivate_variable (ctx, decl, value);
5682 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5683 if (ctx->region_type == ORT_TARGET)
5685 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5686 if (n == NULL)
5688 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5690 error ("%qD referenced in target region does not have "
5691 "a mappable type", decl);
5692 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5694 else
5695 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5697 else
5699 /* If nothing changed, there's nothing left to do. */
5700 if ((n->value & flags) == flags)
5701 return ret;
5702 n->value |= flags;
5704 goto do_outer;
5707 if (n == NULL)
5709 enum omp_clause_default_kind default_kind, kind;
5710 struct gimplify_omp_ctx *octx;
5712 if (ctx->region_type == ORT_WORKSHARE
5713 || ctx->region_type == ORT_SIMD
5714 || ctx->region_type == ORT_TARGET_DATA)
5715 goto do_outer;
5717 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5718 remapped firstprivate instead of shared. To some extent this is
5719 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5720 default_kind = ctx->default_kind;
5721 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5722 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5723 default_kind = kind;
5725 switch (default_kind)
5727 case OMP_CLAUSE_DEFAULT_NONE:
5728 if ((ctx->region_type & ORT_PARALLEL) != 0)
5730 error ("%qE not specified in enclosing parallel",
5731 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5732 error_at (ctx->location, "enclosing parallel");
5734 else if ((ctx->region_type & ORT_TASK) != 0)
5736 error ("%qE not specified in enclosing task",
5737 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5738 error_at (ctx->location, "enclosing task");
5740 else if (ctx->region_type == ORT_TEAMS)
5742 error ("%qE not specified in enclosing teams construct",
5743 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5744 error_at (ctx->location, "enclosing teams construct");
5746 else
5747 gcc_unreachable ();
5748 /* FALLTHRU */
5749 case OMP_CLAUSE_DEFAULT_SHARED:
5750 flags |= GOVD_SHARED;
5751 break;
5752 case OMP_CLAUSE_DEFAULT_PRIVATE:
5753 flags |= GOVD_PRIVATE;
5754 break;
5755 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5756 flags |= GOVD_FIRSTPRIVATE;
5757 break;
5758 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5759 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5760 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5761 if (ctx->outer_context)
5762 omp_notice_variable (ctx->outer_context, decl, in_code);
5763 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5765 splay_tree_node n2;
5767 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5768 continue;
5769 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5770 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5772 flags |= GOVD_FIRSTPRIVATE;
5773 break;
5775 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5776 break;
5778 if (flags & GOVD_FIRSTPRIVATE)
5779 break;
5780 if (octx == NULL
5781 && (TREE_CODE (decl) == PARM_DECL
5782 || (!is_global_var (decl)
5783 && DECL_CONTEXT (decl) == current_function_decl)))
5785 flags |= GOVD_FIRSTPRIVATE;
5786 break;
5788 flags |= GOVD_SHARED;
5789 break;
5790 default:
5791 gcc_unreachable ();
5794 if ((flags & GOVD_PRIVATE)
5795 && lang_hooks.decls.omp_private_outer_ref (decl))
5796 flags |= GOVD_PRIVATE_OUTER_REF;
5798 omp_add_variable (ctx, decl, flags);
5800 shared = (flags & GOVD_SHARED) != 0;
5801 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5802 goto do_outer;
5805 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5806 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5807 && DECL_SIZE (decl)
5808 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5810 splay_tree_node n2;
5811 tree t = DECL_VALUE_EXPR (decl);
5812 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5813 t = TREE_OPERAND (t, 0);
5814 gcc_assert (DECL_P (t));
5815 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5816 n2->value |= GOVD_SEEN;
5819 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5820 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5822 /* If nothing changed, there's nothing left to do. */
5823 if ((n->value & flags) == flags)
5824 return ret;
5825 flags |= n->value;
5826 n->value = flags;
5828 do_outer:
5829 /* If the variable is private in the current context, then we don't
5830 need to propagate anything to an outer context. */
5831 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5832 return ret;
5833 if (ctx->outer_context
5834 && omp_notice_variable (ctx->outer_context, decl, in_code))
5835 return true;
5836 return ret;
5839 /* Verify that DECL is private within CTX. If there's specific information
5840 to the contrary in the innermost scope, generate an error. */
5842 static bool
5843 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5845 splay_tree_node n;
5847 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5848 if (n != NULL)
5850 if (n->value & GOVD_SHARED)
5852 if (ctx == gimplify_omp_ctxp)
5854 if (simd)
5855 error ("iteration variable %qE is predetermined linear",
5856 DECL_NAME (decl));
5857 else
5858 error ("iteration variable %qE should be private",
5859 DECL_NAME (decl));
5860 n->value = GOVD_PRIVATE;
5861 return true;
5863 else
5864 return false;
5866 else if ((n->value & GOVD_EXPLICIT) != 0
5867 && (ctx == gimplify_omp_ctxp
5868 || (ctx->region_type == ORT_COMBINED_PARALLEL
5869 && gimplify_omp_ctxp->outer_context == ctx)))
5871 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5872 error ("iteration variable %qE should not be firstprivate",
5873 DECL_NAME (decl));
5874 else if ((n->value & GOVD_REDUCTION) != 0)
5875 error ("iteration variable %qE should not be reduction",
5876 DECL_NAME (decl));
5877 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5878 error ("iteration variable %qE should not be lastprivate",
5879 DECL_NAME (decl));
5880 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5881 error ("iteration variable %qE should not be private",
5882 DECL_NAME (decl));
5883 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5884 error ("iteration variable %qE is predetermined linear",
5885 DECL_NAME (decl));
5887 return (ctx == gimplify_omp_ctxp
5888 || (ctx->region_type == ORT_COMBINED_PARALLEL
5889 && gimplify_omp_ctxp->outer_context == ctx));
5892 if (ctx->region_type != ORT_WORKSHARE
5893 && ctx->region_type != ORT_SIMD)
5894 return false;
5895 else if (ctx->outer_context)
5896 return omp_is_private (ctx->outer_context, decl, simd);
5897 return false;
5900 /* Return true if DECL is private within a parallel region
5901 that binds to the current construct's context or in parallel
5902 region's REDUCTION clause. */
5904 static bool
5905 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5907 splay_tree_node n;
5911 ctx = ctx->outer_context;
5912 if (ctx == NULL)
5913 return !(is_global_var (decl)
5914 /* References might be private, but might be shared too,
5915 when checking for copyprivate, assume they might be
5916 private, otherwise assume they might be shared. */
5917 || (!copyprivate
5918 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5920 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5921 continue;
5923 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5924 if (n != NULL)
5925 return (n->value & GOVD_SHARED) == 0;
5927 while (ctx->region_type == ORT_WORKSHARE
5928 || ctx->region_type == ORT_SIMD);
5929 return false;
5932 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5933 and previous omp contexts. */
5935 static void
5936 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5937 enum omp_region_type region_type)
5939 struct gimplify_omp_ctx *ctx, *outer_ctx;
5940 tree c;
5942 ctx = new_omp_context (region_type);
5943 outer_ctx = ctx->outer_context;
5945 while ((c = *list_p) != NULL)
5947 bool remove = false;
5948 bool notice_outer = true;
5949 const char *check_non_private = NULL;
5950 unsigned int flags;
5951 tree decl;
5953 switch (OMP_CLAUSE_CODE (c))
5955 case OMP_CLAUSE_PRIVATE:
5956 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5957 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5959 flags |= GOVD_PRIVATE_OUTER_REF;
5960 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5962 else
5963 notice_outer = false;
5964 goto do_add;
5965 case OMP_CLAUSE_SHARED:
5966 flags = GOVD_SHARED | GOVD_EXPLICIT;
5967 goto do_add;
5968 case OMP_CLAUSE_FIRSTPRIVATE:
5969 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5970 check_non_private = "firstprivate";
5971 goto do_add;
5972 case OMP_CLAUSE_LASTPRIVATE:
5973 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5974 check_non_private = "lastprivate";
5975 goto do_add;
5976 case OMP_CLAUSE_REDUCTION:
5977 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5978 check_non_private = "reduction";
5979 goto do_add;
5980 case OMP_CLAUSE_LINEAR:
5981 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5982 is_gimple_val, fb_rvalue) == GS_ERROR)
5984 remove = true;
5985 break;
5987 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5988 goto do_add;
5990 case OMP_CLAUSE_MAP:
5991 decl = OMP_CLAUSE_DECL (c);
5992 if (error_operand_p (decl))
5994 remove = true;
5995 break;
5997 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
5998 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
5999 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6000 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6001 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6003 remove = true;
6004 break;
6006 if (!DECL_P (decl))
6008 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6009 NULL, is_gimple_lvalue, fb_lvalue)
6010 == GS_ERROR)
6012 remove = true;
6013 break;
6015 break;
6017 flags = GOVD_MAP | GOVD_EXPLICIT;
6018 goto do_add;
6020 case OMP_CLAUSE_DEPEND:
6021 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6023 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6024 NULL, is_gimple_val, fb_rvalue);
6025 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6027 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6029 remove = true;
6030 break;
6032 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6033 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6034 is_gimple_val, fb_rvalue) == GS_ERROR)
6036 remove = true;
6037 break;
6039 break;
6041 case OMP_CLAUSE_TO:
6042 case OMP_CLAUSE_FROM:
6043 decl = OMP_CLAUSE_DECL (c);
6044 if (error_operand_p (decl))
6046 remove = true;
6047 break;
6049 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6050 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6051 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6052 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6053 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6055 remove = true;
6056 break;
6058 if (!DECL_P (decl))
6060 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6061 NULL, is_gimple_lvalue, fb_lvalue)
6062 == GS_ERROR)
6064 remove = true;
6065 break;
6067 break;
6069 goto do_notice;
6071 do_add:
6072 decl = OMP_CLAUSE_DECL (c);
6073 if (error_operand_p (decl))
6075 remove = true;
6076 break;
6078 omp_add_variable (ctx, decl, flags);
6079 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6080 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6082 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6083 GOVD_LOCAL | GOVD_SEEN);
6084 gimplify_omp_ctxp = ctx;
6085 push_gimplify_context ();
6087 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6088 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6090 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6091 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6092 pop_gimplify_context
6093 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6094 push_gimplify_context ();
6095 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6096 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6097 pop_gimplify_context
6098 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6099 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6100 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6102 gimplify_omp_ctxp = outer_ctx;
6104 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6105 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6107 gimplify_omp_ctxp = ctx;
6108 push_gimplify_context ();
6109 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6111 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6112 NULL, NULL);
6113 TREE_SIDE_EFFECTS (bind) = 1;
6114 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6115 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6117 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6118 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6119 pop_gimplify_context
6120 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6121 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6123 gimplify_omp_ctxp = outer_ctx;
6125 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6126 && OMP_CLAUSE_LINEAR_STMT (c))
6128 gimplify_omp_ctxp = ctx;
6129 push_gimplify_context ();
6130 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6132 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6133 NULL, NULL);
6134 TREE_SIDE_EFFECTS (bind) = 1;
6135 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6136 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6138 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6139 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6140 pop_gimplify_context
6141 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6142 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6144 gimplify_omp_ctxp = outer_ctx;
6146 if (notice_outer)
6147 goto do_notice;
6148 break;
6150 case OMP_CLAUSE_COPYIN:
6151 case OMP_CLAUSE_COPYPRIVATE:
6152 decl = OMP_CLAUSE_DECL (c);
6153 if (error_operand_p (decl))
6155 remove = true;
6156 break;
6158 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6159 && !remove
6160 && !omp_check_private (ctx, decl, true))
6162 remove = true;
6163 if (is_global_var (decl))
6165 if (DECL_THREAD_LOCAL_P (decl))
6166 remove = false;
6167 else if (DECL_HAS_VALUE_EXPR_P (decl))
6169 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6171 if (value
6172 && DECL_P (value)
6173 && DECL_THREAD_LOCAL_P (value))
6174 remove = false;
6177 if (remove)
6178 error_at (OMP_CLAUSE_LOCATION (c),
6179 "copyprivate variable %qE is not threadprivate"
6180 " or private in outer context", DECL_NAME (decl));
6182 do_notice:
6183 if (outer_ctx)
6184 omp_notice_variable (outer_ctx, decl, true);
6185 if (check_non_private
6186 && region_type == ORT_WORKSHARE
6187 && omp_check_private (ctx, decl, false))
6189 error ("%s variable %qE is private in outer context",
6190 check_non_private, DECL_NAME (decl));
6191 remove = true;
6193 break;
6195 case OMP_CLAUSE_FINAL:
6196 case OMP_CLAUSE_IF:
6197 OMP_CLAUSE_OPERAND (c, 0)
6198 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6199 /* Fall through. */
6201 case OMP_CLAUSE_SCHEDULE:
6202 case OMP_CLAUSE_NUM_THREADS:
6203 case OMP_CLAUSE_NUM_TEAMS:
6204 case OMP_CLAUSE_THREAD_LIMIT:
6205 case OMP_CLAUSE_DIST_SCHEDULE:
6206 case OMP_CLAUSE_DEVICE:
6207 case OMP_CLAUSE__CILK_FOR_COUNT_:
6208 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6209 is_gimple_val, fb_rvalue) == GS_ERROR)
6210 remove = true;
6211 break;
6213 case OMP_CLAUSE_NOWAIT:
6214 case OMP_CLAUSE_ORDERED:
6215 case OMP_CLAUSE_UNTIED:
6216 case OMP_CLAUSE_COLLAPSE:
6217 case OMP_CLAUSE_MERGEABLE:
6218 case OMP_CLAUSE_PROC_BIND:
6219 case OMP_CLAUSE_SAFELEN:
6220 break;
6222 case OMP_CLAUSE_ALIGNED:
6223 decl = OMP_CLAUSE_DECL (c);
6224 if (error_operand_p (decl))
6226 remove = true;
6227 break;
6229 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6230 is_gimple_val, fb_rvalue) == GS_ERROR)
6232 remove = true;
6233 break;
6235 if (!is_global_var (decl)
6236 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6237 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6238 break;
6240 case OMP_CLAUSE_DEFAULT:
6241 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6242 break;
6244 default:
6245 gcc_unreachable ();
6248 if (remove)
6249 *list_p = OMP_CLAUSE_CHAIN (c);
6250 else
6251 list_p = &OMP_CLAUSE_CHAIN (c);
6254 gimplify_omp_ctxp = ctx;
6257 struct gimplify_adjust_omp_clauses_data
6259 tree *list_p;
6260 gimple_seq *pre_p;
6263 /* For all variables that were not actually used within the context,
6264 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6266 static int
6267 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6269 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6270 gimple_seq *pre_p
6271 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6272 tree decl = (tree) n->key;
6273 unsigned flags = n->value;
6274 enum omp_clause_code code;
6275 tree clause;
6276 bool private_debug;
6278 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6279 return 0;
6280 if ((flags & GOVD_SEEN) == 0)
6281 return 0;
6282 if (flags & GOVD_DEBUG_PRIVATE)
6284 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6285 private_debug = true;
6287 else if (flags & GOVD_MAP)
6288 private_debug = false;
6289 else
6290 private_debug
6291 = lang_hooks.decls.omp_private_debug_clause (decl,
6292 !!(flags & GOVD_SHARED));
6293 if (private_debug)
6294 code = OMP_CLAUSE_PRIVATE;
6295 else if (flags & GOVD_MAP)
6296 code = OMP_CLAUSE_MAP;
6297 else if (flags & GOVD_SHARED)
6299 if (is_global_var (decl))
6301 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6302 while (ctx != NULL)
6304 splay_tree_node on
6305 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6306 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6307 | GOVD_PRIVATE | GOVD_REDUCTION
6308 | GOVD_LINEAR | GOVD_MAP)) != 0)
6309 break;
6310 ctx = ctx->outer_context;
6312 if (ctx == NULL)
6313 return 0;
6315 code = OMP_CLAUSE_SHARED;
6317 else if (flags & GOVD_PRIVATE)
6318 code = OMP_CLAUSE_PRIVATE;
6319 else if (flags & GOVD_FIRSTPRIVATE)
6320 code = OMP_CLAUSE_FIRSTPRIVATE;
6321 else if (flags & GOVD_LASTPRIVATE)
6322 code = OMP_CLAUSE_LASTPRIVATE;
6323 else if (flags & GOVD_ALIGNED)
6324 return 0;
6325 else
6326 gcc_unreachable ();
6328 clause = build_omp_clause (input_location, code);
6329 OMP_CLAUSE_DECL (clause) = decl;
6330 OMP_CLAUSE_CHAIN (clause) = *list_p;
6331 if (private_debug)
6332 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6333 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6334 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6335 else if (code == OMP_CLAUSE_MAP)
6337 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6338 ? OMP_CLAUSE_MAP_TO
6339 : OMP_CLAUSE_MAP_TOFROM;
6340 if (DECL_SIZE (decl)
6341 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6343 tree decl2 = DECL_VALUE_EXPR (decl);
6344 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6345 decl2 = TREE_OPERAND (decl2, 0);
6346 gcc_assert (DECL_P (decl2));
6347 tree mem = build_simple_mem_ref (decl2);
6348 OMP_CLAUSE_DECL (clause) = mem;
6349 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6350 if (gimplify_omp_ctxp->outer_context)
6352 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6353 omp_notice_variable (ctx, decl2, true);
6354 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6356 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6357 OMP_CLAUSE_MAP);
6358 OMP_CLAUSE_DECL (nc) = decl;
6359 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6360 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6361 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6362 OMP_CLAUSE_CHAIN (clause) = nc;
6364 else
6365 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6367 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6369 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6370 OMP_CLAUSE_DECL (nc) = decl;
6371 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6372 OMP_CLAUSE_CHAIN (nc) = *list_p;
6373 OMP_CLAUSE_CHAIN (clause) = nc;
6374 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6375 gimplify_omp_ctxp = ctx->outer_context;
6376 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6377 gimplify_omp_ctxp = ctx;
6379 *list_p = clause;
6380 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6381 gimplify_omp_ctxp = ctx->outer_context;
6382 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6383 gimplify_omp_ctxp = ctx;
6384 return 0;
6387 static void
6388 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6390 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6391 tree c, decl;
6393 while ((c = *list_p) != NULL)
6395 splay_tree_node n;
6396 bool remove = false;
6398 switch (OMP_CLAUSE_CODE (c))
6400 case OMP_CLAUSE_PRIVATE:
6401 case OMP_CLAUSE_SHARED:
6402 case OMP_CLAUSE_FIRSTPRIVATE:
6403 case OMP_CLAUSE_LINEAR:
6404 decl = OMP_CLAUSE_DECL (c);
6405 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6406 remove = !(n->value & GOVD_SEEN);
6407 if (! remove)
6409 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6410 if ((n->value & GOVD_DEBUG_PRIVATE)
6411 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6413 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6414 || ((n->value & GOVD_DATA_SHARE_CLASS)
6415 == GOVD_PRIVATE));
6416 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6417 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6419 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6420 && ctx->outer_context
6421 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6422 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6424 if (ctx->outer_context->combined_loop
6425 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6427 n = splay_tree_lookup (ctx->outer_context->variables,
6428 (splay_tree_key) decl);
6429 if (n == NULL
6430 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6432 int flags = GOVD_FIRSTPRIVATE;
6433 /* #pragma omp distribute does not allow
6434 lastprivate clause. */
6435 if (!ctx->outer_context->distribute)
6436 flags |= GOVD_LASTPRIVATE;
6437 if (n == NULL)
6438 omp_add_variable (ctx->outer_context, decl,
6439 flags | GOVD_SEEN);
6440 else
6441 n->value |= flags | GOVD_SEEN;
6444 else if (!is_global_var (decl))
6445 omp_notice_variable (ctx->outer_context, decl, true);
6448 break;
6450 case OMP_CLAUSE_LASTPRIVATE:
6451 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6452 accurately reflect the presence of a FIRSTPRIVATE clause. */
6453 decl = OMP_CLAUSE_DECL (c);
6454 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6455 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6456 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6457 break;
6459 case OMP_CLAUSE_ALIGNED:
6460 decl = OMP_CLAUSE_DECL (c);
6461 if (!is_global_var (decl))
6463 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6464 remove = n == NULL || !(n->value & GOVD_SEEN);
6465 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6467 struct gimplify_omp_ctx *octx;
6468 if (n != NULL
6469 && (n->value & (GOVD_DATA_SHARE_CLASS
6470 & ~GOVD_FIRSTPRIVATE)))
6471 remove = true;
6472 else
6473 for (octx = ctx->outer_context; octx;
6474 octx = octx->outer_context)
6476 n = splay_tree_lookup (octx->variables,
6477 (splay_tree_key) decl);
6478 if (n == NULL)
6479 continue;
6480 if (n->value & GOVD_LOCAL)
6481 break;
6482 /* We have to avoid assigning a shared variable
6483 to itself when trying to add
6484 __builtin_assume_aligned. */
6485 if (n->value & GOVD_SHARED)
6487 remove = true;
6488 break;
6493 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6495 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6496 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6497 remove = true;
6499 break;
6501 case OMP_CLAUSE_MAP:
6502 decl = OMP_CLAUSE_DECL (c);
6503 if (!DECL_P (decl))
6504 break;
6505 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6506 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6507 remove = true;
6508 else if (DECL_SIZE (decl)
6509 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6510 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6512 tree decl2 = DECL_VALUE_EXPR (decl);
6513 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6514 decl2 = TREE_OPERAND (decl2, 0);
6515 gcc_assert (DECL_P (decl2));
6516 tree mem = build_simple_mem_ref (decl2);
6517 OMP_CLAUSE_DECL (c) = mem;
6518 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6519 if (ctx->outer_context)
6521 omp_notice_variable (ctx->outer_context, decl2, true);
6522 omp_notice_variable (ctx->outer_context,
6523 OMP_CLAUSE_SIZE (c), true);
6525 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6526 OMP_CLAUSE_MAP);
6527 OMP_CLAUSE_DECL (nc) = decl;
6528 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6529 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6530 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6531 OMP_CLAUSE_CHAIN (c) = nc;
6532 c = nc;
6534 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6535 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6536 break;
6538 case OMP_CLAUSE_TO:
6539 case OMP_CLAUSE_FROM:
6540 decl = OMP_CLAUSE_DECL (c);
6541 if (!DECL_P (decl))
6542 break;
6543 if (DECL_SIZE (decl)
6544 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6546 tree decl2 = DECL_VALUE_EXPR (decl);
6547 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6548 decl2 = TREE_OPERAND (decl2, 0);
6549 gcc_assert (DECL_P (decl2));
6550 tree mem = build_simple_mem_ref (decl2);
6551 OMP_CLAUSE_DECL (c) = mem;
6552 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6553 if (ctx->outer_context)
6555 omp_notice_variable (ctx->outer_context, decl2, true);
6556 omp_notice_variable (ctx->outer_context,
6557 OMP_CLAUSE_SIZE (c), true);
6560 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6561 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6562 break;
6564 case OMP_CLAUSE_REDUCTION:
6565 case OMP_CLAUSE_COPYIN:
6566 case OMP_CLAUSE_COPYPRIVATE:
6567 case OMP_CLAUSE_IF:
6568 case OMP_CLAUSE_NUM_THREADS:
6569 case OMP_CLAUSE_NUM_TEAMS:
6570 case OMP_CLAUSE_THREAD_LIMIT:
6571 case OMP_CLAUSE_DIST_SCHEDULE:
6572 case OMP_CLAUSE_DEVICE:
6573 case OMP_CLAUSE_SCHEDULE:
6574 case OMP_CLAUSE_NOWAIT:
6575 case OMP_CLAUSE_ORDERED:
6576 case OMP_CLAUSE_DEFAULT:
6577 case OMP_CLAUSE_UNTIED:
6578 case OMP_CLAUSE_COLLAPSE:
6579 case OMP_CLAUSE_FINAL:
6580 case OMP_CLAUSE_MERGEABLE:
6581 case OMP_CLAUSE_PROC_BIND:
6582 case OMP_CLAUSE_SAFELEN:
6583 case OMP_CLAUSE_DEPEND:
6584 case OMP_CLAUSE__CILK_FOR_COUNT_:
6585 break;
6587 default:
6588 gcc_unreachable ();
6591 if (remove)
6592 *list_p = OMP_CLAUSE_CHAIN (c);
6593 else
6594 list_p = &OMP_CLAUSE_CHAIN (c);
6597 /* Add in any implicit data sharing. */
6598 struct gimplify_adjust_omp_clauses_data data;
6599 data.list_p = list_p;
6600 data.pre_p = pre_p;
6601 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6603 gimplify_omp_ctxp = ctx->outer_context;
6604 delete_omp_context (ctx);
6607 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6608 gimplification of the body, as well as scanning the body for used
6609 variables. We need to do this scan now, because variable-sized
6610 decls will be decomposed during gimplification. */
6612 static void
6613 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6615 tree expr = *expr_p;
6616 gimple g;
6617 gimple_seq body = NULL;
6619 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6620 OMP_PARALLEL_COMBINED (expr)
6621 ? ORT_COMBINED_PARALLEL
6622 : ORT_PARALLEL);
6624 push_gimplify_context ();
6626 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6627 if (gimple_code (g) == GIMPLE_BIND)
6628 pop_gimplify_context (g);
6629 else
6630 pop_gimplify_context (NULL);
6632 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6634 g = gimple_build_omp_parallel (body,
6635 OMP_PARALLEL_CLAUSES (expr),
6636 NULL_TREE, NULL_TREE);
6637 if (OMP_PARALLEL_COMBINED (expr))
6638 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6639 gimplify_seq_add_stmt (pre_p, g);
6640 *expr_p = NULL_TREE;
6643 /* Gimplify the contents of an OMP_TASK statement. This involves
6644 gimplification of the body, as well as scanning the body for used
6645 variables. We need to do this scan now, because variable-sized
6646 decls will be decomposed during gimplification. */
6648 static void
6649 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6651 tree expr = *expr_p;
6652 gimple g;
6653 gimple_seq body = NULL;
6655 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6656 find_omp_clause (OMP_TASK_CLAUSES (expr),
6657 OMP_CLAUSE_UNTIED)
6658 ? ORT_UNTIED_TASK : ORT_TASK);
6660 push_gimplify_context ();
6662 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6663 if (gimple_code (g) == GIMPLE_BIND)
6664 pop_gimplify_context (g);
6665 else
6666 pop_gimplify_context (NULL);
6668 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6670 g = gimple_build_omp_task (body,
6671 OMP_TASK_CLAUSES (expr),
6672 NULL_TREE, NULL_TREE,
6673 NULL_TREE, NULL_TREE, NULL_TREE);
6674 gimplify_seq_add_stmt (pre_p, g);
6675 *expr_p = NULL_TREE;
6678 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6679 with non-NULL OMP_FOR_INIT. */
6681 static tree
6682 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6684 *walk_subtrees = 0;
6685 switch (TREE_CODE (*tp))
6687 case OMP_FOR:
6688 *walk_subtrees = 1;
6689 /* FALLTHRU */
6690 case OMP_SIMD:
6691 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6692 return *tp;
6693 break;
6694 case BIND_EXPR:
6695 case STATEMENT_LIST:
6696 case OMP_PARALLEL:
6697 *walk_subtrees = 1;
6698 break;
6699 default:
6700 break;
6702 return NULL_TREE;
6705 /* Gimplify the gross structure of an OMP_FOR statement. */
6707 static enum gimplify_status
6708 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6710 tree for_stmt, orig_for_stmt, decl, var, t;
6711 enum gimplify_status ret = GS_ALL_DONE;
6712 enum gimplify_status tret;
6713 gimple_omp_for gfor;
6714 gimple_seq for_body, for_pre_body;
6715 int i;
6716 bool simd;
6717 bitmap has_decl_expr = NULL;
6719 orig_for_stmt = for_stmt = *expr_p;
6721 simd = (TREE_CODE (for_stmt) == OMP_SIMD
6722 || TREE_CODE (for_stmt) == CILK_SIMD);
6723 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6724 simd ? ORT_SIMD : ORT_WORKSHARE);
6725 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6726 gimplify_omp_ctxp->distribute = true;
6728 /* Handle OMP_FOR_INIT. */
6729 for_pre_body = NULL;
6730 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6732 has_decl_expr = BITMAP_ALLOC (NULL);
6733 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6734 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6735 == VAR_DECL)
6737 t = OMP_FOR_PRE_BODY (for_stmt);
6738 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6740 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6742 tree_stmt_iterator si;
6743 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6744 tsi_next (&si))
6746 t = tsi_stmt (si);
6747 if (TREE_CODE (t) == DECL_EXPR
6748 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6749 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6753 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6754 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6756 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6758 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6759 NULL, NULL);
6760 gcc_assert (for_stmt != NULL_TREE);
6761 gimplify_omp_ctxp->combined_loop = true;
6764 for_body = NULL;
6765 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6766 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6767 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6768 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6769 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6771 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6772 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6773 decl = TREE_OPERAND (t, 0);
6774 gcc_assert (DECL_P (decl));
6775 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6776 || POINTER_TYPE_P (TREE_TYPE (decl)));
6778 /* Make sure the iteration variable is private. */
6779 tree c = NULL_TREE;
6780 tree c2 = NULL_TREE;
6781 if (orig_for_stmt != for_stmt)
6782 /* Do this only on innermost construct for combined ones. */;
6783 else if (simd)
6785 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6786 (splay_tree_key)decl);
6787 omp_is_private (gimplify_omp_ctxp, decl,
6788 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6789 != 1));
6790 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6791 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6792 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6794 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6795 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6796 if (has_decl_expr
6797 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6798 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6799 OMP_CLAUSE_DECL (c) = decl;
6800 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6801 OMP_FOR_CLAUSES (for_stmt) = c;
6802 omp_add_variable (gimplify_omp_ctxp, decl,
6803 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6805 else
6807 bool lastprivate
6808 = (!has_decl_expr
6809 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6810 if (lastprivate
6811 && gimplify_omp_ctxp->outer_context
6812 && gimplify_omp_ctxp->outer_context->region_type
6813 == ORT_WORKSHARE
6814 && gimplify_omp_ctxp->outer_context->combined_loop
6815 && !gimplify_omp_ctxp->outer_context->distribute)
6817 struct gimplify_omp_ctx *outer
6818 = gimplify_omp_ctxp->outer_context;
6819 n = splay_tree_lookup (outer->variables,
6820 (splay_tree_key) decl);
6821 if (n != NULL
6822 && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6823 lastprivate = false;
6824 else if (omp_check_private (outer, decl, false))
6825 error ("lastprivate variable %qE is private in outer "
6826 "context", DECL_NAME (decl));
6827 else
6829 omp_add_variable (outer, decl,
6830 GOVD_LASTPRIVATE | GOVD_SEEN);
6831 if (outer->outer_context)
6832 omp_notice_variable (outer->outer_context, decl, true);
6835 c = build_omp_clause (input_location,
6836 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6837 : OMP_CLAUSE_PRIVATE);
6838 OMP_CLAUSE_DECL (c) = decl;
6839 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6840 OMP_FOR_CLAUSES (for_stmt) = c;
6841 omp_add_variable (gimplify_omp_ctxp, decl,
6842 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6843 | GOVD_EXPLICIT | GOVD_SEEN);
6844 c = NULL_TREE;
6847 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6848 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6849 else
6850 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6852 /* If DECL is not a gimple register, create a temporary variable to act
6853 as an iteration counter. This is valid, since DECL cannot be
6854 modified in the body of the loop. Similarly for any iteration vars
6855 in simd with collapse > 1 where the iterator vars must be
6856 lastprivate. */
6857 if (orig_for_stmt != for_stmt)
6858 var = decl;
6859 else if (!is_gimple_reg (decl)
6860 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
6862 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6863 TREE_OPERAND (t, 0) = var;
6865 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6867 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6869 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6870 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
6871 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
6872 OMP_CLAUSE_DECL (c2) = var;
6873 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
6874 OMP_FOR_CLAUSES (for_stmt) = c2;
6875 omp_add_variable (gimplify_omp_ctxp, var,
6876 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6877 if (c == NULL_TREE)
6879 c = c2;
6880 c2 = NULL_TREE;
6883 else
6884 omp_add_variable (gimplify_omp_ctxp, var,
6885 GOVD_PRIVATE | GOVD_SEEN);
6887 else
6888 var = decl;
6890 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6891 is_gimple_val, fb_rvalue);
6892 ret = MIN (ret, tret);
6893 if (ret == GS_ERROR)
6894 return ret;
6896 /* Handle OMP_FOR_COND. */
6897 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6898 gcc_assert (COMPARISON_CLASS_P (t));
6899 gcc_assert (TREE_OPERAND (t, 0) == decl);
6901 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6902 is_gimple_val, fb_rvalue);
6903 ret = MIN (ret, tret);
6905 /* Handle OMP_FOR_INCR. */
6906 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6907 switch (TREE_CODE (t))
6909 case PREINCREMENT_EXPR:
6910 case POSTINCREMENT_EXPR:
6912 tree decl = TREE_OPERAND (t, 0);
6913 /* c_omp_for_incr_canonicalize_ptr() should have been
6914 called to massage things appropriately. */
6915 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6917 if (orig_for_stmt != for_stmt)
6918 break;
6919 t = build_int_cst (TREE_TYPE (decl), 1);
6920 if (c)
6921 OMP_CLAUSE_LINEAR_STEP (c) = t;
6922 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6923 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6924 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6925 break;
6928 case PREDECREMENT_EXPR:
6929 case POSTDECREMENT_EXPR:
6930 /* c_omp_for_incr_canonicalize_ptr() should have been
6931 called to massage things appropriately. */
6932 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6933 if (orig_for_stmt != for_stmt)
6934 break;
6935 t = build_int_cst (TREE_TYPE (decl), -1);
6936 if (c)
6937 OMP_CLAUSE_LINEAR_STEP (c) = t;
6938 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6939 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6940 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6941 break;
6943 case MODIFY_EXPR:
6944 gcc_assert (TREE_OPERAND (t, 0) == decl);
6945 TREE_OPERAND (t, 0) = var;
6947 t = TREE_OPERAND (t, 1);
6948 switch (TREE_CODE (t))
6950 case PLUS_EXPR:
6951 if (TREE_OPERAND (t, 1) == decl)
6953 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6954 TREE_OPERAND (t, 0) = var;
6955 break;
6958 /* Fallthru. */
6959 case MINUS_EXPR:
6960 case POINTER_PLUS_EXPR:
6961 gcc_assert (TREE_OPERAND (t, 0) == decl);
6962 TREE_OPERAND (t, 0) = var;
6963 break;
6964 default:
6965 gcc_unreachable ();
6968 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6969 is_gimple_val, fb_rvalue);
6970 ret = MIN (ret, tret);
6971 if (c)
6973 tree step = TREE_OPERAND (t, 1);
6974 tree stept = TREE_TYPE (decl);
6975 if (POINTER_TYPE_P (stept))
6976 stept = sizetype;
6977 step = fold_convert (stept, step);
6978 if (TREE_CODE (t) == MINUS_EXPR)
6979 step = fold_build1 (NEGATE_EXPR, stept, step);
6980 OMP_CLAUSE_LINEAR_STEP (c) = step;
6981 if (step != TREE_OPERAND (t, 1))
6983 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6984 &for_pre_body, NULL,
6985 is_gimple_val, fb_rvalue);
6986 ret = MIN (ret, tret);
6989 break;
6991 default:
6992 gcc_unreachable ();
6995 if (c2)
6997 gcc_assert (c);
6998 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7001 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7002 && orig_for_stmt == for_stmt)
7004 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7005 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7006 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7007 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7008 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7009 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7010 && OMP_CLAUSE_DECL (c) == decl)
7012 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7013 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7014 gcc_assert (TREE_OPERAND (t, 0) == var);
7015 t = TREE_OPERAND (t, 1);
7016 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7017 || TREE_CODE (t) == MINUS_EXPR
7018 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7019 gcc_assert (TREE_OPERAND (t, 0) == var);
7020 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7021 TREE_OPERAND (t, 1));
7022 gimple_seq *seq;
7023 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7024 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7025 else
7026 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7027 gimplify_assign (decl, t, seq);
7032 BITMAP_FREE (has_decl_expr);
7034 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7036 if (orig_for_stmt != for_stmt)
7037 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7039 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7040 decl = TREE_OPERAND (t, 0);
7041 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7042 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7043 TREE_OPERAND (t, 0) = var;
7044 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7045 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7046 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7049 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7051 int kind;
7052 switch (TREE_CODE (orig_for_stmt))
7054 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7055 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7056 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7057 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7058 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7059 default:
7060 gcc_unreachable ();
7062 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7063 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7064 for_pre_body);
7065 if (orig_for_stmt != for_stmt)
7066 gimple_omp_for_set_combined_p (gfor, true);
7067 if (gimplify_omp_ctxp
7068 && (gimplify_omp_ctxp->combined_loop
7069 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7070 && gimplify_omp_ctxp->outer_context
7071 && gimplify_omp_ctxp->outer_context->combined_loop)))
7073 gimple_omp_for_set_combined_into_p (gfor, true);
7074 if (gimplify_omp_ctxp->combined_loop)
7075 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7076 else
7077 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7080 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7082 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7083 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7084 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7085 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7086 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7087 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7088 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7089 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7092 gimplify_seq_add_stmt (pre_p, gfor);
7093 if (ret != GS_ALL_DONE)
7094 return GS_ERROR;
7095 *expr_p = NULL_TREE;
7096 return GS_ALL_DONE;
7099 /* Gimplify the gross structure of other OpenMP constructs.
7100 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
7101 and OMP_TEAMS. */
7103 static void
7104 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7106 tree expr = *expr_p;
7107 gimple stmt;
7108 gimple_seq body = NULL;
7109 enum omp_region_type ort = ORT_WORKSHARE;
7111 switch (TREE_CODE (expr))
7113 case OMP_SECTIONS:
7114 case OMP_SINGLE:
7115 break;
7116 case OMP_TARGET:
7117 ort = ORT_TARGET;
7118 break;
7119 case OMP_TARGET_DATA:
7120 ort = ORT_TARGET_DATA;
7121 break;
7122 case OMP_TEAMS:
7123 ort = ORT_TEAMS;
7124 break;
7125 default:
7126 gcc_unreachable ();
7128 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7129 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7131 push_gimplify_context ();
7132 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7133 if (gimple_code (g) == GIMPLE_BIND)
7134 pop_gimplify_context (g);
7135 else
7136 pop_gimplify_context (NULL);
7137 if (ort == ORT_TARGET_DATA)
7139 gimple_seq cleanup = NULL;
7140 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
7141 g = gimple_build_call (fn, 0);
7142 gimple_seq_add_stmt (&cleanup, g);
7143 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7144 body = NULL;
7145 gimple_seq_add_stmt (&body, g);
7148 else
7149 gimplify_and_add (OMP_BODY (expr), &body);
7150 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7152 switch (TREE_CODE (expr))
7154 case OMP_SECTIONS:
7155 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7156 break;
7157 case OMP_SINGLE:
7158 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7159 break;
7160 case OMP_TARGET:
7161 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7162 OMP_CLAUSES (expr));
7163 break;
7164 case OMP_TARGET_DATA:
7165 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7166 OMP_CLAUSES (expr));
7167 break;
7168 case OMP_TEAMS:
7169 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7170 break;
7171 default:
7172 gcc_unreachable ();
7175 gimplify_seq_add_stmt (pre_p, stmt);
7176 *expr_p = NULL_TREE;
7179 /* Gimplify the gross structure of OpenMP target update construct. */
7181 static void
7182 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7184 tree expr = *expr_p;
7185 gimple_omp_target stmt;
7187 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7188 ORT_WORKSHARE);
7189 gimplify_adjust_omp_clauses (pre_p, &OMP_TARGET_UPDATE_CLAUSES (expr));
7190 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7191 OMP_TARGET_UPDATE_CLAUSES (expr));
7193 gimplify_seq_add_stmt (pre_p, stmt);
7194 *expr_p = NULL_TREE;
7197 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7198 stabilized the lhs of the atomic operation as *ADDR. Return true if
7199 EXPR is this stabilized form. */
7201 static bool
7202 goa_lhs_expr_p (tree expr, tree addr)
7204 /* Also include casts to other type variants. The C front end is fond
7205 of adding these for e.g. volatile variables. This is like
7206 STRIP_TYPE_NOPS but includes the main variant lookup. */
7207 STRIP_USELESS_TYPE_CONVERSION (expr);
7209 if (TREE_CODE (expr) == INDIRECT_REF)
7211 expr = TREE_OPERAND (expr, 0);
7212 while (expr != addr
7213 && (CONVERT_EXPR_P (expr)
7214 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7215 && TREE_CODE (expr) == TREE_CODE (addr)
7216 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7218 expr = TREE_OPERAND (expr, 0);
7219 addr = TREE_OPERAND (addr, 0);
7221 if (expr == addr)
7222 return true;
7223 return (TREE_CODE (addr) == ADDR_EXPR
7224 && TREE_CODE (expr) == ADDR_EXPR
7225 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7227 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7228 return true;
7229 return false;
7232 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7233 expression does not involve the lhs, evaluate it into a temporary.
7234 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7235 or -1 if an error was encountered. */
7237 static int
7238 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7239 tree lhs_var)
7241 tree expr = *expr_p;
7242 int saw_lhs;
7244 if (goa_lhs_expr_p (expr, lhs_addr))
7246 *expr_p = lhs_var;
7247 return 1;
7249 if (is_gimple_val (expr))
7250 return 0;
7252 saw_lhs = 0;
7253 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7255 case tcc_binary:
7256 case tcc_comparison:
7257 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7258 lhs_var);
7259 case tcc_unary:
7260 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7261 lhs_var);
7262 break;
7263 case tcc_expression:
7264 switch (TREE_CODE (expr))
7266 case TRUTH_ANDIF_EXPR:
7267 case TRUTH_ORIF_EXPR:
7268 case TRUTH_AND_EXPR:
7269 case TRUTH_OR_EXPR:
7270 case TRUTH_XOR_EXPR:
7271 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7272 lhs_addr, lhs_var);
7273 case TRUTH_NOT_EXPR:
7274 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7275 lhs_addr, lhs_var);
7276 break;
7277 case COMPOUND_EXPR:
7278 /* Break out any preevaluations from cp_build_modify_expr. */
7279 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7280 expr = TREE_OPERAND (expr, 1))
7281 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7282 *expr_p = expr;
7283 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7284 default:
7285 break;
7287 break;
7288 default:
7289 break;
7292 if (saw_lhs == 0)
7294 enum gimplify_status gs;
7295 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7296 if (gs != GS_ALL_DONE)
7297 saw_lhs = -1;
7300 return saw_lhs;
7303 /* Gimplify an OMP_ATOMIC statement. */
7305 static enum gimplify_status
7306 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7308 tree addr = TREE_OPERAND (*expr_p, 0);
7309 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7310 ? NULL : TREE_OPERAND (*expr_p, 1);
7311 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7312 tree tmp_load;
7313 gimple_omp_atomic_load loadstmt;
7314 gimple_omp_atomic_store storestmt;
7316 tmp_load = create_tmp_reg (type, NULL);
7317 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7318 return GS_ERROR;
7320 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7321 != GS_ALL_DONE)
7322 return GS_ERROR;
7324 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7325 gimplify_seq_add_stmt (pre_p, loadstmt);
7326 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7327 != GS_ALL_DONE)
7328 return GS_ERROR;
7330 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7331 rhs = tmp_load;
7332 storestmt = gimple_build_omp_atomic_store (rhs);
7333 gimplify_seq_add_stmt (pre_p, storestmt);
7334 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7336 gimple_omp_atomic_set_seq_cst (loadstmt);
7337 gimple_omp_atomic_set_seq_cst (storestmt);
7339 switch (TREE_CODE (*expr_p))
7341 case OMP_ATOMIC_READ:
7342 case OMP_ATOMIC_CAPTURE_OLD:
7343 *expr_p = tmp_load;
7344 gimple_omp_atomic_set_need_value (loadstmt);
7345 break;
7346 case OMP_ATOMIC_CAPTURE_NEW:
7347 *expr_p = rhs;
7348 gimple_omp_atomic_set_need_value (storestmt);
7349 break;
7350 default:
7351 *expr_p = NULL;
7352 break;
7355 return GS_ALL_DONE;
7358 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7359 body, and adding some EH bits. */
7361 static enum gimplify_status
7362 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7364 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7365 gimple body_stmt;
7366 gimple_transaction trans_stmt;
7367 gimple_seq body = NULL;
7368 int subcode = 0;
7370 /* Wrap the transaction body in a BIND_EXPR so we have a context
7371 where to put decls for OpenMP. */
7372 if (TREE_CODE (tbody) != BIND_EXPR)
7374 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7375 TREE_SIDE_EFFECTS (bind) = 1;
7376 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7377 TRANSACTION_EXPR_BODY (expr) = bind;
7380 push_gimplify_context ();
7381 temp = voidify_wrapper_expr (*expr_p, NULL);
7383 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7384 pop_gimplify_context (body_stmt);
7386 trans_stmt = gimple_build_transaction (body, NULL);
7387 if (TRANSACTION_EXPR_OUTER (expr))
7388 subcode = GTMA_IS_OUTER;
7389 else if (TRANSACTION_EXPR_RELAXED (expr))
7390 subcode = GTMA_IS_RELAXED;
7391 gimple_transaction_set_subcode (trans_stmt, subcode);
7393 gimplify_seq_add_stmt (pre_p, trans_stmt);
7395 if (temp)
7397 *expr_p = temp;
7398 return GS_OK;
7401 *expr_p = NULL_TREE;
7402 return GS_ALL_DONE;
7405 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7406 expression produces a value to be used as an operand inside a GIMPLE
7407 statement, the value will be stored back in *EXPR_P. This value will
7408 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7409 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7410 emitted in PRE_P and POST_P.
7412 Additionally, this process may overwrite parts of the input
7413 expression during gimplification. Ideally, it should be
7414 possible to do non-destructive gimplification.
7416 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7417 the expression needs to evaluate to a value to be used as
7418 an operand in a GIMPLE statement, this value will be stored in
7419 *EXPR_P on exit. This happens when the caller specifies one
7420 of fb_lvalue or fb_rvalue fallback flags.
7422 PRE_P will contain the sequence of GIMPLE statements corresponding
7423 to the evaluation of EXPR and all the side-effects that must
7424 be executed before the main expression. On exit, the last
7425 statement of PRE_P is the core statement being gimplified. For
7426 instance, when gimplifying 'if (++a)' the last statement in
7427 PRE_P will be 'if (t.1)' where t.1 is the result of
7428 pre-incrementing 'a'.
7430 POST_P will contain the sequence of GIMPLE statements corresponding
7431 to the evaluation of all the side-effects that must be executed
7432 after the main expression. If this is NULL, the post
7433 side-effects are stored at the end of PRE_P.
7435 The reason why the output is split in two is to handle post
7436 side-effects explicitly. In some cases, an expression may have
7437 inner and outer post side-effects which need to be emitted in
7438 an order different from the one given by the recursive
7439 traversal. For instance, for the expression (*p--)++ the post
7440 side-effects of '--' must actually occur *after* the post
7441 side-effects of '++'. However, gimplification will first visit
7442 the inner expression, so if a separate POST sequence was not
7443 used, the resulting sequence would be:
7445 1 t.1 = *p
7446 2 p = p - 1
7447 3 t.2 = t.1 + 1
7448 4 *p = t.2
7450 However, the post-decrement operation in line #2 must not be
7451 evaluated until after the store to *p at line #4, so the
7452 correct sequence should be:
7454 1 t.1 = *p
7455 2 t.2 = t.1 + 1
7456 3 *p = t.2
7457 4 p = p - 1
7459 So, by specifying a separate post queue, it is possible
7460 to emit the post side-effects in the correct order.
7461 If POST_P is NULL, an internal queue will be used. Before
7462 returning to the caller, the sequence POST_P is appended to
7463 the main output sequence PRE_P.
7465 GIMPLE_TEST_F points to a function that takes a tree T and
7466 returns nonzero if T is in the GIMPLE form requested by the
7467 caller. The GIMPLE predicates are in gimple.c.
7469 FALLBACK tells the function what sort of a temporary we want if
7470 gimplification cannot produce an expression that complies with
7471 GIMPLE_TEST_F.
7473 fb_none means that no temporary should be generated
7474 fb_rvalue means that an rvalue is OK to generate
7475 fb_lvalue means that an lvalue is OK to generate
7476 fb_either means that either is OK, but an lvalue is preferable.
7477 fb_mayfail means that gimplification may fail (in which case
7478 GS_ERROR will be returned)
7480 The return value is either GS_ERROR or GS_ALL_DONE, since this
7481 function iterates until EXPR is completely gimplified or an error
7482 occurs. */
7484 enum gimplify_status
7485 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7486 bool (*gimple_test_f) (tree), fallback_t fallback)
7488 tree tmp;
7489 gimple_seq internal_pre = NULL;
7490 gimple_seq internal_post = NULL;
7491 tree save_expr;
7492 bool is_statement;
7493 location_t saved_location;
7494 enum gimplify_status ret;
7495 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7497 save_expr = *expr_p;
7498 if (save_expr == NULL_TREE)
7499 return GS_ALL_DONE;
7501 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7502 is_statement = gimple_test_f == is_gimple_stmt;
7503 if (is_statement)
7504 gcc_assert (pre_p);
7506 /* Consistency checks. */
7507 if (gimple_test_f == is_gimple_reg)
7508 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7509 else if (gimple_test_f == is_gimple_val
7510 || gimple_test_f == is_gimple_call_addr
7511 || gimple_test_f == is_gimple_condexpr
7512 || gimple_test_f == is_gimple_mem_rhs
7513 || gimple_test_f == is_gimple_mem_rhs_or_call
7514 || gimple_test_f == is_gimple_reg_rhs
7515 || gimple_test_f == is_gimple_reg_rhs_or_call
7516 || gimple_test_f == is_gimple_asm_val
7517 || gimple_test_f == is_gimple_mem_ref_addr)
7518 gcc_assert (fallback & fb_rvalue);
7519 else if (gimple_test_f == is_gimple_min_lval
7520 || gimple_test_f == is_gimple_lvalue)
7521 gcc_assert (fallback & fb_lvalue);
7522 else if (gimple_test_f == is_gimple_addressable)
7523 gcc_assert (fallback & fb_either);
7524 else if (gimple_test_f == is_gimple_stmt)
7525 gcc_assert (fallback == fb_none);
7526 else
7528 /* We should have recognized the GIMPLE_TEST_F predicate to
7529 know what kind of fallback to use in case a temporary is
7530 needed to hold the value or address of *EXPR_P. */
7531 gcc_unreachable ();
7534 /* We used to check the predicate here and return immediately if it
7535 succeeds. This is wrong; the design is for gimplification to be
7536 idempotent, and for the predicates to only test for valid forms, not
7537 whether they are fully simplified. */
7538 if (pre_p == NULL)
7539 pre_p = &internal_pre;
7541 if (post_p == NULL)
7542 post_p = &internal_post;
7544 /* Remember the last statements added to PRE_P and POST_P. Every
7545 new statement added by the gimplification helpers needs to be
7546 annotated with location information. To centralize the
7547 responsibility, we remember the last statement that had been
7548 added to both queues before gimplifying *EXPR_P. If
7549 gimplification produces new statements in PRE_P and POST_P, those
7550 statements will be annotated with the same location information
7551 as *EXPR_P. */
7552 pre_last_gsi = gsi_last (*pre_p);
7553 post_last_gsi = gsi_last (*post_p);
7555 saved_location = input_location;
7556 if (save_expr != error_mark_node
7557 && EXPR_HAS_LOCATION (*expr_p))
7558 input_location = EXPR_LOCATION (*expr_p);
7560 /* Loop over the specific gimplifiers until the toplevel node
7561 remains the same. */
7564 /* Strip away as many useless type conversions as possible
7565 at the toplevel. */
7566 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7568 /* Remember the expr. */
7569 save_expr = *expr_p;
7571 /* Die, die, die, my darling. */
7572 if (save_expr == error_mark_node
7573 || (TREE_TYPE (save_expr)
7574 && TREE_TYPE (save_expr) == error_mark_node))
7576 ret = GS_ERROR;
7577 break;
7580 /* Do any language-specific gimplification. */
7581 ret = ((enum gimplify_status)
7582 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7583 if (ret == GS_OK)
7585 if (*expr_p == NULL_TREE)
7586 break;
7587 if (*expr_p != save_expr)
7588 continue;
7590 else if (ret != GS_UNHANDLED)
7591 break;
7593 /* Make sure that all the cases set 'ret' appropriately. */
7594 ret = GS_UNHANDLED;
7595 switch (TREE_CODE (*expr_p))
7597 /* First deal with the special cases. */
7599 case POSTINCREMENT_EXPR:
7600 case POSTDECREMENT_EXPR:
7601 case PREINCREMENT_EXPR:
7602 case PREDECREMENT_EXPR:
7603 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7604 fallback != fb_none,
7605 TREE_TYPE (*expr_p));
7606 break;
7608 case VIEW_CONVERT_EXPR:
7609 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7610 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7612 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7613 post_p, is_gimple_val, fb_rvalue);
7614 recalculate_side_effects (*expr_p);
7615 break;
7617 /* Fallthru. */
7619 case ARRAY_REF:
7620 case ARRAY_RANGE_REF:
7621 case REALPART_EXPR:
7622 case IMAGPART_EXPR:
7623 case COMPONENT_REF:
7624 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7625 fallback ? fallback : fb_rvalue);
7626 break;
7628 case COND_EXPR:
7629 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7631 /* C99 code may assign to an array in a structure value of a
7632 conditional expression, and this has undefined behavior
7633 only on execution, so create a temporary if an lvalue is
7634 required. */
7635 if (fallback == fb_lvalue)
7637 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7638 mark_addressable (*expr_p);
7639 ret = GS_OK;
7641 break;
7643 case CALL_EXPR:
7644 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7646 /* C99 code may assign to an array in a structure returned
7647 from a function, and this has undefined behavior only on
7648 execution, so create a temporary if an lvalue is
7649 required. */
7650 if (fallback == fb_lvalue)
7652 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7653 mark_addressable (*expr_p);
7654 ret = GS_OK;
7656 break;
7658 case TREE_LIST:
7659 gcc_unreachable ();
7661 case COMPOUND_EXPR:
7662 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7663 break;
7665 case COMPOUND_LITERAL_EXPR:
7666 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7667 gimple_test_f, fallback);
7668 break;
7670 case MODIFY_EXPR:
7671 case INIT_EXPR:
7672 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7673 fallback != fb_none);
7674 break;
7676 case TRUTH_ANDIF_EXPR:
7677 case TRUTH_ORIF_EXPR:
7679 /* Preserve the original type of the expression and the
7680 source location of the outer expression. */
7681 tree org_type = TREE_TYPE (*expr_p);
7682 *expr_p = gimple_boolify (*expr_p);
7683 *expr_p = build3_loc (input_location, COND_EXPR,
7684 org_type, *expr_p,
7685 fold_convert_loc
7686 (input_location,
7687 org_type, boolean_true_node),
7688 fold_convert_loc
7689 (input_location,
7690 org_type, boolean_false_node));
7691 ret = GS_OK;
7692 break;
7695 case TRUTH_NOT_EXPR:
7697 tree type = TREE_TYPE (*expr_p);
7698 /* The parsers are careful to generate TRUTH_NOT_EXPR
7699 only with operands that are always zero or one.
7700 We do not fold here but handle the only interesting case
7701 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7702 *expr_p = gimple_boolify (*expr_p);
7703 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7704 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7705 TREE_TYPE (*expr_p),
7706 TREE_OPERAND (*expr_p, 0));
7707 else
7708 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7709 TREE_TYPE (*expr_p),
7710 TREE_OPERAND (*expr_p, 0),
7711 build_int_cst (TREE_TYPE (*expr_p), 1));
7712 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7713 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7714 ret = GS_OK;
7715 break;
7718 case ADDR_EXPR:
7719 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7720 break;
7722 case ANNOTATE_EXPR:
7724 tree cond = TREE_OPERAND (*expr_p, 0);
7725 tree kind = TREE_OPERAND (*expr_p, 1);
7726 tree type = TREE_TYPE (cond);
7727 if (!INTEGRAL_TYPE_P (type))
7729 *expr_p = cond;
7730 ret = GS_OK;
7731 break;
7733 tree tmp = create_tmp_var (type, NULL);
7734 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7735 gimple_call call
7736 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7737 gimple_call_set_lhs (call, tmp);
7738 gimplify_seq_add_stmt (pre_p, call);
7739 *expr_p = tmp;
7740 ret = GS_ALL_DONE;
7741 break;
7744 case VA_ARG_EXPR:
7745 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7746 break;
7748 CASE_CONVERT:
7749 if (IS_EMPTY_STMT (*expr_p))
7751 ret = GS_ALL_DONE;
7752 break;
7755 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7756 || fallback == fb_none)
7758 /* Just strip a conversion to void (or in void context) and
7759 try again. */
7760 *expr_p = TREE_OPERAND (*expr_p, 0);
7761 ret = GS_OK;
7762 break;
7765 ret = gimplify_conversion (expr_p);
7766 if (ret == GS_ERROR)
7767 break;
7768 if (*expr_p != save_expr)
7769 break;
7770 /* FALLTHRU */
7772 case FIX_TRUNC_EXPR:
7773 /* unary_expr: ... | '(' cast ')' val | ... */
7774 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7775 is_gimple_val, fb_rvalue);
7776 recalculate_side_effects (*expr_p);
7777 break;
7779 case INDIRECT_REF:
7781 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7782 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7783 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7785 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7786 if (*expr_p != save_expr)
7788 ret = GS_OK;
7789 break;
7792 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7793 is_gimple_reg, fb_rvalue);
7794 if (ret == GS_ERROR)
7795 break;
7797 recalculate_side_effects (*expr_p);
7798 *expr_p = fold_build2_loc (input_location, MEM_REF,
7799 TREE_TYPE (*expr_p),
7800 TREE_OPERAND (*expr_p, 0),
7801 build_int_cst (saved_ptr_type, 0));
7802 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7803 TREE_THIS_NOTRAP (*expr_p) = notrap;
7804 ret = GS_OK;
7805 break;
7808 /* We arrive here through the various re-gimplifcation paths. */
7809 case MEM_REF:
7810 /* First try re-folding the whole thing. */
7811 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7812 TREE_OPERAND (*expr_p, 0),
7813 TREE_OPERAND (*expr_p, 1));
7814 if (tmp)
7816 *expr_p = tmp;
7817 recalculate_side_effects (*expr_p);
7818 ret = GS_OK;
7819 break;
7821 /* Avoid re-gimplifying the address operand if it is already
7822 in suitable form. Re-gimplifying would mark the address
7823 operand addressable. Always gimplify when not in SSA form
7824 as we still may have to gimplify decls with value-exprs. */
7825 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7826 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7828 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7829 is_gimple_mem_ref_addr, fb_rvalue);
7830 if (ret == GS_ERROR)
7831 break;
7833 recalculate_side_effects (*expr_p);
7834 ret = GS_ALL_DONE;
7835 break;
7837 /* Constants need not be gimplified. */
7838 case INTEGER_CST:
7839 case REAL_CST:
7840 case FIXED_CST:
7841 case STRING_CST:
7842 case COMPLEX_CST:
7843 case VECTOR_CST:
7844 /* Drop the overflow flag on constants, we do not want
7845 that in the GIMPLE IL. */
7846 if (TREE_OVERFLOW_P (*expr_p))
7847 *expr_p = drop_tree_overflow (*expr_p);
7848 ret = GS_ALL_DONE;
7849 break;
7851 case CONST_DECL:
7852 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7853 CONST_DECL node. Otherwise the decl is replaceable by its
7854 value. */
7855 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7856 if (fallback & fb_lvalue)
7857 ret = GS_ALL_DONE;
7858 else
7860 *expr_p = DECL_INITIAL (*expr_p);
7861 ret = GS_OK;
7863 break;
7865 case DECL_EXPR:
7866 ret = gimplify_decl_expr (expr_p, pre_p);
7867 break;
7869 case BIND_EXPR:
7870 ret = gimplify_bind_expr (expr_p, pre_p);
7871 break;
7873 case LOOP_EXPR:
7874 ret = gimplify_loop_expr (expr_p, pre_p);
7875 break;
7877 case SWITCH_EXPR:
7878 ret = gimplify_switch_expr (expr_p, pre_p);
7879 break;
7881 case EXIT_EXPR:
7882 ret = gimplify_exit_expr (expr_p);
7883 break;
7885 case GOTO_EXPR:
7886 /* If the target is not LABEL, then it is a computed jump
7887 and the target needs to be gimplified. */
7888 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7890 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7891 NULL, is_gimple_val, fb_rvalue);
7892 if (ret == GS_ERROR)
7893 break;
7895 gimplify_seq_add_stmt (pre_p,
7896 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7897 ret = GS_ALL_DONE;
7898 break;
7900 case PREDICT_EXPR:
7901 gimplify_seq_add_stmt (pre_p,
7902 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7903 PREDICT_EXPR_OUTCOME (*expr_p)));
7904 ret = GS_ALL_DONE;
7905 break;
7907 case LABEL_EXPR:
7908 ret = GS_ALL_DONE;
7909 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7910 == current_function_decl);
7911 gimplify_seq_add_stmt (pre_p,
7912 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7913 break;
7915 case CASE_LABEL_EXPR:
7916 ret = gimplify_case_label_expr (expr_p, pre_p);
7917 break;
7919 case RETURN_EXPR:
7920 ret = gimplify_return_expr (*expr_p, pre_p);
7921 break;
7923 case CONSTRUCTOR:
7924 /* Don't reduce this in place; let gimplify_init_constructor work its
7925 magic. Buf if we're just elaborating this for side effects, just
7926 gimplify any element that has side-effects. */
7927 if (fallback == fb_none)
7929 unsigned HOST_WIDE_INT ix;
7930 tree val;
7931 tree temp = NULL_TREE;
7932 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7933 if (TREE_SIDE_EFFECTS (val))
7934 append_to_statement_list (val, &temp);
7936 *expr_p = temp;
7937 ret = temp ? GS_OK : GS_ALL_DONE;
7939 /* C99 code may assign to an array in a constructed
7940 structure or union, and this has undefined behavior only
7941 on execution, so create a temporary if an lvalue is
7942 required. */
7943 else if (fallback == fb_lvalue)
7945 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7946 mark_addressable (*expr_p);
7947 ret = GS_OK;
7949 else
7950 ret = GS_ALL_DONE;
7951 break;
7953 /* The following are special cases that are not handled by the
7954 original GIMPLE grammar. */
7956 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7957 eliminated. */
7958 case SAVE_EXPR:
7959 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7960 break;
7962 case BIT_FIELD_REF:
7963 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7964 post_p, is_gimple_lvalue, fb_either);
7965 recalculate_side_effects (*expr_p);
7966 break;
7968 case TARGET_MEM_REF:
7970 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7972 if (TMR_BASE (*expr_p))
7973 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7974 post_p, is_gimple_mem_ref_addr, fb_either);
7975 if (TMR_INDEX (*expr_p))
7976 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7977 post_p, is_gimple_val, fb_rvalue);
7978 if (TMR_INDEX2 (*expr_p))
7979 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7980 post_p, is_gimple_val, fb_rvalue);
7981 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7982 ret = MIN (r0, r1);
7984 break;
7986 case NON_LVALUE_EXPR:
7987 /* This should have been stripped above. */
7988 gcc_unreachable ();
7990 case ASM_EXPR:
7991 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7992 break;
7994 case TRY_FINALLY_EXPR:
7995 case TRY_CATCH_EXPR:
7997 gimple_seq eval, cleanup;
7998 gimple_try try_;
8000 /* Calls to destructors are generated automatically in FINALLY/CATCH
8001 block. They should have location as UNKNOWN_LOCATION. However,
8002 gimplify_call_expr will reset these call stmts to input_location
8003 if it finds stmt's location is unknown. To prevent resetting for
8004 destructors, we set the input_location to unknown.
8005 Note that this only affects the destructor calls in FINALLY/CATCH
8006 block, and will automatically reset to its original value by the
8007 end of gimplify_expr. */
8008 input_location = UNKNOWN_LOCATION;
8009 eval = cleanup = NULL;
8010 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8011 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8012 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8013 if (gimple_seq_empty_p (cleanup))
8015 gimple_seq_add_seq (pre_p, eval);
8016 ret = GS_ALL_DONE;
8017 break;
8019 try_ = gimple_build_try (eval, cleanup,
8020 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8021 ? GIMPLE_TRY_FINALLY
8022 : GIMPLE_TRY_CATCH);
8023 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8024 gimple_set_location (try_, saved_location);
8025 else
8026 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8027 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8028 gimple_try_set_catch_is_cleanup (try_,
8029 TRY_CATCH_IS_CLEANUP (*expr_p));
8030 gimplify_seq_add_stmt (pre_p, try_);
8031 ret = GS_ALL_DONE;
8032 break;
8035 case CLEANUP_POINT_EXPR:
8036 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8037 break;
8039 case TARGET_EXPR:
8040 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8041 break;
8043 case CATCH_EXPR:
8045 gimple c;
8046 gimple_seq handler = NULL;
8047 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8048 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8049 gimplify_seq_add_stmt (pre_p, c);
8050 ret = GS_ALL_DONE;
8051 break;
8054 case EH_FILTER_EXPR:
8056 gimple ehf;
8057 gimple_seq failure = NULL;
8059 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8060 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8061 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8062 gimplify_seq_add_stmt (pre_p, ehf);
8063 ret = GS_ALL_DONE;
8064 break;
8067 case OBJ_TYPE_REF:
8069 enum gimplify_status r0, r1;
8070 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8071 post_p, is_gimple_val, fb_rvalue);
8072 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8073 post_p, is_gimple_val, fb_rvalue);
8074 TREE_SIDE_EFFECTS (*expr_p) = 0;
8075 ret = MIN (r0, r1);
8077 break;
8079 case LABEL_DECL:
8080 /* We get here when taking the address of a label. We mark
8081 the label as "forced"; meaning it can never be removed and
8082 it is a potential target for any computed goto. */
8083 FORCED_LABEL (*expr_p) = 1;
8084 ret = GS_ALL_DONE;
8085 break;
8087 case STATEMENT_LIST:
8088 ret = gimplify_statement_list (expr_p, pre_p);
8089 break;
8091 case WITH_SIZE_EXPR:
8093 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8094 post_p == &internal_post ? NULL : post_p,
8095 gimple_test_f, fallback);
8096 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8097 is_gimple_val, fb_rvalue);
8098 ret = GS_ALL_DONE;
8100 break;
8102 case VAR_DECL:
8103 case PARM_DECL:
8104 ret = gimplify_var_or_parm_decl (expr_p);
8105 break;
8107 case RESULT_DECL:
8108 /* When within an OpenMP context, notice uses of variables. */
8109 if (gimplify_omp_ctxp)
8110 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8111 ret = GS_ALL_DONE;
8112 break;
8114 case SSA_NAME:
8115 /* Allow callbacks into the gimplifier during optimization. */
8116 ret = GS_ALL_DONE;
8117 break;
8119 case OMP_PARALLEL:
8120 gimplify_omp_parallel (expr_p, pre_p);
8121 ret = GS_ALL_DONE;
8122 break;
8124 case OMP_TASK:
8125 gimplify_omp_task (expr_p, pre_p);
8126 ret = GS_ALL_DONE;
8127 break;
8129 case OMP_FOR:
8130 case OMP_SIMD:
8131 case CILK_SIMD:
8132 case CILK_FOR:
8133 case OMP_DISTRIBUTE:
8134 ret = gimplify_omp_for (expr_p, pre_p);
8135 break;
8137 case OMP_SECTIONS:
8138 case OMP_SINGLE:
8139 case OMP_TARGET:
8140 case OMP_TARGET_DATA:
8141 case OMP_TEAMS:
8142 gimplify_omp_workshare (expr_p, pre_p);
8143 ret = GS_ALL_DONE;
8144 break;
8146 case OMP_TARGET_UPDATE:
8147 gimplify_omp_target_update (expr_p, pre_p);
8148 ret = GS_ALL_DONE;
8149 break;
8151 case OMP_SECTION:
8152 case OMP_MASTER:
8153 case OMP_TASKGROUP:
8154 case OMP_ORDERED:
8155 case OMP_CRITICAL:
8157 gimple_seq body = NULL;
8158 gimple g;
8160 gimplify_and_add (OMP_BODY (*expr_p), &body);
8161 switch (TREE_CODE (*expr_p))
8163 case OMP_SECTION:
8164 g = gimple_build_omp_section (body);
8165 break;
8166 case OMP_MASTER:
8167 g = gimple_build_omp_master (body);
8168 break;
8169 case OMP_TASKGROUP:
8171 gimple_seq cleanup = NULL;
8172 tree fn
8173 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8174 g = gimple_build_call (fn, 0);
8175 gimple_seq_add_stmt (&cleanup, g);
8176 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8177 body = NULL;
8178 gimple_seq_add_stmt (&body, g);
8179 g = gimple_build_omp_taskgroup (body);
8181 break;
8182 case OMP_ORDERED:
8183 g = gimple_build_omp_ordered (body);
8184 break;
8185 case OMP_CRITICAL:
8186 g = gimple_build_omp_critical (body,
8187 OMP_CRITICAL_NAME (*expr_p));
8188 break;
8189 default:
8190 gcc_unreachable ();
8192 gimplify_seq_add_stmt (pre_p, g);
8193 ret = GS_ALL_DONE;
8194 break;
8197 case OMP_ATOMIC:
8198 case OMP_ATOMIC_READ:
8199 case OMP_ATOMIC_CAPTURE_OLD:
8200 case OMP_ATOMIC_CAPTURE_NEW:
8201 ret = gimplify_omp_atomic (expr_p, pre_p);
8202 break;
8204 case TRANSACTION_EXPR:
8205 ret = gimplify_transaction (expr_p, pre_p);
8206 break;
8208 case TRUTH_AND_EXPR:
8209 case TRUTH_OR_EXPR:
8210 case TRUTH_XOR_EXPR:
8212 tree orig_type = TREE_TYPE (*expr_p);
8213 tree new_type, xop0, xop1;
8214 *expr_p = gimple_boolify (*expr_p);
8215 new_type = TREE_TYPE (*expr_p);
8216 if (!useless_type_conversion_p (orig_type, new_type))
8218 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8219 ret = GS_OK;
8220 break;
8223 /* Boolified binary truth expressions are semantically equivalent
8224 to bitwise binary expressions. Canonicalize them to the
8225 bitwise variant. */
8226 switch (TREE_CODE (*expr_p))
8228 case TRUTH_AND_EXPR:
8229 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8230 break;
8231 case TRUTH_OR_EXPR:
8232 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8233 break;
8234 case TRUTH_XOR_EXPR:
8235 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8236 break;
8237 default:
8238 break;
8240 /* Now make sure that operands have compatible type to
8241 expression's new_type. */
8242 xop0 = TREE_OPERAND (*expr_p, 0);
8243 xop1 = TREE_OPERAND (*expr_p, 1);
8244 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8245 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8246 new_type,
8247 xop0);
8248 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8249 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8250 new_type,
8251 xop1);
8252 /* Continue classified as tcc_binary. */
8253 goto expr_2;
8256 case FMA_EXPR:
8257 case VEC_COND_EXPR:
8258 case VEC_PERM_EXPR:
8259 /* Classified as tcc_expression. */
8260 goto expr_3;
8262 case POINTER_PLUS_EXPR:
8264 enum gimplify_status r0, r1;
8265 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8266 post_p, is_gimple_val, fb_rvalue);
8267 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8268 post_p, is_gimple_val, fb_rvalue);
8269 recalculate_side_effects (*expr_p);
8270 ret = MIN (r0, r1);
8271 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8272 after gimplifying operands - this is similar to how
8273 it would be folding all gimplified stmts on creation
8274 to have them canonicalized, which is what we eventually
8275 should do anyway. */
8276 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8277 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8279 *expr_p = build_fold_addr_expr_with_type_loc
8280 (input_location,
8281 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8282 TREE_OPERAND (*expr_p, 0),
8283 fold_convert (ptr_type_node,
8284 TREE_OPERAND (*expr_p, 1))),
8285 TREE_TYPE (*expr_p));
8286 ret = MIN (ret, GS_OK);
8288 break;
8291 case CILK_SYNC_STMT:
8293 if (!fn_contains_cilk_spawn_p (cfun))
8295 error_at (EXPR_LOCATION (*expr_p),
8296 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8297 ret = GS_ERROR;
8299 else
8301 gimplify_cilk_sync (expr_p, pre_p);
8302 ret = GS_ALL_DONE;
8304 break;
8307 default:
8308 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8310 case tcc_comparison:
8311 /* Handle comparison of objects of non scalar mode aggregates
8312 with a call to memcmp. It would be nice to only have to do
8313 this for variable-sized objects, but then we'd have to allow
8314 the same nest of reference nodes we allow for MODIFY_EXPR and
8315 that's too complex.
8317 Compare scalar mode aggregates as scalar mode values. Using
8318 memcmp for them would be very inefficient at best, and is
8319 plain wrong if bitfields are involved. */
8321 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8323 /* Vector comparisons need no boolification. */
8324 if (TREE_CODE (type) == VECTOR_TYPE)
8325 goto expr_2;
8326 else if (!AGGREGATE_TYPE_P (type))
8328 tree org_type = TREE_TYPE (*expr_p);
8329 *expr_p = gimple_boolify (*expr_p);
8330 if (!useless_type_conversion_p (org_type,
8331 TREE_TYPE (*expr_p)))
8333 *expr_p = fold_convert_loc (input_location,
8334 org_type, *expr_p);
8335 ret = GS_OK;
8337 else
8338 goto expr_2;
8340 else if (TYPE_MODE (type) != BLKmode)
8341 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8342 else
8343 ret = gimplify_variable_sized_compare (expr_p);
8345 break;
8348 /* If *EXPR_P does not need to be special-cased, handle it
8349 according to its class. */
8350 case tcc_unary:
8351 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8352 post_p, is_gimple_val, fb_rvalue);
8353 break;
8355 case tcc_binary:
8356 expr_2:
8358 enum gimplify_status r0, r1;
8360 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8361 post_p, is_gimple_val, fb_rvalue);
8362 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8363 post_p, is_gimple_val, fb_rvalue);
8365 ret = MIN (r0, r1);
8366 break;
8369 expr_3:
8371 enum gimplify_status r0, r1, r2;
8373 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8374 post_p, is_gimple_val, fb_rvalue);
8375 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8376 post_p, is_gimple_val, fb_rvalue);
8377 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8378 post_p, is_gimple_val, fb_rvalue);
8380 ret = MIN (MIN (r0, r1), r2);
8381 break;
8384 case tcc_declaration:
8385 case tcc_constant:
8386 ret = GS_ALL_DONE;
8387 goto dont_recalculate;
8389 default:
8390 gcc_unreachable ();
8393 recalculate_side_effects (*expr_p);
8395 dont_recalculate:
8396 break;
8399 gcc_assert (*expr_p || ret != GS_OK);
8401 while (ret == GS_OK);
8403 /* If we encountered an error_mark somewhere nested inside, either
8404 stub out the statement or propagate the error back out. */
8405 if (ret == GS_ERROR)
8407 if (is_statement)
8408 *expr_p = NULL;
8409 goto out;
8412 /* This was only valid as a return value from the langhook, which
8413 we handled. Make sure it doesn't escape from any other context. */
8414 gcc_assert (ret != GS_UNHANDLED);
8416 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8418 /* We aren't looking for a value, and we don't have a valid
8419 statement. If it doesn't have side-effects, throw it away. */
8420 if (!TREE_SIDE_EFFECTS (*expr_p))
8421 *expr_p = NULL;
8422 else if (!TREE_THIS_VOLATILE (*expr_p))
8424 /* This is probably a _REF that contains something nested that
8425 has side effects. Recurse through the operands to find it. */
8426 enum tree_code code = TREE_CODE (*expr_p);
8428 switch (code)
8430 case COMPONENT_REF:
8431 case REALPART_EXPR:
8432 case IMAGPART_EXPR:
8433 case VIEW_CONVERT_EXPR:
8434 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8435 gimple_test_f, fallback);
8436 break;
8438 case ARRAY_REF:
8439 case ARRAY_RANGE_REF:
8440 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8441 gimple_test_f, fallback);
8442 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8443 gimple_test_f, fallback);
8444 break;
8446 default:
8447 /* Anything else with side-effects must be converted to
8448 a valid statement before we get here. */
8449 gcc_unreachable ();
8452 *expr_p = NULL;
8454 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8455 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8457 /* Historically, the compiler has treated a bare reference
8458 to a non-BLKmode volatile lvalue as forcing a load. */
8459 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8461 /* Normally, we do not want to create a temporary for a
8462 TREE_ADDRESSABLE type because such a type should not be
8463 copied by bitwise-assignment. However, we make an
8464 exception here, as all we are doing here is ensuring that
8465 we read the bytes that make up the type. We use
8466 create_tmp_var_raw because create_tmp_var will abort when
8467 given a TREE_ADDRESSABLE type. */
8468 tree tmp = create_tmp_var_raw (type, "vol");
8469 gimple_add_tmp_var (tmp);
8470 gimplify_assign (tmp, *expr_p, pre_p);
8471 *expr_p = NULL;
8473 else
8474 /* We can't do anything useful with a volatile reference to
8475 an incomplete type, so just throw it away. Likewise for
8476 a BLKmode type, since any implicit inner load should
8477 already have been turned into an explicit one by the
8478 gimplification process. */
8479 *expr_p = NULL;
8482 /* If we are gimplifying at the statement level, we're done. Tack
8483 everything together and return. */
8484 if (fallback == fb_none || is_statement)
8486 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8487 it out for GC to reclaim it. */
8488 *expr_p = NULL_TREE;
8490 if (!gimple_seq_empty_p (internal_pre)
8491 || !gimple_seq_empty_p (internal_post))
8493 gimplify_seq_add_seq (&internal_pre, internal_post);
8494 gimplify_seq_add_seq (pre_p, internal_pre);
8497 /* The result of gimplifying *EXPR_P is going to be the last few
8498 statements in *PRE_P and *POST_P. Add location information
8499 to all the statements that were added by the gimplification
8500 helpers. */
8501 if (!gimple_seq_empty_p (*pre_p))
8502 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8504 if (!gimple_seq_empty_p (*post_p))
8505 annotate_all_with_location_after (*post_p, post_last_gsi,
8506 input_location);
8508 goto out;
8511 #ifdef ENABLE_GIMPLE_CHECKING
8512 if (*expr_p)
8514 enum tree_code code = TREE_CODE (*expr_p);
8515 /* These expressions should already be in gimple IR form. */
8516 gcc_assert (code != MODIFY_EXPR
8517 && code != ASM_EXPR
8518 && code != BIND_EXPR
8519 && code != CATCH_EXPR
8520 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8521 && code != EH_FILTER_EXPR
8522 && code != GOTO_EXPR
8523 && code != LABEL_EXPR
8524 && code != LOOP_EXPR
8525 && code != SWITCH_EXPR
8526 && code != TRY_FINALLY_EXPR
8527 && code != OMP_CRITICAL
8528 && code != OMP_FOR
8529 && code != OMP_MASTER
8530 && code != OMP_TASKGROUP
8531 && code != OMP_ORDERED
8532 && code != OMP_PARALLEL
8533 && code != OMP_SECTIONS
8534 && code != OMP_SECTION
8535 && code != OMP_SINGLE);
8537 #endif
8539 /* Otherwise we're gimplifying a subexpression, so the resulting
8540 value is interesting. If it's a valid operand that matches
8541 GIMPLE_TEST_F, we're done. Unless we are handling some
8542 post-effects internally; if that's the case, we need to copy into
8543 a temporary before adding the post-effects to POST_P. */
8544 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8545 goto out;
8547 /* Otherwise, we need to create a new temporary for the gimplified
8548 expression. */
8550 /* We can't return an lvalue if we have an internal postqueue. The
8551 object the lvalue refers to would (probably) be modified by the
8552 postqueue; we need to copy the value out first, which means an
8553 rvalue. */
8554 if ((fallback & fb_lvalue)
8555 && gimple_seq_empty_p (internal_post)
8556 && is_gimple_addressable (*expr_p))
8558 /* An lvalue will do. Take the address of the expression, store it
8559 in a temporary, and replace the expression with an INDIRECT_REF of
8560 that temporary. */
8561 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8562 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8563 *expr_p = build_simple_mem_ref (tmp);
8565 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8567 /* An rvalue will do. Assign the gimplified expression into a
8568 new temporary TMP and replace the original expression with
8569 TMP. First, make sure that the expression has a type so that
8570 it can be assigned into a temporary. */
8571 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8572 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8574 else
8576 #ifdef ENABLE_GIMPLE_CHECKING
8577 if (!(fallback & fb_mayfail))
8579 fprintf (stderr, "gimplification failed:\n");
8580 print_generic_expr (stderr, *expr_p, 0);
8581 debug_tree (*expr_p);
8582 internal_error ("gimplification failed");
8584 #endif
8585 gcc_assert (fallback & fb_mayfail);
8587 /* If this is an asm statement, and the user asked for the
8588 impossible, don't die. Fail and let gimplify_asm_expr
8589 issue an error. */
8590 ret = GS_ERROR;
8591 goto out;
8594 /* Make sure the temporary matches our predicate. */
8595 gcc_assert ((*gimple_test_f) (*expr_p));
8597 if (!gimple_seq_empty_p (internal_post))
8599 annotate_all_with_location (internal_post, input_location);
8600 gimplify_seq_add_seq (pre_p, internal_post);
8603 out:
8604 input_location = saved_location;
8605 return ret;
8608 /* Look through TYPE for variable-sized objects and gimplify each such
8609 size that we find. Add to LIST_P any statements generated. */
8611 void
8612 gimplify_type_sizes (tree type, gimple_seq *list_p)
8614 tree field, t;
8616 if (type == NULL || type == error_mark_node)
8617 return;
8619 /* We first do the main variant, then copy into any other variants. */
8620 type = TYPE_MAIN_VARIANT (type);
8622 /* Avoid infinite recursion. */
8623 if (TYPE_SIZES_GIMPLIFIED (type))
8624 return;
8626 TYPE_SIZES_GIMPLIFIED (type) = 1;
8628 switch (TREE_CODE (type))
8630 case INTEGER_TYPE:
8631 case ENUMERAL_TYPE:
8632 case BOOLEAN_TYPE:
8633 case REAL_TYPE:
8634 case FIXED_POINT_TYPE:
8635 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8636 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8638 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8640 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8641 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8643 break;
8645 case ARRAY_TYPE:
8646 /* These types may not have declarations, so handle them here. */
8647 gimplify_type_sizes (TREE_TYPE (type), list_p);
8648 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8649 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8650 with assigned stack slots, for -O1+ -g they should be tracked
8651 by VTA. */
8652 if (!(TYPE_NAME (type)
8653 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8654 && DECL_IGNORED_P (TYPE_NAME (type)))
8655 && TYPE_DOMAIN (type)
8656 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8658 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8659 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8660 DECL_IGNORED_P (t) = 0;
8661 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8662 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8663 DECL_IGNORED_P (t) = 0;
8665 break;
8667 case RECORD_TYPE:
8668 case UNION_TYPE:
8669 case QUAL_UNION_TYPE:
8670 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8671 if (TREE_CODE (field) == FIELD_DECL)
8673 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8674 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8675 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8676 gimplify_type_sizes (TREE_TYPE (field), list_p);
8678 break;
8680 case POINTER_TYPE:
8681 case REFERENCE_TYPE:
8682 /* We used to recurse on the pointed-to type here, which turned out to
8683 be incorrect because its definition might refer to variables not
8684 yet initialized at this point if a forward declaration is involved.
8686 It was actually useful for anonymous pointed-to types to ensure
8687 that the sizes evaluation dominates every possible later use of the
8688 values. Restricting to such types here would be safe since there
8689 is no possible forward declaration around, but would introduce an
8690 undesirable middle-end semantic to anonymity. We then defer to
8691 front-ends the responsibility of ensuring that the sizes are
8692 evaluated both early and late enough, e.g. by attaching artificial
8693 type declarations to the tree. */
8694 break;
8696 default:
8697 break;
8700 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8701 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8703 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8705 TYPE_SIZE (t) = TYPE_SIZE (type);
8706 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8707 TYPE_SIZES_GIMPLIFIED (t) = 1;
8711 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8712 a size or position, has had all of its SAVE_EXPRs evaluated.
8713 We add any required statements to *STMT_P. */
8715 void
8716 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8718 tree expr = *expr_p;
8720 /* We don't do anything if the value isn't there, is constant, or contains
8721 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8722 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8723 will want to replace it with a new variable, but that will cause problems
8724 if this type is from outside the function. It's OK to have that here. */
8725 if (is_gimple_sizepos (expr))
8726 return;
8728 *expr_p = unshare_expr (expr);
8730 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8733 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8734 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8735 is true, also gimplify the parameters. */
8737 gimple_bind
8738 gimplify_body (tree fndecl, bool do_parms)
8740 location_t saved_location = input_location;
8741 gimple_seq parm_stmts, seq;
8742 gimple outer_stmt;
8743 gimple_bind outer_bind;
8744 struct cgraph_node *cgn;
8746 timevar_push (TV_TREE_GIMPLIFY);
8748 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8749 gimplification. */
8750 default_rtl_profile ();
8752 gcc_assert (gimplify_ctxp == NULL);
8753 push_gimplify_context ();
8755 if (flag_openmp)
8757 gcc_assert (gimplify_omp_ctxp == NULL);
8758 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8759 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8762 /* Unshare most shared trees in the body and in that of any nested functions.
8763 It would seem we don't have to do this for nested functions because
8764 they are supposed to be output and then the outer function gimplified
8765 first, but the g++ front end doesn't always do it that way. */
8766 unshare_body (fndecl);
8767 unvisit_body (fndecl);
8769 cgn = cgraph_node::get (fndecl);
8770 if (cgn && cgn->origin)
8771 nonlocal_vlas = new hash_set<tree>;
8773 /* Make sure input_location isn't set to something weird. */
8774 input_location = DECL_SOURCE_LOCATION (fndecl);
8776 /* Resolve callee-copies. This has to be done before processing
8777 the body so that DECL_VALUE_EXPR gets processed correctly. */
8778 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8780 /* Gimplify the function's body. */
8781 seq = NULL;
8782 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8783 outer_stmt = gimple_seq_first_stmt (seq);
8784 if (!outer_stmt)
8786 outer_stmt = gimple_build_nop ();
8787 gimplify_seq_add_stmt (&seq, outer_stmt);
8790 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8791 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8792 if (gimple_code (outer_stmt) == GIMPLE_BIND
8793 && gimple_seq_first (seq) == gimple_seq_last (seq))
8794 outer_bind = as_a <gimple_bind> (outer_stmt);
8795 else
8796 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8798 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8800 /* If we had callee-copies statements, insert them at the beginning
8801 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8802 if (!gimple_seq_empty_p (parm_stmts))
8804 tree parm;
8806 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8807 gimple_bind_set_body (outer_bind, parm_stmts);
8809 for (parm = DECL_ARGUMENTS (current_function_decl);
8810 parm; parm = DECL_CHAIN (parm))
8811 if (DECL_HAS_VALUE_EXPR_P (parm))
8813 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8814 DECL_IGNORED_P (parm) = 0;
8818 if (nonlocal_vlas)
8820 if (nonlocal_vla_vars)
8822 /* tree-nested.c may later on call declare_vars (..., true);
8823 which relies on BLOCK_VARS chain to be the tail of the
8824 gimple_bind_vars chain. Ensure we don't violate that
8825 assumption. */
8826 if (gimple_bind_block (outer_bind)
8827 == DECL_INITIAL (current_function_decl))
8828 declare_vars (nonlocal_vla_vars, outer_bind, true);
8829 else
8830 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8831 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8832 nonlocal_vla_vars);
8833 nonlocal_vla_vars = NULL_TREE;
8835 delete nonlocal_vlas;
8836 nonlocal_vlas = NULL;
8839 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8841 delete_omp_context (gimplify_omp_ctxp);
8842 gimplify_omp_ctxp = NULL;
8845 pop_gimplify_context (outer_bind);
8846 gcc_assert (gimplify_ctxp == NULL);
8848 #ifdef ENABLE_CHECKING
8849 if (!seen_error ())
8850 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8851 #endif
8853 timevar_pop (TV_TREE_GIMPLIFY);
8854 input_location = saved_location;
8856 return outer_bind;
8859 typedef char *char_p; /* For DEF_VEC_P. */
8861 /* Return whether we should exclude FNDECL from instrumentation. */
8863 static bool
8864 flag_instrument_functions_exclude_p (tree fndecl)
8866 vec<char_p> *v;
8868 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8869 if (v && v->length () > 0)
8871 const char *name;
8872 int i;
8873 char *s;
8875 name = lang_hooks.decl_printable_name (fndecl, 0);
8876 FOR_EACH_VEC_ELT (*v, i, s)
8877 if (strstr (name, s) != NULL)
8878 return true;
8881 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8882 if (v && v->length () > 0)
8884 const char *name;
8885 int i;
8886 char *s;
8888 name = DECL_SOURCE_FILE (fndecl);
8889 FOR_EACH_VEC_ELT (*v, i, s)
8890 if (strstr (name, s) != NULL)
8891 return true;
8894 return false;
8897 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8898 node for the function we want to gimplify.
8900 Return the sequence of GIMPLE statements corresponding to the body
8901 of FNDECL. */
8903 void
8904 gimplify_function_tree (tree fndecl)
8906 tree parm, ret;
8907 gimple_seq seq;
8908 gimple_bind bind;
8910 gcc_assert (!gimple_body (fndecl));
8912 if (DECL_STRUCT_FUNCTION (fndecl))
8913 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8914 else
8915 push_struct_function (fndecl);
8917 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8919 /* Preliminarily mark non-addressed complex variables as eligible
8920 for promotion to gimple registers. We'll transform their uses
8921 as we find them. */
8922 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8923 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8924 && !TREE_THIS_VOLATILE (parm)
8925 && !needs_to_live_in_memory (parm))
8926 DECL_GIMPLE_REG_P (parm) = 1;
8929 ret = DECL_RESULT (fndecl);
8930 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8931 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8932 && !needs_to_live_in_memory (ret))
8933 DECL_GIMPLE_REG_P (ret) = 1;
8935 bind = gimplify_body (fndecl, true);
8937 /* The tree body of the function is no longer needed, replace it
8938 with the new GIMPLE body. */
8939 seq = NULL;
8940 gimple_seq_add_stmt (&seq, bind);
8941 gimple_set_body (fndecl, seq);
8943 /* If we're instrumenting function entry/exit, then prepend the call to
8944 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8945 catch the exit hook. */
8946 /* ??? Add some way to ignore exceptions for this TFE. */
8947 if (flag_instrument_function_entry_exit
8948 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8949 && !flag_instrument_functions_exclude_p (fndecl))
8951 tree x;
8952 gimple_bind new_bind;
8953 gimple tf;
8954 gimple_seq cleanup = NULL, body = NULL;
8955 tree tmp_var;
8956 gimple_call call;
8958 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8959 call = gimple_build_call (x, 1, integer_zero_node);
8960 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8961 gimple_call_set_lhs (call, tmp_var);
8962 gimplify_seq_add_stmt (&cleanup, call);
8963 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8964 call = gimple_build_call (x, 2,
8965 build_fold_addr_expr (current_function_decl),
8966 tmp_var);
8967 gimplify_seq_add_stmt (&cleanup, call);
8968 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8970 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8971 call = gimple_build_call (x, 1, integer_zero_node);
8972 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8973 gimple_call_set_lhs (call, tmp_var);
8974 gimplify_seq_add_stmt (&body, call);
8975 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8976 call = gimple_build_call (x, 2,
8977 build_fold_addr_expr (current_function_decl),
8978 tmp_var);
8979 gimplify_seq_add_stmt (&body, call);
8980 gimplify_seq_add_stmt (&body, tf);
8981 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8982 /* Clear the block for BIND, since it is no longer directly inside
8983 the function, but within a try block. */
8984 gimple_bind_set_block (bind, NULL);
8986 /* Replace the current function body with the body
8987 wrapped in the try/finally TF. */
8988 seq = NULL;
8989 gimple_seq_add_stmt (&seq, new_bind);
8990 gimple_set_body (fndecl, seq);
8993 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8994 cfun->curr_properties = PROP_gimple_any;
8996 pop_cfun ();
8999 /* Return a dummy expression of type TYPE in order to keep going after an
9000 error. */
9002 static tree
9003 dummy_object (tree type)
9005 tree t = build_int_cst (build_pointer_type (type), 0);
9006 return build2 (MEM_REF, type, t, t);
9009 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9010 builtin function, but a very special sort of operator. */
9012 enum gimplify_status
9013 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9015 tree promoted_type, have_va_type;
9016 tree valist = TREE_OPERAND (*expr_p, 0);
9017 tree type = TREE_TYPE (*expr_p);
9018 tree t;
9019 location_t loc = EXPR_LOCATION (*expr_p);
9021 /* Verify that valist is of the proper type. */
9022 have_va_type = TREE_TYPE (valist);
9023 if (have_va_type == error_mark_node)
9024 return GS_ERROR;
9025 have_va_type = targetm.canonical_va_list_type (have_va_type);
9027 if (have_va_type == NULL_TREE)
9029 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9030 return GS_ERROR;
9033 /* Generate a diagnostic for requesting data of a type that cannot
9034 be passed through `...' due to type promotion at the call site. */
9035 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9036 != type)
9038 static bool gave_help;
9039 bool warned;
9041 /* Unfortunately, this is merely undefined, rather than a constraint
9042 violation, so we cannot make this an error. If this call is never
9043 executed, the program is still strictly conforming. */
9044 warned = warning_at (loc, 0,
9045 "%qT is promoted to %qT when passed through %<...%>",
9046 type, promoted_type);
9047 if (!gave_help && warned)
9049 gave_help = true;
9050 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9051 promoted_type, type);
9054 /* We can, however, treat "undefined" any way we please.
9055 Call abort to encourage the user to fix the program. */
9056 if (warned)
9057 inform (loc, "if this code is reached, the program will abort");
9058 /* Before the abort, allow the evaluation of the va_list
9059 expression to exit or longjmp. */
9060 gimplify_and_add (valist, pre_p);
9061 t = build_call_expr_loc (loc,
9062 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9063 gimplify_and_add (t, pre_p);
9065 /* This is dead code, but go ahead and finish so that the
9066 mode of the result comes out right. */
9067 *expr_p = dummy_object (type);
9068 return GS_ALL_DONE;
9070 else
9072 /* Make it easier for the backends by protecting the valist argument
9073 from multiple evaluations. */
9074 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9076 /* For this case, the backends will be expecting a pointer to
9077 TREE_TYPE (abi), but it's possible we've
9078 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9079 So fix it. */
9080 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9082 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9083 valist = fold_convert_loc (loc, p1,
9084 build_fold_addr_expr_loc (loc, valist));
9087 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9089 else
9090 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9092 if (!targetm.gimplify_va_arg_expr)
9093 /* FIXME: Once most targets are converted we should merely
9094 assert this is non-null. */
9095 return GS_ALL_DONE;
9097 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9098 return GS_OK;
9102 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9104 DST/SRC are the destination and source respectively. You can pass
9105 ungimplified trees in DST or SRC, in which case they will be
9106 converted to a gimple operand if necessary.
9108 This function returns the newly created GIMPLE_ASSIGN tuple. */
9110 gimple
9111 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9113 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9114 gimplify_and_add (t, seq_p);
9115 ggc_free (t);
9116 return gimple_seq_last_stmt (*seq_p);
9119 inline hashval_t
9120 gimplify_hasher::hash (const value_type *p)
9122 tree t = p->val;
9123 return iterative_hash_expr (t, 0);
9126 inline bool
9127 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9129 tree t1 = p1->val;
9130 tree t2 = p2->val;
9131 enum tree_code code = TREE_CODE (t1);
9133 if (TREE_CODE (t2) != code
9134 || TREE_TYPE (t1) != TREE_TYPE (t2))
9135 return false;
9137 if (!operand_equal_p (t1, t2, 0))
9138 return false;
9140 #ifdef ENABLE_CHECKING
9141 /* Only allow them to compare equal if they also hash equal; otherwise
9142 results are nondeterminate, and we fail bootstrap comparison. */
9143 gcc_assert (hash (p1) == hash (p2));
9144 #endif
9146 return true;