Concretize gimple_cond_set_code
[official-gcc.git] / gcc / gimplify.c
blobb49aab78b0d8179d7692eda68da5e4406f535875
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 if (fndecl
4628 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4629 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4630 && call_expr_nargs (*from_p) == 3)
4631 assign = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4632 CALL_EXPR_ARG (*from_p, 0),
4633 CALL_EXPR_ARG (*from_p, 1),
4634 CALL_EXPR_ARG (*from_p, 2));
4635 else
4637 assign = gimple_build_call_from_tree (*from_p);
4638 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4640 notice_special_calls (as_a <gimple_call> (assign));
4641 if (!gimple_call_noreturn_p (assign))
4642 gimple_call_set_lhs (assign, *to_p);
4644 else
4646 assign = gimple_build_assign (*to_p, *from_p);
4647 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4650 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4652 /* We should have got an SSA name from the start. */
4653 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4656 gimplify_seq_add_stmt (pre_p, assign);
4657 gsi = gsi_last (*pre_p);
4658 maybe_fold_stmt (&gsi);
4660 if (want_value)
4662 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4663 return GS_OK;
4665 else
4666 *expr_p = NULL;
4668 return GS_ALL_DONE;
4671 /* Gimplify a comparison between two variable-sized objects. Do this
4672 with a call to BUILT_IN_MEMCMP. */
4674 static enum gimplify_status
4675 gimplify_variable_sized_compare (tree *expr_p)
4677 location_t loc = EXPR_LOCATION (*expr_p);
4678 tree op0 = TREE_OPERAND (*expr_p, 0);
4679 tree op1 = TREE_OPERAND (*expr_p, 1);
4680 tree t, arg, dest, src, expr;
4682 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4683 arg = unshare_expr (arg);
4684 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4685 src = build_fold_addr_expr_loc (loc, op1);
4686 dest = build_fold_addr_expr_loc (loc, op0);
4687 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4688 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4690 expr
4691 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4692 SET_EXPR_LOCATION (expr, loc);
4693 *expr_p = expr;
4695 return GS_OK;
4698 /* Gimplify a comparison between two aggregate objects of integral scalar
4699 mode as a comparison between the bitwise equivalent scalar values. */
4701 static enum gimplify_status
4702 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4704 location_t loc = EXPR_LOCATION (*expr_p);
4705 tree op0 = TREE_OPERAND (*expr_p, 0);
4706 tree op1 = TREE_OPERAND (*expr_p, 1);
4708 tree type = TREE_TYPE (op0);
4709 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4711 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4712 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4714 *expr_p
4715 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4717 return GS_OK;
4720 /* Gimplify an expression sequence. This function gimplifies each
4721 expression and rewrites the original expression with the last
4722 expression of the sequence in GIMPLE form.
4724 PRE_P points to the list where the side effects for all the
4725 expressions in the sequence will be emitted.
4727 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4729 static enum gimplify_status
4730 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4732 tree t = *expr_p;
4736 tree *sub_p = &TREE_OPERAND (t, 0);
4738 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4739 gimplify_compound_expr (sub_p, pre_p, false);
4740 else
4741 gimplify_stmt (sub_p, pre_p);
4743 t = TREE_OPERAND (t, 1);
4745 while (TREE_CODE (t) == COMPOUND_EXPR);
4747 *expr_p = t;
4748 if (want_value)
4749 return GS_OK;
4750 else
4752 gimplify_stmt (expr_p, pre_p);
4753 return GS_ALL_DONE;
4757 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4758 gimplify. After gimplification, EXPR_P will point to a new temporary
4759 that holds the original value of the SAVE_EXPR node.
4761 PRE_P points to the list where side effects that must happen before
4762 *EXPR_P should be stored. */
4764 static enum gimplify_status
4765 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4767 enum gimplify_status ret = GS_ALL_DONE;
4768 tree val;
4770 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4771 val = TREE_OPERAND (*expr_p, 0);
4773 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4774 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4776 /* The operand may be a void-valued expression such as SAVE_EXPRs
4777 generated by the Java frontend for class initialization. It is
4778 being executed only for its side-effects. */
4779 if (TREE_TYPE (val) == void_type_node)
4781 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4782 is_gimple_stmt, fb_none);
4783 val = NULL;
4785 else
4786 val = get_initialized_tmp_var (val, pre_p, post_p);
4788 TREE_OPERAND (*expr_p, 0) = val;
4789 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4792 *expr_p = val;
4794 return ret;
4797 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4799 unary_expr
4800 : ...
4801 | '&' varname
4804 PRE_P points to the list where side effects that must happen before
4805 *EXPR_P should be stored.
4807 POST_P points to the list where side effects that must happen after
4808 *EXPR_P should be stored. */
4810 static enum gimplify_status
4811 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4813 tree expr = *expr_p;
4814 tree op0 = TREE_OPERAND (expr, 0);
4815 enum gimplify_status ret;
4816 location_t loc = EXPR_LOCATION (*expr_p);
4818 switch (TREE_CODE (op0))
4820 case INDIRECT_REF:
4821 do_indirect_ref:
4822 /* Check if we are dealing with an expression of the form '&*ptr'.
4823 While the front end folds away '&*ptr' into 'ptr', these
4824 expressions may be generated internally by the compiler (e.g.,
4825 builtins like __builtin_va_end). */
4826 /* Caution: the silent array decomposition semantics we allow for
4827 ADDR_EXPR means we can't always discard the pair. */
4828 /* Gimplification of the ADDR_EXPR operand may drop
4829 cv-qualification conversions, so make sure we add them if
4830 needed. */
4832 tree op00 = TREE_OPERAND (op0, 0);
4833 tree t_expr = TREE_TYPE (expr);
4834 tree t_op00 = TREE_TYPE (op00);
4836 if (!useless_type_conversion_p (t_expr, t_op00))
4837 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4838 *expr_p = op00;
4839 ret = GS_OK;
4841 break;
4843 case VIEW_CONVERT_EXPR:
4844 /* Take the address of our operand and then convert it to the type of
4845 this ADDR_EXPR.
4847 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4848 all clear. The impact of this transformation is even less clear. */
4850 /* If the operand is a useless conversion, look through it. Doing so
4851 guarantees that the ADDR_EXPR and its operand will remain of the
4852 same type. */
4853 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4854 op0 = TREE_OPERAND (op0, 0);
4856 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4857 build_fold_addr_expr_loc (loc,
4858 TREE_OPERAND (op0, 0)));
4859 ret = GS_OK;
4860 break;
4862 default:
4863 /* We use fb_either here because the C frontend sometimes takes
4864 the address of a call that returns a struct; see
4865 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4866 the implied temporary explicit. */
4868 /* Make the operand addressable. */
4869 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4870 is_gimple_addressable, fb_either);
4871 if (ret == GS_ERROR)
4872 break;
4874 /* Then mark it. Beware that it may not be possible to do so directly
4875 if a temporary has been created by the gimplification. */
4876 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4878 op0 = TREE_OPERAND (expr, 0);
4880 /* For various reasons, the gimplification of the expression
4881 may have made a new INDIRECT_REF. */
4882 if (TREE_CODE (op0) == INDIRECT_REF)
4883 goto do_indirect_ref;
4885 mark_addressable (TREE_OPERAND (expr, 0));
4887 /* The FEs may end up building ADDR_EXPRs early on a decl with
4888 an incomplete type. Re-build ADDR_EXPRs in canonical form
4889 here. */
4890 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4891 *expr_p = build_fold_addr_expr (op0);
4893 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4894 recompute_tree_invariant_for_addr_expr (*expr_p);
4896 /* If we re-built the ADDR_EXPR add a conversion to the original type
4897 if required. */
4898 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4899 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4901 break;
4904 return ret;
4907 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4908 value; output operands should be a gimple lvalue. */
4910 static enum gimplify_status
4911 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4913 tree expr;
4914 int noutputs;
4915 const char **oconstraints;
4916 int i;
4917 tree link;
4918 const char *constraint;
4919 bool allows_mem, allows_reg, is_inout;
4920 enum gimplify_status ret, tret;
4921 gimple_asm stmt;
4922 vec<tree, va_gc> *inputs;
4923 vec<tree, va_gc> *outputs;
4924 vec<tree, va_gc> *clobbers;
4925 vec<tree, va_gc> *labels;
4926 tree link_next;
4928 expr = *expr_p;
4929 noutputs = list_length (ASM_OUTPUTS (expr));
4930 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4932 inputs = NULL;
4933 outputs = NULL;
4934 clobbers = NULL;
4935 labels = NULL;
4937 ret = GS_ALL_DONE;
4938 link_next = NULL_TREE;
4939 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4941 bool ok;
4942 size_t constraint_len;
4944 link_next = TREE_CHAIN (link);
4946 oconstraints[i]
4947 = constraint
4948 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4949 constraint_len = strlen (constraint);
4950 if (constraint_len == 0)
4951 continue;
4953 ok = parse_output_constraint (&constraint, i, 0, 0,
4954 &allows_mem, &allows_reg, &is_inout);
4955 if (!ok)
4957 ret = GS_ERROR;
4958 is_inout = false;
4961 if (!allows_reg && allows_mem)
4962 mark_addressable (TREE_VALUE (link));
4964 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4965 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4966 fb_lvalue | fb_mayfail);
4967 if (tret == GS_ERROR)
4969 error ("invalid lvalue in asm output %d", i);
4970 ret = tret;
4973 vec_safe_push (outputs, link);
4974 TREE_CHAIN (link) = NULL_TREE;
4976 if (is_inout)
4978 /* An input/output operand. To give the optimizers more
4979 flexibility, split it into separate input and output
4980 operands. */
4981 tree input;
4982 char buf[10];
4984 /* Turn the in/out constraint into an output constraint. */
4985 char *p = xstrdup (constraint);
4986 p[0] = '=';
4987 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4989 /* And add a matching input constraint. */
4990 if (allows_reg)
4992 sprintf (buf, "%d", i);
4994 /* If there are multiple alternatives in the constraint,
4995 handle each of them individually. Those that allow register
4996 will be replaced with operand number, the others will stay
4997 unchanged. */
4998 if (strchr (p, ',') != NULL)
5000 size_t len = 0, buflen = strlen (buf);
5001 char *beg, *end, *str, *dst;
5003 for (beg = p + 1;;)
5005 end = strchr (beg, ',');
5006 if (end == NULL)
5007 end = strchr (beg, '\0');
5008 if ((size_t) (end - beg) < buflen)
5009 len += buflen + 1;
5010 else
5011 len += end - beg + 1;
5012 if (*end)
5013 beg = end + 1;
5014 else
5015 break;
5018 str = (char *) alloca (len);
5019 for (beg = p + 1, dst = str;;)
5021 const char *tem;
5022 bool mem_p, reg_p, inout_p;
5024 end = strchr (beg, ',');
5025 if (end)
5026 *end = '\0';
5027 beg[-1] = '=';
5028 tem = beg - 1;
5029 parse_output_constraint (&tem, i, 0, 0,
5030 &mem_p, &reg_p, &inout_p);
5031 if (dst != str)
5032 *dst++ = ',';
5033 if (reg_p)
5035 memcpy (dst, buf, buflen);
5036 dst += buflen;
5038 else
5040 if (end)
5041 len = end - beg;
5042 else
5043 len = strlen (beg);
5044 memcpy (dst, beg, len);
5045 dst += len;
5047 if (end)
5048 beg = end + 1;
5049 else
5050 break;
5052 *dst = '\0';
5053 input = build_string (dst - str, str);
5055 else
5056 input = build_string (strlen (buf), buf);
5058 else
5059 input = build_string (constraint_len - 1, constraint + 1);
5061 free (p);
5063 input = build_tree_list (build_tree_list (NULL_TREE, input),
5064 unshare_expr (TREE_VALUE (link)));
5065 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5069 link_next = NULL_TREE;
5070 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5072 link_next = TREE_CHAIN (link);
5073 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5074 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5075 oconstraints, &allows_mem, &allows_reg);
5077 /* If we can't make copies, we can only accept memory. */
5078 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5080 if (allows_mem)
5081 allows_reg = 0;
5082 else
5084 error ("impossible constraint in %<asm%>");
5085 error ("non-memory input %d must stay in memory", i);
5086 return GS_ERROR;
5090 /* If the operand is a memory input, it should be an lvalue. */
5091 if (!allows_reg && allows_mem)
5093 tree inputv = TREE_VALUE (link);
5094 STRIP_NOPS (inputv);
5095 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5096 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5097 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5098 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5099 TREE_VALUE (link) = error_mark_node;
5100 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5101 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5102 mark_addressable (TREE_VALUE (link));
5103 if (tret == GS_ERROR)
5105 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5106 input_location = EXPR_LOCATION (TREE_VALUE (link));
5107 error ("memory input %d is not directly addressable", i);
5108 ret = tret;
5111 else
5113 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5114 is_gimple_asm_val, fb_rvalue);
5115 if (tret == GS_ERROR)
5116 ret = tret;
5119 TREE_CHAIN (link) = NULL_TREE;
5120 vec_safe_push (inputs, link);
5123 link_next = NULL_TREE;
5124 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5126 link_next = TREE_CHAIN (link);
5127 TREE_CHAIN (link) = NULL_TREE;
5128 vec_safe_push (clobbers, link);
5131 link_next = NULL_TREE;
5132 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5134 link_next = TREE_CHAIN (link);
5135 TREE_CHAIN (link) = NULL_TREE;
5136 vec_safe_push (labels, link);
5139 /* Do not add ASMs with errors to the gimple IL stream. */
5140 if (ret != GS_ERROR)
5142 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5143 inputs, outputs, clobbers, labels);
5145 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5146 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5148 gimplify_seq_add_stmt (pre_p, stmt);
5151 return ret;
5154 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5155 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5156 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5157 return to this function.
5159 FIXME should we complexify the prequeue handling instead? Or use flags
5160 for all the cleanups and let the optimizer tighten them up? The current
5161 code seems pretty fragile; it will break on a cleanup within any
5162 non-conditional nesting. But any such nesting would be broken, anyway;
5163 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5164 and continues out of it. We can do that at the RTL level, though, so
5165 having an optimizer to tighten up try/finally regions would be a Good
5166 Thing. */
5168 static enum gimplify_status
5169 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5171 gimple_stmt_iterator iter;
5172 gimple_seq body_sequence = NULL;
5174 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5176 /* We only care about the number of conditions between the innermost
5177 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5178 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5179 int old_conds = gimplify_ctxp->conditions;
5180 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5181 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5182 gimplify_ctxp->conditions = 0;
5183 gimplify_ctxp->conditional_cleanups = NULL;
5184 gimplify_ctxp->in_cleanup_point_expr = true;
5186 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5188 gimplify_ctxp->conditions = old_conds;
5189 gimplify_ctxp->conditional_cleanups = old_cleanups;
5190 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5192 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5194 gimple wce = gsi_stmt (iter);
5196 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5198 if (gsi_one_before_end_p (iter))
5200 /* Note that gsi_insert_seq_before and gsi_remove do not
5201 scan operands, unlike some other sequence mutators. */
5202 if (!gimple_wce_cleanup_eh_only (wce))
5203 gsi_insert_seq_before_without_update (&iter,
5204 gimple_wce_cleanup (wce),
5205 GSI_SAME_STMT);
5206 gsi_remove (&iter, true);
5207 break;
5209 else
5211 gimple_try gtry;
5212 gimple_seq seq;
5213 enum gimple_try_flags kind;
5215 if (gimple_wce_cleanup_eh_only (wce))
5216 kind = GIMPLE_TRY_CATCH;
5217 else
5218 kind = GIMPLE_TRY_FINALLY;
5219 seq = gsi_split_seq_after (iter);
5221 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5222 /* Do not use gsi_replace here, as it may scan operands.
5223 We want to do a simple structural modification only. */
5224 gsi_set_stmt (&iter, gtry);
5225 iter = gsi_start (gtry->eval);
5228 else
5229 gsi_next (&iter);
5232 gimplify_seq_add_seq (pre_p, body_sequence);
5233 if (temp)
5235 *expr_p = temp;
5236 return GS_OK;
5238 else
5240 *expr_p = NULL;
5241 return GS_ALL_DONE;
5245 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5246 is the cleanup action required. EH_ONLY is true if the cleanup should
5247 only be executed if an exception is thrown, not on normal exit. */
5249 static void
5250 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5252 gimple wce;
5253 gimple_seq cleanup_stmts = NULL;
5255 /* Errors can result in improperly nested cleanups. Which results in
5256 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5257 if (seen_error ())
5258 return;
5260 if (gimple_conditional_context ())
5262 /* If we're in a conditional context, this is more complex. We only
5263 want to run the cleanup if we actually ran the initialization that
5264 necessitates it, but we want to run it after the end of the
5265 conditional context. So we wrap the try/finally around the
5266 condition and use a flag to determine whether or not to actually
5267 run the destructor. Thus
5269 test ? f(A()) : 0
5271 becomes (approximately)
5273 flag = 0;
5274 try {
5275 if (test) { A::A(temp); flag = 1; val = f(temp); }
5276 else { val = 0; }
5277 } finally {
5278 if (flag) A::~A(temp);
5282 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5283 gimple_assign ffalse = gimple_build_assign (flag, boolean_false_node);
5284 gimple_assign ftrue = gimple_build_assign (flag, boolean_true_node);
5286 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5287 gimplify_stmt (&cleanup, &cleanup_stmts);
5288 wce = gimple_build_wce (cleanup_stmts);
5290 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5291 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5292 gimplify_seq_add_stmt (pre_p, ftrue);
5294 /* Because of this manipulation, and the EH edges that jump
5295 threading cannot redirect, the temporary (VAR) will appear
5296 to be used uninitialized. Don't warn. */
5297 TREE_NO_WARNING (var) = 1;
5299 else
5301 gimplify_stmt (&cleanup, &cleanup_stmts);
5302 wce = gimple_build_wce (cleanup_stmts);
5303 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5304 gimplify_seq_add_stmt (pre_p, wce);
5308 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5310 static enum gimplify_status
5311 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5313 tree targ = *expr_p;
5314 tree temp = TARGET_EXPR_SLOT (targ);
5315 tree init = TARGET_EXPR_INITIAL (targ);
5316 enum gimplify_status ret;
5318 if (init)
5320 tree cleanup = NULL_TREE;
5322 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5323 to the temps list. Handle also variable length TARGET_EXPRs. */
5324 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5326 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5327 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5328 gimplify_vla_decl (temp, pre_p);
5330 else
5331 gimple_add_tmp_var (temp);
5333 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5334 expression is supposed to initialize the slot. */
5335 if (VOID_TYPE_P (TREE_TYPE (init)))
5336 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5337 else
5339 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5340 init = init_expr;
5341 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5342 init = NULL;
5343 ggc_free (init_expr);
5345 if (ret == GS_ERROR)
5347 /* PR c++/28266 Make sure this is expanded only once. */
5348 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5349 return GS_ERROR;
5351 if (init)
5352 gimplify_and_add (init, pre_p);
5354 /* If needed, push the cleanup for the temp. */
5355 if (TARGET_EXPR_CLEANUP (targ))
5357 if (CLEANUP_EH_ONLY (targ))
5358 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5359 CLEANUP_EH_ONLY (targ), pre_p);
5360 else
5361 cleanup = TARGET_EXPR_CLEANUP (targ);
5364 /* Add a clobber for the temporary going out of scope, like
5365 gimplify_bind_expr. */
5366 if (gimplify_ctxp->in_cleanup_point_expr
5367 && needs_to_live_in_memory (temp)
5368 && flag_stack_reuse == SR_ALL)
5370 tree clobber = build_constructor (TREE_TYPE (temp),
5371 NULL);
5372 TREE_THIS_VOLATILE (clobber) = true;
5373 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5374 if (cleanup)
5375 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5376 clobber);
5377 else
5378 cleanup = clobber;
5381 if (cleanup)
5382 gimple_push_cleanup (temp, cleanup, false, pre_p);
5384 /* Only expand this once. */
5385 TREE_OPERAND (targ, 3) = init;
5386 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5388 else
5389 /* We should have expanded this before. */
5390 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5392 *expr_p = temp;
5393 return GS_OK;
5396 /* Gimplification of expression trees. */
5398 /* Gimplify an expression which appears at statement context. The
5399 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5400 NULL, a new sequence is allocated.
5402 Return true if we actually added a statement to the queue. */
5404 bool
5405 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5407 gimple_seq_node last;
5409 last = gimple_seq_last (*seq_p);
5410 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5411 return last != gimple_seq_last (*seq_p);
5414 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5415 to CTX. If entries already exist, force them to be some flavor of private.
5416 If there is no enclosing parallel, do nothing. */
5418 void
5419 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5421 splay_tree_node n;
5423 if (decl == NULL || !DECL_P (decl))
5424 return;
5428 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5429 if (n != NULL)
5431 if (n->value & GOVD_SHARED)
5432 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5433 else if (n->value & GOVD_MAP)
5434 n->value |= GOVD_MAP_TO_ONLY;
5435 else
5436 return;
5438 else if (ctx->region_type == ORT_TARGET)
5439 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5440 else if (ctx->region_type != ORT_WORKSHARE
5441 && ctx->region_type != ORT_SIMD
5442 && ctx->region_type != ORT_TARGET_DATA)
5443 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5445 ctx = ctx->outer_context;
5447 while (ctx);
5450 /* Similarly for each of the type sizes of TYPE. */
5452 static void
5453 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5455 if (type == NULL || type == error_mark_node)
5456 return;
5457 type = TYPE_MAIN_VARIANT (type);
5459 if (ctx->privatized_types->add (type))
5460 return;
5462 switch (TREE_CODE (type))
5464 case INTEGER_TYPE:
5465 case ENUMERAL_TYPE:
5466 case BOOLEAN_TYPE:
5467 case REAL_TYPE:
5468 case FIXED_POINT_TYPE:
5469 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5470 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5471 break;
5473 case ARRAY_TYPE:
5474 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5475 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5476 break;
5478 case RECORD_TYPE:
5479 case UNION_TYPE:
5480 case QUAL_UNION_TYPE:
5482 tree field;
5483 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5484 if (TREE_CODE (field) == FIELD_DECL)
5486 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5487 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5490 break;
5492 case POINTER_TYPE:
5493 case REFERENCE_TYPE:
5494 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5495 break;
5497 default:
5498 break;
5501 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5502 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5503 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5506 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5508 static void
5509 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5511 splay_tree_node n;
5512 unsigned int nflags;
5513 tree t;
5515 if (error_operand_p (decl))
5516 return;
5518 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5519 there are constructors involved somewhere. */
5520 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5521 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5522 flags |= GOVD_SEEN;
5524 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5525 if (n != NULL && n->value != GOVD_ALIGNED)
5527 /* We shouldn't be re-adding the decl with the same data
5528 sharing class. */
5529 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5530 /* The only combination of data sharing classes we should see is
5531 FIRSTPRIVATE and LASTPRIVATE. */
5532 nflags = n->value | flags;
5533 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5534 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5535 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5536 n->value = nflags;
5537 return;
5540 /* When adding a variable-sized variable, we have to handle all sorts
5541 of additional bits of data: the pointer replacement variable, and
5542 the parameters of the type. */
5543 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5545 /* Add the pointer replacement variable as PRIVATE if the variable
5546 replacement is private, else FIRSTPRIVATE since we'll need the
5547 address of the original variable either for SHARED, or for the
5548 copy into or out of the context. */
5549 if (!(flags & GOVD_LOCAL))
5551 nflags = flags & GOVD_MAP
5552 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5553 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5554 nflags |= flags & GOVD_SEEN;
5555 t = DECL_VALUE_EXPR (decl);
5556 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5557 t = TREE_OPERAND (t, 0);
5558 gcc_assert (DECL_P (t));
5559 omp_add_variable (ctx, t, nflags);
5562 /* Add all of the variable and type parameters (which should have
5563 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5564 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5565 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5566 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5568 /* The variable-sized variable itself is never SHARED, only some form
5569 of PRIVATE. The sharing would take place via the pointer variable
5570 which we remapped above. */
5571 if (flags & GOVD_SHARED)
5572 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5573 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5575 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5576 alloca statement we generate for the variable, so make sure it
5577 is available. This isn't automatically needed for the SHARED
5578 case, since we won't be allocating local storage then.
5579 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5580 in this case omp_notice_variable will be called later
5581 on when it is gimplified. */
5582 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5583 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5584 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5586 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5587 && lang_hooks.decls.omp_privatize_by_reference (decl))
5589 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5591 /* Similar to the direct variable sized case above, we'll need the
5592 size of references being privatized. */
5593 if ((flags & GOVD_SHARED) == 0)
5595 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5596 if (TREE_CODE (t) != INTEGER_CST)
5597 omp_notice_variable (ctx, t, true);
5601 if (n != NULL)
5602 n->value |= flags;
5603 else
5604 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5607 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5608 This just prints out diagnostics about threadprivate variable uses
5609 in untied tasks. If DECL2 is non-NULL, prevent this warning
5610 on that variable. */
5612 static bool
5613 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5614 tree decl2)
5616 splay_tree_node n;
5617 struct gimplify_omp_ctx *octx;
5619 for (octx = ctx; octx; octx = octx->outer_context)
5620 if (octx->region_type == ORT_TARGET)
5622 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5623 if (n == NULL)
5625 error ("threadprivate variable %qE used in target region",
5626 DECL_NAME (decl));
5627 error_at (octx->location, "enclosing target region");
5628 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5630 if (decl2)
5631 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5634 if (ctx->region_type != ORT_UNTIED_TASK)
5635 return false;
5636 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5637 if (n == NULL)
5639 error ("threadprivate variable %qE used in untied task",
5640 DECL_NAME (decl));
5641 error_at (ctx->location, "enclosing task");
5642 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5644 if (decl2)
5645 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5646 return false;
5649 /* Record the fact that DECL was used within the OpenMP context CTX.
5650 IN_CODE is true when real code uses DECL, and false when we should
5651 merely emit default(none) errors. Return true if DECL is going to
5652 be remapped and thus DECL shouldn't be gimplified into its
5653 DECL_VALUE_EXPR (if any). */
5655 static bool
5656 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5658 splay_tree_node n;
5659 unsigned flags = in_code ? GOVD_SEEN : 0;
5660 bool ret = false, shared;
5662 if (error_operand_p (decl))
5663 return false;
5665 /* Threadprivate variables are predetermined. */
5666 if (is_global_var (decl))
5668 if (DECL_THREAD_LOCAL_P (decl))
5669 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5671 if (DECL_HAS_VALUE_EXPR_P (decl))
5673 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5675 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5676 return omp_notice_threadprivate_variable (ctx, decl, value);
5680 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5681 if (ctx->region_type == ORT_TARGET)
5683 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5684 if (n == NULL)
5686 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5688 error ("%qD referenced in target region does not have "
5689 "a mappable type", decl);
5690 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5692 else
5693 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5695 else
5697 /* If nothing changed, there's nothing left to do. */
5698 if ((n->value & flags) == flags)
5699 return ret;
5700 n->value |= flags;
5702 goto do_outer;
5705 if (n == NULL)
5707 enum omp_clause_default_kind default_kind, kind;
5708 struct gimplify_omp_ctx *octx;
5710 if (ctx->region_type == ORT_WORKSHARE
5711 || ctx->region_type == ORT_SIMD
5712 || ctx->region_type == ORT_TARGET_DATA)
5713 goto do_outer;
5715 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5716 remapped firstprivate instead of shared. To some extent this is
5717 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5718 default_kind = ctx->default_kind;
5719 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5720 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5721 default_kind = kind;
5723 switch (default_kind)
5725 case OMP_CLAUSE_DEFAULT_NONE:
5726 if ((ctx->region_type & ORT_PARALLEL) != 0)
5728 error ("%qE not specified in enclosing parallel",
5729 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5730 error_at (ctx->location, "enclosing parallel");
5732 else if ((ctx->region_type & ORT_TASK) != 0)
5734 error ("%qE not specified in enclosing task",
5735 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5736 error_at (ctx->location, "enclosing task");
5738 else if (ctx->region_type == ORT_TEAMS)
5740 error ("%qE not specified in enclosing teams construct",
5741 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5742 error_at (ctx->location, "enclosing teams construct");
5744 else
5745 gcc_unreachable ();
5746 /* FALLTHRU */
5747 case OMP_CLAUSE_DEFAULT_SHARED:
5748 flags |= GOVD_SHARED;
5749 break;
5750 case OMP_CLAUSE_DEFAULT_PRIVATE:
5751 flags |= GOVD_PRIVATE;
5752 break;
5753 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5754 flags |= GOVD_FIRSTPRIVATE;
5755 break;
5756 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5757 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5758 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5759 if (ctx->outer_context)
5760 omp_notice_variable (ctx->outer_context, decl, in_code);
5761 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5763 splay_tree_node n2;
5765 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5766 continue;
5767 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5768 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5770 flags |= GOVD_FIRSTPRIVATE;
5771 break;
5773 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5774 break;
5776 if (flags & GOVD_FIRSTPRIVATE)
5777 break;
5778 if (octx == NULL
5779 && (TREE_CODE (decl) == PARM_DECL
5780 || (!is_global_var (decl)
5781 && DECL_CONTEXT (decl) == current_function_decl)))
5783 flags |= GOVD_FIRSTPRIVATE;
5784 break;
5786 flags |= GOVD_SHARED;
5787 break;
5788 default:
5789 gcc_unreachable ();
5792 if ((flags & GOVD_PRIVATE)
5793 && lang_hooks.decls.omp_private_outer_ref (decl))
5794 flags |= GOVD_PRIVATE_OUTER_REF;
5796 omp_add_variable (ctx, decl, flags);
5798 shared = (flags & GOVD_SHARED) != 0;
5799 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5800 goto do_outer;
5803 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5804 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5805 && DECL_SIZE (decl)
5806 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5808 splay_tree_node n2;
5809 tree t = DECL_VALUE_EXPR (decl);
5810 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5811 t = TREE_OPERAND (t, 0);
5812 gcc_assert (DECL_P (t));
5813 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5814 n2->value |= GOVD_SEEN;
5817 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5818 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5820 /* If nothing changed, there's nothing left to do. */
5821 if ((n->value & flags) == flags)
5822 return ret;
5823 flags |= n->value;
5824 n->value = flags;
5826 do_outer:
5827 /* If the variable is private in the current context, then we don't
5828 need to propagate anything to an outer context. */
5829 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5830 return ret;
5831 if (ctx->outer_context
5832 && omp_notice_variable (ctx->outer_context, decl, in_code))
5833 return true;
5834 return ret;
5837 /* Verify that DECL is private within CTX. If there's specific information
5838 to the contrary in the innermost scope, generate an error. */
5840 static bool
5841 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5843 splay_tree_node n;
5845 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5846 if (n != NULL)
5848 if (n->value & GOVD_SHARED)
5850 if (ctx == gimplify_omp_ctxp)
5852 if (simd)
5853 error ("iteration variable %qE is predetermined linear",
5854 DECL_NAME (decl));
5855 else
5856 error ("iteration variable %qE should be private",
5857 DECL_NAME (decl));
5858 n->value = GOVD_PRIVATE;
5859 return true;
5861 else
5862 return false;
5864 else if ((n->value & GOVD_EXPLICIT) != 0
5865 && (ctx == gimplify_omp_ctxp
5866 || (ctx->region_type == ORT_COMBINED_PARALLEL
5867 && gimplify_omp_ctxp->outer_context == ctx)))
5869 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5870 error ("iteration variable %qE should not be firstprivate",
5871 DECL_NAME (decl));
5872 else if ((n->value & GOVD_REDUCTION) != 0)
5873 error ("iteration variable %qE should not be reduction",
5874 DECL_NAME (decl));
5875 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5876 error ("iteration variable %qE should not be lastprivate",
5877 DECL_NAME (decl));
5878 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5879 error ("iteration variable %qE should not be private",
5880 DECL_NAME (decl));
5881 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5882 error ("iteration variable %qE is predetermined linear",
5883 DECL_NAME (decl));
5885 return (ctx == gimplify_omp_ctxp
5886 || (ctx->region_type == ORT_COMBINED_PARALLEL
5887 && gimplify_omp_ctxp->outer_context == ctx));
5890 if (ctx->region_type != ORT_WORKSHARE
5891 && ctx->region_type != ORT_SIMD)
5892 return false;
5893 else if (ctx->outer_context)
5894 return omp_is_private (ctx->outer_context, decl, simd);
5895 return false;
5898 /* Return true if DECL is private within a parallel region
5899 that binds to the current construct's context or in parallel
5900 region's REDUCTION clause. */
5902 static bool
5903 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5905 splay_tree_node n;
5909 ctx = ctx->outer_context;
5910 if (ctx == NULL)
5911 return !(is_global_var (decl)
5912 /* References might be private, but might be shared too,
5913 when checking for copyprivate, assume they might be
5914 private, otherwise assume they might be shared. */
5915 || (!copyprivate
5916 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5918 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5919 continue;
5921 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5922 if (n != NULL)
5923 return (n->value & GOVD_SHARED) == 0;
5925 while (ctx->region_type == ORT_WORKSHARE
5926 || ctx->region_type == ORT_SIMD);
5927 return false;
5930 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5931 and previous omp contexts. */
5933 static void
5934 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5935 enum omp_region_type region_type)
5937 struct gimplify_omp_ctx *ctx, *outer_ctx;
5938 tree c;
5940 ctx = new_omp_context (region_type);
5941 outer_ctx = ctx->outer_context;
5943 while ((c = *list_p) != NULL)
5945 bool remove = false;
5946 bool notice_outer = true;
5947 const char *check_non_private = NULL;
5948 unsigned int flags;
5949 tree decl;
5951 switch (OMP_CLAUSE_CODE (c))
5953 case OMP_CLAUSE_PRIVATE:
5954 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5955 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5957 flags |= GOVD_PRIVATE_OUTER_REF;
5958 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5960 else
5961 notice_outer = false;
5962 goto do_add;
5963 case OMP_CLAUSE_SHARED:
5964 flags = GOVD_SHARED | GOVD_EXPLICIT;
5965 goto do_add;
5966 case OMP_CLAUSE_FIRSTPRIVATE:
5967 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5968 check_non_private = "firstprivate";
5969 goto do_add;
5970 case OMP_CLAUSE_LASTPRIVATE:
5971 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5972 check_non_private = "lastprivate";
5973 goto do_add;
5974 case OMP_CLAUSE_REDUCTION:
5975 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5976 check_non_private = "reduction";
5977 goto do_add;
5978 case OMP_CLAUSE_LINEAR:
5979 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5980 is_gimple_val, fb_rvalue) == GS_ERROR)
5982 remove = true;
5983 break;
5985 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5986 goto do_add;
5988 case OMP_CLAUSE_MAP:
5989 decl = OMP_CLAUSE_DECL (c);
5990 if (error_operand_p (decl))
5992 remove = true;
5993 break;
5995 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
5996 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
5997 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
5998 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5999 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6001 remove = true;
6002 break;
6004 if (!DECL_P (decl))
6006 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6007 NULL, is_gimple_lvalue, fb_lvalue)
6008 == GS_ERROR)
6010 remove = true;
6011 break;
6013 break;
6015 flags = GOVD_MAP | GOVD_EXPLICIT;
6016 goto do_add;
6018 case OMP_CLAUSE_DEPEND:
6019 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6021 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6022 NULL, is_gimple_val, fb_rvalue);
6023 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6025 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6027 remove = true;
6028 break;
6030 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6031 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6032 is_gimple_val, fb_rvalue) == GS_ERROR)
6034 remove = true;
6035 break;
6037 break;
6039 case OMP_CLAUSE_TO:
6040 case OMP_CLAUSE_FROM:
6041 decl = OMP_CLAUSE_DECL (c);
6042 if (error_operand_p (decl))
6044 remove = true;
6045 break;
6047 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6048 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6049 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6050 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6051 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6053 remove = true;
6054 break;
6056 if (!DECL_P (decl))
6058 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6059 NULL, is_gimple_lvalue, fb_lvalue)
6060 == GS_ERROR)
6062 remove = true;
6063 break;
6065 break;
6067 goto do_notice;
6069 do_add:
6070 decl = OMP_CLAUSE_DECL (c);
6071 if (error_operand_p (decl))
6073 remove = true;
6074 break;
6076 omp_add_variable (ctx, decl, flags);
6077 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6078 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6080 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6081 GOVD_LOCAL | GOVD_SEEN);
6082 gimplify_omp_ctxp = ctx;
6083 push_gimplify_context ();
6085 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6086 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6088 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6089 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6090 pop_gimplify_context
6091 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6092 push_gimplify_context ();
6093 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6094 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6095 pop_gimplify_context
6096 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6097 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6098 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6100 gimplify_omp_ctxp = outer_ctx;
6102 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6103 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6105 gimplify_omp_ctxp = ctx;
6106 push_gimplify_context ();
6107 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6109 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6110 NULL, NULL);
6111 TREE_SIDE_EFFECTS (bind) = 1;
6112 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6113 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6115 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6116 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6117 pop_gimplify_context
6118 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6119 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6121 gimplify_omp_ctxp = outer_ctx;
6123 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6124 && OMP_CLAUSE_LINEAR_STMT (c))
6126 gimplify_omp_ctxp = ctx;
6127 push_gimplify_context ();
6128 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6130 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6131 NULL, NULL);
6132 TREE_SIDE_EFFECTS (bind) = 1;
6133 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6134 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6136 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6137 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6138 pop_gimplify_context
6139 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6140 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6142 gimplify_omp_ctxp = outer_ctx;
6144 if (notice_outer)
6145 goto do_notice;
6146 break;
6148 case OMP_CLAUSE_COPYIN:
6149 case OMP_CLAUSE_COPYPRIVATE:
6150 decl = OMP_CLAUSE_DECL (c);
6151 if (error_operand_p (decl))
6153 remove = true;
6154 break;
6156 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6157 && !remove
6158 && !omp_check_private (ctx, decl, true))
6160 remove = true;
6161 if (is_global_var (decl))
6163 if (DECL_THREAD_LOCAL_P (decl))
6164 remove = false;
6165 else if (DECL_HAS_VALUE_EXPR_P (decl))
6167 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6169 if (value
6170 && DECL_P (value)
6171 && DECL_THREAD_LOCAL_P (value))
6172 remove = false;
6175 if (remove)
6176 error_at (OMP_CLAUSE_LOCATION (c),
6177 "copyprivate variable %qE is not threadprivate"
6178 " or private in outer context", DECL_NAME (decl));
6180 do_notice:
6181 if (outer_ctx)
6182 omp_notice_variable (outer_ctx, decl, true);
6183 if (check_non_private
6184 && region_type == ORT_WORKSHARE
6185 && omp_check_private (ctx, decl, false))
6187 error ("%s variable %qE is private in outer context",
6188 check_non_private, DECL_NAME (decl));
6189 remove = true;
6191 break;
6193 case OMP_CLAUSE_FINAL:
6194 case OMP_CLAUSE_IF:
6195 OMP_CLAUSE_OPERAND (c, 0)
6196 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6197 /* Fall through. */
6199 case OMP_CLAUSE_SCHEDULE:
6200 case OMP_CLAUSE_NUM_THREADS:
6201 case OMP_CLAUSE_NUM_TEAMS:
6202 case OMP_CLAUSE_THREAD_LIMIT:
6203 case OMP_CLAUSE_DIST_SCHEDULE:
6204 case OMP_CLAUSE_DEVICE:
6205 case OMP_CLAUSE__CILK_FOR_COUNT_:
6206 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6207 is_gimple_val, fb_rvalue) == GS_ERROR)
6208 remove = true;
6209 break;
6211 case OMP_CLAUSE_NOWAIT:
6212 case OMP_CLAUSE_ORDERED:
6213 case OMP_CLAUSE_UNTIED:
6214 case OMP_CLAUSE_COLLAPSE:
6215 case OMP_CLAUSE_MERGEABLE:
6216 case OMP_CLAUSE_PROC_BIND:
6217 case OMP_CLAUSE_SAFELEN:
6218 break;
6220 case OMP_CLAUSE_ALIGNED:
6221 decl = OMP_CLAUSE_DECL (c);
6222 if (error_operand_p (decl))
6224 remove = true;
6225 break;
6227 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6228 is_gimple_val, fb_rvalue) == GS_ERROR)
6230 remove = true;
6231 break;
6233 if (!is_global_var (decl)
6234 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6235 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6236 break;
6238 case OMP_CLAUSE_DEFAULT:
6239 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6240 break;
6242 default:
6243 gcc_unreachable ();
6246 if (remove)
6247 *list_p = OMP_CLAUSE_CHAIN (c);
6248 else
6249 list_p = &OMP_CLAUSE_CHAIN (c);
6252 gimplify_omp_ctxp = ctx;
6255 struct gimplify_adjust_omp_clauses_data
6257 tree *list_p;
6258 gimple_seq *pre_p;
6261 /* For all variables that were not actually used within the context,
6262 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6264 static int
6265 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6267 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6268 gimple_seq *pre_p
6269 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6270 tree decl = (tree) n->key;
6271 unsigned flags = n->value;
6272 enum omp_clause_code code;
6273 tree clause;
6274 bool private_debug;
6276 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6277 return 0;
6278 if ((flags & GOVD_SEEN) == 0)
6279 return 0;
6280 if (flags & GOVD_DEBUG_PRIVATE)
6282 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6283 private_debug = true;
6285 else if (flags & GOVD_MAP)
6286 private_debug = false;
6287 else
6288 private_debug
6289 = lang_hooks.decls.omp_private_debug_clause (decl,
6290 !!(flags & GOVD_SHARED));
6291 if (private_debug)
6292 code = OMP_CLAUSE_PRIVATE;
6293 else if (flags & GOVD_MAP)
6294 code = OMP_CLAUSE_MAP;
6295 else if (flags & GOVD_SHARED)
6297 if (is_global_var (decl))
6299 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6300 while (ctx != NULL)
6302 splay_tree_node on
6303 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6304 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6305 | GOVD_PRIVATE | GOVD_REDUCTION
6306 | GOVD_LINEAR | GOVD_MAP)) != 0)
6307 break;
6308 ctx = ctx->outer_context;
6310 if (ctx == NULL)
6311 return 0;
6313 code = OMP_CLAUSE_SHARED;
6315 else if (flags & GOVD_PRIVATE)
6316 code = OMP_CLAUSE_PRIVATE;
6317 else if (flags & GOVD_FIRSTPRIVATE)
6318 code = OMP_CLAUSE_FIRSTPRIVATE;
6319 else if (flags & GOVD_LASTPRIVATE)
6320 code = OMP_CLAUSE_LASTPRIVATE;
6321 else if (flags & GOVD_ALIGNED)
6322 return 0;
6323 else
6324 gcc_unreachable ();
6326 clause = build_omp_clause (input_location, code);
6327 OMP_CLAUSE_DECL (clause) = decl;
6328 OMP_CLAUSE_CHAIN (clause) = *list_p;
6329 if (private_debug)
6330 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6331 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6332 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6333 else if (code == OMP_CLAUSE_MAP)
6335 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6336 ? OMP_CLAUSE_MAP_TO
6337 : OMP_CLAUSE_MAP_TOFROM;
6338 if (DECL_SIZE (decl)
6339 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6341 tree decl2 = DECL_VALUE_EXPR (decl);
6342 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6343 decl2 = TREE_OPERAND (decl2, 0);
6344 gcc_assert (DECL_P (decl2));
6345 tree mem = build_simple_mem_ref (decl2);
6346 OMP_CLAUSE_DECL (clause) = mem;
6347 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6348 if (gimplify_omp_ctxp->outer_context)
6350 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6351 omp_notice_variable (ctx, decl2, true);
6352 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6354 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6355 OMP_CLAUSE_MAP);
6356 OMP_CLAUSE_DECL (nc) = decl;
6357 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6358 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6359 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6360 OMP_CLAUSE_CHAIN (clause) = nc;
6362 else
6363 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6365 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6367 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6368 OMP_CLAUSE_DECL (nc) = decl;
6369 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6370 OMP_CLAUSE_CHAIN (nc) = *list_p;
6371 OMP_CLAUSE_CHAIN (clause) = nc;
6372 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6373 gimplify_omp_ctxp = ctx->outer_context;
6374 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6375 gimplify_omp_ctxp = ctx;
6377 *list_p = clause;
6378 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6379 gimplify_omp_ctxp = ctx->outer_context;
6380 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6381 gimplify_omp_ctxp = ctx;
6382 return 0;
6385 static void
6386 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6388 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6389 tree c, decl;
6391 while ((c = *list_p) != NULL)
6393 splay_tree_node n;
6394 bool remove = false;
6396 switch (OMP_CLAUSE_CODE (c))
6398 case OMP_CLAUSE_PRIVATE:
6399 case OMP_CLAUSE_SHARED:
6400 case OMP_CLAUSE_FIRSTPRIVATE:
6401 case OMP_CLAUSE_LINEAR:
6402 decl = OMP_CLAUSE_DECL (c);
6403 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6404 remove = !(n->value & GOVD_SEEN);
6405 if (! remove)
6407 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6408 if ((n->value & GOVD_DEBUG_PRIVATE)
6409 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6411 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6412 || ((n->value & GOVD_DATA_SHARE_CLASS)
6413 == GOVD_PRIVATE));
6414 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6415 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6417 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6418 && ctx->outer_context
6419 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6420 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6422 if (ctx->outer_context->combined_loop
6423 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6425 n = splay_tree_lookup (ctx->outer_context->variables,
6426 (splay_tree_key) decl);
6427 if (n == NULL
6428 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6430 int flags = GOVD_FIRSTPRIVATE;
6431 /* #pragma omp distribute does not allow
6432 lastprivate clause. */
6433 if (!ctx->outer_context->distribute)
6434 flags |= GOVD_LASTPRIVATE;
6435 if (n == NULL)
6436 omp_add_variable (ctx->outer_context, decl,
6437 flags | GOVD_SEEN);
6438 else
6439 n->value |= flags | GOVD_SEEN;
6442 else if (!is_global_var (decl))
6443 omp_notice_variable (ctx->outer_context, decl, true);
6446 break;
6448 case OMP_CLAUSE_LASTPRIVATE:
6449 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6450 accurately reflect the presence of a FIRSTPRIVATE clause. */
6451 decl = OMP_CLAUSE_DECL (c);
6452 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6453 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6454 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6455 break;
6457 case OMP_CLAUSE_ALIGNED:
6458 decl = OMP_CLAUSE_DECL (c);
6459 if (!is_global_var (decl))
6461 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6462 remove = n == NULL || !(n->value & GOVD_SEEN);
6463 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6465 struct gimplify_omp_ctx *octx;
6466 if (n != NULL
6467 && (n->value & (GOVD_DATA_SHARE_CLASS
6468 & ~GOVD_FIRSTPRIVATE)))
6469 remove = true;
6470 else
6471 for (octx = ctx->outer_context; octx;
6472 octx = octx->outer_context)
6474 n = splay_tree_lookup (octx->variables,
6475 (splay_tree_key) decl);
6476 if (n == NULL)
6477 continue;
6478 if (n->value & GOVD_LOCAL)
6479 break;
6480 /* We have to avoid assigning a shared variable
6481 to itself when trying to add
6482 __builtin_assume_aligned. */
6483 if (n->value & GOVD_SHARED)
6485 remove = true;
6486 break;
6491 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6493 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6494 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6495 remove = true;
6497 break;
6499 case OMP_CLAUSE_MAP:
6500 decl = OMP_CLAUSE_DECL (c);
6501 if (!DECL_P (decl))
6502 break;
6503 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6504 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6505 remove = true;
6506 else if (DECL_SIZE (decl)
6507 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6508 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6510 tree decl2 = DECL_VALUE_EXPR (decl);
6511 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6512 decl2 = TREE_OPERAND (decl2, 0);
6513 gcc_assert (DECL_P (decl2));
6514 tree mem = build_simple_mem_ref (decl2);
6515 OMP_CLAUSE_DECL (c) = mem;
6516 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6517 if (ctx->outer_context)
6519 omp_notice_variable (ctx->outer_context, decl2, true);
6520 omp_notice_variable (ctx->outer_context,
6521 OMP_CLAUSE_SIZE (c), true);
6523 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6524 OMP_CLAUSE_MAP);
6525 OMP_CLAUSE_DECL (nc) = decl;
6526 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6527 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6528 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6529 OMP_CLAUSE_CHAIN (c) = nc;
6530 c = nc;
6532 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6533 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6534 break;
6536 case OMP_CLAUSE_TO:
6537 case OMP_CLAUSE_FROM:
6538 decl = OMP_CLAUSE_DECL (c);
6539 if (!DECL_P (decl))
6540 break;
6541 if (DECL_SIZE (decl)
6542 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6544 tree decl2 = DECL_VALUE_EXPR (decl);
6545 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6546 decl2 = TREE_OPERAND (decl2, 0);
6547 gcc_assert (DECL_P (decl2));
6548 tree mem = build_simple_mem_ref (decl2);
6549 OMP_CLAUSE_DECL (c) = mem;
6550 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6551 if (ctx->outer_context)
6553 omp_notice_variable (ctx->outer_context, decl2, true);
6554 omp_notice_variable (ctx->outer_context,
6555 OMP_CLAUSE_SIZE (c), true);
6558 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6559 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6560 break;
6562 case OMP_CLAUSE_REDUCTION:
6563 case OMP_CLAUSE_COPYIN:
6564 case OMP_CLAUSE_COPYPRIVATE:
6565 case OMP_CLAUSE_IF:
6566 case OMP_CLAUSE_NUM_THREADS:
6567 case OMP_CLAUSE_NUM_TEAMS:
6568 case OMP_CLAUSE_THREAD_LIMIT:
6569 case OMP_CLAUSE_DIST_SCHEDULE:
6570 case OMP_CLAUSE_DEVICE:
6571 case OMP_CLAUSE_SCHEDULE:
6572 case OMP_CLAUSE_NOWAIT:
6573 case OMP_CLAUSE_ORDERED:
6574 case OMP_CLAUSE_DEFAULT:
6575 case OMP_CLAUSE_UNTIED:
6576 case OMP_CLAUSE_COLLAPSE:
6577 case OMP_CLAUSE_FINAL:
6578 case OMP_CLAUSE_MERGEABLE:
6579 case OMP_CLAUSE_PROC_BIND:
6580 case OMP_CLAUSE_SAFELEN:
6581 case OMP_CLAUSE_DEPEND:
6582 case OMP_CLAUSE__CILK_FOR_COUNT_:
6583 break;
6585 default:
6586 gcc_unreachable ();
6589 if (remove)
6590 *list_p = OMP_CLAUSE_CHAIN (c);
6591 else
6592 list_p = &OMP_CLAUSE_CHAIN (c);
6595 /* Add in any implicit data sharing. */
6596 struct gimplify_adjust_omp_clauses_data data;
6597 data.list_p = list_p;
6598 data.pre_p = pre_p;
6599 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6601 gimplify_omp_ctxp = ctx->outer_context;
6602 delete_omp_context (ctx);
6605 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6606 gimplification of the body, as well as scanning the body for used
6607 variables. We need to do this scan now, because variable-sized
6608 decls will be decomposed during gimplification. */
6610 static void
6611 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6613 tree expr = *expr_p;
6614 gimple g;
6615 gimple_seq body = NULL;
6617 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6618 OMP_PARALLEL_COMBINED (expr)
6619 ? ORT_COMBINED_PARALLEL
6620 : ORT_PARALLEL);
6622 push_gimplify_context ();
6624 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6625 if (gimple_code (g) == GIMPLE_BIND)
6626 pop_gimplify_context (g);
6627 else
6628 pop_gimplify_context (NULL);
6630 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6632 g = gimple_build_omp_parallel (body,
6633 OMP_PARALLEL_CLAUSES (expr),
6634 NULL_TREE, NULL_TREE);
6635 if (OMP_PARALLEL_COMBINED (expr))
6636 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6637 gimplify_seq_add_stmt (pre_p, g);
6638 *expr_p = NULL_TREE;
6641 /* Gimplify the contents of an OMP_TASK statement. This involves
6642 gimplification of the body, as well as scanning the body for used
6643 variables. We need to do this scan now, because variable-sized
6644 decls will be decomposed during gimplification. */
6646 static void
6647 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6649 tree expr = *expr_p;
6650 gimple g;
6651 gimple_seq body = NULL;
6653 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6654 find_omp_clause (OMP_TASK_CLAUSES (expr),
6655 OMP_CLAUSE_UNTIED)
6656 ? ORT_UNTIED_TASK : ORT_TASK);
6658 push_gimplify_context ();
6660 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6661 if (gimple_code (g) == GIMPLE_BIND)
6662 pop_gimplify_context (g);
6663 else
6664 pop_gimplify_context (NULL);
6666 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6668 g = gimple_build_omp_task (body,
6669 OMP_TASK_CLAUSES (expr),
6670 NULL_TREE, NULL_TREE,
6671 NULL_TREE, NULL_TREE, NULL_TREE);
6672 gimplify_seq_add_stmt (pre_p, g);
6673 *expr_p = NULL_TREE;
6676 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6677 with non-NULL OMP_FOR_INIT. */
6679 static tree
6680 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6682 *walk_subtrees = 0;
6683 switch (TREE_CODE (*tp))
6685 case OMP_FOR:
6686 *walk_subtrees = 1;
6687 /* FALLTHRU */
6688 case OMP_SIMD:
6689 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6690 return *tp;
6691 break;
6692 case BIND_EXPR:
6693 case STATEMENT_LIST:
6694 case OMP_PARALLEL:
6695 *walk_subtrees = 1;
6696 break;
6697 default:
6698 break;
6700 return NULL_TREE;
6703 /* Gimplify the gross structure of an OMP_FOR statement. */
6705 static enum gimplify_status
6706 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6708 tree for_stmt, orig_for_stmt, decl, var, t;
6709 enum gimplify_status ret = GS_ALL_DONE;
6710 enum gimplify_status tret;
6711 gimple_omp_for gfor;
6712 gimple_seq for_body, for_pre_body;
6713 int i;
6714 bool simd;
6715 bitmap has_decl_expr = NULL;
6717 orig_for_stmt = for_stmt = *expr_p;
6719 simd = (TREE_CODE (for_stmt) == OMP_SIMD
6720 || TREE_CODE (for_stmt) == CILK_SIMD);
6721 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6722 simd ? ORT_SIMD : ORT_WORKSHARE);
6723 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6724 gimplify_omp_ctxp->distribute = true;
6726 /* Handle OMP_FOR_INIT. */
6727 for_pre_body = NULL;
6728 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6730 has_decl_expr = BITMAP_ALLOC (NULL);
6731 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6732 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6733 == VAR_DECL)
6735 t = OMP_FOR_PRE_BODY (for_stmt);
6736 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6738 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6740 tree_stmt_iterator si;
6741 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6742 tsi_next (&si))
6744 t = tsi_stmt (si);
6745 if (TREE_CODE (t) == DECL_EXPR
6746 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6747 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6751 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6752 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6754 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6756 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6757 NULL, NULL);
6758 gcc_assert (for_stmt != NULL_TREE);
6759 gimplify_omp_ctxp->combined_loop = true;
6762 for_body = NULL;
6763 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6764 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6765 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6766 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6767 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6769 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6770 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6771 decl = TREE_OPERAND (t, 0);
6772 gcc_assert (DECL_P (decl));
6773 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6774 || POINTER_TYPE_P (TREE_TYPE (decl)));
6776 /* Make sure the iteration variable is private. */
6777 tree c = NULL_TREE;
6778 tree c2 = NULL_TREE;
6779 if (orig_for_stmt != for_stmt)
6780 /* Do this only on innermost construct for combined ones. */;
6781 else if (simd)
6783 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6784 (splay_tree_key)decl);
6785 omp_is_private (gimplify_omp_ctxp, decl,
6786 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6787 != 1));
6788 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6789 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6790 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6792 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6793 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6794 if (has_decl_expr
6795 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6796 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6797 OMP_CLAUSE_DECL (c) = decl;
6798 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6799 OMP_FOR_CLAUSES (for_stmt) = c;
6800 omp_add_variable (gimplify_omp_ctxp, decl,
6801 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6803 else
6805 bool lastprivate
6806 = (!has_decl_expr
6807 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6808 if (lastprivate
6809 && gimplify_omp_ctxp->outer_context
6810 && gimplify_omp_ctxp->outer_context->region_type
6811 == ORT_WORKSHARE
6812 && gimplify_omp_ctxp->outer_context->combined_loop
6813 && !gimplify_omp_ctxp->outer_context->distribute)
6815 struct gimplify_omp_ctx *outer
6816 = gimplify_omp_ctxp->outer_context;
6817 n = splay_tree_lookup (outer->variables,
6818 (splay_tree_key) decl);
6819 if (n != NULL
6820 && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6821 lastprivate = false;
6822 else if (omp_check_private (outer, decl, false))
6823 error ("lastprivate variable %qE is private in outer "
6824 "context", DECL_NAME (decl));
6825 else
6827 omp_add_variable (outer, decl,
6828 GOVD_LASTPRIVATE | GOVD_SEEN);
6829 if (outer->outer_context)
6830 omp_notice_variable (outer->outer_context, decl, true);
6833 c = build_omp_clause (input_location,
6834 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6835 : OMP_CLAUSE_PRIVATE);
6836 OMP_CLAUSE_DECL (c) = decl;
6837 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6838 OMP_FOR_CLAUSES (for_stmt) = c;
6839 omp_add_variable (gimplify_omp_ctxp, decl,
6840 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6841 | GOVD_EXPLICIT | GOVD_SEEN);
6842 c = NULL_TREE;
6845 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6846 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6847 else
6848 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6850 /* If DECL is not a gimple register, create a temporary variable to act
6851 as an iteration counter. This is valid, since DECL cannot be
6852 modified in the body of the loop. Similarly for any iteration vars
6853 in simd with collapse > 1 where the iterator vars must be
6854 lastprivate. */
6855 if (orig_for_stmt != for_stmt)
6856 var = decl;
6857 else if (!is_gimple_reg (decl)
6858 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
6860 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6861 TREE_OPERAND (t, 0) = var;
6863 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6865 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6867 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6868 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
6869 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
6870 OMP_CLAUSE_DECL (c2) = var;
6871 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
6872 OMP_FOR_CLAUSES (for_stmt) = c2;
6873 omp_add_variable (gimplify_omp_ctxp, var,
6874 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6875 if (c == NULL_TREE)
6877 c = c2;
6878 c2 = NULL_TREE;
6881 else
6882 omp_add_variable (gimplify_omp_ctxp, var,
6883 GOVD_PRIVATE | GOVD_SEEN);
6885 else
6886 var = decl;
6888 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6889 is_gimple_val, fb_rvalue);
6890 ret = MIN (ret, tret);
6891 if (ret == GS_ERROR)
6892 return ret;
6894 /* Handle OMP_FOR_COND. */
6895 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6896 gcc_assert (COMPARISON_CLASS_P (t));
6897 gcc_assert (TREE_OPERAND (t, 0) == decl);
6899 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6900 is_gimple_val, fb_rvalue);
6901 ret = MIN (ret, tret);
6903 /* Handle OMP_FOR_INCR. */
6904 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6905 switch (TREE_CODE (t))
6907 case PREINCREMENT_EXPR:
6908 case POSTINCREMENT_EXPR:
6910 tree decl = TREE_OPERAND (t, 0);
6911 /* c_omp_for_incr_canonicalize_ptr() should have been
6912 called to massage things appropriately. */
6913 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6915 if (orig_for_stmt != for_stmt)
6916 break;
6917 t = build_int_cst (TREE_TYPE (decl), 1);
6918 if (c)
6919 OMP_CLAUSE_LINEAR_STEP (c) = t;
6920 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6921 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6922 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6923 break;
6926 case PREDECREMENT_EXPR:
6927 case POSTDECREMENT_EXPR:
6928 /* c_omp_for_incr_canonicalize_ptr() should have been
6929 called to massage things appropriately. */
6930 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6931 if (orig_for_stmt != for_stmt)
6932 break;
6933 t = build_int_cst (TREE_TYPE (decl), -1);
6934 if (c)
6935 OMP_CLAUSE_LINEAR_STEP (c) = t;
6936 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6937 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6938 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6939 break;
6941 case MODIFY_EXPR:
6942 gcc_assert (TREE_OPERAND (t, 0) == decl);
6943 TREE_OPERAND (t, 0) = var;
6945 t = TREE_OPERAND (t, 1);
6946 switch (TREE_CODE (t))
6948 case PLUS_EXPR:
6949 if (TREE_OPERAND (t, 1) == decl)
6951 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6952 TREE_OPERAND (t, 0) = var;
6953 break;
6956 /* Fallthru. */
6957 case MINUS_EXPR:
6958 case POINTER_PLUS_EXPR:
6959 gcc_assert (TREE_OPERAND (t, 0) == decl);
6960 TREE_OPERAND (t, 0) = var;
6961 break;
6962 default:
6963 gcc_unreachable ();
6966 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6967 is_gimple_val, fb_rvalue);
6968 ret = MIN (ret, tret);
6969 if (c)
6971 tree step = TREE_OPERAND (t, 1);
6972 tree stept = TREE_TYPE (decl);
6973 if (POINTER_TYPE_P (stept))
6974 stept = sizetype;
6975 step = fold_convert (stept, step);
6976 if (TREE_CODE (t) == MINUS_EXPR)
6977 step = fold_build1 (NEGATE_EXPR, stept, step);
6978 OMP_CLAUSE_LINEAR_STEP (c) = step;
6979 if (step != TREE_OPERAND (t, 1))
6981 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6982 &for_pre_body, NULL,
6983 is_gimple_val, fb_rvalue);
6984 ret = MIN (ret, tret);
6987 break;
6989 default:
6990 gcc_unreachable ();
6993 if (c2)
6995 gcc_assert (c);
6996 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
6999 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7000 && orig_for_stmt == for_stmt)
7002 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7003 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7004 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7005 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7006 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7007 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7008 && OMP_CLAUSE_DECL (c) == decl)
7010 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7011 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7012 gcc_assert (TREE_OPERAND (t, 0) == var);
7013 t = TREE_OPERAND (t, 1);
7014 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7015 || TREE_CODE (t) == MINUS_EXPR
7016 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7017 gcc_assert (TREE_OPERAND (t, 0) == var);
7018 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7019 TREE_OPERAND (t, 1));
7020 gimple_seq *seq;
7021 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7022 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7023 else
7024 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7025 gimplify_assign (decl, t, seq);
7030 BITMAP_FREE (has_decl_expr);
7032 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7034 if (orig_for_stmt != for_stmt)
7035 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7037 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7038 decl = TREE_OPERAND (t, 0);
7039 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7040 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7041 TREE_OPERAND (t, 0) = var;
7042 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7043 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7044 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7047 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7049 int kind;
7050 switch (TREE_CODE (orig_for_stmt))
7052 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7053 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7054 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7055 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7056 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7057 default:
7058 gcc_unreachable ();
7060 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7061 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7062 for_pre_body);
7063 if (orig_for_stmt != for_stmt)
7064 gimple_omp_for_set_combined_p (gfor, true);
7065 if (gimplify_omp_ctxp
7066 && (gimplify_omp_ctxp->combined_loop
7067 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7068 && gimplify_omp_ctxp->outer_context
7069 && gimplify_omp_ctxp->outer_context->combined_loop)))
7071 gimple_omp_for_set_combined_into_p (gfor, true);
7072 if (gimplify_omp_ctxp->combined_loop)
7073 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7074 else
7075 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7078 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7080 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7081 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7082 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7083 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7084 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7085 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7086 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7087 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7090 gimplify_seq_add_stmt (pre_p, gfor);
7091 if (ret != GS_ALL_DONE)
7092 return GS_ERROR;
7093 *expr_p = NULL_TREE;
7094 return GS_ALL_DONE;
7097 /* Gimplify the gross structure of other OpenMP constructs.
7098 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
7099 and OMP_TEAMS. */
7101 static void
7102 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7104 tree expr = *expr_p;
7105 gimple stmt;
7106 gimple_seq body = NULL;
7107 enum omp_region_type ort = ORT_WORKSHARE;
7109 switch (TREE_CODE (expr))
7111 case OMP_SECTIONS:
7112 case OMP_SINGLE:
7113 break;
7114 case OMP_TARGET:
7115 ort = ORT_TARGET;
7116 break;
7117 case OMP_TARGET_DATA:
7118 ort = ORT_TARGET_DATA;
7119 break;
7120 case OMP_TEAMS:
7121 ort = ORT_TEAMS;
7122 break;
7123 default:
7124 gcc_unreachable ();
7126 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7127 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7129 push_gimplify_context ();
7130 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7131 if (gimple_code (g) == GIMPLE_BIND)
7132 pop_gimplify_context (g);
7133 else
7134 pop_gimplify_context (NULL);
7135 if (ort == ORT_TARGET_DATA)
7137 gimple_seq cleanup = NULL;
7138 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
7139 g = gimple_build_call (fn, 0);
7140 gimple_seq_add_stmt (&cleanup, g);
7141 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7142 body = NULL;
7143 gimple_seq_add_stmt (&body, g);
7146 else
7147 gimplify_and_add (OMP_BODY (expr), &body);
7148 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7150 switch (TREE_CODE (expr))
7152 case OMP_SECTIONS:
7153 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7154 break;
7155 case OMP_SINGLE:
7156 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7157 break;
7158 case OMP_TARGET:
7159 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7160 OMP_CLAUSES (expr));
7161 break;
7162 case OMP_TARGET_DATA:
7163 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7164 OMP_CLAUSES (expr));
7165 break;
7166 case OMP_TEAMS:
7167 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7168 break;
7169 default:
7170 gcc_unreachable ();
7173 gimplify_seq_add_stmt (pre_p, stmt);
7174 *expr_p = NULL_TREE;
7177 /* Gimplify the gross structure of OpenMP target update construct. */
7179 static void
7180 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7182 tree expr = *expr_p;
7183 gimple_omp_target stmt;
7185 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7186 ORT_WORKSHARE);
7187 gimplify_adjust_omp_clauses (pre_p, &OMP_TARGET_UPDATE_CLAUSES (expr));
7188 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7189 OMP_TARGET_UPDATE_CLAUSES (expr));
7191 gimplify_seq_add_stmt (pre_p, stmt);
7192 *expr_p = NULL_TREE;
7195 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7196 stabilized the lhs of the atomic operation as *ADDR. Return true if
7197 EXPR is this stabilized form. */
7199 static bool
7200 goa_lhs_expr_p (tree expr, tree addr)
7202 /* Also include casts to other type variants. The C front end is fond
7203 of adding these for e.g. volatile variables. This is like
7204 STRIP_TYPE_NOPS but includes the main variant lookup. */
7205 STRIP_USELESS_TYPE_CONVERSION (expr);
7207 if (TREE_CODE (expr) == INDIRECT_REF)
7209 expr = TREE_OPERAND (expr, 0);
7210 while (expr != addr
7211 && (CONVERT_EXPR_P (expr)
7212 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7213 && TREE_CODE (expr) == TREE_CODE (addr)
7214 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7216 expr = TREE_OPERAND (expr, 0);
7217 addr = TREE_OPERAND (addr, 0);
7219 if (expr == addr)
7220 return true;
7221 return (TREE_CODE (addr) == ADDR_EXPR
7222 && TREE_CODE (expr) == ADDR_EXPR
7223 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7225 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7226 return true;
7227 return false;
7230 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7231 expression does not involve the lhs, evaluate it into a temporary.
7232 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7233 or -1 if an error was encountered. */
7235 static int
7236 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7237 tree lhs_var)
7239 tree expr = *expr_p;
7240 int saw_lhs;
7242 if (goa_lhs_expr_p (expr, lhs_addr))
7244 *expr_p = lhs_var;
7245 return 1;
7247 if (is_gimple_val (expr))
7248 return 0;
7250 saw_lhs = 0;
7251 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7253 case tcc_binary:
7254 case tcc_comparison:
7255 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7256 lhs_var);
7257 case tcc_unary:
7258 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7259 lhs_var);
7260 break;
7261 case tcc_expression:
7262 switch (TREE_CODE (expr))
7264 case TRUTH_ANDIF_EXPR:
7265 case TRUTH_ORIF_EXPR:
7266 case TRUTH_AND_EXPR:
7267 case TRUTH_OR_EXPR:
7268 case TRUTH_XOR_EXPR:
7269 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7270 lhs_addr, lhs_var);
7271 case TRUTH_NOT_EXPR:
7272 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7273 lhs_addr, lhs_var);
7274 break;
7275 case COMPOUND_EXPR:
7276 /* Break out any preevaluations from cp_build_modify_expr. */
7277 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7278 expr = TREE_OPERAND (expr, 1))
7279 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7280 *expr_p = expr;
7281 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7282 default:
7283 break;
7285 break;
7286 default:
7287 break;
7290 if (saw_lhs == 0)
7292 enum gimplify_status gs;
7293 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7294 if (gs != GS_ALL_DONE)
7295 saw_lhs = -1;
7298 return saw_lhs;
7301 /* Gimplify an OMP_ATOMIC statement. */
7303 static enum gimplify_status
7304 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7306 tree addr = TREE_OPERAND (*expr_p, 0);
7307 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7308 ? NULL : TREE_OPERAND (*expr_p, 1);
7309 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7310 tree tmp_load;
7311 gimple_omp_atomic_load loadstmt;
7312 gimple_omp_atomic_store storestmt;
7314 tmp_load = create_tmp_reg (type, NULL);
7315 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7316 return GS_ERROR;
7318 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7319 != GS_ALL_DONE)
7320 return GS_ERROR;
7322 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7323 gimplify_seq_add_stmt (pre_p, loadstmt);
7324 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7325 != GS_ALL_DONE)
7326 return GS_ERROR;
7328 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7329 rhs = tmp_load;
7330 storestmt = gimple_build_omp_atomic_store (rhs);
7331 gimplify_seq_add_stmt (pre_p, storestmt);
7332 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7334 gimple_omp_atomic_set_seq_cst (loadstmt);
7335 gimple_omp_atomic_set_seq_cst (storestmt);
7337 switch (TREE_CODE (*expr_p))
7339 case OMP_ATOMIC_READ:
7340 case OMP_ATOMIC_CAPTURE_OLD:
7341 *expr_p = tmp_load;
7342 gimple_omp_atomic_set_need_value (loadstmt);
7343 break;
7344 case OMP_ATOMIC_CAPTURE_NEW:
7345 *expr_p = rhs;
7346 gimple_omp_atomic_set_need_value (storestmt);
7347 break;
7348 default:
7349 *expr_p = NULL;
7350 break;
7353 return GS_ALL_DONE;
7356 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7357 body, and adding some EH bits. */
7359 static enum gimplify_status
7360 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7362 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7363 gimple body_stmt;
7364 gimple_transaction trans_stmt;
7365 gimple_seq body = NULL;
7366 int subcode = 0;
7368 /* Wrap the transaction body in a BIND_EXPR so we have a context
7369 where to put decls for OpenMP. */
7370 if (TREE_CODE (tbody) != BIND_EXPR)
7372 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7373 TREE_SIDE_EFFECTS (bind) = 1;
7374 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7375 TRANSACTION_EXPR_BODY (expr) = bind;
7378 push_gimplify_context ();
7379 temp = voidify_wrapper_expr (*expr_p, NULL);
7381 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7382 pop_gimplify_context (body_stmt);
7384 trans_stmt = gimple_build_transaction (body, NULL);
7385 if (TRANSACTION_EXPR_OUTER (expr))
7386 subcode = GTMA_IS_OUTER;
7387 else if (TRANSACTION_EXPR_RELAXED (expr))
7388 subcode = GTMA_IS_RELAXED;
7389 gimple_transaction_set_subcode (trans_stmt, subcode);
7391 gimplify_seq_add_stmt (pre_p, trans_stmt);
7393 if (temp)
7395 *expr_p = temp;
7396 return GS_OK;
7399 *expr_p = NULL_TREE;
7400 return GS_ALL_DONE;
7403 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7404 expression produces a value to be used as an operand inside a GIMPLE
7405 statement, the value will be stored back in *EXPR_P. This value will
7406 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7407 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7408 emitted in PRE_P and POST_P.
7410 Additionally, this process may overwrite parts of the input
7411 expression during gimplification. Ideally, it should be
7412 possible to do non-destructive gimplification.
7414 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7415 the expression needs to evaluate to a value to be used as
7416 an operand in a GIMPLE statement, this value will be stored in
7417 *EXPR_P on exit. This happens when the caller specifies one
7418 of fb_lvalue or fb_rvalue fallback flags.
7420 PRE_P will contain the sequence of GIMPLE statements corresponding
7421 to the evaluation of EXPR and all the side-effects that must
7422 be executed before the main expression. On exit, the last
7423 statement of PRE_P is the core statement being gimplified. For
7424 instance, when gimplifying 'if (++a)' the last statement in
7425 PRE_P will be 'if (t.1)' where t.1 is the result of
7426 pre-incrementing 'a'.
7428 POST_P will contain the sequence of GIMPLE statements corresponding
7429 to the evaluation of all the side-effects that must be executed
7430 after the main expression. If this is NULL, the post
7431 side-effects are stored at the end of PRE_P.
7433 The reason why the output is split in two is to handle post
7434 side-effects explicitly. In some cases, an expression may have
7435 inner and outer post side-effects which need to be emitted in
7436 an order different from the one given by the recursive
7437 traversal. For instance, for the expression (*p--)++ the post
7438 side-effects of '--' must actually occur *after* the post
7439 side-effects of '++'. However, gimplification will first visit
7440 the inner expression, so if a separate POST sequence was not
7441 used, the resulting sequence would be:
7443 1 t.1 = *p
7444 2 p = p - 1
7445 3 t.2 = t.1 + 1
7446 4 *p = t.2
7448 However, the post-decrement operation in line #2 must not be
7449 evaluated until after the store to *p at line #4, so the
7450 correct sequence should be:
7452 1 t.1 = *p
7453 2 t.2 = t.1 + 1
7454 3 *p = t.2
7455 4 p = p - 1
7457 So, by specifying a separate post queue, it is possible
7458 to emit the post side-effects in the correct order.
7459 If POST_P is NULL, an internal queue will be used. Before
7460 returning to the caller, the sequence POST_P is appended to
7461 the main output sequence PRE_P.
7463 GIMPLE_TEST_F points to a function that takes a tree T and
7464 returns nonzero if T is in the GIMPLE form requested by the
7465 caller. The GIMPLE predicates are in gimple.c.
7467 FALLBACK tells the function what sort of a temporary we want if
7468 gimplification cannot produce an expression that complies with
7469 GIMPLE_TEST_F.
7471 fb_none means that no temporary should be generated
7472 fb_rvalue means that an rvalue is OK to generate
7473 fb_lvalue means that an lvalue is OK to generate
7474 fb_either means that either is OK, but an lvalue is preferable.
7475 fb_mayfail means that gimplification may fail (in which case
7476 GS_ERROR will be returned)
7478 The return value is either GS_ERROR or GS_ALL_DONE, since this
7479 function iterates until EXPR is completely gimplified or an error
7480 occurs. */
7482 enum gimplify_status
7483 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7484 bool (*gimple_test_f) (tree), fallback_t fallback)
7486 tree tmp;
7487 gimple_seq internal_pre = NULL;
7488 gimple_seq internal_post = NULL;
7489 tree save_expr;
7490 bool is_statement;
7491 location_t saved_location;
7492 enum gimplify_status ret;
7493 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7495 save_expr = *expr_p;
7496 if (save_expr == NULL_TREE)
7497 return GS_ALL_DONE;
7499 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7500 is_statement = gimple_test_f == is_gimple_stmt;
7501 if (is_statement)
7502 gcc_assert (pre_p);
7504 /* Consistency checks. */
7505 if (gimple_test_f == is_gimple_reg)
7506 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7507 else if (gimple_test_f == is_gimple_val
7508 || gimple_test_f == is_gimple_call_addr
7509 || gimple_test_f == is_gimple_condexpr
7510 || gimple_test_f == is_gimple_mem_rhs
7511 || gimple_test_f == is_gimple_mem_rhs_or_call
7512 || gimple_test_f == is_gimple_reg_rhs
7513 || gimple_test_f == is_gimple_reg_rhs_or_call
7514 || gimple_test_f == is_gimple_asm_val
7515 || gimple_test_f == is_gimple_mem_ref_addr)
7516 gcc_assert (fallback & fb_rvalue);
7517 else if (gimple_test_f == is_gimple_min_lval
7518 || gimple_test_f == is_gimple_lvalue)
7519 gcc_assert (fallback & fb_lvalue);
7520 else if (gimple_test_f == is_gimple_addressable)
7521 gcc_assert (fallback & fb_either);
7522 else if (gimple_test_f == is_gimple_stmt)
7523 gcc_assert (fallback == fb_none);
7524 else
7526 /* We should have recognized the GIMPLE_TEST_F predicate to
7527 know what kind of fallback to use in case a temporary is
7528 needed to hold the value or address of *EXPR_P. */
7529 gcc_unreachable ();
7532 /* We used to check the predicate here and return immediately if it
7533 succeeds. This is wrong; the design is for gimplification to be
7534 idempotent, and for the predicates to only test for valid forms, not
7535 whether they are fully simplified. */
7536 if (pre_p == NULL)
7537 pre_p = &internal_pre;
7539 if (post_p == NULL)
7540 post_p = &internal_post;
7542 /* Remember the last statements added to PRE_P and POST_P. Every
7543 new statement added by the gimplification helpers needs to be
7544 annotated with location information. To centralize the
7545 responsibility, we remember the last statement that had been
7546 added to both queues before gimplifying *EXPR_P. If
7547 gimplification produces new statements in PRE_P and POST_P, those
7548 statements will be annotated with the same location information
7549 as *EXPR_P. */
7550 pre_last_gsi = gsi_last (*pre_p);
7551 post_last_gsi = gsi_last (*post_p);
7553 saved_location = input_location;
7554 if (save_expr != error_mark_node
7555 && EXPR_HAS_LOCATION (*expr_p))
7556 input_location = EXPR_LOCATION (*expr_p);
7558 /* Loop over the specific gimplifiers until the toplevel node
7559 remains the same. */
7562 /* Strip away as many useless type conversions as possible
7563 at the toplevel. */
7564 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7566 /* Remember the expr. */
7567 save_expr = *expr_p;
7569 /* Die, die, die, my darling. */
7570 if (save_expr == error_mark_node
7571 || (TREE_TYPE (save_expr)
7572 && TREE_TYPE (save_expr) == error_mark_node))
7574 ret = GS_ERROR;
7575 break;
7578 /* Do any language-specific gimplification. */
7579 ret = ((enum gimplify_status)
7580 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7581 if (ret == GS_OK)
7583 if (*expr_p == NULL_TREE)
7584 break;
7585 if (*expr_p != save_expr)
7586 continue;
7588 else if (ret != GS_UNHANDLED)
7589 break;
7591 /* Make sure that all the cases set 'ret' appropriately. */
7592 ret = GS_UNHANDLED;
7593 switch (TREE_CODE (*expr_p))
7595 /* First deal with the special cases. */
7597 case POSTINCREMENT_EXPR:
7598 case POSTDECREMENT_EXPR:
7599 case PREINCREMENT_EXPR:
7600 case PREDECREMENT_EXPR:
7601 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7602 fallback != fb_none,
7603 TREE_TYPE (*expr_p));
7604 break;
7606 case VIEW_CONVERT_EXPR:
7607 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7608 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7610 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7611 post_p, is_gimple_val, fb_rvalue);
7612 recalculate_side_effects (*expr_p);
7613 break;
7615 /* Fallthru. */
7617 case ARRAY_REF:
7618 case ARRAY_RANGE_REF:
7619 case REALPART_EXPR:
7620 case IMAGPART_EXPR:
7621 case COMPONENT_REF:
7622 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7623 fallback ? fallback : fb_rvalue);
7624 break;
7626 case COND_EXPR:
7627 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7629 /* C99 code may assign to an array in a structure value of a
7630 conditional expression, and this has undefined behavior
7631 only on execution, so create a temporary if an lvalue is
7632 required. */
7633 if (fallback == fb_lvalue)
7635 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7636 mark_addressable (*expr_p);
7637 ret = GS_OK;
7639 break;
7641 case CALL_EXPR:
7642 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7644 /* C99 code may assign to an array in a structure returned
7645 from a function, and this has undefined behavior only on
7646 execution, so create a temporary if an lvalue is
7647 required. */
7648 if (fallback == fb_lvalue)
7650 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7651 mark_addressable (*expr_p);
7652 ret = GS_OK;
7654 break;
7656 case TREE_LIST:
7657 gcc_unreachable ();
7659 case COMPOUND_EXPR:
7660 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7661 break;
7663 case COMPOUND_LITERAL_EXPR:
7664 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7665 gimple_test_f, fallback);
7666 break;
7668 case MODIFY_EXPR:
7669 case INIT_EXPR:
7670 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7671 fallback != fb_none);
7672 break;
7674 case TRUTH_ANDIF_EXPR:
7675 case TRUTH_ORIF_EXPR:
7677 /* Preserve the original type of the expression and the
7678 source location of the outer expression. */
7679 tree org_type = TREE_TYPE (*expr_p);
7680 *expr_p = gimple_boolify (*expr_p);
7681 *expr_p = build3_loc (input_location, COND_EXPR,
7682 org_type, *expr_p,
7683 fold_convert_loc
7684 (input_location,
7685 org_type, boolean_true_node),
7686 fold_convert_loc
7687 (input_location,
7688 org_type, boolean_false_node));
7689 ret = GS_OK;
7690 break;
7693 case TRUTH_NOT_EXPR:
7695 tree type = TREE_TYPE (*expr_p);
7696 /* The parsers are careful to generate TRUTH_NOT_EXPR
7697 only with operands that are always zero or one.
7698 We do not fold here but handle the only interesting case
7699 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7700 *expr_p = gimple_boolify (*expr_p);
7701 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7702 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7703 TREE_TYPE (*expr_p),
7704 TREE_OPERAND (*expr_p, 0));
7705 else
7706 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7707 TREE_TYPE (*expr_p),
7708 TREE_OPERAND (*expr_p, 0),
7709 build_int_cst (TREE_TYPE (*expr_p), 1));
7710 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7711 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7712 ret = GS_OK;
7713 break;
7716 case ADDR_EXPR:
7717 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7718 break;
7720 case ANNOTATE_EXPR:
7722 tree cond = TREE_OPERAND (*expr_p, 0);
7723 tree kind = TREE_OPERAND (*expr_p, 1);
7724 tree type = TREE_TYPE (cond);
7725 if (!INTEGRAL_TYPE_P (type))
7727 *expr_p = cond;
7728 ret = GS_OK;
7729 break;
7731 tree tmp = create_tmp_var (type, NULL);
7732 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7733 gimple_call call
7734 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7735 gimple_call_set_lhs (call, tmp);
7736 gimplify_seq_add_stmt (pre_p, call);
7737 *expr_p = tmp;
7738 ret = GS_ALL_DONE;
7739 break;
7742 case VA_ARG_EXPR:
7743 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7744 break;
7746 CASE_CONVERT:
7747 if (IS_EMPTY_STMT (*expr_p))
7749 ret = GS_ALL_DONE;
7750 break;
7753 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7754 || fallback == fb_none)
7756 /* Just strip a conversion to void (or in void context) and
7757 try again. */
7758 *expr_p = TREE_OPERAND (*expr_p, 0);
7759 ret = GS_OK;
7760 break;
7763 ret = gimplify_conversion (expr_p);
7764 if (ret == GS_ERROR)
7765 break;
7766 if (*expr_p != save_expr)
7767 break;
7768 /* FALLTHRU */
7770 case FIX_TRUNC_EXPR:
7771 /* unary_expr: ... | '(' cast ')' val | ... */
7772 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7773 is_gimple_val, fb_rvalue);
7774 recalculate_side_effects (*expr_p);
7775 break;
7777 case INDIRECT_REF:
7779 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7780 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7781 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7783 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7784 if (*expr_p != save_expr)
7786 ret = GS_OK;
7787 break;
7790 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7791 is_gimple_reg, fb_rvalue);
7792 if (ret == GS_ERROR)
7793 break;
7795 recalculate_side_effects (*expr_p);
7796 *expr_p = fold_build2_loc (input_location, MEM_REF,
7797 TREE_TYPE (*expr_p),
7798 TREE_OPERAND (*expr_p, 0),
7799 build_int_cst (saved_ptr_type, 0));
7800 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7801 TREE_THIS_NOTRAP (*expr_p) = notrap;
7802 ret = GS_OK;
7803 break;
7806 /* We arrive here through the various re-gimplifcation paths. */
7807 case MEM_REF:
7808 /* First try re-folding the whole thing. */
7809 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7810 TREE_OPERAND (*expr_p, 0),
7811 TREE_OPERAND (*expr_p, 1));
7812 if (tmp)
7814 *expr_p = tmp;
7815 recalculate_side_effects (*expr_p);
7816 ret = GS_OK;
7817 break;
7819 /* Avoid re-gimplifying the address operand if it is already
7820 in suitable form. Re-gimplifying would mark the address
7821 operand addressable. Always gimplify when not in SSA form
7822 as we still may have to gimplify decls with value-exprs. */
7823 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7824 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7826 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7827 is_gimple_mem_ref_addr, fb_rvalue);
7828 if (ret == GS_ERROR)
7829 break;
7831 recalculate_side_effects (*expr_p);
7832 ret = GS_ALL_DONE;
7833 break;
7835 /* Constants need not be gimplified. */
7836 case INTEGER_CST:
7837 case REAL_CST:
7838 case FIXED_CST:
7839 case STRING_CST:
7840 case COMPLEX_CST:
7841 case VECTOR_CST:
7842 /* Drop the overflow flag on constants, we do not want
7843 that in the GIMPLE IL. */
7844 if (TREE_OVERFLOW_P (*expr_p))
7845 *expr_p = drop_tree_overflow (*expr_p);
7846 ret = GS_ALL_DONE;
7847 break;
7849 case CONST_DECL:
7850 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7851 CONST_DECL node. Otherwise the decl is replaceable by its
7852 value. */
7853 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7854 if (fallback & fb_lvalue)
7855 ret = GS_ALL_DONE;
7856 else
7858 *expr_p = DECL_INITIAL (*expr_p);
7859 ret = GS_OK;
7861 break;
7863 case DECL_EXPR:
7864 ret = gimplify_decl_expr (expr_p, pre_p);
7865 break;
7867 case BIND_EXPR:
7868 ret = gimplify_bind_expr (expr_p, pre_p);
7869 break;
7871 case LOOP_EXPR:
7872 ret = gimplify_loop_expr (expr_p, pre_p);
7873 break;
7875 case SWITCH_EXPR:
7876 ret = gimplify_switch_expr (expr_p, pre_p);
7877 break;
7879 case EXIT_EXPR:
7880 ret = gimplify_exit_expr (expr_p);
7881 break;
7883 case GOTO_EXPR:
7884 /* If the target is not LABEL, then it is a computed jump
7885 and the target needs to be gimplified. */
7886 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7888 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7889 NULL, is_gimple_val, fb_rvalue);
7890 if (ret == GS_ERROR)
7891 break;
7893 gimplify_seq_add_stmt (pre_p,
7894 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7895 ret = GS_ALL_DONE;
7896 break;
7898 case PREDICT_EXPR:
7899 gimplify_seq_add_stmt (pre_p,
7900 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7901 PREDICT_EXPR_OUTCOME (*expr_p)));
7902 ret = GS_ALL_DONE;
7903 break;
7905 case LABEL_EXPR:
7906 ret = GS_ALL_DONE;
7907 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7908 == current_function_decl);
7909 gimplify_seq_add_stmt (pre_p,
7910 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7911 break;
7913 case CASE_LABEL_EXPR:
7914 ret = gimplify_case_label_expr (expr_p, pre_p);
7915 break;
7917 case RETURN_EXPR:
7918 ret = gimplify_return_expr (*expr_p, pre_p);
7919 break;
7921 case CONSTRUCTOR:
7922 /* Don't reduce this in place; let gimplify_init_constructor work its
7923 magic. Buf if we're just elaborating this for side effects, just
7924 gimplify any element that has side-effects. */
7925 if (fallback == fb_none)
7927 unsigned HOST_WIDE_INT ix;
7928 tree val;
7929 tree temp = NULL_TREE;
7930 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7931 if (TREE_SIDE_EFFECTS (val))
7932 append_to_statement_list (val, &temp);
7934 *expr_p = temp;
7935 ret = temp ? GS_OK : GS_ALL_DONE;
7937 /* C99 code may assign to an array in a constructed
7938 structure or union, and this has undefined behavior only
7939 on execution, so create a temporary if an lvalue is
7940 required. */
7941 else if (fallback == fb_lvalue)
7943 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7944 mark_addressable (*expr_p);
7945 ret = GS_OK;
7947 else
7948 ret = GS_ALL_DONE;
7949 break;
7951 /* The following are special cases that are not handled by the
7952 original GIMPLE grammar. */
7954 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7955 eliminated. */
7956 case SAVE_EXPR:
7957 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7958 break;
7960 case BIT_FIELD_REF:
7961 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7962 post_p, is_gimple_lvalue, fb_either);
7963 recalculate_side_effects (*expr_p);
7964 break;
7966 case TARGET_MEM_REF:
7968 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7970 if (TMR_BASE (*expr_p))
7971 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7972 post_p, is_gimple_mem_ref_addr, fb_either);
7973 if (TMR_INDEX (*expr_p))
7974 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7975 post_p, is_gimple_val, fb_rvalue);
7976 if (TMR_INDEX2 (*expr_p))
7977 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7978 post_p, is_gimple_val, fb_rvalue);
7979 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7980 ret = MIN (r0, r1);
7982 break;
7984 case NON_LVALUE_EXPR:
7985 /* This should have been stripped above. */
7986 gcc_unreachable ();
7988 case ASM_EXPR:
7989 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7990 break;
7992 case TRY_FINALLY_EXPR:
7993 case TRY_CATCH_EXPR:
7995 gimple_seq eval, cleanup;
7996 gimple_try try_;
7998 /* Calls to destructors are generated automatically in FINALLY/CATCH
7999 block. They should have location as UNKNOWN_LOCATION. However,
8000 gimplify_call_expr will reset these call stmts to input_location
8001 if it finds stmt's location is unknown. To prevent resetting for
8002 destructors, we set the input_location to unknown.
8003 Note that this only affects the destructor calls in FINALLY/CATCH
8004 block, and will automatically reset to its original value by the
8005 end of gimplify_expr. */
8006 input_location = UNKNOWN_LOCATION;
8007 eval = cleanup = NULL;
8008 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8009 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8010 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8011 if (gimple_seq_empty_p (cleanup))
8013 gimple_seq_add_seq (pre_p, eval);
8014 ret = GS_ALL_DONE;
8015 break;
8017 try_ = gimple_build_try (eval, cleanup,
8018 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8019 ? GIMPLE_TRY_FINALLY
8020 : GIMPLE_TRY_CATCH);
8021 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8022 gimple_set_location (try_, saved_location);
8023 else
8024 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8025 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8026 gimple_try_set_catch_is_cleanup (try_,
8027 TRY_CATCH_IS_CLEANUP (*expr_p));
8028 gimplify_seq_add_stmt (pre_p, try_);
8029 ret = GS_ALL_DONE;
8030 break;
8033 case CLEANUP_POINT_EXPR:
8034 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8035 break;
8037 case TARGET_EXPR:
8038 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8039 break;
8041 case CATCH_EXPR:
8043 gimple c;
8044 gimple_seq handler = NULL;
8045 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8046 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8047 gimplify_seq_add_stmt (pre_p, c);
8048 ret = GS_ALL_DONE;
8049 break;
8052 case EH_FILTER_EXPR:
8054 gimple ehf;
8055 gimple_seq failure = NULL;
8057 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8058 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8059 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8060 gimplify_seq_add_stmt (pre_p, ehf);
8061 ret = GS_ALL_DONE;
8062 break;
8065 case OBJ_TYPE_REF:
8067 enum gimplify_status r0, r1;
8068 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8069 post_p, is_gimple_val, fb_rvalue);
8070 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8071 post_p, is_gimple_val, fb_rvalue);
8072 TREE_SIDE_EFFECTS (*expr_p) = 0;
8073 ret = MIN (r0, r1);
8075 break;
8077 case LABEL_DECL:
8078 /* We get here when taking the address of a label. We mark
8079 the label as "forced"; meaning it can never be removed and
8080 it is a potential target for any computed goto. */
8081 FORCED_LABEL (*expr_p) = 1;
8082 ret = GS_ALL_DONE;
8083 break;
8085 case STATEMENT_LIST:
8086 ret = gimplify_statement_list (expr_p, pre_p);
8087 break;
8089 case WITH_SIZE_EXPR:
8091 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8092 post_p == &internal_post ? NULL : post_p,
8093 gimple_test_f, fallback);
8094 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8095 is_gimple_val, fb_rvalue);
8096 ret = GS_ALL_DONE;
8098 break;
8100 case VAR_DECL:
8101 case PARM_DECL:
8102 ret = gimplify_var_or_parm_decl (expr_p);
8103 break;
8105 case RESULT_DECL:
8106 /* When within an OpenMP context, notice uses of variables. */
8107 if (gimplify_omp_ctxp)
8108 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8109 ret = GS_ALL_DONE;
8110 break;
8112 case SSA_NAME:
8113 /* Allow callbacks into the gimplifier during optimization. */
8114 ret = GS_ALL_DONE;
8115 break;
8117 case OMP_PARALLEL:
8118 gimplify_omp_parallel (expr_p, pre_p);
8119 ret = GS_ALL_DONE;
8120 break;
8122 case OMP_TASK:
8123 gimplify_omp_task (expr_p, pre_p);
8124 ret = GS_ALL_DONE;
8125 break;
8127 case OMP_FOR:
8128 case OMP_SIMD:
8129 case CILK_SIMD:
8130 case CILK_FOR:
8131 case OMP_DISTRIBUTE:
8132 ret = gimplify_omp_for (expr_p, pre_p);
8133 break;
8135 case OMP_SECTIONS:
8136 case OMP_SINGLE:
8137 case OMP_TARGET:
8138 case OMP_TARGET_DATA:
8139 case OMP_TEAMS:
8140 gimplify_omp_workshare (expr_p, pre_p);
8141 ret = GS_ALL_DONE;
8142 break;
8144 case OMP_TARGET_UPDATE:
8145 gimplify_omp_target_update (expr_p, pre_p);
8146 ret = GS_ALL_DONE;
8147 break;
8149 case OMP_SECTION:
8150 case OMP_MASTER:
8151 case OMP_TASKGROUP:
8152 case OMP_ORDERED:
8153 case OMP_CRITICAL:
8155 gimple_seq body = NULL;
8156 gimple g;
8158 gimplify_and_add (OMP_BODY (*expr_p), &body);
8159 switch (TREE_CODE (*expr_p))
8161 case OMP_SECTION:
8162 g = gimple_build_omp_section (body);
8163 break;
8164 case OMP_MASTER:
8165 g = gimple_build_omp_master (body);
8166 break;
8167 case OMP_TASKGROUP:
8169 gimple_seq cleanup = NULL;
8170 tree fn
8171 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8172 g = gimple_build_call (fn, 0);
8173 gimple_seq_add_stmt (&cleanup, g);
8174 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8175 body = NULL;
8176 gimple_seq_add_stmt (&body, g);
8177 g = gimple_build_omp_taskgroup (body);
8179 break;
8180 case OMP_ORDERED:
8181 g = gimple_build_omp_ordered (body);
8182 break;
8183 case OMP_CRITICAL:
8184 g = gimple_build_omp_critical (body,
8185 OMP_CRITICAL_NAME (*expr_p));
8186 break;
8187 default:
8188 gcc_unreachable ();
8190 gimplify_seq_add_stmt (pre_p, g);
8191 ret = GS_ALL_DONE;
8192 break;
8195 case OMP_ATOMIC:
8196 case OMP_ATOMIC_READ:
8197 case OMP_ATOMIC_CAPTURE_OLD:
8198 case OMP_ATOMIC_CAPTURE_NEW:
8199 ret = gimplify_omp_atomic (expr_p, pre_p);
8200 break;
8202 case TRANSACTION_EXPR:
8203 ret = gimplify_transaction (expr_p, pre_p);
8204 break;
8206 case TRUTH_AND_EXPR:
8207 case TRUTH_OR_EXPR:
8208 case TRUTH_XOR_EXPR:
8210 tree orig_type = TREE_TYPE (*expr_p);
8211 tree new_type, xop0, xop1;
8212 *expr_p = gimple_boolify (*expr_p);
8213 new_type = TREE_TYPE (*expr_p);
8214 if (!useless_type_conversion_p (orig_type, new_type))
8216 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8217 ret = GS_OK;
8218 break;
8221 /* Boolified binary truth expressions are semantically equivalent
8222 to bitwise binary expressions. Canonicalize them to the
8223 bitwise variant. */
8224 switch (TREE_CODE (*expr_p))
8226 case TRUTH_AND_EXPR:
8227 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8228 break;
8229 case TRUTH_OR_EXPR:
8230 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8231 break;
8232 case TRUTH_XOR_EXPR:
8233 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8234 break;
8235 default:
8236 break;
8238 /* Now make sure that operands have compatible type to
8239 expression's new_type. */
8240 xop0 = TREE_OPERAND (*expr_p, 0);
8241 xop1 = TREE_OPERAND (*expr_p, 1);
8242 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8243 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8244 new_type,
8245 xop0);
8246 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8247 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8248 new_type,
8249 xop1);
8250 /* Continue classified as tcc_binary. */
8251 goto expr_2;
8254 case FMA_EXPR:
8255 case VEC_COND_EXPR:
8256 case VEC_PERM_EXPR:
8257 /* Classified as tcc_expression. */
8258 goto expr_3;
8260 case POINTER_PLUS_EXPR:
8262 enum gimplify_status r0, r1;
8263 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8264 post_p, is_gimple_val, fb_rvalue);
8265 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8266 post_p, is_gimple_val, fb_rvalue);
8267 recalculate_side_effects (*expr_p);
8268 ret = MIN (r0, r1);
8269 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8270 after gimplifying operands - this is similar to how
8271 it would be folding all gimplified stmts on creation
8272 to have them canonicalized, which is what we eventually
8273 should do anyway. */
8274 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8275 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8277 *expr_p = build_fold_addr_expr_with_type_loc
8278 (input_location,
8279 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8280 TREE_OPERAND (*expr_p, 0),
8281 fold_convert (ptr_type_node,
8282 TREE_OPERAND (*expr_p, 1))),
8283 TREE_TYPE (*expr_p));
8284 ret = MIN (ret, GS_OK);
8286 break;
8289 case CILK_SYNC_STMT:
8291 if (!fn_contains_cilk_spawn_p (cfun))
8293 error_at (EXPR_LOCATION (*expr_p),
8294 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8295 ret = GS_ERROR;
8297 else
8299 gimplify_cilk_sync (expr_p, pre_p);
8300 ret = GS_ALL_DONE;
8302 break;
8305 default:
8306 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8308 case tcc_comparison:
8309 /* Handle comparison of objects of non scalar mode aggregates
8310 with a call to memcmp. It would be nice to only have to do
8311 this for variable-sized objects, but then we'd have to allow
8312 the same nest of reference nodes we allow for MODIFY_EXPR and
8313 that's too complex.
8315 Compare scalar mode aggregates as scalar mode values. Using
8316 memcmp for them would be very inefficient at best, and is
8317 plain wrong if bitfields are involved. */
8319 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8321 /* Vector comparisons need no boolification. */
8322 if (TREE_CODE (type) == VECTOR_TYPE)
8323 goto expr_2;
8324 else if (!AGGREGATE_TYPE_P (type))
8326 tree org_type = TREE_TYPE (*expr_p);
8327 *expr_p = gimple_boolify (*expr_p);
8328 if (!useless_type_conversion_p (org_type,
8329 TREE_TYPE (*expr_p)))
8331 *expr_p = fold_convert_loc (input_location,
8332 org_type, *expr_p);
8333 ret = GS_OK;
8335 else
8336 goto expr_2;
8338 else if (TYPE_MODE (type) != BLKmode)
8339 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8340 else
8341 ret = gimplify_variable_sized_compare (expr_p);
8343 break;
8346 /* If *EXPR_P does not need to be special-cased, handle it
8347 according to its class. */
8348 case tcc_unary:
8349 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8350 post_p, is_gimple_val, fb_rvalue);
8351 break;
8353 case tcc_binary:
8354 expr_2:
8356 enum gimplify_status r0, r1;
8358 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8359 post_p, is_gimple_val, fb_rvalue);
8360 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8361 post_p, is_gimple_val, fb_rvalue);
8363 ret = MIN (r0, r1);
8364 break;
8367 expr_3:
8369 enum gimplify_status r0, r1, r2;
8371 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8372 post_p, is_gimple_val, fb_rvalue);
8373 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8374 post_p, is_gimple_val, fb_rvalue);
8375 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8376 post_p, is_gimple_val, fb_rvalue);
8378 ret = MIN (MIN (r0, r1), r2);
8379 break;
8382 case tcc_declaration:
8383 case tcc_constant:
8384 ret = GS_ALL_DONE;
8385 goto dont_recalculate;
8387 default:
8388 gcc_unreachable ();
8391 recalculate_side_effects (*expr_p);
8393 dont_recalculate:
8394 break;
8397 gcc_assert (*expr_p || ret != GS_OK);
8399 while (ret == GS_OK);
8401 /* If we encountered an error_mark somewhere nested inside, either
8402 stub out the statement or propagate the error back out. */
8403 if (ret == GS_ERROR)
8405 if (is_statement)
8406 *expr_p = NULL;
8407 goto out;
8410 /* This was only valid as a return value from the langhook, which
8411 we handled. Make sure it doesn't escape from any other context. */
8412 gcc_assert (ret != GS_UNHANDLED);
8414 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8416 /* We aren't looking for a value, and we don't have a valid
8417 statement. If it doesn't have side-effects, throw it away. */
8418 if (!TREE_SIDE_EFFECTS (*expr_p))
8419 *expr_p = NULL;
8420 else if (!TREE_THIS_VOLATILE (*expr_p))
8422 /* This is probably a _REF that contains something nested that
8423 has side effects. Recurse through the operands to find it. */
8424 enum tree_code code = TREE_CODE (*expr_p);
8426 switch (code)
8428 case COMPONENT_REF:
8429 case REALPART_EXPR:
8430 case IMAGPART_EXPR:
8431 case VIEW_CONVERT_EXPR:
8432 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8433 gimple_test_f, fallback);
8434 break;
8436 case ARRAY_REF:
8437 case ARRAY_RANGE_REF:
8438 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8439 gimple_test_f, fallback);
8440 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8441 gimple_test_f, fallback);
8442 break;
8444 default:
8445 /* Anything else with side-effects must be converted to
8446 a valid statement before we get here. */
8447 gcc_unreachable ();
8450 *expr_p = NULL;
8452 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8453 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8455 /* Historically, the compiler has treated a bare reference
8456 to a non-BLKmode volatile lvalue as forcing a load. */
8457 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8459 /* Normally, we do not want to create a temporary for a
8460 TREE_ADDRESSABLE type because such a type should not be
8461 copied by bitwise-assignment. However, we make an
8462 exception here, as all we are doing here is ensuring that
8463 we read the bytes that make up the type. We use
8464 create_tmp_var_raw because create_tmp_var will abort when
8465 given a TREE_ADDRESSABLE type. */
8466 tree tmp = create_tmp_var_raw (type, "vol");
8467 gimple_add_tmp_var (tmp);
8468 gimplify_assign (tmp, *expr_p, pre_p);
8469 *expr_p = NULL;
8471 else
8472 /* We can't do anything useful with a volatile reference to
8473 an incomplete type, so just throw it away. Likewise for
8474 a BLKmode type, since any implicit inner load should
8475 already have been turned into an explicit one by the
8476 gimplification process. */
8477 *expr_p = NULL;
8480 /* If we are gimplifying at the statement level, we're done. Tack
8481 everything together and return. */
8482 if (fallback == fb_none || is_statement)
8484 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8485 it out for GC to reclaim it. */
8486 *expr_p = NULL_TREE;
8488 if (!gimple_seq_empty_p (internal_pre)
8489 || !gimple_seq_empty_p (internal_post))
8491 gimplify_seq_add_seq (&internal_pre, internal_post);
8492 gimplify_seq_add_seq (pre_p, internal_pre);
8495 /* The result of gimplifying *EXPR_P is going to be the last few
8496 statements in *PRE_P and *POST_P. Add location information
8497 to all the statements that were added by the gimplification
8498 helpers. */
8499 if (!gimple_seq_empty_p (*pre_p))
8500 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8502 if (!gimple_seq_empty_p (*post_p))
8503 annotate_all_with_location_after (*post_p, post_last_gsi,
8504 input_location);
8506 goto out;
8509 #ifdef ENABLE_GIMPLE_CHECKING
8510 if (*expr_p)
8512 enum tree_code code = TREE_CODE (*expr_p);
8513 /* These expressions should already be in gimple IR form. */
8514 gcc_assert (code != MODIFY_EXPR
8515 && code != ASM_EXPR
8516 && code != BIND_EXPR
8517 && code != CATCH_EXPR
8518 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8519 && code != EH_FILTER_EXPR
8520 && code != GOTO_EXPR
8521 && code != LABEL_EXPR
8522 && code != LOOP_EXPR
8523 && code != SWITCH_EXPR
8524 && code != TRY_FINALLY_EXPR
8525 && code != OMP_CRITICAL
8526 && code != OMP_FOR
8527 && code != OMP_MASTER
8528 && code != OMP_TASKGROUP
8529 && code != OMP_ORDERED
8530 && code != OMP_PARALLEL
8531 && code != OMP_SECTIONS
8532 && code != OMP_SECTION
8533 && code != OMP_SINGLE);
8535 #endif
8537 /* Otherwise we're gimplifying a subexpression, so the resulting
8538 value is interesting. If it's a valid operand that matches
8539 GIMPLE_TEST_F, we're done. Unless we are handling some
8540 post-effects internally; if that's the case, we need to copy into
8541 a temporary before adding the post-effects to POST_P. */
8542 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8543 goto out;
8545 /* Otherwise, we need to create a new temporary for the gimplified
8546 expression. */
8548 /* We can't return an lvalue if we have an internal postqueue. The
8549 object the lvalue refers to would (probably) be modified by the
8550 postqueue; we need to copy the value out first, which means an
8551 rvalue. */
8552 if ((fallback & fb_lvalue)
8553 && gimple_seq_empty_p (internal_post)
8554 && is_gimple_addressable (*expr_p))
8556 /* An lvalue will do. Take the address of the expression, store it
8557 in a temporary, and replace the expression with an INDIRECT_REF of
8558 that temporary. */
8559 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8560 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8561 *expr_p = build_simple_mem_ref (tmp);
8563 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8565 /* An rvalue will do. Assign the gimplified expression into a
8566 new temporary TMP and replace the original expression with
8567 TMP. First, make sure that the expression has a type so that
8568 it can be assigned into a temporary. */
8569 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8570 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8572 else
8574 #ifdef ENABLE_GIMPLE_CHECKING
8575 if (!(fallback & fb_mayfail))
8577 fprintf (stderr, "gimplification failed:\n");
8578 print_generic_expr (stderr, *expr_p, 0);
8579 debug_tree (*expr_p);
8580 internal_error ("gimplification failed");
8582 #endif
8583 gcc_assert (fallback & fb_mayfail);
8585 /* If this is an asm statement, and the user asked for the
8586 impossible, don't die. Fail and let gimplify_asm_expr
8587 issue an error. */
8588 ret = GS_ERROR;
8589 goto out;
8592 /* Make sure the temporary matches our predicate. */
8593 gcc_assert ((*gimple_test_f) (*expr_p));
8595 if (!gimple_seq_empty_p (internal_post))
8597 annotate_all_with_location (internal_post, input_location);
8598 gimplify_seq_add_seq (pre_p, internal_post);
8601 out:
8602 input_location = saved_location;
8603 return ret;
8606 /* Look through TYPE for variable-sized objects and gimplify each such
8607 size that we find. Add to LIST_P any statements generated. */
8609 void
8610 gimplify_type_sizes (tree type, gimple_seq *list_p)
8612 tree field, t;
8614 if (type == NULL || type == error_mark_node)
8615 return;
8617 /* We first do the main variant, then copy into any other variants. */
8618 type = TYPE_MAIN_VARIANT (type);
8620 /* Avoid infinite recursion. */
8621 if (TYPE_SIZES_GIMPLIFIED (type))
8622 return;
8624 TYPE_SIZES_GIMPLIFIED (type) = 1;
8626 switch (TREE_CODE (type))
8628 case INTEGER_TYPE:
8629 case ENUMERAL_TYPE:
8630 case BOOLEAN_TYPE:
8631 case REAL_TYPE:
8632 case FIXED_POINT_TYPE:
8633 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8634 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8636 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8638 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8639 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8641 break;
8643 case ARRAY_TYPE:
8644 /* These types may not have declarations, so handle them here. */
8645 gimplify_type_sizes (TREE_TYPE (type), list_p);
8646 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8647 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8648 with assigned stack slots, for -O1+ -g they should be tracked
8649 by VTA. */
8650 if (!(TYPE_NAME (type)
8651 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8652 && DECL_IGNORED_P (TYPE_NAME (type)))
8653 && TYPE_DOMAIN (type)
8654 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8656 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8657 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8658 DECL_IGNORED_P (t) = 0;
8659 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8660 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8661 DECL_IGNORED_P (t) = 0;
8663 break;
8665 case RECORD_TYPE:
8666 case UNION_TYPE:
8667 case QUAL_UNION_TYPE:
8668 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8669 if (TREE_CODE (field) == FIELD_DECL)
8671 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8672 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8673 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8674 gimplify_type_sizes (TREE_TYPE (field), list_p);
8676 break;
8678 case POINTER_TYPE:
8679 case REFERENCE_TYPE:
8680 /* We used to recurse on the pointed-to type here, which turned out to
8681 be incorrect because its definition might refer to variables not
8682 yet initialized at this point if a forward declaration is involved.
8684 It was actually useful for anonymous pointed-to types to ensure
8685 that the sizes evaluation dominates every possible later use of the
8686 values. Restricting to such types here would be safe since there
8687 is no possible forward declaration around, but would introduce an
8688 undesirable middle-end semantic to anonymity. We then defer to
8689 front-ends the responsibility of ensuring that the sizes are
8690 evaluated both early and late enough, e.g. by attaching artificial
8691 type declarations to the tree. */
8692 break;
8694 default:
8695 break;
8698 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8699 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8701 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8703 TYPE_SIZE (t) = TYPE_SIZE (type);
8704 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8705 TYPE_SIZES_GIMPLIFIED (t) = 1;
8709 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8710 a size or position, has had all of its SAVE_EXPRs evaluated.
8711 We add any required statements to *STMT_P. */
8713 void
8714 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8716 tree expr = *expr_p;
8718 /* We don't do anything if the value isn't there, is constant, or contains
8719 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8720 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8721 will want to replace it with a new variable, but that will cause problems
8722 if this type is from outside the function. It's OK to have that here. */
8723 if (is_gimple_sizepos (expr))
8724 return;
8726 *expr_p = unshare_expr (expr);
8728 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8731 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8732 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8733 is true, also gimplify the parameters. */
8735 gimple_bind
8736 gimplify_body (tree fndecl, bool do_parms)
8738 location_t saved_location = input_location;
8739 gimple_seq parm_stmts, seq;
8740 gimple outer_stmt;
8741 gimple_bind outer_bind;
8742 struct cgraph_node *cgn;
8744 timevar_push (TV_TREE_GIMPLIFY);
8746 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8747 gimplification. */
8748 default_rtl_profile ();
8750 gcc_assert (gimplify_ctxp == NULL);
8751 push_gimplify_context ();
8753 if (flag_openmp)
8755 gcc_assert (gimplify_omp_ctxp == NULL);
8756 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8757 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8760 /* Unshare most shared trees in the body and in that of any nested functions.
8761 It would seem we don't have to do this for nested functions because
8762 they are supposed to be output and then the outer function gimplified
8763 first, but the g++ front end doesn't always do it that way. */
8764 unshare_body (fndecl);
8765 unvisit_body (fndecl);
8767 cgn = cgraph_node::get (fndecl);
8768 if (cgn && cgn->origin)
8769 nonlocal_vlas = new hash_set<tree>;
8771 /* Make sure input_location isn't set to something weird. */
8772 input_location = DECL_SOURCE_LOCATION (fndecl);
8774 /* Resolve callee-copies. This has to be done before processing
8775 the body so that DECL_VALUE_EXPR gets processed correctly. */
8776 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8778 /* Gimplify the function's body. */
8779 seq = NULL;
8780 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8781 outer_stmt = gimple_seq_first_stmt (seq);
8782 if (!outer_stmt)
8784 outer_stmt = gimple_build_nop ();
8785 gimplify_seq_add_stmt (&seq, outer_stmt);
8788 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8789 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8790 if (gimple_code (outer_stmt) == GIMPLE_BIND
8791 && gimple_seq_first (seq) == gimple_seq_last (seq))
8792 outer_bind = as_a <gimple_bind> (outer_stmt);
8793 else
8794 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8796 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8798 /* If we had callee-copies statements, insert them at the beginning
8799 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8800 if (!gimple_seq_empty_p (parm_stmts))
8802 tree parm;
8804 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8805 gimple_bind_set_body (outer_bind, parm_stmts);
8807 for (parm = DECL_ARGUMENTS (current_function_decl);
8808 parm; parm = DECL_CHAIN (parm))
8809 if (DECL_HAS_VALUE_EXPR_P (parm))
8811 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8812 DECL_IGNORED_P (parm) = 0;
8816 if (nonlocal_vlas)
8818 if (nonlocal_vla_vars)
8820 /* tree-nested.c may later on call declare_vars (..., true);
8821 which relies on BLOCK_VARS chain to be the tail of the
8822 gimple_bind_vars chain. Ensure we don't violate that
8823 assumption. */
8824 if (gimple_bind_block (outer_bind)
8825 == DECL_INITIAL (current_function_decl))
8826 declare_vars (nonlocal_vla_vars, outer_bind, true);
8827 else
8828 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8829 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8830 nonlocal_vla_vars);
8831 nonlocal_vla_vars = NULL_TREE;
8833 delete nonlocal_vlas;
8834 nonlocal_vlas = NULL;
8837 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8839 delete_omp_context (gimplify_omp_ctxp);
8840 gimplify_omp_ctxp = NULL;
8843 pop_gimplify_context (outer_bind);
8844 gcc_assert (gimplify_ctxp == NULL);
8846 #ifdef ENABLE_CHECKING
8847 if (!seen_error ())
8848 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8849 #endif
8851 timevar_pop (TV_TREE_GIMPLIFY);
8852 input_location = saved_location;
8854 return outer_bind;
8857 typedef char *char_p; /* For DEF_VEC_P. */
8859 /* Return whether we should exclude FNDECL from instrumentation. */
8861 static bool
8862 flag_instrument_functions_exclude_p (tree fndecl)
8864 vec<char_p> *v;
8866 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8867 if (v && v->length () > 0)
8869 const char *name;
8870 int i;
8871 char *s;
8873 name = lang_hooks.decl_printable_name (fndecl, 0);
8874 FOR_EACH_VEC_ELT (*v, i, s)
8875 if (strstr (name, s) != NULL)
8876 return true;
8879 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8880 if (v && v->length () > 0)
8882 const char *name;
8883 int i;
8884 char *s;
8886 name = DECL_SOURCE_FILE (fndecl);
8887 FOR_EACH_VEC_ELT (*v, i, s)
8888 if (strstr (name, s) != NULL)
8889 return true;
8892 return false;
8895 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8896 node for the function we want to gimplify.
8898 Return the sequence of GIMPLE statements corresponding to the body
8899 of FNDECL. */
8901 void
8902 gimplify_function_tree (tree fndecl)
8904 tree parm, ret;
8905 gimple_seq seq;
8906 gimple_bind bind;
8908 gcc_assert (!gimple_body (fndecl));
8910 if (DECL_STRUCT_FUNCTION (fndecl))
8911 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8912 else
8913 push_struct_function (fndecl);
8915 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8917 /* Preliminarily mark non-addressed complex variables as eligible
8918 for promotion to gimple registers. We'll transform their uses
8919 as we find them. */
8920 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8921 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8922 && !TREE_THIS_VOLATILE (parm)
8923 && !needs_to_live_in_memory (parm))
8924 DECL_GIMPLE_REG_P (parm) = 1;
8927 ret = DECL_RESULT (fndecl);
8928 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8929 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8930 && !needs_to_live_in_memory (ret))
8931 DECL_GIMPLE_REG_P (ret) = 1;
8933 bind = gimplify_body (fndecl, true);
8935 /* The tree body of the function is no longer needed, replace it
8936 with the new GIMPLE body. */
8937 seq = NULL;
8938 gimple_seq_add_stmt (&seq, bind);
8939 gimple_set_body (fndecl, seq);
8941 /* If we're instrumenting function entry/exit, then prepend the call to
8942 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8943 catch the exit hook. */
8944 /* ??? Add some way to ignore exceptions for this TFE. */
8945 if (flag_instrument_function_entry_exit
8946 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8947 && !flag_instrument_functions_exclude_p (fndecl))
8949 tree x;
8950 gimple_bind new_bind;
8951 gimple tf;
8952 gimple_seq cleanup = NULL, body = NULL;
8953 tree tmp_var;
8954 gimple_call call;
8956 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8957 call = gimple_build_call (x, 1, integer_zero_node);
8958 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8959 gimple_call_set_lhs (call, tmp_var);
8960 gimplify_seq_add_stmt (&cleanup, call);
8961 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8962 call = gimple_build_call (x, 2,
8963 build_fold_addr_expr (current_function_decl),
8964 tmp_var);
8965 gimplify_seq_add_stmt (&cleanup, call);
8966 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8968 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8969 call = gimple_build_call (x, 1, integer_zero_node);
8970 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8971 gimple_call_set_lhs (call, tmp_var);
8972 gimplify_seq_add_stmt (&body, call);
8973 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8974 call = gimple_build_call (x, 2,
8975 build_fold_addr_expr (current_function_decl),
8976 tmp_var);
8977 gimplify_seq_add_stmt (&body, call);
8978 gimplify_seq_add_stmt (&body, tf);
8979 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8980 /* Clear the block for BIND, since it is no longer directly inside
8981 the function, but within a try block. */
8982 gimple_bind_set_block (bind, NULL);
8984 /* Replace the current function body with the body
8985 wrapped in the try/finally TF. */
8986 seq = NULL;
8987 gimple_seq_add_stmt (&seq, new_bind);
8988 gimple_set_body (fndecl, seq);
8991 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8992 cfun->curr_properties = PROP_gimple_any;
8994 pop_cfun ();
8997 /* Return a dummy expression of type TYPE in order to keep going after an
8998 error. */
9000 static tree
9001 dummy_object (tree type)
9003 tree t = build_int_cst (build_pointer_type (type), 0);
9004 return build2 (MEM_REF, type, t, t);
9007 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9008 builtin function, but a very special sort of operator. */
9010 enum gimplify_status
9011 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9013 tree promoted_type, have_va_type;
9014 tree valist = TREE_OPERAND (*expr_p, 0);
9015 tree type = TREE_TYPE (*expr_p);
9016 tree t;
9017 location_t loc = EXPR_LOCATION (*expr_p);
9019 /* Verify that valist is of the proper type. */
9020 have_va_type = TREE_TYPE (valist);
9021 if (have_va_type == error_mark_node)
9022 return GS_ERROR;
9023 have_va_type = targetm.canonical_va_list_type (have_va_type);
9025 if (have_va_type == NULL_TREE)
9027 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9028 return GS_ERROR;
9031 /* Generate a diagnostic for requesting data of a type that cannot
9032 be passed through `...' due to type promotion at the call site. */
9033 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9034 != type)
9036 static bool gave_help;
9037 bool warned;
9039 /* Unfortunately, this is merely undefined, rather than a constraint
9040 violation, so we cannot make this an error. If this call is never
9041 executed, the program is still strictly conforming. */
9042 warned = warning_at (loc, 0,
9043 "%qT is promoted to %qT when passed through %<...%>",
9044 type, promoted_type);
9045 if (!gave_help && warned)
9047 gave_help = true;
9048 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9049 promoted_type, type);
9052 /* We can, however, treat "undefined" any way we please.
9053 Call abort to encourage the user to fix the program. */
9054 if (warned)
9055 inform (loc, "if this code is reached, the program will abort");
9056 /* Before the abort, allow the evaluation of the va_list
9057 expression to exit or longjmp. */
9058 gimplify_and_add (valist, pre_p);
9059 t = build_call_expr_loc (loc,
9060 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9061 gimplify_and_add (t, pre_p);
9063 /* This is dead code, but go ahead and finish so that the
9064 mode of the result comes out right. */
9065 *expr_p = dummy_object (type);
9066 return GS_ALL_DONE;
9068 else
9070 /* Make it easier for the backends by protecting the valist argument
9071 from multiple evaluations. */
9072 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9074 /* For this case, the backends will be expecting a pointer to
9075 TREE_TYPE (abi), but it's possible we've
9076 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9077 So fix it. */
9078 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9080 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9081 valist = fold_convert_loc (loc, p1,
9082 build_fold_addr_expr_loc (loc, valist));
9085 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9087 else
9088 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9090 if (!targetm.gimplify_va_arg_expr)
9091 /* FIXME: Once most targets are converted we should merely
9092 assert this is non-null. */
9093 return GS_ALL_DONE;
9095 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9096 return GS_OK;
9100 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9102 DST/SRC are the destination and source respectively. You can pass
9103 ungimplified trees in DST or SRC, in which case they will be
9104 converted to a gimple operand if necessary.
9106 This function returns the newly created GIMPLE_ASSIGN tuple. */
9108 gimple
9109 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9111 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9112 gimplify_and_add (t, seq_p);
9113 ggc_free (t);
9114 return gimple_seq_last_stmt (*seq_p);
9117 inline hashval_t
9118 gimplify_hasher::hash (const value_type *p)
9120 tree t = p->val;
9121 return iterative_hash_expr (t, 0);
9124 inline bool
9125 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9127 tree t1 = p1->val;
9128 tree t2 = p2->val;
9129 enum tree_code code = TREE_CODE (t1);
9131 if (TREE_CODE (t2) != code
9132 || TREE_TYPE (t1) != TREE_TYPE (t2))
9133 return false;
9135 if (!operand_equal_p (t1, t2, 0))
9136 return false;
9138 #ifdef ENABLE_CHECKING
9139 /* Only allow them to compare equal if they also hash equal; otherwise
9140 results are nondeterminate, and we fail bootstrap comparison. */
9141 gcc_assert (hash (p1) == hash (p2));
9142 #endif
9144 return true;