OpenACC acc_on_device.
[official-gcc.git] / gcc / gimplify.c
blob2319fc3c2113caa2a198e17e925db713eb135ca5
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,
83 /* Flags for GOVD_MAP. */
84 /* Don't copy back. */
85 GOVD_MAP_TO_ONLY = 8192,
86 /* Force a specific behavior (or else, a run-time error). */
87 GOVD_MAP_FORCE = 16384,
89 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
90 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
91 | GOVD_LOCAL)
95 enum omp_region_type
97 ORT_WORKSHARE = 0,
98 ORT_SIMD = 1,
99 ORT_PARALLEL = 2,
100 ORT_COMBINED_PARALLEL = 3,
101 ORT_TASK = 4,
102 ORT_UNTIED_TASK = 5,
103 ORT_TEAMS = 8,
104 ORT_TARGET = 16,
106 /* Flags for ORT_TARGET. */
107 /* Prepare this region for offloading. */
108 ORT_TARGET_OFFLOAD = 32,
109 /* Default to GOVD_MAP_FORCE for implicit mappings in this region. */
110 ORT_TARGET_MAP_FORCE = 64
113 /* Gimplify hashtable helper. */
115 struct gimplify_hasher : typed_free_remove <elt_t>
117 typedef elt_t value_type;
118 typedef elt_t compare_type;
119 static inline hashval_t hash (const value_type *);
120 static inline bool equal (const value_type *, const compare_type *);
123 struct gimplify_ctx
125 struct gimplify_ctx *prev_context;
127 vec<gimple> bind_expr_stack;
128 tree temps;
129 gimple_seq conditional_cleanups;
130 tree exit_label;
131 tree return_temp;
133 vec<tree> case_labels;
134 /* The formal temporary table. Should this be persistent? */
135 hash_table<gimplify_hasher> *temp_htab;
137 int conditions;
138 bool save_stack;
139 bool into_ssa;
140 bool allow_rhs_cond_expr;
141 bool in_cleanup_point_expr;
144 struct gimplify_omp_ctx
146 struct gimplify_omp_ctx *outer_context;
147 splay_tree variables;
148 hash_set<tree> *privatized_types;
149 location_t location;
150 enum omp_clause_default_kind default_kind;
151 enum omp_region_type region_type;
152 bool combined_loop;
153 bool distribute;
156 static struct gimplify_ctx *gimplify_ctxp;
157 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
159 /* Forward declaration. */
160 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
162 /* Shorter alias name for the above function for use in gimplify.c
163 only. */
165 static inline void
166 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
168 gimple_seq_add_stmt_without_update (seq_p, gs);
171 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
172 NULL, a new sequence is allocated. This function is
173 similar to gimple_seq_add_seq, but does not scan the operands.
174 During gimplification, we need to manipulate statement sequences
175 before the def/use vectors have been constructed. */
177 static void
178 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
180 gimple_stmt_iterator si;
182 if (src == NULL)
183 return;
185 si = gsi_last (*dst_p);
186 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
190 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
191 and popping gimplify contexts. */
193 static struct gimplify_ctx *ctx_pool = NULL;
195 /* Return a gimplify context struct from the pool. */
197 static inline struct gimplify_ctx *
198 ctx_alloc (void)
200 struct gimplify_ctx * c = ctx_pool;
202 if (c)
203 ctx_pool = c->prev_context;
204 else
205 c = XNEW (struct gimplify_ctx);
207 memset (c, '\0', sizeof (*c));
208 return c;
211 /* Put gimplify context C back into the pool. */
213 static inline void
214 ctx_free (struct gimplify_ctx *c)
216 c->prev_context = ctx_pool;
217 ctx_pool = c;
220 /* Free allocated ctx stack memory. */
222 void
223 free_gimplify_stack (void)
225 struct gimplify_ctx *c;
227 while ((c = ctx_pool))
229 ctx_pool = c->prev_context;
230 free (c);
235 /* Set up a context for the gimplifier. */
237 void
238 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
240 struct gimplify_ctx *c = ctx_alloc ();
242 c->prev_context = gimplify_ctxp;
243 gimplify_ctxp = c;
244 gimplify_ctxp->into_ssa = in_ssa;
245 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
248 /* Tear down a context for the gimplifier. If BODY is non-null, then
249 put the temporaries into the outer BIND_EXPR. Otherwise, put them
250 in the local_decls.
252 BODY is not a sequence, but the first tuple in a sequence. */
254 void
255 pop_gimplify_context (gimple body)
257 struct gimplify_ctx *c = gimplify_ctxp;
259 gcc_assert (c
260 && (!c->bind_expr_stack.exists ()
261 || c->bind_expr_stack.is_empty ()));
262 c->bind_expr_stack.release ();
263 gimplify_ctxp = c->prev_context;
265 if (body)
266 declare_vars (c->temps, body, false);
267 else
268 record_vars (c->temps);
270 delete c->temp_htab;
271 c->temp_htab = NULL;
272 ctx_free (c);
275 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
277 static void
278 gimple_push_bind_expr (gimple gimple_bind)
280 gimplify_ctxp->bind_expr_stack.reserve (8);
281 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
284 /* Pop the first element off the stack of bindings. */
286 static void
287 gimple_pop_bind_expr (void)
289 gimplify_ctxp->bind_expr_stack.pop ();
292 /* Return the first element of the stack of bindings. */
294 gimple
295 gimple_current_bind_expr (void)
297 return gimplify_ctxp->bind_expr_stack.last ();
300 /* Return the stack of bindings created during gimplification. */
302 vec<gimple>
303 gimple_bind_expr_stack (void)
305 return gimplify_ctxp->bind_expr_stack;
308 /* Return true iff there is a COND_EXPR between us and the innermost
309 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
311 static bool
312 gimple_conditional_context (void)
314 return gimplify_ctxp->conditions > 0;
317 /* Note that we've entered a COND_EXPR. */
319 static void
320 gimple_push_condition (void)
322 #ifdef ENABLE_GIMPLE_CHECKING
323 if (gimplify_ctxp->conditions == 0)
324 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
325 #endif
326 ++(gimplify_ctxp->conditions);
329 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
330 now, add any conditional cleanups we've seen to the prequeue. */
332 static void
333 gimple_pop_condition (gimple_seq *pre_p)
335 int conds = --(gimplify_ctxp->conditions);
337 gcc_assert (conds >= 0);
338 if (conds == 0)
340 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
341 gimplify_ctxp->conditional_cleanups = NULL;
345 /* A stable comparison routine for use with splay trees and DECLs. */
347 static int
348 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
350 tree a = (tree) xa;
351 tree b = (tree) xb;
353 return DECL_UID (a) - DECL_UID (b);
356 /* Create a new omp construct that deals with variable remapping. */
358 static struct gimplify_omp_ctx *
359 new_omp_context (enum omp_region_type region_type)
361 struct gimplify_omp_ctx *c;
363 c = XCNEW (struct gimplify_omp_ctx);
364 c->outer_context = gimplify_omp_ctxp;
365 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
366 c->privatized_types = new hash_set<tree>;
367 c->location = input_location;
368 c->region_type = region_type;
369 if ((region_type & ORT_TASK) == 0)
370 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
371 else
372 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
374 return c;
377 /* Destroy an omp construct that deals with variable remapping. */
379 static void
380 delete_omp_context (struct gimplify_omp_ctx *c)
382 splay_tree_delete (c->variables);
383 delete c->privatized_types;
384 XDELETE (c);
387 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
388 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
390 /* Both gimplify the statement T and append it to *SEQ_P. This function
391 behaves exactly as gimplify_stmt, but you don't have to pass T as a
392 reference. */
394 void
395 gimplify_and_add (tree t, gimple_seq *seq_p)
397 gimplify_stmt (&t, seq_p);
400 /* Gimplify statement T into sequence *SEQ_P, and return the first
401 tuple in the sequence of generated tuples for this statement.
402 Return NULL if gimplifying T produced no tuples. */
404 static gimple
405 gimplify_and_return_first (tree t, gimple_seq *seq_p)
407 gimple_stmt_iterator last = gsi_last (*seq_p);
409 gimplify_and_add (t, seq_p);
411 if (!gsi_end_p (last))
413 gsi_next (&last);
414 return gsi_stmt (last);
416 else
417 return gimple_seq_first_stmt (*seq_p);
420 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
421 LHS, or for a call argument. */
423 static bool
424 is_gimple_mem_rhs (tree t)
426 /* If we're dealing with a renamable type, either source or dest must be
427 a renamed variable. */
428 if (is_gimple_reg_type (TREE_TYPE (t)))
429 return is_gimple_val (t);
430 else
431 return is_gimple_val (t) || is_gimple_lvalue (t);
434 /* Return true if T is a CALL_EXPR or an expression that can be
435 assigned to a temporary. Note that this predicate should only be
436 used during gimplification. See the rationale for this in
437 gimplify_modify_expr. */
439 static bool
440 is_gimple_reg_rhs_or_call (tree t)
442 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
443 || TREE_CODE (t) == CALL_EXPR);
446 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
447 this predicate should only be used during gimplification. See the
448 rationale for this in gimplify_modify_expr. */
450 static bool
451 is_gimple_mem_rhs_or_call (tree t)
453 /* If we're dealing with a renamable type, either source or dest must be
454 a renamed variable. */
455 if (is_gimple_reg_type (TREE_TYPE (t)))
456 return is_gimple_val (t);
457 else
458 return (is_gimple_val (t) || is_gimple_lvalue (t)
459 || TREE_CODE (t) == CALL_EXPR);
462 /* Create a temporary with a name derived from VAL. Subroutine of
463 lookup_tmp_var; nobody else should call this function. */
465 static inline tree
466 create_tmp_from_val (tree val)
468 /* Drop all qualifiers and address-space information from the value type. */
469 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
470 tree var = create_tmp_var (type, get_name (val));
471 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
472 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
473 DECL_GIMPLE_REG_P (var) = 1;
474 return var;
477 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
478 an existing expression temporary. */
480 static tree
481 lookup_tmp_var (tree val, bool is_formal)
483 tree ret;
485 /* If not optimizing, never really reuse a temporary. local-alloc
486 won't allocate any variable that is used in more than one basic
487 block, which means it will go into memory, causing much extra
488 work in reload and final and poorer code generation, outweighing
489 the extra memory allocation here. */
490 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
491 ret = create_tmp_from_val (val);
492 else
494 elt_t elt, *elt_p;
495 elt_t **slot;
497 elt.val = val;
498 if (!gimplify_ctxp->temp_htab)
499 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
500 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
501 if (*slot == NULL)
503 elt_p = XNEW (elt_t);
504 elt_p->val = val;
505 elt_p->temp = ret = create_tmp_from_val (val);
506 *slot = elt_p;
508 else
510 elt_p = *slot;
511 ret = elt_p->temp;
515 return ret;
518 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
520 static tree
521 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
522 bool is_formal)
524 tree t, mod;
526 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
527 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
528 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
529 fb_rvalue);
531 if (gimplify_ctxp->into_ssa
532 && is_gimple_reg_type (TREE_TYPE (val)))
533 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
534 else
535 t = lookup_tmp_var (val, is_formal);
537 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
539 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
541 /* gimplify_modify_expr might want to reduce this further. */
542 gimplify_and_add (mod, pre_p);
543 ggc_free (mod);
545 return t;
548 /* Return a formal temporary variable initialized with VAL. PRE_P is as
549 in gimplify_expr. Only use this function if:
551 1) The value of the unfactored expression represented by VAL will not
552 change between the initialization and use of the temporary, and
553 2) The temporary will not be otherwise modified.
555 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
556 and #2 means it is inappropriate for && temps.
558 For other cases, use get_initialized_tmp_var instead. */
560 tree
561 get_formal_tmp_var (tree val, gimple_seq *pre_p)
563 return internal_get_tmp_var (val, pre_p, NULL, true);
566 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
567 are as in gimplify_expr. */
569 tree
570 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
572 return internal_get_tmp_var (val, pre_p, post_p, false);
575 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
576 generate debug info for them; otherwise don't. */
578 void
579 declare_vars (tree vars, gimple scope, bool debug_info)
581 tree last = vars;
582 if (last)
584 tree temps, block;
586 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
588 temps = nreverse (last);
590 block = gimple_bind_block (scope);
591 gcc_assert (!block || TREE_CODE (block) == BLOCK);
592 if (!block || !debug_info)
594 DECL_CHAIN (last) = gimple_bind_vars (scope);
595 gimple_bind_set_vars (scope, temps);
597 else
599 /* We need to attach the nodes both to the BIND_EXPR and to its
600 associated BLOCK for debugging purposes. The key point here
601 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
602 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
603 if (BLOCK_VARS (block))
604 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
605 else
607 gimple_bind_set_vars (scope,
608 chainon (gimple_bind_vars (scope), temps));
609 BLOCK_VARS (block) = temps;
615 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
616 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
617 no such upper bound can be obtained. */
619 static void
620 force_constant_size (tree var)
622 /* The only attempt we make is by querying the maximum size of objects
623 of the variable's type. */
625 HOST_WIDE_INT max_size;
627 gcc_assert (TREE_CODE (var) == VAR_DECL);
629 max_size = max_int_size_in_bytes (TREE_TYPE (var));
631 gcc_assert (max_size >= 0);
633 DECL_SIZE_UNIT (var)
634 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
635 DECL_SIZE (var)
636 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
639 /* Push the temporary variable TMP into the current binding. */
641 void
642 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
644 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
646 /* Later processing assumes that the object size is constant, which might
647 not be true at this point. Force the use of a constant upper bound in
648 this case. */
649 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
650 force_constant_size (tmp);
652 DECL_CONTEXT (tmp) = fn->decl;
653 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
655 record_vars_into (tmp, fn->decl);
658 /* Push the temporary variable TMP into the current binding. */
660 void
661 gimple_add_tmp_var (tree tmp)
663 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
665 /* Later processing assumes that the object size is constant, which might
666 not be true at this point. Force the use of a constant upper bound in
667 this case. */
668 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
669 force_constant_size (tmp);
671 DECL_CONTEXT (tmp) = current_function_decl;
672 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
674 if (gimplify_ctxp)
676 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
677 gimplify_ctxp->temps = tmp;
679 /* Mark temporaries local within the nearest enclosing parallel. */
680 if (gimplify_omp_ctxp)
682 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
683 while (ctx
684 && (ctx->region_type == ORT_WORKSHARE
685 || ctx->region_type == ORT_SIMD))
686 ctx = ctx->outer_context;
687 if (ctx)
688 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
691 else if (cfun)
692 record_vars (tmp);
693 else
695 gimple_seq body_seq;
697 /* This case is for nested functions. We need to expose the locals
698 they create. */
699 body_seq = gimple_body (current_function_decl);
700 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
706 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
707 nodes that are referenced more than once in GENERIC functions. This is
708 necessary because gimplification (translation into GIMPLE) is performed
709 by modifying tree nodes in-place, so gimplication of a shared node in a
710 first context could generate an invalid GIMPLE form in a second context.
712 This is achieved with a simple mark/copy/unmark algorithm that walks the
713 GENERIC representation top-down, marks nodes with TREE_VISITED the first
714 time it encounters them, duplicates them if they already have TREE_VISITED
715 set, and finally removes the TREE_VISITED marks it has set.
717 The algorithm works only at the function level, i.e. it generates a GENERIC
718 representation of a function with no nodes shared within the function when
719 passed a GENERIC function (except for nodes that are allowed to be shared).
721 At the global level, it is also necessary to unshare tree nodes that are
722 referenced in more than one function, for the same aforementioned reason.
723 This requires some cooperation from the front-end. There are 2 strategies:
725 1. Manual unsharing. The front-end needs to call unshare_expr on every
726 expression that might end up being shared across functions.
728 2. Deep unsharing. This is an extension of regular unsharing. Instead
729 of calling unshare_expr on expressions that might be shared across
730 functions, the front-end pre-marks them with TREE_VISITED. This will
731 ensure that they are unshared on the first reference within functions
732 when the regular unsharing algorithm runs. The counterpart is that
733 this algorithm must look deeper than for manual unsharing, which is
734 specified by LANG_HOOKS_DEEP_UNSHARING.
736 If there are only few specific cases of node sharing across functions, it is
737 probably easier for a front-end to unshare the expressions manually. On the
738 contrary, if the expressions generated at the global level are as widespread
739 as expressions generated within functions, deep unsharing is very likely the
740 way to go. */
742 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
743 These nodes model computations that must be done once. If we were to
744 unshare something like SAVE_EXPR(i++), the gimplification process would
745 create wrong code. However, if DATA is non-null, it must hold a pointer
746 set that is used to unshare the subtrees of these nodes. */
748 static tree
749 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
751 tree t = *tp;
752 enum tree_code code = TREE_CODE (t);
754 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
755 copy their subtrees if we can make sure to do it only once. */
756 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
758 if (data && !((hash_set<tree> *)data)->add (t))
760 else
761 *walk_subtrees = 0;
764 /* Stop at types, decls, constants like copy_tree_r. */
765 else if (TREE_CODE_CLASS (code) == tcc_type
766 || TREE_CODE_CLASS (code) == tcc_declaration
767 || TREE_CODE_CLASS (code) == tcc_constant
768 /* We can't do anything sensible with a BLOCK used as an
769 expression, but we also can't just die when we see it
770 because of non-expression uses. So we avert our eyes
771 and cross our fingers. Silly Java. */
772 || code == BLOCK)
773 *walk_subtrees = 0;
775 /* Cope with the statement expression extension. */
776 else if (code == STATEMENT_LIST)
779 /* Leave the bulk of the work to copy_tree_r itself. */
780 else
781 copy_tree_r (tp, walk_subtrees, NULL);
783 return NULL_TREE;
786 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
787 If *TP has been visited already, then *TP is deeply copied by calling
788 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
790 static tree
791 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
793 tree t = *tp;
794 enum tree_code code = TREE_CODE (t);
796 /* Skip types, decls, and constants. But we do want to look at their
797 types and the bounds of types. Mark them as visited so we properly
798 unmark their subtrees on the unmark pass. If we've already seen them,
799 don't look down further. */
800 if (TREE_CODE_CLASS (code) == tcc_type
801 || TREE_CODE_CLASS (code) == tcc_declaration
802 || TREE_CODE_CLASS (code) == tcc_constant)
804 if (TREE_VISITED (t))
805 *walk_subtrees = 0;
806 else
807 TREE_VISITED (t) = 1;
810 /* If this node has been visited already, unshare it and don't look
811 any deeper. */
812 else if (TREE_VISITED (t))
814 walk_tree (tp, mostly_copy_tree_r, data, NULL);
815 *walk_subtrees = 0;
818 /* Otherwise, mark the node as visited and keep looking. */
819 else
820 TREE_VISITED (t) = 1;
822 return NULL_TREE;
825 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
826 copy_if_shared_r callback unmodified. */
828 static inline void
829 copy_if_shared (tree *tp, void *data)
831 walk_tree (tp, copy_if_shared_r, data, NULL);
834 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
835 any nested functions. */
837 static void
838 unshare_body (tree fndecl)
840 struct cgraph_node *cgn = cgraph_node::get (fndecl);
841 /* If the language requires deep unsharing, we need a pointer set to make
842 sure we don't repeatedly unshare subtrees of unshareable nodes. */
843 hash_set<tree> *visited
844 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
846 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
847 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
848 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
850 delete visited;
852 if (cgn)
853 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
854 unshare_body (cgn->decl);
857 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
858 Subtrees are walked until the first unvisited node is encountered. */
860 static tree
861 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
863 tree t = *tp;
865 /* If this node has been visited, unmark it and keep looking. */
866 if (TREE_VISITED (t))
867 TREE_VISITED (t) = 0;
869 /* Otherwise, don't look any deeper. */
870 else
871 *walk_subtrees = 0;
873 return NULL_TREE;
876 /* Unmark the visited trees rooted at *TP. */
878 static inline void
879 unmark_visited (tree *tp)
881 walk_tree (tp, unmark_visited_r, NULL, NULL);
884 /* Likewise, but mark all trees as not visited. */
886 static void
887 unvisit_body (tree fndecl)
889 struct cgraph_node *cgn = cgraph_node::get (fndecl);
891 unmark_visited (&DECL_SAVED_TREE (fndecl));
892 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
893 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
895 if (cgn)
896 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
897 unvisit_body (cgn->decl);
900 /* Unconditionally make an unshared copy of EXPR. This is used when using
901 stored expressions which span multiple functions, such as BINFO_VTABLE,
902 as the normal unsharing process can't tell that they're shared. */
904 tree
905 unshare_expr (tree expr)
907 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
908 return expr;
911 /* Worker for unshare_expr_without_location. */
913 static tree
914 prune_expr_location (tree *tp, int *walk_subtrees, void *)
916 if (EXPR_P (*tp))
917 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
918 else
919 *walk_subtrees = 0;
920 return NULL_TREE;
923 /* Similar to unshare_expr but also prune all expression locations
924 from EXPR. */
926 tree
927 unshare_expr_without_location (tree expr)
929 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
930 if (EXPR_P (expr))
931 walk_tree (&expr, prune_expr_location, NULL, NULL);
932 return expr;
935 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
936 contain statements and have a value. Assign its value to a temporary
937 and give it void_type_node. Return the temporary, or NULL_TREE if
938 WRAPPER was already void. */
940 tree
941 voidify_wrapper_expr (tree wrapper, tree temp)
943 tree type = TREE_TYPE (wrapper);
944 if (type && !VOID_TYPE_P (type))
946 tree *p;
948 /* Set p to point to the body of the wrapper. Loop until we find
949 something that isn't a wrapper. */
950 for (p = &wrapper; p && *p; )
952 switch (TREE_CODE (*p))
954 case BIND_EXPR:
955 TREE_SIDE_EFFECTS (*p) = 1;
956 TREE_TYPE (*p) = void_type_node;
957 /* For a BIND_EXPR, the body is operand 1. */
958 p = &BIND_EXPR_BODY (*p);
959 break;
961 case CLEANUP_POINT_EXPR:
962 case TRY_FINALLY_EXPR:
963 case TRY_CATCH_EXPR:
964 TREE_SIDE_EFFECTS (*p) = 1;
965 TREE_TYPE (*p) = void_type_node;
966 p = &TREE_OPERAND (*p, 0);
967 break;
969 case STATEMENT_LIST:
971 tree_stmt_iterator i = tsi_last (*p);
972 TREE_SIDE_EFFECTS (*p) = 1;
973 TREE_TYPE (*p) = void_type_node;
974 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
976 break;
978 case COMPOUND_EXPR:
979 /* Advance to the last statement. Set all container types to
980 void. */
981 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
983 TREE_SIDE_EFFECTS (*p) = 1;
984 TREE_TYPE (*p) = void_type_node;
986 break;
988 case TRANSACTION_EXPR:
989 TREE_SIDE_EFFECTS (*p) = 1;
990 TREE_TYPE (*p) = void_type_node;
991 p = &TRANSACTION_EXPR_BODY (*p);
992 break;
994 default:
995 /* Assume that any tree upon which voidify_wrapper_expr is
996 directly called is a wrapper, and that its body is op0. */
997 if (p == &wrapper)
999 TREE_SIDE_EFFECTS (*p) = 1;
1000 TREE_TYPE (*p) = void_type_node;
1001 p = &TREE_OPERAND (*p, 0);
1002 break;
1004 goto out;
1008 out:
1009 if (p == NULL || IS_EMPTY_STMT (*p))
1010 temp = NULL_TREE;
1011 else if (temp)
1013 /* The wrapper is on the RHS of an assignment that we're pushing
1014 down. */
1015 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1016 || TREE_CODE (temp) == MODIFY_EXPR);
1017 TREE_OPERAND (temp, 1) = *p;
1018 *p = temp;
1020 else
1022 temp = create_tmp_var (type, "retval");
1023 *p = build2 (INIT_EXPR, type, temp, *p);
1026 return temp;
1029 return NULL_TREE;
1032 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1033 a temporary through which they communicate. */
1035 static void
1036 build_stack_save_restore (gimple *save, gimple *restore)
1038 tree tmp_var;
1040 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1041 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1042 gimple_call_set_lhs (*save, tmp_var);
1044 *restore
1045 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1046 1, tmp_var);
1049 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1051 static enum gimplify_status
1052 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1054 tree bind_expr = *expr_p;
1055 bool old_save_stack = gimplify_ctxp->save_stack;
1056 tree t;
1057 gimple gimple_bind;
1058 gimple_seq body, cleanup;
1059 gimple stack_save;
1060 location_t start_locus = 0, end_locus = 0;
1062 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1064 /* Mark variables seen in this bind expr. */
1065 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1067 if (TREE_CODE (t) == VAR_DECL)
1069 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1071 /* Mark variable as local. */
1072 if (ctx && !DECL_EXTERNAL (t)
1073 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1074 || splay_tree_lookup (ctx->variables,
1075 (splay_tree_key) t) == NULL))
1077 if (ctx->region_type == ORT_SIMD
1078 && TREE_ADDRESSABLE (t)
1079 && !TREE_STATIC (t))
1080 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1081 else
1082 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1085 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1087 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1088 cfun->has_local_explicit_reg_vars = true;
1091 /* Preliminarily mark non-addressed complex variables as eligible
1092 for promotion to gimple registers. We'll transform their uses
1093 as we find them. */
1094 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1095 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1096 && !TREE_THIS_VOLATILE (t)
1097 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1098 && !needs_to_live_in_memory (t))
1099 DECL_GIMPLE_REG_P (t) = 1;
1102 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1103 BIND_EXPR_BLOCK (bind_expr));
1104 gimple_push_bind_expr (gimple_bind);
1106 gimplify_ctxp->save_stack = false;
1108 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1109 body = NULL;
1110 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1111 gimple_bind_set_body (gimple_bind, body);
1113 /* Source location wise, the cleanup code (stack_restore and clobbers)
1114 belongs to the end of the block, so propagate what we have. The
1115 stack_save operation belongs to the beginning of block, which we can
1116 infer from the bind_expr directly if the block has no explicit
1117 assignment. */
1118 if (BIND_EXPR_BLOCK (bind_expr))
1120 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1121 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1123 if (start_locus == 0)
1124 start_locus = EXPR_LOCATION (bind_expr);
1126 cleanup = NULL;
1127 stack_save = NULL;
1128 if (gimplify_ctxp->save_stack)
1130 gimple stack_restore;
1132 /* Save stack on entry and restore it on exit. Add a try_finally
1133 block to achieve this. */
1134 build_stack_save_restore (&stack_save, &stack_restore);
1136 gimple_set_location (stack_save, start_locus);
1137 gimple_set_location (stack_restore, end_locus);
1139 gimplify_seq_add_stmt (&cleanup, stack_restore);
1142 /* Add clobbers for all variables that go out of scope. */
1143 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1145 if (TREE_CODE (t) == VAR_DECL
1146 && !is_global_var (t)
1147 && DECL_CONTEXT (t) == current_function_decl
1148 && !DECL_HARD_REGISTER (t)
1149 && !TREE_THIS_VOLATILE (t)
1150 && !DECL_HAS_VALUE_EXPR_P (t)
1151 /* Only care for variables that have to be in memory. Others
1152 will be rewritten into SSA names, hence moved to the top-level. */
1153 && !is_gimple_reg (t)
1154 && flag_stack_reuse != SR_NONE)
1156 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1157 gimple clobber_stmt;
1158 TREE_THIS_VOLATILE (clobber) = 1;
1159 clobber_stmt = gimple_build_assign (t, clobber);
1160 gimple_set_location (clobber_stmt, end_locus);
1161 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1165 if (cleanup)
1167 gimple gs;
1168 gimple_seq new_body;
1170 new_body = NULL;
1171 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1172 GIMPLE_TRY_FINALLY);
1174 if (stack_save)
1175 gimplify_seq_add_stmt (&new_body, stack_save);
1176 gimplify_seq_add_stmt (&new_body, gs);
1177 gimple_bind_set_body (gimple_bind, new_body);
1180 gimplify_ctxp->save_stack = old_save_stack;
1181 gimple_pop_bind_expr ();
1183 gimplify_seq_add_stmt (pre_p, gimple_bind);
1185 if (temp)
1187 *expr_p = temp;
1188 return GS_OK;
1191 *expr_p = NULL_TREE;
1192 return GS_ALL_DONE;
1195 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1196 GIMPLE value, it is assigned to a new temporary and the statement is
1197 re-written to return the temporary.
1199 PRE_P points to the sequence where side effects that must happen before
1200 STMT should be stored. */
1202 static enum gimplify_status
1203 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1205 gimple ret;
1206 tree ret_expr = TREE_OPERAND (stmt, 0);
1207 tree result_decl, result;
1209 if (ret_expr == error_mark_node)
1210 return GS_ERROR;
1212 /* Implicit _Cilk_sync must be inserted right before any return statement
1213 if there is a _Cilk_spawn in the function. If the user has provided a
1214 _Cilk_sync, the optimizer should remove this duplicate one. */
1215 if (fn_contains_cilk_spawn_p (cfun))
1217 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1218 gimplify_and_add (impl_sync, pre_p);
1221 if (!ret_expr
1222 || TREE_CODE (ret_expr) == RESULT_DECL
1223 || ret_expr == error_mark_node)
1225 gimple ret = gimple_build_return (ret_expr);
1226 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1227 gimplify_seq_add_stmt (pre_p, ret);
1228 return GS_ALL_DONE;
1231 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1232 result_decl = NULL_TREE;
1233 else
1235 result_decl = TREE_OPERAND (ret_expr, 0);
1237 /* See through a return by reference. */
1238 if (TREE_CODE (result_decl) == INDIRECT_REF)
1239 result_decl = TREE_OPERAND (result_decl, 0);
1241 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1242 || TREE_CODE (ret_expr) == INIT_EXPR)
1243 && TREE_CODE (result_decl) == RESULT_DECL);
1246 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1247 Recall that aggregate_value_p is FALSE for any aggregate type that is
1248 returned in registers. If we're returning values in registers, then
1249 we don't want to extend the lifetime of the RESULT_DECL, particularly
1250 across another call. In addition, for those aggregates for which
1251 hard_function_value generates a PARALLEL, we'll die during normal
1252 expansion of structure assignments; there's special code in expand_return
1253 to handle this case that does not exist in expand_expr. */
1254 if (!result_decl)
1255 result = NULL_TREE;
1256 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1258 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1260 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1261 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1262 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1263 should be effectively allocated by the caller, i.e. all calls to
1264 this function must be subject to the Return Slot Optimization. */
1265 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1266 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1268 result = result_decl;
1270 else if (gimplify_ctxp->return_temp)
1271 result = gimplify_ctxp->return_temp;
1272 else
1274 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1276 /* ??? With complex control flow (usually involving abnormal edges),
1277 we can wind up warning about an uninitialized value for this. Due
1278 to how this variable is constructed and initialized, this is never
1279 true. Give up and never warn. */
1280 TREE_NO_WARNING (result) = 1;
1282 gimplify_ctxp->return_temp = result;
1285 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1286 Then gimplify the whole thing. */
1287 if (result != result_decl)
1288 TREE_OPERAND (ret_expr, 0) = result;
1290 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1292 ret = gimple_build_return (result);
1293 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1294 gimplify_seq_add_stmt (pre_p, ret);
1296 return GS_ALL_DONE;
1299 /* Gimplify a variable-length array DECL. */
1301 static void
1302 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1304 /* This is a variable-sized decl. Simplify its size and mark it
1305 for deferred expansion. */
1306 tree t, addr, ptr_type;
1308 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1309 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1311 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1312 if (DECL_HAS_VALUE_EXPR_P (decl))
1313 return;
1315 /* All occurrences of this decl in final gimplified code will be
1316 replaced by indirection. Setting DECL_VALUE_EXPR does two
1317 things: First, it lets the rest of the gimplifier know what
1318 replacement to use. Second, it lets the debug info know
1319 where to find the value. */
1320 ptr_type = build_pointer_type (TREE_TYPE (decl));
1321 addr = create_tmp_var (ptr_type, get_name (decl));
1322 DECL_IGNORED_P (addr) = 0;
1323 t = build_fold_indirect_ref (addr);
1324 TREE_THIS_NOTRAP (t) = 1;
1325 SET_DECL_VALUE_EXPR (decl, t);
1326 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1328 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1329 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1330 size_int (DECL_ALIGN (decl)));
1331 /* The call has been built for a variable-sized object. */
1332 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1333 t = fold_convert (ptr_type, t);
1334 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1336 gimplify_and_add (t, seq_p);
1338 /* Indicate that we need to restore the stack level when the
1339 enclosing BIND_EXPR is exited. */
1340 gimplify_ctxp->save_stack = true;
1343 /* A helper function to be called via walk_tree. Mark all labels under *TP
1344 as being forced. To be called for DECL_INITIAL of static variables. */
1346 static tree
1347 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1349 if (TYPE_P (*tp))
1350 *walk_subtrees = 0;
1351 if (TREE_CODE (*tp) == LABEL_DECL)
1352 FORCED_LABEL (*tp) = 1;
1354 return NULL_TREE;
1357 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1358 and initialization explicit. */
1360 static enum gimplify_status
1361 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1363 tree stmt = *stmt_p;
1364 tree decl = DECL_EXPR_DECL (stmt);
1366 *stmt_p = NULL_TREE;
1368 if (TREE_TYPE (decl) == error_mark_node)
1369 return GS_ERROR;
1371 if ((TREE_CODE (decl) == TYPE_DECL
1372 || TREE_CODE (decl) == VAR_DECL)
1373 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1374 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1376 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1377 in case its size expressions contain problematic nodes like CALL_EXPR. */
1378 if (TREE_CODE (decl) == TYPE_DECL
1379 && DECL_ORIGINAL_TYPE (decl)
1380 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1381 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1383 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1385 tree init = DECL_INITIAL (decl);
1387 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1388 || (!TREE_STATIC (decl)
1389 && flag_stack_check == GENERIC_STACK_CHECK
1390 && compare_tree_int (DECL_SIZE_UNIT (decl),
1391 STACK_CHECK_MAX_VAR_SIZE) > 0))
1392 gimplify_vla_decl (decl, seq_p);
1394 /* Some front ends do not explicitly declare all anonymous
1395 artificial variables. We compensate here by declaring the
1396 variables, though it would be better if the front ends would
1397 explicitly declare them. */
1398 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1399 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1400 gimple_add_tmp_var (decl);
1402 if (init && init != error_mark_node)
1404 if (!TREE_STATIC (decl))
1406 DECL_INITIAL (decl) = NULL_TREE;
1407 init = build2 (INIT_EXPR, void_type_node, decl, init);
1408 gimplify_and_add (init, seq_p);
1409 ggc_free (init);
1411 else
1412 /* We must still examine initializers for static variables
1413 as they may contain a label address. */
1414 walk_tree (&init, force_labels_r, NULL, NULL);
1418 return GS_ALL_DONE;
1421 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1422 and replacing the LOOP_EXPR with goto, but if the loop contains an
1423 EXIT_EXPR, we need to append a label for it to jump to. */
1425 static enum gimplify_status
1426 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1428 tree saved_label = gimplify_ctxp->exit_label;
1429 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1431 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1433 gimplify_ctxp->exit_label = NULL_TREE;
1435 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1437 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1439 if (gimplify_ctxp->exit_label)
1440 gimplify_seq_add_stmt (pre_p,
1441 gimple_build_label (gimplify_ctxp->exit_label));
1443 gimplify_ctxp->exit_label = saved_label;
1445 *expr_p = NULL;
1446 return GS_ALL_DONE;
1449 /* Gimplify a statement list onto a sequence. These may be created either
1450 by an enlightened front-end, or by shortcut_cond_expr. */
1452 static enum gimplify_status
1453 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1455 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1457 tree_stmt_iterator i = tsi_start (*expr_p);
1459 while (!tsi_end_p (i))
1461 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1462 tsi_delink (&i);
1465 if (temp)
1467 *expr_p = temp;
1468 return GS_OK;
1471 return GS_ALL_DONE;
1475 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1476 branch to. */
1478 static enum gimplify_status
1479 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1481 tree switch_expr = *expr_p;
1482 gimple_seq switch_body_seq = NULL;
1483 enum gimplify_status ret;
1484 tree index_type = TREE_TYPE (switch_expr);
1485 if (index_type == NULL_TREE)
1486 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1488 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1489 fb_rvalue);
1490 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1491 return ret;
1493 if (SWITCH_BODY (switch_expr))
1495 vec<tree> labels;
1496 vec<tree> saved_labels;
1497 tree default_case = NULL_TREE;
1498 gimple gimple_switch;
1500 /* If someone can be bothered to fill in the labels, they can
1501 be bothered to null out the body too. */
1502 gcc_assert (!SWITCH_LABELS (switch_expr));
1504 /* Save old labels, get new ones from body, then restore the old
1505 labels. Save all the things from the switch body to append after. */
1506 saved_labels = gimplify_ctxp->case_labels;
1507 gimplify_ctxp->case_labels.create (8);
1509 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1510 labels = gimplify_ctxp->case_labels;
1511 gimplify_ctxp->case_labels = saved_labels;
1513 preprocess_case_label_vec_for_gimple (labels, index_type,
1514 &default_case);
1516 if (!default_case)
1518 gimple new_default;
1520 default_case
1521 = build_case_label (NULL_TREE, NULL_TREE,
1522 create_artificial_label (UNKNOWN_LOCATION));
1523 new_default = gimple_build_label (CASE_LABEL (default_case));
1524 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1527 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1528 default_case, labels);
1529 gimplify_seq_add_stmt (pre_p, gimple_switch);
1530 gimplify_seq_add_seq (pre_p, switch_body_seq);
1531 labels.release ();
1533 else
1534 gcc_assert (SWITCH_LABELS (switch_expr));
1536 return GS_ALL_DONE;
1539 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1541 static enum gimplify_status
1542 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1544 struct gimplify_ctx *ctxp;
1545 gimple gimple_label;
1547 /* Invalid programs can play Duff's Device type games with, for example,
1548 #pragma omp parallel. At least in the C front end, we don't
1549 detect such invalid branches until after gimplification, in the
1550 diagnose_omp_blocks pass. */
1551 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1552 if (ctxp->case_labels.exists ())
1553 break;
1555 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1556 ctxp->case_labels.safe_push (*expr_p);
1557 gimplify_seq_add_stmt (pre_p, gimple_label);
1559 return GS_ALL_DONE;
1562 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1563 if necessary. */
1565 tree
1566 build_and_jump (tree *label_p)
1568 if (label_p == NULL)
1569 /* If there's nowhere to jump, just fall through. */
1570 return NULL_TREE;
1572 if (*label_p == NULL_TREE)
1574 tree label = create_artificial_label (UNKNOWN_LOCATION);
1575 *label_p = label;
1578 return build1 (GOTO_EXPR, void_type_node, *label_p);
1581 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1582 This also involves building a label to jump to and communicating it to
1583 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1585 static enum gimplify_status
1586 gimplify_exit_expr (tree *expr_p)
1588 tree cond = TREE_OPERAND (*expr_p, 0);
1589 tree expr;
1591 expr = build_and_jump (&gimplify_ctxp->exit_label);
1592 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1593 *expr_p = expr;
1595 return GS_OK;
1598 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1599 different from its canonical type, wrap the whole thing inside a
1600 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1601 type.
1603 The canonical type of a COMPONENT_REF is the type of the field being
1604 referenced--unless the field is a bit-field which can be read directly
1605 in a smaller mode, in which case the canonical type is the
1606 sign-appropriate type corresponding to that mode. */
1608 static void
1609 canonicalize_component_ref (tree *expr_p)
1611 tree expr = *expr_p;
1612 tree type;
1614 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1616 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1617 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1618 else
1619 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1621 /* One could argue that all the stuff below is not necessary for
1622 the non-bitfield case and declare it a FE error if type
1623 adjustment would be needed. */
1624 if (TREE_TYPE (expr) != type)
1626 #ifdef ENABLE_TYPES_CHECKING
1627 tree old_type = TREE_TYPE (expr);
1628 #endif
1629 int type_quals;
1631 /* We need to preserve qualifiers and propagate them from
1632 operand 0. */
1633 type_quals = TYPE_QUALS (type)
1634 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1635 if (TYPE_QUALS (type) != type_quals)
1636 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1638 /* Set the type of the COMPONENT_REF to the underlying type. */
1639 TREE_TYPE (expr) = type;
1641 #ifdef ENABLE_TYPES_CHECKING
1642 /* It is now a FE error, if the conversion from the canonical
1643 type to the original expression type is not useless. */
1644 gcc_assert (useless_type_conversion_p (old_type, type));
1645 #endif
1649 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1650 to foo, embed that change in the ADDR_EXPR by converting
1651 T array[U];
1652 (T *)&array
1654 &array[L]
1655 where L is the lower bound. For simplicity, only do this for constant
1656 lower bound.
1657 The constraint is that the type of &array[L] is trivially convertible
1658 to T *. */
1660 static void
1661 canonicalize_addr_expr (tree *expr_p)
1663 tree expr = *expr_p;
1664 tree addr_expr = TREE_OPERAND (expr, 0);
1665 tree datype, ddatype, pddatype;
1667 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1668 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1669 || TREE_CODE (addr_expr) != ADDR_EXPR)
1670 return;
1672 /* The addr_expr type should be a pointer to an array. */
1673 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1674 if (TREE_CODE (datype) != ARRAY_TYPE)
1675 return;
1677 /* The pointer to element type shall be trivially convertible to
1678 the expression pointer type. */
1679 ddatype = TREE_TYPE (datype);
1680 pddatype = build_pointer_type (ddatype);
1681 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1682 pddatype))
1683 return;
1685 /* The lower bound and element sizes must be constant. */
1686 if (!TYPE_SIZE_UNIT (ddatype)
1687 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1688 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1689 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1690 return;
1692 /* All checks succeeded. Build a new node to merge the cast. */
1693 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1694 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1695 NULL_TREE, NULL_TREE);
1696 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1698 /* We can have stripped a required restrict qualifier above. */
1699 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1700 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1703 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1704 underneath as appropriate. */
1706 static enum gimplify_status
1707 gimplify_conversion (tree *expr_p)
1709 location_t loc = EXPR_LOCATION (*expr_p);
1710 gcc_assert (CONVERT_EXPR_P (*expr_p));
1712 /* Then strip away all but the outermost conversion. */
1713 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1715 /* And remove the outermost conversion if it's useless. */
1716 if (tree_ssa_useless_type_conversion (*expr_p))
1717 *expr_p = TREE_OPERAND (*expr_p, 0);
1719 /* If we still have a conversion at the toplevel,
1720 then canonicalize some constructs. */
1721 if (CONVERT_EXPR_P (*expr_p))
1723 tree sub = TREE_OPERAND (*expr_p, 0);
1725 /* If a NOP conversion is changing the type of a COMPONENT_REF
1726 expression, then canonicalize its type now in order to expose more
1727 redundant conversions. */
1728 if (TREE_CODE (sub) == COMPONENT_REF)
1729 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1731 /* If a NOP conversion is changing a pointer to array of foo
1732 to a pointer to foo, embed that change in the ADDR_EXPR. */
1733 else if (TREE_CODE (sub) == ADDR_EXPR)
1734 canonicalize_addr_expr (expr_p);
1737 /* If we have a conversion to a non-register type force the
1738 use of a VIEW_CONVERT_EXPR instead. */
1739 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1740 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1741 TREE_OPERAND (*expr_p, 0));
1743 return GS_OK;
1746 /* Nonlocal VLAs seen in the current function. */
1747 static hash_set<tree> *nonlocal_vlas;
1749 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1750 static tree nonlocal_vla_vars;
1752 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1753 DECL_VALUE_EXPR, and it's worth re-examining things. */
1755 static enum gimplify_status
1756 gimplify_var_or_parm_decl (tree *expr_p)
1758 tree decl = *expr_p;
1760 /* ??? If this is a local variable, and it has not been seen in any
1761 outer BIND_EXPR, then it's probably the result of a duplicate
1762 declaration, for which we've already issued an error. It would
1763 be really nice if the front end wouldn't leak these at all.
1764 Currently the only known culprit is C++ destructors, as seen
1765 in g++.old-deja/g++.jason/binding.C. */
1766 if (TREE_CODE (decl) == VAR_DECL
1767 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1768 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1769 && decl_function_context (decl) == current_function_decl)
1771 gcc_assert (seen_error ());
1772 return GS_ERROR;
1775 /* When within an OpenMP context, notice uses of variables. */
1776 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1777 return GS_ALL_DONE;
1779 /* If the decl is an alias for another expression, substitute it now. */
1780 if (DECL_HAS_VALUE_EXPR_P (decl))
1782 tree value_expr = DECL_VALUE_EXPR (decl);
1784 /* For referenced nonlocal VLAs add a decl for debugging purposes
1785 to the current function. */
1786 if (TREE_CODE (decl) == VAR_DECL
1787 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1788 && nonlocal_vlas != NULL
1789 && TREE_CODE (value_expr) == INDIRECT_REF
1790 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1791 && decl_function_context (decl) != current_function_decl)
1793 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1794 while (ctx
1795 && (ctx->region_type == ORT_WORKSHARE
1796 || ctx->region_type == ORT_SIMD))
1797 ctx = ctx->outer_context;
1798 if (!ctx && !nonlocal_vlas->add (decl))
1800 tree copy = copy_node (decl);
1802 lang_hooks.dup_lang_specific_decl (copy);
1803 SET_DECL_RTL (copy, 0);
1804 TREE_USED (copy) = 1;
1805 DECL_CHAIN (copy) = nonlocal_vla_vars;
1806 nonlocal_vla_vars = copy;
1807 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1808 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1812 *expr_p = unshare_expr (value_expr);
1813 return GS_OK;
1816 return GS_ALL_DONE;
1819 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1821 static void
1822 recalculate_side_effects (tree t)
1824 enum tree_code code = TREE_CODE (t);
1825 int len = TREE_OPERAND_LENGTH (t);
1826 int i;
1828 switch (TREE_CODE_CLASS (code))
1830 case tcc_expression:
1831 switch (code)
1833 case INIT_EXPR:
1834 case MODIFY_EXPR:
1835 case VA_ARG_EXPR:
1836 case PREDECREMENT_EXPR:
1837 case PREINCREMENT_EXPR:
1838 case POSTDECREMENT_EXPR:
1839 case POSTINCREMENT_EXPR:
1840 /* All of these have side-effects, no matter what their
1841 operands are. */
1842 return;
1844 default:
1845 break;
1847 /* Fall through. */
1849 case tcc_comparison: /* a comparison expression */
1850 case tcc_unary: /* a unary arithmetic expression */
1851 case tcc_binary: /* a binary arithmetic expression */
1852 case tcc_reference: /* a reference */
1853 case tcc_vl_exp: /* a function call */
1854 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1855 for (i = 0; i < len; ++i)
1857 tree op = TREE_OPERAND (t, i);
1858 if (op && TREE_SIDE_EFFECTS (op))
1859 TREE_SIDE_EFFECTS (t) = 1;
1861 break;
1863 case tcc_constant:
1864 /* No side-effects. */
1865 return;
1867 default:
1868 gcc_unreachable ();
1872 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1873 node *EXPR_P.
1875 compound_lval
1876 : min_lval '[' val ']'
1877 | min_lval '.' ID
1878 | compound_lval '[' val ']'
1879 | compound_lval '.' ID
1881 This is not part of the original SIMPLE definition, which separates
1882 array and member references, but it seems reasonable to handle them
1883 together. Also, this way we don't run into problems with union
1884 aliasing; gcc requires that for accesses through a union to alias, the
1885 union reference must be explicit, which was not always the case when we
1886 were splitting up array and member refs.
1888 PRE_P points to the sequence where side effects that must happen before
1889 *EXPR_P should be stored.
1891 POST_P points to the sequence where side effects that must happen after
1892 *EXPR_P should be stored. */
1894 static enum gimplify_status
1895 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1896 fallback_t fallback)
1898 tree *p;
1899 enum gimplify_status ret = GS_ALL_DONE, tret;
1900 int i;
1901 location_t loc = EXPR_LOCATION (*expr_p);
1902 tree expr = *expr_p;
1904 /* Create a stack of the subexpressions so later we can walk them in
1905 order from inner to outer. */
1906 auto_vec<tree, 10> expr_stack;
1908 /* We can handle anything that get_inner_reference can deal with. */
1909 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1911 restart:
1912 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1913 if (TREE_CODE (*p) == INDIRECT_REF)
1914 *p = fold_indirect_ref_loc (loc, *p);
1916 if (handled_component_p (*p))
1918 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1919 additional COMPONENT_REFs. */
1920 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1921 && gimplify_var_or_parm_decl (p) == GS_OK)
1922 goto restart;
1923 else
1924 break;
1926 expr_stack.safe_push (*p);
1929 gcc_assert (expr_stack.length ());
1931 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1932 walked through and P points to the innermost expression.
1934 Java requires that we elaborated nodes in source order. That
1935 means we must gimplify the inner expression followed by each of
1936 the indices, in order. But we can't gimplify the inner
1937 expression until we deal with any variable bounds, sizes, or
1938 positions in order to deal with PLACEHOLDER_EXPRs.
1940 So we do this in three steps. First we deal with the annotations
1941 for any variables in the components, then we gimplify the base,
1942 then we gimplify any indices, from left to right. */
1943 for (i = expr_stack.length () - 1; i >= 0; i--)
1945 tree t = expr_stack[i];
1947 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1949 /* Gimplify the low bound and element type size and put them into
1950 the ARRAY_REF. If these values are set, they have already been
1951 gimplified. */
1952 if (TREE_OPERAND (t, 2) == NULL_TREE)
1954 tree low = unshare_expr (array_ref_low_bound (t));
1955 if (!is_gimple_min_invariant (low))
1957 TREE_OPERAND (t, 2) = low;
1958 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1959 post_p, is_gimple_reg,
1960 fb_rvalue);
1961 ret = MIN (ret, tret);
1964 else
1966 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1967 is_gimple_reg, fb_rvalue);
1968 ret = MIN (ret, tret);
1971 if (TREE_OPERAND (t, 3) == NULL_TREE)
1973 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1974 tree elmt_size = unshare_expr (array_ref_element_size (t));
1975 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1977 /* Divide the element size by the alignment of the element
1978 type (above). */
1979 elmt_size
1980 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1982 if (!is_gimple_min_invariant (elmt_size))
1984 TREE_OPERAND (t, 3) = elmt_size;
1985 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1986 post_p, is_gimple_reg,
1987 fb_rvalue);
1988 ret = MIN (ret, tret);
1991 else
1993 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1994 is_gimple_reg, fb_rvalue);
1995 ret = MIN (ret, tret);
1998 else if (TREE_CODE (t) == COMPONENT_REF)
2000 /* Set the field offset into T and gimplify it. */
2001 if (TREE_OPERAND (t, 2) == NULL_TREE)
2003 tree offset = unshare_expr (component_ref_field_offset (t));
2004 tree field = TREE_OPERAND (t, 1);
2005 tree factor
2006 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2008 /* Divide the offset by its alignment. */
2009 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2011 if (!is_gimple_min_invariant (offset))
2013 TREE_OPERAND (t, 2) = offset;
2014 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2015 post_p, is_gimple_reg,
2016 fb_rvalue);
2017 ret = MIN (ret, tret);
2020 else
2022 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2023 is_gimple_reg, fb_rvalue);
2024 ret = MIN (ret, tret);
2029 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2030 so as to match the min_lval predicate. Failure to do so may result
2031 in the creation of large aggregate temporaries. */
2032 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2033 fallback | fb_lvalue);
2034 ret = MIN (ret, tret);
2036 /* And finally, the indices and operands of ARRAY_REF. During this
2037 loop we also remove any useless conversions. */
2038 for (; expr_stack.length () > 0; )
2040 tree t = expr_stack.pop ();
2042 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2044 /* Gimplify the dimension. */
2045 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2047 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2048 is_gimple_val, fb_rvalue);
2049 ret = MIN (ret, tret);
2053 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2055 /* The innermost expression P may have originally had
2056 TREE_SIDE_EFFECTS set which would have caused all the outer
2057 expressions in *EXPR_P leading to P to also have had
2058 TREE_SIDE_EFFECTS set. */
2059 recalculate_side_effects (t);
2062 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2063 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2065 canonicalize_component_ref (expr_p);
2068 expr_stack.release ();
2070 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2072 return ret;
2075 /* Gimplify the self modifying expression pointed to by EXPR_P
2076 (++, --, +=, -=).
2078 PRE_P points to the list where side effects that must happen before
2079 *EXPR_P should be stored.
2081 POST_P points to the list where side effects that must happen after
2082 *EXPR_P should be stored.
2084 WANT_VALUE is nonzero iff we want to use the value of this expression
2085 in another expression.
2087 ARITH_TYPE is the type the computation should be performed in. */
2089 enum gimplify_status
2090 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2091 bool want_value, tree arith_type)
2093 enum tree_code code;
2094 tree lhs, lvalue, rhs, t1;
2095 gimple_seq post = NULL, *orig_post_p = post_p;
2096 bool postfix;
2097 enum tree_code arith_code;
2098 enum gimplify_status ret;
2099 location_t loc = EXPR_LOCATION (*expr_p);
2101 code = TREE_CODE (*expr_p);
2103 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2104 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2106 /* Prefix or postfix? */
2107 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2108 /* Faster to treat as prefix if result is not used. */
2109 postfix = want_value;
2110 else
2111 postfix = false;
2113 /* For postfix, make sure the inner expression's post side effects
2114 are executed after side effects from this expression. */
2115 if (postfix)
2116 post_p = &post;
2118 /* Add or subtract? */
2119 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2120 arith_code = PLUS_EXPR;
2121 else
2122 arith_code = MINUS_EXPR;
2124 /* Gimplify the LHS into a GIMPLE lvalue. */
2125 lvalue = TREE_OPERAND (*expr_p, 0);
2126 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2127 if (ret == GS_ERROR)
2128 return ret;
2130 /* Extract the operands to the arithmetic operation. */
2131 lhs = lvalue;
2132 rhs = TREE_OPERAND (*expr_p, 1);
2134 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2135 that as the result value and in the postqueue operation. */
2136 if (postfix)
2138 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2139 if (ret == GS_ERROR)
2140 return ret;
2142 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2145 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2146 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2148 rhs = convert_to_ptrofftype_loc (loc, rhs);
2149 if (arith_code == MINUS_EXPR)
2150 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2151 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2153 else
2154 t1 = fold_convert (TREE_TYPE (*expr_p),
2155 fold_build2 (arith_code, arith_type,
2156 fold_convert (arith_type, lhs),
2157 fold_convert (arith_type, rhs)));
2159 if (postfix)
2161 gimplify_assign (lvalue, t1, pre_p);
2162 gimplify_seq_add_seq (orig_post_p, post);
2163 *expr_p = lhs;
2164 return GS_ALL_DONE;
2166 else
2168 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2169 return GS_OK;
2173 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2175 static void
2176 maybe_with_size_expr (tree *expr_p)
2178 tree expr = *expr_p;
2179 tree type = TREE_TYPE (expr);
2180 tree size;
2182 /* If we've already wrapped this or the type is error_mark_node, we can't do
2183 anything. */
2184 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2185 || type == error_mark_node)
2186 return;
2188 /* If the size isn't known or is a constant, we have nothing to do. */
2189 size = TYPE_SIZE_UNIT (type);
2190 if (!size || TREE_CODE (size) == INTEGER_CST)
2191 return;
2193 /* Otherwise, make a WITH_SIZE_EXPR. */
2194 size = unshare_expr (size);
2195 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2196 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2199 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2200 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2201 the CALL_EXPR. */
2203 enum gimplify_status
2204 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2206 bool (*test) (tree);
2207 fallback_t fb;
2209 /* In general, we allow lvalues for function arguments to avoid
2210 extra overhead of copying large aggregates out of even larger
2211 aggregates into temporaries only to copy the temporaries to
2212 the argument list. Make optimizers happy by pulling out to
2213 temporaries those types that fit in registers. */
2214 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2215 test = is_gimple_val, fb = fb_rvalue;
2216 else
2218 test = is_gimple_lvalue, fb = fb_either;
2219 /* Also strip a TARGET_EXPR that would force an extra copy. */
2220 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2222 tree init = TARGET_EXPR_INITIAL (*arg_p);
2223 if (init
2224 && !VOID_TYPE_P (TREE_TYPE (init)))
2225 *arg_p = init;
2229 /* If this is a variable sized type, we must remember the size. */
2230 maybe_with_size_expr (arg_p);
2232 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2233 /* Make sure arguments have the same location as the function call
2234 itself. */
2235 protected_set_expr_location (*arg_p, call_location);
2237 /* There is a sequence point before a function call. Side effects in
2238 the argument list must occur before the actual call. So, when
2239 gimplifying arguments, force gimplify_expr to use an internal
2240 post queue which is then appended to the end of PRE_P. */
2241 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2244 /* Don't fold inside offloading regsion: it can break code by adding decl
2245 references that weren't in the source. We'll do it during omplower pass
2246 instead. */
2248 static bool
2249 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2251 struct gimplify_omp_ctx *ctx;
2252 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2253 if (ctx->region_type & ORT_TARGET
2254 && ctx->region_type & ORT_TARGET_OFFLOAD)
2255 return false;
2256 return fold_stmt (gsi);
2259 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2260 WANT_VALUE is true if the result of the call is desired. */
2262 static enum gimplify_status
2263 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2265 tree fndecl, parms, p, fnptrtype;
2266 enum gimplify_status ret;
2267 int i, nargs;
2268 gimple call;
2269 bool builtin_va_start_p = false;
2270 location_t loc = EXPR_LOCATION (*expr_p);
2272 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2274 /* For reliable diagnostics during inlining, it is necessary that
2275 every call_expr be annotated with file and line. */
2276 if (! EXPR_HAS_LOCATION (*expr_p))
2277 SET_EXPR_LOCATION (*expr_p, input_location);
2279 /* Gimplify internal functions created in the FEs. */
2280 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2282 nargs = call_expr_nargs (*expr_p);
2283 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2284 auto_vec<tree> vargs (nargs);
2286 for (i = 0; i < nargs; i++)
2288 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2289 EXPR_LOCATION (*expr_p));
2290 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2292 gimple call = gimple_build_call_internal_vec (ifn, vargs);
2293 gimplify_seq_add_stmt (pre_p, call);
2294 return GS_ALL_DONE;
2297 /* This may be a call to a builtin function.
2299 Builtin function calls may be transformed into different
2300 (and more efficient) builtin function calls under certain
2301 circumstances. Unfortunately, gimplification can muck things
2302 up enough that the builtin expanders are not aware that certain
2303 transformations are still valid.
2305 So we attempt transformation/gimplification of the call before
2306 we gimplify the CALL_EXPR. At this time we do not manage to
2307 transform all calls in the same manner as the expanders do, but
2308 we do transform most of them. */
2309 fndecl = get_callee_fndecl (*expr_p);
2310 if (fndecl
2311 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2312 switch (DECL_FUNCTION_CODE (fndecl))
2314 case BUILT_IN_VA_START:
2316 builtin_va_start_p = TRUE;
2317 if (call_expr_nargs (*expr_p) < 2)
2319 error ("too few arguments to function %<va_start%>");
2320 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2321 return GS_OK;
2324 if (fold_builtin_next_arg (*expr_p, true))
2326 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2327 return GS_OK;
2329 break;
2331 case BUILT_IN_LINE:
2333 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2334 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2335 return GS_OK;
2337 case BUILT_IN_FILE:
2339 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2340 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2341 return GS_OK;
2343 case BUILT_IN_FUNCTION:
2345 const char *function;
2346 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2347 *expr_p = build_string_literal (strlen (function) + 1, function);
2348 return GS_OK;
2350 default:
2353 if (fndecl && DECL_BUILT_IN (fndecl))
2355 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2356 if (new_tree && new_tree != *expr_p)
2358 /* There was a transformation of this call which computes the
2359 same value, but in a more efficient way. Return and try
2360 again. */
2361 *expr_p = new_tree;
2362 return GS_OK;
2366 /* Remember the original function pointer type. */
2367 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2369 /* There is a sequence point before the call, so any side effects in
2370 the calling expression must occur before the actual call. Force
2371 gimplify_expr to use an internal post queue. */
2372 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2373 is_gimple_call_addr, fb_rvalue);
2375 nargs = call_expr_nargs (*expr_p);
2377 /* Get argument types for verification. */
2378 fndecl = get_callee_fndecl (*expr_p);
2379 parms = NULL_TREE;
2380 if (fndecl)
2381 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2382 else
2383 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2385 if (fndecl && DECL_ARGUMENTS (fndecl))
2386 p = DECL_ARGUMENTS (fndecl);
2387 else if (parms)
2388 p = parms;
2389 else
2390 p = NULL_TREE;
2391 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2394 /* If the last argument is __builtin_va_arg_pack () and it is not
2395 passed as a named argument, decrease the number of CALL_EXPR
2396 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2397 if (!p
2398 && i < nargs
2399 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2401 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2402 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2404 if (last_arg_fndecl
2405 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2406 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2407 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2409 tree call = *expr_p;
2411 --nargs;
2412 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2413 CALL_EXPR_FN (call),
2414 nargs, CALL_EXPR_ARGP (call));
2416 /* Copy all CALL_EXPR flags, location and block, except
2417 CALL_EXPR_VA_ARG_PACK flag. */
2418 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2419 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2420 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2421 = CALL_EXPR_RETURN_SLOT_OPT (call);
2422 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2423 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2425 /* Set CALL_EXPR_VA_ARG_PACK. */
2426 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2430 /* Finally, gimplify the function arguments. */
2431 if (nargs > 0)
2433 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2434 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2435 PUSH_ARGS_REVERSED ? i-- : i++)
2437 enum gimplify_status t;
2439 /* Avoid gimplifying the second argument to va_start, which needs to
2440 be the plain PARM_DECL. */
2441 if ((i != 1) || !builtin_va_start_p)
2443 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2444 EXPR_LOCATION (*expr_p));
2446 if (t == GS_ERROR)
2447 ret = GS_ERROR;
2452 /* Verify the function result. */
2453 if (want_value && fndecl
2454 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2456 error_at (loc, "using result of function returning %<void%>");
2457 ret = GS_ERROR;
2460 /* Try this again in case gimplification exposed something. */
2461 if (ret != GS_ERROR)
2463 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2465 if (new_tree && new_tree != *expr_p)
2467 /* There was a transformation of this call which computes the
2468 same value, but in a more efficient way. Return and try
2469 again. */
2470 *expr_p = new_tree;
2471 return GS_OK;
2474 else
2476 *expr_p = error_mark_node;
2477 return GS_ERROR;
2480 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2481 decl. This allows us to eliminate redundant or useless
2482 calls to "const" functions. */
2483 if (TREE_CODE (*expr_p) == CALL_EXPR)
2485 int flags = call_expr_flags (*expr_p);
2486 if (flags & (ECF_CONST | ECF_PURE)
2487 /* An infinite loop is considered a side effect. */
2488 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2489 TREE_SIDE_EFFECTS (*expr_p) = 0;
2492 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2493 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2494 form and delegate the creation of a GIMPLE_CALL to
2495 gimplify_modify_expr. This is always possible because when
2496 WANT_VALUE is true, the caller wants the result of this call into
2497 a temporary, which means that we will emit an INIT_EXPR in
2498 internal_get_tmp_var which will then be handled by
2499 gimplify_modify_expr. */
2500 if (!want_value)
2502 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2503 have to do is replicate it as a GIMPLE_CALL tuple. */
2504 gimple_stmt_iterator gsi;
2505 call = gimple_build_call_from_tree (*expr_p);
2506 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2507 notice_special_calls (call);
2508 gimplify_seq_add_stmt (pre_p, call);
2509 gsi = gsi_last (*pre_p);
2510 maybe_fold_stmt (&gsi);
2511 *expr_p = NULL_TREE;
2513 else
2514 /* Remember the original function type. */
2515 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2516 CALL_EXPR_FN (*expr_p));
2518 return ret;
2521 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2522 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2524 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2525 condition is true or false, respectively. If null, we should generate
2526 our own to skip over the evaluation of this specific expression.
2528 LOCUS is the source location of the COND_EXPR.
2530 This function is the tree equivalent of do_jump.
2532 shortcut_cond_r should only be called by shortcut_cond_expr. */
2534 static tree
2535 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2536 location_t locus)
2538 tree local_label = NULL_TREE;
2539 tree t, expr = NULL;
2541 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2542 retain the shortcut semantics. Just insert the gotos here;
2543 shortcut_cond_expr will append the real blocks later. */
2544 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2546 location_t new_locus;
2548 /* Turn if (a && b) into
2550 if (a); else goto no;
2551 if (b) goto yes; else goto no;
2552 (no:) */
2554 if (false_label_p == NULL)
2555 false_label_p = &local_label;
2557 /* Keep the original source location on the first 'if'. */
2558 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2559 append_to_statement_list (t, &expr);
2561 /* Set the source location of the && on the second 'if'. */
2562 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2563 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2564 new_locus);
2565 append_to_statement_list (t, &expr);
2567 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2569 location_t new_locus;
2571 /* Turn if (a || b) into
2573 if (a) goto yes;
2574 if (b) goto yes; else goto no;
2575 (yes:) */
2577 if (true_label_p == NULL)
2578 true_label_p = &local_label;
2580 /* Keep the original source location on the first 'if'. */
2581 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2582 append_to_statement_list (t, &expr);
2584 /* Set the source location of the || on the second 'if'. */
2585 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2586 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2587 new_locus);
2588 append_to_statement_list (t, &expr);
2590 else if (TREE_CODE (pred) == COND_EXPR
2591 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2592 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2594 location_t new_locus;
2596 /* As long as we're messing with gotos, turn if (a ? b : c) into
2597 if (a)
2598 if (b) goto yes; else goto no;
2599 else
2600 if (c) goto yes; else goto no;
2602 Don't do this if one of the arms has void type, which can happen
2603 in C++ when the arm is throw. */
2605 /* Keep the original source location on the first 'if'. Set the source
2606 location of the ? on the second 'if'. */
2607 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2608 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2609 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2610 false_label_p, locus),
2611 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2612 false_label_p, new_locus));
2614 else
2616 expr = build3 (COND_EXPR, void_type_node, pred,
2617 build_and_jump (true_label_p),
2618 build_and_jump (false_label_p));
2619 SET_EXPR_LOCATION (expr, locus);
2622 if (local_label)
2624 t = build1 (LABEL_EXPR, void_type_node, local_label);
2625 append_to_statement_list (t, &expr);
2628 return expr;
2631 /* Given a conditional expression EXPR with short-circuit boolean
2632 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2633 predicate apart into the equivalent sequence of conditionals. */
2635 static tree
2636 shortcut_cond_expr (tree expr)
2638 tree pred = TREE_OPERAND (expr, 0);
2639 tree then_ = TREE_OPERAND (expr, 1);
2640 tree else_ = TREE_OPERAND (expr, 2);
2641 tree true_label, false_label, end_label, t;
2642 tree *true_label_p;
2643 tree *false_label_p;
2644 bool emit_end, emit_false, jump_over_else;
2645 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2646 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2648 /* First do simple transformations. */
2649 if (!else_se)
2651 /* If there is no 'else', turn
2652 if (a && b) then c
2653 into
2654 if (a) if (b) then c. */
2655 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2657 /* Keep the original source location on the first 'if'. */
2658 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2659 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2660 /* Set the source location of the && on the second 'if'. */
2661 if (EXPR_HAS_LOCATION (pred))
2662 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2663 then_ = shortcut_cond_expr (expr);
2664 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2665 pred = TREE_OPERAND (pred, 0);
2666 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2667 SET_EXPR_LOCATION (expr, locus);
2671 if (!then_se)
2673 /* If there is no 'then', turn
2674 if (a || b); else d
2675 into
2676 if (a); else if (b); else d. */
2677 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2679 /* Keep the original source location on the first 'if'. */
2680 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2681 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2682 /* Set the source location of the || on the second 'if'. */
2683 if (EXPR_HAS_LOCATION (pred))
2684 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2685 else_ = shortcut_cond_expr (expr);
2686 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2687 pred = TREE_OPERAND (pred, 0);
2688 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2689 SET_EXPR_LOCATION (expr, locus);
2693 /* If we're done, great. */
2694 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2695 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2696 return expr;
2698 /* Otherwise we need to mess with gotos. Change
2699 if (a) c; else d;
2701 if (a); else goto no;
2702 c; goto end;
2703 no: d; end:
2704 and recursively gimplify the condition. */
2706 true_label = false_label = end_label = NULL_TREE;
2708 /* If our arms just jump somewhere, hijack those labels so we don't
2709 generate jumps to jumps. */
2711 if (then_
2712 && TREE_CODE (then_) == GOTO_EXPR
2713 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2715 true_label = GOTO_DESTINATION (then_);
2716 then_ = NULL;
2717 then_se = false;
2720 if (else_
2721 && TREE_CODE (else_) == GOTO_EXPR
2722 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2724 false_label = GOTO_DESTINATION (else_);
2725 else_ = NULL;
2726 else_se = false;
2729 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2730 if (true_label)
2731 true_label_p = &true_label;
2732 else
2733 true_label_p = NULL;
2735 /* The 'else' branch also needs a label if it contains interesting code. */
2736 if (false_label || else_se)
2737 false_label_p = &false_label;
2738 else
2739 false_label_p = NULL;
2741 /* If there was nothing else in our arms, just forward the label(s). */
2742 if (!then_se && !else_se)
2743 return shortcut_cond_r (pred, true_label_p, false_label_p,
2744 EXPR_LOC_OR_LOC (expr, input_location));
2746 /* If our last subexpression already has a terminal label, reuse it. */
2747 if (else_se)
2748 t = expr_last (else_);
2749 else if (then_se)
2750 t = expr_last (then_);
2751 else
2752 t = NULL;
2753 if (t && TREE_CODE (t) == LABEL_EXPR)
2754 end_label = LABEL_EXPR_LABEL (t);
2756 /* If we don't care about jumping to the 'else' branch, jump to the end
2757 if the condition is false. */
2758 if (!false_label_p)
2759 false_label_p = &end_label;
2761 /* We only want to emit these labels if we aren't hijacking them. */
2762 emit_end = (end_label == NULL_TREE);
2763 emit_false = (false_label == NULL_TREE);
2765 /* We only emit the jump over the else clause if we have to--if the
2766 then clause may fall through. Otherwise we can wind up with a
2767 useless jump and a useless label at the end of gimplified code,
2768 which will cause us to think that this conditional as a whole
2769 falls through even if it doesn't. If we then inline a function
2770 which ends with such a condition, that can cause us to issue an
2771 inappropriate warning about control reaching the end of a
2772 non-void function. */
2773 jump_over_else = block_may_fallthru (then_);
2775 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2776 EXPR_LOC_OR_LOC (expr, input_location));
2778 expr = NULL;
2779 append_to_statement_list (pred, &expr);
2781 append_to_statement_list (then_, &expr);
2782 if (else_se)
2784 if (jump_over_else)
2786 tree last = expr_last (expr);
2787 t = build_and_jump (&end_label);
2788 if (EXPR_HAS_LOCATION (last))
2789 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2790 append_to_statement_list (t, &expr);
2792 if (emit_false)
2794 t = build1 (LABEL_EXPR, void_type_node, false_label);
2795 append_to_statement_list (t, &expr);
2797 append_to_statement_list (else_, &expr);
2799 if (emit_end && end_label)
2801 t = build1 (LABEL_EXPR, void_type_node, end_label);
2802 append_to_statement_list (t, &expr);
2805 return expr;
2808 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2810 tree
2811 gimple_boolify (tree expr)
2813 tree type = TREE_TYPE (expr);
2814 location_t loc = EXPR_LOCATION (expr);
2816 if (TREE_CODE (expr) == NE_EXPR
2817 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2818 && integer_zerop (TREE_OPERAND (expr, 1)))
2820 tree call = TREE_OPERAND (expr, 0);
2821 tree fn = get_callee_fndecl (call);
2823 /* For __builtin_expect ((long) (x), y) recurse into x as well
2824 if x is truth_value_p. */
2825 if (fn
2826 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2827 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2828 && call_expr_nargs (call) == 2)
2830 tree arg = CALL_EXPR_ARG (call, 0);
2831 if (arg)
2833 if (TREE_CODE (arg) == NOP_EXPR
2834 && TREE_TYPE (arg) == TREE_TYPE (call))
2835 arg = TREE_OPERAND (arg, 0);
2836 if (truth_value_p (TREE_CODE (arg)))
2838 arg = gimple_boolify (arg);
2839 CALL_EXPR_ARG (call, 0)
2840 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2846 switch (TREE_CODE (expr))
2848 case TRUTH_AND_EXPR:
2849 case TRUTH_OR_EXPR:
2850 case TRUTH_XOR_EXPR:
2851 case TRUTH_ANDIF_EXPR:
2852 case TRUTH_ORIF_EXPR:
2853 /* Also boolify the arguments of truth exprs. */
2854 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2855 /* FALLTHRU */
2857 case TRUTH_NOT_EXPR:
2858 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2860 /* These expressions always produce boolean results. */
2861 if (TREE_CODE (type) != BOOLEAN_TYPE)
2862 TREE_TYPE (expr) = boolean_type_node;
2863 return expr;
2865 case ANNOTATE_EXPR:
2866 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2868 case annot_expr_ivdep_kind:
2869 case annot_expr_no_vector_kind:
2870 case annot_expr_vector_kind:
2871 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2872 if (TREE_CODE (type) != BOOLEAN_TYPE)
2873 TREE_TYPE (expr) = boolean_type_node;
2874 return expr;
2875 default:
2876 gcc_unreachable ();
2879 default:
2880 if (COMPARISON_CLASS_P (expr))
2882 /* There expressions always prduce boolean results. */
2883 if (TREE_CODE (type) != BOOLEAN_TYPE)
2884 TREE_TYPE (expr) = boolean_type_node;
2885 return expr;
2887 /* Other expressions that get here must have boolean values, but
2888 might need to be converted to the appropriate mode. */
2889 if (TREE_CODE (type) == BOOLEAN_TYPE)
2890 return expr;
2891 return fold_convert_loc (loc, boolean_type_node, expr);
2895 /* Given a conditional expression *EXPR_P without side effects, gimplify
2896 its operands. New statements are inserted to PRE_P. */
2898 static enum gimplify_status
2899 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2901 tree expr = *expr_p, cond;
2902 enum gimplify_status ret, tret;
2903 enum tree_code code;
2905 cond = gimple_boolify (COND_EXPR_COND (expr));
2907 /* We need to handle && and || specially, as their gimplification
2908 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2909 code = TREE_CODE (cond);
2910 if (code == TRUTH_ANDIF_EXPR)
2911 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2912 else if (code == TRUTH_ORIF_EXPR)
2913 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2914 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2915 COND_EXPR_COND (*expr_p) = cond;
2917 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2918 is_gimple_val, fb_rvalue);
2919 ret = MIN (ret, tret);
2920 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2921 is_gimple_val, fb_rvalue);
2923 return MIN (ret, tret);
2926 /* Return true if evaluating EXPR could trap.
2927 EXPR is GENERIC, while tree_could_trap_p can be called
2928 only on GIMPLE. */
2930 static bool
2931 generic_expr_could_trap_p (tree expr)
2933 unsigned i, n;
2935 if (!expr || is_gimple_val (expr))
2936 return false;
2938 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2939 return true;
2941 n = TREE_OPERAND_LENGTH (expr);
2942 for (i = 0; i < n; i++)
2943 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2944 return true;
2946 return false;
2949 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2950 into
2952 if (p) if (p)
2953 t1 = a; a;
2954 else or else
2955 t1 = b; b;
2958 The second form is used when *EXPR_P is of type void.
2960 PRE_P points to the list where side effects that must happen before
2961 *EXPR_P should be stored. */
2963 static enum gimplify_status
2964 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2966 tree expr = *expr_p;
2967 tree type = TREE_TYPE (expr);
2968 location_t loc = EXPR_LOCATION (expr);
2969 tree tmp, arm1, arm2;
2970 enum gimplify_status ret;
2971 tree label_true, label_false, label_cont;
2972 bool have_then_clause_p, have_else_clause_p;
2973 gimple gimple_cond;
2974 enum tree_code pred_code;
2975 gimple_seq seq = NULL;
2977 /* If this COND_EXPR has a value, copy the values into a temporary within
2978 the arms. */
2979 if (!VOID_TYPE_P (type))
2981 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2982 tree result;
2984 /* If either an rvalue is ok or we do not require an lvalue, create the
2985 temporary. But we cannot do that if the type is addressable. */
2986 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
2987 && !TREE_ADDRESSABLE (type))
2989 if (gimplify_ctxp->allow_rhs_cond_expr
2990 /* If either branch has side effects or could trap, it can't be
2991 evaluated unconditionally. */
2992 && !TREE_SIDE_EFFECTS (then_)
2993 && !generic_expr_could_trap_p (then_)
2994 && !TREE_SIDE_EFFECTS (else_)
2995 && !generic_expr_could_trap_p (else_))
2996 return gimplify_pure_cond_expr (expr_p, pre_p);
2998 tmp = create_tmp_var (type, "iftmp");
2999 result = tmp;
3002 /* Otherwise, only create and copy references to the values. */
3003 else
3005 type = build_pointer_type (type);
3007 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3008 then_ = build_fold_addr_expr_loc (loc, then_);
3010 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3011 else_ = build_fold_addr_expr_loc (loc, else_);
3013 expr
3014 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3016 tmp = create_tmp_var (type, "iftmp");
3017 result = build_simple_mem_ref_loc (loc, tmp);
3020 /* Build the new then clause, `tmp = then_;'. But don't build the
3021 assignment if the value is void; in C++ it can be if it's a throw. */
3022 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3023 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3025 /* Similarly, build the new else clause, `tmp = else_;'. */
3026 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3027 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3029 TREE_TYPE (expr) = void_type_node;
3030 recalculate_side_effects (expr);
3032 /* Move the COND_EXPR to the prequeue. */
3033 gimplify_stmt (&expr, pre_p);
3035 *expr_p = result;
3036 return GS_ALL_DONE;
3039 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3040 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3041 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3042 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3044 /* Make sure the condition has BOOLEAN_TYPE. */
3045 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3047 /* Break apart && and || conditions. */
3048 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3049 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3051 expr = shortcut_cond_expr (expr);
3053 if (expr != *expr_p)
3055 *expr_p = expr;
3057 /* We can't rely on gimplify_expr to re-gimplify the expanded
3058 form properly, as cleanups might cause the target labels to be
3059 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3060 set up a conditional context. */
3061 gimple_push_condition ();
3062 gimplify_stmt (expr_p, &seq);
3063 gimple_pop_condition (pre_p);
3064 gimple_seq_add_seq (pre_p, seq);
3066 return GS_ALL_DONE;
3070 /* Now do the normal gimplification. */
3072 /* Gimplify condition. */
3073 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3074 fb_rvalue);
3075 if (ret == GS_ERROR)
3076 return GS_ERROR;
3077 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3079 gimple_push_condition ();
3081 have_then_clause_p = have_else_clause_p = false;
3082 if (TREE_OPERAND (expr, 1) != NULL
3083 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3084 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3085 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3086 == current_function_decl)
3087 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3088 have different locations, otherwise we end up with incorrect
3089 location information on the branches. */
3090 && (optimize
3091 || !EXPR_HAS_LOCATION (expr)
3092 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3093 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3095 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3096 have_then_clause_p = true;
3098 else
3099 label_true = create_artificial_label (UNKNOWN_LOCATION);
3100 if (TREE_OPERAND (expr, 2) != NULL
3101 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3102 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3103 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3104 == current_function_decl)
3105 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3106 have different locations, otherwise we end up with incorrect
3107 location information on the branches. */
3108 && (optimize
3109 || !EXPR_HAS_LOCATION (expr)
3110 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3111 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3113 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3114 have_else_clause_p = true;
3116 else
3117 label_false = create_artificial_label (UNKNOWN_LOCATION);
3119 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3120 &arm2);
3122 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3123 label_false);
3125 gimplify_seq_add_stmt (&seq, gimple_cond);
3126 label_cont = NULL_TREE;
3127 if (!have_then_clause_p)
3129 /* For if (...) {} else { code; } put label_true after
3130 the else block. */
3131 if (TREE_OPERAND (expr, 1) == NULL_TREE
3132 && !have_else_clause_p
3133 && TREE_OPERAND (expr, 2) != NULL_TREE)
3134 label_cont = label_true;
3135 else
3137 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3138 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3139 /* For if (...) { code; } else {} or
3140 if (...) { code; } else goto label; or
3141 if (...) { code; return; } else { ... }
3142 label_cont isn't needed. */
3143 if (!have_else_clause_p
3144 && TREE_OPERAND (expr, 2) != NULL_TREE
3145 && gimple_seq_may_fallthru (seq))
3147 gimple g;
3148 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3150 g = gimple_build_goto (label_cont);
3152 /* GIMPLE_COND's are very low level; they have embedded
3153 gotos. This particular embedded goto should not be marked
3154 with the location of the original COND_EXPR, as it would
3155 correspond to the COND_EXPR's condition, not the ELSE or the
3156 THEN arms. To avoid marking it with the wrong location, flag
3157 it as "no location". */
3158 gimple_set_do_not_emit_location (g);
3160 gimplify_seq_add_stmt (&seq, g);
3164 if (!have_else_clause_p)
3166 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3167 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3169 if (label_cont)
3170 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3172 gimple_pop_condition (pre_p);
3173 gimple_seq_add_seq (pre_p, seq);
3175 if (ret == GS_ERROR)
3176 ; /* Do nothing. */
3177 else if (have_then_clause_p || have_else_clause_p)
3178 ret = GS_ALL_DONE;
3179 else
3181 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3182 expr = TREE_OPERAND (expr, 0);
3183 gimplify_stmt (&expr, pre_p);
3186 *expr_p = NULL;
3187 return ret;
3190 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3191 to be marked addressable.
3193 We cannot rely on such an expression being directly markable if a temporary
3194 has been created by the gimplification. In this case, we create another
3195 temporary and initialize it with a copy, which will become a store after we
3196 mark it addressable. This can happen if the front-end passed us something
3197 that it could not mark addressable yet, like a Fortran pass-by-reference
3198 parameter (int) floatvar. */
3200 static void
3201 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3203 while (handled_component_p (*expr_p))
3204 expr_p = &TREE_OPERAND (*expr_p, 0);
3205 if (is_gimple_reg (*expr_p))
3207 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3208 DECL_GIMPLE_REG_P (var) = 0;
3209 *expr_p = var;
3213 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3214 a call to __builtin_memcpy. */
3216 static enum gimplify_status
3217 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3218 gimple_seq *seq_p)
3220 tree t, to, to_ptr, from, from_ptr;
3221 gimple gs;
3222 location_t loc = EXPR_LOCATION (*expr_p);
3224 to = TREE_OPERAND (*expr_p, 0);
3225 from = TREE_OPERAND (*expr_p, 1);
3227 /* Mark the RHS addressable. Beware that it may not be possible to do so
3228 directly if a temporary has been created by the gimplification. */
3229 prepare_gimple_addressable (&from, seq_p);
3231 mark_addressable (from);
3232 from_ptr = build_fold_addr_expr_loc (loc, from);
3233 gimplify_arg (&from_ptr, seq_p, loc);
3235 mark_addressable (to);
3236 to_ptr = build_fold_addr_expr_loc (loc, to);
3237 gimplify_arg (&to_ptr, seq_p, loc);
3239 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3241 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3243 if (want_value)
3245 /* tmp = memcpy() */
3246 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3247 gimple_call_set_lhs (gs, t);
3248 gimplify_seq_add_stmt (seq_p, gs);
3250 *expr_p = build_simple_mem_ref (t);
3251 return GS_ALL_DONE;
3254 gimplify_seq_add_stmt (seq_p, gs);
3255 *expr_p = NULL;
3256 return GS_ALL_DONE;
3259 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3260 a call to __builtin_memset. In this case we know that the RHS is
3261 a CONSTRUCTOR with an empty element list. */
3263 static enum gimplify_status
3264 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3265 gimple_seq *seq_p)
3267 tree t, from, to, to_ptr;
3268 gimple gs;
3269 location_t loc = EXPR_LOCATION (*expr_p);
3271 /* Assert our assumptions, to abort instead of producing wrong code
3272 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3273 not be immediately exposed. */
3274 from = TREE_OPERAND (*expr_p, 1);
3275 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3276 from = TREE_OPERAND (from, 0);
3278 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3279 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3281 /* Now proceed. */
3282 to = TREE_OPERAND (*expr_p, 0);
3284 to_ptr = build_fold_addr_expr_loc (loc, to);
3285 gimplify_arg (&to_ptr, seq_p, loc);
3286 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3288 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3290 if (want_value)
3292 /* tmp = memset() */
3293 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3294 gimple_call_set_lhs (gs, t);
3295 gimplify_seq_add_stmt (seq_p, gs);
3297 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3298 return GS_ALL_DONE;
3301 gimplify_seq_add_stmt (seq_p, gs);
3302 *expr_p = NULL;
3303 return GS_ALL_DONE;
3306 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3307 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3308 assignment. Return non-null if we detect a potential overlap. */
3310 struct gimplify_init_ctor_preeval_data
3312 /* The base decl of the lhs object. May be NULL, in which case we
3313 have to assume the lhs is indirect. */
3314 tree lhs_base_decl;
3316 /* The alias set of the lhs object. */
3317 alias_set_type lhs_alias_set;
3320 static tree
3321 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3323 struct gimplify_init_ctor_preeval_data *data
3324 = (struct gimplify_init_ctor_preeval_data *) xdata;
3325 tree t = *tp;
3327 /* If we find the base object, obviously we have overlap. */
3328 if (data->lhs_base_decl == t)
3329 return t;
3331 /* If the constructor component is indirect, determine if we have a
3332 potential overlap with the lhs. The only bits of information we
3333 have to go on at this point are addressability and alias sets. */
3334 if ((INDIRECT_REF_P (t)
3335 || TREE_CODE (t) == MEM_REF)
3336 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3337 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3338 return t;
3340 /* If the constructor component is a call, determine if it can hide a
3341 potential overlap with the lhs through an INDIRECT_REF like above.
3342 ??? Ugh - this is completely broken. In fact this whole analysis
3343 doesn't look conservative. */
3344 if (TREE_CODE (t) == CALL_EXPR)
3346 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3348 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3349 if (POINTER_TYPE_P (TREE_VALUE (type))
3350 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3351 && alias_sets_conflict_p (data->lhs_alias_set,
3352 get_alias_set
3353 (TREE_TYPE (TREE_VALUE (type)))))
3354 return t;
3357 if (IS_TYPE_OR_DECL_P (t))
3358 *walk_subtrees = 0;
3359 return NULL;
3362 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3363 force values that overlap with the lhs (as described by *DATA)
3364 into temporaries. */
3366 static void
3367 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3368 struct gimplify_init_ctor_preeval_data *data)
3370 enum gimplify_status one;
3372 /* If the value is constant, then there's nothing to pre-evaluate. */
3373 if (TREE_CONSTANT (*expr_p))
3375 /* Ensure it does not have side effects, it might contain a reference to
3376 the object we're initializing. */
3377 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3378 return;
3381 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3382 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3383 return;
3385 /* Recurse for nested constructors. */
3386 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3388 unsigned HOST_WIDE_INT ix;
3389 constructor_elt *ce;
3390 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3392 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3393 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3395 return;
3398 /* If this is a variable sized type, we must remember the size. */
3399 maybe_with_size_expr (expr_p);
3401 /* Gimplify the constructor element to something appropriate for the rhs
3402 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3403 the gimplifier will consider this a store to memory. Doing this
3404 gimplification now means that we won't have to deal with complicated
3405 language-specific trees, nor trees like SAVE_EXPR that can induce
3406 exponential search behavior. */
3407 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3408 if (one == GS_ERROR)
3410 *expr_p = NULL;
3411 return;
3414 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3415 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3416 always be true for all scalars, since is_gimple_mem_rhs insists on a
3417 temporary variable for them. */
3418 if (DECL_P (*expr_p))
3419 return;
3421 /* If this is of variable size, we have no choice but to assume it doesn't
3422 overlap since we can't make a temporary for it. */
3423 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3424 return;
3426 /* Otherwise, we must search for overlap ... */
3427 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3428 return;
3430 /* ... and if found, force the value into a temporary. */
3431 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3434 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3435 a RANGE_EXPR in a CONSTRUCTOR for an array.
3437 var = lower;
3438 loop_entry:
3439 object[var] = value;
3440 if (var == upper)
3441 goto loop_exit;
3442 var = var + 1;
3443 goto loop_entry;
3444 loop_exit:
3446 We increment var _after_ the loop exit check because we might otherwise
3447 fail if upper == TYPE_MAX_VALUE (type for upper).
3449 Note that we never have to deal with SAVE_EXPRs here, because this has
3450 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3452 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3453 gimple_seq *, bool);
3455 static void
3456 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3457 tree value, tree array_elt_type,
3458 gimple_seq *pre_p, bool cleared)
3460 tree loop_entry_label, loop_exit_label, fall_thru_label;
3461 tree var, var_type, cref, tmp;
3463 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3464 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3465 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3467 /* Create and initialize the index variable. */
3468 var_type = TREE_TYPE (upper);
3469 var = create_tmp_var (var_type, NULL);
3470 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3472 /* Add the loop entry label. */
3473 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3475 /* Build the reference. */
3476 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3477 var, NULL_TREE, NULL_TREE);
3479 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3480 the store. Otherwise just assign value to the reference. */
3482 if (TREE_CODE (value) == CONSTRUCTOR)
3483 /* NB we might have to call ourself recursively through
3484 gimplify_init_ctor_eval if the value is a constructor. */
3485 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3486 pre_p, cleared);
3487 else
3488 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3490 /* We exit the loop when the index var is equal to the upper bound. */
3491 gimplify_seq_add_stmt (pre_p,
3492 gimple_build_cond (EQ_EXPR, var, upper,
3493 loop_exit_label, fall_thru_label));
3495 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3497 /* Otherwise, increment the index var... */
3498 tmp = build2 (PLUS_EXPR, var_type, var,
3499 fold_convert (var_type, integer_one_node));
3500 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3502 /* ...and jump back to the loop entry. */
3503 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3505 /* Add the loop exit label. */
3506 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3509 /* Return true if FDECL is accessing a field that is zero sized. */
3511 static bool
3512 zero_sized_field_decl (const_tree fdecl)
3514 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3515 && integer_zerop (DECL_SIZE (fdecl)))
3516 return true;
3517 return false;
3520 /* Return true if TYPE is zero sized. */
3522 static bool
3523 zero_sized_type (const_tree type)
3525 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3526 && integer_zerop (TYPE_SIZE (type)))
3527 return true;
3528 return false;
3531 /* A subroutine of gimplify_init_constructor. Generate individual
3532 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3533 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3534 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3535 zeroed first. */
3537 static void
3538 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3539 gimple_seq *pre_p, bool cleared)
3541 tree array_elt_type = NULL;
3542 unsigned HOST_WIDE_INT ix;
3543 tree purpose, value;
3545 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3546 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3548 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3550 tree cref;
3552 /* NULL values are created above for gimplification errors. */
3553 if (value == NULL)
3554 continue;
3556 if (cleared && initializer_zerop (value))
3557 continue;
3559 /* ??? Here's to hoping the front end fills in all of the indices,
3560 so we don't have to figure out what's missing ourselves. */
3561 gcc_assert (purpose);
3563 /* Skip zero-sized fields, unless value has side-effects. This can
3564 happen with calls to functions returning a zero-sized type, which
3565 we shouldn't discard. As a number of downstream passes don't
3566 expect sets of zero-sized fields, we rely on the gimplification of
3567 the MODIFY_EXPR we make below to drop the assignment statement. */
3568 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3569 continue;
3571 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3572 whole range. */
3573 if (TREE_CODE (purpose) == RANGE_EXPR)
3575 tree lower = TREE_OPERAND (purpose, 0);
3576 tree upper = TREE_OPERAND (purpose, 1);
3578 /* If the lower bound is equal to upper, just treat it as if
3579 upper was the index. */
3580 if (simple_cst_equal (lower, upper))
3581 purpose = upper;
3582 else
3584 gimplify_init_ctor_eval_range (object, lower, upper, value,
3585 array_elt_type, pre_p, cleared);
3586 continue;
3590 if (array_elt_type)
3592 /* Do not use bitsizetype for ARRAY_REF indices. */
3593 if (TYPE_DOMAIN (TREE_TYPE (object)))
3594 purpose
3595 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3596 purpose);
3597 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3598 purpose, NULL_TREE, NULL_TREE);
3600 else
3602 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3603 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3604 unshare_expr (object), purpose, NULL_TREE);
3607 if (TREE_CODE (value) == CONSTRUCTOR
3608 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3609 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3610 pre_p, cleared);
3611 else
3613 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3614 gimplify_and_add (init, pre_p);
3615 ggc_free (init);
3620 /* Return the appropriate RHS predicate for this LHS. */
3622 gimple_predicate
3623 rhs_predicate_for (tree lhs)
3625 if (is_gimple_reg (lhs))
3626 return is_gimple_reg_rhs_or_call;
3627 else
3628 return is_gimple_mem_rhs_or_call;
3631 /* Gimplify a C99 compound literal expression. This just means adding
3632 the DECL_EXPR before the current statement and using its anonymous
3633 decl instead. */
3635 static enum gimplify_status
3636 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3637 bool (*gimple_test_f) (tree),
3638 fallback_t fallback)
3640 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3641 tree decl = DECL_EXPR_DECL (decl_s);
3642 tree init = DECL_INITIAL (decl);
3643 /* Mark the decl as addressable if the compound literal
3644 expression is addressable now, otherwise it is marked too late
3645 after we gimplify the initialization expression. */
3646 if (TREE_ADDRESSABLE (*expr_p))
3647 TREE_ADDRESSABLE (decl) = 1;
3648 /* Otherwise, if we don't need an lvalue and have a literal directly
3649 substitute it. Check if it matches the gimple predicate, as
3650 otherwise we'd generate a new temporary, and we can as well just
3651 use the decl we already have. */
3652 else if (!TREE_ADDRESSABLE (decl)
3653 && init
3654 && (fallback & fb_lvalue) == 0
3655 && gimple_test_f (init))
3657 *expr_p = init;
3658 return GS_OK;
3661 /* Preliminarily mark non-addressed complex variables as eligible
3662 for promotion to gimple registers. We'll transform their uses
3663 as we find them. */
3664 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3665 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3666 && !TREE_THIS_VOLATILE (decl)
3667 && !needs_to_live_in_memory (decl))
3668 DECL_GIMPLE_REG_P (decl) = 1;
3670 /* If the decl is not addressable, then it is being used in some
3671 expression or on the right hand side of a statement, and it can
3672 be put into a readonly data section. */
3673 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3674 TREE_READONLY (decl) = 1;
3676 /* This decl isn't mentioned in the enclosing block, so add it to the
3677 list of temps. FIXME it seems a bit of a kludge to say that
3678 anonymous artificial vars aren't pushed, but everything else is. */
3679 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3680 gimple_add_tmp_var (decl);
3682 gimplify_and_add (decl_s, pre_p);
3683 *expr_p = decl;
3684 return GS_OK;
3687 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3688 return a new CONSTRUCTOR if something changed. */
3690 static tree
3691 optimize_compound_literals_in_ctor (tree orig_ctor)
3693 tree ctor = orig_ctor;
3694 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3695 unsigned int idx, num = vec_safe_length (elts);
3697 for (idx = 0; idx < num; idx++)
3699 tree value = (*elts)[idx].value;
3700 tree newval = value;
3701 if (TREE_CODE (value) == CONSTRUCTOR)
3702 newval = optimize_compound_literals_in_ctor (value);
3703 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3705 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3706 tree decl = DECL_EXPR_DECL (decl_s);
3707 tree init = DECL_INITIAL (decl);
3709 if (!TREE_ADDRESSABLE (value)
3710 && !TREE_ADDRESSABLE (decl)
3711 && init
3712 && TREE_CODE (init) == CONSTRUCTOR)
3713 newval = optimize_compound_literals_in_ctor (init);
3715 if (newval == value)
3716 continue;
3718 if (ctor == orig_ctor)
3720 ctor = copy_node (orig_ctor);
3721 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3722 elts = CONSTRUCTOR_ELTS (ctor);
3724 (*elts)[idx].value = newval;
3726 return ctor;
3729 /* A subroutine of gimplify_modify_expr. Break out elements of a
3730 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3732 Note that we still need to clear any elements that don't have explicit
3733 initializers, so if not all elements are initialized we keep the
3734 original MODIFY_EXPR, we just remove all of the constructor elements.
3736 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3737 GS_ERROR if we would have to create a temporary when gimplifying
3738 this constructor. Otherwise, return GS_OK.
3740 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3742 static enum gimplify_status
3743 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3744 bool want_value, bool notify_temp_creation)
3746 tree object, ctor, type;
3747 enum gimplify_status ret;
3748 vec<constructor_elt, va_gc> *elts;
3750 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3752 if (!notify_temp_creation)
3754 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3755 is_gimple_lvalue, fb_lvalue);
3756 if (ret == GS_ERROR)
3757 return ret;
3760 object = TREE_OPERAND (*expr_p, 0);
3761 ctor = TREE_OPERAND (*expr_p, 1) =
3762 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3763 type = TREE_TYPE (ctor);
3764 elts = CONSTRUCTOR_ELTS (ctor);
3765 ret = GS_ALL_DONE;
3767 switch (TREE_CODE (type))
3769 case RECORD_TYPE:
3770 case UNION_TYPE:
3771 case QUAL_UNION_TYPE:
3772 case ARRAY_TYPE:
3774 struct gimplify_init_ctor_preeval_data preeval_data;
3775 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3776 bool cleared, complete_p, valid_const_initializer;
3778 /* Aggregate types must lower constructors to initialization of
3779 individual elements. The exception is that a CONSTRUCTOR node
3780 with no elements indicates zero-initialization of the whole. */
3781 if (vec_safe_is_empty (elts))
3783 if (notify_temp_creation)
3784 return GS_OK;
3785 break;
3788 /* Fetch information about the constructor to direct later processing.
3789 We might want to make static versions of it in various cases, and
3790 can only do so if it known to be a valid constant initializer. */
3791 valid_const_initializer
3792 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3793 &num_ctor_elements, &complete_p);
3795 /* If a const aggregate variable is being initialized, then it
3796 should never be a lose to promote the variable to be static. */
3797 if (valid_const_initializer
3798 && num_nonzero_elements > 1
3799 && TREE_READONLY (object)
3800 && TREE_CODE (object) == VAR_DECL
3801 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3803 if (notify_temp_creation)
3804 return GS_ERROR;
3805 DECL_INITIAL (object) = ctor;
3806 TREE_STATIC (object) = 1;
3807 if (!DECL_NAME (object))
3808 DECL_NAME (object) = create_tmp_var_name ("C");
3809 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3811 /* ??? C++ doesn't automatically append a .<number> to the
3812 assembler name, and even when it does, it looks at FE private
3813 data structures to figure out what that number should be,
3814 which are not set for this variable. I suppose this is
3815 important for local statics for inline functions, which aren't
3816 "local" in the object file sense. So in order to get a unique
3817 TU-local symbol, we must invoke the lhd version now. */
3818 lhd_set_decl_assembler_name (object);
3820 *expr_p = NULL_TREE;
3821 break;
3824 /* If there are "lots" of initialized elements, even discounting
3825 those that are not address constants (and thus *must* be
3826 computed at runtime), then partition the constructor into
3827 constant and non-constant parts. Block copy the constant
3828 parts in, then generate code for the non-constant parts. */
3829 /* TODO. There's code in cp/typeck.c to do this. */
3831 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3832 /* store_constructor will ignore the clearing of variable-sized
3833 objects. Initializers for such objects must explicitly set
3834 every field that needs to be set. */
3835 cleared = false;
3836 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3837 /* If the constructor isn't complete, clear the whole object
3838 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3840 ??? This ought not to be needed. For any element not present
3841 in the initializer, we should simply set them to zero. Except
3842 we'd need to *find* the elements that are not present, and that
3843 requires trickery to avoid quadratic compile-time behavior in
3844 large cases or excessive memory use in small cases. */
3845 cleared = true;
3846 else if (num_ctor_elements - num_nonzero_elements
3847 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3848 && num_nonzero_elements < num_ctor_elements / 4)
3849 /* If there are "lots" of zeros, it's more efficient to clear
3850 the memory and then set the nonzero elements. */
3851 cleared = true;
3852 else
3853 cleared = false;
3855 /* If there are "lots" of initialized elements, and all of them
3856 are valid address constants, then the entire initializer can
3857 be dropped to memory, and then memcpy'd out. Don't do this
3858 for sparse arrays, though, as it's more efficient to follow
3859 the standard CONSTRUCTOR behavior of memset followed by
3860 individual element initialization. Also don't do this for small
3861 all-zero initializers (which aren't big enough to merit
3862 clearing), and don't try to make bitwise copies of
3863 TREE_ADDRESSABLE types. */
3864 if (valid_const_initializer
3865 && !(cleared || num_nonzero_elements == 0)
3866 && !TREE_ADDRESSABLE (type))
3868 HOST_WIDE_INT size = int_size_in_bytes (type);
3869 unsigned int align;
3871 /* ??? We can still get unbounded array types, at least
3872 from the C++ front end. This seems wrong, but attempt
3873 to work around it for now. */
3874 if (size < 0)
3876 size = int_size_in_bytes (TREE_TYPE (object));
3877 if (size >= 0)
3878 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3881 /* Find the maximum alignment we can assume for the object. */
3882 /* ??? Make use of DECL_OFFSET_ALIGN. */
3883 if (DECL_P (object))
3884 align = DECL_ALIGN (object);
3885 else
3886 align = TYPE_ALIGN (type);
3888 /* Do a block move either if the size is so small as to make
3889 each individual move a sub-unit move on average, or if it
3890 is so large as to make individual moves inefficient. */
3891 if (size > 0
3892 && num_nonzero_elements > 1
3893 && (size < num_nonzero_elements
3894 || !can_move_by_pieces (size, align)))
3896 if (notify_temp_creation)
3897 return GS_ERROR;
3899 walk_tree (&ctor, force_labels_r, NULL, NULL);
3900 ctor = tree_output_constant_def (ctor);
3901 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3902 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3903 TREE_OPERAND (*expr_p, 1) = ctor;
3905 /* This is no longer an assignment of a CONSTRUCTOR, but
3906 we still may have processing to do on the LHS. So
3907 pretend we didn't do anything here to let that happen. */
3908 return GS_UNHANDLED;
3912 /* If the target is volatile, we have non-zero elements and more than
3913 one field to assign, initialize the target from a temporary. */
3914 if (TREE_THIS_VOLATILE (object)
3915 && !TREE_ADDRESSABLE (type)
3916 && num_nonzero_elements > 0
3917 && vec_safe_length (elts) > 1)
3919 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3920 TREE_OPERAND (*expr_p, 0) = temp;
3921 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3922 *expr_p,
3923 build2 (MODIFY_EXPR, void_type_node,
3924 object, temp));
3925 return GS_OK;
3928 if (notify_temp_creation)
3929 return GS_OK;
3931 /* If there are nonzero elements and if needed, pre-evaluate to capture
3932 elements overlapping with the lhs into temporaries. We must do this
3933 before clearing to fetch the values before they are zeroed-out. */
3934 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3936 preeval_data.lhs_base_decl = get_base_address (object);
3937 if (!DECL_P (preeval_data.lhs_base_decl))
3938 preeval_data.lhs_base_decl = NULL;
3939 preeval_data.lhs_alias_set = get_alias_set (object);
3941 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3942 pre_p, post_p, &preeval_data);
3945 if (cleared)
3947 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3948 Note that we still have to gimplify, in order to handle the
3949 case of variable sized types. Avoid shared tree structures. */
3950 CONSTRUCTOR_ELTS (ctor) = NULL;
3951 TREE_SIDE_EFFECTS (ctor) = 0;
3952 object = unshare_expr (object);
3953 gimplify_stmt (expr_p, pre_p);
3956 /* If we have not block cleared the object, or if there are nonzero
3957 elements in the constructor, add assignments to the individual
3958 scalar fields of the object. */
3959 if (!cleared || num_nonzero_elements > 0)
3960 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3962 *expr_p = NULL_TREE;
3964 break;
3966 case COMPLEX_TYPE:
3968 tree r, i;
3970 if (notify_temp_creation)
3971 return GS_OK;
3973 /* Extract the real and imaginary parts out of the ctor. */
3974 gcc_assert (elts->length () == 2);
3975 r = (*elts)[0].value;
3976 i = (*elts)[1].value;
3977 if (r == NULL || i == NULL)
3979 tree zero = build_zero_cst (TREE_TYPE (type));
3980 if (r == NULL)
3981 r = zero;
3982 if (i == NULL)
3983 i = zero;
3986 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3987 represent creation of a complex value. */
3988 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3990 ctor = build_complex (type, r, i);
3991 TREE_OPERAND (*expr_p, 1) = ctor;
3993 else
3995 ctor = build2 (COMPLEX_EXPR, type, r, i);
3996 TREE_OPERAND (*expr_p, 1) = ctor;
3997 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
3998 pre_p,
3999 post_p,
4000 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4001 fb_rvalue);
4004 break;
4006 case VECTOR_TYPE:
4008 unsigned HOST_WIDE_INT ix;
4009 constructor_elt *ce;
4011 if (notify_temp_creation)
4012 return GS_OK;
4014 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4015 if (TREE_CONSTANT (ctor))
4017 bool constant_p = true;
4018 tree value;
4020 /* Even when ctor is constant, it might contain non-*_CST
4021 elements, such as addresses or trapping values like
4022 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4023 in VECTOR_CST nodes. */
4024 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4025 if (!CONSTANT_CLASS_P (value))
4027 constant_p = false;
4028 break;
4031 if (constant_p)
4033 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4034 break;
4037 /* Don't reduce an initializer constant even if we can't
4038 make a VECTOR_CST. It won't do anything for us, and it'll
4039 prevent us from representing it as a single constant. */
4040 if (initializer_constant_valid_p (ctor, type))
4041 break;
4043 TREE_CONSTANT (ctor) = 0;
4046 /* Vector types use CONSTRUCTOR all the way through gimple
4047 compilation as a general initializer. */
4048 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4050 enum gimplify_status tret;
4051 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4052 fb_rvalue);
4053 if (tret == GS_ERROR)
4054 ret = GS_ERROR;
4056 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4057 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4059 break;
4061 default:
4062 /* So how did we get a CONSTRUCTOR for a scalar type? */
4063 gcc_unreachable ();
4066 if (ret == GS_ERROR)
4067 return GS_ERROR;
4068 else if (want_value)
4070 *expr_p = object;
4071 return GS_OK;
4073 else
4075 /* If we have gimplified both sides of the initializer but have
4076 not emitted an assignment, do so now. */
4077 if (*expr_p)
4079 tree lhs = TREE_OPERAND (*expr_p, 0);
4080 tree rhs = TREE_OPERAND (*expr_p, 1);
4081 gimple init = gimple_build_assign (lhs, rhs);
4082 gimplify_seq_add_stmt (pre_p, init);
4083 *expr_p = NULL;
4086 return GS_ALL_DONE;
4090 /* Given a pointer value OP0, return a simplified version of an
4091 indirection through OP0, or NULL_TREE if no simplification is
4092 possible. This may only be applied to a rhs of an expression.
4093 Note that the resulting type may be different from the type pointed
4094 to in the sense that it is still compatible from the langhooks
4095 point of view. */
4097 static tree
4098 gimple_fold_indirect_ref_rhs (tree t)
4100 return gimple_fold_indirect_ref (t);
4103 /* Subroutine of gimplify_modify_expr to do simplifications of
4104 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4105 something changes. */
4107 static enum gimplify_status
4108 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4109 gimple_seq *pre_p, gimple_seq *post_p,
4110 bool want_value)
4112 enum gimplify_status ret = GS_UNHANDLED;
4113 bool changed;
4117 changed = false;
4118 switch (TREE_CODE (*from_p))
4120 case VAR_DECL:
4121 /* If we're assigning from a read-only variable initialized with
4122 a constructor, do the direct assignment from the constructor,
4123 but only if neither source nor target are volatile since this
4124 latter assignment might end up being done on a per-field basis. */
4125 if (DECL_INITIAL (*from_p)
4126 && TREE_READONLY (*from_p)
4127 && !TREE_THIS_VOLATILE (*from_p)
4128 && !TREE_THIS_VOLATILE (*to_p)
4129 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4131 tree old_from = *from_p;
4132 enum gimplify_status subret;
4134 /* Move the constructor into the RHS. */
4135 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4137 /* Let's see if gimplify_init_constructor will need to put
4138 it in memory. */
4139 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4140 false, true);
4141 if (subret == GS_ERROR)
4143 /* If so, revert the change. */
4144 *from_p = old_from;
4146 else
4148 ret = GS_OK;
4149 changed = true;
4152 break;
4153 case INDIRECT_REF:
4155 /* If we have code like
4157 *(const A*)(A*)&x
4159 where the type of "x" is a (possibly cv-qualified variant
4160 of "A"), treat the entire expression as identical to "x".
4161 This kind of code arises in C++ when an object is bound
4162 to a const reference, and if "x" is a TARGET_EXPR we want
4163 to take advantage of the optimization below. */
4164 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4165 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4166 if (t)
4168 if (TREE_THIS_VOLATILE (t) != volatile_p)
4170 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4171 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4172 build_fold_addr_expr (t));
4173 if (REFERENCE_CLASS_P (t))
4174 TREE_THIS_VOLATILE (t) = volatile_p;
4176 *from_p = t;
4177 ret = GS_OK;
4178 changed = true;
4180 break;
4183 case TARGET_EXPR:
4185 /* If we are initializing something from a TARGET_EXPR, strip the
4186 TARGET_EXPR and initialize it directly, if possible. This can't
4187 be done if the initializer is void, since that implies that the
4188 temporary is set in some non-trivial way.
4190 ??? What about code that pulls out the temp and uses it
4191 elsewhere? I think that such code never uses the TARGET_EXPR as
4192 an initializer. If I'm wrong, we'll die because the temp won't
4193 have any RTL. In that case, I guess we'll need to replace
4194 references somehow. */
4195 tree init = TARGET_EXPR_INITIAL (*from_p);
4197 if (init
4198 && !VOID_TYPE_P (TREE_TYPE (init)))
4200 *from_p = init;
4201 ret = GS_OK;
4202 changed = true;
4205 break;
4207 case COMPOUND_EXPR:
4208 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4209 caught. */
4210 gimplify_compound_expr (from_p, pre_p, true);
4211 ret = GS_OK;
4212 changed = true;
4213 break;
4215 case CONSTRUCTOR:
4216 /* If we already made some changes, let the front end have a
4217 crack at this before we break it down. */
4218 if (ret != GS_UNHANDLED)
4219 break;
4220 /* If we're initializing from a CONSTRUCTOR, break this into
4221 individual MODIFY_EXPRs. */
4222 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4223 false);
4225 case COND_EXPR:
4226 /* If we're assigning to a non-register type, push the assignment
4227 down into the branches. This is mandatory for ADDRESSABLE types,
4228 since we cannot generate temporaries for such, but it saves a
4229 copy in other cases as well. */
4230 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4232 /* This code should mirror the code in gimplify_cond_expr. */
4233 enum tree_code code = TREE_CODE (*expr_p);
4234 tree cond = *from_p;
4235 tree result = *to_p;
4237 ret = gimplify_expr (&result, pre_p, post_p,
4238 is_gimple_lvalue, fb_lvalue);
4239 if (ret != GS_ERROR)
4240 ret = GS_OK;
4242 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4243 TREE_OPERAND (cond, 1)
4244 = build2 (code, void_type_node, result,
4245 TREE_OPERAND (cond, 1));
4246 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4247 TREE_OPERAND (cond, 2)
4248 = build2 (code, void_type_node, unshare_expr (result),
4249 TREE_OPERAND (cond, 2));
4251 TREE_TYPE (cond) = void_type_node;
4252 recalculate_side_effects (cond);
4254 if (want_value)
4256 gimplify_and_add (cond, pre_p);
4257 *expr_p = unshare_expr (result);
4259 else
4260 *expr_p = cond;
4261 return ret;
4263 break;
4265 case CALL_EXPR:
4266 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4267 return slot so that we don't generate a temporary. */
4268 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4269 && aggregate_value_p (*from_p, *from_p))
4271 bool use_target;
4273 if (!(rhs_predicate_for (*to_p))(*from_p))
4274 /* If we need a temporary, *to_p isn't accurate. */
4275 use_target = false;
4276 /* It's OK to use the return slot directly unless it's an NRV. */
4277 else if (TREE_CODE (*to_p) == RESULT_DECL
4278 && DECL_NAME (*to_p) == NULL_TREE
4279 && needs_to_live_in_memory (*to_p))
4280 use_target = true;
4281 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4282 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4283 /* Don't force regs into memory. */
4284 use_target = false;
4285 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4286 /* It's OK to use the target directly if it's being
4287 initialized. */
4288 use_target = true;
4289 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4290 /* Always use the target and thus RSO for variable-sized types.
4291 GIMPLE cannot deal with a variable-sized assignment
4292 embedded in a call statement. */
4293 use_target = true;
4294 else if (TREE_CODE (*to_p) != SSA_NAME
4295 && (!is_gimple_variable (*to_p)
4296 || needs_to_live_in_memory (*to_p)))
4297 /* Don't use the original target if it's already addressable;
4298 if its address escapes, and the called function uses the
4299 NRV optimization, a conforming program could see *to_p
4300 change before the called function returns; see c++/19317.
4301 When optimizing, the return_slot pass marks more functions
4302 as safe after we have escape info. */
4303 use_target = false;
4304 else
4305 use_target = true;
4307 if (use_target)
4309 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4310 mark_addressable (*to_p);
4313 break;
4315 case WITH_SIZE_EXPR:
4316 /* Likewise for calls that return an aggregate of non-constant size,
4317 since we would not be able to generate a temporary at all. */
4318 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4320 *from_p = TREE_OPERAND (*from_p, 0);
4321 /* We don't change ret in this case because the
4322 WITH_SIZE_EXPR might have been added in
4323 gimplify_modify_expr, so returning GS_OK would lead to an
4324 infinite loop. */
4325 changed = true;
4327 break;
4329 /* If we're initializing from a container, push the initialization
4330 inside it. */
4331 case CLEANUP_POINT_EXPR:
4332 case BIND_EXPR:
4333 case STATEMENT_LIST:
4335 tree wrap = *from_p;
4336 tree t;
4338 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4339 fb_lvalue);
4340 if (ret != GS_ERROR)
4341 ret = GS_OK;
4343 t = voidify_wrapper_expr (wrap, *expr_p);
4344 gcc_assert (t == *expr_p);
4346 if (want_value)
4348 gimplify_and_add (wrap, pre_p);
4349 *expr_p = unshare_expr (*to_p);
4351 else
4352 *expr_p = wrap;
4353 return GS_OK;
4356 case COMPOUND_LITERAL_EXPR:
4358 tree complit = TREE_OPERAND (*expr_p, 1);
4359 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4360 tree decl = DECL_EXPR_DECL (decl_s);
4361 tree init = DECL_INITIAL (decl);
4363 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4364 into struct T x = { 0, 1, 2 } if the address of the
4365 compound literal has never been taken. */
4366 if (!TREE_ADDRESSABLE (complit)
4367 && !TREE_ADDRESSABLE (decl)
4368 && init)
4370 *expr_p = copy_node (*expr_p);
4371 TREE_OPERAND (*expr_p, 1) = init;
4372 return GS_OK;
4376 default:
4377 break;
4380 while (changed);
4382 return ret;
4386 /* Return true if T looks like a valid GIMPLE statement. */
4388 static bool
4389 is_gimple_stmt (tree t)
4391 const enum tree_code code = TREE_CODE (t);
4393 switch (code)
4395 case NOP_EXPR:
4396 /* The only valid NOP_EXPR is the empty statement. */
4397 return IS_EMPTY_STMT (t);
4399 case BIND_EXPR:
4400 case COND_EXPR:
4401 /* These are only valid if they're void. */
4402 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4404 case SWITCH_EXPR:
4405 case GOTO_EXPR:
4406 case RETURN_EXPR:
4407 case LABEL_EXPR:
4408 case CASE_LABEL_EXPR:
4409 case TRY_CATCH_EXPR:
4410 case TRY_FINALLY_EXPR:
4411 case EH_FILTER_EXPR:
4412 case CATCH_EXPR:
4413 case ASM_EXPR:
4414 case STATEMENT_LIST:
4415 case OACC_PARALLEL:
4416 case OACC_KERNELS:
4417 case OACC_DATA:
4418 case OACC_HOST_DATA:
4419 case OACC_DECLARE:
4420 case OACC_UPDATE:
4421 case OACC_ENTER_DATA:
4422 case OACC_EXIT_DATA:
4423 case OACC_WAIT:
4424 case OACC_CACHE:
4425 case OMP_PARALLEL:
4426 case OMP_FOR:
4427 case OMP_SIMD:
4428 case CILK_SIMD:
4429 case OMP_DISTRIBUTE:
4430 case OACC_LOOP:
4431 case OMP_SECTIONS:
4432 case OMP_SECTION:
4433 case OMP_SINGLE:
4434 case OMP_MASTER:
4435 case OMP_TASKGROUP:
4436 case OMP_ORDERED:
4437 case OMP_CRITICAL:
4438 case OMP_TASK:
4439 /* These are always void. */
4440 return true;
4442 case CALL_EXPR:
4443 case MODIFY_EXPR:
4444 case PREDICT_EXPR:
4445 /* These are valid regardless of their type. */
4446 return true;
4448 default:
4449 return false;
4454 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4455 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4456 DECL_GIMPLE_REG_P set.
4458 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4459 other, unmodified part of the complex object just before the total store.
4460 As a consequence, if the object is still uninitialized, an undefined value
4461 will be loaded into a register, which may result in a spurious exception
4462 if the register is floating-point and the value happens to be a signaling
4463 NaN for example. Then the fully-fledged complex operations lowering pass
4464 followed by a DCE pass are necessary in order to fix things up. */
4466 static enum gimplify_status
4467 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4468 bool want_value)
4470 enum tree_code code, ocode;
4471 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4473 lhs = TREE_OPERAND (*expr_p, 0);
4474 rhs = TREE_OPERAND (*expr_p, 1);
4475 code = TREE_CODE (lhs);
4476 lhs = TREE_OPERAND (lhs, 0);
4478 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4479 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4480 TREE_NO_WARNING (other) = 1;
4481 other = get_formal_tmp_var (other, pre_p);
4483 realpart = code == REALPART_EXPR ? rhs : other;
4484 imagpart = code == REALPART_EXPR ? other : rhs;
4486 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4487 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4488 else
4489 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4491 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4492 *expr_p = (want_value) ? rhs : NULL_TREE;
4494 return GS_ALL_DONE;
4497 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4499 modify_expr
4500 : varname '=' rhs
4501 | '*' ID '=' rhs
4503 PRE_P points to the list where side effects that must happen before
4504 *EXPR_P should be stored.
4506 POST_P points to the list where side effects that must happen after
4507 *EXPR_P should be stored.
4509 WANT_VALUE is nonzero iff we want to use the value of this expression
4510 in another expression. */
4512 static enum gimplify_status
4513 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4514 bool want_value)
4516 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4517 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4518 enum gimplify_status ret = GS_UNHANDLED;
4519 gimple assign;
4520 location_t loc = EXPR_LOCATION (*expr_p);
4521 gimple_stmt_iterator gsi;
4523 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4524 || TREE_CODE (*expr_p) == INIT_EXPR);
4526 /* Trying to simplify a clobber using normal logic doesn't work,
4527 so handle it here. */
4528 if (TREE_CLOBBER_P (*from_p))
4530 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4531 if (ret == GS_ERROR)
4532 return ret;
4533 gcc_assert (!want_value
4534 && (TREE_CODE (*to_p) == VAR_DECL
4535 || TREE_CODE (*to_p) == MEM_REF));
4536 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4537 *expr_p = NULL;
4538 return GS_ALL_DONE;
4541 /* Insert pointer conversions required by the middle-end that are not
4542 required by the frontend. This fixes middle-end type checking for
4543 for example gcc.dg/redecl-6.c. */
4544 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4546 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4547 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4548 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4551 /* See if any simplifications can be done based on what the RHS is. */
4552 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4553 want_value);
4554 if (ret != GS_UNHANDLED)
4555 return ret;
4557 /* For zero sized types only gimplify the left hand side and right hand
4558 side as statements and throw away the assignment. Do this after
4559 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4560 types properly. */
4561 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4563 gimplify_stmt (from_p, pre_p);
4564 gimplify_stmt (to_p, pre_p);
4565 *expr_p = NULL_TREE;
4566 return GS_ALL_DONE;
4569 /* If the value being copied is of variable width, compute the length
4570 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4571 before gimplifying any of the operands so that we can resolve any
4572 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4573 the size of the expression to be copied, not of the destination, so
4574 that is what we must do here. */
4575 maybe_with_size_expr (from_p);
4577 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4578 if (ret == GS_ERROR)
4579 return ret;
4581 /* As a special case, we have to temporarily allow for assignments
4582 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4583 a toplevel statement, when gimplifying the GENERIC expression
4584 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4585 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4587 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4588 prevent gimplify_expr from trying to create a new temporary for
4589 foo's LHS, we tell it that it should only gimplify until it
4590 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4591 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4592 and all we need to do here is set 'a' to be its LHS. */
4593 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4594 fb_rvalue);
4595 if (ret == GS_ERROR)
4596 return ret;
4598 /* Now see if the above changed *from_p to something we handle specially. */
4599 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4600 want_value);
4601 if (ret != GS_UNHANDLED)
4602 return ret;
4604 /* If we've got a variable sized assignment between two lvalues (i.e. does
4605 not involve a call), then we can make things a bit more straightforward
4606 by converting the assignment to memcpy or memset. */
4607 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4609 tree from = TREE_OPERAND (*from_p, 0);
4610 tree size = TREE_OPERAND (*from_p, 1);
4612 if (TREE_CODE (from) == CONSTRUCTOR)
4613 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4615 if (is_gimple_addressable (from))
4617 *from_p = from;
4618 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4619 pre_p);
4623 /* Transform partial stores to non-addressable complex variables into
4624 total stores. This allows us to use real instead of virtual operands
4625 for these variables, which improves optimization. */
4626 if ((TREE_CODE (*to_p) == REALPART_EXPR
4627 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4628 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4629 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4631 /* Try to alleviate the effects of the gimplification creating artificial
4632 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4633 if (!gimplify_ctxp->into_ssa
4634 && TREE_CODE (*from_p) == VAR_DECL
4635 && DECL_IGNORED_P (*from_p)
4636 && DECL_P (*to_p)
4637 && !DECL_IGNORED_P (*to_p))
4639 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4640 DECL_NAME (*from_p)
4641 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4642 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4643 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4646 if (want_value && TREE_THIS_VOLATILE (*to_p))
4647 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4649 if (TREE_CODE (*from_p) == CALL_EXPR)
4651 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4652 instead of a GIMPLE_ASSIGN. */
4653 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4654 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4655 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4656 tree fndecl = get_callee_fndecl (*from_p);
4657 if (fndecl
4658 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4659 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4660 && call_expr_nargs (*from_p) == 3)
4661 assign = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4662 CALL_EXPR_ARG (*from_p, 0),
4663 CALL_EXPR_ARG (*from_p, 1),
4664 CALL_EXPR_ARG (*from_p, 2));
4665 else
4667 assign = gimple_build_call_from_tree (*from_p);
4668 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4670 notice_special_calls (assign);
4671 if (!gimple_call_noreturn_p (assign))
4672 gimple_call_set_lhs (assign, *to_p);
4674 else
4676 assign = gimple_build_assign (*to_p, *from_p);
4677 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4680 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4682 /* We should have got an SSA name from the start. */
4683 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4686 gimplify_seq_add_stmt (pre_p, assign);
4687 gsi = gsi_last (*pre_p);
4688 maybe_fold_stmt (&gsi);
4690 if (want_value)
4692 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4693 return GS_OK;
4695 else
4696 *expr_p = NULL;
4698 return GS_ALL_DONE;
4701 /* Gimplify a comparison between two variable-sized objects. Do this
4702 with a call to BUILT_IN_MEMCMP. */
4704 static enum gimplify_status
4705 gimplify_variable_sized_compare (tree *expr_p)
4707 location_t loc = EXPR_LOCATION (*expr_p);
4708 tree op0 = TREE_OPERAND (*expr_p, 0);
4709 tree op1 = TREE_OPERAND (*expr_p, 1);
4710 tree t, arg, dest, src, expr;
4712 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4713 arg = unshare_expr (arg);
4714 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4715 src = build_fold_addr_expr_loc (loc, op1);
4716 dest = build_fold_addr_expr_loc (loc, op0);
4717 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4718 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4720 expr
4721 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4722 SET_EXPR_LOCATION (expr, loc);
4723 *expr_p = expr;
4725 return GS_OK;
4728 /* Gimplify a comparison between two aggregate objects of integral scalar
4729 mode as a comparison between the bitwise equivalent scalar values. */
4731 static enum gimplify_status
4732 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4734 location_t loc = EXPR_LOCATION (*expr_p);
4735 tree op0 = TREE_OPERAND (*expr_p, 0);
4736 tree op1 = TREE_OPERAND (*expr_p, 1);
4738 tree type = TREE_TYPE (op0);
4739 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4741 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4742 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4744 *expr_p
4745 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4747 return GS_OK;
4750 /* Gimplify an expression sequence. This function gimplifies each
4751 expression and rewrites the original expression with the last
4752 expression of the sequence in GIMPLE form.
4754 PRE_P points to the list where the side effects for all the
4755 expressions in the sequence will be emitted.
4757 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4759 static enum gimplify_status
4760 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4762 tree t = *expr_p;
4766 tree *sub_p = &TREE_OPERAND (t, 0);
4768 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4769 gimplify_compound_expr (sub_p, pre_p, false);
4770 else
4771 gimplify_stmt (sub_p, pre_p);
4773 t = TREE_OPERAND (t, 1);
4775 while (TREE_CODE (t) == COMPOUND_EXPR);
4777 *expr_p = t;
4778 if (want_value)
4779 return GS_OK;
4780 else
4782 gimplify_stmt (expr_p, pre_p);
4783 return GS_ALL_DONE;
4787 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4788 gimplify. After gimplification, EXPR_P will point to a new temporary
4789 that holds the original value of the SAVE_EXPR node.
4791 PRE_P points to the list where side effects that must happen before
4792 *EXPR_P should be stored. */
4794 static enum gimplify_status
4795 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4797 enum gimplify_status ret = GS_ALL_DONE;
4798 tree val;
4800 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4801 val = TREE_OPERAND (*expr_p, 0);
4803 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4804 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4806 /* The operand may be a void-valued expression such as SAVE_EXPRs
4807 generated by the Java frontend for class initialization. It is
4808 being executed only for its side-effects. */
4809 if (TREE_TYPE (val) == void_type_node)
4811 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4812 is_gimple_stmt, fb_none);
4813 val = NULL;
4815 else
4816 val = get_initialized_tmp_var (val, pre_p, post_p);
4818 TREE_OPERAND (*expr_p, 0) = val;
4819 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4822 *expr_p = val;
4824 return ret;
4827 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4829 unary_expr
4830 : ...
4831 | '&' varname
4834 PRE_P points to the list where side effects that must happen before
4835 *EXPR_P should be stored.
4837 POST_P points to the list where side effects that must happen after
4838 *EXPR_P should be stored. */
4840 static enum gimplify_status
4841 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4843 tree expr = *expr_p;
4844 tree op0 = TREE_OPERAND (expr, 0);
4845 enum gimplify_status ret;
4846 location_t loc = EXPR_LOCATION (*expr_p);
4848 switch (TREE_CODE (op0))
4850 case INDIRECT_REF:
4851 do_indirect_ref:
4852 /* Check if we are dealing with an expression of the form '&*ptr'.
4853 While the front end folds away '&*ptr' into 'ptr', these
4854 expressions may be generated internally by the compiler (e.g.,
4855 builtins like __builtin_va_end). */
4856 /* Caution: the silent array decomposition semantics we allow for
4857 ADDR_EXPR means we can't always discard the pair. */
4858 /* Gimplification of the ADDR_EXPR operand may drop
4859 cv-qualification conversions, so make sure we add them if
4860 needed. */
4862 tree op00 = TREE_OPERAND (op0, 0);
4863 tree t_expr = TREE_TYPE (expr);
4864 tree t_op00 = TREE_TYPE (op00);
4866 if (!useless_type_conversion_p (t_expr, t_op00))
4867 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4868 *expr_p = op00;
4869 ret = GS_OK;
4871 break;
4873 case VIEW_CONVERT_EXPR:
4874 /* Take the address of our operand and then convert it to the type of
4875 this ADDR_EXPR.
4877 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4878 all clear. The impact of this transformation is even less clear. */
4880 /* If the operand is a useless conversion, look through it. Doing so
4881 guarantees that the ADDR_EXPR and its operand will remain of the
4882 same type. */
4883 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4884 op0 = TREE_OPERAND (op0, 0);
4886 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4887 build_fold_addr_expr_loc (loc,
4888 TREE_OPERAND (op0, 0)));
4889 ret = GS_OK;
4890 break;
4892 default:
4893 /* We use fb_either here because the C frontend sometimes takes
4894 the address of a call that returns a struct; see
4895 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4896 the implied temporary explicit. */
4898 /* Make the operand addressable. */
4899 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4900 is_gimple_addressable, fb_either);
4901 if (ret == GS_ERROR)
4902 break;
4904 /* Then mark it. Beware that it may not be possible to do so directly
4905 if a temporary has been created by the gimplification. */
4906 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4908 op0 = TREE_OPERAND (expr, 0);
4910 /* For various reasons, the gimplification of the expression
4911 may have made a new INDIRECT_REF. */
4912 if (TREE_CODE (op0) == INDIRECT_REF)
4913 goto do_indirect_ref;
4915 mark_addressable (TREE_OPERAND (expr, 0));
4917 /* The FEs may end up building ADDR_EXPRs early on a decl with
4918 an incomplete type. Re-build ADDR_EXPRs in canonical form
4919 here. */
4920 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4921 *expr_p = build_fold_addr_expr (op0);
4923 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4924 recompute_tree_invariant_for_addr_expr (*expr_p);
4926 /* If we re-built the ADDR_EXPR add a conversion to the original type
4927 if required. */
4928 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4929 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4931 break;
4934 return ret;
4937 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4938 value; output operands should be a gimple lvalue. */
4940 static enum gimplify_status
4941 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4943 tree expr;
4944 int noutputs;
4945 const char **oconstraints;
4946 int i;
4947 tree link;
4948 const char *constraint;
4949 bool allows_mem, allows_reg, is_inout;
4950 enum gimplify_status ret, tret;
4951 gimple stmt;
4952 vec<tree, va_gc> *inputs;
4953 vec<tree, va_gc> *outputs;
4954 vec<tree, va_gc> *clobbers;
4955 vec<tree, va_gc> *labels;
4956 tree link_next;
4958 expr = *expr_p;
4959 noutputs = list_length (ASM_OUTPUTS (expr));
4960 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4962 inputs = NULL;
4963 outputs = NULL;
4964 clobbers = NULL;
4965 labels = NULL;
4967 ret = GS_ALL_DONE;
4968 link_next = NULL_TREE;
4969 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4971 bool ok;
4972 size_t constraint_len;
4974 link_next = TREE_CHAIN (link);
4976 oconstraints[i]
4977 = constraint
4978 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4979 constraint_len = strlen (constraint);
4980 if (constraint_len == 0)
4981 continue;
4983 ok = parse_output_constraint (&constraint, i, 0, 0,
4984 &allows_mem, &allows_reg, &is_inout);
4985 if (!ok)
4987 ret = GS_ERROR;
4988 is_inout = false;
4991 if (!allows_reg && allows_mem)
4992 mark_addressable (TREE_VALUE (link));
4994 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4995 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4996 fb_lvalue | fb_mayfail);
4997 if (tret == GS_ERROR)
4999 error ("invalid lvalue in asm output %d", i);
5000 ret = tret;
5003 vec_safe_push (outputs, link);
5004 TREE_CHAIN (link) = NULL_TREE;
5006 if (is_inout)
5008 /* An input/output operand. To give the optimizers more
5009 flexibility, split it into separate input and output
5010 operands. */
5011 tree input;
5012 char buf[10];
5014 /* Turn the in/out constraint into an output constraint. */
5015 char *p = xstrdup (constraint);
5016 p[0] = '=';
5017 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5019 /* And add a matching input constraint. */
5020 if (allows_reg)
5022 sprintf (buf, "%d", i);
5024 /* If there are multiple alternatives in the constraint,
5025 handle each of them individually. Those that allow register
5026 will be replaced with operand number, the others will stay
5027 unchanged. */
5028 if (strchr (p, ',') != NULL)
5030 size_t len = 0, buflen = strlen (buf);
5031 char *beg, *end, *str, *dst;
5033 for (beg = p + 1;;)
5035 end = strchr (beg, ',');
5036 if (end == NULL)
5037 end = strchr (beg, '\0');
5038 if ((size_t) (end - beg) < buflen)
5039 len += buflen + 1;
5040 else
5041 len += end - beg + 1;
5042 if (*end)
5043 beg = end + 1;
5044 else
5045 break;
5048 str = (char *) alloca (len);
5049 for (beg = p + 1, dst = str;;)
5051 const char *tem;
5052 bool mem_p, reg_p, inout_p;
5054 end = strchr (beg, ',');
5055 if (end)
5056 *end = '\0';
5057 beg[-1] = '=';
5058 tem = beg - 1;
5059 parse_output_constraint (&tem, i, 0, 0,
5060 &mem_p, &reg_p, &inout_p);
5061 if (dst != str)
5062 *dst++ = ',';
5063 if (reg_p)
5065 memcpy (dst, buf, buflen);
5066 dst += buflen;
5068 else
5070 if (end)
5071 len = end - beg;
5072 else
5073 len = strlen (beg);
5074 memcpy (dst, beg, len);
5075 dst += len;
5077 if (end)
5078 beg = end + 1;
5079 else
5080 break;
5082 *dst = '\0';
5083 input = build_string (dst - str, str);
5085 else
5086 input = build_string (strlen (buf), buf);
5088 else
5089 input = build_string (constraint_len - 1, constraint + 1);
5091 free (p);
5093 input = build_tree_list (build_tree_list (NULL_TREE, input),
5094 unshare_expr (TREE_VALUE (link)));
5095 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5099 link_next = NULL_TREE;
5100 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5102 link_next = TREE_CHAIN (link);
5103 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5104 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5105 oconstraints, &allows_mem, &allows_reg);
5107 /* If we can't make copies, we can only accept memory. */
5108 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5110 if (allows_mem)
5111 allows_reg = 0;
5112 else
5114 error ("impossible constraint in %<asm%>");
5115 error ("non-memory input %d must stay in memory", i);
5116 return GS_ERROR;
5120 /* If the operand is a memory input, it should be an lvalue. */
5121 if (!allows_reg && allows_mem)
5123 tree inputv = TREE_VALUE (link);
5124 STRIP_NOPS (inputv);
5125 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5126 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5127 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5128 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5129 TREE_VALUE (link) = error_mark_node;
5130 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5131 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5132 mark_addressable (TREE_VALUE (link));
5133 if (tret == GS_ERROR)
5135 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5136 input_location = EXPR_LOCATION (TREE_VALUE (link));
5137 error ("memory input %d is not directly addressable", i);
5138 ret = tret;
5141 else
5143 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5144 is_gimple_asm_val, fb_rvalue);
5145 if (tret == GS_ERROR)
5146 ret = tret;
5149 TREE_CHAIN (link) = NULL_TREE;
5150 vec_safe_push (inputs, link);
5153 link_next = NULL_TREE;
5154 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5156 link_next = TREE_CHAIN (link);
5157 TREE_CHAIN (link) = NULL_TREE;
5158 vec_safe_push (clobbers, link);
5161 link_next = NULL_TREE;
5162 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5164 link_next = TREE_CHAIN (link);
5165 TREE_CHAIN (link) = NULL_TREE;
5166 vec_safe_push (labels, link);
5169 /* Do not add ASMs with errors to the gimple IL stream. */
5170 if (ret != GS_ERROR)
5172 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5173 inputs, outputs, clobbers, labels);
5175 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5176 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5178 gimplify_seq_add_stmt (pre_p, stmt);
5181 return ret;
5184 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5185 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5186 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5187 return to this function.
5189 FIXME should we complexify the prequeue handling instead? Or use flags
5190 for all the cleanups and let the optimizer tighten them up? The current
5191 code seems pretty fragile; it will break on a cleanup within any
5192 non-conditional nesting. But any such nesting would be broken, anyway;
5193 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5194 and continues out of it. We can do that at the RTL level, though, so
5195 having an optimizer to tighten up try/finally regions would be a Good
5196 Thing. */
5198 static enum gimplify_status
5199 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5201 gimple_stmt_iterator iter;
5202 gimple_seq body_sequence = NULL;
5204 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5206 /* We only care about the number of conditions between the innermost
5207 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5208 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5209 int old_conds = gimplify_ctxp->conditions;
5210 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5211 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5212 gimplify_ctxp->conditions = 0;
5213 gimplify_ctxp->conditional_cleanups = NULL;
5214 gimplify_ctxp->in_cleanup_point_expr = true;
5216 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5218 gimplify_ctxp->conditions = old_conds;
5219 gimplify_ctxp->conditional_cleanups = old_cleanups;
5220 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5222 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5224 gimple wce = gsi_stmt (iter);
5226 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5228 if (gsi_one_before_end_p (iter))
5230 /* Note that gsi_insert_seq_before and gsi_remove do not
5231 scan operands, unlike some other sequence mutators. */
5232 if (!gimple_wce_cleanup_eh_only (wce))
5233 gsi_insert_seq_before_without_update (&iter,
5234 gimple_wce_cleanup (wce),
5235 GSI_SAME_STMT);
5236 gsi_remove (&iter, true);
5237 break;
5239 else
5241 gimple_statement_try *gtry;
5242 gimple_seq seq;
5243 enum gimple_try_flags kind;
5245 if (gimple_wce_cleanup_eh_only (wce))
5246 kind = GIMPLE_TRY_CATCH;
5247 else
5248 kind = GIMPLE_TRY_FINALLY;
5249 seq = gsi_split_seq_after (iter);
5251 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5252 /* Do not use gsi_replace here, as it may scan operands.
5253 We want to do a simple structural modification only. */
5254 gsi_set_stmt (&iter, gtry);
5255 iter = gsi_start (gtry->eval);
5258 else
5259 gsi_next (&iter);
5262 gimplify_seq_add_seq (pre_p, body_sequence);
5263 if (temp)
5265 *expr_p = temp;
5266 return GS_OK;
5268 else
5270 *expr_p = NULL;
5271 return GS_ALL_DONE;
5275 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5276 is the cleanup action required. EH_ONLY is true if the cleanup should
5277 only be executed if an exception is thrown, not on normal exit. */
5279 static void
5280 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5282 gimple wce;
5283 gimple_seq cleanup_stmts = NULL;
5285 /* Errors can result in improperly nested cleanups. Which results in
5286 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5287 if (seen_error ())
5288 return;
5290 if (gimple_conditional_context ())
5292 /* If we're in a conditional context, this is more complex. We only
5293 want to run the cleanup if we actually ran the initialization that
5294 necessitates it, but we want to run it after the end of the
5295 conditional context. So we wrap the try/finally around the
5296 condition and use a flag to determine whether or not to actually
5297 run the destructor. Thus
5299 test ? f(A()) : 0
5301 becomes (approximately)
5303 flag = 0;
5304 try {
5305 if (test) { A::A(temp); flag = 1; val = f(temp); }
5306 else { val = 0; }
5307 } finally {
5308 if (flag) A::~A(temp);
5312 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5313 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5314 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5316 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5317 gimplify_stmt (&cleanup, &cleanup_stmts);
5318 wce = gimple_build_wce (cleanup_stmts);
5320 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5321 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5322 gimplify_seq_add_stmt (pre_p, ftrue);
5324 /* Because of this manipulation, and the EH edges that jump
5325 threading cannot redirect, the temporary (VAR) will appear
5326 to be used uninitialized. Don't warn. */
5327 TREE_NO_WARNING (var) = 1;
5329 else
5331 gimplify_stmt (&cleanup, &cleanup_stmts);
5332 wce = gimple_build_wce (cleanup_stmts);
5333 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5334 gimplify_seq_add_stmt (pre_p, wce);
5338 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5340 static enum gimplify_status
5341 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5343 tree targ = *expr_p;
5344 tree temp = TARGET_EXPR_SLOT (targ);
5345 tree init = TARGET_EXPR_INITIAL (targ);
5346 enum gimplify_status ret;
5348 if (init)
5350 tree cleanup = NULL_TREE;
5352 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5353 to the temps list. Handle also variable length TARGET_EXPRs. */
5354 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5356 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5357 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5358 gimplify_vla_decl (temp, pre_p);
5360 else
5361 gimple_add_tmp_var (temp);
5363 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5364 expression is supposed to initialize the slot. */
5365 if (VOID_TYPE_P (TREE_TYPE (init)))
5366 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5367 else
5369 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5370 init = init_expr;
5371 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5372 init = NULL;
5373 ggc_free (init_expr);
5375 if (ret == GS_ERROR)
5377 /* PR c++/28266 Make sure this is expanded only once. */
5378 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5379 return GS_ERROR;
5381 if (init)
5382 gimplify_and_add (init, pre_p);
5384 /* If needed, push the cleanup for the temp. */
5385 if (TARGET_EXPR_CLEANUP (targ))
5387 if (CLEANUP_EH_ONLY (targ))
5388 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5389 CLEANUP_EH_ONLY (targ), pre_p);
5390 else
5391 cleanup = TARGET_EXPR_CLEANUP (targ);
5394 /* Add a clobber for the temporary going out of scope, like
5395 gimplify_bind_expr. */
5396 if (gimplify_ctxp->in_cleanup_point_expr
5397 && needs_to_live_in_memory (temp)
5398 && flag_stack_reuse == SR_ALL)
5400 tree clobber = build_constructor (TREE_TYPE (temp),
5401 NULL);
5402 TREE_THIS_VOLATILE (clobber) = true;
5403 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5404 if (cleanup)
5405 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5406 clobber);
5407 else
5408 cleanup = clobber;
5411 if (cleanup)
5412 gimple_push_cleanup (temp, cleanup, false, pre_p);
5414 /* Only expand this once. */
5415 TREE_OPERAND (targ, 3) = init;
5416 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5418 else
5419 /* We should have expanded this before. */
5420 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5422 *expr_p = temp;
5423 return GS_OK;
5426 /* Gimplification of expression trees. */
5428 /* Gimplify an expression which appears at statement context. The
5429 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5430 NULL, a new sequence is allocated.
5432 Return true if we actually added a statement to the queue. */
5434 bool
5435 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5437 gimple_seq_node last;
5439 last = gimple_seq_last (*seq_p);
5440 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5441 return last != gimple_seq_last (*seq_p);
5444 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5445 to CTX. If entries already exist, force them to be some flavor of private.
5446 If there is no enclosing parallel, do nothing. */
5448 void
5449 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5451 splay_tree_node n;
5453 if (decl == NULL || !DECL_P (decl))
5454 return;
5458 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5459 if (n != NULL)
5461 if (n->value & GOVD_SHARED)
5462 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5463 else if (n->value & GOVD_MAP)
5464 n->value |= GOVD_MAP_TO_ONLY;
5465 else
5466 return;
5468 else if (ctx->region_type & ORT_TARGET)
5470 if (ctx->region_type & ORT_TARGET_OFFLOAD)
5471 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5473 else if (ctx->region_type != ORT_WORKSHARE
5474 && ctx->region_type != ORT_SIMD)
5475 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5477 ctx = ctx->outer_context;
5479 while (ctx);
5482 /* Similarly for each of the type sizes of TYPE. */
5484 static void
5485 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5487 if (type == NULL || type == error_mark_node)
5488 return;
5489 type = TYPE_MAIN_VARIANT (type);
5491 if (ctx->privatized_types->add (type))
5492 return;
5494 switch (TREE_CODE (type))
5496 case INTEGER_TYPE:
5497 case ENUMERAL_TYPE:
5498 case BOOLEAN_TYPE:
5499 case REAL_TYPE:
5500 case FIXED_POINT_TYPE:
5501 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5502 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5503 break;
5505 case ARRAY_TYPE:
5506 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5507 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5508 break;
5510 case RECORD_TYPE:
5511 case UNION_TYPE:
5512 case QUAL_UNION_TYPE:
5514 tree field;
5515 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5516 if (TREE_CODE (field) == FIELD_DECL)
5518 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5519 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5522 break;
5524 case POINTER_TYPE:
5525 case REFERENCE_TYPE:
5526 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5527 break;
5529 default:
5530 break;
5533 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5534 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5535 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5538 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5540 static void
5541 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5543 splay_tree_node n;
5544 unsigned int nflags;
5545 tree t;
5547 if (error_operand_p (decl))
5548 return;
5550 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5551 there are constructors involved somewhere. */
5552 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5553 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5554 flags |= GOVD_SEEN;
5556 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5557 if (n != NULL && n->value != GOVD_ALIGNED)
5559 /* We shouldn't be re-adding the decl with the same data
5560 sharing class. */
5561 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5562 /* The only combination of data sharing classes we should see is
5563 FIRSTPRIVATE and LASTPRIVATE. */
5564 nflags = n->value | flags;
5565 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5566 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5567 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5568 n->value = nflags;
5569 return;
5572 /* When adding a variable-sized variable, we have to handle all sorts
5573 of additional bits of data: the pointer replacement variable, and
5574 the parameters of the type. */
5575 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5577 /* Add the pointer replacement variable as PRIVATE if the variable
5578 replacement is private, else FIRSTPRIVATE since we'll need the
5579 address of the original variable either for SHARED, or for the
5580 copy into or out of the context. */
5581 if (!(flags & GOVD_LOCAL))
5583 if (flags & GOVD_MAP)
5585 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5586 #if 0
5587 /* Not sure if this is actually needed; haven't found a case
5588 where this would change anything; TODO. */
5589 if (flags & GOVD_MAP_FORCE)
5590 nflags |= OMP_CLAUSE_MAP_FORCE;
5591 #endif
5593 else if (flags & GOVD_PRIVATE)
5594 nflags = GOVD_PRIVATE;
5595 else
5596 nflags = GOVD_FIRSTPRIVATE;
5597 nflags |= flags & GOVD_SEEN;
5598 t = DECL_VALUE_EXPR (decl);
5599 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5600 t = TREE_OPERAND (t, 0);
5601 gcc_assert (DECL_P (t));
5602 omp_add_variable (ctx, t, nflags);
5605 /* Add all of the variable and type parameters (which should have
5606 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5607 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5608 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5609 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5611 /* The variable-sized variable itself is never SHARED, only some form
5612 of PRIVATE. The sharing would take place via the pointer variable
5613 which we remapped above. */
5614 if (flags & GOVD_SHARED)
5615 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5616 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5618 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5619 alloca statement we generate for the variable, so make sure it
5620 is available. This isn't automatically needed for the SHARED
5621 case, since we won't be allocating local storage then.
5622 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5623 in this case omp_notice_variable will be called later
5624 on when it is gimplified. */
5625 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5626 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5627 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5629 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5630 && lang_hooks.decls.omp_privatize_by_reference (decl))
5632 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5634 /* Similar to the direct variable sized case above, we'll need the
5635 size of references being privatized. */
5636 if ((flags & GOVD_SHARED) == 0)
5638 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5639 if (TREE_CODE (t) != INTEGER_CST)
5640 omp_notice_variable (ctx, t, true);
5644 if (n != NULL)
5645 n->value |= flags;
5646 else
5647 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5650 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5651 This just prints out diagnostics about threadprivate variable uses
5652 in untied tasks. If DECL2 is non-NULL, prevent this warning
5653 on that variable. */
5655 static bool
5656 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5657 tree decl2)
5659 splay_tree_node n;
5660 struct gimplify_omp_ctx *octx;
5662 for (octx = ctx; octx; octx = octx->outer_context)
5663 if ((octx->region_type & ORT_TARGET)
5664 && (octx->region_type & ORT_TARGET_OFFLOAD))
5666 gcc_assert (!(octx->region_type & ORT_TARGET_MAP_FORCE));
5668 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5669 if (n == NULL)
5671 error ("threadprivate variable %qE used in target region",
5672 DECL_NAME (decl));
5673 error_at (octx->location, "enclosing target region");
5674 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5676 if (decl2)
5677 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5680 if (ctx->region_type != ORT_UNTIED_TASK)
5681 return false;
5682 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5683 if (n == NULL)
5685 error ("threadprivate variable %qE used in untied task",
5686 DECL_NAME (decl));
5687 error_at (ctx->location, "enclosing task");
5688 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5690 if (decl2)
5691 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5692 return false;
5695 /* Record the fact that DECL was used within the OpenMP context CTX.
5696 IN_CODE is true when real code uses DECL, and false when we should
5697 merely emit default(none) errors. Return true if DECL is going to
5698 be remapped and thus DECL shouldn't be gimplified into its
5699 DECL_VALUE_EXPR (if any). */
5701 static bool
5702 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5704 splay_tree_node n;
5705 unsigned flags = in_code ? GOVD_SEEN : 0;
5706 bool ret = false, shared;
5708 if (error_operand_p (decl))
5709 return false;
5711 /* Threadprivate variables are predetermined. */
5712 if (is_global_var (decl))
5714 if (DECL_THREAD_LOCAL_P (decl))
5715 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5717 if (DECL_HAS_VALUE_EXPR_P (decl))
5719 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5721 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5722 return omp_notice_threadprivate_variable (ctx, decl, value);
5726 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5727 if ((ctx->region_type & ORT_TARGET)
5728 && (ctx->region_type & ORT_TARGET_OFFLOAD))
5730 unsigned map_force;
5731 if (ctx->region_type & ORT_TARGET_MAP_FORCE)
5732 map_force = GOVD_MAP_FORCE;
5733 else
5734 map_force = 0;
5735 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5736 if (n == NULL)
5738 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5740 error ("%qD referenced in target region does not have "
5741 "a mappable type", decl);
5742 omp_add_variable (ctx, decl, GOVD_MAP | map_force | GOVD_EXPLICIT | flags);
5744 else
5745 omp_add_variable (ctx, decl, GOVD_MAP | map_force | flags);
5747 else
5749 #if 0
5750 /* The following fails for:
5752 int l = 10;
5753 float c[l];
5754 #pragma acc parallel copy(c[2:4])
5756 #pragma acc parallel
5758 int t = sizeof c;
5762 ..., which we currently don't have to care about (nesting
5763 disabled), but eventually will have to; TODO. */
5764 if ((n->value & GOVD_MAP) && !(n->value & GOVD_EXPLICIT))
5765 gcc_assert ((n->value & GOVD_MAP_FORCE) == map_force);
5766 #endif
5768 /* If nothing changed, there's nothing left to do. */
5769 if ((n->value & flags) == flags)
5770 return ret;
5771 n->value |= flags;
5773 goto do_outer;
5776 if (n == NULL)
5778 enum omp_clause_default_kind default_kind, kind;
5779 struct gimplify_omp_ctx *octx;
5781 if (ctx->region_type == ORT_WORKSHARE
5782 || ctx->region_type == ORT_SIMD
5783 || ((ctx->region_type & ORT_TARGET)
5784 && !(ctx->region_type & ORT_TARGET_OFFLOAD)))
5785 goto do_outer;
5787 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5788 remapped firstprivate instead of shared. To some extent this is
5789 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5790 default_kind = ctx->default_kind;
5791 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5792 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5793 default_kind = kind;
5795 switch (default_kind)
5797 case OMP_CLAUSE_DEFAULT_NONE:
5798 if ((ctx->region_type & ORT_PARALLEL) != 0)
5800 error ("%qE not specified in enclosing parallel",
5801 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5802 error_at (ctx->location, "enclosing parallel");
5804 else if ((ctx->region_type & ORT_TASK) != 0)
5806 error ("%qE not specified in enclosing task",
5807 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5808 error_at (ctx->location, "enclosing task");
5810 else if (ctx->region_type == ORT_TEAMS)
5812 error ("%qE not specified in enclosing teams construct",
5813 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5814 error_at (ctx->location, "enclosing teams construct");
5816 else
5817 gcc_unreachable ();
5818 /* FALLTHRU */
5819 case OMP_CLAUSE_DEFAULT_SHARED:
5820 flags |= GOVD_SHARED;
5821 break;
5822 case OMP_CLAUSE_DEFAULT_PRIVATE:
5823 flags |= GOVD_PRIVATE;
5824 break;
5825 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5826 flags |= GOVD_FIRSTPRIVATE;
5827 break;
5828 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5829 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5830 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5831 if (ctx->outer_context)
5832 omp_notice_variable (ctx->outer_context, decl, in_code);
5833 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5835 splay_tree_node n2;
5837 if (octx->region_type & ORT_TARGET)
5838 continue;
5839 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5840 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5842 flags |= GOVD_FIRSTPRIVATE;
5843 break;
5845 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5846 break;
5848 if (flags & GOVD_FIRSTPRIVATE)
5849 break;
5850 if (octx == NULL
5851 && (TREE_CODE (decl) == PARM_DECL
5852 || (!is_global_var (decl)
5853 && DECL_CONTEXT (decl) == current_function_decl)))
5855 flags |= GOVD_FIRSTPRIVATE;
5856 break;
5858 flags |= GOVD_SHARED;
5859 break;
5860 default:
5861 gcc_unreachable ();
5864 if ((flags & GOVD_PRIVATE)
5865 && lang_hooks.decls.omp_private_outer_ref (decl))
5866 flags |= GOVD_PRIVATE_OUTER_REF;
5868 omp_add_variable (ctx, decl, flags);
5870 shared = (flags & GOVD_SHARED) != 0;
5871 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5872 goto do_outer;
5875 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5876 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5877 && DECL_SIZE (decl)
5878 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5880 splay_tree_node n2;
5881 tree t = DECL_VALUE_EXPR (decl);
5882 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5883 t = TREE_OPERAND (t, 0);
5884 gcc_assert (DECL_P (t));
5885 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5886 n2->value |= GOVD_SEEN;
5889 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5890 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5892 /* If nothing changed, there's nothing left to do. */
5893 if ((n->value & flags) == flags)
5894 return ret;
5895 flags |= n->value;
5896 n->value = flags;
5898 do_outer:
5899 /* If the variable is private in the current context, then we don't
5900 need to propagate anything to an outer context. */
5901 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5902 return ret;
5903 if (ctx->outer_context
5904 && omp_notice_variable (ctx->outer_context, decl, in_code))
5905 return true;
5906 return ret;
5909 /* Verify that DECL is private within CTX. If there's specific information
5910 to the contrary in the innermost scope, generate an error. */
5912 static bool
5913 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5915 splay_tree_node n;
5917 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5918 if (n != NULL)
5920 if (n->value & GOVD_SHARED)
5922 if (ctx == gimplify_omp_ctxp)
5924 if (simd)
5925 error ("iteration variable %qE is predetermined linear",
5926 DECL_NAME (decl));
5927 else
5928 error ("iteration variable %qE should be private",
5929 DECL_NAME (decl));
5930 n->value = GOVD_PRIVATE;
5931 return true;
5933 else
5934 return false;
5936 else if ((n->value & GOVD_EXPLICIT) != 0
5937 && (ctx == gimplify_omp_ctxp
5938 || (ctx->region_type == ORT_COMBINED_PARALLEL
5939 && gimplify_omp_ctxp->outer_context == ctx)))
5941 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5942 error ("iteration variable %qE should not be firstprivate",
5943 DECL_NAME (decl));
5944 else if ((n->value & GOVD_REDUCTION) != 0)
5945 error ("iteration variable %qE should not be reduction",
5946 DECL_NAME (decl));
5947 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5948 error ("iteration variable %qE should not be lastprivate",
5949 DECL_NAME (decl));
5950 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5951 error ("iteration variable %qE should not be private",
5952 DECL_NAME (decl));
5953 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5954 error ("iteration variable %qE is predetermined linear",
5955 DECL_NAME (decl));
5957 return (ctx == gimplify_omp_ctxp
5958 || (ctx->region_type == ORT_COMBINED_PARALLEL
5959 && gimplify_omp_ctxp->outer_context == ctx));
5962 if (ctx->region_type != ORT_WORKSHARE
5963 && ctx->region_type != ORT_SIMD)
5964 return false;
5965 else if (ctx->outer_context)
5966 return omp_is_private (ctx->outer_context, decl, simd);
5967 return false;
5970 /* Return true if DECL is private within a parallel region
5971 that binds to the current construct's context or in parallel
5972 region's REDUCTION clause. */
5974 static bool
5975 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5977 splay_tree_node n;
5981 ctx = ctx->outer_context;
5982 if (ctx == NULL)
5983 return !(is_global_var (decl)
5984 /* References might be private, but might be shared too,
5985 when checking for copyprivate, assume they might be
5986 private, otherwise assume they might be shared. */
5987 || (!copyprivate
5988 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5990 if (ctx->region_type & ORT_TARGET)
5991 continue;
5993 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5994 if (n != NULL)
5995 return (n->value & GOVD_SHARED) == 0;
5997 while (ctx->region_type == ORT_WORKSHARE
5998 || ctx->region_type == ORT_SIMD);
5999 return false;
6002 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6003 and previous omp contexts. */
6005 static void
6006 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6007 enum omp_region_type region_type)
6009 struct gimplify_omp_ctx *ctx, *outer_ctx;
6010 tree c;
6012 ctx = new_omp_context (region_type);
6013 outer_ctx = ctx->outer_context;
6015 while ((c = *list_p) != NULL)
6017 bool remove = false;
6018 bool notice_outer = true;
6019 const char *check_non_private = NULL;
6020 unsigned int flags;
6021 tree decl;
6023 switch (OMP_CLAUSE_CODE (c))
6025 case OMP_CLAUSE_PRIVATE:
6026 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6027 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6029 flags |= GOVD_PRIVATE_OUTER_REF;
6030 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6032 else
6033 notice_outer = false;
6034 goto do_add;
6035 case OMP_CLAUSE_SHARED:
6036 flags = GOVD_SHARED | GOVD_EXPLICIT;
6037 goto do_add;
6038 case OMP_CLAUSE_FIRSTPRIVATE:
6039 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6040 check_non_private = "firstprivate";
6041 goto do_add;
6042 case OMP_CLAUSE_LASTPRIVATE:
6043 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6044 check_non_private = "lastprivate";
6045 goto do_add;
6046 case OMP_CLAUSE_REDUCTION:
6047 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6048 check_non_private = "reduction";
6049 goto do_add;
6050 case OMP_CLAUSE_LINEAR:
6051 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6052 is_gimple_val, fb_rvalue) == GS_ERROR)
6054 remove = true;
6055 break;
6057 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6058 goto do_add;
6060 case OMP_CLAUSE_MAP:
6061 switch (OMP_CLAUSE_MAP_KIND (c))
6063 case OMP_CLAUSE_MAP_FORCE_DEALLOC:
6064 input_location = OMP_CLAUSE_LOCATION (c);
6065 /* TODO. */
6066 sorry ("data clause not yet implemented");
6067 remove = true;
6068 break;
6069 default:
6070 break;
6072 decl = OMP_CLAUSE_DECL (c);
6073 if (error_operand_p (decl))
6075 remove = true;
6076 break;
6078 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6079 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6080 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6081 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6082 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6084 remove = true;
6085 break;
6087 if (!DECL_P (decl))
6089 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6090 NULL, is_gimple_lvalue, fb_lvalue)
6091 == GS_ERROR)
6093 remove = true;
6094 break;
6096 break;
6098 flags = GOVD_MAP | GOVD_EXPLICIT;
6099 goto do_add;
6101 case OMP_CLAUSE_DEPEND:
6102 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6104 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6105 NULL, is_gimple_val, fb_rvalue);
6106 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6108 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6110 remove = true;
6111 break;
6113 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6114 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6115 is_gimple_val, fb_rvalue) == GS_ERROR)
6117 remove = true;
6118 break;
6120 break;
6122 case OMP_CLAUSE_TO:
6123 case OMP_CLAUSE_FROM:
6124 decl = OMP_CLAUSE_DECL (c);
6125 if (error_operand_p (decl))
6127 remove = true;
6128 break;
6130 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6131 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6132 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6133 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6134 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6136 remove = true;
6137 break;
6139 if (!DECL_P (decl))
6141 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6142 NULL, is_gimple_lvalue, fb_lvalue)
6143 == GS_ERROR)
6145 remove = true;
6146 break;
6148 break;
6150 goto do_notice;
6152 do_add:
6153 decl = OMP_CLAUSE_DECL (c);
6154 if (error_operand_p (decl))
6156 remove = true;
6157 break;
6159 omp_add_variable (ctx, decl, flags);
6160 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6161 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6163 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6164 GOVD_LOCAL | GOVD_SEEN);
6165 gimplify_omp_ctxp = ctx;
6166 push_gimplify_context ();
6168 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6169 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6171 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6172 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6173 pop_gimplify_context
6174 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6175 push_gimplify_context ();
6176 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6177 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6178 pop_gimplify_context
6179 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6180 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6181 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6183 gimplify_omp_ctxp = outer_ctx;
6185 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6186 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6188 gimplify_omp_ctxp = ctx;
6189 push_gimplify_context ();
6190 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6192 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6193 NULL, NULL);
6194 TREE_SIDE_EFFECTS (bind) = 1;
6195 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6196 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6198 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6199 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6200 pop_gimplify_context
6201 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6202 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6204 gimplify_omp_ctxp = outer_ctx;
6206 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6207 && OMP_CLAUSE_LINEAR_STMT (c))
6209 gimplify_omp_ctxp = ctx;
6210 push_gimplify_context ();
6211 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6213 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6214 NULL, NULL);
6215 TREE_SIDE_EFFECTS (bind) = 1;
6216 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6217 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6219 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6220 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6221 pop_gimplify_context
6222 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6223 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6225 gimplify_omp_ctxp = outer_ctx;
6227 if (notice_outer)
6228 goto do_notice;
6229 break;
6231 case OMP_CLAUSE_COPYIN:
6232 case OMP_CLAUSE_COPYPRIVATE:
6233 decl = OMP_CLAUSE_DECL (c);
6234 if (error_operand_p (decl))
6236 remove = true;
6237 break;
6239 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6240 && !remove
6241 && !omp_check_private (ctx, decl, true))
6243 remove = true;
6244 if (is_global_var (decl))
6246 if (DECL_THREAD_LOCAL_P (decl))
6247 remove = false;
6248 else if (DECL_HAS_VALUE_EXPR_P (decl))
6250 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6252 if (value
6253 && DECL_P (value)
6254 && DECL_THREAD_LOCAL_P (value))
6255 remove = false;
6258 if (remove)
6259 error_at (OMP_CLAUSE_LOCATION (c),
6260 "copyprivate variable %qE is not threadprivate"
6261 " or private in outer context", DECL_NAME (decl));
6263 do_notice:
6264 if (outer_ctx)
6265 omp_notice_variable (outer_ctx, decl, true);
6266 if (check_non_private
6267 && region_type == ORT_WORKSHARE
6268 && omp_check_private (ctx, decl, false))
6270 error ("%s variable %qE is private in outer context",
6271 check_non_private, DECL_NAME (decl));
6272 remove = true;
6274 break;
6276 case OMP_CLAUSE_FINAL:
6277 case OMP_CLAUSE_IF:
6278 OMP_CLAUSE_OPERAND (c, 0)
6279 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6280 /* Fall through. */
6282 case OMP_CLAUSE_SCHEDULE:
6283 case OMP_CLAUSE_NUM_THREADS:
6284 case OMP_CLAUSE_NUM_TEAMS:
6285 case OMP_CLAUSE_THREAD_LIMIT:
6286 case OMP_CLAUSE_DIST_SCHEDULE:
6287 case OMP_CLAUSE_DEVICE:
6288 case OMP_CLAUSE__CILK_FOR_COUNT_:
6289 case OMP_CLAUSE_NUM_GANGS:
6290 case OMP_CLAUSE_NUM_WORKERS:
6291 case OMP_CLAUSE_VECTOR_LENGTH:
6292 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6293 is_gimple_val, fb_rvalue) == GS_ERROR)
6294 remove = true;
6295 break;
6297 case OMP_CLAUSE_HOST:
6298 case OMP_CLAUSE_OACC_DEVICE:
6299 case OMP_CLAUSE_DEVICE_RESIDENT:
6300 case OMP_CLAUSE_USE_DEVICE:
6301 case OMP_CLAUSE_GANG:
6302 case OMP_CLAUSE_ASYNC:
6303 case OMP_CLAUSE_WAIT:
6304 case OMP_NO_CLAUSE_CACHE:
6305 case OMP_CLAUSE_INDEPENDENT:
6306 case OMP_CLAUSE_WORKER:
6307 case OMP_CLAUSE_VECTOR:
6308 remove = true;
6309 break;
6311 case OMP_CLAUSE_NOWAIT:
6312 case OMP_CLAUSE_ORDERED:
6313 case OMP_CLAUSE_UNTIED:
6314 case OMP_CLAUSE_COLLAPSE:
6315 case OMP_CLAUSE_MERGEABLE:
6316 case OMP_CLAUSE_PROC_BIND:
6317 case OMP_CLAUSE_SAFELEN:
6318 break;
6320 case OMP_CLAUSE_ALIGNED:
6321 decl = OMP_CLAUSE_DECL (c);
6322 if (error_operand_p (decl))
6324 remove = true;
6325 break;
6327 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6328 is_gimple_val, fb_rvalue) == GS_ERROR)
6330 remove = true;
6331 break;
6333 if (!is_global_var (decl)
6334 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6335 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6336 break;
6338 case OMP_CLAUSE_DEFAULT:
6339 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6340 break;
6342 default:
6343 gcc_unreachable ();
6346 if (remove)
6347 *list_p = OMP_CLAUSE_CHAIN (c);
6348 else
6349 list_p = &OMP_CLAUSE_CHAIN (c);
6352 gimplify_omp_ctxp = ctx;
6355 struct gimplify_adjust_omp_clauses_data
6357 tree *list_p;
6358 gimple_seq *pre_p;
6361 /* For all variables that were not actually used within the context,
6362 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6364 static int
6365 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6367 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6368 gimple_seq *pre_p
6369 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6370 tree decl = (tree) n->key;
6371 unsigned flags = n->value;
6372 enum omp_clause_code code;
6373 tree clause;
6374 bool private_debug;
6376 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6377 return 0;
6378 if ((flags & GOVD_SEEN) == 0)
6379 return 0;
6380 if (flags & GOVD_DEBUG_PRIVATE)
6382 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6383 private_debug = true;
6385 else if (flags & GOVD_MAP)
6386 private_debug = false;
6387 else
6388 private_debug
6389 = lang_hooks.decls.omp_private_debug_clause (decl,
6390 !!(flags & GOVD_SHARED));
6391 if (private_debug)
6392 code = OMP_CLAUSE_PRIVATE;
6393 else if (flags & GOVD_MAP)
6394 code = OMP_CLAUSE_MAP;
6395 else if (flags & GOVD_SHARED)
6397 if (is_global_var (decl))
6399 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6400 while (ctx != NULL)
6402 splay_tree_node on
6403 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6404 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6405 | GOVD_PRIVATE | GOVD_REDUCTION
6406 | GOVD_LINEAR | GOVD_MAP)) != 0)
6407 break;
6408 ctx = ctx->outer_context;
6410 if (ctx == NULL)
6411 return 0;
6413 code = OMP_CLAUSE_SHARED;
6415 else if (flags & GOVD_PRIVATE)
6416 code = OMP_CLAUSE_PRIVATE;
6417 else if (flags & GOVD_FIRSTPRIVATE)
6418 code = OMP_CLAUSE_FIRSTPRIVATE;
6419 else if (flags & GOVD_LASTPRIVATE)
6420 code = OMP_CLAUSE_LASTPRIVATE;
6421 else if (flags & GOVD_ALIGNED)
6422 return 0;
6423 else
6424 gcc_unreachable ();
6426 clause = build_omp_clause (input_location, code);
6427 OMP_CLAUSE_DECL (clause) = decl;
6428 OMP_CLAUSE_CHAIN (clause) = *list_p;
6429 if (private_debug)
6430 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6431 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6432 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6433 else if (code == OMP_CLAUSE_MAP)
6435 unsigned map_kind;
6436 map_kind = (flags & GOVD_MAP_TO_ONLY
6437 ? OMP_CLAUSE_MAP_TO
6438 : OMP_CLAUSE_MAP_TOFROM);
6439 if (flags & GOVD_MAP_FORCE)
6440 map_kind |= OMP_CLAUSE_MAP_FORCE;
6441 OMP_CLAUSE_MAP_KIND (clause) = (enum omp_clause_map_kind) map_kind;
6443 if (DECL_SIZE (decl)
6444 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6446 tree decl2 = DECL_VALUE_EXPR (decl);
6447 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6448 decl2 = TREE_OPERAND (decl2, 0);
6449 gcc_assert (DECL_P (decl2));
6450 tree mem = build_simple_mem_ref (decl2);
6451 OMP_CLAUSE_DECL (clause) = mem;
6452 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6453 if (gimplify_omp_ctxp->outer_context)
6455 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6456 omp_notice_variable (ctx, decl2, true);
6457 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6459 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6460 OMP_CLAUSE_MAP);
6461 OMP_CLAUSE_DECL (nc) = decl;
6462 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6463 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6464 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6465 OMP_CLAUSE_CHAIN (clause) = nc;
6467 else
6468 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6470 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6472 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6473 OMP_CLAUSE_DECL (nc) = decl;
6474 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6475 OMP_CLAUSE_CHAIN (nc) = *list_p;
6476 OMP_CLAUSE_CHAIN (clause) = nc;
6477 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6478 gimplify_omp_ctxp = ctx->outer_context;
6479 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6480 gimplify_omp_ctxp = ctx;
6482 *list_p = clause;
6483 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6484 gimplify_omp_ctxp = ctx->outer_context;
6485 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6486 gimplify_omp_ctxp = ctx;
6487 return 0;
6490 static void
6491 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6493 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6494 tree c, decl;
6496 while ((c = *list_p) != NULL)
6498 splay_tree_node n;
6499 bool remove = false;
6501 switch (OMP_CLAUSE_CODE (c))
6503 case OMP_CLAUSE_PRIVATE:
6504 case OMP_CLAUSE_SHARED:
6505 case OMP_CLAUSE_FIRSTPRIVATE:
6506 case OMP_CLAUSE_LINEAR:
6507 decl = OMP_CLAUSE_DECL (c);
6508 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6509 remove = !(n->value & GOVD_SEEN);
6510 if (! remove)
6512 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6513 if ((n->value & GOVD_DEBUG_PRIVATE)
6514 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6516 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6517 || ((n->value & GOVD_DATA_SHARE_CLASS)
6518 == GOVD_PRIVATE));
6519 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6520 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6522 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6523 && ctx->outer_context
6524 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6525 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6527 if (ctx->outer_context->combined_loop
6528 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6530 n = splay_tree_lookup (ctx->outer_context->variables,
6531 (splay_tree_key) decl);
6532 if (n == NULL
6533 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6535 int flags = GOVD_FIRSTPRIVATE;
6536 /* #pragma omp distribute does not allow
6537 lastprivate clause. */
6538 if (!ctx->outer_context->distribute)
6539 flags |= GOVD_LASTPRIVATE;
6540 if (n == NULL)
6541 omp_add_variable (ctx->outer_context, decl,
6542 flags | GOVD_SEEN);
6543 else
6544 n->value |= flags | GOVD_SEEN;
6547 else if (!is_global_var (decl))
6548 omp_notice_variable (ctx->outer_context, decl, true);
6551 break;
6553 case OMP_CLAUSE_LASTPRIVATE:
6554 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6555 accurately reflect the presence of a FIRSTPRIVATE clause. */
6556 decl = OMP_CLAUSE_DECL (c);
6557 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6558 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6559 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6560 break;
6562 case OMP_CLAUSE_ALIGNED:
6563 decl = OMP_CLAUSE_DECL (c);
6564 if (!is_global_var (decl))
6566 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6567 remove = n == NULL || !(n->value & GOVD_SEEN);
6568 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6570 struct gimplify_omp_ctx *octx;
6571 if (n != NULL
6572 && (n->value & (GOVD_DATA_SHARE_CLASS
6573 & ~GOVD_FIRSTPRIVATE)))
6574 remove = true;
6575 else
6576 for (octx = ctx->outer_context; octx;
6577 octx = octx->outer_context)
6579 n = splay_tree_lookup (octx->variables,
6580 (splay_tree_key) decl);
6581 if (n == NULL)
6582 continue;
6583 if (n->value & GOVD_LOCAL)
6584 break;
6585 /* We have to avoid assigning a shared variable
6586 to itself when trying to add
6587 __builtin_assume_aligned. */
6588 if (n->value & GOVD_SHARED)
6590 remove = true;
6591 break;
6596 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6598 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6599 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6600 remove = true;
6602 break;
6604 case OMP_CLAUSE_MAP:
6605 decl = OMP_CLAUSE_DECL (c);
6606 if (!DECL_P (decl))
6607 break;
6608 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6609 if ((ctx->region_type & ORT_TARGET)
6610 && (ctx->region_type & ORT_TARGET_OFFLOAD)
6611 && !(n->value & GOVD_SEEN))
6612 remove = true;
6613 else if (DECL_SIZE (decl)
6614 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6615 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6617 /* For OMP_CLAUSE_MAP_FORCE_DEVICEPTR, we'll never enter here,
6618 because for these, TREE_CODE (DECL_SIZE (decl)) will always be
6619 INTEGER_CST. */
6620 gcc_assert (OMP_CLAUSE_MAP_KIND (c)
6621 != OMP_CLAUSE_MAP_FORCE_DEVICEPTR);
6623 tree decl2 = DECL_VALUE_EXPR (decl);
6624 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6625 decl2 = TREE_OPERAND (decl2, 0);
6626 gcc_assert (DECL_P (decl2));
6627 tree mem = build_simple_mem_ref (decl2);
6628 OMP_CLAUSE_DECL (c) = mem;
6629 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6630 if (ctx->outer_context)
6632 omp_notice_variable (ctx->outer_context, decl2, true);
6633 omp_notice_variable (ctx->outer_context,
6634 OMP_CLAUSE_SIZE (c), true);
6636 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6637 OMP_CLAUSE_MAP);
6638 OMP_CLAUSE_DECL (nc) = decl;
6639 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6640 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6641 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6642 OMP_CLAUSE_CHAIN (c) = nc;
6643 c = nc;
6645 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6646 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6647 break;
6649 case OMP_CLAUSE_TO:
6650 case OMP_CLAUSE_FROM:
6651 decl = OMP_CLAUSE_DECL (c);
6652 if (!DECL_P (decl))
6653 break;
6654 if (DECL_SIZE (decl)
6655 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6657 tree decl2 = DECL_VALUE_EXPR (decl);
6658 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6659 decl2 = TREE_OPERAND (decl2, 0);
6660 gcc_assert (DECL_P (decl2));
6661 tree mem = build_simple_mem_ref (decl2);
6662 OMP_CLAUSE_DECL (c) = mem;
6663 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6664 if (ctx->outer_context)
6666 omp_notice_variable (ctx->outer_context, decl2, true);
6667 omp_notice_variable (ctx->outer_context,
6668 OMP_CLAUSE_SIZE (c), true);
6671 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6672 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6673 break;
6675 case OMP_CLAUSE_REDUCTION:
6676 case OMP_CLAUSE_COPYIN:
6677 case OMP_CLAUSE_COPYPRIVATE:
6678 case OMP_CLAUSE_IF:
6679 case OMP_CLAUSE_NUM_THREADS:
6680 case OMP_CLAUSE_NUM_TEAMS:
6681 case OMP_CLAUSE_THREAD_LIMIT:
6682 case OMP_CLAUSE_DIST_SCHEDULE:
6683 case OMP_CLAUSE_DEVICE:
6684 case OMP_CLAUSE_SCHEDULE:
6685 case OMP_CLAUSE_NOWAIT:
6686 case OMP_CLAUSE_ORDERED:
6687 case OMP_CLAUSE_DEFAULT:
6688 case OMP_CLAUSE_UNTIED:
6689 case OMP_CLAUSE_COLLAPSE:
6690 case OMP_CLAUSE_FINAL:
6691 case OMP_CLAUSE_MERGEABLE:
6692 case OMP_CLAUSE_PROC_BIND:
6693 case OMP_CLAUSE_SAFELEN:
6694 case OMP_CLAUSE_DEPEND:
6695 case OMP_CLAUSE__CILK_FOR_COUNT_:
6696 case OMP_CLAUSE_NUM_GANGS:
6697 case OMP_CLAUSE_NUM_WORKERS:
6698 case OMP_CLAUSE_VECTOR_LENGTH:
6699 break;
6701 case OMP_CLAUSE_HOST:
6702 case OMP_CLAUSE_OACC_DEVICE:
6703 case OMP_CLAUSE_DEVICE_RESIDENT:
6704 case OMP_CLAUSE_USE_DEVICE:
6705 case OMP_CLAUSE_GANG:
6706 case OMP_CLAUSE_ASYNC:
6707 case OMP_CLAUSE_WAIT:
6708 case OMP_NO_CLAUSE_CACHE:
6709 case OMP_CLAUSE_INDEPENDENT:
6710 case OMP_CLAUSE_WORKER:
6711 case OMP_CLAUSE_VECTOR:
6712 default:
6713 gcc_unreachable ();
6716 if (remove)
6717 *list_p = OMP_CLAUSE_CHAIN (c);
6718 else
6719 list_p = &OMP_CLAUSE_CHAIN (c);
6722 /* Add in any implicit data sharing. */
6723 struct gimplify_adjust_omp_clauses_data data;
6724 data.list_p = list_p;
6725 data.pre_p = pre_p;
6726 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6728 gimplify_omp_ctxp = ctx->outer_context;
6729 delete_omp_context (ctx);
6732 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6733 gimplification of the body, as well as scanning the body for used
6734 variables. We need to do this scan now, because variable-sized
6735 decls will be decomposed during gimplification. */
6737 static void
6738 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6740 tree expr = *expr_p;
6741 gimple g;
6742 gimple_seq body = NULL;
6744 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6745 OMP_PARALLEL_COMBINED (expr)
6746 ? ORT_COMBINED_PARALLEL
6747 : ORT_PARALLEL);
6749 push_gimplify_context ();
6751 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6752 if (gimple_code (g) == GIMPLE_BIND)
6753 pop_gimplify_context (g);
6754 else
6755 pop_gimplify_context (NULL);
6757 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6759 g = gimple_build_omp_parallel (body,
6760 OMP_PARALLEL_CLAUSES (expr),
6761 NULL_TREE, NULL_TREE);
6762 if (OMP_PARALLEL_COMBINED (expr))
6763 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6764 gimplify_seq_add_stmt (pre_p, g);
6765 *expr_p = NULL_TREE;
6768 /* Gimplify the contents of an OMP_TASK statement. This involves
6769 gimplification of the body, as well as scanning the body for used
6770 variables. We need to do this scan now, because variable-sized
6771 decls will be decomposed during gimplification. */
6773 static void
6774 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6776 tree expr = *expr_p;
6777 gimple g;
6778 gimple_seq body = NULL;
6780 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6781 find_omp_clause (OMP_TASK_CLAUSES (expr),
6782 OMP_CLAUSE_UNTIED)
6783 ? ORT_UNTIED_TASK : ORT_TASK);
6785 push_gimplify_context ();
6787 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6788 if (gimple_code (g) == GIMPLE_BIND)
6789 pop_gimplify_context (g);
6790 else
6791 pop_gimplify_context (NULL);
6793 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6795 g = gimple_build_omp_task (body,
6796 OMP_TASK_CLAUSES (expr),
6797 NULL_TREE, NULL_TREE,
6798 NULL_TREE, NULL_TREE, NULL_TREE);
6799 gimplify_seq_add_stmt (pre_p, g);
6800 *expr_p = NULL_TREE;
6803 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6804 with non-NULL OMP_FOR_INIT. */
6806 static tree
6807 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6809 *walk_subtrees = 0;
6810 switch (TREE_CODE (*tp))
6812 case OMP_FOR:
6813 *walk_subtrees = 1;
6814 /* FALLTHRU */
6815 case OMP_SIMD:
6816 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6817 return *tp;
6818 break;
6819 case BIND_EXPR:
6820 case STATEMENT_LIST:
6821 case OMP_PARALLEL:
6822 *walk_subtrees = 1;
6823 break;
6824 default:
6825 break;
6827 return NULL_TREE;
6830 /* Gimplify the gross structure of an OMP_FOR statement. */
6832 static enum gimplify_status
6833 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6835 tree for_stmt, orig_for_stmt, decl, var, t;
6836 enum gimplify_status ret = GS_ALL_DONE;
6837 enum gimplify_status tret;
6838 gimple gfor;
6839 gimple_seq for_body, for_pre_body;
6840 int i;
6841 bool simd;
6842 enum gimplify_omp_var_data govd_private;
6843 enum omp_region_type ort;
6844 bitmap has_decl_expr = NULL;
6846 orig_for_stmt = for_stmt = *expr_p;
6848 switch (TREE_CODE (for_stmt))
6850 case OMP_FOR:
6851 case CILK_FOR:
6852 case OMP_DISTRIBUTE:
6853 simd = false;
6854 govd_private = GOVD_PRIVATE;
6855 ort = ORT_WORKSHARE;
6856 break;
6857 case OMP_SIMD:
6858 case CILK_SIMD:
6859 simd = true;
6860 govd_private = GOVD_PRIVATE;
6861 ort = ORT_SIMD;
6862 break;
6863 case OACC_LOOP:
6864 simd = false;
6865 govd_private = /* TODO */ GOVD_LOCAL;
6866 ort = /* TODO */ ORT_WORKSHARE;
6867 break;
6868 default:
6869 gcc_unreachable ();
6872 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort);
6873 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6874 gimplify_omp_ctxp->distribute = true;
6876 /* Handle OMP_FOR_INIT. */
6877 for_pre_body = NULL;
6878 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6880 has_decl_expr = BITMAP_ALLOC (NULL);
6881 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6882 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6883 == VAR_DECL)
6885 t = OMP_FOR_PRE_BODY (for_stmt);
6886 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6888 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6890 tree_stmt_iterator si;
6891 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6892 tsi_next (&si))
6894 t = tsi_stmt (si);
6895 if (TREE_CODE (t) == DECL_EXPR
6896 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6897 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6901 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6902 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6904 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6906 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
6907 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6908 NULL, NULL);
6909 gcc_assert (for_stmt != NULL_TREE);
6910 gimplify_omp_ctxp->combined_loop = true;
6913 for_body = NULL;
6914 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6915 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6916 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6917 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6918 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6920 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6921 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6922 decl = TREE_OPERAND (t, 0);
6923 gcc_assert (DECL_P (decl));
6924 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6925 || POINTER_TYPE_P (TREE_TYPE (decl)));
6927 /* Make sure the iteration variable is some kind of private. */
6928 tree c = NULL_TREE;
6929 tree c2 = NULL_TREE;
6930 if (orig_for_stmt != for_stmt)
6931 /* Do this only on innermost construct for combined ones. */;
6932 else if (simd)
6934 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6935 (splay_tree_key)decl);
6936 omp_is_private (gimplify_omp_ctxp, decl,
6937 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6938 != 1));
6939 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6940 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6941 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6943 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6944 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6945 if (has_decl_expr
6946 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6947 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6948 OMP_CLAUSE_DECL (c) = decl;
6949 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6950 OMP_FOR_CLAUSES (for_stmt) = c;
6951 omp_add_variable (gimplify_omp_ctxp, decl,
6952 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6954 else
6956 gcc_assert (govd_private == GOVD_PRIVATE);
6957 bool lastprivate
6958 = (!has_decl_expr
6959 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6960 if (lastprivate
6961 && gimplify_omp_ctxp->outer_context
6962 && gimplify_omp_ctxp->outer_context->region_type
6963 == ORT_WORKSHARE
6964 && gimplify_omp_ctxp->outer_context->combined_loop
6965 && !gimplify_omp_ctxp->outer_context->distribute)
6967 struct gimplify_omp_ctx *outer
6968 = gimplify_omp_ctxp->outer_context;
6969 n = splay_tree_lookup (outer->variables,
6970 (splay_tree_key) decl);
6971 if (n != NULL
6972 && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6973 lastprivate = false;
6974 else if (omp_check_private (outer, decl, false))
6975 error ("lastprivate variable %qE is private in outer "
6976 "context", DECL_NAME (decl));
6977 else
6979 omp_add_variable (outer, decl,
6980 GOVD_LASTPRIVATE | GOVD_SEEN);
6981 if (outer->outer_context)
6982 omp_notice_variable (outer->outer_context, decl, true);
6985 c = build_omp_clause (input_location,
6986 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6987 : OMP_CLAUSE_PRIVATE);
6988 OMP_CLAUSE_DECL (c) = decl;
6989 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6990 OMP_FOR_CLAUSES (for_stmt) = c;
6991 omp_add_variable (gimplify_omp_ctxp, decl,
6992 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6993 | GOVD_EXPLICIT | GOVD_SEEN);
6994 c = NULL_TREE;
6997 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6998 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6999 else
7000 omp_add_variable (gimplify_omp_ctxp, decl, govd_private | GOVD_SEEN);
7002 /* If DECL is not a gimple register, create a temporary variable to act
7003 as an iteration counter. This is valid, since DECL cannot be
7004 modified in the body of the loop. Similarly for any iteration vars
7005 in simd with collapse > 1 where the iterator vars must be
7006 lastprivate. */
7007 if (orig_for_stmt != for_stmt)
7008 var = decl;
7009 else if (!is_gimple_reg (decl)
7010 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
7012 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7013 TREE_OPERAND (t, 0) = var;
7015 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
7017 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7019 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7020 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
7021 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
7022 OMP_CLAUSE_DECL (c2) = var;
7023 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
7024 OMP_FOR_CLAUSES (for_stmt) = c2;
7025 omp_add_variable (gimplify_omp_ctxp, var,
7026 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
7027 if (c == NULL_TREE)
7029 c = c2;
7030 c2 = NULL_TREE;
7033 else
7034 omp_add_variable (gimplify_omp_ctxp, var,
7035 govd_private | GOVD_SEEN);
7037 else
7038 var = decl;
7040 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7041 is_gimple_val, fb_rvalue);
7042 ret = MIN (ret, tret);
7043 if (ret == GS_ERROR)
7044 return ret;
7046 /* Handle OMP_FOR_COND. */
7047 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7048 gcc_assert (COMPARISON_CLASS_P (t));
7049 gcc_assert (TREE_OPERAND (t, 0) == decl);
7051 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7052 is_gimple_val, fb_rvalue);
7053 ret = MIN (ret, tret);
7055 /* Handle OMP_FOR_INCR. */
7056 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7057 switch (TREE_CODE (t))
7059 case PREINCREMENT_EXPR:
7060 case POSTINCREMENT_EXPR:
7062 tree decl = TREE_OPERAND (t, 0);
7063 /* c_omp_for_incr_canonicalize_ptr() should have been
7064 called to massage things appropriately. */
7065 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7067 if (orig_for_stmt != for_stmt)
7068 break;
7069 t = build_int_cst (TREE_TYPE (decl), 1);
7070 if (c)
7071 OMP_CLAUSE_LINEAR_STEP (c) = t;
7072 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7073 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7074 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7075 break;
7078 case PREDECREMENT_EXPR:
7079 case POSTDECREMENT_EXPR:
7080 /* c_omp_for_incr_canonicalize_ptr() should have been
7081 called to massage things appropriately. */
7082 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7083 if (orig_for_stmt != for_stmt)
7084 break;
7085 t = build_int_cst (TREE_TYPE (decl), -1);
7086 if (c)
7087 OMP_CLAUSE_LINEAR_STEP (c) = t;
7088 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7089 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7090 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7091 break;
7093 case MODIFY_EXPR:
7094 gcc_assert (TREE_OPERAND (t, 0) == decl);
7095 TREE_OPERAND (t, 0) = var;
7097 t = TREE_OPERAND (t, 1);
7098 switch (TREE_CODE (t))
7100 case PLUS_EXPR:
7101 if (TREE_OPERAND (t, 1) == decl)
7103 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
7104 TREE_OPERAND (t, 0) = var;
7105 break;
7108 /* Fallthru. */
7109 case MINUS_EXPR:
7110 case POINTER_PLUS_EXPR:
7111 gcc_assert (TREE_OPERAND (t, 0) == decl);
7112 TREE_OPERAND (t, 0) = var;
7113 break;
7114 default:
7115 gcc_unreachable ();
7118 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7119 is_gimple_val, fb_rvalue);
7120 ret = MIN (ret, tret);
7121 if (c)
7123 tree step = TREE_OPERAND (t, 1);
7124 tree stept = TREE_TYPE (decl);
7125 if (POINTER_TYPE_P (stept))
7126 stept = sizetype;
7127 step = fold_convert (stept, step);
7128 if (TREE_CODE (t) == MINUS_EXPR)
7129 step = fold_build1 (NEGATE_EXPR, stept, step);
7130 OMP_CLAUSE_LINEAR_STEP (c) = step;
7131 if (step != TREE_OPERAND (t, 1))
7133 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7134 &for_pre_body, NULL,
7135 is_gimple_val, fb_rvalue);
7136 ret = MIN (ret, tret);
7139 break;
7141 default:
7142 gcc_unreachable ();
7145 if (c2)
7147 gcc_assert (c);
7148 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7151 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7152 && orig_for_stmt == for_stmt)
7154 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7155 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7156 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7157 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7158 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7159 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7160 && OMP_CLAUSE_DECL (c) == decl)
7162 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7163 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7164 gcc_assert (TREE_OPERAND (t, 0) == var);
7165 t = TREE_OPERAND (t, 1);
7166 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7167 || TREE_CODE (t) == MINUS_EXPR
7168 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7169 gcc_assert (TREE_OPERAND (t, 0) == var);
7170 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7171 TREE_OPERAND (t, 1));
7172 gimple_seq *seq;
7173 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7174 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7175 else
7176 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7177 gimplify_assign (decl, t, seq);
7182 BITMAP_FREE (has_decl_expr);
7184 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7186 if (orig_for_stmt != for_stmt)
7187 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7189 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7190 decl = TREE_OPERAND (t, 0);
7191 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7192 omp_add_variable (gimplify_omp_ctxp, var, govd_private | GOVD_SEEN);
7193 TREE_OPERAND (t, 0) = var;
7194 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7195 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7196 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7199 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7201 int kind;
7202 switch (TREE_CODE (orig_for_stmt))
7204 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7205 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7206 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7207 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7208 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7209 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
7210 default:
7211 gcc_unreachable ();
7213 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7214 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7215 for_pre_body);
7216 if (orig_for_stmt != for_stmt)
7217 gimple_omp_for_set_combined_p (gfor, true);
7218 if (gimplify_omp_ctxp
7219 && (gimplify_omp_ctxp->combined_loop
7220 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7221 && gimplify_omp_ctxp->outer_context
7222 && gimplify_omp_ctxp->outer_context->combined_loop)))
7224 gimple_omp_for_set_combined_into_p (gfor, true);
7225 if (gimplify_omp_ctxp->combined_loop)
7226 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7227 else
7228 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7231 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7233 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7234 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7235 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7236 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7237 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7238 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7239 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7240 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7243 gimplify_seq_add_stmt (pre_p, gfor);
7244 if (ret != GS_ALL_DONE)
7245 return GS_ERROR;
7246 *expr_p = NULL_TREE;
7247 return GS_ALL_DONE;
7250 /* Gimplify the gross structure of several OpenACC or OpenMP constructs. */
7252 static void
7253 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7255 tree expr = *expr_p;
7256 gimple stmt;
7257 gimple_seq body = NULL;
7258 enum omp_region_type ort;
7260 switch (TREE_CODE (expr))
7262 case OACC_DATA:
7263 ort = (enum omp_region_type) (ORT_TARGET
7264 | ORT_TARGET_MAP_FORCE);
7265 break;
7266 case OACC_KERNELS:
7267 case OACC_PARALLEL:
7268 ort = (enum omp_region_type) (ORT_TARGET
7269 | ORT_TARGET_OFFLOAD
7270 | ORT_TARGET_MAP_FORCE);
7271 break;
7272 case OMP_SECTIONS:
7273 case OMP_SINGLE:
7274 ort = ORT_WORKSHARE;
7275 break;
7276 case OMP_TARGET:
7277 ort = (enum omp_region_type) (ORT_TARGET | ORT_TARGET_OFFLOAD);
7278 break;
7279 case OMP_TARGET_DATA:
7280 ort = ORT_TARGET;
7281 break;
7282 case OMP_TEAMS:
7283 ort = ORT_TEAMS;
7284 break;
7285 default:
7286 gcc_unreachable ();
7288 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7289 if (ort & ORT_TARGET)
7291 push_gimplify_context ();
7292 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7293 if (gimple_code (g) == GIMPLE_BIND)
7294 pop_gimplify_context (g);
7295 else
7296 pop_gimplify_context (NULL);
7297 if (!(ort & ORT_TARGET_OFFLOAD))
7299 enum built_in_function end_ix;
7300 switch (TREE_CODE (expr))
7302 case OACC_DATA:
7303 end_ix = BUILT_IN_GOACC_DATA_END;
7304 break;
7305 case OMP_TARGET_DATA:
7306 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
7307 break;
7308 default:
7309 gcc_unreachable ();
7311 tree fn = builtin_decl_explicit (end_ix);
7312 g = gimple_build_call (fn, 0);
7313 gimple_seq cleanup = NULL;
7314 gimple_seq_add_stmt (&cleanup, g);
7315 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7316 body = NULL;
7317 gimple_seq_add_stmt (&body, g);
7320 else
7321 gimplify_and_add (OMP_BODY (expr), &body);
7322 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7324 switch (TREE_CODE (expr))
7326 case OACC_DATA:
7327 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
7328 OACC_DATA_CLAUSES (expr));
7329 break;
7330 case OACC_KERNELS:
7331 stmt = gimple_build_oacc_kernels (body, OACC_KERNELS_CLAUSES (expr));
7332 break;
7333 case OACC_PARALLEL:
7334 stmt = gimple_build_oacc_parallel (body, OACC_PARALLEL_CLAUSES (expr));
7335 break;
7336 case OMP_SECTIONS:
7337 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7338 break;
7339 case OMP_SINGLE:
7340 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7341 break;
7342 case OMP_TARGET:
7343 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7344 OMP_CLAUSES (expr));
7345 break;
7346 case OMP_TARGET_DATA:
7347 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7348 OMP_CLAUSES (expr));
7349 break;
7350 case OMP_TEAMS:
7351 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7352 break;
7353 default:
7354 gcc_unreachable ();
7357 gimplify_seq_add_stmt (pre_p, stmt);
7358 *expr_p = NULL_TREE;
7361 /* Gimplify the gross structure of OpenACC update and OpenMP target update
7362 constructs. */
7364 static void
7365 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7367 tree expr = *expr_p, clauses;
7368 int kind;
7369 gimple stmt;
7371 switch (TREE_CODE (expr))
7373 case OACC_UPDATE:
7374 clauses = OACC_UPDATE_CLAUSES (expr);
7375 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
7376 break;
7377 case OMP_TARGET_UPDATE:
7378 clauses = OMP_TARGET_UPDATE_CLAUSES (expr);
7379 kind = GF_OMP_TARGET_KIND_UPDATE;
7380 break;
7381 default:
7382 gcc_unreachable ();
7384 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_WORKSHARE);
7385 gimplify_adjust_omp_clauses (pre_p, &clauses);
7386 stmt = gimple_build_omp_target (NULL, kind, clauses);
7388 gimplify_seq_add_stmt (pre_p, stmt);
7389 *expr_p = NULL_TREE;
7392 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7393 stabilized the lhs of the atomic operation as *ADDR. Return true if
7394 EXPR is this stabilized form. */
7396 static bool
7397 goa_lhs_expr_p (tree expr, tree addr)
7399 /* Also include casts to other type variants. The C front end is fond
7400 of adding these for e.g. volatile variables. This is like
7401 STRIP_TYPE_NOPS but includes the main variant lookup. */
7402 STRIP_USELESS_TYPE_CONVERSION (expr);
7404 if (TREE_CODE (expr) == INDIRECT_REF)
7406 expr = TREE_OPERAND (expr, 0);
7407 while (expr != addr
7408 && (CONVERT_EXPR_P (expr)
7409 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7410 && TREE_CODE (expr) == TREE_CODE (addr)
7411 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7413 expr = TREE_OPERAND (expr, 0);
7414 addr = TREE_OPERAND (addr, 0);
7416 if (expr == addr)
7417 return true;
7418 return (TREE_CODE (addr) == ADDR_EXPR
7419 && TREE_CODE (expr) == ADDR_EXPR
7420 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7422 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7423 return true;
7424 return false;
7427 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7428 expression does not involve the lhs, evaluate it into a temporary.
7429 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7430 or -1 if an error was encountered. */
7432 static int
7433 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7434 tree lhs_var)
7436 tree expr = *expr_p;
7437 int saw_lhs;
7439 if (goa_lhs_expr_p (expr, lhs_addr))
7441 *expr_p = lhs_var;
7442 return 1;
7444 if (is_gimple_val (expr))
7445 return 0;
7447 saw_lhs = 0;
7448 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7450 case tcc_binary:
7451 case tcc_comparison:
7452 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7453 lhs_var);
7454 case tcc_unary:
7455 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7456 lhs_var);
7457 break;
7458 case tcc_expression:
7459 switch (TREE_CODE (expr))
7461 case TRUTH_ANDIF_EXPR:
7462 case TRUTH_ORIF_EXPR:
7463 case TRUTH_AND_EXPR:
7464 case TRUTH_OR_EXPR:
7465 case TRUTH_XOR_EXPR:
7466 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7467 lhs_addr, lhs_var);
7468 case TRUTH_NOT_EXPR:
7469 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7470 lhs_addr, lhs_var);
7471 break;
7472 case COMPOUND_EXPR:
7473 /* Break out any preevaluations from cp_build_modify_expr. */
7474 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7475 expr = TREE_OPERAND (expr, 1))
7476 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7477 *expr_p = expr;
7478 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7479 default:
7480 break;
7482 break;
7483 default:
7484 break;
7487 if (saw_lhs == 0)
7489 enum gimplify_status gs;
7490 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7491 if (gs != GS_ALL_DONE)
7492 saw_lhs = -1;
7495 return saw_lhs;
7498 /* Gimplify an OMP_ATOMIC statement. */
7500 static enum gimplify_status
7501 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7503 tree addr = TREE_OPERAND (*expr_p, 0);
7504 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7505 ? NULL : TREE_OPERAND (*expr_p, 1);
7506 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7507 tree tmp_load;
7508 gimple loadstmt, storestmt;
7510 tmp_load = create_tmp_reg (type, NULL);
7511 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7512 return GS_ERROR;
7514 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7515 != GS_ALL_DONE)
7516 return GS_ERROR;
7518 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7519 gimplify_seq_add_stmt (pre_p, loadstmt);
7520 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7521 != GS_ALL_DONE)
7522 return GS_ERROR;
7524 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7525 rhs = tmp_load;
7526 storestmt = gimple_build_omp_atomic_store (rhs);
7527 gimplify_seq_add_stmt (pre_p, storestmt);
7528 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7530 gimple_omp_atomic_set_seq_cst (loadstmt);
7531 gimple_omp_atomic_set_seq_cst (storestmt);
7533 switch (TREE_CODE (*expr_p))
7535 case OMP_ATOMIC_READ:
7536 case OMP_ATOMIC_CAPTURE_OLD:
7537 *expr_p = tmp_load;
7538 gimple_omp_atomic_set_need_value (loadstmt);
7539 break;
7540 case OMP_ATOMIC_CAPTURE_NEW:
7541 *expr_p = rhs;
7542 gimple_omp_atomic_set_need_value (storestmt);
7543 break;
7544 default:
7545 *expr_p = NULL;
7546 break;
7549 return GS_ALL_DONE;
7552 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7553 body, and adding some EH bits. */
7555 static enum gimplify_status
7556 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7558 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7559 gimple g;
7560 gimple_seq body = NULL;
7561 int subcode = 0;
7563 /* Wrap the transaction body in a BIND_EXPR so we have a context
7564 where to put decls for OpenMP. */
7565 if (TREE_CODE (tbody) != BIND_EXPR)
7567 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7568 TREE_SIDE_EFFECTS (bind) = 1;
7569 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7570 TRANSACTION_EXPR_BODY (expr) = bind;
7573 push_gimplify_context ();
7574 temp = voidify_wrapper_expr (*expr_p, NULL);
7576 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7577 pop_gimplify_context (g);
7579 g = gimple_build_transaction (body, NULL);
7580 if (TRANSACTION_EXPR_OUTER (expr))
7581 subcode = GTMA_IS_OUTER;
7582 else if (TRANSACTION_EXPR_RELAXED (expr))
7583 subcode = GTMA_IS_RELAXED;
7584 gimple_transaction_set_subcode (g, subcode);
7586 gimplify_seq_add_stmt (pre_p, g);
7588 if (temp)
7590 *expr_p = temp;
7591 return GS_OK;
7594 *expr_p = NULL_TREE;
7595 return GS_ALL_DONE;
7598 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7599 expression produces a value to be used as an operand inside a GIMPLE
7600 statement, the value will be stored back in *EXPR_P. This value will
7601 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7602 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7603 emitted in PRE_P and POST_P.
7605 Additionally, this process may overwrite parts of the input
7606 expression during gimplification. Ideally, it should be
7607 possible to do non-destructive gimplification.
7609 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7610 the expression needs to evaluate to a value to be used as
7611 an operand in a GIMPLE statement, this value will be stored in
7612 *EXPR_P on exit. This happens when the caller specifies one
7613 of fb_lvalue or fb_rvalue fallback flags.
7615 PRE_P will contain the sequence of GIMPLE statements corresponding
7616 to the evaluation of EXPR and all the side-effects that must
7617 be executed before the main expression. On exit, the last
7618 statement of PRE_P is the core statement being gimplified. For
7619 instance, when gimplifying 'if (++a)' the last statement in
7620 PRE_P will be 'if (t.1)' where t.1 is the result of
7621 pre-incrementing 'a'.
7623 POST_P will contain the sequence of GIMPLE statements corresponding
7624 to the evaluation of all the side-effects that must be executed
7625 after the main expression. If this is NULL, the post
7626 side-effects are stored at the end of PRE_P.
7628 The reason why the output is split in two is to handle post
7629 side-effects explicitly. In some cases, an expression may have
7630 inner and outer post side-effects which need to be emitted in
7631 an order different from the one given by the recursive
7632 traversal. For instance, for the expression (*p--)++ the post
7633 side-effects of '--' must actually occur *after* the post
7634 side-effects of '++'. However, gimplification will first visit
7635 the inner expression, so if a separate POST sequence was not
7636 used, the resulting sequence would be:
7638 1 t.1 = *p
7639 2 p = p - 1
7640 3 t.2 = t.1 + 1
7641 4 *p = t.2
7643 However, the post-decrement operation in line #2 must not be
7644 evaluated until after the store to *p at line #4, so the
7645 correct sequence should be:
7647 1 t.1 = *p
7648 2 t.2 = t.1 + 1
7649 3 *p = t.2
7650 4 p = p - 1
7652 So, by specifying a separate post queue, it is possible
7653 to emit the post side-effects in the correct order.
7654 If POST_P is NULL, an internal queue will be used. Before
7655 returning to the caller, the sequence POST_P is appended to
7656 the main output sequence PRE_P.
7658 GIMPLE_TEST_F points to a function that takes a tree T and
7659 returns nonzero if T is in the GIMPLE form requested by the
7660 caller. The GIMPLE predicates are in gimple.c.
7662 FALLBACK tells the function what sort of a temporary we want if
7663 gimplification cannot produce an expression that complies with
7664 GIMPLE_TEST_F.
7666 fb_none means that no temporary should be generated
7667 fb_rvalue means that an rvalue is OK to generate
7668 fb_lvalue means that an lvalue is OK to generate
7669 fb_either means that either is OK, but an lvalue is preferable.
7670 fb_mayfail means that gimplification may fail (in which case
7671 GS_ERROR will be returned)
7673 The return value is either GS_ERROR or GS_ALL_DONE, since this
7674 function iterates until EXPR is completely gimplified or an error
7675 occurs. */
7677 enum gimplify_status
7678 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7679 bool (*gimple_test_f) (tree), fallback_t fallback)
7681 tree tmp;
7682 gimple_seq internal_pre = NULL;
7683 gimple_seq internal_post = NULL;
7684 tree save_expr;
7685 bool is_statement;
7686 location_t saved_location;
7687 enum gimplify_status ret;
7688 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7690 save_expr = *expr_p;
7691 if (save_expr == NULL_TREE)
7692 return GS_ALL_DONE;
7694 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7695 is_statement = gimple_test_f == is_gimple_stmt;
7696 if (is_statement)
7697 gcc_assert (pre_p);
7699 /* Consistency checks. */
7700 if (gimple_test_f == is_gimple_reg)
7701 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7702 else if (gimple_test_f == is_gimple_val
7703 || gimple_test_f == is_gimple_call_addr
7704 || gimple_test_f == is_gimple_condexpr
7705 || gimple_test_f == is_gimple_mem_rhs
7706 || gimple_test_f == is_gimple_mem_rhs_or_call
7707 || gimple_test_f == is_gimple_reg_rhs
7708 || gimple_test_f == is_gimple_reg_rhs_or_call
7709 || gimple_test_f == is_gimple_asm_val
7710 || gimple_test_f == is_gimple_mem_ref_addr)
7711 gcc_assert (fallback & fb_rvalue);
7712 else if (gimple_test_f == is_gimple_min_lval
7713 || gimple_test_f == is_gimple_lvalue)
7714 gcc_assert (fallback & fb_lvalue);
7715 else if (gimple_test_f == is_gimple_addressable)
7716 gcc_assert (fallback & fb_either);
7717 else if (gimple_test_f == is_gimple_stmt)
7718 gcc_assert (fallback == fb_none);
7719 else
7721 /* We should have recognized the GIMPLE_TEST_F predicate to
7722 know what kind of fallback to use in case a temporary is
7723 needed to hold the value or address of *EXPR_P. */
7724 gcc_unreachable ();
7727 /* We used to check the predicate here and return immediately if it
7728 succeeds. This is wrong; the design is for gimplification to be
7729 idempotent, and for the predicates to only test for valid forms, not
7730 whether they are fully simplified. */
7731 if (pre_p == NULL)
7732 pre_p = &internal_pre;
7734 if (post_p == NULL)
7735 post_p = &internal_post;
7737 /* Remember the last statements added to PRE_P and POST_P. Every
7738 new statement added by the gimplification helpers needs to be
7739 annotated with location information. To centralize the
7740 responsibility, we remember the last statement that had been
7741 added to both queues before gimplifying *EXPR_P. If
7742 gimplification produces new statements in PRE_P and POST_P, those
7743 statements will be annotated with the same location information
7744 as *EXPR_P. */
7745 pre_last_gsi = gsi_last (*pre_p);
7746 post_last_gsi = gsi_last (*post_p);
7748 saved_location = input_location;
7749 if (save_expr != error_mark_node
7750 && EXPR_HAS_LOCATION (*expr_p))
7751 input_location = EXPR_LOCATION (*expr_p);
7753 /* Loop over the specific gimplifiers until the toplevel node
7754 remains the same. */
7757 /* Strip away as many useless type conversions as possible
7758 at the toplevel. */
7759 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7761 /* Remember the expr. */
7762 save_expr = *expr_p;
7764 /* Die, die, die, my darling. */
7765 if (save_expr == error_mark_node
7766 || (TREE_TYPE (save_expr)
7767 && TREE_TYPE (save_expr) == error_mark_node))
7769 ret = GS_ERROR;
7770 break;
7773 /* Do any language-specific gimplification. */
7774 ret = ((enum gimplify_status)
7775 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7776 if (ret == GS_OK)
7778 if (*expr_p == NULL_TREE)
7779 break;
7780 if (*expr_p != save_expr)
7781 continue;
7783 else if (ret != GS_UNHANDLED)
7784 break;
7786 /* Make sure that all the cases set 'ret' appropriately. */
7787 ret = GS_UNHANDLED;
7788 switch (TREE_CODE (*expr_p))
7790 /* First deal with the special cases. */
7792 case POSTINCREMENT_EXPR:
7793 case POSTDECREMENT_EXPR:
7794 case PREINCREMENT_EXPR:
7795 case PREDECREMENT_EXPR:
7796 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7797 fallback != fb_none,
7798 TREE_TYPE (*expr_p));
7799 break;
7801 case VIEW_CONVERT_EXPR:
7802 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7803 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7805 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7806 post_p, is_gimple_val, fb_rvalue);
7807 recalculate_side_effects (*expr_p);
7808 break;
7810 /* Fallthru. */
7812 case ARRAY_REF:
7813 case ARRAY_RANGE_REF:
7814 case REALPART_EXPR:
7815 case IMAGPART_EXPR:
7816 case COMPONENT_REF:
7817 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7818 fallback ? fallback : fb_rvalue);
7819 break;
7821 case COND_EXPR:
7822 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7824 /* C99 code may assign to an array in a structure value of a
7825 conditional expression, and this has undefined behavior
7826 only on execution, so create a temporary if an lvalue is
7827 required. */
7828 if (fallback == fb_lvalue)
7830 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7831 mark_addressable (*expr_p);
7832 ret = GS_OK;
7834 break;
7836 case CALL_EXPR:
7837 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7839 /* C99 code may assign to an array in a structure returned
7840 from a function, and this has undefined behavior only on
7841 execution, so create a temporary if an lvalue is
7842 required. */
7843 if (fallback == fb_lvalue)
7845 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7846 mark_addressable (*expr_p);
7847 ret = GS_OK;
7849 break;
7851 case TREE_LIST:
7852 gcc_unreachable ();
7854 case COMPOUND_EXPR:
7855 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7856 break;
7858 case COMPOUND_LITERAL_EXPR:
7859 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7860 gimple_test_f, fallback);
7861 break;
7863 case MODIFY_EXPR:
7864 case INIT_EXPR:
7865 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7866 fallback != fb_none);
7867 break;
7869 case TRUTH_ANDIF_EXPR:
7870 case TRUTH_ORIF_EXPR:
7872 /* Preserve the original type of the expression and the
7873 source location of the outer expression. */
7874 tree org_type = TREE_TYPE (*expr_p);
7875 *expr_p = gimple_boolify (*expr_p);
7876 *expr_p = build3_loc (input_location, COND_EXPR,
7877 org_type, *expr_p,
7878 fold_convert_loc
7879 (input_location,
7880 org_type, boolean_true_node),
7881 fold_convert_loc
7882 (input_location,
7883 org_type, boolean_false_node));
7884 ret = GS_OK;
7885 break;
7888 case TRUTH_NOT_EXPR:
7890 tree type = TREE_TYPE (*expr_p);
7891 /* The parsers are careful to generate TRUTH_NOT_EXPR
7892 only with operands that are always zero or one.
7893 We do not fold here but handle the only interesting case
7894 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7895 *expr_p = gimple_boolify (*expr_p);
7896 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7897 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7898 TREE_TYPE (*expr_p),
7899 TREE_OPERAND (*expr_p, 0));
7900 else
7901 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7902 TREE_TYPE (*expr_p),
7903 TREE_OPERAND (*expr_p, 0),
7904 build_int_cst (TREE_TYPE (*expr_p), 1));
7905 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7906 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7907 ret = GS_OK;
7908 break;
7911 case ADDR_EXPR:
7912 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7913 break;
7915 case ANNOTATE_EXPR:
7917 tree cond = TREE_OPERAND (*expr_p, 0);
7918 tree kind = TREE_OPERAND (*expr_p, 1);
7919 tree type = TREE_TYPE (cond);
7920 if (!INTEGRAL_TYPE_P (type))
7922 *expr_p = cond;
7923 ret = GS_OK;
7924 break;
7926 tree tmp = create_tmp_var (type, NULL);
7927 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7928 gimple call
7929 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7930 gimple_call_set_lhs (call, tmp);
7931 gimplify_seq_add_stmt (pre_p, call);
7932 *expr_p = tmp;
7933 ret = GS_ALL_DONE;
7934 break;
7937 case VA_ARG_EXPR:
7938 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7939 break;
7941 CASE_CONVERT:
7942 if (IS_EMPTY_STMT (*expr_p))
7944 ret = GS_ALL_DONE;
7945 break;
7948 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7949 || fallback == fb_none)
7951 /* Just strip a conversion to void (or in void context) and
7952 try again. */
7953 *expr_p = TREE_OPERAND (*expr_p, 0);
7954 ret = GS_OK;
7955 break;
7958 ret = gimplify_conversion (expr_p);
7959 if (ret == GS_ERROR)
7960 break;
7961 if (*expr_p != save_expr)
7962 break;
7963 /* FALLTHRU */
7965 case FIX_TRUNC_EXPR:
7966 /* unary_expr: ... | '(' cast ')' val | ... */
7967 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7968 is_gimple_val, fb_rvalue);
7969 recalculate_side_effects (*expr_p);
7970 break;
7972 case INDIRECT_REF:
7974 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7975 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7976 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7978 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7979 if (*expr_p != save_expr)
7981 ret = GS_OK;
7982 break;
7985 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7986 is_gimple_reg, fb_rvalue);
7987 if (ret == GS_ERROR)
7988 break;
7990 recalculate_side_effects (*expr_p);
7991 *expr_p = fold_build2_loc (input_location, MEM_REF,
7992 TREE_TYPE (*expr_p),
7993 TREE_OPERAND (*expr_p, 0),
7994 build_int_cst (saved_ptr_type, 0));
7995 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7996 TREE_THIS_NOTRAP (*expr_p) = notrap;
7997 ret = GS_OK;
7998 break;
8001 /* We arrive here through the various re-gimplifcation paths. */
8002 case MEM_REF:
8003 /* First try re-folding the whole thing. */
8004 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
8005 TREE_OPERAND (*expr_p, 0),
8006 TREE_OPERAND (*expr_p, 1));
8007 if (tmp)
8009 *expr_p = tmp;
8010 recalculate_side_effects (*expr_p);
8011 ret = GS_OK;
8012 break;
8014 /* Avoid re-gimplifying the address operand if it is already
8015 in suitable form. Re-gimplifying would mark the address
8016 operand addressable. Always gimplify when not in SSA form
8017 as we still may have to gimplify decls with value-exprs. */
8018 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
8019 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
8021 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8022 is_gimple_mem_ref_addr, fb_rvalue);
8023 if (ret == GS_ERROR)
8024 break;
8026 recalculate_side_effects (*expr_p);
8027 ret = GS_ALL_DONE;
8028 break;
8030 /* Constants need not be gimplified. */
8031 case INTEGER_CST:
8032 case REAL_CST:
8033 case FIXED_CST:
8034 case STRING_CST:
8035 case COMPLEX_CST:
8036 case VECTOR_CST:
8037 /* Drop the overflow flag on constants, we do not want
8038 that in the GIMPLE IL. */
8039 if (TREE_OVERFLOW_P (*expr_p))
8040 *expr_p = drop_tree_overflow (*expr_p);
8041 ret = GS_ALL_DONE;
8042 break;
8044 case CONST_DECL:
8045 /* If we require an lvalue, such as for ADDR_EXPR, retain the
8046 CONST_DECL node. Otherwise the decl is replaceable by its
8047 value. */
8048 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
8049 if (fallback & fb_lvalue)
8050 ret = GS_ALL_DONE;
8051 else
8053 *expr_p = DECL_INITIAL (*expr_p);
8054 ret = GS_OK;
8056 break;
8058 case DECL_EXPR:
8059 ret = gimplify_decl_expr (expr_p, pre_p);
8060 break;
8062 case BIND_EXPR:
8063 ret = gimplify_bind_expr (expr_p, pre_p);
8064 break;
8066 case LOOP_EXPR:
8067 ret = gimplify_loop_expr (expr_p, pre_p);
8068 break;
8070 case SWITCH_EXPR:
8071 ret = gimplify_switch_expr (expr_p, pre_p);
8072 break;
8074 case EXIT_EXPR:
8075 ret = gimplify_exit_expr (expr_p);
8076 break;
8078 case GOTO_EXPR:
8079 /* If the target is not LABEL, then it is a computed jump
8080 and the target needs to be gimplified. */
8081 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
8083 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
8084 NULL, is_gimple_val, fb_rvalue);
8085 if (ret == GS_ERROR)
8086 break;
8088 gimplify_seq_add_stmt (pre_p,
8089 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
8090 ret = GS_ALL_DONE;
8091 break;
8093 case PREDICT_EXPR:
8094 gimplify_seq_add_stmt (pre_p,
8095 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
8096 PREDICT_EXPR_OUTCOME (*expr_p)));
8097 ret = GS_ALL_DONE;
8098 break;
8100 case LABEL_EXPR:
8101 ret = GS_ALL_DONE;
8102 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
8103 == current_function_decl);
8104 gimplify_seq_add_stmt (pre_p,
8105 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
8106 break;
8108 case CASE_LABEL_EXPR:
8109 ret = gimplify_case_label_expr (expr_p, pre_p);
8110 break;
8112 case RETURN_EXPR:
8113 ret = gimplify_return_expr (*expr_p, pre_p);
8114 break;
8116 case CONSTRUCTOR:
8117 /* Don't reduce this in place; let gimplify_init_constructor work its
8118 magic. Buf if we're just elaborating this for side effects, just
8119 gimplify any element that has side-effects. */
8120 if (fallback == fb_none)
8122 unsigned HOST_WIDE_INT ix;
8123 tree val;
8124 tree temp = NULL_TREE;
8125 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
8126 if (TREE_SIDE_EFFECTS (val))
8127 append_to_statement_list (val, &temp);
8129 *expr_p = temp;
8130 ret = temp ? GS_OK : GS_ALL_DONE;
8132 /* C99 code may assign to an array in a constructed
8133 structure or union, and this has undefined behavior only
8134 on execution, so create a temporary if an lvalue is
8135 required. */
8136 else if (fallback == fb_lvalue)
8138 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8139 mark_addressable (*expr_p);
8140 ret = GS_OK;
8142 else
8143 ret = GS_ALL_DONE;
8144 break;
8146 /* The following are special cases that are not handled by the
8147 original GIMPLE grammar. */
8149 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
8150 eliminated. */
8151 case SAVE_EXPR:
8152 ret = gimplify_save_expr (expr_p, pre_p, post_p);
8153 break;
8155 case BIT_FIELD_REF:
8156 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8157 post_p, is_gimple_lvalue, fb_either);
8158 recalculate_side_effects (*expr_p);
8159 break;
8161 case TARGET_MEM_REF:
8163 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
8165 if (TMR_BASE (*expr_p))
8166 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
8167 post_p, is_gimple_mem_ref_addr, fb_either);
8168 if (TMR_INDEX (*expr_p))
8169 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
8170 post_p, is_gimple_val, fb_rvalue);
8171 if (TMR_INDEX2 (*expr_p))
8172 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
8173 post_p, is_gimple_val, fb_rvalue);
8174 /* TMR_STEP and TMR_OFFSET are always integer constants. */
8175 ret = MIN (r0, r1);
8177 break;
8179 case NON_LVALUE_EXPR:
8180 /* This should have been stripped above. */
8181 gcc_unreachable ();
8183 case ASM_EXPR:
8184 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
8185 break;
8187 case TRY_FINALLY_EXPR:
8188 case TRY_CATCH_EXPR:
8190 gimple_seq eval, cleanup;
8191 gimple try_;
8193 /* Calls to destructors are generated automatically in FINALLY/CATCH
8194 block. They should have location as UNKNOWN_LOCATION. However,
8195 gimplify_call_expr will reset these call stmts to input_location
8196 if it finds stmt's location is unknown. To prevent resetting for
8197 destructors, we set the input_location to unknown.
8198 Note that this only affects the destructor calls in FINALLY/CATCH
8199 block, and will automatically reset to its original value by the
8200 end of gimplify_expr. */
8201 input_location = UNKNOWN_LOCATION;
8202 eval = cleanup = NULL;
8203 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8204 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8205 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8206 if (gimple_seq_empty_p (cleanup))
8208 gimple_seq_add_seq (pre_p, eval);
8209 ret = GS_ALL_DONE;
8210 break;
8212 try_ = gimple_build_try (eval, cleanup,
8213 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8214 ? GIMPLE_TRY_FINALLY
8215 : GIMPLE_TRY_CATCH);
8216 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8217 gimple_set_location (try_, saved_location);
8218 else
8219 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8220 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8221 gimple_try_set_catch_is_cleanup (try_,
8222 TRY_CATCH_IS_CLEANUP (*expr_p));
8223 gimplify_seq_add_stmt (pre_p, try_);
8224 ret = GS_ALL_DONE;
8225 break;
8228 case CLEANUP_POINT_EXPR:
8229 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8230 break;
8232 case TARGET_EXPR:
8233 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8234 break;
8236 case CATCH_EXPR:
8238 gimple c;
8239 gimple_seq handler = NULL;
8240 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8241 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8242 gimplify_seq_add_stmt (pre_p, c);
8243 ret = GS_ALL_DONE;
8244 break;
8247 case EH_FILTER_EXPR:
8249 gimple ehf;
8250 gimple_seq failure = NULL;
8252 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8253 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8254 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8255 gimplify_seq_add_stmt (pre_p, ehf);
8256 ret = GS_ALL_DONE;
8257 break;
8260 case OBJ_TYPE_REF:
8262 enum gimplify_status r0, r1;
8263 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8264 post_p, is_gimple_val, fb_rvalue);
8265 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8266 post_p, is_gimple_val, fb_rvalue);
8267 TREE_SIDE_EFFECTS (*expr_p) = 0;
8268 ret = MIN (r0, r1);
8270 break;
8272 case LABEL_DECL:
8273 /* We get here when taking the address of a label. We mark
8274 the label as "forced"; meaning it can never be removed and
8275 it is a potential target for any computed goto. */
8276 FORCED_LABEL (*expr_p) = 1;
8277 ret = GS_ALL_DONE;
8278 break;
8280 case STATEMENT_LIST:
8281 ret = gimplify_statement_list (expr_p, pre_p);
8282 break;
8284 case WITH_SIZE_EXPR:
8286 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8287 post_p == &internal_post ? NULL : post_p,
8288 gimple_test_f, fallback);
8289 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8290 is_gimple_val, fb_rvalue);
8291 ret = GS_ALL_DONE;
8293 break;
8295 case VAR_DECL:
8296 case PARM_DECL:
8297 ret = gimplify_var_or_parm_decl (expr_p);
8298 break;
8300 case RESULT_DECL:
8301 /* When within an OpenMP context, notice uses of variables. */
8302 if (gimplify_omp_ctxp)
8303 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8304 ret = GS_ALL_DONE;
8305 break;
8307 case SSA_NAME:
8308 /* Allow callbacks into the gimplifier during optimization. */
8309 ret = GS_ALL_DONE;
8310 break;
8312 case OACC_HOST_DATA:
8313 case OACC_DECLARE:
8314 case OACC_ENTER_DATA:
8315 case OACC_EXIT_DATA:
8316 case OACC_WAIT:
8317 case OACC_CACHE:
8318 sorry ("directive not yet implemented");
8319 ret = GS_ALL_DONE;
8320 break;
8322 case OMP_PARALLEL:
8323 gimplify_omp_parallel (expr_p, pre_p);
8324 ret = GS_ALL_DONE;
8325 break;
8327 case OMP_TASK:
8328 gimplify_omp_task (expr_p, pre_p);
8329 ret = GS_ALL_DONE;
8330 break;
8332 case OMP_FOR:
8333 case OMP_SIMD:
8334 case CILK_SIMD:
8335 case CILK_FOR:
8336 case OMP_DISTRIBUTE:
8337 case OACC_LOOP:
8338 ret = gimplify_omp_for (expr_p, pre_p);
8339 break;
8341 case OACC_KERNELS:
8342 if (OACC_KERNELS_COMBINED (*expr_p))
8343 sorry ("directive not yet implemented");
8344 else
8345 gimplify_omp_workshare (expr_p, pre_p);
8346 ret = GS_ALL_DONE;
8347 break;
8349 case OACC_PARALLEL:
8350 if (OACC_PARALLEL_COMBINED (*expr_p))
8351 sorry ("directive not yet implemented");
8352 else
8353 gimplify_omp_workshare (expr_p, pre_p);
8354 ret = GS_ALL_DONE;
8355 break;
8357 case OACC_DATA:
8358 case OMP_SECTIONS:
8359 case OMP_SINGLE:
8360 case OMP_TARGET:
8361 case OMP_TARGET_DATA:
8362 case OMP_TEAMS:
8363 gimplify_omp_workshare (expr_p, pre_p);
8364 ret = GS_ALL_DONE;
8365 break;
8367 case OACC_UPDATE:
8368 case OMP_TARGET_UPDATE:
8369 gimplify_omp_target_update (expr_p, pre_p);
8370 ret = GS_ALL_DONE;
8371 break;
8373 case OMP_SECTION:
8374 case OMP_MASTER:
8375 case OMP_TASKGROUP:
8376 case OMP_ORDERED:
8377 case OMP_CRITICAL:
8379 gimple_seq body = NULL;
8380 gimple g;
8382 gimplify_and_add (OMP_BODY (*expr_p), &body);
8383 switch (TREE_CODE (*expr_p))
8385 case OMP_SECTION:
8386 g = gimple_build_omp_section (body);
8387 break;
8388 case OMP_MASTER:
8389 g = gimple_build_omp_master (body);
8390 break;
8391 case OMP_TASKGROUP:
8393 gimple_seq cleanup = NULL;
8394 tree fn
8395 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8396 g = gimple_build_call (fn, 0);
8397 gimple_seq_add_stmt (&cleanup, g);
8398 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8399 body = NULL;
8400 gimple_seq_add_stmt (&body, g);
8401 g = gimple_build_omp_taskgroup (body);
8403 break;
8404 case OMP_ORDERED:
8405 g = gimple_build_omp_ordered (body);
8406 break;
8407 case OMP_CRITICAL:
8408 g = gimple_build_omp_critical (body,
8409 OMP_CRITICAL_NAME (*expr_p));
8410 break;
8411 default:
8412 gcc_unreachable ();
8414 gimplify_seq_add_stmt (pre_p, g);
8415 ret = GS_ALL_DONE;
8416 break;
8419 case OMP_ATOMIC:
8420 case OMP_ATOMIC_READ:
8421 case OMP_ATOMIC_CAPTURE_OLD:
8422 case OMP_ATOMIC_CAPTURE_NEW:
8423 ret = gimplify_omp_atomic (expr_p, pre_p);
8424 break;
8426 case TRANSACTION_EXPR:
8427 ret = gimplify_transaction (expr_p, pre_p);
8428 break;
8430 case TRUTH_AND_EXPR:
8431 case TRUTH_OR_EXPR:
8432 case TRUTH_XOR_EXPR:
8434 tree orig_type = TREE_TYPE (*expr_p);
8435 tree new_type, xop0, xop1;
8436 *expr_p = gimple_boolify (*expr_p);
8437 new_type = TREE_TYPE (*expr_p);
8438 if (!useless_type_conversion_p (orig_type, new_type))
8440 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8441 ret = GS_OK;
8442 break;
8445 /* Boolified binary truth expressions are semantically equivalent
8446 to bitwise binary expressions. Canonicalize them to the
8447 bitwise variant. */
8448 switch (TREE_CODE (*expr_p))
8450 case TRUTH_AND_EXPR:
8451 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8452 break;
8453 case TRUTH_OR_EXPR:
8454 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8455 break;
8456 case TRUTH_XOR_EXPR:
8457 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8458 break;
8459 default:
8460 break;
8462 /* Now make sure that operands have compatible type to
8463 expression's new_type. */
8464 xop0 = TREE_OPERAND (*expr_p, 0);
8465 xop1 = TREE_OPERAND (*expr_p, 1);
8466 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8467 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8468 new_type,
8469 xop0);
8470 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8471 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8472 new_type,
8473 xop1);
8474 /* Continue classified as tcc_binary. */
8475 goto expr_2;
8478 case FMA_EXPR:
8479 case VEC_COND_EXPR:
8480 case VEC_PERM_EXPR:
8481 /* Classified as tcc_expression. */
8482 goto expr_3;
8484 case POINTER_PLUS_EXPR:
8486 enum gimplify_status r0, r1;
8487 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8488 post_p, is_gimple_val, fb_rvalue);
8489 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8490 post_p, is_gimple_val, fb_rvalue);
8491 recalculate_side_effects (*expr_p);
8492 ret = MIN (r0, r1);
8493 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8494 after gimplifying operands - this is similar to how
8495 it would be folding all gimplified stmts on creation
8496 to have them canonicalized, which is what we eventually
8497 should do anyway. */
8498 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8499 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8501 *expr_p = build_fold_addr_expr_with_type_loc
8502 (input_location,
8503 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8504 TREE_OPERAND (*expr_p, 0),
8505 fold_convert (ptr_type_node,
8506 TREE_OPERAND (*expr_p, 1))),
8507 TREE_TYPE (*expr_p));
8508 ret = MIN (ret, GS_OK);
8510 break;
8513 case CILK_SYNC_STMT:
8515 if (!fn_contains_cilk_spawn_p (cfun))
8517 error_at (EXPR_LOCATION (*expr_p),
8518 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8519 ret = GS_ERROR;
8521 else
8523 gimplify_cilk_sync (expr_p, pre_p);
8524 ret = GS_ALL_DONE;
8526 break;
8529 default:
8530 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8532 case tcc_comparison:
8533 /* Handle comparison of objects of non scalar mode aggregates
8534 with a call to memcmp. It would be nice to only have to do
8535 this for variable-sized objects, but then we'd have to allow
8536 the same nest of reference nodes we allow for MODIFY_EXPR and
8537 that's too complex.
8539 Compare scalar mode aggregates as scalar mode values. Using
8540 memcmp for them would be very inefficient at best, and is
8541 plain wrong if bitfields are involved. */
8543 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8545 /* Vector comparisons need no boolification. */
8546 if (TREE_CODE (type) == VECTOR_TYPE)
8547 goto expr_2;
8548 else if (!AGGREGATE_TYPE_P (type))
8550 tree org_type = TREE_TYPE (*expr_p);
8551 *expr_p = gimple_boolify (*expr_p);
8552 if (!useless_type_conversion_p (org_type,
8553 TREE_TYPE (*expr_p)))
8555 *expr_p = fold_convert_loc (input_location,
8556 org_type, *expr_p);
8557 ret = GS_OK;
8559 else
8560 goto expr_2;
8562 else if (TYPE_MODE (type) != BLKmode)
8563 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8564 else
8565 ret = gimplify_variable_sized_compare (expr_p);
8567 break;
8570 /* If *EXPR_P does not need to be special-cased, handle it
8571 according to its class. */
8572 case tcc_unary:
8573 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8574 post_p, is_gimple_val, fb_rvalue);
8575 break;
8577 case tcc_binary:
8578 expr_2:
8580 enum gimplify_status r0, r1;
8582 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8583 post_p, is_gimple_val, fb_rvalue);
8584 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8585 post_p, is_gimple_val, fb_rvalue);
8587 ret = MIN (r0, r1);
8588 break;
8591 expr_3:
8593 enum gimplify_status r0, r1, r2;
8595 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8596 post_p, is_gimple_val, fb_rvalue);
8597 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8598 post_p, is_gimple_val, fb_rvalue);
8599 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8600 post_p, is_gimple_val, fb_rvalue);
8602 ret = MIN (MIN (r0, r1), r2);
8603 break;
8606 case tcc_declaration:
8607 case tcc_constant:
8608 ret = GS_ALL_DONE;
8609 goto dont_recalculate;
8611 default:
8612 gcc_unreachable ();
8615 recalculate_side_effects (*expr_p);
8617 dont_recalculate:
8618 break;
8621 gcc_assert (*expr_p || ret != GS_OK);
8623 while (ret == GS_OK);
8625 /* If we encountered an error_mark somewhere nested inside, either
8626 stub out the statement or propagate the error back out. */
8627 if (ret == GS_ERROR)
8629 if (is_statement)
8630 *expr_p = NULL;
8631 goto out;
8634 /* This was only valid as a return value from the langhook, which
8635 we handled. Make sure it doesn't escape from any other context. */
8636 gcc_assert (ret != GS_UNHANDLED);
8638 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8640 /* We aren't looking for a value, and we don't have a valid
8641 statement. If it doesn't have side-effects, throw it away. */
8642 if (!TREE_SIDE_EFFECTS (*expr_p))
8643 *expr_p = NULL;
8644 else if (!TREE_THIS_VOLATILE (*expr_p))
8646 /* This is probably a _REF that contains something nested that
8647 has side effects. Recurse through the operands to find it. */
8648 enum tree_code code = TREE_CODE (*expr_p);
8650 switch (code)
8652 case COMPONENT_REF:
8653 case REALPART_EXPR:
8654 case IMAGPART_EXPR:
8655 case VIEW_CONVERT_EXPR:
8656 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8657 gimple_test_f, fallback);
8658 break;
8660 case ARRAY_REF:
8661 case ARRAY_RANGE_REF:
8662 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8663 gimple_test_f, fallback);
8664 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8665 gimple_test_f, fallback);
8666 break;
8668 default:
8669 /* Anything else with side-effects must be converted to
8670 a valid statement before we get here. */
8671 gcc_unreachable ();
8674 *expr_p = NULL;
8676 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8677 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8679 /* Historically, the compiler has treated a bare reference
8680 to a non-BLKmode volatile lvalue as forcing a load. */
8681 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8683 /* Normally, we do not want to create a temporary for a
8684 TREE_ADDRESSABLE type because such a type should not be
8685 copied by bitwise-assignment. However, we make an
8686 exception here, as all we are doing here is ensuring that
8687 we read the bytes that make up the type. We use
8688 create_tmp_var_raw because create_tmp_var will abort when
8689 given a TREE_ADDRESSABLE type. */
8690 tree tmp = create_tmp_var_raw (type, "vol");
8691 gimple_add_tmp_var (tmp);
8692 gimplify_assign (tmp, *expr_p, pre_p);
8693 *expr_p = NULL;
8695 else
8696 /* We can't do anything useful with a volatile reference to
8697 an incomplete type, so just throw it away. Likewise for
8698 a BLKmode type, since any implicit inner load should
8699 already have been turned into an explicit one by the
8700 gimplification process. */
8701 *expr_p = NULL;
8704 /* If we are gimplifying at the statement level, we're done. Tack
8705 everything together and return. */
8706 if (fallback == fb_none || is_statement)
8708 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8709 it out for GC to reclaim it. */
8710 *expr_p = NULL_TREE;
8712 if (!gimple_seq_empty_p (internal_pre)
8713 || !gimple_seq_empty_p (internal_post))
8715 gimplify_seq_add_seq (&internal_pre, internal_post);
8716 gimplify_seq_add_seq (pre_p, internal_pre);
8719 /* The result of gimplifying *EXPR_P is going to be the last few
8720 statements in *PRE_P and *POST_P. Add location information
8721 to all the statements that were added by the gimplification
8722 helpers. */
8723 if (!gimple_seq_empty_p (*pre_p))
8724 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8726 if (!gimple_seq_empty_p (*post_p))
8727 annotate_all_with_location_after (*post_p, post_last_gsi,
8728 input_location);
8730 goto out;
8733 #ifdef ENABLE_GIMPLE_CHECKING
8734 if (*expr_p)
8736 enum tree_code code = TREE_CODE (*expr_p);
8737 /* These expressions should already be in gimple IR form. */
8738 gcc_assert (code != MODIFY_EXPR
8739 && code != ASM_EXPR
8740 && code != BIND_EXPR
8741 && code != CATCH_EXPR
8742 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8743 && code != EH_FILTER_EXPR
8744 && code != GOTO_EXPR
8745 && code != LABEL_EXPR
8746 && code != LOOP_EXPR
8747 && code != SWITCH_EXPR
8748 && code != TRY_FINALLY_EXPR
8749 && code != OACC_PARALLEL
8750 && code != OACC_KERNELS
8751 && code != OACC_DATA
8752 && code != OACC_HOST_DATA
8753 && code != OACC_DECLARE
8754 && code != OACC_UPDATE
8755 && code != OACC_ENTER_DATA
8756 && code != OACC_EXIT_DATA
8757 && code != OACC_WAIT
8758 && code != OACC_CACHE
8759 && code != OMP_CRITICAL
8760 && code != OMP_FOR
8761 && code != OACC_LOOP
8762 && code != OMP_MASTER
8763 && code != OMP_TASKGROUP
8764 && code != OMP_ORDERED
8765 && code != OMP_PARALLEL
8766 && code != OMP_SECTIONS
8767 && code != OMP_SECTION
8768 && code != OMP_SINGLE);
8770 #endif
8772 /* Otherwise we're gimplifying a subexpression, so the resulting
8773 value is interesting. If it's a valid operand that matches
8774 GIMPLE_TEST_F, we're done. Unless we are handling some
8775 post-effects internally; if that's the case, we need to copy into
8776 a temporary before adding the post-effects to POST_P. */
8777 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8778 goto out;
8780 /* Otherwise, we need to create a new temporary for the gimplified
8781 expression. */
8783 /* We can't return an lvalue if we have an internal postqueue. The
8784 object the lvalue refers to would (probably) be modified by the
8785 postqueue; we need to copy the value out first, which means an
8786 rvalue. */
8787 if ((fallback & fb_lvalue)
8788 && gimple_seq_empty_p (internal_post)
8789 && is_gimple_addressable (*expr_p))
8791 /* An lvalue will do. Take the address of the expression, store it
8792 in a temporary, and replace the expression with an INDIRECT_REF of
8793 that temporary. */
8794 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8795 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8796 *expr_p = build_simple_mem_ref (tmp);
8798 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8800 /* An rvalue will do. Assign the gimplified expression into a
8801 new temporary TMP and replace the original expression with
8802 TMP. First, make sure that the expression has a type so that
8803 it can be assigned into a temporary. */
8804 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8805 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8807 else
8809 #ifdef ENABLE_GIMPLE_CHECKING
8810 if (!(fallback & fb_mayfail))
8812 fprintf (stderr, "gimplification failed:\n");
8813 print_generic_expr (stderr, *expr_p, 0);
8814 debug_tree (*expr_p);
8815 internal_error ("gimplification failed");
8817 #endif
8818 gcc_assert (fallback & fb_mayfail);
8820 /* If this is an asm statement, and the user asked for the
8821 impossible, don't die. Fail and let gimplify_asm_expr
8822 issue an error. */
8823 ret = GS_ERROR;
8824 goto out;
8827 /* Make sure the temporary matches our predicate. */
8828 gcc_assert ((*gimple_test_f) (*expr_p));
8830 if (!gimple_seq_empty_p (internal_post))
8832 annotate_all_with_location (internal_post, input_location);
8833 gimplify_seq_add_seq (pre_p, internal_post);
8836 out:
8837 input_location = saved_location;
8838 return ret;
8841 /* Look through TYPE for variable-sized objects and gimplify each such
8842 size that we find. Add to LIST_P any statements generated. */
8844 void
8845 gimplify_type_sizes (tree type, gimple_seq *list_p)
8847 tree field, t;
8849 if (type == NULL || type == error_mark_node)
8850 return;
8852 /* We first do the main variant, then copy into any other variants. */
8853 type = TYPE_MAIN_VARIANT (type);
8855 /* Avoid infinite recursion. */
8856 if (TYPE_SIZES_GIMPLIFIED (type))
8857 return;
8859 TYPE_SIZES_GIMPLIFIED (type) = 1;
8861 switch (TREE_CODE (type))
8863 case INTEGER_TYPE:
8864 case ENUMERAL_TYPE:
8865 case BOOLEAN_TYPE:
8866 case REAL_TYPE:
8867 case FIXED_POINT_TYPE:
8868 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8869 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8871 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8873 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8874 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8876 break;
8878 case ARRAY_TYPE:
8879 /* These types may not have declarations, so handle them here. */
8880 gimplify_type_sizes (TREE_TYPE (type), list_p);
8881 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8882 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8883 with assigned stack slots, for -O1+ -g they should be tracked
8884 by VTA. */
8885 if (!(TYPE_NAME (type)
8886 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8887 && DECL_IGNORED_P (TYPE_NAME (type)))
8888 && TYPE_DOMAIN (type)
8889 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8891 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8892 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8893 DECL_IGNORED_P (t) = 0;
8894 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8895 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8896 DECL_IGNORED_P (t) = 0;
8898 break;
8900 case RECORD_TYPE:
8901 case UNION_TYPE:
8902 case QUAL_UNION_TYPE:
8903 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8904 if (TREE_CODE (field) == FIELD_DECL)
8906 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8907 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8908 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8909 gimplify_type_sizes (TREE_TYPE (field), list_p);
8911 break;
8913 case POINTER_TYPE:
8914 case REFERENCE_TYPE:
8915 /* We used to recurse on the pointed-to type here, which turned out to
8916 be incorrect because its definition might refer to variables not
8917 yet initialized at this point if a forward declaration is involved.
8919 It was actually useful for anonymous pointed-to types to ensure
8920 that the sizes evaluation dominates every possible later use of the
8921 values. Restricting to such types here would be safe since there
8922 is no possible forward declaration around, but would introduce an
8923 undesirable middle-end semantic to anonymity. We then defer to
8924 front-ends the responsibility of ensuring that the sizes are
8925 evaluated both early and late enough, e.g. by attaching artificial
8926 type declarations to the tree. */
8927 break;
8929 default:
8930 break;
8933 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8934 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8936 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8938 TYPE_SIZE (t) = TYPE_SIZE (type);
8939 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8940 TYPE_SIZES_GIMPLIFIED (t) = 1;
8944 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8945 a size or position, has had all of its SAVE_EXPRs evaluated.
8946 We add any required statements to *STMT_P. */
8948 void
8949 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8951 tree expr = *expr_p;
8953 /* We don't do anything if the value isn't there, is constant, or contains
8954 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8955 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8956 will want to replace it with a new variable, but that will cause problems
8957 if this type is from outside the function. It's OK to have that here. */
8958 if (is_gimple_sizepos (expr))
8959 return;
8961 *expr_p = unshare_expr (expr);
8963 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8966 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8967 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8968 is true, also gimplify the parameters. */
8970 gimple
8971 gimplify_body (tree fndecl, bool do_parms)
8973 location_t saved_location = input_location;
8974 gimple_seq parm_stmts, seq;
8975 gimple outer_bind;
8976 struct cgraph_node *cgn;
8978 timevar_push (TV_TREE_GIMPLIFY);
8980 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8981 gimplification. */
8982 default_rtl_profile ();
8984 gcc_assert (gimplify_ctxp == NULL);
8985 push_gimplify_context ();
8987 if (flag_openacc || flag_openmp)
8989 gcc_assert (gimplify_omp_ctxp == NULL);
8990 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8991 gimplify_omp_ctxp
8992 = new_omp_context ((enum omp_region_type) (ORT_TARGET
8993 | ORT_TARGET_OFFLOAD));
8996 /* Unshare most shared trees in the body and in that of any nested functions.
8997 It would seem we don't have to do this for nested functions because
8998 they are supposed to be output and then the outer function gimplified
8999 first, but the g++ front end doesn't always do it that way. */
9000 unshare_body (fndecl);
9001 unvisit_body (fndecl);
9003 cgn = cgraph_node::get (fndecl);
9004 if (cgn && cgn->origin)
9005 nonlocal_vlas = new hash_set<tree>;
9007 /* Make sure input_location isn't set to something weird. */
9008 input_location = DECL_SOURCE_LOCATION (fndecl);
9010 /* Resolve callee-copies. This has to be done before processing
9011 the body so that DECL_VALUE_EXPR gets processed correctly. */
9012 parm_stmts = do_parms ? gimplify_parameters () : NULL;
9014 /* Gimplify the function's body. */
9015 seq = NULL;
9016 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
9017 outer_bind = gimple_seq_first_stmt (seq);
9018 if (!outer_bind)
9020 outer_bind = gimple_build_nop ();
9021 gimplify_seq_add_stmt (&seq, outer_bind);
9024 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
9025 not the case, wrap everything in a GIMPLE_BIND to make it so. */
9026 if (gimple_code (outer_bind) == GIMPLE_BIND
9027 && gimple_seq_first (seq) == gimple_seq_last (seq))
9029 else
9030 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
9032 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9034 /* If we had callee-copies statements, insert them at the beginning
9035 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
9036 if (!gimple_seq_empty_p (parm_stmts))
9038 tree parm;
9040 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
9041 gimple_bind_set_body (outer_bind, parm_stmts);
9043 for (parm = DECL_ARGUMENTS (current_function_decl);
9044 parm; parm = DECL_CHAIN (parm))
9045 if (DECL_HAS_VALUE_EXPR_P (parm))
9047 DECL_HAS_VALUE_EXPR_P (parm) = 0;
9048 DECL_IGNORED_P (parm) = 0;
9052 if (nonlocal_vlas)
9054 if (nonlocal_vla_vars)
9056 /* tree-nested.c may later on call declare_vars (..., true);
9057 which relies on BLOCK_VARS chain to be the tail of the
9058 gimple_bind_vars chain. Ensure we don't violate that
9059 assumption. */
9060 if (gimple_bind_block (outer_bind)
9061 == DECL_INITIAL (current_function_decl))
9062 declare_vars (nonlocal_vla_vars, outer_bind, true);
9063 else
9064 BLOCK_VARS (DECL_INITIAL (current_function_decl))
9065 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
9066 nonlocal_vla_vars);
9067 nonlocal_vla_vars = NULL_TREE;
9069 delete nonlocal_vlas;
9070 nonlocal_vlas = NULL;
9073 if ((flag_openacc || flag_openmp || flag_openmp_simd)
9074 && gimplify_omp_ctxp)
9076 delete_omp_context (gimplify_omp_ctxp);
9077 gimplify_omp_ctxp = NULL;
9080 pop_gimplify_context (outer_bind);
9081 gcc_assert (gimplify_ctxp == NULL);
9083 #ifdef ENABLE_CHECKING
9084 if (!seen_error ())
9085 verify_gimple_in_seq (gimple_bind_body (outer_bind));
9086 #endif
9088 timevar_pop (TV_TREE_GIMPLIFY);
9089 input_location = saved_location;
9091 return outer_bind;
9094 typedef char *char_p; /* For DEF_VEC_P. */
9096 /* Return whether we should exclude FNDECL from instrumentation. */
9098 static bool
9099 flag_instrument_functions_exclude_p (tree fndecl)
9101 vec<char_p> *v;
9103 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
9104 if (v && v->length () > 0)
9106 const char *name;
9107 int i;
9108 char *s;
9110 name = lang_hooks.decl_printable_name (fndecl, 0);
9111 FOR_EACH_VEC_ELT (*v, i, s)
9112 if (strstr (name, s) != NULL)
9113 return true;
9116 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
9117 if (v && v->length () > 0)
9119 const char *name;
9120 int i;
9121 char *s;
9123 name = DECL_SOURCE_FILE (fndecl);
9124 FOR_EACH_VEC_ELT (*v, i, s)
9125 if (strstr (name, s) != NULL)
9126 return true;
9129 return false;
9132 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
9133 node for the function we want to gimplify.
9135 Return the sequence of GIMPLE statements corresponding to the body
9136 of FNDECL. */
9138 void
9139 gimplify_function_tree (tree fndecl)
9141 tree parm, ret;
9142 gimple_seq seq;
9143 gimple bind;
9145 gcc_assert (!gimple_body (fndecl));
9147 if (DECL_STRUCT_FUNCTION (fndecl))
9148 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
9149 else
9150 push_struct_function (fndecl);
9152 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
9154 /* Preliminarily mark non-addressed complex variables as eligible
9155 for promotion to gimple registers. We'll transform their uses
9156 as we find them. */
9157 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
9158 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
9159 && !TREE_THIS_VOLATILE (parm)
9160 && !needs_to_live_in_memory (parm))
9161 DECL_GIMPLE_REG_P (parm) = 1;
9164 ret = DECL_RESULT (fndecl);
9165 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
9166 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
9167 && !needs_to_live_in_memory (ret))
9168 DECL_GIMPLE_REG_P (ret) = 1;
9170 bind = gimplify_body (fndecl, true);
9172 /* The tree body of the function is no longer needed, replace it
9173 with the new GIMPLE body. */
9174 seq = NULL;
9175 gimple_seq_add_stmt (&seq, bind);
9176 gimple_set_body (fndecl, seq);
9178 /* If we're instrumenting function entry/exit, then prepend the call to
9179 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
9180 catch the exit hook. */
9181 /* ??? Add some way to ignore exceptions for this TFE. */
9182 if (flag_instrument_function_entry_exit
9183 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
9184 && !flag_instrument_functions_exclude_p (fndecl))
9186 tree x;
9187 gimple new_bind;
9188 gimple tf;
9189 gimple_seq cleanup = NULL, body = NULL;
9190 tree tmp_var;
9191 gimple call;
9193 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9194 call = gimple_build_call (x, 1, integer_zero_node);
9195 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9196 gimple_call_set_lhs (call, tmp_var);
9197 gimplify_seq_add_stmt (&cleanup, call);
9198 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
9199 call = gimple_build_call (x, 2,
9200 build_fold_addr_expr (current_function_decl),
9201 tmp_var);
9202 gimplify_seq_add_stmt (&cleanup, call);
9203 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
9205 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9206 call = gimple_build_call (x, 1, integer_zero_node);
9207 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9208 gimple_call_set_lhs (call, tmp_var);
9209 gimplify_seq_add_stmt (&body, call);
9210 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
9211 call = gimple_build_call (x, 2,
9212 build_fold_addr_expr (current_function_decl),
9213 tmp_var);
9214 gimplify_seq_add_stmt (&body, call);
9215 gimplify_seq_add_stmt (&body, tf);
9216 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
9217 /* Clear the block for BIND, since it is no longer directly inside
9218 the function, but within a try block. */
9219 gimple_bind_set_block (bind, NULL);
9221 /* Replace the current function body with the body
9222 wrapped in the try/finally TF. */
9223 seq = NULL;
9224 gimple_seq_add_stmt (&seq, new_bind);
9225 gimple_set_body (fndecl, seq);
9228 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9229 cfun->curr_properties = PROP_gimple_any;
9231 pop_cfun ();
9234 /* Return a dummy expression of type TYPE in order to keep going after an
9235 error. */
9237 static tree
9238 dummy_object (tree type)
9240 tree t = build_int_cst (build_pointer_type (type), 0);
9241 return build2 (MEM_REF, type, t, t);
9244 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9245 builtin function, but a very special sort of operator. */
9247 enum gimplify_status
9248 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9250 tree promoted_type, have_va_type;
9251 tree valist = TREE_OPERAND (*expr_p, 0);
9252 tree type = TREE_TYPE (*expr_p);
9253 tree t;
9254 location_t loc = EXPR_LOCATION (*expr_p);
9256 /* Verify that valist is of the proper type. */
9257 have_va_type = TREE_TYPE (valist);
9258 if (have_va_type == error_mark_node)
9259 return GS_ERROR;
9260 have_va_type = targetm.canonical_va_list_type (have_va_type);
9262 if (have_va_type == NULL_TREE)
9264 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9265 return GS_ERROR;
9268 /* Generate a diagnostic for requesting data of a type that cannot
9269 be passed through `...' due to type promotion at the call site. */
9270 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9271 != type)
9273 static bool gave_help;
9274 bool warned;
9276 /* Unfortunately, this is merely undefined, rather than a constraint
9277 violation, so we cannot make this an error. If this call is never
9278 executed, the program is still strictly conforming. */
9279 warned = warning_at (loc, 0,
9280 "%qT is promoted to %qT when passed through %<...%>",
9281 type, promoted_type);
9282 if (!gave_help && warned)
9284 gave_help = true;
9285 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9286 promoted_type, type);
9289 /* We can, however, treat "undefined" any way we please.
9290 Call abort to encourage the user to fix the program. */
9291 if (warned)
9292 inform (loc, "if this code is reached, the program will abort");
9293 /* Before the abort, allow the evaluation of the va_list
9294 expression to exit or longjmp. */
9295 gimplify_and_add (valist, pre_p);
9296 t = build_call_expr_loc (loc,
9297 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9298 gimplify_and_add (t, pre_p);
9300 /* This is dead code, but go ahead and finish so that the
9301 mode of the result comes out right. */
9302 *expr_p = dummy_object (type);
9303 return GS_ALL_DONE;
9305 else
9307 /* Make it easier for the backends by protecting the valist argument
9308 from multiple evaluations. */
9309 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9311 /* For this case, the backends will be expecting a pointer to
9312 TREE_TYPE (abi), but it's possible we've
9313 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9314 So fix it. */
9315 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9317 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9318 valist = fold_convert_loc (loc, p1,
9319 build_fold_addr_expr_loc (loc, valist));
9322 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9324 else
9325 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9327 if (!targetm.gimplify_va_arg_expr)
9328 /* FIXME: Once most targets are converted we should merely
9329 assert this is non-null. */
9330 return GS_ALL_DONE;
9332 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9333 return GS_OK;
9337 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9339 DST/SRC are the destination and source respectively. You can pass
9340 ungimplified trees in DST or SRC, in which case they will be
9341 converted to a gimple operand if necessary.
9343 This function returns the newly created GIMPLE_ASSIGN tuple. */
9345 gimple
9346 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9348 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9349 gimplify_and_add (t, seq_p);
9350 ggc_free (t);
9351 return gimple_seq_last_stmt (*seq_p);
9354 inline hashval_t
9355 gimplify_hasher::hash (const value_type *p)
9357 tree t = p->val;
9358 return iterative_hash_expr (t, 0);
9361 inline bool
9362 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9364 tree t1 = p1->val;
9365 tree t2 = p2->val;
9366 enum tree_code code = TREE_CODE (t1);
9368 if (TREE_CODE (t2) != code
9369 || TREE_TYPE (t1) != TREE_TYPE (t2))
9370 return false;
9372 if (!operand_equal_p (t1, t2, 0))
9373 return false;
9375 #ifdef ENABLE_CHECKING
9376 /* Only allow them to compare equal if they also hash equal; otherwise
9377 results are nondeterminate, and we fail bootstrap comparison. */
9378 gcc_assert (hash (p1) == hash (p2));
9379 #endif
9381 return true;