Fix bootstrap failure.
[official-gcc.git] / gcc / gimplify.c
blob5aeb726f5ae1376f6c32dd9208c3670fa176cf64
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 "predict.h"
31 #include "vec.h"
32 #include "hashtab.h"
33 #include "machmode.h"
34 #include "tm.h"
35 #include "hard-reg-set.h"
36 #include "input.h"
37 #include "function.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-fold.h"
42 #include "tree-eh.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "gimplify.h"
47 #include "gimple-iterator.h"
48 #include "stringpool.h"
49 #include "calls.h"
50 #include "varasm.h"
51 #include "stor-layout.h"
52 #include "stmt.h"
53 #include "print-tree.h"
54 #include "tree-iterator.h"
55 #include "tree-inline.h"
56 #include "tree-pretty-print.h"
57 #include "langhooks.h"
58 #include "bitmap.h"
59 #include "gimple-ssa.h"
60 #include "hash-map.h"
61 #include "plugin-api.h"
62 #include "ipa-ref.h"
63 #include "cgraph.h"
64 #include "tree-cfg.h"
65 #include "tree-ssanames.h"
66 #include "tree-ssa.h"
67 #include "diagnostic-core.h"
68 #include "target.h"
69 #include "splay-tree.h"
70 #include "omp-low.h"
71 #include "gimple-low.h"
72 #include "cilk.h"
74 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
75 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
76 #include "builtins.h"
78 enum gimplify_omp_var_data
80 GOVD_SEEN = 1,
81 GOVD_EXPLICIT = 2,
82 GOVD_SHARED = 4,
83 GOVD_PRIVATE = 8,
84 GOVD_FIRSTPRIVATE = 16,
85 GOVD_LASTPRIVATE = 32,
86 GOVD_REDUCTION = 64,
87 GOVD_LOCAL = 128,
88 GOVD_MAP = 256,
89 GOVD_DEBUG_PRIVATE = 512,
90 GOVD_PRIVATE_OUTER_REF = 1024,
91 GOVD_LINEAR = 2048,
92 GOVD_ALIGNED = 4096,
94 /* Flags for GOVD_MAP. */
95 /* Don't copy back. */
96 GOVD_MAP_TO_ONLY = 8192,
97 /* Force a specific behavior (or else, a run-time error). */
98 GOVD_MAP_FORCE = 16384,
100 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
101 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
102 | GOVD_LOCAL)
106 enum omp_region_type
108 ORT_WORKSHARE = 0,
109 ORT_SIMD = 1,
110 ORT_PARALLEL = 2,
111 ORT_COMBINED_PARALLEL = 3,
112 ORT_TASK = 4,
113 ORT_UNTIED_TASK = 5,
114 ORT_TEAMS = 8,
115 ORT_TARGET = 16,
117 /* Flags for ORT_TARGET. */
118 /* Prepare this region for offloading. */
119 ORT_TARGET_OFFLOAD = 32,
120 /* Default to GOVD_MAP_FORCE for implicit mappings in this region. */
121 ORT_TARGET_MAP_FORCE = 64
124 /* Gimplify hashtable helper. */
126 struct gimplify_hasher : typed_free_remove <elt_t>
128 typedef elt_t value_type;
129 typedef elt_t compare_type;
130 static inline hashval_t hash (const value_type *);
131 static inline bool equal (const value_type *, const compare_type *);
134 struct gimplify_ctx
136 struct gimplify_ctx *prev_context;
138 vec<gimple> bind_expr_stack;
139 tree temps;
140 gimple_seq conditional_cleanups;
141 tree exit_label;
142 tree return_temp;
144 vec<tree> case_labels;
145 /* The formal temporary table. Should this be persistent? */
146 hash_table<gimplify_hasher> *temp_htab;
148 int conditions;
149 bool save_stack;
150 bool into_ssa;
151 bool allow_rhs_cond_expr;
152 bool in_cleanup_point_expr;
155 struct gimplify_omp_ctx
157 struct gimplify_omp_ctx *outer_context;
158 splay_tree variables;
159 hash_set<tree> *privatized_types;
160 location_t location;
161 enum omp_clause_default_kind default_kind;
162 enum omp_region_type region_type;
163 bool combined_loop;
164 bool distribute;
167 static struct gimplify_ctx *gimplify_ctxp;
168 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
170 /* Forward declaration. */
171 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
173 /* Shorter alias name for the above function for use in gimplify.c
174 only. */
176 static inline void
177 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
179 gimple_seq_add_stmt_without_update (seq_p, gs);
182 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
183 NULL, a new sequence is allocated. This function is
184 similar to gimple_seq_add_seq, but does not scan the operands.
185 During gimplification, we need to manipulate statement sequences
186 before the def/use vectors have been constructed. */
188 static void
189 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
191 gimple_stmt_iterator si;
193 if (src == NULL)
194 return;
196 si = gsi_last (*dst_p);
197 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
201 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
202 and popping gimplify contexts. */
204 static struct gimplify_ctx *ctx_pool = NULL;
206 /* Return a gimplify context struct from the pool. */
208 static inline struct gimplify_ctx *
209 ctx_alloc (void)
211 struct gimplify_ctx * c = ctx_pool;
213 if (c)
214 ctx_pool = c->prev_context;
215 else
216 c = XNEW (struct gimplify_ctx);
218 memset (c, '\0', sizeof (*c));
219 return c;
222 /* Put gimplify context C back into the pool. */
224 static inline void
225 ctx_free (struct gimplify_ctx *c)
227 c->prev_context = ctx_pool;
228 ctx_pool = c;
231 /* Free allocated ctx stack memory. */
233 void
234 free_gimplify_stack (void)
236 struct gimplify_ctx *c;
238 while ((c = ctx_pool))
240 ctx_pool = c->prev_context;
241 free (c);
246 /* Set up a context for the gimplifier. */
248 void
249 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
251 struct gimplify_ctx *c = ctx_alloc ();
253 c->prev_context = gimplify_ctxp;
254 gimplify_ctxp = c;
255 gimplify_ctxp->into_ssa = in_ssa;
256 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
259 /* Tear down a context for the gimplifier. If BODY is non-null, then
260 put the temporaries into the outer BIND_EXPR. Otherwise, put them
261 in the local_decls.
263 BODY is not a sequence, but the first tuple in a sequence. */
265 void
266 pop_gimplify_context (gimple body)
268 struct gimplify_ctx *c = gimplify_ctxp;
270 gcc_assert (c
271 && (!c->bind_expr_stack.exists ()
272 || c->bind_expr_stack.is_empty ()));
273 c->bind_expr_stack.release ();
274 gimplify_ctxp = c->prev_context;
276 if (body)
277 declare_vars (c->temps, body, false);
278 else
279 record_vars (c->temps);
281 delete c->temp_htab;
282 c->temp_htab = NULL;
283 ctx_free (c);
286 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
288 static void
289 gimple_push_bind_expr (gimple gimple_bind)
291 gimplify_ctxp->bind_expr_stack.reserve (8);
292 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
295 /* Pop the first element off the stack of bindings. */
297 static void
298 gimple_pop_bind_expr (void)
300 gimplify_ctxp->bind_expr_stack.pop ();
303 /* Return the first element of the stack of bindings. */
305 gimple
306 gimple_current_bind_expr (void)
308 return gimplify_ctxp->bind_expr_stack.last ();
311 /* Return the stack of bindings created during gimplification. */
313 vec<gimple>
314 gimple_bind_expr_stack (void)
316 return gimplify_ctxp->bind_expr_stack;
319 /* Return true iff there is a COND_EXPR between us and the innermost
320 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
322 static bool
323 gimple_conditional_context (void)
325 return gimplify_ctxp->conditions > 0;
328 /* Note that we've entered a COND_EXPR. */
330 static void
331 gimple_push_condition (void)
333 #ifdef ENABLE_GIMPLE_CHECKING
334 if (gimplify_ctxp->conditions == 0)
335 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
336 #endif
337 ++(gimplify_ctxp->conditions);
340 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
341 now, add any conditional cleanups we've seen to the prequeue. */
343 static void
344 gimple_pop_condition (gimple_seq *pre_p)
346 int conds = --(gimplify_ctxp->conditions);
348 gcc_assert (conds >= 0);
349 if (conds == 0)
351 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
352 gimplify_ctxp->conditional_cleanups = NULL;
356 /* A stable comparison routine for use with splay trees and DECLs. */
358 static int
359 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
361 tree a = (tree) xa;
362 tree b = (tree) xb;
364 return DECL_UID (a) - DECL_UID (b);
367 /* Create a new omp construct that deals with variable remapping. */
369 static struct gimplify_omp_ctx *
370 new_omp_context (enum omp_region_type region_type)
372 struct gimplify_omp_ctx *c;
374 c = XCNEW (struct gimplify_omp_ctx);
375 c->outer_context = gimplify_omp_ctxp;
376 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
377 c->privatized_types = new hash_set<tree>;
378 c->location = input_location;
379 c->region_type = region_type;
380 if ((region_type & ORT_TASK) == 0)
381 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
382 else
383 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
385 return c;
388 /* Destroy an omp construct that deals with variable remapping. */
390 static void
391 delete_omp_context (struct gimplify_omp_ctx *c)
393 splay_tree_delete (c->variables);
394 delete c->privatized_types;
395 XDELETE (c);
398 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
399 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
401 /* Both gimplify the statement T and append it to *SEQ_P. This function
402 behaves exactly as gimplify_stmt, but you don't have to pass T as a
403 reference. */
405 void
406 gimplify_and_add (tree t, gimple_seq *seq_p)
408 gimplify_stmt (&t, seq_p);
411 /* Gimplify statement T into sequence *SEQ_P, and return the first
412 tuple in the sequence of generated tuples for this statement.
413 Return NULL if gimplifying T produced no tuples. */
415 static gimple
416 gimplify_and_return_first (tree t, gimple_seq *seq_p)
418 gimple_stmt_iterator last = gsi_last (*seq_p);
420 gimplify_and_add (t, seq_p);
422 if (!gsi_end_p (last))
424 gsi_next (&last);
425 return gsi_stmt (last);
427 else
428 return gimple_seq_first_stmt (*seq_p);
431 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
432 LHS, or for a call argument. */
434 static bool
435 is_gimple_mem_rhs (tree t)
437 /* If we're dealing with a renamable type, either source or dest must be
438 a renamed variable. */
439 if (is_gimple_reg_type (TREE_TYPE (t)))
440 return is_gimple_val (t);
441 else
442 return is_gimple_val (t) || is_gimple_lvalue (t);
445 /* Return true if T is a CALL_EXPR or an expression that can be
446 assigned to a temporary. Note that this predicate should only be
447 used during gimplification. See the rationale for this in
448 gimplify_modify_expr. */
450 static bool
451 is_gimple_reg_rhs_or_call (tree t)
453 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
454 || TREE_CODE (t) == CALL_EXPR);
457 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
458 this predicate should only be used during gimplification. See the
459 rationale for this in gimplify_modify_expr. */
461 static bool
462 is_gimple_mem_rhs_or_call (tree t)
464 /* If we're dealing with a renamable type, either source or dest must be
465 a renamed variable. */
466 if (is_gimple_reg_type (TREE_TYPE (t)))
467 return is_gimple_val (t);
468 else
469 return (is_gimple_val (t) || is_gimple_lvalue (t)
470 || TREE_CODE (t) == CALL_EXPR);
473 /* Create a temporary with a name derived from VAL. Subroutine of
474 lookup_tmp_var; nobody else should call this function. */
476 static inline tree
477 create_tmp_from_val (tree val)
479 /* Drop all qualifiers and address-space information from the value type. */
480 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
481 tree var = create_tmp_var (type, get_name (val));
482 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
483 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
484 DECL_GIMPLE_REG_P (var) = 1;
485 return var;
488 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
489 an existing expression temporary. */
491 static tree
492 lookup_tmp_var (tree val, bool is_formal)
494 tree ret;
496 /* If not optimizing, never really reuse a temporary. local-alloc
497 won't allocate any variable that is used in more than one basic
498 block, which means it will go into memory, causing much extra
499 work in reload and final and poorer code generation, outweighing
500 the extra memory allocation here. */
501 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
502 ret = create_tmp_from_val (val);
503 else
505 elt_t elt, *elt_p;
506 elt_t **slot;
508 elt.val = val;
509 if (!gimplify_ctxp->temp_htab)
510 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
511 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
512 if (*slot == NULL)
514 elt_p = XNEW (elt_t);
515 elt_p->val = val;
516 elt_p->temp = ret = create_tmp_from_val (val);
517 *slot = elt_p;
519 else
521 elt_p = *slot;
522 ret = elt_p->temp;
526 return ret;
529 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
531 static tree
532 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
533 bool is_formal)
535 tree t, mod;
537 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
538 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
539 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
540 fb_rvalue);
542 if (gimplify_ctxp->into_ssa
543 && is_gimple_reg_type (TREE_TYPE (val)))
544 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
545 else
546 t = lookup_tmp_var (val, is_formal);
548 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
550 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
552 /* gimplify_modify_expr might want to reduce this further. */
553 gimplify_and_add (mod, pre_p);
554 ggc_free (mod);
556 return t;
559 /* Return a formal temporary variable initialized with VAL. PRE_P is as
560 in gimplify_expr. Only use this function if:
562 1) The value of the unfactored expression represented by VAL will not
563 change between the initialization and use of the temporary, and
564 2) The temporary will not be otherwise modified.
566 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
567 and #2 means it is inappropriate for && temps.
569 For other cases, use get_initialized_tmp_var instead. */
571 tree
572 get_formal_tmp_var (tree val, gimple_seq *pre_p)
574 return internal_get_tmp_var (val, pre_p, NULL, true);
577 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
578 are as in gimplify_expr. */
580 tree
581 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
583 return internal_get_tmp_var (val, pre_p, post_p, false);
586 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
587 generate debug info for them; otherwise don't. */
589 void
590 declare_vars (tree vars, gimple scope, bool debug_info)
592 tree last = vars;
593 if (last)
595 tree temps, block;
597 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
599 temps = nreverse (last);
601 block = gimple_bind_block (scope);
602 gcc_assert (!block || TREE_CODE (block) == BLOCK);
603 if (!block || !debug_info)
605 DECL_CHAIN (last) = gimple_bind_vars (scope);
606 gimple_bind_set_vars (scope, temps);
608 else
610 /* We need to attach the nodes both to the BIND_EXPR and to its
611 associated BLOCK for debugging purposes. The key point here
612 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
613 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
614 if (BLOCK_VARS (block))
615 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
616 else
618 gimple_bind_set_vars (scope,
619 chainon (gimple_bind_vars (scope), temps));
620 BLOCK_VARS (block) = temps;
626 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
627 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
628 no such upper bound can be obtained. */
630 static void
631 force_constant_size (tree var)
633 /* The only attempt we make is by querying the maximum size of objects
634 of the variable's type. */
636 HOST_WIDE_INT max_size;
638 gcc_assert (TREE_CODE (var) == VAR_DECL);
640 max_size = max_int_size_in_bytes (TREE_TYPE (var));
642 gcc_assert (max_size >= 0);
644 DECL_SIZE_UNIT (var)
645 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
646 DECL_SIZE (var)
647 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
650 /* Push the temporary variable TMP into the current binding. */
652 void
653 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
655 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
657 /* Later processing assumes that the object size is constant, which might
658 not be true at this point. Force the use of a constant upper bound in
659 this case. */
660 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
661 force_constant_size (tmp);
663 DECL_CONTEXT (tmp) = fn->decl;
664 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
666 record_vars_into (tmp, fn->decl);
669 /* Push the temporary variable TMP into the current binding. */
671 void
672 gimple_add_tmp_var (tree tmp)
674 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
676 /* Later processing assumes that the object size is constant, which might
677 not be true at this point. Force the use of a constant upper bound in
678 this case. */
679 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
680 force_constant_size (tmp);
682 DECL_CONTEXT (tmp) = current_function_decl;
683 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
685 if (gimplify_ctxp)
687 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
688 gimplify_ctxp->temps = tmp;
690 /* Mark temporaries local within the nearest enclosing parallel. */
691 if (gimplify_omp_ctxp)
693 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
694 while (ctx
695 && (ctx->region_type == ORT_WORKSHARE
696 || ctx->region_type == ORT_SIMD))
697 ctx = ctx->outer_context;
698 if (ctx)
699 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
702 else if (cfun)
703 record_vars (tmp);
704 else
706 gimple_seq body_seq;
708 /* This case is for nested functions. We need to expose the locals
709 they create. */
710 body_seq = gimple_body (current_function_decl);
711 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
717 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
718 nodes that are referenced more than once in GENERIC functions. This is
719 necessary because gimplification (translation into GIMPLE) is performed
720 by modifying tree nodes in-place, so gimplication of a shared node in a
721 first context could generate an invalid GIMPLE form in a second context.
723 This is achieved with a simple mark/copy/unmark algorithm that walks the
724 GENERIC representation top-down, marks nodes with TREE_VISITED the first
725 time it encounters them, duplicates them if they already have TREE_VISITED
726 set, and finally removes the TREE_VISITED marks it has set.
728 The algorithm works only at the function level, i.e. it generates a GENERIC
729 representation of a function with no nodes shared within the function when
730 passed a GENERIC function (except for nodes that are allowed to be shared).
732 At the global level, it is also necessary to unshare tree nodes that are
733 referenced in more than one function, for the same aforementioned reason.
734 This requires some cooperation from the front-end. There are 2 strategies:
736 1. Manual unsharing. The front-end needs to call unshare_expr on every
737 expression that might end up being shared across functions.
739 2. Deep unsharing. This is an extension of regular unsharing. Instead
740 of calling unshare_expr on expressions that might be shared across
741 functions, the front-end pre-marks them with TREE_VISITED. This will
742 ensure that they are unshared on the first reference within functions
743 when the regular unsharing algorithm runs. The counterpart is that
744 this algorithm must look deeper than for manual unsharing, which is
745 specified by LANG_HOOKS_DEEP_UNSHARING.
747 If there are only few specific cases of node sharing across functions, it is
748 probably easier for a front-end to unshare the expressions manually. On the
749 contrary, if the expressions generated at the global level are as widespread
750 as expressions generated within functions, deep unsharing is very likely the
751 way to go. */
753 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
754 These nodes model computations that must be done once. If we were to
755 unshare something like SAVE_EXPR(i++), the gimplification process would
756 create wrong code. However, if DATA is non-null, it must hold a pointer
757 set that is used to unshare the subtrees of these nodes. */
759 static tree
760 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
762 tree t = *tp;
763 enum tree_code code = TREE_CODE (t);
765 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
766 copy their subtrees if we can make sure to do it only once. */
767 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
769 if (data && !((hash_set<tree> *)data)->add (t))
771 else
772 *walk_subtrees = 0;
775 /* Stop at types, decls, constants like copy_tree_r. */
776 else if (TREE_CODE_CLASS (code) == tcc_type
777 || TREE_CODE_CLASS (code) == tcc_declaration
778 || TREE_CODE_CLASS (code) == tcc_constant
779 /* We can't do anything sensible with a BLOCK used as an
780 expression, but we also can't just die when we see it
781 because of non-expression uses. So we avert our eyes
782 and cross our fingers. Silly Java. */
783 || code == BLOCK)
784 *walk_subtrees = 0;
786 /* Cope with the statement expression extension. */
787 else if (code == STATEMENT_LIST)
790 /* Leave the bulk of the work to copy_tree_r itself. */
791 else
792 copy_tree_r (tp, walk_subtrees, NULL);
794 return NULL_TREE;
797 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
798 If *TP has been visited already, then *TP is deeply copied by calling
799 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
801 static tree
802 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
804 tree t = *tp;
805 enum tree_code code = TREE_CODE (t);
807 /* Skip types, decls, and constants. But we do want to look at their
808 types and the bounds of types. Mark them as visited so we properly
809 unmark their subtrees on the unmark pass. If we've already seen them,
810 don't look down further. */
811 if (TREE_CODE_CLASS (code) == tcc_type
812 || TREE_CODE_CLASS (code) == tcc_declaration
813 || TREE_CODE_CLASS (code) == tcc_constant)
815 if (TREE_VISITED (t))
816 *walk_subtrees = 0;
817 else
818 TREE_VISITED (t) = 1;
821 /* If this node has been visited already, unshare it and don't look
822 any deeper. */
823 else if (TREE_VISITED (t))
825 walk_tree (tp, mostly_copy_tree_r, data, NULL);
826 *walk_subtrees = 0;
829 /* Otherwise, mark the node as visited and keep looking. */
830 else
831 TREE_VISITED (t) = 1;
833 return NULL_TREE;
836 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
837 copy_if_shared_r callback unmodified. */
839 static inline void
840 copy_if_shared (tree *tp, void *data)
842 walk_tree (tp, copy_if_shared_r, data, NULL);
845 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
846 any nested functions. */
848 static void
849 unshare_body (tree fndecl)
851 struct cgraph_node *cgn = cgraph_node::get (fndecl);
852 /* If the language requires deep unsharing, we need a pointer set to make
853 sure we don't repeatedly unshare subtrees of unshareable nodes. */
854 hash_set<tree> *visited
855 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
857 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
858 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
859 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
861 delete visited;
863 if (cgn)
864 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
865 unshare_body (cgn->decl);
868 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
869 Subtrees are walked until the first unvisited node is encountered. */
871 static tree
872 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
874 tree t = *tp;
876 /* If this node has been visited, unmark it and keep looking. */
877 if (TREE_VISITED (t))
878 TREE_VISITED (t) = 0;
880 /* Otherwise, don't look any deeper. */
881 else
882 *walk_subtrees = 0;
884 return NULL_TREE;
887 /* Unmark the visited trees rooted at *TP. */
889 static inline void
890 unmark_visited (tree *tp)
892 walk_tree (tp, unmark_visited_r, NULL, NULL);
895 /* Likewise, but mark all trees as not visited. */
897 static void
898 unvisit_body (tree fndecl)
900 struct cgraph_node *cgn = cgraph_node::get (fndecl);
902 unmark_visited (&DECL_SAVED_TREE (fndecl));
903 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
904 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
906 if (cgn)
907 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
908 unvisit_body (cgn->decl);
911 /* Unconditionally make an unshared copy of EXPR. This is used when using
912 stored expressions which span multiple functions, such as BINFO_VTABLE,
913 as the normal unsharing process can't tell that they're shared. */
915 tree
916 unshare_expr (tree expr)
918 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
919 return expr;
922 /* Worker for unshare_expr_without_location. */
924 static tree
925 prune_expr_location (tree *tp, int *walk_subtrees, void *)
927 if (EXPR_P (*tp))
928 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
929 else
930 *walk_subtrees = 0;
931 return NULL_TREE;
934 /* Similar to unshare_expr but also prune all expression locations
935 from EXPR. */
937 tree
938 unshare_expr_without_location (tree expr)
940 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
941 if (EXPR_P (expr))
942 walk_tree (&expr, prune_expr_location, NULL, NULL);
943 return expr;
946 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
947 contain statements and have a value. Assign its value to a temporary
948 and give it void_type_node. Return the temporary, or NULL_TREE if
949 WRAPPER was already void. */
951 tree
952 voidify_wrapper_expr (tree wrapper, tree temp)
954 tree type = TREE_TYPE (wrapper);
955 if (type && !VOID_TYPE_P (type))
957 tree *p;
959 /* Set p to point to the body of the wrapper. Loop until we find
960 something that isn't a wrapper. */
961 for (p = &wrapper; p && *p; )
963 switch (TREE_CODE (*p))
965 case BIND_EXPR:
966 TREE_SIDE_EFFECTS (*p) = 1;
967 TREE_TYPE (*p) = void_type_node;
968 /* For a BIND_EXPR, the body is operand 1. */
969 p = &BIND_EXPR_BODY (*p);
970 break;
972 case CLEANUP_POINT_EXPR:
973 case TRY_FINALLY_EXPR:
974 case TRY_CATCH_EXPR:
975 TREE_SIDE_EFFECTS (*p) = 1;
976 TREE_TYPE (*p) = void_type_node;
977 p = &TREE_OPERAND (*p, 0);
978 break;
980 case STATEMENT_LIST:
982 tree_stmt_iterator i = tsi_last (*p);
983 TREE_SIDE_EFFECTS (*p) = 1;
984 TREE_TYPE (*p) = void_type_node;
985 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
987 break;
989 case COMPOUND_EXPR:
990 /* Advance to the last statement. Set all container types to
991 void. */
992 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
994 TREE_SIDE_EFFECTS (*p) = 1;
995 TREE_TYPE (*p) = void_type_node;
997 break;
999 case TRANSACTION_EXPR:
1000 TREE_SIDE_EFFECTS (*p) = 1;
1001 TREE_TYPE (*p) = void_type_node;
1002 p = &TRANSACTION_EXPR_BODY (*p);
1003 break;
1005 default:
1006 /* Assume that any tree upon which voidify_wrapper_expr is
1007 directly called is a wrapper, and that its body is op0. */
1008 if (p == &wrapper)
1010 TREE_SIDE_EFFECTS (*p) = 1;
1011 TREE_TYPE (*p) = void_type_node;
1012 p = &TREE_OPERAND (*p, 0);
1013 break;
1015 goto out;
1019 out:
1020 if (p == NULL || IS_EMPTY_STMT (*p))
1021 temp = NULL_TREE;
1022 else if (temp)
1024 /* The wrapper is on the RHS of an assignment that we're pushing
1025 down. */
1026 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1027 || TREE_CODE (temp) == MODIFY_EXPR);
1028 TREE_OPERAND (temp, 1) = *p;
1029 *p = temp;
1031 else
1033 temp = create_tmp_var (type, "retval");
1034 *p = build2 (INIT_EXPR, type, temp, *p);
1037 return temp;
1040 return NULL_TREE;
1043 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1044 a temporary through which they communicate. */
1046 static void
1047 build_stack_save_restore (gimple *save, gimple *restore)
1049 tree tmp_var;
1051 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1052 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1053 gimple_call_set_lhs (*save, tmp_var);
1055 *restore
1056 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1057 1, tmp_var);
1060 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1062 static enum gimplify_status
1063 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1065 tree bind_expr = *expr_p;
1066 bool old_save_stack = gimplify_ctxp->save_stack;
1067 tree t;
1068 gimple gimple_bind;
1069 gimple_seq body, cleanup;
1070 gimple stack_save;
1071 location_t start_locus = 0, end_locus = 0;
1073 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1075 /* Mark variables seen in this bind expr. */
1076 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1078 if (TREE_CODE (t) == VAR_DECL)
1080 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1082 /* Mark variable as local. */
1083 if (ctx && !DECL_EXTERNAL (t)
1084 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1085 || splay_tree_lookup (ctx->variables,
1086 (splay_tree_key) t) == NULL))
1088 if (ctx->region_type == ORT_SIMD
1089 && TREE_ADDRESSABLE (t)
1090 && !TREE_STATIC (t))
1091 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1092 else
1093 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1096 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1098 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1099 cfun->has_local_explicit_reg_vars = true;
1102 /* Preliminarily mark non-addressed complex variables as eligible
1103 for promotion to gimple registers. We'll transform their uses
1104 as we find them. */
1105 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1106 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1107 && !TREE_THIS_VOLATILE (t)
1108 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1109 && !needs_to_live_in_memory (t))
1110 DECL_GIMPLE_REG_P (t) = 1;
1113 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1114 BIND_EXPR_BLOCK (bind_expr));
1115 gimple_push_bind_expr (gimple_bind);
1117 gimplify_ctxp->save_stack = false;
1119 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1120 body = NULL;
1121 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1122 gimple_bind_set_body (gimple_bind, body);
1124 /* Source location wise, the cleanup code (stack_restore and clobbers)
1125 belongs to the end of the block, so propagate what we have. The
1126 stack_save operation belongs to the beginning of block, which we can
1127 infer from the bind_expr directly if the block has no explicit
1128 assignment. */
1129 if (BIND_EXPR_BLOCK (bind_expr))
1131 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1132 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1134 if (start_locus == 0)
1135 start_locus = EXPR_LOCATION (bind_expr);
1137 cleanup = NULL;
1138 stack_save = NULL;
1139 if (gimplify_ctxp->save_stack)
1141 gimple stack_restore;
1143 /* Save stack on entry and restore it on exit. Add a try_finally
1144 block to achieve this. */
1145 build_stack_save_restore (&stack_save, &stack_restore);
1147 gimple_set_location (stack_save, start_locus);
1148 gimple_set_location (stack_restore, end_locus);
1150 gimplify_seq_add_stmt (&cleanup, stack_restore);
1153 /* Add clobbers for all variables that go out of scope. */
1154 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1156 if (TREE_CODE (t) == VAR_DECL
1157 && !is_global_var (t)
1158 && DECL_CONTEXT (t) == current_function_decl
1159 && !DECL_HARD_REGISTER (t)
1160 && !TREE_THIS_VOLATILE (t)
1161 && !DECL_HAS_VALUE_EXPR_P (t)
1162 /* Only care for variables that have to be in memory. Others
1163 will be rewritten into SSA names, hence moved to the top-level. */
1164 && !is_gimple_reg (t)
1165 && flag_stack_reuse != SR_NONE)
1167 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1168 gimple clobber_stmt;
1169 TREE_THIS_VOLATILE (clobber) = 1;
1170 clobber_stmt = gimple_build_assign (t, clobber);
1171 gimple_set_location (clobber_stmt, end_locus);
1172 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1176 if (cleanup)
1178 gimple gs;
1179 gimple_seq new_body;
1181 new_body = NULL;
1182 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1183 GIMPLE_TRY_FINALLY);
1185 if (stack_save)
1186 gimplify_seq_add_stmt (&new_body, stack_save);
1187 gimplify_seq_add_stmt (&new_body, gs);
1188 gimple_bind_set_body (gimple_bind, new_body);
1191 gimplify_ctxp->save_stack = old_save_stack;
1192 gimple_pop_bind_expr ();
1194 gimplify_seq_add_stmt (pre_p, gimple_bind);
1196 if (temp)
1198 *expr_p = temp;
1199 return GS_OK;
1202 *expr_p = NULL_TREE;
1203 return GS_ALL_DONE;
1206 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1207 GIMPLE value, it is assigned to a new temporary and the statement is
1208 re-written to return the temporary.
1210 PRE_P points to the sequence where side effects that must happen before
1211 STMT should be stored. */
1213 static enum gimplify_status
1214 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1216 gimple ret;
1217 tree ret_expr = TREE_OPERAND (stmt, 0);
1218 tree result_decl, result;
1220 if (ret_expr == error_mark_node)
1221 return GS_ERROR;
1223 /* Implicit _Cilk_sync must be inserted right before any return statement
1224 if there is a _Cilk_spawn in the function. If the user has provided a
1225 _Cilk_sync, the optimizer should remove this duplicate one. */
1226 if (fn_contains_cilk_spawn_p (cfun))
1228 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1229 gimplify_and_add (impl_sync, pre_p);
1232 if (!ret_expr
1233 || TREE_CODE (ret_expr) == RESULT_DECL
1234 || ret_expr == error_mark_node)
1236 gimple ret = gimple_build_return (ret_expr);
1237 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1238 gimplify_seq_add_stmt (pre_p, ret);
1239 return GS_ALL_DONE;
1242 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1243 result_decl = NULL_TREE;
1244 else
1246 result_decl = TREE_OPERAND (ret_expr, 0);
1248 /* See through a return by reference. */
1249 if (TREE_CODE (result_decl) == INDIRECT_REF)
1250 result_decl = TREE_OPERAND (result_decl, 0);
1252 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1253 || TREE_CODE (ret_expr) == INIT_EXPR)
1254 && TREE_CODE (result_decl) == RESULT_DECL);
1257 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1258 Recall that aggregate_value_p is FALSE for any aggregate type that is
1259 returned in registers. If we're returning values in registers, then
1260 we don't want to extend the lifetime of the RESULT_DECL, particularly
1261 across another call. In addition, for those aggregates for which
1262 hard_function_value generates a PARALLEL, we'll die during normal
1263 expansion of structure assignments; there's special code in expand_return
1264 to handle this case that does not exist in expand_expr. */
1265 if (!result_decl)
1266 result = NULL_TREE;
1267 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1269 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1271 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1272 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1273 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1274 should be effectively allocated by the caller, i.e. all calls to
1275 this function must be subject to the Return Slot Optimization. */
1276 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1277 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1279 result = result_decl;
1281 else if (gimplify_ctxp->return_temp)
1282 result = gimplify_ctxp->return_temp;
1283 else
1285 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1287 /* ??? With complex control flow (usually involving abnormal edges),
1288 we can wind up warning about an uninitialized value for this. Due
1289 to how this variable is constructed and initialized, this is never
1290 true. Give up and never warn. */
1291 TREE_NO_WARNING (result) = 1;
1293 gimplify_ctxp->return_temp = result;
1296 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1297 Then gimplify the whole thing. */
1298 if (result != result_decl)
1299 TREE_OPERAND (ret_expr, 0) = result;
1301 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1303 ret = gimple_build_return (result);
1304 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1305 gimplify_seq_add_stmt (pre_p, ret);
1307 return GS_ALL_DONE;
1310 /* Gimplify a variable-length array DECL. */
1312 static void
1313 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1315 /* This is a variable-sized decl. Simplify its size and mark it
1316 for deferred expansion. */
1317 tree t, addr, ptr_type;
1319 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1320 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1322 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1323 if (DECL_HAS_VALUE_EXPR_P (decl))
1324 return;
1326 /* All occurrences of this decl in final gimplified code will be
1327 replaced by indirection. Setting DECL_VALUE_EXPR does two
1328 things: First, it lets the rest of the gimplifier know what
1329 replacement to use. Second, it lets the debug info know
1330 where to find the value. */
1331 ptr_type = build_pointer_type (TREE_TYPE (decl));
1332 addr = create_tmp_var (ptr_type, get_name (decl));
1333 DECL_IGNORED_P (addr) = 0;
1334 t = build_fold_indirect_ref (addr);
1335 TREE_THIS_NOTRAP (t) = 1;
1336 SET_DECL_VALUE_EXPR (decl, t);
1337 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1339 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1340 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1341 size_int (DECL_ALIGN (decl)));
1342 /* The call has been built for a variable-sized object. */
1343 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1344 t = fold_convert (ptr_type, t);
1345 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1347 gimplify_and_add (t, seq_p);
1349 /* Indicate that we need to restore the stack level when the
1350 enclosing BIND_EXPR is exited. */
1351 gimplify_ctxp->save_stack = true;
1354 /* A helper function to be called via walk_tree. Mark all labels under *TP
1355 as being forced. To be called for DECL_INITIAL of static variables. */
1357 static tree
1358 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1360 if (TYPE_P (*tp))
1361 *walk_subtrees = 0;
1362 if (TREE_CODE (*tp) == LABEL_DECL)
1363 FORCED_LABEL (*tp) = 1;
1365 return NULL_TREE;
1368 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1369 and initialization explicit. */
1371 static enum gimplify_status
1372 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1374 tree stmt = *stmt_p;
1375 tree decl = DECL_EXPR_DECL (stmt);
1377 *stmt_p = NULL_TREE;
1379 if (TREE_TYPE (decl) == error_mark_node)
1380 return GS_ERROR;
1382 if ((TREE_CODE (decl) == TYPE_DECL
1383 || TREE_CODE (decl) == VAR_DECL)
1384 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1385 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1387 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1388 in case its size expressions contain problematic nodes like CALL_EXPR. */
1389 if (TREE_CODE (decl) == TYPE_DECL
1390 && DECL_ORIGINAL_TYPE (decl)
1391 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1392 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1394 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1396 tree init = DECL_INITIAL (decl);
1398 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1399 || (!TREE_STATIC (decl)
1400 && flag_stack_check == GENERIC_STACK_CHECK
1401 && compare_tree_int (DECL_SIZE_UNIT (decl),
1402 STACK_CHECK_MAX_VAR_SIZE) > 0))
1403 gimplify_vla_decl (decl, seq_p);
1405 /* Some front ends do not explicitly declare all anonymous
1406 artificial variables. We compensate here by declaring the
1407 variables, though it would be better if the front ends would
1408 explicitly declare them. */
1409 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1410 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1411 gimple_add_tmp_var (decl);
1413 if (init && init != error_mark_node)
1415 if (!TREE_STATIC (decl))
1417 DECL_INITIAL (decl) = NULL_TREE;
1418 init = build2 (INIT_EXPR, void_type_node, decl, init);
1419 gimplify_and_add (init, seq_p);
1420 ggc_free (init);
1422 else
1423 /* We must still examine initializers for static variables
1424 as they may contain a label address. */
1425 walk_tree (&init, force_labels_r, NULL, NULL);
1429 return GS_ALL_DONE;
1432 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1433 and replacing the LOOP_EXPR with goto, but if the loop contains an
1434 EXIT_EXPR, we need to append a label for it to jump to. */
1436 static enum gimplify_status
1437 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1439 tree saved_label = gimplify_ctxp->exit_label;
1440 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1442 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1444 gimplify_ctxp->exit_label = NULL_TREE;
1446 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1448 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1450 if (gimplify_ctxp->exit_label)
1451 gimplify_seq_add_stmt (pre_p,
1452 gimple_build_label (gimplify_ctxp->exit_label));
1454 gimplify_ctxp->exit_label = saved_label;
1456 *expr_p = NULL;
1457 return GS_ALL_DONE;
1460 /* Gimplify a statement list onto a sequence. These may be created either
1461 by an enlightened front-end, or by shortcut_cond_expr. */
1463 static enum gimplify_status
1464 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1466 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1468 tree_stmt_iterator i = tsi_start (*expr_p);
1470 while (!tsi_end_p (i))
1472 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1473 tsi_delink (&i);
1476 if (temp)
1478 *expr_p = temp;
1479 return GS_OK;
1482 return GS_ALL_DONE;
1486 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1487 branch to. */
1489 static enum gimplify_status
1490 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1492 tree switch_expr = *expr_p;
1493 gimple_seq switch_body_seq = NULL;
1494 enum gimplify_status ret;
1495 tree index_type = TREE_TYPE (switch_expr);
1496 if (index_type == NULL_TREE)
1497 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1499 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1500 fb_rvalue);
1501 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1502 return ret;
1504 if (SWITCH_BODY (switch_expr))
1506 vec<tree> labels;
1507 vec<tree> saved_labels;
1508 tree default_case = NULL_TREE;
1509 gimple gimple_switch;
1511 /* If someone can be bothered to fill in the labels, they can
1512 be bothered to null out the body too. */
1513 gcc_assert (!SWITCH_LABELS (switch_expr));
1515 /* Save old labels, get new ones from body, then restore the old
1516 labels. Save all the things from the switch body to append after. */
1517 saved_labels = gimplify_ctxp->case_labels;
1518 gimplify_ctxp->case_labels.create (8);
1520 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1521 labels = gimplify_ctxp->case_labels;
1522 gimplify_ctxp->case_labels = saved_labels;
1524 preprocess_case_label_vec_for_gimple (labels, index_type,
1525 &default_case);
1527 if (!default_case)
1529 gimple new_default;
1531 default_case
1532 = build_case_label (NULL_TREE, NULL_TREE,
1533 create_artificial_label (UNKNOWN_LOCATION));
1534 new_default = gimple_build_label (CASE_LABEL (default_case));
1535 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1538 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1539 default_case, labels);
1540 gimplify_seq_add_stmt (pre_p, gimple_switch);
1541 gimplify_seq_add_seq (pre_p, switch_body_seq);
1542 labels.release ();
1544 else
1545 gcc_assert (SWITCH_LABELS (switch_expr));
1547 return GS_ALL_DONE;
1550 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1552 static enum gimplify_status
1553 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1555 struct gimplify_ctx *ctxp;
1556 gimple gimple_label;
1558 /* Invalid programs can play Duff's Device type games with, for example,
1559 #pragma omp parallel. At least in the C front end, we don't
1560 detect such invalid branches until after gimplification, in the
1561 diagnose_omp_blocks pass. */
1562 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1563 if (ctxp->case_labels.exists ())
1564 break;
1566 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1567 ctxp->case_labels.safe_push (*expr_p);
1568 gimplify_seq_add_stmt (pre_p, gimple_label);
1570 return GS_ALL_DONE;
1573 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1574 if necessary. */
1576 tree
1577 build_and_jump (tree *label_p)
1579 if (label_p == NULL)
1580 /* If there's nowhere to jump, just fall through. */
1581 return NULL_TREE;
1583 if (*label_p == NULL_TREE)
1585 tree label = create_artificial_label (UNKNOWN_LOCATION);
1586 *label_p = label;
1589 return build1 (GOTO_EXPR, void_type_node, *label_p);
1592 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1593 This also involves building a label to jump to and communicating it to
1594 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1596 static enum gimplify_status
1597 gimplify_exit_expr (tree *expr_p)
1599 tree cond = TREE_OPERAND (*expr_p, 0);
1600 tree expr;
1602 expr = build_and_jump (&gimplify_ctxp->exit_label);
1603 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1604 *expr_p = expr;
1606 return GS_OK;
1609 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1610 different from its canonical type, wrap the whole thing inside a
1611 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1612 type.
1614 The canonical type of a COMPONENT_REF is the type of the field being
1615 referenced--unless the field is a bit-field which can be read directly
1616 in a smaller mode, in which case the canonical type is the
1617 sign-appropriate type corresponding to that mode. */
1619 static void
1620 canonicalize_component_ref (tree *expr_p)
1622 tree expr = *expr_p;
1623 tree type;
1625 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1627 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1628 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1629 else
1630 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1632 /* One could argue that all the stuff below is not necessary for
1633 the non-bitfield case and declare it a FE error if type
1634 adjustment would be needed. */
1635 if (TREE_TYPE (expr) != type)
1637 #ifdef ENABLE_TYPES_CHECKING
1638 tree old_type = TREE_TYPE (expr);
1639 #endif
1640 int type_quals;
1642 /* We need to preserve qualifiers and propagate them from
1643 operand 0. */
1644 type_quals = TYPE_QUALS (type)
1645 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1646 if (TYPE_QUALS (type) != type_quals)
1647 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1649 /* Set the type of the COMPONENT_REF to the underlying type. */
1650 TREE_TYPE (expr) = type;
1652 #ifdef ENABLE_TYPES_CHECKING
1653 /* It is now a FE error, if the conversion from the canonical
1654 type to the original expression type is not useless. */
1655 gcc_assert (useless_type_conversion_p (old_type, type));
1656 #endif
1660 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1661 to foo, embed that change in the ADDR_EXPR by converting
1662 T array[U];
1663 (T *)&array
1665 &array[L]
1666 where L is the lower bound. For simplicity, only do this for constant
1667 lower bound.
1668 The constraint is that the type of &array[L] is trivially convertible
1669 to T *. */
1671 static void
1672 canonicalize_addr_expr (tree *expr_p)
1674 tree expr = *expr_p;
1675 tree addr_expr = TREE_OPERAND (expr, 0);
1676 tree datype, ddatype, pddatype;
1678 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1679 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1680 || TREE_CODE (addr_expr) != ADDR_EXPR)
1681 return;
1683 /* The addr_expr type should be a pointer to an array. */
1684 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1685 if (TREE_CODE (datype) != ARRAY_TYPE)
1686 return;
1688 /* The pointer to element type shall be trivially convertible to
1689 the expression pointer type. */
1690 ddatype = TREE_TYPE (datype);
1691 pddatype = build_pointer_type (ddatype);
1692 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1693 pddatype))
1694 return;
1696 /* The lower bound and element sizes must be constant. */
1697 if (!TYPE_SIZE_UNIT (ddatype)
1698 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1699 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1700 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1701 return;
1703 /* All checks succeeded. Build a new node to merge the cast. */
1704 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1705 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1706 NULL_TREE, NULL_TREE);
1707 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1709 /* We can have stripped a required restrict qualifier above. */
1710 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1711 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1714 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1715 underneath as appropriate. */
1717 static enum gimplify_status
1718 gimplify_conversion (tree *expr_p)
1720 location_t loc = EXPR_LOCATION (*expr_p);
1721 gcc_assert (CONVERT_EXPR_P (*expr_p));
1723 /* Then strip away all but the outermost conversion. */
1724 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1726 /* And remove the outermost conversion if it's useless. */
1727 if (tree_ssa_useless_type_conversion (*expr_p))
1728 *expr_p = TREE_OPERAND (*expr_p, 0);
1730 /* If we still have a conversion at the toplevel,
1731 then canonicalize some constructs. */
1732 if (CONVERT_EXPR_P (*expr_p))
1734 tree sub = TREE_OPERAND (*expr_p, 0);
1736 /* If a NOP conversion is changing the type of a COMPONENT_REF
1737 expression, then canonicalize its type now in order to expose more
1738 redundant conversions. */
1739 if (TREE_CODE (sub) == COMPONENT_REF)
1740 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1742 /* If a NOP conversion is changing a pointer to array of foo
1743 to a pointer to foo, embed that change in the ADDR_EXPR. */
1744 else if (TREE_CODE (sub) == ADDR_EXPR)
1745 canonicalize_addr_expr (expr_p);
1748 /* If we have a conversion to a non-register type force the
1749 use of a VIEW_CONVERT_EXPR instead. */
1750 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1751 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1752 TREE_OPERAND (*expr_p, 0));
1754 return GS_OK;
1757 /* Nonlocal VLAs seen in the current function. */
1758 static hash_set<tree> *nonlocal_vlas;
1760 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1761 static tree nonlocal_vla_vars;
1763 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1764 DECL_VALUE_EXPR, and it's worth re-examining things. */
1766 static enum gimplify_status
1767 gimplify_var_or_parm_decl (tree *expr_p)
1769 tree decl = *expr_p;
1771 /* ??? If this is a local variable, and it has not been seen in any
1772 outer BIND_EXPR, then it's probably the result of a duplicate
1773 declaration, for which we've already issued an error. It would
1774 be really nice if the front end wouldn't leak these at all.
1775 Currently the only known culprit is C++ destructors, as seen
1776 in g++.old-deja/g++.jason/binding.C. */
1777 if (TREE_CODE (decl) == VAR_DECL
1778 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1779 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1780 && decl_function_context (decl) == current_function_decl)
1782 gcc_assert (seen_error ());
1783 return GS_ERROR;
1786 /* When within an OpenMP context, notice uses of variables. */
1787 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1788 return GS_ALL_DONE;
1790 /* If the decl is an alias for another expression, substitute it now. */
1791 if (DECL_HAS_VALUE_EXPR_P (decl))
1793 tree value_expr = DECL_VALUE_EXPR (decl);
1795 /* For referenced nonlocal VLAs add a decl for debugging purposes
1796 to the current function. */
1797 if (TREE_CODE (decl) == VAR_DECL
1798 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1799 && nonlocal_vlas != NULL
1800 && TREE_CODE (value_expr) == INDIRECT_REF
1801 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1802 && decl_function_context (decl) != current_function_decl)
1804 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1805 while (ctx
1806 && (ctx->region_type == ORT_WORKSHARE
1807 || ctx->region_type == ORT_SIMD))
1808 ctx = ctx->outer_context;
1809 if (!ctx && !nonlocal_vlas->add (decl))
1811 tree copy = copy_node (decl);
1813 lang_hooks.dup_lang_specific_decl (copy);
1814 SET_DECL_RTL (copy, 0);
1815 TREE_USED (copy) = 1;
1816 DECL_CHAIN (copy) = nonlocal_vla_vars;
1817 nonlocal_vla_vars = copy;
1818 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1819 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1823 *expr_p = unshare_expr (value_expr);
1824 return GS_OK;
1827 return GS_ALL_DONE;
1830 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1832 static void
1833 recalculate_side_effects (tree t)
1835 enum tree_code code = TREE_CODE (t);
1836 int len = TREE_OPERAND_LENGTH (t);
1837 int i;
1839 switch (TREE_CODE_CLASS (code))
1841 case tcc_expression:
1842 switch (code)
1844 case INIT_EXPR:
1845 case MODIFY_EXPR:
1846 case VA_ARG_EXPR:
1847 case PREDECREMENT_EXPR:
1848 case PREINCREMENT_EXPR:
1849 case POSTDECREMENT_EXPR:
1850 case POSTINCREMENT_EXPR:
1851 /* All of these have side-effects, no matter what their
1852 operands are. */
1853 return;
1855 default:
1856 break;
1858 /* Fall through. */
1860 case tcc_comparison: /* a comparison expression */
1861 case tcc_unary: /* a unary arithmetic expression */
1862 case tcc_binary: /* a binary arithmetic expression */
1863 case tcc_reference: /* a reference */
1864 case tcc_vl_exp: /* a function call */
1865 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1866 for (i = 0; i < len; ++i)
1868 tree op = TREE_OPERAND (t, i);
1869 if (op && TREE_SIDE_EFFECTS (op))
1870 TREE_SIDE_EFFECTS (t) = 1;
1872 break;
1874 case tcc_constant:
1875 /* No side-effects. */
1876 return;
1878 default:
1879 gcc_unreachable ();
1883 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1884 node *EXPR_P.
1886 compound_lval
1887 : min_lval '[' val ']'
1888 | min_lval '.' ID
1889 | compound_lval '[' val ']'
1890 | compound_lval '.' ID
1892 This is not part of the original SIMPLE definition, which separates
1893 array and member references, but it seems reasonable to handle them
1894 together. Also, this way we don't run into problems with union
1895 aliasing; gcc requires that for accesses through a union to alias, the
1896 union reference must be explicit, which was not always the case when we
1897 were splitting up array and member refs.
1899 PRE_P points to the sequence where side effects that must happen before
1900 *EXPR_P should be stored.
1902 POST_P points to the sequence where side effects that must happen after
1903 *EXPR_P should be stored. */
1905 static enum gimplify_status
1906 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1907 fallback_t fallback)
1909 tree *p;
1910 enum gimplify_status ret = GS_ALL_DONE, tret;
1911 int i;
1912 location_t loc = EXPR_LOCATION (*expr_p);
1913 tree expr = *expr_p;
1915 /* Create a stack of the subexpressions so later we can walk them in
1916 order from inner to outer. */
1917 auto_vec<tree, 10> expr_stack;
1919 /* We can handle anything that get_inner_reference can deal with. */
1920 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1922 restart:
1923 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1924 if (TREE_CODE (*p) == INDIRECT_REF)
1925 *p = fold_indirect_ref_loc (loc, *p);
1927 if (handled_component_p (*p))
1929 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1930 additional COMPONENT_REFs. */
1931 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1932 && gimplify_var_or_parm_decl (p) == GS_OK)
1933 goto restart;
1934 else
1935 break;
1937 expr_stack.safe_push (*p);
1940 gcc_assert (expr_stack.length ());
1942 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1943 walked through and P points to the innermost expression.
1945 Java requires that we elaborated nodes in source order. That
1946 means we must gimplify the inner expression followed by each of
1947 the indices, in order. But we can't gimplify the inner
1948 expression until we deal with any variable bounds, sizes, or
1949 positions in order to deal with PLACEHOLDER_EXPRs.
1951 So we do this in three steps. First we deal with the annotations
1952 for any variables in the components, then we gimplify the base,
1953 then we gimplify any indices, from left to right. */
1954 for (i = expr_stack.length () - 1; i >= 0; i--)
1956 tree t = expr_stack[i];
1958 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1960 /* Gimplify the low bound and element type size and put them into
1961 the ARRAY_REF. If these values are set, they have already been
1962 gimplified. */
1963 if (TREE_OPERAND (t, 2) == NULL_TREE)
1965 tree low = unshare_expr (array_ref_low_bound (t));
1966 if (!is_gimple_min_invariant (low))
1968 TREE_OPERAND (t, 2) = low;
1969 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1970 post_p, is_gimple_reg,
1971 fb_rvalue);
1972 ret = MIN (ret, tret);
1975 else
1977 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1978 is_gimple_reg, fb_rvalue);
1979 ret = MIN (ret, tret);
1982 if (TREE_OPERAND (t, 3) == NULL_TREE)
1984 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1985 tree elmt_size = unshare_expr (array_ref_element_size (t));
1986 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1988 /* Divide the element size by the alignment of the element
1989 type (above). */
1990 elmt_size
1991 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1993 if (!is_gimple_min_invariant (elmt_size))
1995 TREE_OPERAND (t, 3) = elmt_size;
1996 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1997 post_p, is_gimple_reg,
1998 fb_rvalue);
1999 ret = MIN (ret, tret);
2002 else
2004 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2005 is_gimple_reg, fb_rvalue);
2006 ret = MIN (ret, tret);
2009 else if (TREE_CODE (t) == COMPONENT_REF)
2011 /* Set the field offset into T and gimplify it. */
2012 if (TREE_OPERAND (t, 2) == NULL_TREE)
2014 tree offset = unshare_expr (component_ref_field_offset (t));
2015 tree field = TREE_OPERAND (t, 1);
2016 tree factor
2017 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2019 /* Divide the offset by its alignment. */
2020 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2022 if (!is_gimple_min_invariant (offset))
2024 TREE_OPERAND (t, 2) = offset;
2025 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2026 post_p, is_gimple_reg,
2027 fb_rvalue);
2028 ret = MIN (ret, tret);
2031 else
2033 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2034 is_gimple_reg, fb_rvalue);
2035 ret = MIN (ret, tret);
2040 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2041 so as to match the min_lval predicate. Failure to do so may result
2042 in the creation of large aggregate temporaries. */
2043 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2044 fallback | fb_lvalue);
2045 ret = MIN (ret, tret);
2047 /* And finally, the indices and operands of ARRAY_REF. During this
2048 loop we also remove any useless conversions. */
2049 for (; expr_stack.length () > 0; )
2051 tree t = expr_stack.pop ();
2053 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2055 /* Gimplify the dimension. */
2056 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2058 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2059 is_gimple_val, fb_rvalue);
2060 ret = MIN (ret, tret);
2064 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2066 /* The innermost expression P may have originally had
2067 TREE_SIDE_EFFECTS set which would have caused all the outer
2068 expressions in *EXPR_P leading to P to also have had
2069 TREE_SIDE_EFFECTS set. */
2070 recalculate_side_effects (t);
2073 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2074 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2076 canonicalize_component_ref (expr_p);
2079 expr_stack.release ();
2081 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2083 return ret;
2086 /* Gimplify the self modifying expression pointed to by EXPR_P
2087 (++, --, +=, -=).
2089 PRE_P points to the list where side effects that must happen before
2090 *EXPR_P should be stored.
2092 POST_P points to the list where side effects that must happen after
2093 *EXPR_P should be stored.
2095 WANT_VALUE is nonzero iff we want to use the value of this expression
2096 in another expression.
2098 ARITH_TYPE is the type the computation should be performed in. */
2100 enum gimplify_status
2101 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2102 bool want_value, tree arith_type)
2104 enum tree_code code;
2105 tree lhs, lvalue, rhs, t1;
2106 gimple_seq post = NULL, *orig_post_p = post_p;
2107 bool postfix;
2108 enum tree_code arith_code;
2109 enum gimplify_status ret;
2110 location_t loc = EXPR_LOCATION (*expr_p);
2112 code = TREE_CODE (*expr_p);
2114 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2115 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2117 /* Prefix or postfix? */
2118 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2119 /* Faster to treat as prefix if result is not used. */
2120 postfix = want_value;
2121 else
2122 postfix = false;
2124 /* For postfix, make sure the inner expression's post side effects
2125 are executed after side effects from this expression. */
2126 if (postfix)
2127 post_p = &post;
2129 /* Add or subtract? */
2130 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2131 arith_code = PLUS_EXPR;
2132 else
2133 arith_code = MINUS_EXPR;
2135 /* Gimplify the LHS into a GIMPLE lvalue. */
2136 lvalue = TREE_OPERAND (*expr_p, 0);
2137 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2138 if (ret == GS_ERROR)
2139 return ret;
2141 /* Extract the operands to the arithmetic operation. */
2142 lhs = lvalue;
2143 rhs = TREE_OPERAND (*expr_p, 1);
2145 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2146 that as the result value and in the postqueue operation. */
2147 if (postfix)
2149 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2150 if (ret == GS_ERROR)
2151 return ret;
2153 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2156 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2157 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2159 rhs = convert_to_ptrofftype_loc (loc, rhs);
2160 if (arith_code == MINUS_EXPR)
2161 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2162 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2164 else
2165 t1 = fold_convert (TREE_TYPE (*expr_p),
2166 fold_build2 (arith_code, arith_type,
2167 fold_convert (arith_type, lhs),
2168 fold_convert (arith_type, rhs)));
2170 if (postfix)
2172 gimplify_assign (lvalue, t1, pre_p);
2173 gimplify_seq_add_seq (orig_post_p, post);
2174 *expr_p = lhs;
2175 return GS_ALL_DONE;
2177 else
2179 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2180 return GS_OK;
2184 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2186 static void
2187 maybe_with_size_expr (tree *expr_p)
2189 tree expr = *expr_p;
2190 tree type = TREE_TYPE (expr);
2191 tree size;
2193 /* If we've already wrapped this or the type is error_mark_node, we can't do
2194 anything. */
2195 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2196 || type == error_mark_node)
2197 return;
2199 /* If the size isn't known or is a constant, we have nothing to do. */
2200 size = TYPE_SIZE_UNIT (type);
2201 if (!size || TREE_CODE (size) == INTEGER_CST)
2202 return;
2204 /* Otherwise, make a WITH_SIZE_EXPR. */
2205 size = unshare_expr (size);
2206 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2207 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2210 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2211 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2212 the CALL_EXPR. */
2214 enum gimplify_status
2215 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2217 bool (*test) (tree);
2218 fallback_t fb;
2220 /* In general, we allow lvalues for function arguments to avoid
2221 extra overhead of copying large aggregates out of even larger
2222 aggregates into temporaries only to copy the temporaries to
2223 the argument list. Make optimizers happy by pulling out to
2224 temporaries those types that fit in registers. */
2225 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2226 test = is_gimple_val, fb = fb_rvalue;
2227 else
2229 test = is_gimple_lvalue, fb = fb_either;
2230 /* Also strip a TARGET_EXPR that would force an extra copy. */
2231 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2233 tree init = TARGET_EXPR_INITIAL (*arg_p);
2234 if (init
2235 && !VOID_TYPE_P (TREE_TYPE (init)))
2236 *arg_p = init;
2240 /* If this is a variable sized type, we must remember the size. */
2241 maybe_with_size_expr (arg_p);
2243 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2244 /* Make sure arguments have the same location as the function call
2245 itself. */
2246 protected_set_expr_location (*arg_p, call_location);
2248 /* There is a sequence point before a function call. Side effects in
2249 the argument list must occur before the actual call. So, when
2250 gimplifying arguments, force gimplify_expr to use an internal
2251 post queue which is then appended to the end of PRE_P. */
2252 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2255 /* Don't fold inside offloading regsion: it can break code by adding decl
2256 references that weren't in the source. We'll do it during omplower pass
2257 instead. */
2259 static bool
2260 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2262 struct gimplify_omp_ctx *ctx;
2263 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2264 if (ctx->region_type & ORT_TARGET
2265 && ctx->region_type & ORT_TARGET_OFFLOAD)
2266 return false;
2267 return fold_stmt (gsi);
2270 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2271 WANT_VALUE is true if the result of the call is desired. */
2273 static enum gimplify_status
2274 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2276 tree fndecl, parms, p, fnptrtype;
2277 enum gimplify_status ret;
2278 int i, nargs;
2279 gimple call;
2280 bool builtin_va_start_p = false;
2281 location_t loc = EXPR_LOCATION (*expr_p);
2283 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2285 /* For reliable diagnostics during inlining, it is necessary that
2286 every call_expr be annotated with file and line. */
2287 if (! EXPR_HAS_LOCATION (*expr_p))
2288 SET_EXPR_LOCATION (*expr_p, input_location);
2290 /* Gimplify internal functions created in the FEs. */
2291 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2293 nargs = call_expr_nargs (*expr_p);
2294 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2295 auto_vec<tree> vargs (nargs);
2297 for (i = 0; i < nargs; i++)
2299 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2300 EXPR_LOCATION (*expr_p));
2301 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2303 gimple call = gimple_build_call_internal_vec (ifn, vargs);
2304 gimplify_seq_add_stmt (pre_p, call);
2305 return GS_ALL_DONE;
2308 /* This may be a call to a builtin function.
2310 Builtin function calls may be transformed into different
2311 (and more efficient) builtin function calls under certain
2312 circumstances. Unfortunately, gimplification can muck things
2313 up enough that the builtin expanders are not aware that certain
2314 transformations are still valid.
2316 So we attempt transformation/gimplification of the call before
2317 we gimplify the CALL_EXPR. At this time we do not manage to
2318 transform all calls in the same manner as the expanders do, but
2319 we do transform most of them. */
2320 fndecl = get_callee_fndecl (*expr_p);
2321 if (fndecl
2322 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2323 switch (DECL_FUNCTION_CODE (fndecl))
2325 case BUILT_IN_VA_START:
2327 builtin_va_start_p = TRUE;
2328 if (call_expr_nargs (*expr_p) < 2)
2330 error ("too few arguments to function %<va_start%>");
2331 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2332 return GS_OK;
2335 if (fold_builtin_next_arg (*expr_p, true))
2337 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2338 return GS_OK;
2340 break;
2342 case BUILT_IN_LINE:
2344 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2345 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2346 return GS_OK;
2348 case BUILT_IN_FILE:
2350 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2351 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2352 return GS_OK;
2354 case BUILT_IN_FUNCTION:
2356 const char *function;
2357 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2358 *expr_p = build_string_literal (strlen (function) + 1, function);
2359 return GS_OK;
2361 default:
2364 if (fndecl && DECL_BUILT_IN (fndecl))
2366 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2367 if (new_tree && new_tree != *expr_p)
2369 /* There was a transformation of this call which computes the
2370 same value, but in a more efficient way. Return and try
2371 again. */
2372 *expr_p = new_tree;
2373 return GS_OK;
2377 /* Remember the original function pointer type. */
2378 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2380 /* There is a sequence point before the call, so any side effects in
2381 the calling expression must occur before the actual call. Force
2382 gimplify_expr to use an internal post queue. */
2383 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2384 is_gimple_call_addr, fb_rvalue);
2386 nargs = call_expr_nargs (*expr_p);
2388 /* Get argument types for verification. */
2389 fndecl = get_callee_fndecl (*expr_p);
2390 parms = NULL_TREE;
2391 if (fndecl)
2392 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2393 else
2394 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2396 if (fndecl && DECL_ARGUMENTS (fndecl))
2397 p = DECL_ARGUMENTS (fndecl);
2398 else if (parms)
2399 p = parms;
2400 else
2401 p = NULL_TREE;
2402 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2405 /* If the last argument is __builtin_va_arg_pack () and it is not
2406 passed as a named argument, decrease the number of CALL_EXPR
2407 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2408 if (!p
2409 && i < nargs
2410 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2412 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2413 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2415 if (last_arg_fndecl
2416 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2417 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2418 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2420 tree call = *expr_p;
2422 --nargs;
2423 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2424 CALL_EXPR_FN (call),
2425 nargs, CALL_EXPR_ARGP (call));
2427 /* Copy all CALL_EXPR flags, location and block, except
2428 CALL_EXPR_VA_ARG_PACK flag. */
2429 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2430 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2431 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2432 = CALL_EXPR_RETURN_SLOT_OPT (call);
2433 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2434 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2436 /* Set CALL_EXPR_VA_ARG_PACK. */
2437 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2441 /* Finally, gimplify the function arguments. */
2442 if (nargs > 0)
2444 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2445 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2446 PUSH_ARGS_REVERSED ? i-- : i++)
2448 enum gimplify_status t;
2450 /* Avoid gimplifying the second argument to va_start, which needs to
2451 be the plain PARM_DECL. */
2452 if ((i != 1) || !builtin_va_start_p)
2454 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2455 EXPR_LOCATION (*expr_p));
2457 if (t == GS_ERROR)
2458 ret = GS_ERROR;
2463 /* Verify the function result. */
2464 if (want_value && fndecl
2465 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2467 error_at (loc, "using result of function returning %<void%>");
2468 ret = GS_ERROR;
2471 /* Try this again in case gimplification exposed something. */
2472 if (ret != GS_ERROR)
2474 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2476 if (new_tree && new_tree != *expr_p)
2478 /* There was a transformation of this call which computes the
2479 same value, but in a more efficient way. Return and try
2480 again. */
2481 *expr_p = new_tree;
2482 return GS_OK;
2485 else
2487 *expr_p = error_mark_node;
2488 return GS_ERROR;
2491 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2492 decl. This allows us to eliminate redundant or useless
2493 calls to "const" functions. */
2494 if (TREE_CODE (*expr_p) == CALL_EXPR)
2496 int flags = call_expr_flags (*expr_p);
2497 if (flags & (ECF_CONST | ECF_PURE)
2498 /* An infinite loop is considered a side effect. */
2499 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2500 TREE_SIDE_EFFECTS (*expr_p) = 0;
2503 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2504 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2505 form and delegate the creation of a GIMPLE_CALL to
2506 gimplify_modify_expr. This is always possible because when
2507 WANT_VALUE is true, the caller wants the result of this call into
2508 a temporary, which means that we will emit an INIT_EXPR in
2509 internal_get_tmp_var which will then be handled by
2510 gimplify_modify_expr. */
2511 if (!want_value)
2513 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2514 have to do is replicate it as a GIMPLE_CALL tuple. */
2515 gimple_stmt_iterator gsi;
2516 call = gimple_build_call_from_tree (*expr_p);
2517 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2518 notice_special_calls (call);
2519 gimplify_seq_add_stmt (pre_p, call);
2520 gsi = gsi_last (*pre_p);
2521 maybe_fold_stmt (&gsi);
2522 *expr_p = NULL_TREE;
2524 else
2525 /* Remember the original function type. */
2526 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2527 CALL_EXPR_FN (*expr_p));
2529 return ret;
2532 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2533 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2535 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2536 condition is true or false, respectively. If null, we should generate
2537 our own to skip over the evaluation of this specific expression.
2539 LOCUS is the source location of the COND_EXPR.
2541 This function is the tree equivalent of do_jump.
2543 shortcut_cond_r should only be called by shortcut_cond_expr. */
2545 static tree
2546 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2547 location_t locus)
2549 tree local_label = NULL_TREE;
2550 tree t, expr = NULL;
2552 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2553 retain the shortcut semantics. Just insert the gotos here;
2554 shortcut_cond_expr will append the real blocks later. */
2555 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2557 location_t new_locus;
2559 /* Turn if (a && b) into
2561 if (a); else goto no;
2562 if (b) goto yes; else goto no;
2563 (no:) */
2565 if (false_label_p == NULL)
2566 false_label_p = &local_label;
2568 /* Keep the original source location on the first 'if'. */
2569 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2570 append_to_statement_list (t, &expr);
2572 /* Set the source location of the && on the second 'if'. */
2573 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2574 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2575 new_locus);
2576 append_to_statement_list (t, &expr);
2578 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2580 location_t new_locus;
2582 /* Turn if (a || b) into
2584 if (a) goto yes;
2585 if (b) goto yes; else goto no;
2586 (yes:) */
2588 if (true_label_p == NULL)
2589 true_label_p = &local_label;
2591 /* Keep the original source location on the first 'if'. */
2592 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2593 append_to_statement_list (t, &expr);
2595 /* Set the source location of the || on the second 'if'. */
2596 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2597 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2598 new_locus);
2599 append_to_statement_list (t, &expr);
2601 else if (TREE_CODE (pred) == COND_EXPR
2602 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2603 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2605 location_t new_locus;
2607 /* As long as we're messing with gotos, turn if (a ? b : c) into
2608 if (a)
2609 if (b) goto yes; else goto no;
2610 else
2611 if (c) goto yes; else goto no;
2613 Don't do this if one of the arms has void type, which can happen
2614 in C++ when the arm is throw. */
2616 /* Keep the original source location on the first 'if'. Set the source
2617 location of the ? on the second 'if'. */
2618 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2619 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2620 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2621 false_label_p, locus),
2622 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2623 false_label_p, new_locus));
2625 else
2627 expr = build3 (COND_EXPR, void_type_node, pred,
2628 build_and_jump (true_label_p),
2629 build_and_jump (false_label_p));
2630 SET_EXPR_LOCATION (expr, locus);
2633 if (local_label)
2635 t = build1 (LABEL_EXPR, void_type_node, local_label);
2636 append_to_statement_list (t, &expr);
2639 return expr;
2642 /* Given a conditional expression EXPR with short-circuit boolean
2643 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2644 predicate apart into the equivalent sequence of conditionals. */
2646 static tree
2647 shortcut_cond_expr (tree expr)
2649 tree pred = TREE_OPERAND (expr, 0);
2650 tree then_ = TREE_OPERAND (expr, 1);
2651 tree else_ = TREE_OPERAND (expr, 2);
2652 tree true_label, false_label, end_label, t;
2653 tree *true_label_p;
2654 tree *false_label_p;
2655 bool emit_end, emit_false, jump_over_else;
2656 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2657 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2659 /* First do simple transformations. */
2660 if (!else_se)
2662 /* If there is no 'else', turn
2663 if (a && b) then c
2664 into
2665 if (a) if (b) then c. */
2666 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2668 /* Keep the original source location on the first 'if'. */
2669 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2670 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2671 /* Set the source location of the && on the second 'if'. */
2672 if (EXPR_HAS_LOCATION (pred))
2673 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2674 then_ = shortcut_cond_expr (expr);
2675 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2676 pred = TREE_OPERAND (pred, 0);
2677 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2678 SET_EXPR_LOCATION (expr, locus);
2682 if (!then_se)
2684 /* If there is no 'then', turn
2685 if (a || b); else d
2686 into
2687 if (a); else if (b); else d. */
2688 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2690 /* Keep the original source location on the first 'if'. */
2691 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2692 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2693 /* Set the source location of the || on the second 'if'. */
2694 if (EXPR_HAS_LOCATION (pred))
2695 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2696 else_ = shortcut_cond_expr (expr);
2697 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2698 pred = TREE_OPERAND (pred, 0);
2699 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2700 SET_EXPR_LOCATION (expr, locus);
2704 /* If we're done, great. */
2705 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2706 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2707 return expr;
2709 /* Otherwise we need to mess with gotos. Change
2710 if (a) c; else d;
2712 if (a); else goto no;
2713 c; goto end;
2714 no: d; end:
2715 and recursively gimplify the condition. */
2717 true_label = false_label = end_label = NULL_TREE;
2719 /* If our arms just jump somewhere, hijack those labels so we don't
2720 generate jumps to jumps. */
2722 if (then_
2723 && TREE_CODE (then_) == GOTO_EXPR
2724 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2726 true_label = GOTO_DESTINATION (then_);
2727 then_ = NULL;
2728 then_se = false;
2731 if (else_
2732 && TREE_CODE (else_) == GOTO_EXPR
2733 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2735 false_label = GOTO_DESTINATION (else_);
2736 else_ = NULL;
2737 else_se = false;
2740 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2741 if (true_label)
2742 true_label_p = &true_label;
2743 else
2744 true_label_p = NULL;
2746 /* The 'else' branch also needs a label if it contains interesting code. */
2747 if (false_label || else_se)
2748 false_label_p = &false_label;
2749 else
2750 false_label_p = NULL;
2752 /* If there was nothing else in our arms, just forward the label(s). */
2753 if (!then_se && !else_se)
2754 return shortcut_cond_r (pred, true_label_p, false_label_p,
2755 EXPR_LOC_OR_LOC (expr, input_location));
2757 /* If our last subexpression already has a terminal label, reuse it. */
2758 if (else_se)
2759 t = expr_last (else_);
2760 else if (then_se)
2761 t = expr_last (then_);
2762 else
2763 t = NULL;
2764 if (t && TREE_CODE (t) == LABEL_EXPR)
2765 end_label = LABEL_EXPR_LABEL (t);
2767 /* If we don't care about jumping to the 'else' branch, jump to the end
2768 if the condition is false. */
2769 if (!false_label_p)
2770 false_label_p = &end_label;
2772 /* We only want to emit these labels if we aren't hijacking them. */
2773 emit_end = (end_label == NULL_TREE);
2774 emit_false = (false_label == NULL_TREE);
2776 /* We only emit the jump over the else clause if we have to--if the
2777 then clause may fall through. Otherwise we can wind up with a
2778 useless jump and a useless label at the end of gimplified code,
2779 which will cause us to think that this conditional as a whole
2780 falls through even if it doesn't. If we then inline a function
2781 which ends with such a condition, that can cause us to issue an
2782 inappropriate warning about control reaching the end of a
2783 non-void function. */
2784 jump_over_else = block_may_fallthru (then_);
2786 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2787 EXPR_LOC_OR_LOC (expr, input_location));
2789 expr = NULL;
2790 append_to_statement_list (pred, &expr);
2792 append_to_statement_list (then_, &expr);
2793 if (else_se)
2795 if (jump_over_else)
2797 tree last = expr_last (expr);
2798 t = build_and_jump (&end_label);
2799 if (EXPR_HAS_LOCATION (last))
2800 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2801 append_to_statement_list (t, &expr);
2803 if (emit_false)
2805 t = build1 (LABEL_EXPR, void_type_node, false_label);
2806 append_to_statement_list (t, &expr);
2808 append_to_statement_list (else_, &expr);
2810 if (emit_end && end_label)
2812 t = build1 (LABEL_EXPR, void_type_node, end_label);
2813 append_to_statement_list (t, &expr);
2816 return expr;
2819 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2821 tree
2822 gimple_boolify (tree expr)
2824 tree type = TREE_TYPE (expr);
2825 location_t loc = EXPR_LOCATION (expr);
2827 if (TREE_CODE (expr) == NE_EXPR
2828 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2829 && integer_zerop (TREE_OPERAND (expr, 1)))
2831 tree call = TREE_OPERAND (expr, 0);
2832 tree fn = get_callee_fndecl (call);
2834 /* For __builtin_expect ((long) (x), y) recurse into x as well
2835 if x is truth_value_p. */
2836 if (fn
2837 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2838 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2839 && call_expr_nargs (call) == 2)
2841 tree arg = CALL_EXPR_ARG (call, 0);
2842 if (arg)
2844 if (TREE_CODE (arg) == NOP_EXPR
2845 && TREE_TYPE (arg) == TREE_TYPE (call))
2846 arg = TREE_OPERAND (arg, 0);
2847 if (truth_value_p (TREE_CODE (arg)))
2849 arg = gimple_boolify (arg);
2850 CALL_EXPR_ARG (call, 0)
2851 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2857 switch (TREE_CODE (expr))
2859 case TRUTH_AND_EXPR:
2860 case TRUTH_OR_EXPR:
2861 case TRUTH_XOR_EXPR:
2862 case TRUTH_ANDIF_EXPR:
2863 case TRUTH_ORIF_EXPR:
2864 /* Also boolify the arguments of truth exprs. */
2865 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2866 /* FALLTHRU */
2868 case TRUTH_NOT_EXPR:
2869 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2871 /* These expressions always produce boolean results. */
2872 if (TREE_CODE (type) != BOOLEAN_TYPE)
2873 TREE_TYPE (expr) = boolean_type_node;
2874 return expr;
2876 case ANNOTATE_EXPR:
2877 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2879 case annot_expr_ivdep_kind:
2880 case annot_expr_no_vector_kind:
2881 case annot_expr_vector_kind:
2882 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2883 if (TREE_CODE (type) != BOOLEAN_TYPE)
2884 TREE_TYPE (expr) = boolean_type_node;
2885 return expr;
2886 default:
2887 gcc_unreachable ();
2890 default:
2891 if (COMPARISON_CLASS_P (expr))
2893 /* There expressions always prduce boolean results. */
2894 if (TREE_CODE (type) != BOOLEAN_TYPE)
2895 TREE_TYPE (expr) = boolean_type_node;
2896 return expr;
2898 /* Other expressions that get here must have boolean values, but
2899 might need to be converted to the appropriate mode. */
2900 if (TREE_CODE (type) == BOOLEAN_TYPE)
2901 return expr;
2902 return fold_convert_loc (loc, boolean_type_node, expr);
2906 /* Given a conditional expression *EXPR_P without side effects, gimplify
2907 its operands. New statements are inserted to PRE_P. */
2909 static enum gimplify_status
2910 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2912 tree expr = *expr_p, cond;
2913 enum gimplify_status ret, tret;
2914 enum tree_code code;
2916 cond = gimple_boolify (COND_EXPR_COND (expr));
2918 /* We need to handle && and || specially, as their gimplification
2919 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2920 code = TREE_CODE (cond);
2921 if (code == TRUTH_ANDIF_EXPR)
2922 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2923 else if (code == TRUTH_ORIF_EXPR)
2924 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2925 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2926 COND_EXPR_COND (*expr_p) = cond;
2928 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2929 is_gimple_val, fb_rvalue);
2930 ret = MIN (ret, tret);
2931 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2932 is_gimple_val, fb_rvalue);
2934 return MIN (ret, tret);
2937 /* Return true if evaluating EXPR could trap.
2938 EXPR is GENERIC, while tree_could_trap_p can be called
2939 only on GIMPLE. */
2941 static bool
2942 generic_expr_could_trap_p (tree expr)
2944 unsigned i, n;
2946 if (!expr || is_gimple_val (expr))
2947 return false;
2949 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2950 return true;
2952 n = TREE_OPERAND_LENGTH (expr);
2953 for (i = 0; i < n; i++)
2954 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2955 return true;
2957 return false;
2960 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2961 into
2963 if (p) if (p)
2964 t1 = a; a;
2965 else or else
2966 t1 = b; b;
2969 The second form is used when *EXPR_P is of type void.
2971 PRE_P points to the list where side effects that must happen before
2972 *EXPR_P should be stored. */
2974 static enum gimplify_status
2975 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2977 tree expr = *expr_p;
2978 tree type = TREE_TYPE (expr);
2979 location_t loc = EXPR_LOCATION (expr);
2980 tree tmp, arm1, arm2;
2981 enum gimplify_status ret;
2982 tree label_true, label_false, label_cont;
2983 bool have_then_clause_p, have_else_clause_p;
2984 gimple gimple_cond;
2985 enum tree_code pred_code;
2986 gimple_seq seq = NULL;
2988 /* If this COND_EXPR has a value, copy the values into a temporary within
2989 the arms. */
2990 if (!VOID_TYPE_P (type))
2992 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2993 tree result;
2995 /* If either an rvalue is ok or we do not require an lvalue, create the
2996 temporary. But we cannot do that if the type is addressable. */
2997 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
2998 && !TREE_ADDRESSABLE (type))
3000 if (gimplify_ctxp->allow_rhs_cond_expr
3001 /* If either branch has side effects or could trap, it can't be
3002 evaluated unconditionally. */
3003 && !TREE_SIDE_EFFECTS (then_)
3004 && !generic_expr_could_trap_p (then_)
3005 && !TREE_SIDE_EFFECTS (else_)
3006 && !generic_expr_could_trap_p (else_))
3007 return gimplify_pure_cond_expr (expr_p, pre_p);
3009 tmp = create_tmp_var (type, "iftmp");
3010 result = tmp;
3013 /* Otherwise, only create and copy references to the values. */
3014 else
3016 type = build_pointer_type (type);
3018 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3019 then_ = build_fold_addr_expr_loc (loc, then_);
3021 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3022 else_ = build_fold_addr_expr_loc (loc, else_);
3024 expr
3025 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3027 tmp = create_tmp_var (type, "iftmp");
3028 result = build_simple_mem_ref_loc (loc, tmp);
3031 /* Build the new then clause, `tmp = then_;'. But don't build the
3032 assignment if the value is void; in C++ it can be if it's a throw. */
3033 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3034 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3036 /* Similarly, build the new else clause, `tmp = else_;'. */
3037 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3038 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3040 TREE_TYPE (expr) = void_type_node;
3041 recalculate_side_effects (expr);
3043 /* Move the COND_EXPR to the prequeue. */
3044 gimplify_stmt (&expr, pre_p);
3046 *expr_p = result;
3047 return GS_ALL_DONE;
3050 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3051 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3052 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3053 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3055 /* Make sure the condition has BOOLEAN_TYPE. */
3056 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3058 /* Break apart && and || conditions. */
3059 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3060 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3062 expr = shortcut_cond_expr (expr);
3064 if (expr != *expr_p)
3066 *expr_p = expr;
3068 /* We can't rely on gimplify_expr to re-gimplify the expanded
3069 form properly, as cleanups might cause the target labels to be
3070 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3071 set up a conditional context. */
3072 gimple_push_condition ();
3073 gimplify_stmt (expr_p, &seq);
3074 gimple_pop_condition (pre_p);
3075 gimple_seq_add_seq (pre_p, seq);
3077 return GS_ALL_DONE;
3081 /* Now do the normal gimplification. */
3083 /* Gimplify condition. */
3084 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3085 fb_rvalue);
3086 if (ret == GS_ERROR)
3087 return GS_ERROR;
3088 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3090 gimple_push_condition ();
3092 have_then_clause_p = have_else_clause_p = false;
3093 if (TREE_OPERAND (expr, 1) != NULL
3094 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3095 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3096 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3097 == current_function_decl)
3098 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3099 have different locations, otherwise we end up with incorrect
3100 location information on the branches. */
3101 && (optimize
3102 || !EXPR_HAS_LOCATION (expr)
3103 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3104 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3106 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3107 have_then_clause_p = true;
3109 else
3110 label_true = create_artificial_label (UNKNOWN_LOCATION);
3111 if (TREE_OPERAND (expr, 2) != NULL
3112 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3113 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3114 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3115 == current_function_decl)
3116 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3117 have different locations, otherwise we end up with incorrect
3118 location information on the branches. */
3119 && (optimize
3120 || !EXPR_HAS_LOCATION (expr)
3121 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3122 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3124 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3125 have_else_clause_p = true;
3127 else
3128 label_false = create_artificial_label (UNKNOWN_LOCATION);
3130 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3131 &arm2);
3133 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3134 label_false);
3136 gimplify_seq_add_stmt (&seq, gimple_cond);
3137 label_cont = NULL_TREE;
3138 if (!have_then_clause_p)
3140 /* For if (...) {} else { code; } put label_true after
3141 the else block. */
3142 if (TREE_OPERAND (expr, 1) == NULL_TREE
3143 && !have_else_clause_p
3144 && TREE_OPERAND (expr, 2) != NULL_TREE)
3145 label_cont = label_true;
3146 else
3148 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3149 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3150 /* For if (...) { code; } else {} or
3151 if (...) { code; } else goto label; or
3152 if (...) { code; return; } else { ... }
3153 label_cont isn't needed. */
3154 if (!have_else_clause_p
3155 && TREE_OPERAND (expr, 2) != NULL_TREE
3156 && gimple_seq_may_fallthru (seq))
3158 gimple g;
3159 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3161 g = gimple_build_goto (label_cont);
3163 /* GIMPLE_COND's are very low level; they have embedded
3164 gotos. This particular embedded goto should not be marked
3165 with the location of the original COND_EXPR, as it would
3166 correspond to the COND_EXPR's condition, not the ELSE or the
3167 THEN arms. To avoid marking it with the wrong location, flag
3168 it as "no location". */
3169 gimple_set_do_not_emit_location (g);
3171 gimplify_seq_add_stmt (&seq, g);
3175 if (!have_else_clause_p)
3177 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3178 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3180 if (label_cont)
3181 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3183 gimple_pop_condition (pre_p);
3184 gimple_seq_add_seq (pre_p, seq);
3186 if (ret == GS_ERROR)
3187 ; /* Do nothing. */
3188 else if (have_then_clause_p || have_else_clause_p)
3189 ret = GS_ALL_DONE;
3190 else
3192 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3193 expr = TREE_OPERAND (expr, 0);
3194 gimplify_stmt (&expr, pre_p);
3197 *expr_p = NULL;
3198 return ret;
3201 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3202 to be marked addressable.
3204 We cannot rely on such an expression being directly markable if a temporary
3205 has been created by the gimplification. In this case, we create another
3206 temporary and initialize it with a copy, which will become a store after we
3207 mark it addressable. This can happen if the front-end passed us something
3208 that it could not mark addressable yet, like a Fortran pass-by-reference
3209 parameter (int) floatvar. */
3211 static void
3212 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3214 while (handled_component_p (*expr_p))
3215 expr_p = &TREE_OPERAND (*expr_p, 0);
3216 if (is_gimple_reg (*expr_p))
3218 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3219 DECL_GIMPLE_REG_P (var) = 0;
3220 *expr_p = var;
3224 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3225 a call to __builtin_memcpy. */
3227 static enum gimplify_status
3228 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3229 gimple_seq *seq_p)
3231 tree t, to, to_ptr, from, from_ptr;
3232 gimple gs;
3233 location_t loc = EXPR_LOCATION (*expr_p);
3235 to = TREE_OPERAND (*expr_p, 0);
3236 from = TREE_OPERAND (*expr_p, 1);
3238 /* Mark the RHS addressable. Beware that it may not be possible to do so
3239 directly if a temporary has been created by the gimplification. */
3240 prepare_gimple_addressable (&from, seq_p);
3242 mark_addressable (from);
3243 from_ptr = build_fold_addr_expr_loc (loc, from);
3244 gimplify_arg (&from_ptr, seq_p, loc);
3246 mark_addressable (to);
3247 to_ptr = build_fold_addr_expr_loc (loc, to);
3248 gimplify_arg (&to_ptr, seq_p, loc);
3250 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3252 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3254 if (want_value)
3256 /* tmp = memcpy() */
3257 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3258 gimple_call_set_lhs (gs, t);
3259 gimplify_seq_add_stmt (seq_p, gs);
3261 *expr_p = build_simple_mem_ref (t);
3262 return GS_ALL_DONE;
3265 gimplify_seq_add_stmt (seq_p, gs);
3266 *expr_p = NULL;
3267 return GS_ALL_DONE;
3270 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3271 a call to __builtin_memset. In this case we know that the RHS is
3272 a CONSTRUCTOR with an empty element list. */
3274 static enum gimplify_status
3275 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3276 gimple_seq *seq_p)
3278 tree t, from, to, to_ptr;
3279 gimple gs;
3280 location_t loc = EXPR_LOCATION (*expr_p);
3282 /* Assert our assumptions, to abort instead of producing wrong code
3283 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3284 not be immediately exposed. */
3285 from = TREE_OPERAND (*expr_p, 1);
3286 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3287 from = TREE_OPERAND (from, 0);
3289 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3290 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3292 /* Now proceed. */
3293 to = TREE_OPERAND (*expr_p, 0);
3295 to_ptr = build_fold_addr_expr_loc (loc, to);
3296 gimplify_arg (&to_ptr, seq_p, loc);
3297 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3299 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3301 if (want_value)
3303 /* tmp = memset() */
3304 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3305 gimple_call_set_lhs (gs, t);
3306 gimplify_seq_add_stmt (seq_p, gs);
3308 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3309 return GS_ALL_DONE;
3312 gimplify_seq_add_stmt (seq_p, gs);
3313 *expr_p = NULL;
3314 return GS_ALL_DONE;
3317 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3318 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3319 assignment. Return non-null if we detect a potential overlap. */
3321 struct gimplify_init_ctor_preeval_data
3323 /* The base decl of the lhs object. May be NULL, in which case we
3324 have to assume the lhs is indirect. */
3325 tree lhs_base_decl;
3327 /* The alias set of the lhs object. */
3328 alias_set_type lhs_alias_set;
3331 static tree
3332 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3334 struct gimplify_init_ctor_preeval_data *data
3335 = (struct gimplify_init_ctor_preeval_data *) xdata;
3336 tree t = *tp;
3338 /* If we find the base object, obviously we have overlap. */
3339 if (data->lhs_base_decl == t)
3340 return t;
3342 /* If the constructor component is indirect, determine if we have a
3343 potential overlap with the lhs. The only bits of information we
3344 have to go on at this point are addressability and alias sets. */
3345 if ((INDIRECT_REF_P (t)
3346 || TREE_CODE (t) == MEM_REF)
3347 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3348 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3349 return t;
3351 /* If the constructor component is a call, determine if it can hide a
3352 potential overlap with the lhs through an INDIRECT_REF like above.
3353 ??? Ugh - this is completely broken. In fact this whole analysis
3354 doesn't look conservative. */
3355 if (TREE_CODE (t) == CALL_EXPR)
3357 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3359 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3360 if (POINTER_TYPE_P (TREE_VALUE (type))
3361 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3362 && alias_sets_conflict_p (data->lhs_alias_set,
3363 get_alias_set
3364 (TREE_TYPE (TREE_VALUE (type)))))
3365 return t;
3368 if (IS_TYPE_OR_DECL_P (t))
3369 *walk_subtrees = 0;
3370 return NULL;
3373 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3374 force values that overlap with the lhs (as described by *DATA)
3375 into temporaries. */
3377 static void
3378 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3379 struct gimplify_init_ctor_preeval_data *data)
3381 enum gimplify_status one;
3383 /* If the value is constant, then there's nothing to pre-evaluate. */
3384 if (TREE_CONSTANT (*expr_p))
3386 /* Ensure it does not have side effects, it might contain a reference to
3387 the object we're initializing. */
3388 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3389 return;
3392 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3393 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3394 return;
3396 /* Recurse for nested constructors. */
3397 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3399 unsigned HOST_WIDE_INT ix;
3400 constructor_elt *ce;
3401 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3403 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3404 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3406 return;
3409 /* If this is a variable sized type, we must remember the size. */
3410 maybe_with_size_expr (expr_p);
3412 /* Gimplify the constructor element to something appropriate for the rhs
3413 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3414 the gimplifier will consider this a store to memory. Doing this
3415 gimplification now means that we won't have to deal with complicated
3416 language-specific trees, nor trees like SAVE_EXPR that can induce
3417 exponential search behavior. */
3418 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3419 if (one == GS_ERROR)
3421 *expr_p = NULL;
3422 return;
3425 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3426 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3427 always be true for all scalars, since is_gimple_mem_rhs insists on a
3428 temporary variable for them. */
3429 if (DECL_P (*expr_p))
3430 return;
3432 /* If this is of variable size, we have no choice but to assume it doesn't
3433 overlap since we can't make a temporary for it. */
3434 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3435 return;
3437 /* Otherwise, we must search for overlap ... */
3438 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3439 return;
3441 /* ... and if found, force the value into a temporary. */
3442 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3445 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3446 a RANGE_EXPR in a CONSTRUCTOR for an array.
3448 var = lower;
3449 loop_entry:
3450 object[var] = value;
3451 if (var == upper)
3452 goto loop_exit;
3453 var = var + 1;
3454 goto loop_entry;
3455 loop_exit:
3457 We increment var _after_ the loop exit check because we might otherwise
3458 fail if upper == TYPE_MAX_VALUE (type for upper).
3460 Note that we never have to deal with SAVE_EXPRs here, because this has
3461 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3463 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3464 gimple_seq *, bool);
3466 static void
3467 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3468 tree value, tree array_elt_type,
3469 gimple_seq *pre_p, bool cleared)
3471 tree loop_entry_label, loop_exit_label, fall_thru_label;
3472 tree var, var_type, cref, tmp;
3474 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3475 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3476 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3478 /* Create and initialize the index variable. */
3479 var_type = TREE_TYPE (upper);
3480 var = create_tmp_var (var_type, NULL);
3481 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3483 /* Add the loop entry label. */
3484 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3486 /* Build the reference. */
3487 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3488 var, NULL_TREE, NULL_TREE);
3490 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3491 the store. Otherwise just assign value to the reference. */
3493 if (TREE_CODE (value) == CONSTRUCTOR)
3494 /* NB we might have to call ourself recursively through
3495 gimplify_init_ctor_eval if the value is a constructor. */
3496 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3497 pre_p, cleared);
3498 else
3499 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3501 /* We exit the loop when the index var is equal to the upper bound. */
3502 gimplify_seq_add_stmt (pre_p,
3503 gimple_build_cond (EQ_EXPR, var, upper,
3504 loop_exit_label, fall_thru_label));
3506 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3508 /* Otherwise, increment the index var... */
3509 tmp = build2 (PLUS_EXPR, var_type, var,
3510 fold_convert (var_type, integer_one_node));
3511 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3513 /* ...and jump back to the loop entry. */
3514 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3516 /* Add the loop exit label. */
3517 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3520 /* Return true if FDECL is accessing a field that is zero sized. */
3522 static bool
3523 zero_sized_field_decl (const_tree fdecl)
3525 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3526 && integer_zerop (DECL_SIZE (fdecl)))
3527 return true;
3528 return false;
3531 /* Return true if TYPE is zero sized. */
3533 static bool
3534 zero_sized_type (const_tree type)
3536 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3537 && integer_zerop (TYPE_SIZE (type)))
3538 return true;
3539 return false;
3542 /* A subroutine of gimplify_init_constructor. Generate individual
3543 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3544 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3545 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3546 zeroed first. */
3548 static void
3549 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3550 gimple_seq *pre_p, bool cleared)
3552 tree array_elt_type = NULL;
3553 unsigned HOST_WIDE_INT ix;
3554 tree purpose, value;
3556 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3557 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3559 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3561 tree cref;
3563 /* NULL values are created above for gimplification errors. */
3564 if (value == NULL)
3565 continue;
3567 if (cleared && initializer_zerop (value))
3568 continue;
3570 /* ??? Here's to hoping the front end fills in all of the indices,
3571 so we don't have to figure out what's missing ourselves. */
3572 gcc_assert (purpose);
3574 /* Skip zero-sized fields, unless value has side-effects. This can
3575 happen with calls to functions returning a zero-sized type, which
3576 we shouldn't discard. As a number of downstream passes don't
3577 expect sets of zero-sized fields, we rely on the gimplification of
3578 the MODIFY_EXPR we make below to drop the assignment statement. */
3579 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3580 continue;
3582 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3583 whole range. */
3584 if (TREE_CODE (purpose) == RANGE_EXPR)
3586 tree lower = TREE_OPERAND (purpose, 0);
3587 tree upper = TREE_OPERAND (purpose, 1);
3589 /* If the lower bound is equal to upper, just treat it as if
3590 upper was the index. */
3591 if (simple_cst_equal (lower, upper))
3592 purpose = upper;
3593 else
3595 gimplify_init_ctor_eval_range (object, lower, upper, value,
3596 array_elt_type, pre_p, cleared);
3597 continue;
3601 if (array_elt_type)
3603 /* Do not use bitsizetype for ARRAY_REF indices. */
3604 if (TYPE_DOMAIN (TREE_TYPE (object)))
3605 purpose
3606 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3607 purpose);
3608 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3609 purpose, NULL_TREE, NULL_TREE);
3611 else
3613 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3614 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3615 unshare_expr (object), purpose, NULL_TREE);
3618 if (TREE_CODE (value) == CONSTRUCTOR
3619 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3620 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3621 pre_p, cleared);
3622 else
3624 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3625 gimplify_and_add (init, pre_p);
3626 ggc_free (init);
3631 /* Return the appropriate RHS predicate for this LHS. */
3633 gimple_predicate
3634 rhs_predicate_for (tree lhs)
3636 if (is_gimple_reg (lhs))
3637 return is_gimple_reg_rhs_or_call;
3638 else
3639 return is_gimple_mem_rhs_or_call;
3642 /* Gimplify a C99 compound literal expression. This just means adding
3643 the DECL_EXPR before the current statement and using its anonymous
3644 decl instead. */
3646 static enum gimplify_status
3647 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3648 bool (*gimple_test_f) (tree),
3649 fallback_t fallback)
3651 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3652 tree decl = DECL_EXPR_DECL (decl_s);
3653 tree init = DECL_INITIAL (decl);
3654 /* Mark the decl as addressable if the compound literal
3655 expression is addressable now, otherwise it is marked too late
3656 after we gimplify the initialization expression. */
3657 if (TREE_ADDRESSABLE (*expr_p))
3658 TREE_ADDRESSABLE (decl) = 1;
3659 /* Otherwise, if we don't need an lvalue and have a literal directly
3660 substitute it. Check if it matches the gimple predicate, as
3661 otherwise we'd generate a new temporary, and we can as well just
3662 use the decl we already have. */
3663 else if (!TREE_ADDRESSABLE (decl)
3664 && init
3665 && (fallback & fb_lvalue) == 0
3666 && gimple_test_f (init))
3668 *expr_p = init;
3669 return GS_OK;
3672 /* Preliminarily mark non-addressed complex variables as eligible
3673 for promotion to gimple registers. We'll transform their uses
3674 as we find them. */
3675 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3676 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3677 && !TREE_THIS_VOLATILE (decl)
3678 && !needs_to_live_in_memory (decl))
3679 DECL_GIMPLE_REG_P (decl) = 1;
3681 /* If the decl is not addressable, then it is being used in some
3682 expression or on the right hand side of a statement, and it can
3683 be put into a readonly data section. */
3684 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3685 TREE_READONLY (decl) = 1;
3687 /* This decl isn't mentioned in the enclosing block, so add it to the
3688 list of temps. FIXME it seems a bit of a kludge to say that
3689 anonymous artificial vars aren't pushed, but everything else is. */
3690 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3691 gimple_add_tmp_var (decl);
3693 gimplify_and_add (decl_s, pre_p);
3694 *expr_p = decl;
3695 return GS_OK;
3698 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3699 return a new CONSTRUCTOR if something changed. */
3701 static tree
3702 optimize_compound_literals_in_ctor (tree orig_ctor)
3704 tree ctor = orig_ctor;
3705 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3706 unsigned int idx, num = vec_safe_length (elts);
3708 for (idx = 0; idx < num; idx++)
3710 tree value = (*elts)[idx].value;
3711 tree newval = value;
3712 if (TREE_CODE (value) == CONSTRUCTOR)
3713 newval = optimize_compound_literals_in_ctor (value);
3714 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3716 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3717 tree decl = DECL_EXPR_DECL (decl_s);
3718 tree init = DECL_INITIAL (decl);
3720 if (!TREE_ADDRESSABLE (value)
3721 && !TREE_ADDRESSABLE (decl)
3722 && init
3723 && TREE_CODE (init) == CONSTRUCTOR)
3724 newval = optimize_compound_literals_in_ctor (init);
3726 if (newval == value)
3727 continue;
3729 if (ctor == orig_ctor)
3731 ctor = copy_node (orig_ctor);
3732 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3733 elts = CONSTRUCTOR_ELTS (ctor);
3735 (*elts)[idx].value = newval;
3737 return ctor;
3740 /* A subroutine of gimplify_modify_expr. Break out elements of a
3741 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3743 Note that we still need to clear any elements that don't have explicit
3744 initializers, so if not all elements are initialized we keep the
3745 original MODIFY_EXPR, we just remove all of the constructor elements.
3747 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3748 GS_ERROR if we would have to create a temporary when gimplifying
3749 this constructor. Otherwise, return GS_OK.
3751 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3753 static enum gimplify_status
3754 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3755 bool want_value, bool notify_temp_creation)
3757 tree object, ctor, type;
3758 enum gimplify_status ret;
3759 vec<constructor_elt, va_gc> *elts;
3761 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3763 if (!notify_temp_creation)
3765 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3766 is_gimple_lvalue, fb_lvalue);
3767 if (ret == GS_ERROR)
3768 return ret;
3771 object = TREE_OPERAND (*expr_p, 0);
3772 ctor = TREE_OPERAND (*expr_p, 1) =
3773 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3774 type = TREE_TYPE (ctor);
3775 elts = CONSTRUCTOR_ELTS (ctor);
3776 ret = GS_ALL_DONE;
3778 switch (TREE_CODE (type))
3780 case RECORD_TYPE:
3781 case UNION_TYPE:
3782 case QUAL_UNION_TYPE:
3783 case ARRAY_TYPE:
3785 struct gimplify_init_ctor_preeval_data preeval_data;
3786 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3787 bool cleared, complete_p, valid_const_initializer;
3789 /* Aggregate types must lower constructors to initialization of
3790 individual elements. The exception is that a CONSTRUCTOR node
3791 with no elements indicates zero-initialization of the whole. */
3792 if (vec_safe_is_empty (elts))
3794 if (notify_temp_creation)
3795 return GS_OK;
3796 break;
3799 /* Fetch information about the constructor to direct later processing.
3800 We might want to make static versions of it in various cases, and
3801 can only do so if it known to be a valid constant initializer. */
3802 valid_const_initializer
3803 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3804 &num_ctor_elements, &complete_p);
3806 /* If a const aggregate variable is being initialized, then it
3807 should never be a lose to promote the variable to be static. */
3808 if (valid_const_initializer
3809 && num_nonzero_elements > 1
3810 && TREE_READONLY (object)
3811 && TREE_CODE (object) == VAR_DECL
3812 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3814 if (notify_temp_creation)
3815 return GS_ERROR;
3816 DECL_INITIAL (object) = ctor;
3817 TREE_STATIC (object) = 1;
3818 if (!DECL_NAME (object))
3819 DECL_NAME (object) = create_tmp_var_name ("C");
3820 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3822 /* ??? C++ doesn't automatically append a .<number> to the
3823 assembler name, and even when it does, it looks at FE private
3824 data structures to figure out what that number should be,
3825 which are not set for this variable. I suppose this is
3826 important for local statics for inline functions, which aren't
3827 "local" in the object file sense. So in order to get a unique
3828 TU-local symbol, we must invoke the lhd version now. */
3829 lhd_set_decl_assembler_name (object);
3831 *expr_p = NULL_TREE;
3832 break;
3835 /* If there are "lots" of initialized elements, even discounting
3836 those that are not address constants (and thus *must* be
3837 computed at runtime), then partition the constructor into
3838 constant and non-constant parts. Block copy the constant
3839 parts in, then generate code for the non-constant parts. */
3840 /* TODO. There's code in cp/typeck.c to do this. */
3842 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3843 /* store_constructor will ignore the clearing of variable-sized
3844 objects. Initializers for such objects must explicitly set
3845 every field that needs to be set. */
3846 cleared = false;
3847 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3848 /* If the constructor isn't complete, clear the whole object
3849 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3851 ??? This ought not to be needed. For any element not present
3852 in the initializer, we should simply set them to zero. Except
3853 we'd need to *find* the elements that are not present, and that
3854 requires trickery to avoid quadratic compile-time behavior in
3855 large cases or excessive memory use in small cases. */
3856 cleared = true;
3857 else if (num_ctor_elements - num_nonzero_elements
3858 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3859 && num_nonzero_elements < num_ctor_elements / 4)
3860 /* If there are "lots" of zeros, it's more efficient to clear
3861 the memory and then set the nonzero elements. */
3862 cleared = true;
3863 else
3864 cleared = false;
3866 /* If there are "lots" of initialized elements, and all of them
3867 are valid address constants, then the entire initializer can
3868 be dropped to memory, and then memcpy'd out. Don't do this
3869 for sparse arrays, though, as it's more efficient to follow
3870 the standard CONSTRUCTOR behavior of memset followed by
3871 individual element initialization. Also don't do this for small
3872 all-zero initializers (which aren't big enough to merit
3873 clearing), and don't try to make bitwise copies of
3874 TREE_ADDRESSABLE types. */
3875 if (valid_const_initializer
3876 && !(cleared || num_nonzero_elements == 0)
3877 && !TREE_ADDRESSABLE (type))
3879 HOST_WIDE_INT size = int_size_in_bytes (type);
3880 unsigned int align;
3882 /* ??? We can still get unbounded array types, at least
3883 from the C++ front end. This seems wrong, but attempt
3884 to work around it for now. */
3885 if (size < 0)
3887 size = int_size_in_bytes (TREE_TYPE (object));
3888 if (size >= 0)
3889 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3892 /* Find the maximum alignment we can assume for the object. */
3893 /* ??? Make use of DECL_OFFSET_ALIGN. */
3894 if (DECL_P (object))
3895 align = DECL_ALIGN (object);
3896 else
3897 align = TYPE_ALIGN (type);
3899 /* Do a block move either if the size is so small as to make
3900 each individual move a sub-unit move on average, or if it
3901 is so large as to make individual moves inefficient. */
3902 if (size > 0
3903 && num_nonzero_elements > 1
3904 && (size < num_nonzero_elements
3905 || !can_move_by_pieces (size, align)))
3907 if (notify_temp_creation)
3908 return GS_ERROR;
3910 walk_tree (&ctor, force_labels_r, NULL, NULL);
3911 ctor = tree_output_constant_def (ctor);
3912 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3913 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3914 TREE_OPERAND (*expr_p, 1) = ctor;
3916 /* This is no longer an assignment of a CONSTRUCTOR, but
3917 we still may have processing to do on the LHS. So
3918 pretend we didn't do anything here to let that happen. */
3919 return GS_UNHANDLED;
3923 /* If the target is volatile, we have non-zero elements and more than
3924 one field to assign, initialize the target from a temporary. */
3925 if (TREE_THIS_VOLATILE (object)
3926 && !TREE_ADDRESSABLE (type)
3927 && num_nonzero_elements > 0
3928 && vec_safe_length (elts) > 1)
3930 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3931 TREE_OPERAND (*expr_p, 0) = temp;
3932 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3933 *expr_p,
3934 build2 (MODIFY_EXPR, void_type_node,
3935 object, temp));
3936 return GS_OK;
3939 if (notify_temp_creation)
3940 return GS_OK;
3942 /* If there are nonzero elements and if needed, pre-evaluate to capture
3943 elements overlapping with the lhs into temporaries. We must do this
3944 before clearing to fetch the values before they are zeroed-out. */
3945 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3947 preeval_data.lhs_base_decl = get_base_address (object);
3948 if (!DECL_P (preeval_data.lhs_base_decl))
3949 preeval_data.lhs_base_decl = NULL;
3950 preeval_data.lhs_alias_set = get_alias_set (object);
3952 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3953 pre_p, post_p, &preeval_data);
3956 if (cleared)
3958 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3959 Note that we still have to gimplify, in order to handle the
3960 case of variable sized types. Avoid shared tree structures. */
3961 CONSTRUCTOR_ELTS (ctor) = NULL;
3962 TREE_SIDE_EFFECTS (ctor) = 0;
3963 object = unshare_expr (object);
3964 gimplify_stmt (expr_p, pre_p);
3967 /* If we have not block cleared the object, or if there are nonzero
3968 elements in the constructor, add assignments to the individual
3969 scalar fields of the object. */
3970 if (!cleared || num_nonzero_elements > 0)
3971 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3973 *expr_p = NULL_TREE;
3975 break;
3977 case COMPLEX_TYPE:
3979 tree r, i;
3981 if (notify_temp_creation)
3982 return GS_OK;
3984 /* Extract the real and imaginary parts out of the ctor. */
3985 gcc_assert (elts->length () == 2);
3986 r = (*elts)[0].value;
3987 i = (*elts)[1].value;
3988 if (r == NULL || i == NULL)
3990 tree zero = build_zero_cst (TREE_TYPE (type));
3991 if (r == NULL)
3992 r = zero;
3993 if (i == NULL)
3994 i = zero;
3997 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3998 represent creation of a complex value. */
3999 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4001 ctor = build_complex (type, r, i);
4002 TREE_OPERAND (*expr_p, 1) = ctor;
4004 else
4006 ctor = build2 (COMPLEX_EXPR, type, r, i);
4007 TREE_OPERAND (*expr_p, 1) = ctor;
4008 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4009 pre_p,
4010 post_p,
4011 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4012 fb_rvalue);
4015 break;
4017 case VECTOR_TYPE:
4019 unsigned HOST_WIDE_INT ix;
4020 constructor_elt *ce;
4022 if (notify_temp_creation)
4023 return GS_OK;
4025 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4026 if (TREE_CONSTANT (ctor))
4028 bool constant_p = true;
4029 tree value;
4031 /* Even when ctor is constant, it might contain non-*_CST
4032 elements, such as addresses or trapping values like
4033 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4034 in VECTOR_CST nodes. */
4035 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4036 if (!CONSTANT_CLASS_P (value))
4038 constant_p = false;
4039 break;
4042 if (constant_p)
4044 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4045 break;
4048 TREE_CONSTANT (ctor) = 0;
4051 /* Vector types use CONSTRUCTOR all the way through gimple
4052 compilation as a general initializer. */
4053 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4055 enum gimplify_status tret;
4056 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4057 fb_rvalue);
4058 if (tret == GS_ERROR)
4059 ret = GS_ERROR;
4061 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4062 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4064 break;
4066 default:
4067 /* So how did we get a CONSTRUCTOR for a scalar type? */
4068 gcc_unreachable ();
4071 if (ret == GS_ERROR)
4072 return GS_ERROR;
4073 else if (want_value)
4075 *expr_p = object;
4076 return GS_OK;
4078 else
4080 /* If we have gimplified both sides of the initializer but have
4081 not emitted an assignment, do so now. */
4082 if (*expr_p)
4084 tree lhs = TREE_OPERAND (*expr_p, 0);
4085 tree rhs = TREE_OPERAND (*expr_p, 1);
4086 gimple init = gimple_build_assign (lhs, rhs);
4087 gimplify_seq_add_stmt (pre_p, init);
4088 *expr_p = NULL;
4091 return GS_ALL_DONE;
4095 /* Given a pointer value OP0, return a simplified version of an
4096 indirection through OP0, or NULL_TREE if no simplification is
4097 possible. This may only be applied to a rhs of an expression.
4098 Note that the resulting type may be different from the type pointed
4099 to in the sense that it is still compatible from the langhooks
4100 point of view. */
4102 static tree
4103 gimple_fold_indirect_ref_rhs (tree t)
4105 return gimple_fold_indirect_ref (t);
4108 /* Subroutine of gimplify_modify_expr to do simplifications of
4109 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4110 something changes. */
4112 static enum gimplify_status
4113 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4114 gimple_seq *pre_p, gimple_seq *post_p,
4115 bool want_value)
4117 enum gimplify_status ret = GS_UNHANDLED;
4118 bool changed;
4122 changed = false;
4123 switch (TREE_CODE (*from_p))
4125 case VAR_DECL:
4126 /* If we're assigning from a read-only variable initialized with
4127 a constructor, do the direct assignment from the constructor,
4128 but only if neither source nor target are volatile since this
4129 latter assignment might end up being done on a per-field basis. */
4130 if (DECL_INITIAL (*from_p)
4131 && TREE_READONLY (*from_p)
4132 && !TREE_THIS_VOLATILE (*from_p)
4133 && !TREE_THIS_VOLATILE (*to_p)
4134 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4136 tree old_from = *from_p;
4137 enum gimplify_status subret;
4139 /* Move the constructor into the RHS. */
4140 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4142 /* Let's see if gimplify_init_constructor will need to put
4143 it in memory. */
4144 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4145 false, true);
4146 if (subret == GS_ERROR)
4148 /* If so, revert the change. */
4149 *from_p = old_from;
4151 else
4153 ret = GS_OK;
4154 changed = true;
4157 break;
4158 case INDIRECT_REF:
4160 /* If we have code like
4162 *(const A*)(A*)&x
4164 where the type of "x" is a (possibly cv-qualified variant
4165 of "A"), treat the entire expression as identical to "x".
4166 This kind of code arises in C++ when an object is bound
4167 to a const reference, and if "x" is a TARGET_EXPR we want
4168 to take advantage of the optimization below. */
4169 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4170 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4171 if (t)
4173 if (TREE_THIS_VOLATILE (t) != volatile_p)
4175 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4176 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4177 build_fold_addr_expr (t));
4178 if (REFERENCE_CLASS_P (t))
4179 TREE_THIS_VOLATILE (t) = volatile_p;
4181 *from_p = t;
4182 ret = GS_OK;
4183 changed = true;
4185 break;
4188 case TARGET_EXPR:
4190 /* If we are initializing something from a TARGET_EXPR, strip the
4191 TARGET_EXPR and initialize it directly, if possible. This can't
4192 be done if the initializer is void, since that implies that the
4193 temporary is set in some non-trivial way.
4195 ??? What about code that pulls out the temp and uses it
4196 elsewhere? I think that such code never uses the TARGET_EXPR as
4197 an initializer. If I'm wrong, we'll die because the temp won't
4198 have any RTL. In that case, I guess we'll need to replace
4199 references somehow. */
4200 tree init = TARGET_EXPR_INITIAL (*from_p);
4202 if (init
4203 && !VOID_TYPE_P (TREE_TYPE (init)))
4205 *from_p = init;
4206 ret = GS_OK;
4207 changed = true;
4210 break;
4212 case COMPOUND_EXPR:
4213 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4214 caught. */
4215 gimplify_compound_expr (from_p, pre_p, true);
4216 ret = GS_OK;
4217 changed = true;
4218 break;
4220 case CONSTRUCTOR:
4221 /* If we already made some changes, let the front end have a
4222 crack at this before we break it down. */
4223 if (ret != GS_UNHANDLED)
4224 break;
4225 /* If we're initializing from a CONSTRUCTOR, break this into
4226 individual MODIFY_EXPRs. */
4227 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4228 false);
4230 case COND_EXPR:
4231 /* If we're assigning to a non-register type, push the assignment
4232 down into the branches. This is mandatory for ADDRESSABLE types,
4233 since we cannot generate temporaries for such, but it saves a
4234 copy in other cases as well. */
4235 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4237 /* This code should mirror the code in gimplify_cond_expr. */
4238 enum tree_code code = TREE_CODE (*expr_p);
4239 tree cond = *from_p;
4240 tree result = *to_p;
4242 ret = gimplify_expr (&result, pre_p, post_p,
4243 is_gimple_lvalue, fb_lvalue);
4244 if (ret != GS_ERROR)
4245 ret = GS_OK;
4247 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4248 TREE_OPERAND (cond, 1)
4249 = build2 (code, void_type_node, result,
4250 TREE_OPERAND (cond, 1));
4251 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4252 TREE_OPERAND (cond, 2)
4253 = build2 (code, void_type_node, unshare_expr (result),
4254 TREE_OPERAND (cond, 2));
4256 TREE_TYPE (cond) = void_type_node;
4257 recalculate_side_effects (cond);
4259 if (want_value)
4261 gimplify_and_add (cond, pre_p);
4262 *expr_p = unshare_expr (result);
4264 else
4265 *expr_p = cond;
4266 return ret;
4268 break;
4270 case CALL_EXPR:
4271 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4272 return slot so that we don't generate a temporary. */
4273 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4274 && aggregate_value_p (*from_p, *from_p))
4276 bool use_target;
4278 if (!(rhs_predicate_for (*to_p))(*from_p))
4279 /* If we need a temporary, *to_p isn't accurate. */
4280 use_target = false;
4281 /* It's OK to use the return slot directly unless it's an NRV. */
4282 else if (TREE_CODE (*to_p) == RESULT_DECL
4283 && DECL_NAME (*to_p) == NULL_TREE
4284 && needs_to_live_in_memory (*to_p))
4285 use_target = true;
4286 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4287 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4288 /* Don't force regs into memory. */
4289 use_target = false;
4290 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4291 /* It's OK to use the target directly if it's being
4292 initialized. */
4293 use_target = true;
4294 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4295 /* Always use the target and thus RSO for variable-sized types.
4296 GIMPLE cannot deal with a variable-sized assignment
4297 embedded in a call statement. */
4298 use_target = true;
4299 else if (TREE_CODE (*to_p) != SSA_NAME
4300 && (!is_gimple_variable (*to_p)
4301 || needs_to_live_in_memory (*to_p)))
4302 /* Don't use the original target if it's already addressable;
4303 if its address escapes, and the called function uses the
4304 NRV optimization, a conforming program could see *to_p
4305 change before the called function returns; see c++/19317.
4306 When optimizing, the return_slot pass marks more functions
4307 as safe after we have escape info. */
4308 use_target = false;
4309 else
4310 use_target = true;
4312 if (use_target)
4314 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4315 mark_addressable (*to_p);
4318 break;
4320 case WITH_SIZE_EXPR:
4321 /* Likewise for calls that return an aggregate of non-constant size,
4322 since we would not be able to generate a temporary at all. */
4323 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4325 *from_p = TREE_OPERAND (*from_p, 0);
4326 /* We don't change ret in this case because the
4327 WITH_SIZE_EXPR might have been added in
4328 gimplify_modify_expr, so returning GS_OK would lead to an
4329 infinite loop. */
4330 changed = true;
4332 break;
4334 /* If we're initializing from a container, push the initialization
4335 inside it. */
4336 case CLEANUP_POINT_EXPR:
4337 case BIND_EXPR:
4338 case STATEMENT_LIST:
4340 tree wrap = *from_p;
4341 tree t;
4343 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4344 fb_lvalue);
4345 if (ret != GS_ERROR)
4346 ret = GS_OK;
4348 t = voidify_wrapper_expr (wrap, *expr_p);
4349 gcc_assert (t == *expr_p);
4351 if (want_value)
4353 gimplify_and_add (wrap, pre_p);
4354 *expr_p = unshare_expr (*to_p);
4356 else
4357 *expr_p = wrap;
4358 return GS_OK;
4361 case COMPOUND_LITERAL_EXPR:
4363 tree complit = TREE_OPERAND (*expr_p, 1);
4364 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4365 tree decl = DECL_EXPR_DECL (decl_s);
4366 tree init = DECL_INITIAL (decl);
4368 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4369 into struct T x = { 0, 1, 2 } if the address of the
4370 compound literal has never been taken. */
4371 if (!TREE_ADDRESSABLE (complit)
4372 && !TREE_ADDRESSABLE (decl)
4373 && init)
4375 *expr_p = copy_node (*expr_p);
4376 TREE_OPERAND (*expr_p, 1) = init;
4377 return GS_OK;
4381 default:
4382 break;
4385 while (changed);
4387 return ret;
4391 /* Return true if T looks like a valid GIMPLE statement. */
4393 static bool
4394 is_gimple_stmt (tree t)
4396 const enum tree_code code = TREE_CODE (t);
4398 switch (code)
4400 case NOP_EXPR:
4401 /* The only valid NOP_EXPR is the empty statement. */
4402 return IS_EMPTY_STMT (t);
4404 case BIND_EXPR:
4405 case COND_EXPR:
4406 /* These are only valid if they're void. */
4407 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4409 case SWITCH_EXPR:
4410 case GOTO_EXPR:
4411 case RETURN_EXPR:
4412 case LABEL_EXPR:
4413 case CASE_LABEL_EXPR:
4414 case TRY_CATCH_EXPR:
4415 case TRY_FINALLY_EXPR:
4416 case EH_FILTER_EXPR:
4417 case CATCH_EXPR:
4418 case ASM_EXPR:
4419 case STATEMENT_LIST:
4420 case OACC_PARALLEL:
4421 case OACC_KERNELS:
4422 case OACC_DATA:
4423 case OACC_HOST_DATA:
4424 case OACC_DECLARE:
4425 case OACC_UPDATE:
4426 case OACC_ENTER_DATA:
4427 case OACC_EXIT_DATA:
4428 case OACC_CACHE:
4429 case OMP_PARALLEL:
4430 case OMP_FOR:
4431 case OMP_SIMD:
4432 case CILK_SIMD:
4433 case OMP_DISTRIBUTE:
4434 case OACC_LOOP:
4435 case OMP_SECTIONS:
4436 case OMP_SECTION:
4437 case OMP_SINGLE:
4438 case OMP_MASTER:
4439 case OMP_TASKGROUP:
4440 case OMP_ORDERED:
4441 case OMP_CRITICAL:
4442 case OMP_TASK:
4443 /* These are always void. */
4444 return true;
4446 case CALL_EXPR:
4447 case MODIFY_EXPR:
4448 case PREDICT_EXPR:
4449 /* These are valid regardless of their type. */
4450 return true;
4452 default:
4453 return false;
4458 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4459 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4460 DECL_GIMPLE_REG_P set.
4462 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4463 other, unmodified part of the complex object just before the total store.
4464 As a consequence, if the object is still uninitialized, an undefined value
4465 will be loaded into a register, which may result in a spurious exception
4466 if the register is floating-point and the value happens to be a signaling
4467 NaN for example. Then the fully-fledged complex operations lowering pass
4468 followed by a DCE pass are necessary in order to fix things up. */
4470 static enum gimplify_status
4471 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4472 bool want_value)
4474 enum tree_code code, ocode;
4475 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4477 lhs = TREE_OPERAND (*expr_p, 0);
4478 rhs = TREE_OPERAND (*expr_p, 1);
4479 code = TREE_CODE (lhs);
4480 lhs = TREE_OPERAND (lhs, 0);
4482 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4483 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4484 TREE_NO_WARNING (other) = 1;
4485 other = get_formal_tmp_var (other, pre_p);
4487 realpart = code == REALPART_EXPR ? rhs : other;
4488 imagpart = code == REALPART_EXPR ? other : rhs;
4490 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4491 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4492 else
4493 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4495 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4496 *expr_p = (want_value) ? rhs : NULL_TREE;
4498 return GS_ALL_DONE;
4501 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4503 modify_expr
4504 : varname '=' rhs
4505 | '*' ID '=' rhs
4507 PRE_P points to the list where side effects that must happen before
4508 *EXPR_P should be stored.
4510 POST_P points to the list where side effects that must happen after
4511 *EXPR_P should be stored.
4513 WANT_VALUE is nonzero iff we want to use the value of this expression
4514 in another expression. */
4516 static enum gimplify_status
4517 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4518 bool want_value)
4520 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4521 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4522 enum gimplify_status ret = GS_UNHANDLED;
4523 gimple assign;
4524 location_t loc = EXPR_LOCATION (*expr_p);
4525 gimple_stmt_iterator gsi;
4527 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4528 || TREE_CODE (*expr_p) == INIT_EXPR);
4530 /* Trying to simplify a clobber using normal logic doesn't work,
4531 so handle it here. */
4532 if (TREE_CLOBBER_P (*from_p))
4534 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4535 if (ret == GS_ERROR)
4536 return ret;
4537 gcc_assert (!want_value
4538 && (TREE_CODE (*to_p) == VAR_DECL
4539 || TREE_CODE (*to_p) == MEM_REF));
4540 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4541 *expr_p = NULL;
4542 return GS_ALL_DONE;
4545 /* Insert pointer conversions required by the middle-end that are not
4546 required by the frontend. This fixes middle-end type checking for
4547 for example gcc.dg/redecl-6.c. */
4548 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4550 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4551 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4552 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4555 /* See if any simplifications can be done based on what the RHS is. */
4556 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4557 want_value);
4558 if (ret != GS_UNHANDLED)
4559 return ret;
4561 /* For zero sized types only gimplify the left hand side and right hand
4562 side as statements and throw away the assignment. Do this after
4563 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4564 types properly. */
4565 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4567 gimplify_stmt (from_p, pre_p);
4568 gimplify_stmt (to_p, pre_p);
4569 *expr_p = NULL_TREE;
4570 return GS_ALL_DONE;
4573 /* If the value being copied is of variable width, compute the length
4574 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4575 before gimplifying any of the operands so that we can resolve any
4576 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4577 the size of the expression to be copied, not of the destination, so
4578 that is what we must do here. */
4579 maybe_with_size_expr (from_p);
4581 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4582 if (ret == GS_ERROR)
4583 return ret;
4585 /* As a special case, we have to temporarily allow for assignments
4586 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4587 a toplevel statement, when gimplifying the GENERIC expression
4588 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4589 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4591 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4592 prevent gimplify_expr from trying to create a new temporary for
4593 foo's LHS, we tell it that it should only gimplify until it
4594 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4595 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4596 and all we need to do here is set 'a' to be its LHS. */
4597 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4598 fb_rvalue);
4599 if (ret == GS_ERROR)
4600 return ret;
4602 /* Now see if the above changed *from_p to something we handle specially. */
4603 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4604 want_value);
4605 if (ret != GS_UNHANDLED)
4606 return ret;
4608 /* If we've got a variable sized assignment between two lvalues (i.e. does
4609 not involve a call), then we can make things a bit more straightforward
4610 by converting the assignment to memcpy or memset. */
4611 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4613 tree from = TREE_OPERAND (*from_p, 0);
4614 tree size = TREE_OPERAND (*from_p, 1);
4616 if (TREE_CODE (from) == CONSTRUCTOR)
4617 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4619 if (is_gimple_addressable (from))
4621 *from_p = from;
4622 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4623 pre_p);
4627 /* Transform partial stores to non-addressable complex variables into
4628 total stores. This allows us to use real instead of virtual operands
4629 for these variables, which improves optimization. */
4630 if ((TREE_CODE (*to_p) == REALPART_EXPR
4631 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4632 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4633 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4635 /* Try to alleviate the effects of the gimplification creating artificial
4636 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4637 if (!gimplify_ctxp->into_ssa
4638 && TREE_CODE (*from_p) == VAR_DECL
4639 && DECL_IGNORED_P (*from_p)
4640 && DECL_P (*to_p)
4641 && !DECL_IGNORED_P (*to_p))
4643 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4644 DECL_NAME (*from_p)
4645 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4646 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4647 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4650 if (want_value && TREE_THIS_VOLATILE (*to_p))
4651 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4653 if (TREE_CODE (*from_p) == CALL_EXPR)
4655 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4656 instead of a GIMPLE_ASSIGN. */
4657 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4658 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4659 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4660 tree fndecl = get_callee_fndecl (*from_p);
4661 if (fndecl
4662 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4663 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4664 && call_expr_nargs (*from_p) == 3)
4665 assign = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4666 CALL_EXPR_ARG (*from_p, 0),
4667 CALL_EXPR_ARG (*from_p, 1),
4668 CALL_EXPR_ARG (*from_p, 2));
4669 else
4671 assign = gimple_build_call_from_tree (*from_p);
4672 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4674 notice_special_calls (assign);
4675 if (!gimple_call_noreturn_p (assign))
4676 gimple_call_set_lhs (assign, *to_p);
4678 else
4680 assign = gimple_build_assign (*to_p, *from_p);
4681 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4684 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4686 /* We should have got an SSA name from the start. */
4687 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4690 gimplify_seq_add_stmt (pre_p, assign);
4691 gsi = gsi_last (*pre_p);
4692 maybe_fold_stmt (&gsi);
4694 if (want_value)
4696 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4697 return GS_OK;
4699 else
4700 *expr_p = NULL;
4702 return GS_ALL_DONE;
4705 /* Gimplify a comparison between two variable-sized objects. Do this
4706 with a call to BUILT_IN_MEMCMP. */
4708 static enum gimplify_status
4709 gimplify_variable_sized_compare (tree *expr_p)
4711 location_t loc = EXPR_LOCATION (*expr_p);
4712 tree op0 = TREE_OPERAND (*expr_p, 0);
4713 tree op1 = TREE_OPERAND (*expr_p, 1);
4714 tree t, arg, dest, src, expr;
4716 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4717 arg = unshare_expr (arg);
4718 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4719 src = build_fold_addr_expr_loc (loc, op1);
4720 dest = build_fold_addr_expr_loc (loc, op0);
4721 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4722 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4724 expr
4725 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4726 SET_EXPR_LOCATION (expr, loc);
4727 *expr_p = expr;
4729 return GS_OK;
4732 /* Gimplify a comparison between two aggregate objects of integral scalar
4733 mode as a comparison between the bitwise equivalent scalar values. */
4735 static enum gimplify_status
4736 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4738 location_t loc = EXPR_LOCATION (*expr_p);
4739 tree op0 = TREE_OPERAND (*expr_p, 0);
4740 tree op1 = TREE_OPERAND (*expr_p, 1);
4742 tree type = TREE_TYPE (op0);
4743 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4745 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4746 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4748 *expr_p
4749 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4751 return GS_OK;
4754 /* Gimplify an expression sequence. This function gimplifies each
4755 expression and rewrites the original expression with the last
4756 expression of the sequence in GIMPLE form.
4758 PRE_P points to the list where the side effects for all the
4759 expressions in the sequence will be emitted.
4761 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4763 static enum gimplify_status
4764 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4766 tree t = *expr_p;
4770 tree *sub_p = &TREE_OPERAND (t, 0);
4772 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4773 gimplify_compound_expr (sub_p, pre_p, false);
4774 else
4775 gimplify_stmt (sub_p, pre_p);
4777 t = TREE_OPERAND (t, 1);
4779 while (TREE_CODE (t) == COMPOUND_EXPR);
4781 *expr_p = t;
4782 if (want_value)
4783 return GS_OK;
4784 else
4786 gimplify_stmt (expr_p, pre_p);
4787 return GS_ALL_DONE;
4791 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4792 gimplify. After gimplification, EXPR_P will point to a new temporary
4793 that holds the original value of the SAVE_EXPR node.
4795 PRE_P points to the list where side effects that must happen before
4796 *EXPR_P should be stored. */
4798 static enum gimplify_status
4799 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4801 enum gimplify_status ret = GS_ALL_DONE;
4802 tree val;
4804 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4805 val = TREE_OPERAND (*expr_p, 0);
4807 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4808 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4810 /* The operand may be a void-valued expression such as SAVE_EXPRs
4811 generated by the Java frontend for class initialization. It is
4812 being executed only for its side-effects. */
4813 if (TREE_TYPE (val) == void_type_node)
4815 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4816 is_gimple_stmt, fb_none);
4817 val = NULL;
4819 else
4820 val = get_initialized_tmp_var (val, pre_p, post_p);
4822 TREE_OPERAND (*expr_p, 0) = val;
4823 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4826 *expr_p = val;
4828 return ret;
4831 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4833 unary_expr
4834 : ...
4835 | '&' varname
4838 PRE_P points to the list where side effects that must happen before
4839 *EXPR_P should be stored.
4841 POST_P points to the list where side effects that must happen after
4842 *EXPR_P should be stored. */
4844 static enum gimplify_status
4845 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4847 tree expr = *expr_p;
4848 tree op0 = TREE_OPERAND (expr, 0);
4849 enum gimplify_status ret;
4850 location_t loc = EXPR_LOCATION (*expr_p);
4852 switch (TREE_CODE (op0))
4854 case INDIRECT_REF:
4855 do_indirect_ref:
4856 /* Check if we are dealing with an expression of the form '&*ptr'.
4857 While the front end folds away '&*ptr' into 'ptr', these
4858 expressions may be generated internally by the compiler (e.g.,
4859 builtins like __builtin_va_end). */
4860 /* Caution: the silent array decomposition semantics we allow for
4861 ADDR_EXPR means we can't always discard the pair. */
4862 /* Gimplification of the ADDR_EXPR operand may drop
4863 cv-qualification conversions, so make sure we add them if
4864 needed. */
4866 tree op00 = TREE_OPERAND (op0, 0);
4867 tree t_expr = TREE_TYPE (expr);
4868 tree t_op00 = TREE_TYPE (op00);
4870 if (!useless_type_conversion_p (t_expr, t_op00))
4871 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4872 *expr_p = op00;
4873 ret = GS_OK;
4875 break;
4877 case VIEW_CONVERT_EXPR:
4878 /* Take the address of our operand and then convert it to the type of
4879 this ADDR_EXPR.
4881 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4882 all clear. The impact of this transformation is even less clear. */
4884 /* If the operand is a useless conversion, look through it. Doing so
4885 guarantees that the ADDR_EXPR and its operand will remain of the
4886 same type. */
4887 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4888 op0 = TREE_OPERAND (op0, 0);
4890 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4891 build_fold_addr_expr_loc (loc,
4892 TREE_OPERAND (op0, 0)));
4893 ret = GS_OK;
4894 break;
4896 default:
4897 /* We use fb_either here because the C frontend sometimes takes
4898 the address of a call that returns a struct; see
4899 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4900 the implied temporary explicit. */
4902 /* Make the operand addressable. */
4903 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4904 is_gimple_addressable, fb_either);
4905 if (ret == GS_ERROR)
4906 break;
4908 /* Then mark it. Beware that it may not be possible to do so directly
4909 if a temporary has been created by the gimplification. */
4910 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4912 op0 = TREE_OPERAND (expr, 0);
4914 /* For various reasons, the gimplification of the expression
4915 may have made a new INDIRECT_REF. */
4916 if (TREE_CODE (op0) == INDIRECT_REF)
4917 goto do_indirect_ref;
4919 mark_addressable (TREE_OPERAND (expr, 0));
4921 /* The FEs may end up building ADDR_EXPRs early on a decl with
4922 an incomplete type. Re-build ADDR_EXPRs in canonical form
4923 here. */
4924 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4925 *expr_p = build_fold_addr_expr (op0);
4927 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4928 recompute_tree_invariant_for_addr_expr (*expr_p);
4930 /* If we re-built the ADDR_EXPR add a conversion to the original type
4931 if required. */
4932 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4933 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4935 break;
4938 return ret;
4941 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4942 value; output operands should be a gimple lvalue. */
4944 static enum gimplify_status
4945 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4947 tree expr;
4948 int noutputs;
4949 const char **oconstraints;
4950 int i;
4951 tree link;
4952 const char *constraint;
4953 bool allows_mem, allows_reg, is_inout;
4954 enum gimplify_status ret, tret;
4955 gimple stmt;
4956 vec<tree, va_gc> *inputs;
4957 vec<tree, va_gc> *outputs;
4958 vec<tree, va_gc> *clobbers;
4959 vec<tree, va_gc> *labels;
4960 tree link_next;
4962 expr = *expr_p;
4963 noutputs = list_length (ASM_OUTPUTS (expr));
4964 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4966 inputs = NULL;
4967 outputs = NULL;
4968 clobbers = NULL;
4969 labels = NULL;
4971 ret = GS_ALL_DONE;
4972 link_next = NULL_TREE;
4973 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4975 bool ok;
4976 size_t constraint_len;
4978 link_next = TREE_CHAIN (link);
4980 oconstraints[i]
4981 = constraint
4982 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4983 constraint_len = strlen (constraint);
4984 if (constraint_len == 0)
4985 continue;
4987 ok = parse_output_constraint (&constraint, i, 0, 0,
4988 &allows_mem, &allows_reg, &is_inout);
4989 if (!ok)
4991 ret = GS_ERROR;
4992 is_inout = false;
4995 if (!allows_reg && allows_mem)
4996 mark_addressable (TREE_VALUE (link));
4998 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4999 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5000 fb_lvalue | fb_mayfail);
5001 if (tret == GS_ERROR)
5003 error ("invalid lvalue in asm output %d", i);
5004 ret = tret;
5007 vec_safe_push (outputs, link);
5008 TREE_CHAIN (link) = NULL_TREE;
5010 if (is_inout)
5012 /* An input/output operand. To give the optimizers more
5013 flexibility, split it into separate input and output
5014 operands. */
5015 tree input;
5016 char buf[10];
5018 /* Turn the in/out constraint into an output constraint. */
5019 char *p = xstrdup (constraint);
5020 p[0] = '=';
5021 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5023 /* And add a matching input constraint. */
5024 if (allows_reg)
5026 sprintf (buf, "%d", i);
5028 /* If there are multiple alternatives in the constraint,
5029 handle each of them individually. Those that allow register
5030 will be replaced with operand number, the others will stay
5031 unchanged. */
5032 if (strchr (p, ',') != NULL)
5034 size_t len = 0, buflen = strlen (buf);
5035 char *beg, *end, *str, *dst;
5037 for (beg = p + 1;;)
5039 end = strchr (beg, ',');
5040 if (end == NULL)
5041 end = strchr (beg, '\0');
5042 if ((size_t) (end - beg) < buflen)
5043 len += buflen + 1;
5044 else
5045 len += end - beg + 1;
5046 if (*end)
5047 beg = end + 1;
5048 else
5049 break;
5052 str = (char *) alloca (len);
5053 for (beg = p + 1, dst = str;;)
5055 const char *tem;
5056 bool mem_p, reg_p, inout_p;
5058 end = strchr (beg, ',');
5059 if (end)
5060 *end = '\0';
5061 beg[-1] = '=';
5062 tem = beg - 1;
5063 parse_output_constraint (&tem, i, 0, 0,
5064 &mem_p, &reg_p, &inout_p);
5065 if (dst != str)
5066 *dst++ = ',';
5067 if (reg_p)
5069 memcpy (dst, buf, buflen);
5070 dst += buflen;
5072 else
5074 if (end)
5075 len = end - beg;
5076 else
5077 len = strlen (beg);
5078 memcpy (dst, beg, len);
5079 dst += len;
5081 if (end)
5082 beg = end + 1;
5083 else
5084 break;
5086 *dst = '\0';
5087 input = build_string (dst - str, str);
5089 else
5090 input = build_string (strlen (buf), buf);
5092 else
5093 input = build_string (constraint_len - 1, constraint + 1);
5095 free (p);
5097 input = build_tree_list (build_tree_list (NULL_TREE, input),
5098 unshare_expr (TREE_VALUE (link)));
5099 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5103 link_next = NULL_TREE;
5104 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5106 link_next = TREE_CHAIN (link);
5107 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5108 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5109 oconstraints, &allows_mem, &allows_reg);
5111 /* If we can't make copies, we can only accept memory. */
5112 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5114 if (allows_mem)
5115 allows_reg = 0;
5116 else
5118 error ("impossible constraint in %<asm%>");
5119 error ("non-memory input %d must stay in memory", i);
5120 return GS_ERROR;
5124 /* If the operand is a memory input, it should be an lvalue. */
5125 if (!allows_reg && allows_mem)
5127 tree inputv = TREE_VALUE (link);
5128 STRIP_NOPS (inputv);
5129 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5130 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5131 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5132 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5133 TREE_VALUE (link) = error_mark_node;
5134 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5135 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5136 mark_addressable (TREE_VALUE (link));
5137 if (tret == GS_ERROR)
5139 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5140 input_location = EXPR_LOCATION (TREE_VALUE (link));
5141 error ("memory input %d is not directly addressable", i);
5142 ret = tret;
5145 else
5147 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5148 is_gimple_asm_val, fb_rvalue);
5149 if (tret == GS_ERROR)
5150 ret = tret;
5153 TREE_CHAIN (link) = NULL_TREE;
5154 vec_safe_push (inputs, link);
5157 link_next = NULL_TREE;
5158 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5160 link_next = TREE_CHAIN (link);
5161 TREE_CHAIN (link) = NULL_TREE;
5162 vec_safe_push (clobbers, link);
5165 link_next = NULL_TREE;
5166 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5168 link_next = TREE_CHAIN (link);
5169 TREE_CHAIN (link) = NULL_TREE;
5170 vec_safe_push (labels, link);
5173 /* Do not add ASMs with errors to the gimple IL stream. */
5174 if (ret != GS_ERROR)
5176 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5177 inputs, outputs, clobbers, labels);
5179 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5180 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5182 gimplify_seq_add_stmt (pre_p, stmt);
5185 return ret;
5188 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5189 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5190 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5191 return to this function.
5193 FIXME should we complexify the prequeue handling instead? Or use flags
5194 for all the cleanups and let the optimizer tighten them up? The current
5195 code seems pretty fragile; it will break on a cleanup within any
5196 non-conditional nesting. But any such nesting would be broken, anyway;
5197 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5198 and continues out of it. We can do that at the RTL level, though, so
5199 having an optimizer to tighten up try/finally regions would be a Good
5200 Thing. */
5202 static enum gimplify_status
5203 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5205 gimple_stmt_iterator iter;
5206 gimple_seq body_sequence = NULL;
5208 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5210 /* We only care about the number of conditions between the innermost
5211 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5212 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5213 int old_conds = gimplify_ctxp->conditions;
5214 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5215 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5216 gimplify_ctxp->conditions = 0;
5217 gimplify_ctxp->conditional_cleanups = NULL;
5218 gimplify_ctxp->in_cleanup_point_expr = true;
5220 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5222 gimplify_ctxp->conditions = old_conds;
5223 gimplify_ctxp->conditional_cleanups = old_cleanups;
5224 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5226 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5228 gimple wce = gsi_stmt (iter);
5230 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5232 if (gsi_one_before_end_p (iter))
5234 /* Note that gsi_insert_seq_before and gsi_remove do not
5235 scan operands, unlike some other sequence mutators. */
5236 if (!gimple_wce_cleanup_eh_only (wce))
5237 gsi_insert_seq_before_without_update (&iter,
5238 gimple_wce_cleanup (wce),
5239 GSI_SAME_STMT);
5240 gsi_remove (&iter, true);
5241 break;
5243 else
5245 gimple_statement_try *gtry;
5246 gimple_seq seq;
5247 enum gimple_try_flags kind;
5249 if (gimple_wce_cleanup_eh_only (wce))
5250 kind = GIMPLE_TRY_CATCH;
5251 else
5252 kind = GIMPLE_TRY_FINALLY;
5253 seq = gsi_split_seq_after (iter);
5255 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5256 /* Do not use gsi_replace here, as it may scan operands.
5257 We want to do a simple structural modification only. */
5258 gsi_set_stmt (&iter, gtry);
5259 iter = gsi_start (gtry->eval);
5262 else
5263 gsi_next (&iter);
5266 gimplify_seq_add_seq (pre_p, body_sequence);
5267 if (temp)
5269 *expr_p = temp;
5270 return GS_OK;
5272 else
5274 *expr_p = NULL;
5275 return GS_ALL_DONE;
5279 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5280 is the cleanup action required. EH_ONLY is true if the cleanup should
5281 only be executed if an exception is thrown, not on normal exit. */
5283 static void
5284 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5286 gimple wce;
5287 gimple_seq cleanup_stmts = NULL;
5289 /* Errors can result in improperly nested cleanups. Which results in
5290 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5291 if (seen_error ())
5292 return;
5294 if (gimple_conditional_context ())
5296 /* If we're in a conditional context, this is more complex. We only
5297 want to run the cleanup if we actually ran the initialization that
5298 necessitates it, but we want to run it after the end of the
5299 conditional context. So we wrap the try/finally around the
5300 condition and use a flag to determine whether or not to actually
5301 run the destructor. Thus
5303 test ? f(A()) : 0
5305 becomes (approximately)
5307 flag = 0;
5308 try {
5309 if (test) { A::A(temp); flag = 1; val = f(temp); }
5310 else { val = 0; }
5311 } finally {
5312 if (flag) A::~A(temp);
5316 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5317 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5318 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5320 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5321 gimplify_stmt (&cleanup, &cleanup_stmts);
5322 wce = gimple_build_wce (cleanup_stmts);
5324 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5325 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5326 gimplify_seq_add_stmt (pre_p, ftrue);
5328 /* Because of this manipulation, and the EH edges that jump
5329 threading cannot redirect, the temporary (VAR) will appear
5330 to be used uninitialized. Don't warn. */
5331 TREE_NO_WARNING (var) = 1;
5333 else
5335 gimplify_stmt (&cleanup, &cleanup_stmts);
5336 wce = gimple_build_wce (cleanup_stmts);
5337 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5338 gimplify_seq_add_stmt (pre_p, wce);
5342 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5344 static enum gimplify_status
5345 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5347 tree targ = *expr_p;
5348 tree temp = TARGET_EXPR_SLOT (targ);
5349 tree init = TARGET_EXPR_INITIAL (targ);
5350 enum gimplify_status ret;
5352 if (init)
5354 tree cleanup = NULL_TREE;
5356 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5357 to the temps list. Handle also variable length TARGET_EXPRs. */
5358 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5360 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5361 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5362 gimplify_vla_decl (temp, pre_p);
5364 else
5365 gimple_add_tmp_var (temp);
5367 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5368 expression is supposed to initialize the slot. */
5369 if (VOID_TYPE_P (TREE_TYPE (init)))
5370 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5371 else
5373 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5374 init = init_expr;
5375 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5376 init = NULL;
5377 ggc_free (init_expr);
5379 if (ret == GS_ERROR)
5381 /* PR c++/28266 Make sure this is expanded only once. */
5382 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5383 return GS_ERROR;
5385 if (init)
5386 gimplify_and_add (init, pre_p);
5388 /* If needed, push the cleanup for the temp. */
5389 if (TARGET_EXPR_CLEANUP (targ))
5391 if (CLEANUP_EH_ONLY (targ))
5392 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5393 CLEANUP_EH_ONLY (targ), pre_p);
5394 else
5395 cleanup = TARGET_EXPR_CLEANUP (targ);
5398 /* Add a clobber for the temporary going out of scope, like
5399 gimplify_bind_expr. */
5400 if (gimplify_ctxp->in_cleanup_point_expr
5401 && needs_to_live_in_memory (temp)
5402 && flag_stack_reuse == SR_ALL)
5404 tree clobber = build_constructor (TREE_TYPE (temp),
5405 NULL);
5406 TREE_THIS_VOLATILE (clobber) = true;
5407 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5408 if (cleanup)
5409 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5410 clobber);
5411 else
5412 cleanup = clobber;
5415 if (cleanup)
5416 gimple_push_cleanup (temp, cleanup, false, pre_p);
5418 /* Only expand this once. */
5419 TREE_OPERAND (targ, 3) = init;
5420 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5422 else
5423 /* We should have expanded this before. */
5424 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5426 *expr_p = temp;
5427 return GS_OK;
5430 /* Gimplification of expression trees. */
5432 /* Gimplify an expression which appears at statement context. The
5433 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5434 NULL, a new sequence is allocated.
5436 Return true if we actually added a statement to the queue. */
5438 bool
5439 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5441 gimple_seq_node last;
5443 last = gimple_seq_last (*seq_p);
5444 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5445 return last != gimple_seq_last (*seq_p);
5448 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5449 to CTX. If entries already exist, force them to be some flavor of private.
5450 If there is no enclosing parallel, do nothing. */
5452 void
5453 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5455 splay_tree_node n;
5457 if (decl == NULL || !DECL_P (decl))
5458 return;
5462 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5463 if (n != NULL)
5465 if (n->value & GOVD_SHARED)
5466 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5467 else if (n->value & GOVD_MAP)
5468 n->value |= GOVD_MAP_TO_ONLY;
5469 else
5470 return;
5472 else if (ctx->region_type & ORT_TARGET)
5474 if (ctx->region_type & ORT_TARGET_OFFLOAD)
5475 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5477 else if (ctx->region_type != ORT_WORKSHARE
5478 && ctx->region_type != ORT_SIMD)
5479 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5481 ctx = ctx->outer_context;
5483 while (ctx);
5486 /* Similarly for each of the type sizes of TYPE. */
5488 static void
5489 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5491 if (type == NULL || type == error_mark_node)
5492 return;
5493 type = TYPE_MAIN_VARIANT (type);
5495 if (ctx->privatized_types->add (type))
5496 return;
5498 switch (TREE_CODE (type))
5500 case INTEGER_TYPE:
5501 case ENUMERAL_TYPE:
5502 case BOOLEAN_TYPE:
5503 case REAL_TYPE:
5504 case FIXED_POINT_TYPE:
5505 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5506 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5507 break;
5509 case ARRAY_TYPE:
5510 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5511 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5512 break;
5514 case RECORD_TYPE:
5515 case UNION_TYPE:
5516 case QUAL_UNION_TYPE:
5518 tree field;
5519 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5520 if (TREE_CODE (field) == FIELD_DECL)
5522 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5523 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5526 break;
5528 case POINTER_TYPE:
5529 case REFERENCE_TYPE:
5530 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5531 break;
5533 default:
5534 break;
5537 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5538 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5539 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5542 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5544 static void
5545 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5547 splay_tree_node n;
5548 unsigned int nflags;
5549 tree t;
5551 if (error_operand_p (decl))
5552 return;
5554 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5555 there are constructors involved somewhere. */
5556 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5557 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5558 flags |= GOVD_SEEN;
5560 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5561 if (n != NULL && n->value != GOVD_ALIGNED)
5563 /* We shouldn't be re-adding the decl with the same data
5564 sharing class. */
5565 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5566 /* The only combination of data sharing classes we should see is
5567 FIRSTPRIVATE and LASTPRIVATE. */
5568 nflags = n->value | flags;
5569 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5570 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5571 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5572 n->value = nflags;
5573 return;
5576 /* When adding a variable-sized variable, we have to handle all sorts
5577 of additional bits of data: the pointer replacement variable, and
5578 the parameters of the type. */
5579 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5581 /* Add the pointer replacement variable as PRIVATE if the variable
5582 replacement is private, else FIRSTPRIVATE since we'll need the
5583 address of the original variable either for SHARED, or for the
5584 copy into or out of the context. */
5585 if (!(flags & GOVD_LOCAL))
5587 if (flags & GOVD_MAP)
5589 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5590 #if 0
5591 /* Not sure if this is actually needed; haven't found a case
5592 where this would change anything; TODO. */
5593 if (flags & GOVD_MAP_FORCE)
5594 nflags |= OMP_CLAUSE_MAP_FORCE;
5595 #endif
5597 else if (flags & GOVD_PRIVATE)
5598 nflags = GOVD_PRIVATE;
5599 else
5600 nflags = GOVD_FIRSTPRIVATE;
5601 nflags |= flags & GOVD_SEEN;
5602 t = DECL_VALUE_EXPR (decl);
5603 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5604 t = TREE_OPERAND (t, 0);
5605 gcc_assert (DECL_P (t));
5606 omp_add_variable (ctx, t, nflags);
5609 /* Add all of the variable and type parameters (which should have
5610 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5611 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5612 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5613 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5615 /* The variable-sized variable itself is never SHARED, only some form
5616 of PRIVATE. The sharing would take place via the pointer variable
5617 which we remapped above. */
5618 if (flags & GOVD_SHARED)
5619 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5620 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5622 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5623 alloca statement we generate for the variable, so make sure it
5624 is available. This isn't automatically needed for the SHARED
5625 case, since we won't be allocating local storage then.
5626 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5627 in this case omp_notice_variable will be called later
5628 on when it is gimplified. */
5629 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5630 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5631 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5633 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5634 && lang_hooks.decls.omp_privatize_by_reference (decl))
5636 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5638 /* Similar to the direct variable sized case above, we'll need the
5639 size of references being privatized. */
5640 if ((flags & GOVD_SHARED) == 0)
5642 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5643 if (TREE_CODE (t) != INTEGER_CST)
5644 omp_notice_variable (ctx, t, true);
5648 if (n != NULL)
5649 n->value |= flags;
5650 else
5651 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5654 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5655 This just prints out diagnostics about threadprivate variable uses
5656 in untied tasks. If DECL2 is non-NULL, prevent this warning
5657 on that variable. */
5659 static bool
5660 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5661 tree decl2)
5663 splay_tree_node n;
5664 struct gimplify_omp_ctx *octx;
5666 for (octx = ctx; octx; octx = octx->outer_context)
5667 if ((octx->region_type & ORT_TARGET)
5668 && (octx->region_type & ORT_TARGET_OFFLOAD))
5670 gcc_assert (!(octx->region_type & ORT_TARGET_MAP_FORCE));
5672 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5673 if (n == NULL)
5675 error ("threadprivate variable %qE used in target region",
5676 DECL_NAME (decl));
5677 error_at (octx->location, "enclosing target region");
5678 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5680 if (decl2)
5681 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5684 if (ctx->region_type != ORT_UNTIED_TASK)
5685 return false;
5686 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5687 if (n == NULL)
5689 error ("threadprivate variable %qE used in untied task",
5690 DECL_NAME (decl));
5691 error_at (ctx->location, "enclosing task");
5692 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5694 if (decl2)
5695 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5696 return false;
5699 /* Record the fact that DECL was used within the OpenMP context CTX.
5700 IN_CODE is true when real code uses DECL, and false when we should
5701 merely emit default(none) errors. Return true if DECL is going to
5702 be remapped and thus DECL shouldn't be gimplified into its
5703 DECL_VALUE_EXPR (if any). */
5705 static bool
5706 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5708 splay_tree_node n;
5709 unsigned flags = in_code ? GOVD_SEEN : 0;
5710 bool ret = false, shared;
5712 if (error_operand_p (decl))
5713 return false;
5715 /* Threadprivate variables are predetermined. */
5716 if (is_global_var (decl))
5718 if (DECL_THREAD_LOCAL_P (decl))
5719 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5721 if (DECL_HAS_VALUE_EXPR_P (decl))
5723 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5725 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5726 return omp_notice_threadprivate_variable (ctx, decl, value);
5730 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5731 if ((ctx->region_type & ORT_TARGET)
5732 && (ctx->region_type & ORT_TARGET_OFFLOAD))
5734 unsigned map_force;
5735 if (ctx->region_type & ORT_TARGET_MAP_FORCE)
5736 map_force = GOVD_MAP_FORCE;
5737 else
5738 map_force = 0;
5739 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5740 if (n == NULL)
5742 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5744 error ("%qD referenced in target region does not have "
5745 "a mappable type", decl);
5746 omp_add_variable (ctx, decl, GOVD_MAP | map_force | GOVD_EXPLICIT | flags);
5748 else
5749 omp_add_variable (ctx, decl, GOVD_MAP | map_force | flags);
5751 else
5753 #if 0
5754 /* The following fails for:
5756 int l = 10;
5757 float c[l];
5758 #pragma acc parallel copy(c[2:4])
5760 #pragma acc parallel
5762 int t = sizeof c;
5766 ..., which we currently don't have to care about (nesting
5767 disabled), but eventually will have to; TODO. */
5768 if ((n->value & GOVD_MAP) && !(n->value & GOVD_EXPLICIT))
5769 gcc_assert ((n->value & GOVD_MAP_FORCE) == map_force);
5770 #endif
5772 /* If nothing changed, there's nothing left to do. */
5773 if ((n->value & flags) == flags)
5774 return ret;
5775 n->value |= flags;
5777 goto do_outer;
5780 if (n == NULL)
5782 enum omp_clause_default_kind default_kind, kind;
5783 struct gimplify_omp_ctx *octx;
5785 if (ctx->region_type == ORT_WORKSHARE
5786 || ctx->region_type == ORT_SIMD
5787 || ((ctx->region_type & ORT_TARGET)
5788 && !(ctx->region_type & ORT_TARGET_OFFLOAD)))
5789 goto do_outer;
5791 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5792 remapped firstprivate instead of shared. To some extent this is
5793 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5794 default_kind = ctx->default_kind;
5795 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5796 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5797 default_kind = kind;
5799 switch (default_kind)
5801 case OMP_CLAUSE_DEFAULT_NONE:
5802 if ((ctx->region_type & ORT_PARALLEL) != 0)
5804 error ("%qE not specified in enclosing parallel",
5805 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5806 error_at (ctx->location, "enclosing parallel");
5808 else if ((ctx->region_type & ORT_TASK) != 0)
5810 error ("%qE not specified in enclosing task",
5811 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5812 error_at (ctx->location, "enclosing task");
5814 else if (ctx->region_type == ORT_TEAMS)
5816 error ("%qE not specified in enclosing teams construct",
5817 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5818 error_at (ctx->location, "enclosing teams construct");
5820 else
5821 gcc_unreachable ();
5822 /* FALLTHRU */
5823 case OMP_CLAUSE_DEFAULT_SHARED:
5824 flags |= GOVD_SHARED;
5825 break;
5826 case OMP_CLAUSE_DEFAULT_PRIVATE:
5827 flags |= GOVD_PRIVATE;
5828 break;
5829 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5830 flags |= GOVD_FIRSTPRIVATE;
5831 break;
5832 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5833 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5834 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5835 if (ctx->outer_context)
5836 omp_notice_variable (ctx->outer_context, decl, in_code);
5837 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5839 splay_tree_node n2;
5841 if (octx->region_type & ORT_TARGET)
5842 continue;
5843 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5844 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5846 flags |= GOVD_FIRSTPRIVATE;
5847 break;
5849 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5850 break;
5852 if (flags & GOVD_FIRSTPRIVATE)
5853 break;
5854 if (octx == NULL
5855 && (TREE_CODE (decl) == PARM_DECL
5856 || (!is_global_var (decl)
5857 && DECL_CONTEXT (decl) == current_function_decl)))
5859 flags |= GOVD_FIRSTPRIVATE;
5860 break;
5862 flags |= GOVD_SHARED;
5863 break;
5864 default:
5865 gcc_unreachable ();
5868 if ((flags & GOVD_PRIVATE)
5869 && lang_hooks.decls.omp_private_outer_ref (decl))
5870 flags |= GOVD_PRIVATE_OUTER_REF;
5872 omp_add_variable (ctx, decl, flags);
5874 shared = (flags & GOVD_SHARED) != 0;
5875 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5876 goto do_outer;
5879 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5880 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5881 && DECL_SIZE (decl)
5882 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5884 splay_tree_node n2;
5885 tree t = DECL_VALUE_EXPR (decl);
5886 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5887 t = TREE_OPERAND (t, 0);
5888 gcc_assert (DECL_P (t));
5889 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5890 n2->value |= GOVD_SEEN;
5893 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5894 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5896 /* If nothing changed, there's nothing left to do. */
5897 if ((n->value & flags) == flags)
5898 return ret;
5899 flags |= n->value;
5900 n->value = flags;
5902 do_outer:
5903 /* If the variable is private in the current context, then we don't
5904 need to propagate anything to an outer context. */
5905 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5906 return ret;
5907 if (ctx->outer_context
5908 && omp_notice_variable (ctx->outer_context, decl, in_code))
5909 return true;
5910 return ret;
5913 /* Verify that DECL is private within CTX. If there's specific information
5914 to the contrary in the innermost scope, generate an error. */
5916 static bool
5917 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5919 splay_tree_node n;
5921 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5922 if (n != NULL)
5924 if (n->value & GOVD_SHARED)
5926 if (ctx == gimplify_omp_ctxp)
5928 if (simd)
5929 error ("iteration variable %qE is predetermined linear",
5930 DECL_NAME (decl));
5931 else
5932 error ("iteration variable %qE should be private",
5933 DECL_NAME (decl));
5934 n->value = GOVD_PRIVATE;
5935 return true;
5937 else
5938 return false;
5940 else if ((n->value & GOVD_EXPLICIT) != 0
5941 && (ctx == gimplify_omp_ctxp
5942 || (ctx->region_type == ORT_COMBINED_PARALLEL
5943 && gimplify_omp_ctxp->outer_context == ctx)))
5945 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5946 error ("iteration variable %qE should not be firstprivate",
5947 DECL_NAME (decl));
5948 else if ((n->value & GOVD_REDUCTION) != 0)
5949 error ("iteration variable %qE should not be reduction",
5950 DECL_NAME (decl));
5951 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5952 error ("iteration variable %qE should not be lastprivate",
5953 DECL_NAME (decl));
5954 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5955 error ("iteration variable %qE should not be private",
5956 DECL_NAME (decl));
5957 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5958 error ("iteration variable %qE is predetermined linear",
5959 DECL_NAME (decl));
5961 return (ctx == gimplify_omp_ctxp
5962 || (ctx->region_type == ORT_COMBINED_PARALLEL
5963 && gimplify_omp_ctxp->outer_context == ctx));
5966 if (ctx->region_type != ORT_WORKSHARE
5967 && ctx->region_type != ORT_SIMD)
5968 return false;
5969 else if (ctx->outer_context)
5970 return omp_is_private (ctx->outer_context, decl, simd);
5971 return false;
5974 /* Return true if DECL is private within a parallel region
5975 that binds to the current construct's context or in parallel
5976 region's REDUCTION clause. */
5978 static bool
5979 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5981 splay_tree_node n;
5985 ctx = ctx->outer_context;
5986 if (ctx == NULL)
5987 return !(is_global_var (decl)
5988 /* References might be private, but might be shared too,
5989 when checking for copyprivate, assume they might be
5990 private, otherwise assume they might be shared. */
5991 || (!copyprivate
5992 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5994 if (ctx->region_type & ORT_TARGET)
5995 continue;
5997 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5998 if (n != NULL)
5999 return (n->value & GOVD_SHARED) == 0;
6001 while (ctx->region_type == ORT_WORKSHARE
6002 || ctx->region_type == ORT_SIMD);
6003 return false;
6006 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6007 and previous omp contexts. */
6009 static void
6010 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6011 enum omp_region_type region_type)
6013 struct gimplify_omp_ctx *ctx, *outer_ctx;
6014 tree c;
6016 ctx = new_omp_context (region_type);
6017 outer_ctx = ctx->outer_context;
6019 while ((c = *list_p) != NULL)
6021 bool remove = false;
6022 bool notice_outer = true;
6023 const char *check_non_private = NULL;
6024 unsigned int flags;
6025 tree decl;
6027 switch (OMP_CLAUSE_CODE (c))
6029 case OMP_CLAUSE_PRIVATE:
6030 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6031 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6033 flags |= GOVD_PRIVATE_OUTER_REF;
6034 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6036 else
6037 notice_outer = false;
6038 goto do_add;
6039 case OMP_CLAUSE_SHARED:
6040 flags = GOVD_SHARED | GOVD_EXPLICIT;
6041 goto do_add;
6042 case OMP_CLAUSE_FIRSTPRIVATE:
6043 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6044 check_non_private = "firstprivate";
6045 goto do_add;
6046 case OMP_CLAUSE_LASTPRIVATE:
6047 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6048 check_non_private = "lastprivate";
6049 goto do_add;
6050 case OMP_CLAUSE_REDUCTION:
6051 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6052 check_non_private = "reduction";
6053 goto do_add;
6054 case OMP_CLAUSE_LINEAR:
6055 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6056 is_gimple_val, fb_rvalue) == GS_ERROR)
6058 remove = true;
6059 break;
6061 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6062 goto do_add;
6064 case OMP_CLAUSE_MAP:
6065 decl = OMP_CLAUSE_DECL (c);
6066 if (error_operand_p (decl))
6068 remove = true;
6069 break;
6071 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6072 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6073 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6074 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6075 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6077 remove = true;
6078 break;
6080 if (!DECL_P (decl))
6082 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6083 NULL, is_gimple_lvalue, fb_lvalue)
6084 == GS_ERROR)
6086 remove = true;
6087 break;
6089 break;
6091 flags = GOVD_MAP | GOVD_EXPLICIT;
6092 goto do_add;
6094 case OMP_CLAUSE_DEPEND:
6095 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6097 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6098 NULL, is_gimple_val, fb_rvalue);
6099 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6101 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6103 remove = true;
6104 break;
6106 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6107 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6108 is_gimple_val, fb_rvalue) == GS_ERROR)
6110 remove = true;
6111 break;
6113 break;
6115 case OMP_CLAUSE_TO:
6116 case OMP_CLAUSE_FROM:
6117 case OMP_CLAUSE__CACHE_:
6118 decl = OMP_CLAUSE_DECL (c);
6119 if (error_operand_p (decl))
6121 remove = true;
6122 break;
6124 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6125 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6126 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6127 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6128 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6130 remove = true;
6131 break;
6133 if (!DECL_P (decl))
6135 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6136 NULL, is_gimple_lvalue, fb_lvalue)
6137 == GS_ERROR)
6139 remove = true;
6140 break;
6142 break;
6144 goto do_notice;
6146 do_add:
6147 decl = OMP_CLAUSE_DECL (c);
6148 if (error_operand_p (decl))
6150 remove = true;
6151 break;
6153 omp_add_variable (ctx, decl, flags);
6154 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6155 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6157 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6158 GOVD_LOCAL | GOVD_SEEN);
6159 gimplify_omp_ctxp = ctx;
6160 push_gimplify_context ();
6162 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6163 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6165 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6166 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6167 pop_gimplify_context
6168 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6169 push_gimplify_context ();
6170 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6171 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6172 pop_gimplify_context
6173 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6174 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6175 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6177 gimplify_omp_ctxp = outer_ctx;
6179 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6180 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6182 gimplify_omp_ctxp = ctx;
6183 push_gimplify_context ();
6184 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6186 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6187 NULL, NULL);
6188 TREE_SIDE_EFFECTS (bind) = 1;
6189 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6190 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6192 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6193 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6194 pop_gimplify_context
6195 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6196 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6198 gimplify_omp_ctxp = outer_ctx;
6200 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6201 && OMP_CLAUSE_LINEAR_STMT (c))
6203 gimplify_omp_ctxp = ctx;
6204 push_gimplify_context ();
6205 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6207 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6208 NULL, NULL);
6209 TREE_SIDE_EFFECTS (bind) = 1;
6210 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6211 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6213 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6214 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6215 pop_gimplify_context
6216 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6217 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6219 gimplify_omp_ctxp = outer_ctx;
6221 if (notice_outer)
6222 goto do_notice;
6223 break;
6225 case OMP_CLAUSE_COPYIN:
6226 case OMP_CLAUSE_COPYPRIVATE:
6227 decl = OMP_CLAUSE_DECL (c);
6228 if (error_operand_p (decl))
6230 remove = true;
6231 break;
6233 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6234 && !remove
6235 && !omp_check_private (ctx, decl, true))
6237 remove = true;
6238 if (is_global_var (decl))
6240 if (DECL_THREAD_LOCAL_P (decl))
6241 remove = false;
6242 else if (DECL_HAS_VALUE_EXPR_P (decl))
6244 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6246 if (value
6247 && DECL_P (value)
6248 && DECL_THREAD_LOCAL_P (value))
6249 remove = false;
6252 if (remove)
6253 error_at (OMP_CLAUSE_LOCATION (c),
6254 "copyprivate variable %qE is not threadprivate"
6255 " or private in outer context", DECL_NAME (decl));
6257 do_notice:
6258 if (outer_ctx)
6259 omp_notice_variable (outer_ctx, decl, true);
6260 if (check_non_private
6261 && region_type == ORT_WORKSHARE
6262 && omp_check_private (ctx, decl, false))
6264 error ("%s variable %qE is private in outer context",
6265 check_non_private, DECL_NAME (decl));
6266 remove = true;
6268 break;
6270 case OMP_CLAUSE_FINAL:
6271 case OMP_CLAUSE_IF:
6272 OMP_CLAUSE_OPERAND (c, 0)
6273 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6274 /* Fall through. */
6276 case OMP_CLAUSE_SCHEDULE:
6277 case OMP_CLAUSE_NUM_THREADS:
6278 case OMP_CLAUSE_NUM_TEAMS:
6279 case OMP_CLAUSE_THREAD_LIMIT:
6280 case OMP_CLAUSE_DIST_SCHEDULE:
6281 case OMP_CLAUSE_DEVICE:
6282 case OMP_CLAUSE__CILK_FOR_COUNT_:
6283 case OMP_CLAUSE_NUM_GANGS:
6284 case OMP_CLAUSE_NUM_WORKERS:
6285 case OMP_CLAUSE_VECTOR_LENGTH:
6286 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6287 is_gimple_val, fb_rvalue) == GS_ERROR)
6288 remove = true;
6289 break;
6291 case OMP_CLAUSE_DEVICE_RESIDENT:
6292 case OMP_CLAUSE_USE_DEVICE:
6293 case OMP_CLAUSE_GANG:
6294 case OMP_CLAUSE_ASYNC:
6295 case OMP_CLAUSE_WAIT:
6296 case OMP_CLAUSE_INDEPENDENT:
6297 case OMP_CLAUSE_WORKER:
6298 case OMP_CLAUSE_VECTOR:
6299 remove = true;
6300 break;
6302 case OMP_CLAUSE_NOWAIT:
6303 case OMP_CLAUSE_ORDERED:
6304 case OMP_CLAUSE_UNTIED:
6305 case OMP_CLAUSE_COLLAPSE:
6306 case OMP_CLAUSE_MERGEABLE:
6307 case OMP_CLAUSE_PROC_BIND:
6308 case OMP_CLAUSE_SAFELEN:
6309 break;
6311 case OMP_CLAUSE_ALIGNED:
6312 decl = OMP_CLAUSE_DECL (c);
6313 if (error_operand_p (decl))
6315 remove = true;
6316 break;
6318 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6319 is_gimple_val, fb_rvalue) == GS_ERROR)
6321 remove = true;
6322 break;
6324 if (!is_global_var (decl)
6325 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6326 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6327 break;
6329 case OMP_CLAUSE_DEFAULT:
6330 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6331 break;
6333 default:
6334 gcc_unreachable ();
6337 if (remove)
6338 *list_p = OMP_CLAUSE_CHAIN (c);
6339 else
6340 list_p = &OMP_CLAUSE_CHAIN (c);
6343 gimplify_omp_ctxp = ctx;
6346 struct gimplify_adjust_omp_clauses_data
6348 tree *list_p;
6349 gimple_seq *pre_p;
6352 /* For all variables that were not actually used within the context,
6353 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6355 static int
6356 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6358 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6359 gimple_seq *pre_p
6360 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6361 tree decl = (tree) n->key;
6362 unsigned flags = n->value;
6363 enum omp_clause_code code;
6364 tree clause;
6365 bool private_debug;
6367 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6368 return 0;
6369 if ((flags & GOVD_SEEN) == 0)
6370 return 0;
6371 if (flags & GOVD_DEBUG_PRIVATE)
6373 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6374 private_debug = true;
6376 else if (flags & GOVD_MAP)
6377 private_debug = false;
6378 else
6379 private_debug
6380 = lang_hooks.decls.omp_private_debug_clause (decl,
6381 !!(flags & GOVD_SHARED));
6382 if (private_debug)
6383 code = OMP_CLAUSE_PRIVATE;
6384 else if (flags & GOVD_MAP)
6385 code = OMP_CLAUSE_MAP;
6386 else if (flags & GOVD_SHARED)
6388 if (is_global_var (decl))
6390 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6391 while (ctx != NULL)
6393 splay_tree_node on
6394 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6395 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6396 | GOVD_PRIVATE | GOVD_REDUCTION
6397 | GOVD_LINEAR | GOVD_MAP)) != 0)
6398 break;
6399 ctx = ctx->outer_context;
6401 if (ctx == NULL)
6402 return 0;
6404 code = OMP_CLAUSE_SHARED;
6406 else if (flags & GOVD_PRIVATE)
6407 code = OMP_CLAUSE_PRIVATE;
6408 else if (flags & GOVD_FIRSTPRIVATE)
6409 code = OMP_CLAUSE_FIRSTPRIVATE;
6410 else if (flags & GOVD_LASTPRIVATE)
6411 code = OMP_CLAUSE_LASTPRIVATE;
6412 else if (flags & GOVD_ALIGNED)
6413 return 0;
6414 else
6415 gcc_unreachable ();
6417 clause = build_omp_clause (input_location, code);
6418 OMP_CLAUSE_DECL (clause) = decl;
6419 OMP_CLAUSE_CHAIN (clause) = *list_p;
6420 if (private_debug)
6421 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6422 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6423 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6424 else if (code == OMP_CLAUSE_MAP)
6426 unsigned map_kind;
6427 map_kind = (flags & GOVD_MAP_TO_ONLY
6428 ? OMP_CLAUSE_MAP_TO
6429 : OMP_CLAUSE_MAP_TOFROM);
6430 if (flags & GOVD_MAP_FORCE)
6431 map_kind |= OMP_CLAUSE_MAP_FORCE;
6432 OMP_CLAUSE_MAP_KIND (clause) = (enum omp_clause_map_kind) map_kind;
6434 if (DECL_SIZE (decl)
6435 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6437 tree decl2 = DECL_VALUE_EXPR (decl);
6438 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6439 decl2 = TREE_OPERAND (decl2, 0);
6440 gcc_assert (DECL_P (decl2));
6441 tree mem = build_simple_mem_ref (decl2);
6442 OMP_CLAUSE_DECL (clause) = mem;
6443 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6444 if (gimplify_omp_ctxp->outer_context)
6446 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6447 omp_notice_variable (ctx, decl2, true);
6448 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6450 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6451 OMP_CLAUSE_MAP);
6452 OMP_CLAUSE_DECL (nc) = decl;
6453 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6454 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6455 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6456 OMP_CLAUSE_CHAIN (clause) = nc;
6458 else
6459 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6461 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6463 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6464 OMP_CLAUSE_DECL (nc) = decl;
6465 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6466 OMP_CLAUSE_CHAIN (nc) = *list_p;
6467 OMP_CLAUSE_CHAIN (clause) = nc;
6468 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6469 gimplify_omp_ctxp = ctx->outer_context;
6470 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6471 gimplify_omp_ctxp = ctx;
6473 *list_p = clause;
6474 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6475 gimplify_omp_ctxp = ctx->outer_context;
6476 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6477 gimplify_omp_ctxp = ctx;
6478 return 0;
6481 static void
6482 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6484 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6485 tree c, decl;
6487 while ((c = *list_p) != NULL)
6489 splay_tree_node n;
6490 bool remove = false;
6492 switch (OMP_CLAUSE_CODE (c))
6494 case OMP_CLAUSE_PRIVATE:
6495 case OMP_CLAUSE_SHARED:
6496 case OMP_CLAUSE_FIRSTPRIVATE:
6497 case OMP_CLAUSE_LINEAR:
6498 decl = OMP_CLAUSE_DECL (c);
6499 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6500 remove = !(n->value & GOVD_SEEN);
6501 if (! remove)
6503 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6504 if ((n->value & GOVD_DEBUG_PRIVATE)
6505 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6507 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6508 || ((n->value & GOVD_DATA_SHARE_CLASS)
6509 == GOVD_PRIVATE));
6510 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6511 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6513 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6514 && ctx->outer_context
6515 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6516 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6518 if (ctx->outer_context->combined_loop
6519 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6521 n = splay_tree_lookup (ctx->outer_context->variables,
6522 (splay_tree_key) decl);
6523 if (n == NULL
6524 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6526 int flags = GOVD_FIRSTPRIVATE;
6527 /* #pragma omp distribute does not allow
6528 lastprivate clause. */
6529 if (!ctx->outer_context->distribute)
6530 flags |= GOVD_LASTPRIVATE;
6531 if (n == NULL)
6532 omp_add_variable (ctx->outer_context, decl,
6533 flags | GOVD_SEEN);
6534 else
6535 n->value |= flags | GOVD_SEEN;
6538 else if (!is_global_var (decl))
6539 omp_notice_variable (ctx->outer_context, decl, true);
6542 break;
6544 case OMP_CLAUSE_LASTPRIVATE:
6545 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6546 accurately reflect the presence of a FIRSTPRIVATE clause. */
6547 decl = OMP_CLAUSE_DECL (c);
6548 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6549 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6550 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6551 break;
6553 case OMP_CLAUSE_ALIGNED:
6554 decl = OMP_CLAUSE_DECL (c);
6555 if (!is_global_var (decl))
6557 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6558 remove = n == NULL || !(n->value & GOVD_SEEN);
6559 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6561 struct gimplify_omp_ctx *octx;
6562 if (n != NULL
6563 && (n->value & (GOVD_DATA_SHARE_CLASS
6564 & ~GOVD_FIRSTPRIVATE)))
6565 remove = true;
6566 else
6567 for (octx = ctx->outer_context; octx;
6568 octx = octx->outer_context)
6570 n = splay_tree_lookup (octx->variables,
6571 (splay_tree_key) decl);
6572 if (n == NULL)
6573 continue;
6574 if (n->value & GOVD_LOCAL)
6575 break;
6576 /* We have to avoid assigning a shared variable
6577 to itself when trying to add
6578 __builtin_assume_aligned. */
6579 if (n->value & GOVD_SHARED)
6581 remove = true;
6582 break;
6587 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6589 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6590 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6591 remove = true;
6593 break;
6595 case OMP_CLAUSE_MAP:
6596 decl = OMP_CLAUSE_DECL (c);
6597 if (!DECL_P (decl))
6598 break;
6599 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6600 if ((ctx->region_type & ORT_TARGET)
6601 && (ctx->region_type & ORT_TARGET_OFFLOAD)
6602 && !(n->value & GOVD_SEEN))
6603 remove = true;
6604 else if (DECL_SIZE (decl)
6605 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6606 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6608 /* For OMP_CLAUSE_MAP_FORCE_DEVICEPTR, we'll never enter here,
6609 because for these, TREE_CODE (DECL_SIZE (decl)) will always be
6610 INTEGER_CST. */
6611 gcc_assert (OMP_CLAUSE_MAP_KIND (c)
6612 != OMP_CLAUSE_MAP_FORCE_DEVICEPTR);
6614 tree decl2 = DECL_VALUE_EXPR (decl);
6615 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6616 decl2 = TREE_OPERAND (decl2, 0);
6617 gcc_assert (DECL_P (decl2));
6618 tree mem = build_simple_mem_ref (decl2);
6619 OMP_CLAUSE_DECL (c) = mem;
6620 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6621 if (ctx->outer_context)
6623 omp_notice_variable (ctx->outer_context, decl2, true);
6624 omp_notice_variable (ctx->outer_context,
6625 OMP_CLAUSE_SIZE (c), true);
6627 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6628 OMP_CLAUSE_MAP);
6629 OMP_CLAUSE_DECL (nc) = decl;
6630 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6631 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6632 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6633 OMP_CLAUSE_CHAIN (c) = nc;
6634 c = nc;
6636 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6637 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6638 break;
6640 case OMP_CLAUSE_TO:
6641 case OMP_CLAUSE_FROM:
6642 case OMP_CLAUSE__CACHE_:
6643 decl = OMP_CLAUSE_DECL (c);
6644 if (!DECL_P (decl))
6645 break;
6646 if (DECL_SIZE (decl)
6647 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6649 tree decl2 = DECL_VALUE_EXPR (decl);
6650 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6651 decl2 = TREE_OPERAND (decl2, 0);
6652 gcc_assert (DECL_P (decl2));
6653 tree mem = build_simple_mem_ref (decl2);
6654 OMP_CLAUSE_DECL (c) = mem;
6655 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6656 if (ctx->outer_context)
6658 omp_notice_variable (ctx->outer_context, decl2, true);
6659 omp_notice_variable (ctx->outer_context,
6660 OMP_CLAUSE_SIZE (c), true);
6663 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6664 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6665 break;
6667 case OMP_CLAUSE_REDUCTION:
6668 case OMP_CLAUSE_COPYIN:
6669 case OMP_CLAUSE_COPYPRIVATE:
6670 case OMP_CLAUSE_IF:
6671 case OMP_CLAUSE_NUM_THREADS:
6672 case OMP_CLAUSE_NUM_TEAMS:
6673 case OMP_CLAUSE_THREAD_LIMIT:
6674 case OMP_CLAUSE_DIST_SCHEDULE:
6675 case OMP_CLAUSE_DEVICE:
6676 case OMP_CLAUSE_SCHEDULE:
6677 case OMP_CLAUSE_NOWAIT:
6678 case OMP_CLAUSE_ORDERED:
6679 case OMP_CLAUSE_DEFAULT:
6680 case OMP_CLAUSE_UNTIED:
6681 case OMP_CLAUSE_COLLAPSE:
6682 case OMP_CLAUSE_FINAL:
6683 case OMP_CLAUSE_MERGEABLE:
6684 case OMP_CLAUSE_PROC_BIND:
6685 case OMP_CLAUSE_SAFELEN:
6686 case OMP_CLAUSE_DEPEND:
6687 case OMP_CLAUSE__CILK_FOR_COUNT_:
6688 case OMP_CLAUSE_NUM_GANGS:
6689 case OMP_CLAUSE_NUM_WORKERS:
6690 case OMP_CLAUSE_VECTOR_LENGTH:
6691 break;
6693 case OMP_CLAUSE_DEVICE_RESIDENT:
6694 case OMP_CLAUSE_USE_DEVICE:
6695 case OMP_CLAUSE_GANG:
6696 case OMP_CLAUSE_ASYNC:
6697 case OMP_CLAUSE_WAIT:
6698 case OMP_CLAUSE_INDEPENDENT:
6699 case OMP_CLAUSE_WORKER:
6700 case OMP_CLAUSE_VECTOR:
6701 default:
6702 gcc_unreachable ();
6705 if (remove)
6706 *list_p = OMP_CLAUSE_CHAIN (c);
6707 else
6708 list_p = &OMP_CLAUSE_CHAIN (c);
6711 /* Add in any implicit data sharing. */
6712 struct gimplify_adjust_omp_clauses_data data;
6713 data.list_p = list_p;
6714 data.pre_p = pre_p;
6715 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6717 gimplify_omp_ctxp = ctx->outer_context;
6718 delete_omp_context (ctx);
6721 /* Gimplify OACC_CACHE. */
6723 static void
6724 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
6726 tree expr = *expr_p;
6728 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6729 gimplify_adjust_omp_clauses (pre_p, &OACC_CACHE_CLAUSES (expr));
6731 /* TODO: Do something sensible with this information. */
6733 *expr_p = NULL_TREE;
6736 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6737 gimplification of the body, as well as scanning the body for used
6738 variables. We need to do this scan now, because variable-sized
6739 decls will be decomposed during gimplification. */
6741 static void
6742 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6744 tree expr = *expr_p;
6745 gimple g;
6746 gimple_seq body = NULL;
6748 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6749 OMP_PARALLEL_COMBINED (expr)
6750 ? ORT_COMBINED_PARALLEL
6751 : ORT_PARALLEL);
6753 push_gimplify_context ();
6755 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6756 if (gimple_code (g) == GIMPLE_BIND)
6757 pop_gimplify_context (g);
6758 else
6759 pop_gimplify_context (NULL);
6761 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6763 g = gimple_build_omp_parallel (body,
6764 OMP_PARALLEL_CLAUSES (expr),
6765 NULL_TREE, NULL_TREE);
6766 if (OMP_PARALLEL_COMBINED (expr))
6767 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6768 gimplify_seq_add_stmt (pre_p, g);
6769 *expr_p = NULL_TREE;
6772 /* Gimplify the contents of an OMP_TASK statement. This involves
6773 gimplification of the body, as well as scanning the body for used
6774 variables. We need to do this scan now, because variable-sized
6775 decls will be decomposed during gimplification. */
6777 static void
6778 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6780 tree expr = *expr_p;
6781 gimple g;
6782 gimple_seq body = NULL;
6784 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6785 find_omp_clause (OMP_TASK_CLAUSES (expr),
6786 OMP_CLAUSE_UNTIED)
6787 ? ORT_UNTIED_TASK : ORT_TASK);
6789 push_gimplify_context ();
6791 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6792 if (gimple_code (g) == GIMPLE_BIND)
6793 pop_gimplify_context (g);
6794 else
6795 pop_gimplify_context (NULL);
6797 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6799 g = gimple_build_omp_task (body,
6800 OMP_TASK_CLAUSES (expr),
6801 NULL_TREE, NULL_TREE,
6802 NULL_TREE, NULL_TREE, NULL_TREE);
6803 gimplify_seq_add_stmt (pre_p, g);
6804 *expr_p = NULL_TREE;
6807 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6808 with non-NULL OMP_FOR_INIT. */
6810 static tree
6811 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6813 *walk_subtrees = 0;
6814 switch (TREE_CODE (*tp))
6816 case OMP_FOR:
6817 *walk_subtrees = 1;
6818 /* FALLTHRU */
6819 case OMP_SIMD:
6820 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6821 return *tp;
6822 break;
6823 case BIND_EXPR:
6824 case STATEMENT_LIST:
6825 case OMP_PARALLEL:
6826 *walk_subtrees = 1;
6827 break;
6828 default:
6829 break;
6831 return NULL_TREE;
6834 /* Gimplify the gross structure of an OMP_FOR statement. */
6836 static enum gimplify_status
6837 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6839 tree for_stmt, orig_for_stmt, decl, var, t;
6840 enum gimplify_status ret = GS_ALL_DONE;
6841 enum gimplify_status tret;
6842 gimple gfor;
6843 gimple_seq for_body, for_pre_body;
6844 int i;
6845 bool simd;
6846 enum gimplify_omp_var_data govd_private;
6847 enum omp_region_type ort;
6848 bitmap has_decl_expr = NULL;
6850 orig_for_stmt = for_stmt = *expr_p;
6852 switch (TREE_CODE (for_stmt))
6854 case OMP_FOR:
6855 case CILK_FOR:
6856 case OMP_DISTRIBUTE:
6857 simd = false;
6858 govd_private = GOVD_PRIVATE;
6859 ort = ORT_WORKSHARE;
6860 break;
6861 case OMP_SIMD:
6862 case CILK_SIMD:
6863 simd = true;
6864 govd_private = GOVD_PRIVATE;
6865 ort = ORT_SIMD;
6866 break;
6867 case OACC_LOOP:
6868 simd = false;
6869 govd_private = /* TODO */ GOVD_LOCAL;
6870 ort = /* TODO */ ORT_WORKSHARE;
6871 break;
6872 default:
6873 gcc_unreachable ();
6876 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort);
6877 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6878 gimplify_omp_ctxp->distribute = true;
6880 /* Handle OMP_FOR_INIT. */
6881 for_pre_body = NULL;
6882 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6884 has_decl_expr = BITMAP_ALLOC (NULL);
6885 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6886 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6887 == VAR_DECL)
6889 t = OMP_FOR_PRE_BODY (for_stmt);
6890 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6892 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6894 tree_stmt_iterator si;
6895 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6896 tsi_next (&si))
6898 t = tsi_stmt (si);
6899 if (TREE_CODE (t) == DECL_EXPR
6900 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6901 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6905 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6906 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6908 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6910 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
6911 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6912 NULL, NULL);
6913 gcc_assert (for_stmt != NULL_TREE);
6914 gimplify_omp_ctxp->combined_loop = true;
6917 for_body = NULL;
6918 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6919 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6920 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6921 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6922 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6924 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6925 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6926 decl = TREE_OPERAND (t, 0);
6927 gcc_assert (DECL_P (decl));
6928 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6929 || POINTER_TYPE_P (TREE_TYPE (decl)));
6931 /* Make sure the iteration variable is some kind of private. */
6932 tree c = NULL_TREE;
6933 tree c2 = NULL_TREE;
6934 if (orig_for_stmt != for_stmt)
6935 /* Do this only on innermost construct for combined ones. */;
6936 else if (simd)
6938 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6939 (splay_tree_key)decl);
6940 omp_is_private (gimplify_omp_ctxp, decl,
6941 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6942 != 1));
6943 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6944 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6945 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6947 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6948 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6949 if (has_decl_expr
6950 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6951 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6952 OMP_CLAUSE_DECL (c) = decl;
6953 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6954 OMP_FOR_CLAUSES (for_stmt) = c;
6955 omp_add_variable (gimplify_omp_ctxp, decl,
6956 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6958 else
6960 gcc_assert (govd_private == GOVD_PRIVATE);
6961 bool lastprivate
6962 = (!has_decl_expr
6963 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6964 if (lastprivate
6965 && gimplify_omp_ctxp->outer_context
6966 && gimplify_omp_ctxp->outer_context->region_type
6967 == ORT_WORKSHARE
6968 && gimplify_omp_ctxp->outer_context->combined_loop
6969 && !gimplify_omp_ctxp->outer_context->distribute)
6971 struct gimplify_omp_ctx *outer
6972 = gimplify_omp_ctxp->outer_context;
6973 n = splay_tree_lookup (outer->variables,
6974 (splay_tree_key) decl);
6975 if (n != NULL
6976 && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6977 lastprivate = false;
6978 else if (omp_check_private (outer, decl, false))
6979 error ("lastprivate variable %qE is private in outer "
6980 "context", DECL_NAME (decl));
6981 else
6983 omp_add_variable (outer, decl,
6984 GOVD_LASTPRIVATE | GOVD_SEEN);
6985 if (outer->outer_context)
6986 omp_notice_variable (outer->outer_context, decl, true);
6989 c = build_omp_clause (input_location,
6990 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6991 : OMP_CLAUSE_PRIVATE);
6992 OMP_CLAUSE_DECL (c) = decl;
6993 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6994 OMP_FOR_CLAUSES (for_stmt) = c;
6995 omp_add_variable (gimplify_omp_ctxp, decl,
6996 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6997 | GOVD_EXPLICIT | GOVD_SEEN);
6998 c = NULL_TREE;
7001 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
7002 omp_notice_variable (gimplify_omp_ctxp, decl, true);
7003 else
7004 omp_add_variable (gimplify_omp_ctxp, decl, govd_private | GOVD_SEEN);
7006 /* If DECL is not a gimple register, create a temporary variable to act
7007 as an iteration counter. This is valid, since DECL cannot be
7008 modified in the body of the loop. Similarly for any iteration vars
7009 in simd with collapse > 1 where the iterator vars must be
7010 lastprivate. */
7011 if (orig_for_stmt != for_stmt)
7012 var = decl;
7013 else if (!is_gimple_reg (decl)
7014 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
7016 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7017 TREE_OPERAND (t, 0) = var;
7019 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
7021 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7023 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7024 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
7025 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
7026 OMP_CLAUSE_DECL (c2) = var;
7027 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
7028 OMP_FOR_CLAUSES (for_stmt) = c2;
7029 omp_add_variable (gimplify_omp_ctxp, var,
7030 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
7031 if (c == NULL_TREE)
7033 c = c2;
7034 c2 = NULL_TREE;
7037 else
7038 omp_add_variable (gimplify_omp_ctxp, var,
7039 govd_private | GOVD_SEEN);
7041 else
7042 var = decl;
7044 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7045 is_gimple_val, fb_rvalue);
7046 ret = MIN (ret, tret);
7047 if (ret == GS_ERROR)
7048 return ret;
7050 /* Handle OMP_FOR_COND. */
7051 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7052 gcc_assert (COMPARISON_CLASS_P (t));
7053 gcc_assert (TREE_OPERAND (t, 0) == decl);
7055 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7056 is_gimple_val, fb_rvalue);
7057 ret = MIN (ret, tret);
7059 /* Handle OMP_FOR_INCR. */
7060 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7061 switch (TREE_CODE (t))
7063 case PREINCREMENT_EXPR:
7064 case POSTINCREMENT_EXPR:
7066 tree decl = TREE_OPERAND (t, 0);
7067 /* c_omp_for_incr_canonicalize_ptr() should have been
7068 called to massage things appropriately. */
7069 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7071 if (orig_for_stmt != for_stmt)
7072 break;
7073 t = build_int_cst (TREE_TYPE (decl), 1);
7074 if (c)
7075 OMP_CLAUSE_LINEAR_STEP (c) = t;
7076 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7077 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7078 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7079 break;
7082 case PREDECREMENT_EXPR:
7083 case POSTDECREMENT_EXPR:
7084 /* c_omp_for_incr_canonicalize_ptr() should have been
7085 called to massage things appropriately. */
7086 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7087 if (orig_for_stmt != for_stmt)
7088 break;
7089 t = build_int_cst (TREE_TYPE (decl), -1);
7090 if (c)
7091 OMP_CLAUSE_LINEAR_STEP (c) = t;
7092 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7093 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7094 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7095 break;
7097 case MODIFY_EXPR:
7098 gcc_assert (TREE_OPERAND (t, 0) == decl);
7099 TREE_OPERAND (t, 0) = var;
7101 t = TREE_OPERAND (t, 1);
7102 switch (TREE_CODE (t))
7104 case PLUS_EXPR:
7105 if (TREE_OPERAND (t, 1) == decl)
7107 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
7108 TREE_OPERAND (t, 0) = var;
7109 break;
7112 /* Fallthru. */
7113 case MINUS_EXPR:
7114 case POINTER_PLUS_EXPR:
7115 gcc_assert (TREE_OPERAND (t, 0) == decl);
7116 TREE_OPERAND (t, 0) = var;
7117 break;
7118 default:
7119 gcc_unreachable ();
7122 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7123 is_gimple_val, fb_rvalue);
7124 ret = MIN (ret, tret);
7125 if (c)
7127 tree step = TREE_OPERAND (t, 1);
7128 tree stept = TREE_TYPE (decl);
7129 if (POINTER_TYPE_P (stept))
7130 stept = sizetype;
7131 step = fold_convert (stept, step);
7132 if (TREE_CODE (t) == MINUS_EXPR)
7133 step = fold_build1 (NEGATE_EXPR, stept, step);
7134 OMP_CLAUSE_LINEAR_STEP (c) = step;
7135 if (step != TREE_OPERAND (t, 1))
7137 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7138 &for_pre_body, NULL,
7139 is_gimple_val, fb_rvalue);
7140 ret = MIN (ret, tret);
7143 break;
7145 default:
7146 gcc_unreachable ();
7149 if (c2)
7151 gcc_assert (c);
7152 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7155 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7156 && orig_for_stmt == for_stmt)
7158 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7159 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7160 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7161 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7162 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7163 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7164 && OMP_CLAUSE_DECL (c) == decl)
7166 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7167 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7168 gcc_assert (TREE_OPERAND (t, 0) == var);
7169 t = TREE_OPERAND (t, 1);
7170 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7171 || TREE_CODE (t) == MINUS_EXPR
7172 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7173 gcc_assert (TREE_OPERAND (t, 0) == var);
7174 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7175 TREE_OPERAND (t, 1));
7176 gimple_seq *seq;
7177 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7178 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7179 else
7180 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7181 gimplify_assign (decl, t, seq);
7186 BITMAP_FREE (has_decl_expr);
7188 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7190 if (orig_for_stmt != for_stmt)
7191 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7193 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7194 decl = TREE_OPERAND (t, 0);
7195 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7196 omp_add_variable (gimplify_omp_ctxp, var, govd_private | GOVD_SEEN);
7197 TREE_OPERAND (t, 0) = var;
7198 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7199 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7200 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7203 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7205 int kind;
7206 switch (TREE_CODE (orig_for_stmt))
7208 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7209 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7210 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7211 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7212 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7213 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
7214 default:
7215 gcc_unreachable ();
7217 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7218 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7219 for_pre_body);
7220 if (orig_for_stmt != for_stmt)
7221 gimple_omp_for_set_combined_p (gfor, true);
7222 if (gimplify_omp_ctxp
7223 && (gimplify_omp_ctxp->combined_loop
7224 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7225 && gimplify_omp_ctxp->outer_context
7226 && gimplify_omp_ctxp->outer_context->combined_loop)))
7228 gimple_omp_for_set_combined_into_p (gfor, true);
7229 if (gimplify_omp_ctxp->combined_loop)
7230 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7231 else
7232 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7235 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7237 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7238 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7239 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7240 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7241 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7242 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7243 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7244 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7247 gimplify_seq_add_stmt (pre_p, gfor);
7248 if (ret != GS_ALL_DONE)
7249 return GS_ERROR;
7250 *expr_p = NULL_TREE;
7251 return GS_ALL_DONE;
7254 /* Gimplify the gross structure of several OpenACC or OpenMP constructs. */
7256 static void
7257 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7259 tree expr = *expr_p;
7260 gimple stmt;
7261 gimple_seq body = NULL;
7262 enum omp_region_type ort;
7264 switch (TREE_CODE (expr))
7266 case OACC_DATA:
7267 ort = (enum omp_region_type) (ORT_TARGET
7268 | ORT_TARGET_MAP_FORCE);
7269 break;
7270 case OACC_KERNELS:
7271 case OACC_PARALLEL:
7272 ort = (enum omp_region_type) (ORT_TARGET
7273 | ORT_TARGET_OFFLOAD
7274 | ORT_TARGET_MAP_FORCE);
7275 break;
7276 case OMP_SECTIONS:
7277 case OMP_SINGLE:
7278 ort = ORT_WORKSHARE;
7279 break;
7280 case OMP_TARGET:
7281 ort = (enum omp_region_type) (ORT_TARGET | ORT_TARGET_OFFLOAD);
7282 break;
7283 case OMP_TARGET_DATA:
7284 ort = ORT_TARGET;
7285 break;
7286 case OMP_TEAMS:
7287 ort = ORT_TEAMS;
7288 break;
7289 default:
7290 gcc_unreachable ();
7292 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7293 if (ort & ORT_TARGET)
7295 push_gimplify_context ();
7296 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7297 if (gimple_code (g) == GIMPLE_BIND)
7298 pop_gimplify_context (g);
7299 else
7300 pop_gimplify_context (NULL);
7301 if (!(ort & ORT_TARGET_OFFLOAD))
7303 enum built_in_function end_ix;
7304 switch (TREE_CODE (expr))
7306 case OACC_DATA:
7307 end_ix = BUILT_IN_GOACC_DATA_END;
7308 break;
7309 case OMP_TARGET_DATA:
7310 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
7311 break;
7312 default:
7313 gcc_unreachable ();
7315 tree fn = builtin_decl_explicit (end_ix);
7316 g = gimple_build_call (fn, 0);
7317 gimple_seq cleanup = NULL;
7318 gimple_seq_add_stmt (&cleanup, g);
7319 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7320 body = NULL;
7321 gimple_seq_add_stmt (&body, g);
7324 else
7325 gimplify_and_add (OMP_BODY (expr), &body);
7326 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7328 switch (TREE_CODE (expr))
7330 case OACC_DATA:
7331 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
7332 OACC_DATA_CLAUSES (expr));
7333 break;
7334 case OACC_KERNELS:
7335 stmt = gimple_build_oacc_kernels (body, OACC_KERNELS_CLAUSES (expr));
7336 break;
7337 case OACC_PARALLEL:
7338 stmt = gimple_build_oacc_parallel (body, OACC_PARALLEL_CLAUSES (expr));
7339 break;
7340 case OMP_SECTIONS:
7341 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7342 break;
7343 case OMP_SINGLE:
7344 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7345 break;
7346 case OMP_TARGET:
7347 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7348 OMP_CLAUSES (expr));
7349 break;
7350 case OMP_TARGET_DATA:
7351 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7352 OMP_CLAUSES (expr));
7353 break;
7354 case OMP_TEAMS:
7355 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7356 break;
7357 default:
7358 gcc_unreachable ();
7361 gimplify_seq_add_stmt (pre_p, stmt);
7362 *expr_p = NULL_TREE;
7365 /* Gimplify the gross structure of OpenACC update and OpenMP target update
7366 constructs. */
7368 static void
7369 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7371 tree expr = *expr_p, clauses;
7372 int kind;
7373 gimple stmt;
7375 switch (TREE_CODE (expr))
7377 case OACC_ENTER_DATA:
7378 clauses = OACC_ENTER_DATA_CLAUSES (expr);
7379 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7380 break;
7381 case OACC_EXIT_DATA:
7382 clauses = OACC_EXIT_DATA_CLAUSES (expr);
7383 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7384 break;
7385 case OACC_UPDATE:
7386 clauses = OACC_UPDATE_CLAUSES (expr);
7387 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
7388 break;
7389 case OMP_TARGET_UPDATE:
7390 clauses = OMP_TARGET_UPDATE_CLAUSES (expr);
7391 kind = GF_OMP_TARGET_KIND_UPDATE;
7392 break;
7393 default:
7394 gcc_unreachable ();
7396 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_WORKSHARE);
7397 gimplify_adjust_omp_clauses (pre_p, &clauses);
7398 stmt = gimple_build_omp_target (NULL, kind, clauses);
7400 gimplify_seq_add_stmt (pre_p, stmt);
7401 *expr_p = NULL_TREE;
7404 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7405 stabilized the lhs of the atomic operation as *ADDR. Return true if
7406 EXPR is this stabilized form. */
7408 static bool
7409 goa_lhs_expr_p (tree expr, tree addr)
7411 /* Also include casts to other type variants. The C front end is fond
7412 of adding these for e.g. volatile variables. This is like
7413 STRIP_TYPE_NOPS but includes the main variant lookup. */
7414 STRIP_USELESS_TYPE_CONVERSION (expr);
7416 if (TREE_CODE (expr) == INDIRECT_REF)
7418 expr = TREE_OPERAND (expr, 0);
7419 while (expr != addr
7420 && (CONVERT_EXPR_P (expr)
7421 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7422 && TREE_CODE (expr) == TREE_CODE (addr)
7423 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7425 expr = TREE_OPERAND (expr, 0);
7426 addr = TREE_OPERAND (addr, 0);
7428 if (expr == addr)
7429 return true;
7430 return (TREE_CODE (addr) == ADDR_EXPR
7431 && TREE_CODE (expr) == ADDR_EXPR
7432 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7434 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7435 return true;
7436 return false;
7439 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7440 expression does not involve the lhs, evaluate it into a temporary.
7441 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7442 or -1 if an error was encountered. */
7444 static int
7445 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7446 tree lhs_var)
7448 tree expr = *expr_p;
7449 int saw_lhs;
7451 if (goa_lhs_expr_p (expr, lhs_addr))
7453 *expr_p = lhs_var;
7454 return 1;
7456 if (is_gimple_val (expr))
7457 return 0;
7459 saw_lhs = 0;
7460 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7462 case tcc_binary:
7463 case tcc_comparison:
7464 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7465 lhs_var);
7466 case tcc_unary:
7467 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7468 lhs_var);
7469 break;
7470 case tcc_expression:
7471 switch (TREE_CODE (expr))
7473 case TRUTH_ANDIF_EXPR:
7474 case TRUTH_ORIF_EXPR:
7475 case TRUTH_AND_EXPR:
7476 case TRUTH_OR_EXPR:
7477 case TRUTH_XOR_EXPR:
7478 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7479 lhs_addr, lhs_var);
7480 case TRUTH_NOT_EXPR:
7481 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7482 lhs_addr, lhs_var);
7483 break;
7484 case COMPOUND_EXPR:
7485 /* Break out any preevaluations from cp_build_modify_expr. */
7486 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7487 expr = TREE_OPERAND (expr, 1))
7488 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7489 *expr_p = expr;
7490 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7491 default:
7492 break;
7494 break;
7495 default:
7496 break;
7499 if (saw_lhs == 0)
7501 enum gimplify_status gs;
7502 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7503 if (gs != GS_ALL_DONE)
7504 saw_lhs = -1;
7507 return saw_lhs;
7510 /* Gimplify an OMP_ATOMIC statement. */
7512 static enum gimplify_status
7513 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7515 tree addr = TREE_OPERAND (*expr_p, 0);
7516 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7517 ? NULL : TREE_OPERAND (*expr_p, 1);
7518 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7519 tree tmp_load;
7520 gimple loadstmt, storestmt;
7522 tmp_load = create_tmp_reg (type, NULL);
7523 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7524 return GS_ERROR;
7526 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7527 != GS_ALL_DONE)
7528 return GS_ERROR;
7530 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7531 gimplify_seq_add_stmt (pre_p, loadstmt);
7532 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7533 != GS_ALL_DONE)
7534 return GS_ERROR;
7536 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7537 rhs = tmp_load;
7538 storestmt = gimple_build_omp_atomic_store (rhs);
7539 gimplify_seq_add_stmt (pre_p, storestmt);
7540 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7542 gimple_omp_atomic_set_seq_cst (loadstmt);
7543 gimple_omp_atomic_set_seq_cst (storestmt);
7545 switch (TREE_CODE (*expr_p))
7547 case OMP_ATOMIC_READ:
7548 case OMP_ATOMIC_CAPTURE_OLD:
7549 *expr_p = tmp_load;
7550 gimple_omp_atomic_set_need_value (loadstmt);
7551 break;
7552 case OMP_ATOMIC_CAPTURE_NEW:
7553 *expr_p = rhs;
7554 gimple_omp_atomic_set_need_value (storestmt);
7555 break;
7556 default:
7557 *expr_p = NULL;
7558 break;
7561 return GS_ALL_DONE;
7564 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7565 body, and adding some EH bits. */
7567 static enum gimplify_status
7568 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7570 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7571 gimple g;
7572 gimple_seq body = NULL;
7573 int subcode = 0;
7575 /* Wrap the transaction body in a BIND_EXPR so we have a context
7576 where to put decls for OpenMP. */
7577 if (TREE_CODE (tbody) != BIND_EXPR)
7579 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7580 TREE_SIDE_EFFECTS (bind) = 1;
7581 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7582 TRANSACTION_EXPR_BODY (expr) = bind;
7585 push_gimplify_context ();
7586 temp = voidify_wrapper_expr (*expr_p, NULL);
7588 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7589 pop_gimplify_context (g);
7591 g = gimple_build_transaction (body, NULL);
7592 if (TRANSACTION_EXPR_OUTER (expr))
7593 subcode = GTMA_IS_OUTER;
7594 else if (TRANSACTION_EXPR_RELAXED (expr))
7595 subcode = GTMA_IS_RELAXED;
7596 gimple_transaction_set_subcode (g, subcode);
7598 gimplify_seq_add_stmt (pre_p, g);
7600 if (temp)
7602 *expr_p = temp;
7603 return GS_OK;
7606 *expr_p = NULL_TREE;
7607 return GS_ALL_DONE;
7610 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7611 expression produces a value to be used as an operand inside a GIMPLE
7612 statement, the value will be stored back in *EXPR_P. This value will
7613 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7614 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7615 emitted in PRE_P and POST_P.
7617 Additionally, this process may overwrite parts of the input
7618 expression during gimplification. Ideally, it should be
7619 possible to do non-destructive gimplification.
7621 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7622 the expression needs to evaluate to a value to be used as
7623 an operand in a GIMPLE statement, this value will be stored in
7624 *EXPR_P on exit. This happens when the caller specifies one
7625 of fb_lvalue or fb_rvalue fallback flags.
7627 PRE_P will contain the sequence of GIMPLE statements corresponding
7628 to the evaluation of EXPR and all the side-effects that must
7629 be executed before the main expression. On exit, the last
7630 statement of PRE_P is the core statement being gimplified. For
7631 instance, when gimplifying 'if (++a)' the last statement in
7632 PRE_P will be 'if (t.1)' where t.1 is the result of
7633 pre-incrementing 'a'.
7635 POST_P will contain the sequence of GIMPLE statements corresponding
7636 to the evaluation of all the side-effects that must be executed
7637 after the main expression. If this is NULL, the post
7638 side-effects are stored at the end of PRE_P.
7640 The reason why the output is split in two is to handle post
7641 side-effects explicitly. In some cases, an expression may have
7642 inner and outer post side-effects which need to be emitted in
7643 an order different from the one given by the recursive
7644 traversal. For instance, for the expression (*p--)++ the post
7645 side-effects of '--' must actually occur *after* the post
7646 side-effects of '++'. However, gimplification will first visit
7647 the inner expression, so if a separate POST sequence was not
7648 used, the resulting sequence would be:
7650 1 t.1 = *p
7651 2 p = p - 1
7652 3 t.2 = t.1 + 1
7653 4 *p = t.2
7655 However, the post-decrement operation in line #2 must not be
7656 evaluated until after the store to *p at line #4, so the
7657 correct sequence should be:
7659 1 t.1 = *p
7660 2 t.2 = t.1 + 1
7661 3 *p = t.2
7662 4 p = p - 1
7664 So, by specifying a separate post queue, it is possible
7665 to emit the post side-effects in the correct order.
7666 If POST_P is NULL, an internal queue will be used. Before
7667 returning to the caller, the sequence POST_P is appended to
7668 the main output sequence PRE_P.
7670 GIMPLE_TEST_F points to a function that takes a tree T and
7671 returns nonzero if T is in the GIMPLE form requested by the
7672 caller. The GIMPLE predicates are in gimple.c.
7674 FALLBACK tells the function what sort of a temporary we want if
7675 gimplification cannot produce an expression that complies with
7676 GIMPLE_TEST_F.
7678 fb_none means that no temporary should be generated
7679 fb_rvalue means that an rvalue is OK to generate
7680 fb_lvalue means that an lvalue is OK to generate
7681 fb_either means that either is OK, but an lvalue is preferable.
7682 fb_mayfail means that gimplification may fail (in which case
7683 GS_ERROR will be returned)
7685 The return value is either GS_ERROR or GS_ALL_DONE, since this
7686 function iterates until EXPR is completely gimplified or an error
7687 occurs. */
7689 enum gimplify_status
7690 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7691 bool (*gimple_test_f) (tree), fallback_t fallback)
7693 tree tmp;
7694 gimple_seq internal_pre = NULL;
7695 gimple_seq internal_post = NULL;
7696 tree save_expr;
7697 bool is_statement;
7698 location_t saved_location;
7699 enum gimplify_status ret;
7700 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7702 save_expr = *expr_p;
7703 if (save_expr == NULL_TREE)
7704 return GS_ALL_DONE;
7706 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7707 is_statement = gimple_test_f == is_gimple_stmt;
7708 if (is_statement)
7709 gcc_assert (pre_p);
7711 /* Consistency checks. */
7712 if (gimple_test_f == is_gimple_reg)
7713 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7714 else if (gimple_test_f == is_gimple_val
7715 || gimple_test_f == is_gimple_call_addr
7716 || gimple_test_f == is_gimple_condexpr
7717 || gimple_test_f == is_gimple_mem_rhs
7718 || gimple_test_f == is_gimple_mem_rhs_or_call
7719 || gimple_test_f == is_gimple_reg_rhs
7720 || gimple_test_f == is_gimple_reg_rhs_or_call
7721 || gimple_test_f == is_gimple_asm_val
7722 || gimple_test_f == is_gimple_mem_ref_addr)
7723 gcc_assert (fallback & fb_rvalue);
7724 else if (gimple_test_f == is_gimple_min_lval
7725 || gimple_test_f == is_gimple_lvalue)
7726 gcc_assert (fallback & fb_lvalue);
7727 else if (gimple_test_f == is_gimple_addressable)
7728 gcc_assert (fallback & fb_either);
7729 else if (gimple_test_f == is_gimple_stmt)
7730 gcc_assert (fallback == fb_none);
7731 else
7733 /* We should have recognized the GIMPLE_TEST_F predicate to
7734 know what kind of fallback to use in case a temporary is
7735 needed to hold the value or address of *EXPR_P. */
7736 gcc_unreachable ();
7739 /* We used to check the predicate here and return immediately if it
7740 succeeds. This is wrong; the design is for gimplification to be
7741 idempotent, and for the predicates to only test for valid forms, not
7742 whether they are fully simplified. */
7743 if (pre_p == NULL)
7744 pre_p = &internal_pre;
7746 if (post_p == NULL)
7747 post_p = &internal_post;
7749 /* Remember the last statements added to PRE_P and POST_P. Every
7750 new statement added by the gimplification helpers needs to be
7751 annotated with location information. To centralize the
7752 responsibility, we remember the last statement that had been
7753 added to both queues before gimplifying *EXPR_P. If
7754 gimplification produces new statements in PRE_P and POST_P, those
7755 statements will be annotated with the same location information
7756 as *EXPR_P. */
7757 pre_last_gsi = gsi_last (*pre_p);
7758 post_last_gsi = gsi_last (*post_p);
7760 saved_location = input_location;
7761 if (save_expr != error_mark_node
7762 && EXPR_HAS_LOCATION (*expr_p))
7763 input_location = EXPR_LOCATION (*expr_p);
7765 /* Loop over the specific gimplifiers until the toplevel node
7766 remains the same. */
7769 /* Strip away as many useless type conversions as possible
7770 at the toplevel. */
7771 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7773 /* Remember the expr. */
7774 save_expr = *expr_p;
7776 /* Die, die, die, my darling. */
7777 if (save_expr == error_mark_node
7778 || (TREE_TYPE (save_expr)
7779 && TREE_TYPE (save_expr) == error_mark_node))
7781 ret = GS_ERROR;
7782 break;
7785 /* Do any language-specific gimplification. */
7786 ret = ((enum gimplify_status)
7787 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7788 if (ret == GS_OK)
7790 if (*expr_p == NULL_TREE)
7791 break;
7792 if (*expr_p != save_expr)
7793 continue;
7795 else if (ret != GS_UNHANDLED)
7796 break;
7798 /* Make sure that all the cases set 'ret' appropriately. */
7799 ret = GS_UNHANDLED;
7800 switch (TREE_CODE (*expr_p))
7802 /* First deal with the special cases. */
7804 case POSTINCREMENT_EXPR:
7805 case POSTDECREMENT_EXPR:
7806 case PREINCREMENT_EXPR:
7807 case PREDECREMENT_EXPR:
7808 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7809 fallback != fb_none,
7810 TREE_TYPE (*expr_p));
7811 break;
7813 case VIEW_CONVERT_EXPR:
7814 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7815 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7817 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7818 post_p, is_gimple_val, fb_rvalue);
7819 recalculate_side_effects (*expr_p);
7820 break;
7822 /* Fallthru. */
7824 case ARRAY_REF:
7825 case ARRAY_RANGE_REF:
7826 case REALPART_EXPR:
7827 case IMAGPART_EXPR:
7828 case COMPONENT_REF:
7829 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7830 fallback ? fallback : fb_rvalue);
7831 break;
7833 case COND_EXPR:
7834 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7836 /* C99 code may assign to an array in a structure value of a
7837 conditional expression, and this has undefined behavior
7838 only on execution, so create a temporary if an lvalue is
7839 required. */
7840 if (fallback == fb_lvalue)
7842 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7843 mark_addressable (*expr_p);
7844 ret = GS_OK;
7846 break;
7848 case CALL_EXPR:
7849 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7851 /* C99 code may assign to an array in a structure returned
7852 from a function, and this has undefined behavior only on
7853 execution, so create a temporary if an lvalue is
7854 required. */
7855 if (fallback == fb_lvalue)
7857 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7858 mark_addressable (*expr_p);
7859 ret = GS_OK;
7861 break;
7863 case TREE_LIST:
7864 gcc_unreachable ();
7866 case COMPOUND_EXPR:
7867 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7868 break;
7870 case COMPOUND_LITERAL_EXPR:
7871 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7872 gimple_test_f, fallback);
7873 break;
7875 case MODIFY_EXPR:
7876 case INIT_EXPR:
7877 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7878 fallback != fb_none);
7879 break;
7881 case TRUTH_ANDIF_EXPR:
7882 case TRUTH_ORIF_EXPR:
7884 /* Preserve the original type of the expression and the
7885 source location of the outer expression. */
7886 tree org_type = TREE_TYPE (*expr_p);
7887 *expr_p = gimple_boolify (*expr_p);
7888 *expr_p = build3_loc (input_location, COND_EXPR,
7889 org_type, *expr_p,
7890 fold_convert_loc
7891 (input_location,
7892 org_type, boolean_true_node),
7893 fold_convert_loc
7894 (input_location,
7895 org_type, boolean_false_node));
7896 ret = GS_OK;
7897 break;
7900 case TRUTH_NOT_EXPR:
7902 tree type = TREE_TYPE (*expr_p);
7903 /* The parsers are careful to generate TRUTH_NOT_EXPR
7904 only with operands that are always zero or one.
7905 We do not fold here but handle the only interesting case
7906 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7907 *expr_p = gimple_boolify (*expr_p);
7908 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7909 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7910 TREE_TYPE (*expr_p),
7911 TREE_OPERAND (*expr_p, 0));
7912 else
7913 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7914 TREE_TYPE (*expr_p),
7915 TREE_OPERAND (*expr_p, 0),
7916 build_int_cst (TREE_TYPE (*expr_p), 1));
7917 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7918 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7919 ret = GS_OK;
7920 break;
7923 case ADDR_EXPR:
7924 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7925 break;
7927 case ANNOTATE_EXPR:
7929 tree cond = TREE_OPERAND (*expr_p, 0);
7930 tree kind = TREE_OPERAND (*expr_p, 1);
7931 tree type = TREE_TYPE (cond);
7932 if (!INTEGRAL_TYPE_P (type))
7934 *expr_p = cond;
7935 ret = GS_OK;
7936 break;
7938 tree tmp = create_tmp_var (type, NULL);
7939 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7940 gimple call
7941 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7942 gimple_call_set_lhs (call, tmp);
7943 gimplify_seq_add_stmt (pre_p, call);
7944 *expr_p = tmp;
7945 ret = GS_ALL_DONE;
7946 break;
7949 case VA_ARG_EXPR:
7950 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7951 break;
7953 CASE_CONVERT:
7954 if (IS_EMPTY_STMT (*expr_p))
7956 ret = GS_ALL_DONE;
7957 break;
7960 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7961 || fallback == fb_none)
7963 /* Just strip a conversion to void (or in void context) and
7964 try again. */
7965 *expr_p = TREE_OPERAND (*expr_p, 0);
7966 ret = GS_OK;
7967 break;
7970 ret = gimplify_conversion (expr_p);
7971 if (ret == GS_ERROR)
7972 break;
7973 if (*expr_p != save_expr)
7974 break;
7975 /* FALLTHRU */
7977 case FIX_TRUNC_EXPR:
7978 /* unary_expr: ... | '(' cast ')' val | ... */
7979 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7980 is_gimple_val, fb_rvalue);
7981 recalculate_side_effects (*expr_p);
7982 break;
7984 case INDIRECT_REF:
7986 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7987 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7988 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7990 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7991 if (*expr_p != save_expr)
7993 ret = GS_OK;
7994 break;
7997 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7998 is_gimple_reg, fb_rvalue);
7999 if (ret == GS_ERROR)
8000 break;
8002 recalculate_side_effects (*expr_p);
8003 *expr_p = fold_build2_loc (input_location, MEM_REF,
8004 TREE_TYPE (*expr_p),
8005 TREE_OPERAND (*expr_p, 0),
8006 build_int_cst (saved_ptr_type, 0));
8007 TREE_THIS_VOLATILE (*expr_p) = volatilep;
8008 TREE_THIS_NOTRAP (*expr_p) = notrap;
8009 ret = GS_OK;
8010 break;
8013 /* We arrive here through the various re-gimplifcation paths. */
8014 case MEM_REF:
8015 /* First try re-folding the whole thing. */
8016 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
8017 TREE_OPERAND (*expr_p, 0),
8018 TREE_OPERAND (*expr_p, 1));
8019 if (tmp)
8021 *expr_p = tmp;
8022 recalculate_side_effects (*expr_p);
8023 ret = GS_OK;
8024 break;
8026 /* Avoid re-gimplifying the address operand if it is already
8027 in suitable form. Re-gimplifying would mark the address
8028 operand addressable. Always gimplify when not in SSA form
8029 as we still may have to gimplify decls with value-exprs. */
8030 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
8031 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
8033 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8034 is_gimple_mem_ref_addr, fb_rvalue);
8035 if (ret == GS_ERROR)
8036 break;
8038 recalculate_side_effects (*expr_p);
8039 ret = GS_ALL_DONE;
8040 break;
8042 /* Constants need not be gimplified. */
8043 case INTEGER_CST:
8044 case REAL_CST:
8045 case FIXED_CST:
8046 case STRING_CST:
8047 case COMPLEX_CST:
8048 case VECTOR_CST:
8049 /* Drop the overflow flag on constants, we do not want
8050 that in the GIMPLE IL. */
8051 if (TREE_OVERFLOW_P (*expr_p))
8052 *expr_p = drop_tree_overflow (*expr_p);
8053 ret = GS_ALL_DONE;
8054 break;
8056 case CONST_DECL:
8057 /* If we require an lvalue, such as for ADDR_EXPR, retain the
8058 CONST_DECL node. Otherwise the decl is replaceable by its
8059 value. */
8060 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
8061 if (fallback & fb_lvalue)
8062 ret = GS_ALL_DONE;
8063 else
8065 *expr_p = DECL_INITIAL (*expr_p);
8066 ret = GS_OK;
8068 break;
8070 case DECL_EXPR:
8071 ret = gimplify_decl_expr (expr_p, pre_p);
8072 break;
8074 case BIND_EXPR:
8075 ret = gimplify_bind_expr (expr_p, pre_p);
8076 break;
8078 case LOOP_EXPR:
8079 ret = gimplify_loop_expr (expr_p, pre_p);
8080 break;
8082 case SWITCH_EXPR:
8083 ret = gimplify_switch_expr (expr_p, pre_p);
8084 break;
8086 case EXIT_EXPR:
8087 ret = gimplify_exit_expr (expr_p);
8088 break;
8090 case GOTO_EXPR:
8091 /* If the target is not LABEL, then it is a computed jump
8092 and the target needs to be gimplified. */
8093 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
8095 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
8096 NULL, is_gimple_val, fb_rvalue);
8097 if (ret == GS_ERROR)
8098 break;
8100 gimplify_seq_add_stmt (pre_p,
8101 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
8102 ret = GS_ALL_DONE;
8103 break;
8105 case PREDICT_EXPR:
8106 gimplify_seq_add_stmt (pre_p,
8107 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
8108 PREDICT_EXPR_OUTCOME (*expr_p)));
8109 ret = GS_ALL_DONE;
8110 break;
8112 case LABEL_EXPR:
8113 ret = GS_ALL_DONE;
8114 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
8115 == current_function_decl);
8116 gimplify_seq_add_stmt (pre_p,
8117 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
8118 break;
8120 case CASE_LABEL_EXPR:
8121 ret = gimplify_case_label_expr (expr_p, pre_p);
8122 break;
8124 case RETURN_EXPR:
8125 ret = gimplify_return_expr (*expr_p, pre_p);
8126 break;
8128 case CONSTRUCTOR:
8129 /* Don't reduce this in place; let gimplify_init_constructor work its
8130 magic. Buf if we're just elaborating this for side effects, just
8131 gimplify any element that has side-effects. */
8132 if (fallback == fb_none)
8134 unsigned HOST_WIDE_INT ix;
8135 tree val;
8136 tree temp = NULL_TREE;
8137 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
8138 if (TREE_SIDE_EFFECTS (val))
8139 append_to_statement_list (val, &temp);
8141 *expr_p = temp;
8142 ret = temp ? GS_OK : GS_ALL_DONE;
8144 /* C99 code may assign to an array in a constructed
8145 structure or union, and this has undefined behavior only
8146 on execution, so create a temporary if an lvalue is
8147 required. */
8148 else if (fallback == fb_lvalue)
8150 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8151 mark_addressable (*expr_p);
8152 ret = GS_OK;
8154 else
8155 ret = GS_ALL_DONE;
8156 break;
8158 /* The following are special cases that are not handled by the
8159 original GIMPLE grammar. */
8161 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
8162 eliminated. */
8163 case SAVE_EXPR:
8164 ret = gimplify_save_expr (expr_p, pre_p, post_p);
8165 break;
8167 case BIT_FIELD_REF:
8168 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8169 post_p, is_gimple_lvalue, fb_either);
8170 recalculate_side_effects (*expr_p);
8171 break;
8173 case TARGET_MEM_REF:
8175 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
8177 if (TMR_BASE (*expr_p))
8178 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
8179 post_p, is_gimple_mem_ref_addr, fb_either);
8180 if (TMR_INDEX (*expr_p))
8181 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
8182 post_p, is_gimple_val, fb_rvalue);
8183 if (TMR_INDEX2 (*expr_p))
8184 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
8185 post_p, is_gimple_val, fb_rvalue);
8186 /* TMR_STEP and TMR_OFFSET are always integer constants. */
8187 ret = MIN (r0, r1);
8189 break;
8191 case NON_LVALUE_EXPR:
8192 /* This should have been stripped above. */
8193 gcc_unreachable ();
8195 case ASM_EXPR:
8196 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
8197 break;
8199 case TRY_FINALLY_EXPR:
8200 case TRY_CATCH_EXPR:
8202 gimple_seq eval, cleanup;
8203 gimple try_;
8205 /* Calls to destructors are generated automatically in FINALLY/CATCH
8206 block. They should have location as UNKNOWN_LOCATION. However,
8207 gimplify_call_expr will reset these call stmts to input_location
8208 if it finds stmt's location is unknown. To prevent resetting for
8209 destructors, we set the input_location to unknown.
8210 Note that this only affects the destructor calls in FINALLY/CATCH
8211 block, and will automatically reset to its original value by the
8212 end of gimplify_expr. */
8213 input_location = UNKNOWN_LOCATION;
8214 eval = cleanup = NULL;
8215 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8216 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8217 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8218 if (gimple_seq_empty_p (cleanup))
8220 gimple_seq_add_seq (pre_p, eval);
8221 ret = GS_ALL_DONE;
8222 break;
8224 try_ = gimple_build_try (eval, cleanup,
8225 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8226 ? GIMPLE_TRY_FINALLY
8227 : GIMPLE_TRY_CATCH);
8228 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8229 gimple_set_location (try_, saved_location);
8230 else
8231 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8232 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8233 gimple_try_set_catch_is_cleanup (try_,
8234 TRY_CATCH_IS_CLEANUP (*expr_p));
8235 gimplify_seq_add_stmt (pre_p, try_);
8236 ret = GS_ALL_DONE;
8237 break;
8240 case CLEANUP_POINT_EXPR:
8241 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8242 break;
8244 case TARGET_EXPR:
8245 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8246 break;
8248 case CATCH_EXPR:
8250 gimple c;
8251 gimple_seq handler = NULL;
8252 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8253 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8254 gimplify_seq_add_stmt (pre_p, c);
8255 ret = GS_ALL_DONE;
8256 break;
8259 case EH_FILTER_EXPR:
8261 gimple ehf;
8262 gimple_seq failure = NULL;
8264 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8265 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8266 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8267 gimplify_seq_add_stmt (pre_p, ehf);
8268 ret = GS_ALL_DONE;
8269 break;
8272 case OBJ_TYPE_REF:
8274 enum gimplify_status r0, r1;
8275 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8276 post_p, is_gimple_val, fb_rvalue);
8277 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8278 post_p, is_gimple_val, fb_rvalue);
8279 TREE_SIDE_EFFECTS (*expr_p) = 0;
8280 ret = MIN (r0, r1);
8282 break;
8284 case LABEL_DECL:
8285 /* We get here when taking the address of a label. We mark
8286 the label as "forced"; meaning it can never be removed and
8287 it is a potential target for any computed goto. */
8288 FORCED_LABEL (*expr_p) = 1;
8289 ret = GS_ALL_DONE;
8290 break;
8292 case STATEMENT_LIST:
8293 ret = gimplify_statement_list (expr_p, pre_p);
8294 break;
8296 case WITH_SIZE_EXPR:
8298 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8299 post_p == &internal_post ? NULL : post_p,
8300 gimple_test_f, fallback);
8301 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8302 is_gimple_val, fb_rvalue);
8303 ret = GS_ALL_DONE;
8305 break;
8307 case VAR_DECL:
8308 case PARM_DECL:
8309 ret = gimplify_var_or_parm_decl (expr_p);
8310 break;
8312 case RESULT_DECL:
8313 /* When within an OpenMP context, notice uses of variables. */
8314 if (gimplify_omp_ctxp)
8315 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8316 ret = GS_ALL_DONE;
8317 break;
8319 case SSA_NAME:
8320 /* Allow callbacks into the gimplifier during optimization. */
8321 ret = GS_ALL_DONE;
8322 break;
8324 case OACC_HOST_DATA:
8325 case OACC_DECLARE:
8326 sorry ("directive not yet implemented");
8327 ret = GS_ALL_DONE;
8328 break;
8330 case OMP_PARALLEL:
8331 gimplify_omp_parallel (expr_p, pre_p);
8332 ret = GS_ALL_DONE;
8333 break;
8335 case OMP_TASK:
8336 gimplify_omp_task (expr_p, pre_p);
8337 ret = GS_ALL_DONE;
8338 break;
8340 case OMP_FOR:
8341 case OMP_SIMD:
8342 case CILK_SIMD:
8343 case CILK_FOR:
8344 case OMP_DISTRIBUTE:
8345 case OACC_LOOP:
8346 ret = gimplify_omp_for (expr_p, pre_p);
8347 break;
8349 case OACC_KERNELS:
8350 if (OACC_KERNELS_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_PARALLEL:
8358 if (OACC_PARALLEL_COMBINED (*expr_p))
8359 sorry ("directive not yet implemented");
8360 else
8361 gimplify_omp_workshare (expr_p, pre_p);
8362 ret = GS_ALL_DONE;
8363 break;
8365 case OACC_CACHE:
8366 gimplify_oacc_cache (expr_p, pre_p);
8367 ret = GS_ALL_DONE;
8368 break;
8370 case OACC_DATA:
8371 case OMP_SECTIONS:
8372 case OMP_SINGLE:
8373 case OMP_TARGET:
8374 case OMP_TARGET_DATA:
8375 case OMP_TEAMS:
8376 gimplify_omp_workshare (expr_p, pre_p);
8377 ret = GS_ALL_DONE;
8378 break;
8380 case OACC_UPDATE:
8381 case OMP_TARGET_UPDATE:
8382 case OACC_ENTER_DATA:
8383 case OACC_EXIT_DATA:
8384 gimplify_omp_target_update (expr_p, pre_p);
8385 ret = GS_ALL_DONE;
8386 break;
8388 case OMP_SECTION:
8389 case OMP_MASTER:
8390 case OMP_TASKGROUP:
8391 case OMP_ORDERED:
8392 case OMP_CRITICAL:
8394 gimple_seq body = NULL;
8395 gimple g;
8397 gimplify_and_add (OMP_BODY (*expr_p), &body);
8398 switch (TREE_CODE (*expr_p))
8400 case OMP_SECTION:
8401 g = gimple_build_omp_section (body);
8402 break;
8403 case OMP_MASTER:
8404 g = gimple_build_omp_master (body);
8405 break;
8406 case OMP_TASKGROUP:
8408 gimple_seq cleanup = NULL;
8409 tree fn
8410 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8411 g = gimple_build_call (fn, 0);
8412 gimple_seq_add_stmt (&cleanup, g);
8413 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8414 body = NULL;
8415 gimple_seq_add_stmt (&body, g);
8416 g = gimple_build_omp_taskgroup (body);
8418 break;
8419 case OMP_ORDERED:
8420 g = gimple_build_omp_ordered (body);
8421 break;
8422 case OMP_CRITICAL:
8423 g = gimple_build_omp_critical (body,
8424 OMP_CRITICAL_NAME (*expr_p));
8425 break;
8426 default:
8427 gcc_unreachable ();
8429 gimplify_seq_add_stmt (pre_p, g);
8430 ret = GS_ALL_DONE;
8431 break;
8434 case OMP_ATOMIC:
8435 case OMP_ATOMIC_READ:
8436 case OMP_ATOMIC_CAPTURE_OLD:
8437 case OMP_ATOMIC_CAPTURE_NEW:
8438 ret = gimplify_omp_atomic (expr_p, pre_p);
8439 break;
8441 case TRANSACTION_EXPR:
8442 ret = gimplify_transaction (expr_p, pre_p);
8443 break;
8445 case TRUTH_AND_EXPR:
8446 case TRUTH_OR_EXPR:
8447 case TRUTH_XOR_EXPR:
8449 tree orig_type = TREE_TYPE (*expr_p);
8450 tree new_type, xop0, xop1;
8451 *expr_p = gimple_boolify (*expr_p);
8452 new_type = TREE_TYPE (*expr_p);
8453 if (!useless_type_conversion_p (orig_type, new_type))
8455 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8456 ret = GS_OK;
8457 break;
8460 /* Boolified binary truth expressions are semantically equivalent
8461 to bitwise binary expressions. Canonicalize them to the
8462 bitwise variant. */
8463 switch (TREE_CODE (*expr_p))
8465 case TRUTH_AND_EXPR:
8466 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8467 break;
8468 case TRUTH_OR_EXPR:
8469 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8470 break;
8471 case TRUTH_XOR_EXPR:
8472 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8473 break;
8474 default:
8475 break;
8477 /* Now make sure that operands have compatible type to
8478 expression's new_type. */
8479 xop0 = TREE_OPERAND (*expr_p, 0);
8480 xop1 = TREE_OPERAND (*expr_p, 1);
8481 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8482 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8483 new_type,
8484 xop0);
8485 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8486 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8487 new_type,
8488 xop1);
8489 /* Continue classified as tcc_binary. */
8490 goto expr_2;
8493 case FMA_EXPR:
8494 case VEC_COND_EXPR:
8495 case VEC_PERM_EXPR:
8496 /* Classified as tcc_expression. */
8497 goto expr_3;
8499 case POINTER_PLUS_EXPR:
8501 enum gimplify_status r0, r1;
8502 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8503 post_p, is_gimple_val, fb_rvalue);
8504 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8505 post_p, is_gimple_val, fb_rvalue);
8506 recalculate_side_effects (*expr_p);
8507 ret = MIN (r0, r1);
8508 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8509 after gimplifying operands - this is similar to how
8510 it would be folding all gimplified stmts on creation
8511 to have them canonicalized, which is what we eventually
8512 should do anyway. */
8513 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8514 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8516 *expr_p = build_fold_addr_expr_with_type_loc
8517 (input_location,
8518 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8519 TREE_OPERAND (*expr_p, 0),
8520 fold_convert (ptr_type_node,
8521 TREE_OPERAND (*expr_p, 1))),
8522 TREE_TYPE (*expr_p));
8523 ret = MIN (ret, GS_OK);
8525 break;
8528 case CILK_SYNC_STMT:
8530 if (!fn_contains_cilk_spawn_p (cfun))
8532 error_at (EXPR_LOCATION (*expr_p),
8533 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8534 ret = GS_ERROR;
8536 else
8538 gimplify_cilk_sync (expr_p, pre_p);
8539 ret = GS_ALL_DONE;
8541 break;
8544 default:
8545 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8547 case tcc_comparison:
8548 /* Handle comparison of objects of non scalar mode aggregates
8549 with a call to memcmp. It would be nice to only have to do
8550 this for variable-sized objects, but then we'd have to allow
8551 the same nest of reference nodes we allow for MODIFY_EXPR and
8552 that's too complex.
8554 Compare scalar mode aggregates as scalar mode values. Using
8555 memcmp for them would be very inefficient at best, and is
8556 plain wrong if bitfields are involved. */
8558 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8560 /* Vector comparisons need no boolification. */
8561 if (TREE_CODE (type) == VECTOR_TYPE)
8562 goto expr_2;
8563 else if (!AGGREGATE_TYPE_P (type))
8565 tree org_type = TREE_TYPE (*expr_p);
8566 *expr_p = gimple_boolify (*expr_p);
8567 if (!useless_type_conversion_p (org_type,
8568 TREE_TYPE (*expr_p)))
8570 *expr_p = fold_convert_loc (input_location,
8571 org_type, *expr_p);
8572 ret = GS_OK;
8574 else
8575 goto expr_2;
8577 else if (TYPE_MODE (type) != BLKmode)
8578 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8579 else
8580 ret = gimplify_variable_sized_compare (expr_p);
8582 break;
8585 /* If *EXPR_P does not need to be special-cased, handle it
8586 according to its class. */
8587 case tcc_unary:
8588 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8589 post_p, is_gimple_val, fb_rvalue);
8590 break;
8592 case tcc_binary:
8593 expr_2:
8595 enum gimplify_status r0, r1;
8597 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8598 post_p, is_gimple_val, fb_rvalue);
8599 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8600 post_p, is_gimple_val, fb_rvalue);
8602 ret = MIN (r0, r1);
8603 break;
8606 expr_3:
8608 enum gimplify_status r0, r1, r2;
8610 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8611 post_p, is_gimple_val, fb_rvalue);
8612 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8613 post_p, is_gimple_val, fb_rvalue);
8614 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8615 post_p, is_gimple_val, fb_rvalue);
8617 ret = MIN (MIN (r0, r1), r2);
8618 break;
8621 case tcc_declaration:
8622 case tcc_constant:
8623 ret = GS_ALL_DONE;
8624 goto dont_recalculate;
8626 default:
8627 gcc_unreachable ();
8630 recalculate_side_effects (*expr_p);
8632 dont_recalculate:
8633 break;
8636 gcc_assert (*expr_p || ret != GS_OK);
8638 while (ret == GS_OK);
8640 /* If we encountered an error_mark somewhere nested inside, either
8641 stub out the statement or propagate the error back out. */
8642 if (ret == GS_ERROR)
8644 if (is_statement)
8645 *expr_p = NULL;
8646 goto out;
8649 /* This was only valid as a return value from the langhook, which
8650 we handled. Make sure it doesn't escape from any other context. */
8651 gcc_assert (ret != GS_UNHANDLED);
8653 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8655 /* We aren't looking for a value, and we don't have a valid
8656 statement. If it doesn't have side-effects, throw it away. */
8657 if (!TREE_SIDE_EFFECTS (*expr_p))
8658 *expr_p = NULL;
8659 else if (!TREE_THIS_VOLATILE (*expr_p))
8661 /* This is probably a _REF that contains something nested that
8662 has side effects. Recurse through the operands to find it. */
8663 enum tree_code code = TREE_CODE (*expr_p);
8665 switch (code)
8667 case COMPONENT_REF:
8668 case REALPART_EXPR:
8669 case IMAGPART_EXPR:
8670 case VIEW_CONVERT_EXPR:
8671 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8672 gimple_test_f, fallback);
8673 break;
8675 case ARRAY_REF:
8676 case ARRAY_RANGE_REF:
8677 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8678 gimple_test_f, fallback);
8679 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8680 gimple_test_f, fallback);
8681 break;
8683 default:
8684 /* Anything else with side-effects must be converted to
8685 a valid statement before we get here. */
8686 gcc_unreachable ();
8689 *expr_p = NULL;
8691 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8692 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8694 /* Historically, the compiler has treated a bare reference
8695 to a non-BLKmode volatile lvalue as forcing a load. */
8696 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8698 /* Normally, we do not want to create a temporary for a
8699 TREE_ADDRESSABLE type because such a type should not be
8700 copied by bitwise-assignment. However, we make an
8701 exception here, as all we are doing here is ensuring that
8702 we read the bytes that make up the type. We use
8703 create_tmp_var_raw because create_tmp_var will abort when
8704 given a TREE_ADDRESSABLE type. */
8705 tree tmp = create_tmp_var_raw (type, "vol");
8706 gimple_add_tmp_var (tmp);
8707 gimplify_assign (tmp, *expr_p, pre_p);
8708 *expr_p = NULL;
8710 else
8711 /* We can't do anything useful with a volatile reference to
8712 an incomplete type, so just throw it away. Likewise for
8713 a BLKmode type, since any implicit inner load should
8714 already have been turned into an explicit one by the
8715 gimplification process. */
8716 *expr_p = NULL;
8719 /* If we are gimplifying at the statement level, we're done. Tack
8720 everything together and return. */
8721 if (fallback == fb_none || is_statement)
8723 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8724 it out for GC to reclaim it. */
8725 *expr_p = NULL_TREE;
8727 if (!gimple_seq_empty_p (internal_pre)
8728 || !gimple_seq_empty_p (internal_post))
8730 gimplify_seq_add_seq (&internal_pre, internal_post);
8731 gimplify_seq_add_seq (pre_p, internal_pre);
8734 /* The result of gimplifying *EXPR_P is going to be the last few
8735 statements in *PRE_P and *POST_P. Add location information
8736 to all the statements that were added by the gimplification
8737 helpers. */
8738 if (!gimple_seq_empty_p (*pre_p))
8739 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8741 if (!gimple_seq_empty_p (*post_p))
8742 annotate_all_with_location_after (*post_p, post_last_gsi,
8743 input_location);
8745 goto out;
8748 #ifdef ENABLE_GIMPLE_CHECKING
8749 if (*expr_p)
8751 enum tree_code code = TREE_CODE (*expr_p);
8752 /* These expressions should already be in gimple IR form. */
8753 gcc_assert (code != MODIFY_EXPR
8754 && code != ASM_EXPR
8755 && code != BIND_EXPR
8756 && code != CATCH_EXPR
8757 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8758 && code != EH_FILTER_EXPR
8759 && code != GOTO_EXPR
8760 && code != LABEL_EXPR
8761 && code != LOOP_EXPR
8762 && code != SWITCH_EXPR
8763 && code != TRY_FINALLY_EXPR
8764 && code != OACC_PARALLEL
8765 && code != OACC_KERNELS
8766 && code != OACC_DATA
8767 && code != OACC_HOST_DATA
8768 && code != OACC_DECLARE
8769 && code != OACC_UPDATE
8770 && code != OACC_ENTER_DATA
8771 && code != OACC_EXIT_DATA
8772 && code != OACC_CACHE
8773 && code != OMP_CRITICAL
8774 && code != OMP_FOR
8775 && code != OACC_LOOP
8776 && code != OMP_MASTER
8777 && code != OMP_TASKGROUP
8778 && code != OMP_ORDERED
8779 && code != OMP_PARALLEL
8780 && code != OMP_SECTIONS
8781 && code != OMP_SECTION
8782 && code != OMP_SINGLE);
8784 #endif
8786 /* Otherwise we're gimplifying a subexpression, so the resulting
8787 value is interesting. If it's a valid operand that matches
8788 GIMPLE_TEST_F, we're done. Unless we are handling some
8789 post-effects internally; if that's the case, we need to copy into
8790 a temporary before adding the post-effects to POST_P. */
8791 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8792 goto out;
8794 /* Otherwise, we need to create a new temporary for the gimplified
8795 expression. */
8797 /* We can't return an lvalue if we have an internal postqueue. The
8798 object the lvalue refers to would (probably) be modified by the
8799 postqueue; we need to copy the value out first, which means an
8800 rvalue. */
8801 if ((fallback & fb_lvalue)
8802 && gimple_seq_empty_p (internal_post)
8803 && is_gimple_addressable (*expr_p))
8805 /* An lvalue will do. Take the address of the expression, store it
8806 in a temporary, and replace the expression with an INDIRECT_REF of
8807 that temporary. */
8808 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8809 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8810 *expr_p = build_simple_mem_ref (tmp);
8812 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8814 /* An rvalue will do. Assign the gimplified expression into a
8815 new temporary TMP and replace the original expression with
8816 TMP. First, make sure that the expression has a type so that
8817 it can be assigned into a temporary. */
8818 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8819 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8821 else
8823 #ifdef ENABLE_GIMPLE_CHECKING
8824 if (!(fallback & fb_mayfail))
8826 fprintf (stderr, "gimplification failed:\n");
8827 print_generic_expr (stderr, *expr_p, 0);
8828 debug_tree (*expr_p);
8829 internal_error ("gimplification failed");
8831 #endif
8832 gcc_assert (fallback & fb_mayfail);
8834 /* If this is an asm statement, and the user asked for the
8835 impossible, don't die. Fail and let gimplify_asm_expr
8836 issue an error. */
8837 ret = GS_ERROR;
8838 goto out;
8841 /* Make sure the temporary matches our predicate. */
8842 gcc_assert ((*gimple_test_f) (*expr_p));
8844 if (!gimple_seq_empty_p (internal_post))
8846 annotate_all_with_location (internal_post, input_location);
8847 gimplify_seq_add_seq (pre_p, internal_post);
8850 out:
8851 input_location = saved_location;
8852 return ret;
8855 /* Look through TYPE for variable-sized objects and gimplify each such
8856 size that we find. Add to LIST_P any statements generated. */
8858 void
8859 gimplify_type_sizes (tree type, gimple_seq *list_p)
8861 tree field, t;
8863 if (type == NULL || type == error_mark_node)
8864 return;
8866 /* We first do the main variant, then copy into any other variants. */
8867 type = TYPE_MAIN_VARIANT (type);
8869 /* Avoid infinite recursion. */
8870 if (TYPE_SIZES_GIMPLIFIED (type))
8871 return;
8873 TYPE_SIZES_GIMPLIFIED (type) = 1;
8875 switch (TREE_CODE (type))
8877 case INTEGER_TYPE:
8878 case ENUMERAL_TYPE:
8879 case BOOLEAN_TYPE:
8880 case REAL_TYPE:
8881 case FIXED_POINT_TYPE:
8882 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8883 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8885 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8887 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8888 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8890 break;
8892 case ARRAY_TYPE:
8893 /* These types may not have declarations, so handle them here. */
8894 gimplify_type_sizes (TREE_TYPE (type), list_p);
8895 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8896 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8897 with assigned stack slots, for -O1+ -g they should be tracked
8898 by VTA. */
8899 if (!(TYPE_NAME (type)
8900 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8901 && DECL_IGNORED_P (TYPE_NAME (type)))
8902 && TYPE_DOMAIN (type)
8903 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8905 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8906 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8907 DECL_IGNORED_P (t) = 0;
8908 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8909 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8910 DECL_IGNORED_P (t) = 0;
8912 break;
8914 case RECORD_TYPE:
8915 case UNION_TYPE:
8916 case QUAL_UNION_TYPE:
8917 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8918 if (TREE_CODE (field) == FIELD_DECL)
8920 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8921 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8922 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8923 gimplify_type_sizes (TREE_TYPE (field), list_p);
8925 break;
8927 case POINTER_TYPE:
8928 case REFERENCE_TYPE:
8929 /* We used to recurse on the pointed-to type here, which turned out to
8930 be incorrect because its definition might refer to variables not
8931 yet initialized at this point if a forward declaration is involved.
8933 It was actually useful for anonymous pointed-to types to ensure
8934 that the sizes evaluation dominates every possible later use of the
8935 values. Restricting to such types here would be safe since there
8936 is no possible forward declaration around, but would introduce an
8937 undesirable middle-end semantic to anonymity. We then defer to
8938 front-ends the responsibility of ensuring that the sizes are
8939 evaluated both early and late enough, e.g. by attaching artificial
8940 type declarations to the tree. */
8941 break;
8943 default:
8944 break;
8947 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8948 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8950 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8952 TYPE_SIZE (t) = TYPE_SIZE (type);
8953 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8954 TYPE_SIZES_GIMPLIFIED (t) = 1;
8958 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8959 a size or position, has had all of its SAVE_EXPRs evaluated.
8960 We add any required statements to *STMT_P. */
8962 void
8963 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8965 tree expr = *expr_p;
8967 /* We don't do anything if the value isn't there, is constant, or contains
8968 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8969 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8970 will want to replace it with a new variable, but that will cause problems
8971 if this type is from outside the function. It's OK to have that here. */
8972 if (is_gimple_sizepos (expr))
8973 return;
8975 *expr_p = unshare_expr (expr);
8977 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8980 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8981 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8982 is true, also gimplify the parameters. */
8984 gimple
8985 gimplify_body (tree fndecl, bool do_parms)
8987 location_t saved_location = input_location;
8988 gimple_seq parm_stmts, seq;
8989 gimple outer_bind;
8990 struct cgraph_node *cgn;
8992 timevar_push (TV_TREE_GIMPLIFY);
8994 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8995 gimplification. */
8996 default_rtl_profile ();
8998 gcc_assert (gimplify_ctxp == NULL);
8999 push_gimplify_context ();
9001 if (flag_openacc || flag_openmp)
9003 gcc_assert (gimplify_omp_ctxp == NULL);
9004 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
9005 gimplify_omp_ctxp
9006 = new_omp_context ((enum omp_region_type) (ORT_TARGET
9007 | ORT_TARGET_OFFLOAD));
9010 /* Unshare most shared trees in the body and in that of any nested functions.
9011 It would seem we don't have to do this for nested functions because
9012 they are supposed to be output and then the outer function gimplified
9013 first, but the g++ front end doesn't always do it that way. */
9014 unshare_body (fndecl);
9015 unvisit_body (fndecl);
9017 cgn = cgraph_node::get (fndecl);
9018 if (cgn && cgn->origin)
9019 nonlocal_vlas = new hash_set<tree>;
9021 /* Make sure input_location isn't set to something weird. */
9022 input_location = DECL_SOURCE_LOCATION (fndecl);
9024 /* Resolve callee-copies. This has to be done before processing
9025 the body so that DECL_VALUE_EXPR gets processed correctly. */
9026 parm_stmts = do_parms ? gimplify_parameters () : NULL;
9028 /* Gimplify the function's body. */
9029 seq = NULL;
9030 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
9031 outer_bind = gimple_seq_first_stmt (seq);
9032 if (!outer_bind)
9034 outer_bind = gimple_build_nop ();
9035 gimplify_seq_add_stmt (&seq, outer_bind);
9038 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
9039 not the case, wrap everything in a GIMPLE_BIND to make it so. */
9040 if (gimple_code (outer_bind) == GIMPLE_BIND
9041 && gimple_seq_first (seq) == gimple_seq_last (seq))
9043 else
9044 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
9046 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9048 /* If we had callee-copies statements, insert them at the beginning
9049 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
9050 if (!gimple_seq_empty_p (parm_stmts))
9052 tree parm;
9054 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
9055 gimple_bind_set_body (outer_bind, parm_stmts);
9057 for (parm = DECL_ARGUMENTS (current_function_decl);
9058 parm; parm = DECL_CHAIN (parm))
9059 if (DECL_HAS_VALUE_EXPR_P (parm))
9061 DECL_HAS_VALUE_EXPR_P (parm) = 0;
9062 DECL_IGNORED_P (parm) = 0;
9066 if (nonlocal_vlas)
9068 if (nonlocal_vla_vars)
9070 /* tree-nested.c may later on call declare_vars (..., true);
9071 which relies on BLOCK_VARS chain to be the tail of the
9072 gimple_bind_vars chain. Ensure we don't violate that
9073 assumption. */
9074 if (gimple_bind_block (outer_bind)
9075 == DECL_INITIAL (current_function_decl))
9076 declare_vars (nonlocal_vla_vars, outer_bind, true);
9077 else
9078 BLOCK_VARS (DECL_INITIAL (current_function_decl))
9079 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
9080 nonlocal_vla_vars);
9081 nonlocal_vla_vars = NULL_TREE;
9083 delete nonlocal_vlas;
9084 nonlocal_vlas = NULL;
9087 if ((flag_openacc || flag_openmp || flag_openmp_simd)
9088 && gimplify_omp_ctxp)
9090 delete_omp_context (gimplify_omp_ctxp);
9091 gimplify_omp_ctxp = NULL;
9094 pop_gimplify_context (outer_bind);
9095 gcc_assert (gimplify_ctxp == NULL);
9097 #ifdef ENABLE_CHECKING
9098 if (!seen_error ())
9099 verify_gimple_in_seq (gimple_bind_body (outer_bind));
9100 #endif
9102 timevar_pop (TV_TREE_GIMPLIFY);
9103 input_location = saved_location;
9105 return outer_bind;
9108 typedef char *char_p; /* For DEF_VEC_P. */
9110 /* Return whether we should exclude FNDECL from instrumentation. */
9112 static bool
9113 flag_instrument_functions_exclude_p (tree fndecl)
9115 vec<char_p> *v;
9117 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
9118 if (v && v->length () > 0)
9120 const char *name;
9121 int i;
9122 char *s;
9124 name = lang_hooks.decl_printable_name (fndecl, 0);
9125 FOR_EACH_VEC_ELT (*v, i, s)
9126 if (strstr (name, s) != NULL)
9127 return true;
9130 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
9131 if (v && v->length () > 0)
9133 const char *name;
9134 int i;
9135 char *s;
9137 name = DECL_SOURCE_FILE (fndecl);
9138 FOR_EACH_VEC_ELT (*v, i, s)
9139 if (strstr (name, s) != NULL)
9140 return true;
9143 return false;
9146 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
9147 node for the function we want to gimplify.
9149 Return the sequence of GIMPLE statements corresponding to the body
9150 of FNDECL. */
9152 void
9153 gimplify_function_tree (tree fndecl)
9155 tree parm, ret;
9156 gimple_seq seq;
9157 gimple bind;
9159 gcc_assert (!gimple_body (fndecl));
9161 if (DECL_STRUCT_FUNCTION (fndecl))
9162 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
9163 else
9164 push_struct_function (fndecl);
9166 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
9168 /* Preliminarily mark non-addressed complex variables as eligible
9169 for promotion to gimple registers. We'll transform their uses
9170 as we find them. */
9171 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
9172 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
9173 && !TREE_THIS_VOLATILE (parm)
9174 && !needs_to_live_in_memory (parm))
9175 DECL_GIMPLE_REG_P (parm) = 1;
9178 ret = DECL_RESULT (fndecl);
9179 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
9180 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
9181 && !needs_to_live_in_memory (ret))
9182 DECL_GIMPLE_REG_P (ret) = 1;
9184 bind = gimplify_body (fndecl, true);
9186 /* The tree body of the function is no longer needed, replace it
9187 with the new GIMPLE body. */
9188 seq = NULL;
9189 gimple_seq_add_stmt (&seq, bind);
9190 gimple_set_body (fndecl, seq);
9192 /* If we're instrumenting function entry/exit, then prepend the call to
9193 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
9194 catch the exit hook. */
9195 /* ??? Add some way to ignore exceptions for this TFE. */
9196 if (flag_instrument_function_entry_exit
9197 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
9198 && !flag_instrument_functions_exclude_p (fndecl))
9200 tree x;
9201 gimple new_bind;
9202 gimple tf;
9203 gimple_seq cleanup = NULL, body = NULL;
9204 tree tmp_var;
9205 gimple call;
9207 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9208 call = gimple_build_call (x, 1, integer_zero_node);
9209 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9210 gimple_call_set_lhs (call, tmp_var);
9211 gimplify_seq_add_stmt (&cleanup, call);
9212 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
9213 call = gimple_build_call (x, 2,
9214 build_fold_addr_expr (current_function_decl),
9215 tmp_var);
9216 gimplify_seq_add_stmt (&cleanup, call);
9217 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
9219 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9220 call = gimple_build_call (x, 1, integer_zero_node);
9221 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9222 gimple_call_set_lhs (call, tmp_var);
9223 gimplify_seq_add_stmt (&body, call);
9224 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
9225 call = gimple_build_call (x, 2,
9226 build_fold_addr_expr (current_function_decl),
9227 tmp_var);
9228 gimplify_seq_add_stmt (&body, call);
9229 gimplify_seq_add_stmt (&body, tf);
9230 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
9231 /* Clear the block for BIND, since it is no longer directly inside
9232 the function, but within a try block. */
9233 gimple_bind_set_block (bind, NULL);
9235 /* Replace the current function body with the body
9236 wrapped in the try/finally TF. */
9237 seq = NULL;
9238 gimple_seq_add_stmt (&seq, new_bind);
9239 gimple_set_body (fndecl, seq);
9242 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9243 cfun->curr_properties = PROP_gimple_any;
9245 pop_cfun ();
9248 /* Return a dummy expression of type TYPE in order to keep going after an
9249 error. */
9251 static tree
9252 dummy_object (tree type)
9254 tree t = build_int_cst (build_pointer_type (type), 0);
9255 return build2 (MEM_REF, type, t, t);
9258 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9259 builtin function, but a very special sort of operator. */
9261 enum gimplify_status
9262 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9264 tree promoted_type, have_va_type;
9265 tree valist = TREE_OPERAND (*expr_p, 0);
9266 tree type = TREE_TYPE (*expr_p);
9267 tree t;
9268 location_t loc = EXPR_LOCATION (*expr_p);
9270 /* Verify that valist is of the proper type. */
9271 have_va_type = TREE_TYPE (valist);
9272 if (have_va_type == error_mark_node)
9273 return GS_ERROR;
9274 have_va_type = targetm.canonical_va_list_type (have_va_type);
9276 if (have_va_type == NULL_TREE)
9278 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9279 return GS_ERROR;
9282 /* Generate a diagnostic for requesting data of a type that cannot
9283 be passed through `...' due to type promotion at the call site. */
9284 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9285 != type)
9287 static bool gave_help;
9288 bool warned;
9290 /* Unfortunately, this is merely undefined, rather than a constraint
9291 violation, so we cannot make this an error. If this call is never
9292 executed, the program is still strictly conforming. */
9293 warned = warning_at (loc, 0,
9294 "%qT is promoted to %qT when passed through %<...%>",
9295 type, promoted_type);
9296 if (!gave_help && warned)
9298 gave_help = true;
9299 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9300 promoted_type, type);
9303 /* We can, however, treat "undefined" any way we please.
9304 Call abort to encourage the user to fix the program. */
9305 if (warned)
9306 inform (loc, "if this code is reached, the program will abort");
9307 /* Before the abort, allow the evaluation of the va_list
9308 expression to exit or longjmp. */
9309 gimplify_and_add (valist, pre_p);
9310 t = build_call_expr_loc (loc,
9311 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9312 gimplify_and_add (t, pre_p);
9314 /* This is dead code, but go ahead and finish so that the
9315 mode of the result comes out right. */
9316 *expr_p = dummy_object (type);
9317 return GS_ALL_DONE;
9319 else
9321 /* Make it easier for the backends by protecting the valist argument
9322 from multiple evaluations. */
9323 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9325 /* For this case, the backends will be expecting a pointer to
9326 TREE_TYPE (abi), but it's possible we've
9327 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9328 So fix it. */
9329 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9331 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9332 valist = fold_convert_loc (loc, p1,
9333 build_fold_addr_expr_loc (loc, valist));
9336 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9338 else
9339 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9341 if (!targetm.gimplify_va_arg_expr)
9342 /* FIXME: Once most targets are converted we should merely
9343 assert this is non-null. */
9344 return GS_ALL_DONE;
9346 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9347 return GS_OK;
9351 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9353 DST/SRC are the destination and source respectively. You can pass
9354 ungimplified trees in DST or SRC, in which case they will be
9355 converted to a gimple operand if necessary.
9357 This function returns the newly created GIMPLE_ASSIGN tuple. */
9359 gimple
9360 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9362 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9363 gimplify_and_add (t, seq_p);
9364 ggc_free (t);
9365 return gimple_seq_last_stmt (*seq_p);
9368 inline hashval_t
9369 gimplify_hasher::hash (const value_type *p)
9371 tree t = p->val;
9372 return iterative_hash_expr (t, 0);
9375 inline bool
9376 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9378 tree t1 = p1->val;
9379 tree t2 = p2->val;
9380 enum tree_code code = TREE_CODE (t1);
9382 if (TREE_CODE (t2) != code
9383 || TREE_TYPE (t1) != TREE_TYPE (t2))
9384 return false;
9386 if (!operand_equal_p (t1, t2, 0))
9387 return false;
9389 #ifdef ENABLE_CHECKING
9390 /* Only allow them to compare equal if they also hash equal; otherwise
9391 results are nondeterminate, and we fail bootstrap comparison. */
9392 gcc_assert (hash (p1) == hash (p2));
9393 #endif
9395 return true;