svn merge -r 216846:217483 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / gimplify.c
blobad48d5154350d805578482e607ea39438996f327
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,
98 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
99 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
100 | GOVD_LOCAL)
104 enum omp_region_type
106 ORT_WORKSHARE = 0,
107 ORT_SIMD = 1,
108 ORT_PARALLEL = 2,
109 ORT_COMBINED_PARALLEL = 3,
110 ORT_TASK = 4,
111 ORT_UNTIED_TASK = 5,
112 ORT_TEAMS = 8,
113 ORT_TARGET = 16,
115 /* Flags for ORT_TARGET. */
116 /* Prepare this region for offloading. */
117 ORT_TARGET_OFFLOAD = 32
120 /* Gimplify hashtable helper. */
122 struct gimplify_hasher : typed_free_remove <elt_t>
124 typedef elt_t value_type;
125 typedef elt_t compare_type;
126 static inline hashval_t hash (const value_type *);
127 static inline bool equal (const value_type *, const compare_type *);
130 struct gimplify_ctx
132 struct gimplify_ctx *prev_context;
134 vec<gimple> bind_expr_stack;
135 tree temps;
136 gimple_seq conditional_cleanups;
137 tree exit_label;
138 tree return_temp;
140 vec<tree> case_labels;
141 /* The formal temporary table. Should this be persistent? */
142 hash_table<gimplify_hasher> *temp_htab;
144 int conditions;
145 bool save_stack;
146 bool into_ssa;
147 bool allow_rhs_cond_expr;
148 bool in_cleanup_point_expr;
151 struct gimplify_omp_ctx
153 struct gimplify_omp_ctx *outer_context;
154 splay_tree variables;
155 hash_set<tree> *privatized_types;
156 location_t location;
157 enum omp_clause_default_kind default_kind;
158 enum omp_region_type region_type;
159 bool combined_loop;
160 bool distribute;
163 static struct gimplify_ctx *gimplify_ctxp;
164 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
166 /* Forward declaration. */
167 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
169 /* Shorter alias name for the above function for use in gimplify.c
170 only. */
172 static inline void
173 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
175 gimple_seq_add_stmt_without_update (seq_p, gs);
178 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
179 NULL, a new sequence is allocated. This function is
180 similar to gimple_seq_add_seq, but does not scan the operands.
181 During gimplification, we need to manipulate statement sequences
182 before the def/use vectors have been constructed. */
184 static void
185 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
187 gimple_stmt_iterator si;
189 if (src == NULL)
190 return;
192 si = gsi_last (*dst_p);
193 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
197 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
198 and popping gimplify contexts. */
200 static struct gimplify_ctx *ctx_pool = NULL;
202 /* Return a gimplify context struct from the pool. */
204 static inline struct gimplify_ctx *
205 ctx_alloc (void)
207 struct gimplify_ctx * c = ctx_pool;
209 if (c)
210 ctx_pool = c->prev_context;
211 else
212 c = XNEW (struct gimplify_ctx);
214 memset (c, '\0', sizeof (*c));
215 return c;
218 /* Put gimplify context C back into the pool. */
220 static inline void
221 ctx_free (struct gimplify_ctx *c)
223 c->prev_context = ctx_pool;
224 ctx_pool = c;
227 /* Free allocated ctx stack memory. */
229 void
230 free_gimplify_stack (void)
232 struct gimplify_ctx *c;
234 while ((c = ctx_pool))
236 ctx_pool = c->prev_context;
237 free (c);
242 /* Set up a context for the gimplifier. */
244 void
245 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
247 struct gimplify_ctx *c = ctx_alloc ();
249 c->prev_context = gimplify_ctxp;
250 gimplify_ctxp = c;
251 gimplify_ctxp->into_ssa = in_ssa;
252 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
255 /* Tear down a context for the gimplifier. If BODY is non-null, then
256 put the temporaries into the outer BIND_EXPR. Otherwise, put them
257 in the local_decls.
259 BODY is not a sequence, but the first tuple in a sequence. */
261 void
262 pop_gimplify_context (gimple body)
264 struct gimplify_ctx *c = gimplify_ctxp;
266 gcc_assert (c
267 && (!c->bind_expr_stack.exists ()
268 || c->bind_expr_stack.is_empty ()));
269 c->bind_expr_stack.release ();
270 gimplify_ctxp = c->prev_context;
272 if (body)
273 declare_vars (c->temps, body, false);
274 else
275 record_vars (c->temps);
277 delete c->temp_htab;
278 c->temp_htab = NULL;
279 ctx_free (c);
282 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
284 static void
285 gimple_push_bind_expr (gimple gimple_bind)
287 gimplify_ctxp->bind_expr_stack.reserve (8);
288 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
291 /* Pop the first element off the stack of bindings. */
293 static void
294 gimple_pop_bind_expr (void)
296 gimplify_ctxp->bind_expr_stack.pop ();
299 /* Return the first element of the stack of bindings. */
301 gimple
302 gimple_current_bind_expr (void)
304 return gimplify_ctxp->bind_expr_stack.last ();
307 /* Return the stack of bindings created during gimplification. */
309 vec<gimple>
310 gimple_bind_expr_stack (void)
312 return gimplify_ctxp->bind_expr_stack;
315 /* Return true iff there is a COND_EXPR between us and the innermost
316 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
318 static bool
319 gimple_conditional_context (void)
321 return gimplify_ctxp->conditions > 0;
324 /* Note that we've entered a COND_EXPR. */
326 static void
327 gimple_push_condition (void)
329 #ifdef ENABLE_GIMPLE_CHECKING
330 if (gimplify_ctxp->conditions == 0)
331 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
332 #endif
333 ++(gimplify_ctxp->conditions);
336 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
337 now, add any conditional cleanups we've seen to the prequeue. */
339 static void
340 gimple_pop_condition (gimple_seq *pre_p)
342 int conds = --(gimplify_ctxp->conditions);
344 gcc_assert (conds >= 0);
345 if (conds == 0)
347 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
348 gimplify_ctxp->conditional_cleanups = NULL;
352 /* A stable comparison routine for use with splay trees and DECLs. */
354 static int
355 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
357 tree a = (tree) xa;
358 tree b = (tree) xb;
360 return DECL_UID (a) - DECL_UID (b);
363 /* Create a new omp construct that deals with variable remapping. */
365 static struct gimplify_omp_ctx *
366 new_omp_context (enum omp_region_type region_type)
368 struct gimplify_omp_ctx *c;
370 c = XCNEW (struct gimplify_omp_ctx);
371 c->outer_context = gimplify_omp_ctxp;
372 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
373 c->privatized_types = new hash_set<tree>;
374 c->location = input_location;
375 c->region_type = region_type;
376 if ((region_type & ORT_TASK) == 0)
377 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
378 else
379 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
381 return c;
384 /* Destroy an omp construct that deals with variable remapping. */
386 static void
387 delete_omp_context (struct gimplify_omp_ctx *c)
389 splay_tree_delete (c->variables);
390 delete c->privatized_types;
391 XDELETE (c);
394 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
395 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
397 /* Both gimplify the statement T and append it to *SEQ_P. This function
398 behaves exactly as gimplify_stmt, but you don't have to pass T as a
399 reference. */
401 void
402 gimplify_and_add (tree t, gimple_seq *seq_p)
404 gimplify_stmt (&t, seq_p);
407 /* Gimplify statement T into sequence *SEQ_P, and return the first
408 tuple in the sequence of generated tuples for this statement.
409 Return NULL if gimplifying T produced no tuples. */
411 static gimple
412 gimplify_and_return_first (tree t, gimple_seq *seq_p)
414 gimple_stmt_iterator last = gsi_last (*seq_p);
416 gimplify_and_add (t, seq_p);
418 if (!gsi_end_p (last))
420 gsi_next (&last);
421 return gsi_stmt (last);
423 else
424 return gimple_seq_first_stmt (*seq_p);
427 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
428 LHS, or for a call argument. */
430 static bool
431 is_gimple_mem_rhs (tree t)
433 /* If we're dealing with a renamable type, either source or dest must be
434 a renamed variable. */
435 if (is_gimple_reg_type (TREE_TYPE (t)))
436 return is_gimple_val (t);
437 else
438 return is_gimple_val (t) || is_gimple_lvalue (t);
441 /* Return true if T is a CALL_EXPR or an expression that can be
442 assigned to a temporary. Note that this predicate should only be
443 used during gimplification. See the rationale for this in
444 gimplify_modify_expr. */
446 static bool
447 is_gimple_reg_rhs_or_call (tree t)
449 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
450 || TREE_CODE (t) == CALL_EXPR);
453 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
454 this predicate should only be used during gimplification. See the
455 rationale for this in gimplify_modify_expr. */
457 static bool
458 is_gimple_mem_rhs_or_call (tree t)
460 /* If we're dealing with a renamable type, either source or dest must be
461 a renamed variable. */
462 if (is_gimple_reg_type (TREE_TYPE (t)))
463 return is_gimple_val (t);
464 else
465 return (is_gimple_val (t) || is_gimple_lvalue (t)
466 || TREE_CODE (t) == CALL_EXPR);
469 /* Create a temporary with a name derived from VAL. Subroutine of
470 lookup_tmp_var; nobody else should call this function. */
472 static inline tree
473 create_tmp_from_val (tree val)
475 /* Drop all qualifiers and address-space information from the value type. */
476 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
477 tree var = create_tmp_var (type, get_name (val));
478 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
479 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
480 DECL_GIMPLE_REG_P (var) = 1;
481 return var;
484 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
485 an existing expression temporary. */
487 static tree
488 lookup_tmp_var (tree val, bool is_formal)
490 tree ret;
492 /* If not optimizing, never really reuse a temporary. local-alloc
493 won't allocate any variable that is used in more than one basic
494 block, which means it will go into memory, causing much extra
495 work in reload and final and poorer code generation, outweighing
496 the extra memory allocation here. */
497 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
498 ret = create_tmp_from_val (val);
499 else
501 elt_t elt, *elt_p;
502 elt_t **slot;
504 elt.val = val;
505 if (!gimplify_ctxp->temp_htab)
506 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
507 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
508 if (*slot == NULL)
510 elt_p = XNEW (elt_t);
511 elt_p->val = val;
512 elt_p->temp = ret = create_tmp_from_val (val);
513 *slot = elt_p;
515 else
517 elt_p = *slot;
518 ret = elt_p->temp;
522 return ret;
525 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
527 static tree
528 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
529 bool is_formal)
531 tree t, mod;
533 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
534 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
535 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
536 fb_rvalue);
538 if (gimplify_ctxp->into_ssa
539 && is_gimple_reg_type (TREE_TYPE (val)))
540 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
541 else
542 t = lookup_tmp_var (val, is_formal);
544 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
546 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
548 /* gimplify_modify_expr might want to reduce this further. */
549 gimplify_and_add (mod, pre_p);
550 ggc_free (mod);
552 return t;
555 /* Return a formal temporary variable initialized with VAL. PRE_P is as
556 in gimplify_expr. Only use this function if:
558 1) The value of the unfactored expression represented by VAL will not
559 change between the initialization and use of the temporary, and
560 2) The temporary will not be otherwise modified.
562 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
563 and #2 means it is inappropriate for && temps.
565 For other cases, use get_initialized_tmp_var instead. */
567 tree
568 get_formal_tmp_var (tree val, gimple_seq *pre_p)
570 return internal_get_tmp_var (val, pre_p, NULL, true);
573 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
574 are as in gimplify_expr. */
576 tree
577 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
579 return internal_get_tmp_var (val, pre_p, post_p, false);
582 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
583 generate debug info for them; otherwise don't. */
585 void
586 declare_vars (tree vars, gimple scope, bool debug_info)
588 tree last = vars;
589 if (last)
591 tree temps, block;
593 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
595 temps = nreverse (last);
597 block = gimple_bind_block (scope);
598 gcc_assert (!block || TREE_CODE (block) == BLOCK);
599 if (!block || !debug_info)
601 DECL_CHAIN (last) = gimple_bind_vars (scope);
602 gimple_bind_set_vars (scope, temps);
604 else
606 /* We need to attach the nodes both to the BIND_EXPR and to its
607 associated BLOCK for debugging purposes. The key point here
608 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
609 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
610 if (BLOCK_VARS (block))
611 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
612 else
614 gimple_bind_set_vars (scope,
615 chainon (gimple_bind_vars (scope), temps));
616 BLOCK_VARS (block) = temps;
622 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
623 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
624 no such upper bound can be obtained. */
626 static void
627 force_constant_size (tree var)
629 /* The only attempt we make is by querying the maximum size of objects
630 of the variable's type. */
632 HOST_WIDE_INT max_size;
634 gcc_assert (TREE_CODE (var) == VAR_DECL);
636 max_size = max_int_size_in_bytes (TREE_TYPE (var));
638 gcc_assert (max_size >= 0);
640 DECL_SIZE_UNIT (var)
641 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
642 DECL_SIZE (var)
643 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
646 /* Push the temporary variable TMP into the current binding. */
648 void
649 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
651 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
653 /* Later processing assumes that the object size is constant, which might
654 not be true at this point. Force the use of a constant upper bound in
655 this case. */
656 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
657 force_constant_size (tmp);
659 DECL_CONTEXT (tmp) = fn->decl;
660 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
662 record_vars_into (tmp, fn->decl);
665 /* Push the temporary variable TMP into the current binding. */
667 void
668 gimple_add_tmp_var (tree tmp)
670 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
672 /* Later processing assumes that the object size is constant, which might
673 not be true at this point. Force the use of a constant upper bound in
674 this case. */
675 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
676 force_constant_size (tmp);
678 DECL_CONTEXT (tmp) = current_function_decl;
679 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
681 if (gimplify_ctxp)
683 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
684 gimplify_ctxp->temps = tmp;
686 /* Mark temporaries local within the nearest enclosing parallel. */
687 if (gimplify_omp_ctxp)
689 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
690 while (ctx
691 && (ctx->region_type == ORT_WORKSHARE
692 || ctx->region_type == ORT_SIMD))
693 ctx = ctx->outer_context;
694 if (ctx)
695 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
698 else if (cfun)
699 record_vars (tmp);
700 else
702 gimple_seq body_seq;
704 /* This case is for nested functions. We need to expose the locals
705 they create. */
706 body_seq = gimple_body (current_function_decl);
707 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
713 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
714 nodes that are referenced more than once in GENERIC functions. This is
715 necessary because gimplification (translation into GIMPLE) is performed
716 by modifying tree nodes in-place, so gimplication of a shared node in a
717 first context could generate an invalid GIMPLE form in a second context.
719 This is achieved with a simple mark/copy/unmark algorithm that walks the
720 GENERIC representation top-down, marks nodes with TREE_VISITED the first
721 time it encounters them, duplicates them if they already have TREE_VISITED
722 set, and finally removes the TREE_VISITED marks it has set.
724 The algorithm works only at the function level, i.e. it generates a GENERIC
725 representation of a function with no nodes shared within the function when
726 passed a GENERIC function (except for nodes that are allowed to be shared).
728 At the global level, it is also necessary to unshare tree nodes that are
729 referenced in more than one function, for the same aforementioned reason.
730 This requires some cooperation from the front-end. There are 2 strategies:
732 1. Manual unsharing. The front-end needs to call unshare_expr on every
733 expression that might end up being shared across functions.
735 2. Deep unsharing. This is an extension of regular unsharing. Instead
736 of calling unshare_expr on expressions that might be shared across
737 functions, the front-end pre-marks them with TREE_VISITED. This will
738 ensure that they are unshared on the first reference within functions
739 when the regular unsharing algorithm runs. The counterpart is that
740 this algorithm must look deeper than for manual unsharing, which is
741 specified by LANG_HOOKS_DEEP_UNSHARING.
743 If there are only few specific cases of node sharing across functions, it is
744 probably easier for a front-end to unshare the expressions manually. On the
745 contrary, if the expressions generated at the global level are as widespread
746 as expressions generated within functions, deep unsharing is very likely the
747 way to go. */
749 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
750 These nodes model computations that must be done once. If we were to
751 unshare something like SAVE_EXPR(i++), the gimplification process would
752 create wrong code. However, if DATA is non-null, it must hold a pointer
753 set that is used to unshare the subtrees of these nodes. */
755 static tree
756 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
758 tree t = *tp;
759 enum tree_code code = TREE_CODE (t);
761 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
762 copy their subtrees if we can make sure to do it only once. */
763 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
765 if (data && !((hash_set<tree> *)data)->add (t))
767 else
768 *walk_subtrees = 0;
771 /* Stop at types, decls, constants like copy_tree_r. */
772 else if (TREE_CODE_CLASS (code) == tcc_type
773 || TREE_CODE_CLASS (code) == tcc_declaration
774 || TREE_CODE_CLASS (code) == tcc_constant
775 /* We can't do anything sensible with a BLOCK used as an
776 expression, but we also can't just die when we see it
777 because of non-expression uses. So we avert our eyes
778 and cross our fingers. Silly Java. */
779 || code == BLOCK)
780 *walk_subtrees = 0;
782 /* Cope with the statement expression extension. */
783 else if (code == STATEMENT_LIST)
786 /* Leave the bulk of the work to copy_tree_r itself. */
787 else
788 copy_tree_r (tp, walk_subtrees, NULL);
790 return NULL_TREE;
793 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
794 If *TP has been visited already, then *TP is deeply copied by calling
795 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
797 static tree
798 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
800 tree t = *tp;
801 enum tree_code code = TREE_CODE (t);
803 /* Skip types, decls, and constants. But we do want to look at their
804 types and the bounds of types. Mark them as visited so we properly
805 unmark their subtrees on the unmark pass. If we've already seen them,
806 don't look down further. */
807 if (TREE_CODE_CLASS (code) == tcc_type
808 || TREE_CODE_CLASS (code) == tcc_declaration
809 || TREE_CODE_CLASS (code) == tcc_constant)
811 if (TREE_VISITED (t))
812 *walk_subtrees = 0;
813 else
814 TREE_VISITED (t) = 1;
817 /* If this node has been visited already, unshare it and don't look
818 any deeper. */
819 else if (TREE_VISITED (t))
821 walk_tree (tp, mostly_copy_tree_r, data, NULL);
822 *walk_subtrees = 0;
825 /* Otherwise, mark the node as visited and keep looking. */
826 else
827 TREE_VISITED (t) = 1;
829 return NULL_TREE;
832 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
833 copy_if_shared_r callback unmodified. */
835 static inline void
836 copy_if_shared (tree *tp, void *data)
838 walk_tree (tp, copy_if_shared_r, data, NULL);
841 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
842 any nested functions. */
844 static void
845 unshare_body (tree fndecl)
847 struct cgraph_node *cgn = cgraph_node::get (fndecl);
848 /* If the language requires deep unsharing, we need a pointer set to make
849 sure we don't repeatedly unshare subtrees of unshareable nodes. */
850 hash_set<tree> *visited
851 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
853 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
854 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
855 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
857 delete visited;
859 if (cgn)
860 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
861 unshare_body (cgn->decl);
864 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
865 Subtrees are walked until the first unvisited node is encountered. */
867 static tree
868 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
870 tree t = *tp;
872 /* If this node has been visited, unmark it and keep looking. */
873 if (TREE_VISITED (t))
874 TREE_VISITED (t) = 0;
876 /* Otherwise, don't look any deeper. */
877 else
878 *walk_subtrees = 0;
880 return NULL_TREE;
883 /* Unmark the visited trees rooted at *TP. */
885 static inline void
886 unmark_visited (tree *tp)
888 walk_tree (tp, unmark_visited_r, NULL, NULL);
891 /* Likewise, but mark all trees as not visited. */
893 static void
894 unvisit_body (tree fndecl)
896 struct cgraph_node *cgn = cgraph_node::get (fndecl);
898 unmark_visited (&DECL_SAVED_TREE (fndecl));
899 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
900 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
902 if (cgn)
903 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
904 unvisit_body (cgn->decl);
907 /* Unconditionally make an unshared copy of EXPR. This is used when using
908 stored expressions which span multiple functions, such as BINFO_VTABLE,
909 as the normal unsharing process can't tell that they're shared. */
911 tree
912 unshare_expr (tree expr)
914 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
915 return expr;
918 /* Worker for unshare_expr_without_location. */
920 static tree
921 prune_expr_location (tree *tp, int *walk_subtrees, void *)
923 if (EXPR_P (*tp))
924 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
925 else
926 *walk_subtrees = 0;
927 return NULL_TREE;
930 /* Similar to unshare_expr but also prune all expression locations
931 from EXPR. */
933 tree
934 unshare_expr_without_location (tree expr)
936 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
937 if (EXPR_P (expr))
938 walk_tree (&expr, prune_expr_location, NULL, NULL);
939 return expr;
942 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
943 contain statements and have a value. Assign its value to a temporary
944 and give it void_type_node. Return the temporary, or NULL_TREE if
945 WRAPPER was already void. */
947 tree
948 voidify_wrapper_expr (tree wrapper, tree temp)
950 tree type = TREE_TYPE (wrapper);
951 if (type && !VOID_TYPE_P (type))
953 tree *p;
955 /* Set p to point to the body of the wrapper. Loop until we find
956 something that isn't a wrapper. */
957 for (p = &wrapper; p && *p; )
959 switch (TREE_CODE (*p))
961 case BIND_EXPR:
962 TREE_SIDE_EFFECTS (*p) = 1;
963 TREE_TYPE (*p) = void_type_node;
964 /* For a BIND_EXPR, the body is operand 1. */
965 p = &BIND_EXPR_BODY (*p);
966 break;
968 case CLEANUP_POINT_EXPR:
969 case TRY_FINALLY_EXPR:
970 case TRY_CATCH_EXPR:
971 TREE_SIDE_EFFECTS (*p) = 1;
972 TREE_TYPE (*p) = void_type_node;
973 p = &TREE_OPERAND (*p, 0);
974 break;
976 case STATEMENT_LIST:
978 tree_stmt_iterator i = tsi_last (*p);
979 TREE_SIDE_EFFECTS (*p) = 1;
980 TREE_TYPE (*p) = void_type_node;
981 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
983 break;
985 case COMPOUND_EXPR:
986 /* Advance to the last statement. Set all container types to
987 void. */
988 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
990 TREE_SIDE_EFFECTS (*p) = 1;
991 TREE_TYPE (*p) = void_type_node;
993 break;
995 case TRANSACTION_EXPR:
996 TREE_SIDE_EFFECTS (*p) = 1;
997 TREE_TYPE (*p) = void_type_node;
998 p = &TRANSACTION_EXPR_BODY (*p);
999 break;
1001 default:
1002 /* Assume that any tree upon which voidify_wrapper_expr is
1003 directly called is a wrapper, and that its body is op0. */
1004 if (p == &wrapper)
1006 TREE_SIDE_EFFECTS (*p) = 1;
1007 TREE_TYPE (*p) = void_type_node;
1008 p = &TREE_OPERAND (*p, 0);
1009 break;
1011 goto out;
1015 out:
1016 if (p == NULL || IS_EMPTY_STMT (*p))
1017 temp = NULL_TREE;
1018 else if (temp)
1020 /* The wrapper is on the RHS of an assignment that we're pushing
1021 down. */
1022 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1023 || TREE_CODE (temp) == MODIFY_EXPR);
1024 TREE_OPERAND (temp, 1) = *p;
1025 *p = temp;
1027 else
1029 temp = create_tmp_var (type, "retval");
1030 *p = build2 (INIT_EXPR, type, temp, *p);
1033 return temp;
1036 return NULL_TREE;
1039 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1040 a temporary through which they communicate. */
1042 static void
1043 build_stack_save_restore (gimple *save, gimple *restore)
1045 tree tmp_var;
1047 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1048 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1049 gimple_call_set_lhs (*save, tmp_var);
1051 *restore
1052 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1053 1, tmp_var);
1056 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1058 static enum gimplify_status
1059 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1061 tree bind_expr = *expr_p;
1062 bool old_save_stack = gimplify_ctxp->save_stack;
1063 tree t;
1064 gimple gimple_bind;
1065 gimple_seq body, cleanup;
1066 gimple stack_save;
1067 location_t start_locus = 0, end_locus = 0;
1069 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1071 /* Mark variables seen in this bind expr. */
1072 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1074 if (TREE_CODE (t) == VAR_DECL)
1076 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1078 /* Mark variable as local. */
1079 if (ctx && !DECL_EXTERNAL (t)
1080 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1081 || splay_tree_lookup (ctx->variables,
1082 (splay_tree_key) t) == NULL))
1084 if (ctx->region_type == ORT_SIMD
1085 && TREE_ADDRESSABLE (t)
1086 && !TREE_STATIC (t))
1087 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1088 else
1089 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1092 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1094 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1095 cfun->has_local_explicit_reg_vars = true;
1098 /* Preliminarily mark non-addressed complex variables as eligible
1099 for promotion to gimple registers. We'll transform their uses
1100 as we find them. */
1101 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1102 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1103 && !TREE_THIS_VOLATILE (t)
1104 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1105 && !needs_to_live_in_memory (t))
1106 DECL_GIMPLE_REG_P (t) = 1;
1109 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1110 BIND_EXPR_BLOCK (bind_expr));
1111 gimple_push_bind_expr (gimple_bind);
1113 gimplify_ctxp->save_stack = false;
1115 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1116 body = NULL;
1117 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1118 gimple_bind_set_body (gimple_bind, body);
1120 /* Source location wise, the cleanup code (stack_restore and clobbers)
1121 belongs to the end of the block, so propagate what we have. The
1122 stack_save operation belongs to the beginning of block, which we can
1123 infer from the bind_expr directly if the block has no explicit
1124 assignment. */
1125 if (BIND_EXPR_BLOCK (bind_expr))
1127 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1128 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1130 if (start_locus == 0)
1131 start_locus = EXPR_LOCATION (bind_expr);
1133 cleanup = NULL;
1134 stack_save = NULL;
1135 if (gimplify_ctxp->save_stack)
1137 gimple stack_restore;
1139 /* Save stack on entry and restore it on exit. Add a try_finally
1140 block to achieve this. */
1141 build_stack_save_restore (&stack_save, &stack_restore);
1143 gimple_set_location (stack_save, start_locus);
1144 gimple_set_location (stack_restore, end_locus);
1146 gimplify_seq_add_stmt (&cleanup, stack_restore);
1149 /* Add clobbers for all variables that go out of scope. */
1150 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1152 if (TREE_CODE (t) == VAR_DECL
1153 && !is_global_var (t)
1154 && DECL_CONTEXT (t) == current_function_decl
1155 && !DECL_HARD_REGISTER (t)
1156 && !TREE_THIS_VOLATILE (t)
1157 && !DECL_HAS_VALUE_EXPR_P (t)
1158 /* Only care for variables that have to be in memory. Others
1159 will be rewritten into SSA names, hence moved to the top-level. */
1160 && !is_gimple_reg (t)
1161 && flag_stack_reuse != SR_NONE)
1163 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1164 gimple clobber_stmt;
1165 TREE_THIS_VOLATILE (clobber) = 1;
1166 clobber_stmt = gimple_build_assign (t, clobber);
1167 gimple_set_location (clobber_stmt, end_locus);
1168 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1172 if (cleanup)
1174 gimple gs;
1175 gimple_seq new_body;
1177 new_body = NULL;
1178 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1179 GIMPLE_TRY_FINALLY);
1181 if (stack_save)
1182 gimplify_seq_add_stmt (&new_body, stack_save);
1183 gimplify_seq_add_stmt (&new_body, gs);
1184 gimple_bind_set_body (gimple_bind, new_body);
1187 gimplify_ctxp->save_stack = old_save_stack;
1188 gimple_pop_bind_expr ();
1190 gimplify_seq_add_stmt (pre_p, gimple_bind);
1192 if (temp)
1194 *expr_p = temp;
1195 return GS_OK;
1198 *expr_p = NULL_TREE;
1199 return GS_ALL_DONE;
1202 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1203 GIMPLE value, it is assigned to a new temporary and the statement is
1204 re-written to return the temporary.
1206 PRE_P points to the sequence where side effects that must happen before
1207 STMT should be stored. */
1209 static enum gimplify_status
1210 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1212 gimple ret;
1213 tree ret_expr = TREE_OPERAND (stmt, 0);
1214 tree result_decl, result;
1216 if (ret_expr == error_mark_node)
1217 return GS_ERROR;
1219 /* Implicit _Cilk_sync must be inserted right before any return statement
1220 if there is a _Cilk_spawn in the function. If the user has provided a
1221 _Cilk_sync, the optimizer should remove this duplicate one. */
1222 if (fn_contains_cilk_spawn_p (cfun))
1224 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1225 gimplify_and_add (impl_sync, pre_p);
1228 if (!ret_expr
1229 || TREE_CODE (ret_expr) == RESULT_DECL
1230 || ret_expr == error_mark_node)
1232 gimple ret = gimple_build_return (ret_expr);
1233 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1234 gimplify_seq_add_stmt (pre_p, ret);
1235 return GS_ALL_DONE;
1238 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1239 result_decl = NULL_TREE;
1240 else
1242 result_decl = TREE_OPERAND (ret_expr, 0);
1244 /* See through a return by reference. */
1245 if (TREE_CODE (result_decl) == INDIRECT_REF)
1246 result_decl = TREE_OPERAND (result_decl, 0);
1248 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1249 || TREE_CODE (ret_expr) == INIT_EXPR)
1250 && TREE_CODE (result_decl) == RESULT_DECL);
1253 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1254 Recall that aggregate_value_p is FALSE for any aggregate type that is
1255 returned in registers. If we're returning values in registers, then
1256 we don't want to extend the lifetime of the RESULT_DECL, particularly
1257 across another call. In addition, for those aggregates for which
1258 hard_function_value generates a PARALLEL, we'll die during normal
1259 expansion of structure assignments; there's special code in expand_return
1260 to handle this case that does not exist in expand_expr. */
1261 if (!result_decl)
1262 result = NULL_TREE;
1263 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1265 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1267 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1268 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1269 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1270 should be effectively allocated by the caller, i.e. all calls to
1271 this function must be subject to the Return Slot Optimization. */
1272 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1273 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1275 result = result_decl;
1277 else if (gimplify_ctxp->return_temp)
1278 result = gimplify_ctxp->return_temp;
1279 else
1281 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1283 /* ??? With complex control flow (usually involving abnormal edges),
1284 we can wind up warning about an uninitialized value for this. Due
1285 to how this variable is constructed and initialized, this is never
1286 true. Give up and never warn. */
1287 TREE_NO_WARNING (result) = 1;
1289 gimplify_ctxp->return_temp = result;
1292 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1293 Then gimplify the whole thing. */
1294 if (result != result_decl)
1295 TREE_OPERAND (ret_expr, 0) = result;
1297 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1299 ret = gimple_build_return (result);
1300 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1301 gimplify_seq_add_stmt (pre_p, ret);
1303 return GS_ALL_DONE;
1306 /* Gimplify a variable-length array DECL. */
1308 static void
1309 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1311 /* This is a variable-sized decl. Simplify its size and mark it
1312 for deferred expansion. */
1313 tree t, addr, ptr_type;
1315 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1316 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1318 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1319 if (DECL_HAS_VALUE_EXPR_P (decl))
1320 return;
1322 /* All occurrences of this decl in final gimplified code will be
1323 replaced by indirection. Setting DECL_VALUE_EXPR does two
1324 things: First, it lets the rest of the gimplifier know what
1325 replacement to use. Second, it lets the debug info know
1326 where to find the value. */
1327 ptr_type = build_pointer_type (TREE_TYPE (decl));
1328 addr = create_tmp_var (ptr_type, get_name (decl));
1329 DECL_IGNORED_P (addr) = 0;
1330 t = build_fold_indirect_ref (addr);
1331 TREE_THIS_NOTRAP (t) = 1;
1332 SET_DECL_VALUE_EXPR (decl, t);
1333 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1335 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1336 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1337 size_int (DECL_ALIGN (decl)));
1338 /* The call has been built for a variable-sized object. */
1339 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1340 t = fold_convert (ptr_type, t);
1341 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1343 gimplify_and_add (t, seq_p);
1345 /* Indicate that we need to restore the stack level when the
1346 enclosing BIND_EXPR is exited. */
1347 gimplify_ctxp->save_stack = true;
1350 /* A helper function to be called via walk_tree. Mark all labels under *TP
1351 as being forced. To be called for DECL_INITIAL of static variables. */
1353 static tree
1354 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1356 if (TYPE_P (*tp))
1357 *walk_subtrees = 0;
1358 if (TREE_CODE (*tp) == LABEL_DECL)
1359 FORCED_LABEL (*tp) = 1;
1361 return NULL_TREE;
1364 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1365 and initialization explicit. */
1367 static enum gimplify_status
1368 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1370 tree stmt = *stmt_p;
1371 tree decl = DECL_EXPR_DECL (stmt);
1373 *stmt_p = NULL_TREE;
1375 if (TREE_TYPE (decl) == error_mark_node)
1376 return GS_ERROR;
1378 if ((TREE_CODE (decl) == TYPE_DECL
1379 || TREE_CODE (decl) == VAR_DECL)
1380 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1381 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1383 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1384 in case its size expressions contain problematic nodes like CALL_EXPR. */
1385 if (TREE_CODE (decl) == TYPE_DECL
1386 && DECL_ORIGINAL_TYPE (decl)
1387 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1388 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1390 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1392 tree init = DECL_INITIAL (decl);
1394 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1395 || (!TREE_STATIC (decl)
1396 && flag_stack_check == GENERIC_STACK_CHECK
1397 && compare_tree_int (DECL_SIZE_UNIT (decl),
1398 STACK_CHECK_MAX_VAR_SIZE) > 0))
1399 gimplify_vla_decl (decl, seq_p);
1401 /* Some front ends do not explicitly declare all anonymous
1402 artificial variables. We compensate here by declaring the
1403 variables, though it would be better if the front ends would
1404 explicitly declare them. */
1405 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1406 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1407 gimple_add_tmp_var (decl);
1409 if (init && init != error_mark_node)
1411 if (!TREE_STATIC (decl))
1413 DECL_INITIAL (decl) = NULL_TREE;
1414 init = build2 (INIT_EXPR, void_type_node, decl, init);
1415 gimplify_and_add (init, seq_p);
1416 ggc_free (init);
1418 else
1419 /* We must still examine initializers for static variables
1420 as they may contain a label address. */
1421 walk_tree (&init, force_labels_r, NULL, NULL);
1425 return GS_ALL_DONE;
1428 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1429 and replacing the LOOP_EXPR with goto, but if the loop contains an
1430 EXIT_EXPR, we need to append a label for it to jump to. */
1432 static enum gimplify_status
1433 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1435 tree saved_label = gimplify_ctxp->exit_label;
1436 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1438 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1440 gimplify_ctxp->exit_label = NULL_TREE;
1442 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1444 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1446 if (gimplify_ctxp->exit_label)
1447 gimplify_seq_add_stmt (pre_p,
1448 gimple_build_label (gimplify_ctxp->exit_label));
1450 gimplify_ctxp->exit_label = saved_label;
1452 *expr_p = NULL;
1453 return GS_ALL_DONE;
1456 /* Gimplify a statement list onto a sequence. These may be created either
1457 by an enlightened front-end, or by shortcut_cond_expr. */
1459 static enum gimplify_status
1460 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1462 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1464 tree_stmt_iterator i = tsi_start (*expr_p);
1466 while (!tsi_end_p (i))
1468 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1469 tsi_delink (&i);
1472 if (temp)
1474 *expr_p = temp;
1475 return GS_OK;
1478 return GS_ALL_DONE;
1482 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1483 branch to. */
1485 static enum gimplify_status
1486 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1488 tree switch_expr = *expr_p;
1489 gimple_seq switch_body_seq = NULL;
1490 enum gimplify_status ret;
1491 tree index_type = TREE_TYPE (switch_expr);
1492 if (index_type == NULL_TREE)
1493 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1495 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1496 fb_rvalue);
1497 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1498 return ret;
1500 if (SWITCH_BODY (switch_expr))
1502 vec<tree> labels;
1503 vec<tree> saved_labels;
1504 tree default_case = NULL_TREE;
1505 gimple gimple_switch;
1507 /* If someone can be bothered to fill in the labels, they can
1508 be bothered to null out the body too. */
1509 gcc_assert (!SWITCH_LABELS (switch_expr));
1511 /* Save old labels, get new ones from body, then restore the old
1512 labels. Save all the things from the switch body to append after. */
1513 saved_labels = gimplify_ctxp->case_labels;
1514 gimplify_ctxp->case_labels.create (8);
1516 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1517 labels = gimplify_ctxp->case_labels;
1518 gimplify_ctxp->case_labels = saved_labels;
1520 preprocess_case_label_vec_for_gimple (labels, index_type,
1521 &default_case);
1523 if (!default_case)
1525 gimple new_default;
1527 default_case
1528 = build_case_label (NULL_TREE, NULL_TREE,
1529 create_artificial_label (UNKNOWN_LOCATION));
1530 new_default = gimple_build_label (CASE_LABEL (default_case));
1531 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1534 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1535 default_case, labels);
1536 gimplify_seq_add_stmt (pre_p, gimple_switch);
1537 gimplify_seq_add_seq (pre_p, switch_body_seq);
1538 labels.release ();
1540 else
1541 gcc_assert (SWITCH_LABELS (switch_expr));
1543 return GS_ALL_DONE;
1546 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1548 static enum gimplify_status
1549 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1551 struct gimplify_ctx *ctxp;
1552 gimple gimple_label;
1554 /* Invalid programs can play Duff's Device type games with, for example,
1555 #pragma omp parallel. At least in the C front end, we don't
1556 detect such invalid branches until after gimplification, in the
1557 diagnose_omp_blocks pass. */
1558 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1559 if (ctxp->case_labels.exists ())
1560 break;
1562 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1563 ctxp->case_labels.safe_push (*expr_p);
1564 gimplify_seq_add_stmt (pre_p, gimple_label);
1566 return GS_ALL_DONE;
1569 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1570 if necessary. */
1572 tree
1573 build_and_jump (tree *label_p)
1575 if (label_p == NULL)
1576 /* If there's nowhere to jump, just fall through. */
1577 return NULL_TREE;
1579 if (*label_p == NULL_TREE)
1581 tree label = create_artificial_label (UNKNOWN_LOCATION);
1582 *label_p = label;
1585 return build1 (GOTO_EXPR, void_type_node, *label_p);
1588 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1589 This also involves building a label to jump to and communicating it to
1590 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1592 static enum gimplify_status
1593 gimplify_exit_expr (tree *expr_p)
1595 tree cond = TREE_OPERAND (*expr_p, 0);
1596 tree expr;
1598 expr = build_and_jump (&gimplify_ctxp->exit_label);
1599 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1600 *expr_p = expr;
1602 return GS_OK;
1605 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1606 different from its canonical type, wrap the whole thing inside a
1607 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1608 type.
1610 The canonical type of a COMPONENT_REF is the type of the field being
1611 referenced--unless the field is a bit-field which can be read directly
1612 in a smaller mode, in which case the canonical type is the
1613 sign-appropriate type corresponding to that mode. */
1615 static void
1616 canonicalize_component_ref (tree *expr_p)
1618 tree expr = *expr_p;
1619 tree type;
1621 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1623 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1624 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1625 else
1626 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1628 /* One could argue that all the stuff below is not necessary for
1629 the non-bitfield case and declare it a FE error if type
1630 adjustment would be needed. */
1631 if (TREE_TYPE (expr) != type)
1633 #ifdef ENABLE_TYPES_CHECKING
1634 tree old_type = TREE_TYPE (expr);
1635 #endif
1636 int type_quals;
1638 /* We need to preserve qualifiers and propagate them from
1639 operand 0. */
1640 type_quals = TYPE_QUALS (type)
1641 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1642 if (TYPE_QUALS (type) != type_quals)
1643 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1645 /* Set the type of the COMPONENT_REF to the underlying type. */
1646 TREE_TYPE (expr) = type;
1648 #ifdef ENABLE_TYPES_CHECKING
1649 /* It is now a FE error, if the conversion from the canonical
1650 type to the original expression type is not useless. */
1651 gcc_assert (useless_type_conversion_p (old_type, type));
1652 #endif
1656 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1657 to foo, embed that change in the ADDR_EXPR by converting
1658 T array[U];
1659 (T *)&array
1661 &array[L]
1662 where L is the lower bound. For simplicity, only do this for constant
1663 lower bound.
1664 The constraint is that the type of &array[L] is trivially convertible
1665 to T *. */
1667 static void
1668 canonicalize_addr_expr (tree *expr_p)
1670 tree expr = *expr_p;
1671 tree addr_expr = TREE_OPERAND (expr, 0);
1672 tree datype, ddatype, pddatype;
1674 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1675 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1676 || TREE_CODE (addr_expr) != ADDR_EXPR)
1677 return;
1679 /* The addr_expr type should be a pointer to an array. */
1680 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1681 if (TREE_CODE (datype) != ARRAY_TYPE)
1682 return;
1684 /* The pointer to element type shall be trivially convertible to
1685 the expression pointer type. */
1686 ddatype = TREE_TYPE (datype);
1687 pddatype = build_pointer_type (ddatype);
1688 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1689 pddatype))
1690 return;
1692 /* The lower bound and element sizes must be constant. */
1693 if (!TYPE_SIZE_UNIT (ddatype)
1694 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1695 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1696 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1697 return;
1699 /* All checks succeeded. Build a new node to merge the cast. */
1700 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1701 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1702 NULL_TREE, NULL_TREE);
1703 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1705 /* We can have stripped a required restrict qualifier above. */
1706 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1707 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1710 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1711 underneath as appropriate. */
1713 static enum gimplify_status
1714 gimplify_conversion (tree *expr_p)
1716 location_t loc = EXPR_LOCATION (*expr_p);
1717 gcc_assert (CONVERT_EXPR_P (*expr_p));
1719 /* Then strip away all but the outermost conversion. */
1720 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1722 /* And remove the outermost conversion if it's useless. */
1723 if (tree_ssa_useless_type_conversion (*expr_p))
1724 *expr_p = TREE_OPERAND (*expr_p, 0);
1726 /* If we still have a conversion at the toplevel,
1727 then canonicalize some constructs. */
1728 if (CONVERT_EXPR_P (*expr_p))
1730 tree sub = TREE_OPERAND (*expr_p, 0);
1732 /* If a NOP conversion is changing the type of a COMPONENT_REF
1733 expression, then canonicalize its type now in order to expose more
1734 redundant conversions. */
1735 if (TREE_CODE (sub) == COMPONENT_REF)
1736 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1738 /* If a NOP conversion is changing a pointer to array of foo
1739 to a pointer to foo, embed that change in the ADDR_EXPR. */
1740 else if (TREE_CODE (sub) == ADDR_EXPR)
1741 canonicalize_addr_expr (expr_p);
1744 /* If we have a conversion to a non-register type force the
1745 use of a VIEW_CONVERT_EXPR instead. */
1746 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1747 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1748 TREE_OPERAND (*expr_p, 0));
1750 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
1751 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
1752 TREE_SET_CODE (*expr_p, NOP_EXPR);
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 if (want_value)
2294 return GS_ALL_DONE;
2296 nargs = call_expr_nargs (*expr_p);
2297 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2298 auto_vec<tree> vargs (nargs);
2300 for (i = 0; i < nargs; i++)
2302 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2303 EXPR_LOCATION (*expr_p));
2304 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2306 gimple call = gimple_build_call_internal_vec (ifn, vargs);
2307 gimplify_seq_add_stmt (pre_p, call);
2308 return GS_ALL_DONE;
2311 /* This may be a call to a builtin function.
2313 Builtin function calls may be transformed into different
2314 (and more efficient) builtin function calls under certain
2315 circumstances. Unfortunately, gimplification can muck things
2316 up enough that the builtin expanders are not aware that certain
2317 transformations are still valid.
2319 So we attempt transformation/gimplification of the call before
2320 we gimplify the CALL_EXPR. At this time we do not manage to
2321 transform all calls in the same manner as the expanders do, but
2322 we do transform most of them. */
2323 fndecl = get_callee_fndecl (*expr_p);
2324 if (fndecl
2325 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2326 switch (DECL_FUNCTION_CODE (fndecl))
2328 case BUILT_IN_VA_START:
2330 builtin_va_start_p = TRUE;
2331 if (call_expr_nargs (*expr_p) < 2)
2333 error ("too few arguments to function %<va_start%>");
2334 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2335 return GS_OK;
2338 if (fold_builtin_next_arg (*expr_p, true))
2340 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2341 return GS_OK;
2343 break;
2345 case BUILT_IN_LINE:
2347 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2348 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2349 return GS_OK;
2351 case BUILT_IN_FILE:
2353 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2354 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2355 return GS_OK;
2357 case BUILT_IN_FUNCTION:
2359 const char *function;
2360 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2361 *expr_p = build_string_literal (strlen (function) + 1, function);
2362 return GS_OK;
2364 default:
2367 if (fndecl && DECL_BUILT_IN (fndecl))
2369 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2370 if (new_tree && new_tree != *expr_p)
2372 /* There was a transformation of this call which computes the
2373 same value, but in a more efficient way. Return and try
2374 again. */
2375 *expr_p = new_tree;
2376 return GS_OK;
2380 /* Remember the original function pointer type. */
2381 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2383 /* There is a sequence point before the call, so any side effects in
2384 the calling expression must occur before the actual call. Force
2385 gimplify_expr to use an internal post queue. */
2386 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2387 is_gimple_call_addr, fb_rvalue);
2389 nargs = call_expr_nargs (*expr_p);
2391 /* Get argument types for verification. */
2392 fndecl = get_callee_fndecl (*expr_p);
2393 parms = NULL_TREE;
2394 if (fndecl)
2395 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2396 else
2397 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2399 if (fndecl && DECL_ARGUMENTS (fndecl))
2400 p = DECL_ARGUMENTS (fndecl);
2401 else if (parms)
2402 p = parms;
2403 else
2404 p = NULL_TREE;
2405 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2408 /* If the last argument is __builtin_va_arg_pack () and it is not
2409 passed as a named argument, decrease the number of CALL_EXPR
2410 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2411 if (!p
2412 && i < nargs
2413 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2415 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2416 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2418 if (last_arg_fndecl
2419 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2420 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2421 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2423 tree call = *expr_p;
2425 --nargs;
2426 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2427 CALL_EXPR_FN (call),
2428 nargs, CALL_EXPR_ARGP (call));
2430 /* Copy all CALL_EXPR flags, location and block, except
2431 CALL_EXPR_VA_ARG_PACK flag. */
2432 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2433 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2434 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2435 = CALL_EXPR_RETURN_SLOT_OPT (call);
2436 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2437 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2439 /* Set CALL_EXPR_VA_ARG_PACK. */
2440 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2444 /* Finally, gimplify the function arguments. */
2445 if (nargs > 0)
2447 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2448 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2449 PUSH_ARGS_REVERSED ? i-- : i++)
2451 enum gimplify_status t;
2453 /* Avoid gimplifying the second argument to va_start, which needs to
2454 be the plain PARM_DECL. */
2455 if ((i != 1) || !builtin_va_start_p)
2457 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2458 EXPR_LOCATION (*expr_p));
2460 if (t == GS_ERROR)
2461 ret = GS_ERROR;
2466 /* Verify the function result. */
2467 if (want_value && fndecl
2468 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2470 error_at (loc, "using result of function returning %<void%>");
2471 ret = GS_ERROR;
2474 /* Try this again in case gimplification exposed something. */
2475 if (ret != GS_ERROR)
2477 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2479 if (new_tree && new_tree != *expr_p)
2481 /* There was a transformation of this call which computes the
2482 same value, but in a more efficient way. Return and try
2483 again. */
2484 *expr_p = new_tree;
2485 return GS_OK;
2488 else
2490 *expr_p = error_mark_node;
2491 return GS_ERROR;
2494 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2495 decl. This allows us to eliminate redundant or useless
2496 calls to "const" functions. */
2497 if (TREE_CODE (*expr_p) == CALL_EXPR)
2499 int flags = call_expr_flags (*expr_p);
2500 if (flags & (ECF_CONST | ECF_PURE)
2501 /* An infinite loop is considered a side effect. */
2502 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2503 TREE_SIDE_EFFECTS (*expr_p) = 0;
2506 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2507 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2508 form and delegate the creation of a GIMPLE_CALL to
2509 gimplify_modify_expr. This is always possible because when
2510 WANT_VALUE is true, the caller wants the result of this call into
2511 a temporary, which means that we will emit an INIT_EXPR in
2512 internal_get_tmp_var which will then be handled by
2513 gimplify_modify_expr. */
2514 if (!want_value)
2516 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2517 have to do is replicate it as a GIMPLE_CALL tuple. */
2518 gimple_stmt_iterator gsi;
2519 call = gimple_build_call_from_tree (*expr_p);
2520 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2521 notice_special_calls (call);
2522 gimplify_seq_add_stmt (pre_p, call);
2523 gsi = gsi_last (*pre_p);
2524 maybe_fold_stmt (&gsi);
2525 *expr_p = NULL_TREE;
2527 else
2528 /* Remember the original function type. */
2529 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2530 CALL_EXPR_FN (*expr_p));
2532 return ret;
2535 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2536 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2538 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2539 condition is true or false, respectively. If null, we should generate
2540 our own to skip over the evaluation of this specific expression.
2542 LOCUS is the source location of the COND_EXPR.
2544 This function is the tree equivalent of do_jump.
2546 shortcut_cond_r should only be called by shortcut_cond_expr. */
2548 static tree
2549 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2550 location_t locus)
2552 tree local_label = NULL_TREE;
2553 tree t, expr = NULL;
2555 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2556 retain the shortcut semantics. Just insert the gotos here;
2557 shortcut_cond_expr will append the real blocks later. */
2558 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2560 location_t new_locus;
2562 /* Turn if (a && b) into
2564 if (a); else goto no;
2565 if (b) goto yes; else goto no;
2566 (no:) */
2568 if (false_label_p == NULL)
2569 false_label_p = &local_label;
2571 /* Keep the original source location on the first 'if'. */
2572 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2573 append_to_statement_list (t, &expr);
2575 /* Set the source location of the && on the second 'if'. */
2576 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2577 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2578 new_locus);
2579 append_to_statement_list (t, &expr);
2581 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2583 location_t new_locus;
2585 /* Turn if (a || b) into
2587 if (a) goto yes;
2588 if (b) goto yes; else goto no;
2589 (yes:) */
2591 if (true_label_p == NULL)
2592 true_label_p = &local_label;
2594 /* Keep the original source location on the first 'if'. */
2595 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2596 append_to_statement_list (t, &expr);
2598 /* Set the source location of the || on the second 'if'. */
2599 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2600 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2601 new_locus);
2602 append_to_statement_list (t, &expr);
2604 else if (TREE_CODE (pred) == COND_EXPR
2605 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2606 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2608 location_t new_locus;
2610 /* As long as we're messing with gotos, turn if (a ? b : c) into
2611 if (a)
2612 if (b) goto yes; else goto no;
2613 else
2614 if (c) goto yes; else goto no;
2616 Don't do this if one of the arms has void type, which can happen
2617 in C++ when the arm is throw. */
2619 /* Keep the original source location on the first 'if'. Set the source
2620 location of the ? on the second 'if'. */
2621 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2622 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2623 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2624 false_label_p, locus),
2625 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2626 false_label_p, new_locus));
2628 else
2630 expr = build3 (COND_EXPR, void_type_node, pred,
2631 build_and_jump (true_label_p),
2632 build_and_jump (false_label_p));
2633 SET_EXPR_LOCATION (expr, locus);
2636 if (local_label)
2638 t = build1 (LABEL_EXPR, void_type_node, local_label);
2639 append_to_statement_list (t, &expr);
2642 return expr;
2645 /* Given a conditional expression EXPR with short-circuit boolean
2646 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2647 predicate apart into the equivalent sequence of conditionals. */
2649 static tree
2650 shortcut_cond_expr (tree expr)
2652 tree pred = TREE_OPERAND (expr, 0);
2653 tree then_ = TREE_OPERAND (expr, 1);
2654 tree else_ = TREE_OPERAND (expr, 2);
2655 tree true_label, false_label, end_label, t;
2656 tree *true_label_p;
2657 tree *false_label_p;
2658 bool emit_end, emit_false, jump_over_else;
2659 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2660 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2662 /* First do simple transformations. */
2663 if (!else_se)
2665 /* If there is no 'else', turn
2666 if (a && b) then c
2667 into
2668 if (a) if (b) then c. */
2669 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2671 /* Keep the original source location on the first 'if'. */
2672 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2673 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2674 /* Set the source location of the && on the second 'if'. */
2675 if (EXPR_HAS_LOCATION (pred))
2676 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2677 then_ = shortcut_cond_expr (expr);
2678 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2679 pred = TREE_OPERAND (pred, 0);
2680 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2681 SET_EXPR_LOCATION (expr, locus);
2685 if (!then_se)
2687 /* If there is no 'then', turn
2688 if (a || b); else d
2689 into
2690 if (a); else if (b); else d. */
2691 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2693 /* Keep the original source location on the first 'if'. */
2694 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2695 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2696 /* Set the source location of the || on the second 'if'. */
2697 if (EXPR_HAS_LOCATION (pred))
2698 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2699 else_ = shortcut_cond_expr (expr);
2700 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2701 pred = TREE_OPERAND (pred, 0);
2702 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2703 SET_EXPR_LOCATION (expr, locus);
2707 /* If we're done, great. */
2708 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2709 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2710 return expr;
2712 /* Otherwise we need to mess with gotos. Change
2713 if (a) c; else d;
2715 if (a); else goto no;
2716 c; goto end;
2717 no: d; end:
2718 and recursively gimplify the condition. */
2720 true_label = false_label = end_label = NULL_TREE;
2722 /* If our arms just jump somewhere, hijack those labels so we don't
2723 generate jumps to jumps. */
2725 if (then_
2726 && TREE_CODE (then_) == GOTO_EXPR
2727 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2729 true_label = GOTO_DESTINATION (then_);
2730 then_ = NULL;
2731 then_se = false;
2734 if (else_
2735 && TREE_CODE (else_) == GOTO_EXPR
2736 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2738 false_label = GOTO_DESTINATION (else_);
2739 else_ = NULL;
2740 else_se = false;
2743 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2744 if (true_label)
2745 true_label_p = &true_label;
2746 else
2747 true_label_p = NULL;
2749 /* The 'else' branch also needs a label if it contains interesting code. */
2750 if (false_label || else_se)
2751 false_label_p = &false_label;
2752 else
2753 false_label_p = NULL;
2755 /* If there was nothing else in our arms, just forward the label(s). */
2756 if (!then_se && !else_se)
2757 return shortcut_cond_r (pred, true_label_p, false_label_p,
2758 EXPR_LOC_OR_LOC (expr, input_location));
2760 /* If our last subexpression already has a terminal label, reuse it. */
2761 if (else_se)
2762 t = expr_last (else_);
2763 else if (then_se)
2764 t = expr_last (then_);
2765 else
2766 t = NULL;
2767 if (t && TREE_CODE (t) == LABEL_EXPR)
2768 end_label = LABEL_EXPR_LABEL (t);
2770 /* If we don't care about jumping to the 'else' branch, jump to the end
2771 if the condition is false. */
2772 if (!false_label_p)
2773 false_label_p = &end_label;
2775 /* We only want to emit these labels if we aren't hijacking them. */
2776 emit_end = (end_label == NULL_TREE);
2777 emit_false = (false_label == NULL_TREE);
2779 /* We only emit the jump over the else clause if we have to--if the
2780 then clause may fall through. Otherwise we can wind up with a
2781 useless jump and a useless label at the end of gimplified code,
2782 which will cause us to think that this conditional as a whole
2783 falls through even if it doesn't. If we then inline a function
2784 which ends with such a condition, that can cause us to issue an
2785 inappropriate warning about control reaching the end of a
2786 non-void function. */
2787 jump_over_else = block_may_fallthru (then_);
2789 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2790 EXPR_LOC_OR_LOC (expr, input_location));
2792 expr = NULL;
2793 append_to_statement_list (pred, &expr);
2795 append_to_statement_list (then_, &expr);
2796 if (else_se)
2798 if (jump_over_else)
2800 tree last = expr_last (expr);
2801 t = build_and_jump (&end_label);
2802 if (EXPR_HAS_LOCATION (last))
2803 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2804 append_to_statement_list (t, &expr);
2806 if (emit_false)
2808 t = build1 (LABEL_EXPR, void_type_node, false_label);
2809 append_to_statement_list (t, &expr);
2811 append_to_statement_list (else_, &expr);
2813 if (emit_end && end_label)
2815 t = build1 (LABEL_EXPR, void_type_node, end_label);
2816 append_to_statement_list (t, &expr);
2819 return expr;
2822 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2824 tree
2825 gimple_boolify (tree expr)
2827 tree type = TREE_TYPE (expr);
2828 location_t loc = EXPR_LOCATION (expr);
2830 if (TREE_CODE (expr) == NE_EXPR
2831 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2832 && integer_zerop (TREE_OPERAND (expr, 1)))
2834 tree call = TREE_OPERAND (expr, 0);
2835 tree fn = get_callee_fndecl (call);
2837 /* For __builtin_expect ((long) (x), y) recurse into x as well
2838 if x is truth_value_p. */
2839 if (fn
2840 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2841 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2842 && call_expr_nargs (call) == 2)
2844 tree arg = CALL_EXPR_ARG (call, 0);
2845 if (arg)
2847 if (TREE_CODE (arg) == NOP_EXPR
2848 && TREE_TYPE (arg) == TREE_TYPE (call))
2849 arg = TREE_OPERAND (arg, 0);
2850 if (truth_value_p (TREE_CODE (arg)))
2852 arg = gimple_boolify (arg);
2853 CALL_EXPR_ARG (call, 0)
2854 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2860 switch (TREE_CODE (expr))
2862 case TRUTH_AND_EXPR:
2863 case TRUTH_OR_EXPR:
2864 case TRUTH_XOR_EXPR:
2865 case TRUTH_ANDIF_EXPR:
2866 case TRUTH_ORIF_EXPR:
2867 /* Also boolify the arguments of truth exprs. */
2868 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2869 /* FALLTHRU */
2871 case TRUTH_NOT_EXPR:
2872 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2874 /* These expressions always produce boolean results. */
2875 if (TREE_CODE (type) != BOOLEAN_TYPE)
2876 TREE_TYPE (expr) = boolean_type_node;
2877 return expr;
2879 case ANNOTATE_EXPR:
2880 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2882 case annot_expr_ivdep_kind:
2883 case annot_expr_no_vector_kind:
2884 case annot_expr_vector_kind:
2885 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2886 if (TREE_CODE (type) != BOOLEAN_TYPE)
2887 TREE_TYPE (expr) = boolean_type_node;
2888 return expr;
2889 default:
2890 gcc_unreachable ();
2893 default:
2894 if (COMPARISON_CLASS_P (expr))
2896 /* There expressions always prduce boolean results. */
2897 if (TREE_CODE (type) != BOOLEAN_TYPE)
2898 TREE_TYPE (expr) = boolean_type_node;
2899 return expr;
2901 /* Other expressions that get here must have boolean values, but
2902 might need to be converted to the appropriate mode. */
2903 if (TREE_CODE (type) == BOOLEAN_TYPE)
2904 return expr;
2905 return fold_convert_loc (loc, boolean_type_node, expr);
2909 /* Given a conditional expression *EXPR_P without side effects, gimplify
2910 its operands. New statements are inserted to PRE_P. */
2912 static enum gimplify_status
2913 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2915 tree expr = *expr_p, cond;
2916 enum gimplify_status ret, tret;
2917 enum tree_code code;
2919 cond = gimple_boolify (COND_EXPR_COND (expr));
2921 /* We need to handle && and || specially, as their gimplification
2922 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2923 code = TREE_CODE (cond);
2924 if (code == TRUTH_ANDIF_EXPR)
2925 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2926 else if (code == TRUTH_ORIF_EXPR)
2927 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2928 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2929 COND_EXPR_COND (*expr_p) = cond;
2931 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2932 is_gimple_val, fb_rvalue);
2933 ret = MIN (ret, tret);
2934 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2935 is_gimple_val, fb_rvalue);
2937 return MIN (ret, tret);
2940 /* Return true if evaluating EXPR could trap.
2941 EXPR is GENERIC, while tree_could_trap_p can be called
2942 only on GIMPLE. */
2944 static bool
2945 generic_expr_could_trap_p (tree expr)
2947 unsigned i, n;
2949 if (!expr || is_gimple_val (expr))
2950 return false;
2952 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2953 return true;
2955 n = TREE_OPERAND_LENGTH (expr);
2956 for (i = 0; i < n; i++)
2957 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2958 return true;
2960 return false;
2963 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2964 into
2966 if (p) if (p)
2967 t1 = a; a;
2968 else or else
2969 t1 = b; b;
2972 The second form is used when *EXPR_P is of type void.
2974 PRE_P points to the list where side effects that must happen before
2975 *EXPR_P should be stored. */
2977 static enum gimplify_status
2978 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2980 tree expr = *expr_p;
2981 tree type = TREE_TYPE (expr);
2982 location_t loc = EXPR_LOCATION (expr);
2983 tree tmp, arm1, arm2;
2984 enum gimplify_status ret;
2985 tree label_true, label_false, label_cont;
2986 bool have_then_clause_p, have_else_clause_p;
2987 gimple gimple_cond;
2988 enum tree_code pred_code;
2989 gimple_seq seq = NULL;
2991 /* If this COND_EXPR has a value, copy the values into a temporary within
2992 the arms. */
2993 if (!VOID_TYPE_P (type))
2995 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2996 tree result;
2998 /* If either an rvalue is ok or we do not require an lvalue, create the
2999 temporary. But we cannot do that if the type is addressable. */
3000 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3001 && !TREE_ADDRESSABLE (type))
3003 if (gimplify_ctxp->allow_rhs_cond_expr
3004 /* If either branch has side effects or could trap, it can't be
3005 evaluated unconditionally. */
3006 && !TREE_SIDE_EFFECTS (then_)
3007 && !generic_expr_could_trap_p (then_)
3008 && !TREE_SIDE_EFFECTS (else_)
3009 && !generic_expr_could_trap_p (else_))
3010 return gimplify_pure_cond_expr (expr_p, pre_p);
3012 tmp = create_tmp_var (type, "iftmp");
3013 result = tmp;
3016 /* Otherwise, only create and copy references to the values. */
3017 else
3019 type = build_pointer_type (type);
3021 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3022 then_ = build_fold_addr_expr_loc (loc, then_);
3024 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3025 else_ = build_fold_addr_expr_loc (loc, else_);
3027 expr
3028 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3030 tmp = create_tmp_var (type, "iftmp");
3031 result = build_simple_mem_ref_loc (loc, tmp);
3034 /* Build the new then clause, `tmp = then_;'. But don't build the
3035 assignment if the value is void; in C++ it can be if it's a throw. */
3036 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3037 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3039 /* Similarly, build the new else clause, `tmp = else_;'. */
3040 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3041 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3043 TREE_TYPE (expr) = void_type_node;
3044 recalculate_side_effects (expr);
3046 /* Move the COND_EXPR to the prequeue. */
3047 gimplify_stmt (&expr, pre_p);
3049 *expr_p = result;
3050 return GS_ALL_DONE;
3053 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3054 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3055 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3056 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3058 /* Make sure the condition has BOOLEAN_TYPE. */
3059 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3061 /* Break apart && and || conditions. */
3062 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3063 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3065 expr = shortcut_cond_expr (expr);
3067 if (expr != *expr_p)
3069 *expr_p = expr;
3071 /* We can't rely on gimplify_expr to re-gimplify the expanded
3072 form properly, as cleanups might cause the target labels to be
3073 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3074 set up a conditional context. */
3075 gimple_push_condition ();
3076 gimplify_stmt (expr_p, &seq);
3077 gimple_pop_condition (pre_p);
3078 gimple_seq_add_seq (pre_p, seq);
3080 return GS_ALL_DONE;
3084 /* Now do the normal gimplification. */
3086 /* Gimplify condition. */
3087 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3088 fb_rvalue);
3089 if (ret == GS_ERROR)
3090 return GS_ERROR;
3091 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3093 gimple_push_condition ();
3095 have_then_clause_p = have_else_clause_p = false;
3096 if (TREE_OPERAND (expr, 1) != NULL
3097 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3098 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3099 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3100 == current_function_decl)
3101 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3102 have different locations, otherwise we end up with incorrect
3103 location information on the branches. */
3104 && (optimize
3105 || !EXPR_HAS_LOCATION (expr)
3106 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3107 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3109 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3110 have_then_clause_p = true;
3112 else
3113 label_true = create_artificial_label (UNKNOWN_LOCATION);
3114 if (TREE_OPERAND (expr, 2) != NULL
3115 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3116 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3117 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3118 == current_function_decl)
3119 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3120 have different locations, otherwise we end up with incorrect
3121 location information on the branches. */
3122 && (optimize
3123 || !EXPR_HAS_LOCATION (expr)
3124 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3125 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3127 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3128 have_else_clause_p = true;
3130 else
3131 label_false = create_artificial_label (UNKNOWN_LOCATION);
3133 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3134 &arm2);
3136 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3137 label_false);
3139 gimplify_seq_add_stmt (&seq, gimple_cond);
3140 label_cont = NULL_TREE;
3141 if (!have_then_clause_p)
3143 /* For if (...) {} else { code; } put label_true after
3144 the else block. */
3145 if (TREE_OPERAND (expr, 1) == NULL_TREE
3146 && !have_else_clause_p
3147 && TREE_OPERAND (expr, 2) != NULL_TREE)
3148 label_cont = label_true;
3149 else
3151 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3152 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3153 /* For if (...) { code; } else {} or
3154 if (...) { code; } else goto label; or
3155 if (...) { code; return; } else { ... }
3156 label_cont isn't needed. */
3157 if (!have_else_clause_p
3158 && TREE_OPERAND (expr, 2) != NULL_TREE
3159 && gimple_seq_may_fallthru (seq))
3161 gimple g;
3162 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3164 g = gimple_build_goto (label_cont);
3166 /* GIMPLE_COND's are very low level; they have embedded
3167 gotos. This particular embedded goto should not be marked
3168 with the location of the original COND_EXPR, as it would
3169 correspond to the COND_EXPR's condition, not the ELSE or the
3170 THEN arms. To avoid marking it with the wrong location, flag
3171 it as "no location". */
3172 gimple_set_do_not_emit_location (g);
3174 gimplify_seq_add_stmt (&seq, g);
3178 if (!have_else_clause_p)
3180 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3181 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3183 if (label_cont)
3184 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3186 gimple_pop_condition (pre_p);
3187 gimple_seq_add_seq (pre_p, seq);
3189 if (ret == GS_ERROR)
3190 ; /* Do nothing. */
3191 else if (have_then_clause_p || have_else_clause_p)
3192 ret = GS_ALL_DONE;
3193 else
3195 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3196 expr = TREE_OPERAND (expr, 0);
3197 gimplify_stmt (&expr, pre_p);
3200 *expr_p = NULL;
3201 return ret;
3204 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3205 to be marked addressable.
3207 We cannot rely on such an expression being directly markable if a temporary
3208 has been created by the gimplification. In this case, we create another
3209 temporary and initialize it with a copy, which will become a store after we
3210 mark it addressable. This can happen if the front-end passed us something
3211 that it could not mark addressable yet, like a Fortran pass-by-reference
3212 parameter (int) floatvar. */
3214 static void
3215 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3217 while (handled_component_p (*expr_p))
3218 expr_p = &TREE_OPERAND (*expr_p, 0);
3219 if (is_gimple_reg (*expr_p))
3221 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3222 DECL_GIMPLE_REG_P (var) = 0;
3223 *expr_p = var;
3227 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3228 a call to __builtin_memcpy. */
3230 static enum gimplify_status
3231 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3232 gimple_seq *seq_p)
3234 tree t, to, to_ptr, from, from_ptr;
3235 gimple gs;
3236 location_t loc = EXPR_LOCATION (*expr_p);
3238 to = TREE_OPERAND (*expr_p, 0);
3239 from = TREE_OPERAND (*expr_p, 1);
3241 /* Mark the RHS addressable. Beware that it may not be possible to do so
3242 directly if a temporary has been created by the gimplification. */
3243 prepare_gimple_addressable (&from, seq_p);
3245 mark_addressable (from);
3246 from_ptr = build_fold_addr_expr_loc (loc, from);
3247 gimplify_arg (&from_ptr, seq_p, loc);
3249 mark_addressable (to);
3250 to_ptr = build_fold_addr_expr_loc (loc, to);
3251 gimplify_arg (&to_ptr, seq_p, loc);
3253 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3255 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3257 if (want_value)
3259 /* tmp = memcpy() */
3260 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3261 gimple_call_set_lhs (gs, t);
3262 gimplify_seq_add_stmt (seq_p, gs);
3264 *expr_p = build_simple_mem_ref (t);
3265 return GS_ALL_DONE;
3268 gimplify_seq_add_stmt (seq_p, gs);
3269 *expr_p = NULL;
3270 return GS_ALL_DONE;
3273 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3274 a call to __builtin_memset. In this case we know that the RHS is
3275 a CONSTRUCTOR with an empty element list. */
3277 static enum gimplify_status
3278 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3279 gimple_seq *seq_p)
3281 tree t, from, to, to_ptr;
3282 gimple gs;
3283 location_t loc = EXPR_LOCATION (*expr_p);
3285 /* Assert our assumptions, to abort instead of producing wrong code
3286 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3287 not be immediately exposed. */
3288 from = TREE_OPERAND (*expr_p, 1);
3289 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3290 from = TREE_OPERAND (from, 0);
3292 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3293 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3295 /* Now proceed. */
3296 to = TREE_OPERAND (*expr_p, 0);
3298 to_ptr = build_fold_addr_expr_loc (loc, to);
3299 gimplify_arg (&to_ptr, seq_p, loc);
3300 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3302 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3304 if (want_value)
3306 /* tmp = memset() */
3307 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3308 gimple_call_set_lhs (gs, t);
3309 gimplify_seq_add_stmt (seq_p, gs);
3311 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3312 return GS_ALL_DONE;
3315 gimplify_seq_add_stmt (seq_p, gs);
3316 *expr_p = NULL;
3317 return GS_ALL_DONE;
3320 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3321 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3322 assignment. Return non-null if we detect a potential overlap. */
3324 struct gimplify_init_ctor_preeval_data
3326 /* The base decl of the lhs object. May be NULL, in which case we
3327 have to assume the lhs is indirect. */
3328 tree lhs_base_decl;
3330 /* The alias set of the lhs object. */
3331 alias_set_type lhs_alias_set;
3334 static tree
3335 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3337 struct gimplify_init_ctor_preeval_data *data
3338 = (struct gimplify_init_ctor_preeval_data *) xdata;
3339 tree t = *tp;
3341 /* If we find the base object, obviously we have overlap. */
3342 if (data->lhs_base_decl == t)
3343 return t;
3345 /* If the constructor component is indirect, determine if we have a
3346 potential overlap with the lhs. The only bits of information we
3347 have to go on at this point are addressability and alias sets. */
3348 if ((INDIRECT_REF_P (t)
3349 || TREE_CODE (t) == MEM_REF)
3350 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3351 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3352 return t;
3354 /* If the constructor component is a call, determine if it can hide a
3355 potential overlap with the lhs through an INDIRECT_REF like above.
3356 ??? Ugh - this is completely broken. In fact this whole analysis
3357 doesn't look conservative. */
3358 if (TREE_CODE (t) == CALL_EXPR)
3360 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3362 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3363 if (POINTER_TYPE_P (TREE_VALUE (type))
3364 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3365 && alias_sets_conflict_p (data->lhs_alias_set,
3366 get_alias_set
3367 (TREE_TYPE (TREE_VALUE (type)))))
3368 return t;
3371 if (IS_TYPE_OR_DECL_P (t))
3372 *walk_subtrees = 0;
3373 return NULL;
3376 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3377 force values that overlap with the lhs (as described by *DATA)
3378 into temporaries. */
3380 static void
3381 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3382 struct gimplify_init_ctor_preeval_data *data)
3384 enum gimplify_status one;
3386 /* If the value is constant, then there's nothing to pre-evaluate. */
3387 if (TREE_CONSTANT (*expr_p))
3389 /* Ensure it does not have side effects, it might contain a reference to
3390 the object we're initializing. */
3391 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3392 return;
3395 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3396 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3397 return;
3399 /* Recurse for nested constructors. */
3400 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3402 unsigned HOST_WIDE_INT ix;
3403 constructor_elt *ce;
3404 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3406 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3407 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3409 return;
3412 /* If this is a variable sized type, we must remember the size. */
3413 maybe_with_size_expr (expr_p);
3415 /* Gimplify the constructor element to something appropriate for the rhs
3416 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3417 the gimplifier will consider this a store to memory. Doing this
3418 gimplification now means that we won't have to deal with complicated
3419 language-specific trees, nor trees like SAVE_EXPR that can induce
3420 exponential search behavior. */
3421 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3422 if (one == GS_ERROR)
3424 *expr_p = NULL;
3425 return;
3428 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3429 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3430 always be true for all scalars, since is_gimple_mem_rhs insists on a
3431 temporary variable for them. */
3432 if (DECL_P (*expr_p))
3433 return;
3435 /* If this is of variable size, we have no choice but to assume it doesn't
3436 overlap since we can't make a temporary for it. */
3437 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3438 return;
3440 /* Otherwise, we must search for overlap ... */
3441 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3442 return;
3444 /* ... and if found, force the value into a temporary. */
3445 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3448 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3449 a RANGE_EXPR in a CONSTRUCTOR for an array.
3451 var = lower;
3452 loop_entry:
3453 object[var] = value;
3454 if (var == upper)
3455 goto loop_exit;
3456 var = var + 1;
3457 goto loop_entry;
3458 loop_exit:
3460 We increment var _after_ the loop exit check because we might otherwise
3461 fail if upper == TYPE_MAX_VALUE (type for upper).
3463 Note that we never have to deal with SAVE_EXPRs here, because this has
3464 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3466 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3467 gimple_seq *, bool);
3469 static void
3470 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3471 tree value, tree array_elt_type,
3472 gimple_seq *pre_p, bool cleared)
3474 tree loop_entry_label, loop_exit_label, fall_thru_label;
3475 tree var, var_type, cref, tmp;
3477 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3478 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3479 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3481 /* Create and initialize the index variable. */
3482 var_type = TREE_TYPE (upper);
3483 var = create_tmp_var (var_type, NULL);
3484 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3486 /* Add the loop entry label. */
3487 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3489 /* Build the reference. */
3490 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3491 var, NULL_TREE, NULL_TREE);
3493 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3494 the store. Otherwise just assign value to the reference. */
3496 if (TREE_CODE (value) == CONSTRUCTOR)
3497 /* NB we might have to call ourself recursively through
3498 gimplify_init_ctor_eval if the value is a constructor. */
3499 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3500 pre_p, cleared);
3501 else
3502 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3504 /* We exit the loop when the index var is equal to the upper bound. */
3505 gimplify_seq_add_stmt (pre_p,
3506 gimple_build_cond (EQ_EXPR, var, upper,
3507 loop_exit_label, fall_thru_label));
3509 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3511 /* Otherwise, increment the index var... */
3512 tmp = build2 (PLUS_EXPR, var_type, var,
3513 fold_convert (var_type, integer_one_node));
3514 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3516 /* ...and jump back to the loop entry. */
3517 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3519 /* Add the loop exit label. */
3520 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3523 /* Return true if FDECL is accessing a field that is zero sized. */
3525 static bool
3526 zero_sized_field_decl (const_tree fdecl)
3528 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3529 && integer_zerop (DECL_SIZE (fdecl)))
3530 return true;
3531 return false;
3534 /* Return true if TYPE is zero sized. */
3536 static bool
3537 zero_sized_type (const_tree type)
3539 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3540 && integer_zerop (TYPE_SIZE (type)))
3541 return true;
3542 return false;
3545 /* A subroutine of gimplify_init_constructor. Generate individual
3546 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3547 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3548 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3549 zeroed first. */
3551 static void
3552 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3553 gimple_seq *pre_p, bool cleared)
3555 tree array_elt_type = NULL;
3556 unsigned HOST_WIDE_INT ix;
3557 tree purpose, value;
3559 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3560 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3562 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3564 tree cref;
3566 /* NULL values are created above for gimplification errors. */
3567 if (value == NULL)
3568 continue;
3570 if (cleared && initializer_zerop (value))
3571 continue;
3573 /* ??? Here's to hoping the front end fills in all of the indices,
3574 so we don't have to figure out what's missing ourselves. */
3575 gcc_assert (purpose);
3577 /* Skip zero-sized fields, unless value has side-effects. This can
3578 happen with calls to functions returning a zero-sized type, which
3579 we shouldn't discard. As a number of downstream passes don't
3580 expect sets of zero-sized fields, we rely on the gimplification of
3581 the MODIFY_EXPR we make below to drop the assignment statement. */
3582 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3583 continue;
3585 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3586 whole range. */
3587 if (TREE_CODE (purpose) == RANGE_EXPR)
3589 tree lower = TREE_OPERAND (purpose, 0);
3590 tree upper = TREE_OPERAND (purpose, 1);
3592 /* If the lower bound is equal to upper, just treat it as if
3593 upper was the index. */
3594 if (simple_cst_equal (lower, upper))
3595 purpose = upper;
3596 else
3598 gimplify_init_ctor_eval_range (object, lower, upper, value,
3599 array_elt_type, pre_p, cleared);
3600 continue;
3604 if (array_elt_type)
3606 /* Do not use bitsizetype for ARRAY_REF indices. */
3607 if (TYPE_DOMAIN (TREE_TYPE (object)))
3608 purpose
3609 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3610 purpose);
3611 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3612 purpose, NULL_TREE, NULL_TREE);
3614 else
3616 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3617 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3618 unshare_expr (object), purpose, NULL_TREE);
3621 if (TREE_CODE (value) == CONSTRUCTOR
3622 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3623 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3624 pre_p, cleared);
3625 else
3627 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3628 gimplify_and_add (init, pre_p);
3629 ggc_free (init);
3634 /* Return the appropriate RHS predicate for this LHS. */
3636 gimple_predicate
3637 rhs_predicate_for (tree lhs)
3639 if (is_gimple_reg (lhs))
3640 return is_gimple_reg_rhs_or_call;
3641 else
3642 return is_gimple_mem_rhs_or_call;
3645 /* Gimplify a C99 compound literal expression. This just means adding
3646 the DECL_EXPR before the current statement and using its anonymous
3647 decl instead. */
3649 static enum gimplify_status
3650 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3651 bool (*gimple_test_f) (tree),
3652 fallback_t fallback)
3654 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3655 tree decl = DECL_EXPR_DECL (decl_s);
3656 tree init = DECL_INITIAL (decl);
3657 /* Mark the decl as addressable if the compound literal
3658 expression is addressable now, otherwise it is marked too late
3659 after we gimplify the initialization expression. */
3660 if (TREE_ADDRESSABLE (*expr_p))
3661 TREE_ADDRESSABLE (decl) = 1;
3662 /* Otherwise, if we don't need an lvalue and have a literal directly
3663 substitute it. Check if it matches the gimple predicate, as
3664 otherwise we'd generate a new temporary, and we can as well just
3665 use the decl we already have. */
3666 else if (!TREE_ADDRESSABLE (decl)
3667 && init
3668 && (fallback & fb_lvalue) == 0
3669 && gimple_test_f (init))
3671 *expr_p = init;
3672 return GS_OK;
3675 /* Preliminarily mark non-addressed complex variables as eligible
3676 for promotion to gimple registers. We'll transform their uses
3677 as we find them. */
3678 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3679 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3680 && !TREE_THIS_VOLATILE (decl)
3681 && !needs_to_live_in_memory (decl))
3682 DECL_GIMPLE_REG_P (decl) = 1;
3684 /* If the decl is not addressable, then it is being used in some
3685 expression or on the right hand side of a statement, and it can
3686 be put into a readonly data section. */
3687 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3688 TREE_READONLY (decl) = 1;
3690 /* This decl isn't mentioned in the enclosing block, so add it to the
3691 list of temps. FIXME it seems a bit of a kludge to say that
3692 anonymous artificial vars aren't pushed, but everything else is. */
3693 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3694 gimple_add_tmp_var (decl);
3696 gimplify_and_add (decl_s, pre_p);
3697 *expr_p = decl;
3698 return GS_OK;
3701 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3702 return a new CONSTRUCTOR if something changed. */
3704 static tree
3705 optimize_compound_literals_in_ctor (tree orig_ctor)
3707 tree ctor = orig_ctor;
3708 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3709 unsigned int idx, num = vec_safe_length (elts);
3711 for (idx = 0; idx < num; idx++)
3713 tree value = (*elts)[idx].value;
3714 tree newval = value;
3715 if (TREE_CODE (value) == CONSTRUCTOR)
3716 newval = optimize_compound_literals_in_ctor (value);
3717 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3719 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3720 tree decl = DECL_EXPR_DECL (decl_s);
3721 tree init = DECL_INITIAL (decl);
3723 if (!TREE_ADDRESSABLE (value)
3724 && !TREE_ADDRESSABLE (decl)
3725 && init
3726 && TREE_CODE (init) == CONSTRUCTOR)
3727 newval = optimize_compound_literals_in_ctor (init);
3729 if (newval == value)
3730 continue;
3732 if (ctor == orig_ctor)
3734 ctor = copy_node (orig_ctor);
3735 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3736 elts = CONSTRUCTOR_ELTS (ctor);
3738 (*elts)[idx].value = newval;
3740 return ctor;
3743 /* A subroutine of gimplify_modify_expr. Break out elements of a
3744 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3746 Note that we still need to clear any elements that don't have explicit
3747 initializers, so if not all elements are initialized we keep the
3748 original MODIFY_EXPR, we just remove all of the constructor elements.
3750 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3751 GS_ERROR if we would have to create a temporary when gimplifying
3752 this constructor. Otherwise, return GS_OK.
3754 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3756 static enum gimplify_status
3757 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3758 bool want_value, bool notify_temp_creation)
3760 tree object, ctor, type;
3761 enum gimplify_status ret;
3762 vec<constructor_elt, va_gc> *elts;
3764 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3766 if (!notify_temp_creation)
3768 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3769 is_gimple_lvalue, fb_lvalue);
3770 if (ret == GS_ERROR)
3771 return ret;
3774 object = TREE_OPERAND (*expr_p, 0);
3775 ctor = TREE_OPERAND (*expr_p, 1) =
3776 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3777 type = TREE_TYPE (ctor);
3778 elts = CONSTRUCTOR_ELTS (ctor);
3779 ret = GS_ALL_DONE;
3781 switch (TREE_CODE (type))
3783 case RECORD_TYPE:
3784 case UNION_TYPE:
3785 case QUAL_UNION_TYPE:
3786 case ARRAY_TYPE:
3788 struct gimplify_init_ctor_preeval_data preeval_data;
3789 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3790 bool cleared, complete_p, valid_const_initializer;
3792 /* Aggregate types must lower constructors to initialization of
3793 individual elements. The exception is that a CONSTRUCTOR node
3794 with no elements indicates zero-initialization of the whole. */
3795 if (vec_safe_is_empty (elts))
3797 if (notify_temp_creation)
3798 return GS_OK;
3799 break;
3802 /* Fetch information about the constructor to direct later processing.
3803 We might want to make static versions of it in various cases, and
3804 can only do so if it known to be a valid constant initializer. */
3805 valid_const_initializer
3806 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3807 &num_ctor_elements, &complete_p);
3809 /* If a const aggregate variable is being initialized, then it
3810 should never be a lose to promote the variable to be static. */
3811 if (valid_const_initializer
3812 && num_nonzero_elements > 1
3813 && TREE_READONLY (object)
3814 && TREE_CODE (object) == VAR_DECL
3815 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3817 if (notify_temp_creation)
3818 return GS_ERROR;
3819 DECL_INITIAL (object) = ctor;
3820 TREE_STATIC (object) = 1;
3821 if (!DECL_NAME (object))
3822 DECL_NAME (object) = create_tmp_var_name ("C");
3823 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3825 /* ??? C++ doesn't automatically append a .<number> to the
3826 assembler name, and even when it does, it looks at FE private
3827 data structures to figure out what that number should be,
3828 which are not set for this variable. I suppose this is
3829 important for local statics for inline functions, which aren't
3830 "local" in the object file sense. So in order to get a unique
3831 TU-local symbol, we must invoke the lhd version now. */
3832 lhd_set_decl_assembler_name (object);
3834 *expr_p = NULL_TREE;
3835 break;
3838 /* If there are "lots" of initialized elements, even discounting
3839 those that are not address constants (and thus *must* be
3840 computed at runtime), then partition the constructor into
3841 constant and non-constant parts. Block copy the constant
3842 parts in, then generate code for the non-constant parts. */
3843 /* TODO. There's code in cp/typeck.c to do this. */
3845 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3846 /* store_constructor will ignore the clearing of variable-sized
3847 objects. Initializers for such objects must explicitly set
3848 every field that needs to be set. */
3849 cleared = false;
3850 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3851 /* If the constructor isn't complete, clear the whole object
3852 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3854 ??? This ought not to be needed. For any element not present
3855 in the initializer, we should simply set them to zero. Except
3856 we'd need to *find* the elements that are not present, and that
3857 requires trickery to avoid quadratic compile-time behavior in
3858 large cases or excessive memory use in small cases. */
3859 cleared = true;
3860 else if (num_ctor_elements - num_nonzero_elements
3861 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3862 && num_nonzero_elements < num_ctor_elements / 4)
3863 /* If there are "lots" of zeros, it's more efficient to clear
3864 the memory and then set the nonzero elements. */
3865 cleared = true;
3866 else
3867 cleared = false;
3869 /* If there are "lots" of initialized elements, and all of them
3870 are valid address constants, then the entire initializer can
3871 be dropped to memory, and then memcpy'd out. Don't do this
3872 for sparse arrays, though, as it's more efficient to follow
3873 the standard CONSTRUCTOR behavior of memset followed by
3874 individual element initialization. Also don't do this for small
3875 all-zero initializers (which aren't big enough to merit
3876 clearing), and don't try to make bitwise copies of
3877 TREE_ADDRESSABLE types.
3879 We cannot apply such transformation when compiling chkp static
3880 initializer because creation of initializer image in the memory
3881 will require static initialization of bounds for it. It should
3882 result in another gimplification of similar initializer and we
3883 may fall into infinite loop. */
3884 if (valid_const_initializer
3885 && !(cleared || num_nonzero_elements == 0)
3886 && !TREE_ADDRESSABLE (type)
3887 && (!current_function_decl
3888 || !lookup_attribute ("chkp ctor",
3889 DECL_ATTRIBUTES (current_function_decl))))
3891 HOST_WIDE_INT size = int_size_in_bytes (type);
3892 unsigned int align;
3894 /* ??? We can still get unbounded array types, at least
3895 from the C++ front end. This seems wrong, but attempt
3896 to work around it for now. */
3897 if (size < 0)
3899 size = int_size_in_bytes (TREE_TYPE (object));
3900 if (size >= 0)
3901 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3904 /* Find the maximum alignment we can assume for the object. */
3905 /* ??? Make use of DECL_OFFSET_ALIGN. */
3906 if (DECL_P (object))
3907 align = DECL_ALIGN (object);
3908 else
3909 align = TYPE_ALIGN (type);
3911 /* Do a block move either if the size is so small as to make
3912 each individual move a sub-unit move on average, or if it
3913 is so large as to make individual moves inefficient. */
3914 if (size > 0
3915 && num_nonzero_elements > 1
3916 && (size < num_nonzero_elements
3917 || !can_move_by_pieces (size, align)))
3919 if (notify_temp_creation)
3920 return GS_ERROR;
3922 walk_tree (&ctor, force_labels_r, NULL, NULL);
3923 ctor = tree_output_constant_def (ctor);
3924 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3925 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3926 TREE_OPERAND (*expr_p, 1) = ctor;
3928 /* This is no longer an assignment of a CONSTRUCTOR, but
3929 we still may have processing to do on the LHS. So
3930 pretend we didn't do anything here to let that happen. */
3931 return GS_UNHANDLED;
3935 /* If the target is volatile, we have non-zero elements and more than
3936 one field to assign, initialize the target from a temporary. */
3937 if (TREE_THIS_VOLATILE (object)
3938 && !TREE_ADDRESSABLE (type)
3939 && num_nonzero_elements > 0
3940 && vec_safe_length (elts) > 1)
3942 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3943 TREE_OPERAND (*expr_p, 0) = temp;
3944 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3945 *expr_p,
3946 build2 (MODIFY_EXPR, void_type_node,
3947 object, temp));
3948 return GS_OK;
3951 if (notify_temp_creation)
3952 return GS_OK;
3954 /* If there are nonzero elements and if needed, pre-evaluate to capture
3955 elements overlapping with the lhs into temporaries. We must do this
3956 before clearing to fetch the values before they are zeroed-out. */
3957 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3959 preeval_data.lhs_base_decl = get_base_address (object);
3960 if (!DECL_P (preeval_data.lhs_base_decl))
3961 preeval_data.lhs_base_decl = NULL;
3962 preeval_data.lhs_alias_set = get_alias_set (object);
3964 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3965 pre_p, post_p, &preeval_data);
3968 if (cleared)
3970 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3971 Note that we still have to gimplify, in order to handle the
3972 case of variable sized types. Avoid shared tree structures. */
3973 CONSTRUCTOR_ELTS (ctor) = NULL;
3974 TREE_SIDE_EFFECTS (ctor) = 0;
3975 object = unshare_expr (object);
3976 gimplify_stmt (expr_p, pre_p);
3979 /* If we have not block cleared the object, or if there are nonzero
3980 elements in the constructor, add assignments to the individual
3981 scalar fields of the object. */
3982 if (!cleared || num_nonzero_elements > 0)
3983 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3985 *expr_p = NULL_TREE;
3987 break;
3989 case COMPLEX_TYPE:
3991 tree r, i;
3993 if (notify_temp_creation)
3994 return GS_OK;
3996 /* Extract the real and imaginary parts out of the ctor. */
3997 gcc_assert (elts->length () == 2);
3998 r = (*elts)[0].value;
3999 i = (*elts)[1].value;
4000 if (r == NULL || i == NULL)
4002 tree zero = build_zero_cst (TREE_TYPE (type));
4003 if (r == NULL)
4004 r = zero;
4005 if (i == NULL)
4006 i = zero;
4009 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4010 represent creation of a complex value. */
4011 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4013 ctor = build_complex (type, r, i);
4014 TREE_OPERAND (*expr_p, 1) = ctor;
4016 else
4018 ctor = build2 (COMPLEX_EXPR, type, r, i);
4019 TREE_OPERAND (*expr_p, 1) = ctor;
4020 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4021 pre_p,
4022 post_p,
4023 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4024 fb_rvalue);
4027 break;
4029 case VECTOR_TYPE:
4031 unsigned HOST_WIDE_INT ix;
4032 constructor_elt *ce;
4034 if (notify_temp_creation)
4035 return GS_OK;
4037 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4038 if (TREE_CONSTANT (ctor))
4040 bool constant_p = true;
4041 tree value;
4043 /* Even when ctor is constant, it might contain non-*_CST
4044 elements, such as addresses or trapping values like
4045 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4046 in VECTOR_CST nodes. */
4047 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4048 if (!CONSTANT_CLASS_P (value))
4050 constant_p = false;
4051 break;
4054 if (constant_p)
4056 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4057 break;
4060 TREE_CONSTANT (ctor) = 0;
4063 /* Vector types use CONSTRUCTOR all the way through gimple
4064 compilation as a general initializer. */
4065 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4067 enum gimplify_status tret;
4068 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4069 fb_rvalue);
4070 if (tret == GS_ERROR)
4071 ret = GS_ERROR;
4073 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4074 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4076 break;
4078 default:
4079 /* So how did we get a CONSTRUCTOR for a scalar type? */
4080 gcc_unreachable ();
4083 if (ret == GS_ERROR)
4084 return GS_ERROR;
4085 else if (want_value)
4087 *expr_p = object;
4088 return GS_OK;
4090 else
4092 /* If we have gimplified both sides of the initializer but have
4093 not emitted an assignment, do so now. */
4094 if (*expr_p)
4096 tree lhs = TREE_OPERAND (*expr_p, 0);
4097 tree rhs = TREE_OPERAND (*expr_p, 1);
4098 gimple init = gimple_build_assign (lhs, rhs);
4099 gimplify_seq_add_stmt (pre_p, init);
4100 *expr_p = NULL;
4103 return GS_ALL_DONE;
4107 /* Given a pointer value OP0, return a simplified version of an
4108 indirection through OP0, or NULL_TREE if no simplification is
4109 possible. This may only be applied to a rhs of an expression.
4110 Note that the resulting type may be different from the type pointed
4111 to in the sense that it is still compatible from the langhooks
4112 point of view. */
4114 static tree
4115 gimple_fold_indirect_ref_rhs (tree t)
4117 return gimple_fold_indirect_ref (t);
4120 /* Subroutine of gimplify_modify_expr to do simplifications of
4121 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4122 something changes. */
4124 static enum gimplify_status
4125 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4126 gimple_seq *pre_p, gimple_seq *post_p,
4127 bool want_value)
4129 enum gimplify_status ret = GS_UNHANDLED;
4130 bool changed;
4134 changed = false;
4135 switch (TREE_CODE (*from_p))
4137 case VAR_DECL:
4138 /* If we're assigning from a read-only variable initialized with
4139 a constructor, do the direct assignment from the constructor,
4140 but only if neither source nor target are volatile since this
4141 latter assignment might end up being done on a per-field basis. */
4142 if (DECL_INITIAL (*from_p)
4143 && TREE_READONLY (*from_p)
4144 && !TREE_THIS_VOLATILE (*from_p)
4145 && !TREE_THIS_VOLATILE (*to_p)
4146 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4148 tree old_from = *from_p;
4149 enum gimplify_status subret;
4151 /* Move the constructor into the RHS. */
4152 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4154 /* Let's see if gimplify_init_constructor will need to put
4155 it in memory. */
4156 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4157 false, true);
4158 if (subret == GS_ERROR)
4160 /* If so, revert the change. */
4161 *from_p = old_from;
4163 else
4165 ret = GS_OK;
4166 changed = true;
4169 break;
4170 case INDIRECT_REF:
4172 /* If we have code like
4174 *(const A*)(A*)&x
4176 where the type of "x" is a (possibly cv-qualified variant
4177 of "A"), treat the entire expression as identical to "x".
4178 This kind of code arises in C++ when an object is bound
4179 to a const reference, and if "x" is a TARGET_EXPR we want
4180 to take advantage of the optimization below. */
4181 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4182 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4183 if (t)
4185 if (TREE_THIS_VOLATILE (t) != volatile_p)
4187 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4188 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4189 build_fold_addr_expr (t));
4190 if (REFERENCE_CLASS_P (t))
4191 TREE_THIS_VOLATILE (t) = volatile_p;
4193 *from_p = t;
4194 ret = GS_OK;
4195 changed = true;
4197 break;
4200 case TARGET_EXPR:
4202 /* If we are initializing something from a TARGET_EXPR, strip the
4203 TARGET_EXPR and initialize it directly, if possible. This can't
4204 be done if the initializer is void, since that implies that the
4205 temporary is set in some non-trivial way.
4207 ??? What about code that pulls out the temp and uses it
4208 elsewhere? I think that such code never uses the TARGET_EXPR as
4209 an initializer. If I'm wrong, we'll die because the temp won't
4210 have any RTL. In that case, I guess we'll need to replace
4211 references somehow. */
4212 tree init = TARGET_EXPR_INITIAL (*from_p);
4214 if (init
4215 && !VOID_TYPE_P (TREE_TYPE (init)))
4217 *from_p = init;
4218 ret = GS_OK;
4219 changed = true;
4222 break;
4224 case COMPOUND_EXPR:
4225 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4226 caught. */
4227 gimplify_compound_expr (from_p, pre_p, true);
4228 ret = GS_OK;
4229 changed = true;
4230 break;
4232 case CONSTRUCTOR:
4233 /* If we already made some changes, let the front end have a
4234 crack at this before we break it down. */
4235 if (ret != GS_UNHANDLED)
4236 break;
4237 /* If we're initializing from a CONSTRUCTOR, break this into
4238 individual MODIFY_EXPRs. */
4239 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4240 false);
4242 case COND_EXPR:
4243 /* If we're assigning to a non-register type, push the assignment
4244 down into the branches. This is mandatory for ADDRESSABLE types,
4245 since we cannot generate temporaries for such, but it saves a
4246 copy in other cases as well. */
4247 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4249 /* This code should mirror the code in gimplify_cond_expr. */
4250 enum tree_code code = TREE_CODE (*expr_p);
4251 tree cond = *from_p;
4252 tree result = *to_p;
4254 ret = gimplify_expr (&result, pre_p, post_p,
4255 is_gimple_lvalue, fb_lvalue);
4256 if (ret != GS_ERROR)
4257 ret = GS_OK;
4259 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4260 TREE_OPERAND (cond, 1)
4261 = build2 (code, void_type_node, result,
4262 TREE_OPERAND (cond, 1));
4263 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4264 TREE_OPERAND (cond, 2)
4265 = build2 (code, void_type_node, unshare_expr (result),
4266 TREE_OPERAND (cond, 2));
4268 TREE_TYPE (cond) = void_type_node;
4269 recalculate_side_effects (cond);
4271 if (want_value)
4273 gimplify_and_add (cond, pre_p);
4274 *expr_p = unshare_expr (result);
4276 else
4277 *expr_p = cond;
4278 return ret;
4280 break;
4282 case CALL_EXPR:
4283 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4284 return slot so that we don't generate a temporary. */
4285 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4286 && aggregate_value_p (*from_p, *from_p))
4288 bool use_target;
4290 if (!(rhs_predicate_for (*to_p))(*from_p))
4291 /* If we need a temporary, *to_p isn't accurate. */
4292 use_target = false;
4293 /* It's OK to use the return slot directly unless it's an NRV. */
4294 else if (TREE_CODE (*to_p) == RESULT_DECL
4295 && DECL_NAME (*to_p) == NULL_TREE
4296 && needs_to_live_in_memory (*to_p))
4297 use_target = true;
4298 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4299 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4300 /* Don't force regs into memory. */
4301 use_target = false;
4302 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4303 /* It's OK to use the target directly if it's being
4304 initialized. */
4305 use_target = true;
4306 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4307 /* Always use the target and thus RSO for variable-sized types.
4308 GIMPLE cannot deal with a variable-sized assignment
4309 embedded in a call statement. */
4310 use_target = true;
4311 else if (TREE_CODE (*to_p) != SSA_NAME
4312 && (!is_gimple_variable (*to_p)
4313 || needs_to_live_in_memory (*to_p)))
4314 /* Don't use the original target if it's already addressable;
4315 if its address escapes, and the called function uses the
4316 NRV optimization, a conforming program could see *to_p
4317 change before the called function returns; see c++/19317.
4318 When optimizing, the return_slot pass marks more functions
4319 as safe after we have escape info. */
4320 use_target = false;
4321 else
4322 use_target = true;
4324 if (use_target)
4326 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4327 mark_addressable (*to_p);
4330 break;
4332 case WITH_SIZE_EXPR:
4333 /* Likewise for calls that return an aggregate of non-constant size,
4334 since we would not be able to generate a temporary at all. */
4335 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4337 *from_p = TREE_OPERAND (*from_p, 0);
4338 /* We don't change ret in this case because the
4339 WITH_SIZE_EXPR might have been added in
4340 gimplify_modify_expr, so returning GS_OK would lead to an
4341 infinite loop. */
4342 changed = true;
4344 break;
4346 /* If we're initializing from a container, push the initialization
4347 inside it. */
4348 case CLEANUP_POINT_EXPR:
4349 case BIND_EXPR:
4350 case STATEMENT_LIST:
4352 tree wrap = *from_p;
4353 tree t;
4355 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4356 fb_lvalue);
4357 if (ret != GS_ERROR)
4358 ret = GS_OK;
4360 t = voidify_wrapper_expr (wrap, *expr_p);
4361 gcc_assert (t == *expr_p);
4363 if (want_value)
4365 gimplify_and_add (wrap, pre_p);
4366 *expr_p = unshare_expr (*to_p);
4368 else
4369 *expr_p = wrap;
4370 return GS_OK;
4373 case COMPOUND_LITERAL_EXPR:
4375 tree complit = TREE_OPERAND (*expr_p, 1);
4376 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4377 tree decl = DECL_EXPR_DECL (decl_s);
4378 tree init = DECL_INITIAL (decl);
4380 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4381 into struct T x = { 0, 1, 2 } if the address of the
4382 compound literal has never been taken. */
4383 if (!TREE_ADDRESSABLE (complit)
4384 && !TREE_ADDRESSABLE (decl)
4385 && init)
4387 *expr_p = copy_node (*expr_p);
4388 TREE_OPERAND (*expr_p, 1) = init;
4389 return GS_OK;
4393 default:
4394 break;
4397 while (changed);
4399 return ret;
4403 /* Return true if T looks like a valid GIMPLE statement. */
4405 static bool
4406 is_gimple_stmt (tree t)
4408 const enum tree_code code = TREE_CODE (t);
4410 switch (code)
4412 case NOP_EXPR:
4413 /* The only valid NOP_EXPR is the empty statement. */
4414 return IS_EMPTY_STMT (t);
4416 case BIND_EXPR:
4417 case COND_EXPR:
4418 /* These are only valid if they're void. */
4419 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4421 case SWITCH_EXPR:
4422 case GOTO_EXPR:
4423 case RETURN_EXPR:
4424 case LABEL_EXPR:
4425 case CASE_LABEL_EXPR:
4426 case TRY_CATCH_EXPR:
4427 case TRY_FINALLY_EXPR:
4428 case EH_FILTER_EXPR:
4429 case CATCH_EXPR:
4430 case ASM_EXPR:
4431 case STATEMENT_LIST:
4432 case OACC_PARALLEL:
4433 case OACC_KERNELS:
4434 case OACC_DATA:
4435 case OACC_HOST_DATA:
4436 case OACC_DECLARE:
4437 case OACC_UPDATE:
4438 case OACC_ENTER_DATA:
4439 case OACC_EXIT_DATA:
4440 case OACC_CACHE:
4441 case OMP_PARALLEL:
4442 case OMP_FOR:
4443 case OMP_SIMD:
4444 case CILK_SIMD:
4445 case OMP_DISTRIBUTE:
4446 case OACC_LOOP:
4447 case OMP_SECTIONS:
4448 case OMP_SECTION:
4449 case OMP_SINGLE:
4450 case OMP_MASTER:
4451 case OMP_TASKGROUP:
4452 case OMP_ORDERED:
4453 case OMP_CRITICAL:
4454 case OMP_TASK:
4455 /* These are always void. */
4456 return true;
4458 case CALL_EXPR:
4459 case MODIFY_EXPR:
4460 case PREDICT_EXPR:
4461 /* These are valid regardless of their type. */
4462 return true;
4464 default:
4465 return false;
4470 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4471 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4472 DECL_GIMPLE_REG_P set.
4474 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4475 other, unmodified part of the complex object just before the total store.
4476 As a consequence, if the object is still uninitialized, an undefined value
4477 will be loaded into a register, which may result in a spurious exception
4478 if the register is floating-point and the value happens to be a signaling
4479 NaN for example. Then the fully-fledged complex operations lowering pass
4480 followed by a DCE pass are necessary in order to fix things up. */
4482 static enum gimplify_status
4483 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4484 bool want_value)
4486 enum tree_code code, ocode;
4487 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4489 lhs = TREE_OPERAND (*expr_p, 0);
4490 rhs = TREE_OPERAND (*expr_p, 1);
4491 code = TREE_CODE (lhs);
4492 lhs = TREE_OPERAND (lhs, 0);
4494 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4495 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4496 TREE_NO_WARNING (other) = 1;
4497 other = get_formal_tmp_var (other, pre_p);
4499 realpart = code == REALPART_EXPR ? rhs : other;
4500 imagpart = code == REALPART_EXPR ? other : rhs;
4502 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4503 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4504 else
4505 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4507 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4508 *expr_p = (want_value) ? rhs : NULL_TREE;
4510 return GS_ALL_DONE;
4513 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4515 modify_expr
4516 : varname '=' rhs
4517 | '*' ID '=' rhs
4519 PRE_P points to the list where side effects that must happen before
4520 *EXPR_P should be stored.
4522 POST_P points to the list where side effects that must happen after
4523 *EXPR_P should be stored.
4525 WANT_VALUE is nonzero iff we want to use the value of this expression
4526 in another expression. */
4528 static enum gimplify_status
4529 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4530 bool want_value)
4532 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4533 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4534 enum gimplify_status ret = GS_UNHANDLED;
4535 gimple assign;
4536 location_t loc = EXPR_LOCATION (*expr_p);
4537 gimple_stmt_iterator gsi;
4539 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4540 || TREE_CODE (*expr_p) == INIT_EXPR);
4542 /* Trying to simplify a clobber using normal logic doesn't work,
4543 so handle it here. */
4544 if (TREE_CLOBBER_P (*from_p))
4546 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4547 if (ret == GS_ERROR)
4548 return ret;
4549 gcc_assert (!want_value
4550 && (TREE_CODE (*to_p) == VAR_DECL
4551 || TREE_CODE (*to_p) == MEM_REF));
4552 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4553 *expr_p = NULL;
4554 return GS_ALL_DONE;
4557 /* Insert pointer conversions required by the middle-end that are not
4558 required by the frontend. This fixes middle-end type checking for
4559 for example gcc.dg/redecl-6.c. */
4560 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4562 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4563 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4564 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4567 /* See if any simplifications can be done based on what the RHS is. */
4568 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4569 want_value);
4570 if (ret != GS_UNHANDLED)
4571 return ret;
4573 /* For zero sized types only gimplify the left hand side and right hand
4574 side as statements and throw away the assignment. Do this after
4575 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4576 types properly. */
4577 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4579 gimplify_stmt (from_p, pre_p);
4580 gimplify_stmt (to_p, pre_p);
4581 *expr_p = NULL_TREE;
4582 return GS_ALL_DONE;
4585 /* If the value being copied is of variable width, compute the length
4586 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4587 before gimplifying any of the operands so that we can resolve any
4588 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4589 the size of the expression to be copied, not of the destination, so
4590 that is what we must do here. */
4591 maybe_with_size_expr (from_p);
4593 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4594 if (ret == GS_ERROR)
4595 return ret;
4597 /* As a special case, we have to temporarily allow for assignments
4598 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4599 a toplevel statement, when gimplifying the GENERIC expression
4600 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4601 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4603 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4604 prevent gimplify_expr from trying to create a new temporary for
4605 foo's LHS, we tell it that it should only gimplify until it
4606 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4607 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4608 and all we need to do here is set 'a' to be its LHS. */
4609 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4610 fb_rvalue);
4611 if (ret == GS_ERROR)
4612 return ret;
4614 /* Now see if the above changed *from_p to something we handle specially. */
4615 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4616 want_value);
4617 if (ret != GS_UNHANDLED)
4618 return ret;
4620 /* If we've got a variable sized assignment between two lvalues (i.e. does
4621 not involve a call), then we can make things a bit more straightforward
4622 by converting the assignment to memcpy or memset. */
4623 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4625 tree from = TREE_OPERAND (*from_p, 0);
4626 tree size = TREE_OPERAND (*from_p, 1);
4628 if (TREE_CODE (from) == CONSTRUCTOR)
4629 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4631 if (is_gimple_addressable (from))
4633 *from_p = from;
4634 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4635 pre_p);
4639 /* Transform partial stores to non-addressable complex variables into
4640 total stores. This allows us to use real instead of virtual operands
4641 for these variables, which improves optimization. */
4642 if ((TREE_CODE (*to_p) == REALPART_EXPR
4643 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4644 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4645 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4647 /* Try to alleviate the effects of the gimplification creating artificial
4648 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4649 if (!gimplify_ctxp->into_ssa
4650 && TREE_CODE (*from_p) == VAR_DECL
4651 && DECL_IGNORED_P (*from_p)
4652 && DECL_P (*to_p)
4653 && !DECL_IGNORED_P (*to_p))
4655 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4656 DECL_NAME (*from_p)
4657 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4658 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4659 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4662 if (want_value && TREE_THIS_VOLATILE (*to_p))
4663 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4665 if (TREE_CODE (*from_p) == CALL_EXPR)
4667 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4668 instead of a GIMPLE_ASSIGN. */
4669 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
4671 /* Gimplify internal functions created in the FEs. */
4672 int nargs = call_expr_nargs (*from_p), i;
4673 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
4674 auto_vec<tree> vargs (nargs);
4676 for (i = 0; i < nargs; i++)
4678 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
4679 EXPR_LOCATION (*from_p));
4680 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
4682 assign = gimple_build_call_internal_vec (ifn, vargs);
4683 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4685 else
4687 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4688 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4689 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4690 tree fndecl = get_callee_fndecl (*from_p);
4691 if (fndecl
4692 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4693 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4694 && call_expr_nargs (*from_p) == 3)
4695 assign = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4696 CALL_EXPR_ARG (*from_p, 0),
4697 CALL_EXPR_ARG (*from_p, 1),
4698 CALL_EXPR_ARG (*from_p, 2));
4699 else
4701 assign = gimple_build_call_from_tree (*from_p);
4702 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4705 notice_special_calls (assign);
4706 if (!gimple_call_noreturn_p (assign))
4707 gimple_call_set_lhs (assign, *to_p);
4709 else
4711 assign = gimple_build_assign (*to_p, *from_p);
4712 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4715 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4717 /* We should have got an SSA name from the start. */
4718 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4721 gimplify_seq_add_stmt (pre_p, assign);
4722 gsi = gsi_last (*pre_p);
4723 maybe_fold_stmt (&gsi);
4725 if (want_value)
4727 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4728 return GS_OK;
4730 else
4731 *expr_p = NULL;
4733 return GS_ALL_DONE;
4736 /* Gimplify a comparison between two variable-sized objects. Do this
4737 with a call to BUILT_IN_MEMCMP. */
4739 static enum gimplify_status
4740 gimplify_variable_sized_compare (tree *expr_p)
4742 location_t loc = EXPR_LOCATION (*expr_p);
4743 tree op0 = TREE_OPERAND (*expr_p, 0);
4744 tree op1 = TREE_OPERAND (*expr_p, 1);
4745 tree t, arg, dest, src, expr;
4747 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4748 arg = unshare_expr (arg);
4749 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4750 src = build_fold_addr_expr_loc (loc, op1);
4751 dest = build_fold_addr_expr_loc (loc, op0);
4752 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4753 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4755 expr
4756 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4757 SET_EXPR_LOCATION (expr, loc);
4758 *expr_p = expr;
4760 return GS_OK;
4763 /* Gimplify a comparison between two aggregate objects of integral scalar
4764 mode as a comparison between the bitwise equivalent scalar values. */
4766 static enum gimplify_status
4767 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4769 location_t loc = EXPR_LOCATION (*expr_p);
4770 tree op0 = TREE_OPERAND (*expr_p, 0);
4771 tree op1 = TREE_OPERAND (*expr_p, 1);
4773 tree type = TREE_TYPE (op0);
4774 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4776 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4777 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4779 *expr_p
4780 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4782 return GS_OK;
4785 /* Gimplify an expression sequence. This function gimplifies each
4786 expression and rewrites the original expression with the last
4787 expression of the sequence in GIMPLE form.
4789 PRE_P points to the list where the side effects for all the
4790 expressions in the sequence will be emitted.
4792 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4794 static enum gimplify_status
4795 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4797 tree t = *expr_p;
4801 tree *sub_p = &TREE_OPERAND (t, 0);
4803 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4804 gimplify_compound_expr (sub_p, pre_p, false);
4805 else
4806 gimplify_stmt (sub_p, pre_p);
4808 t = TREE_OPERAND (t, 1);
4810 while (TREE_CODE (t) == COMPOUND_EXPR);
4812 *expr_p = t;
4813 if (want_value)
4814 return GS_OK;
4815 else
4817 gimplify_stmt (expr_p, pre_p);
4818 return GS_ALL_DONE;
4822 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4823 gimplify. After gimplification, EXPR_P will point to a new temporary
4824 that holds the original value of the SAVE_EXPR node.
4826 PRE_P points to the list where side effects that must happen before
4827 *EXPR_P should be stored. */
4829 static enum gimplify_status
4830 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4832 enum gimplify_status ret = GS_ALL_DONE;
4833 tree val;
4835 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4836 val = TREE_OPERAND (*expr_p, 0);
4838 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4839 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4841 /* The operand may be a void-valued expression such as SAVE_EXPRs
4842 generated by the Java frontend for class initialization. It is
4843 being executed only for its side-effects. */
4844 if (TREE_TYPE (val) == void_type_node)
4846 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4847 is_gimple_stmt, fb_none);
4848 val = NULL;
4850 else
4851 val = get_initialized_tmp_var (val, pre_p, post_p);
4853 TREE_OPERAND (*expr_p, 0) = val;
4854 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4857 *expr_p = val;
4859 return ret;
4862 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4864 unary_expr
4865 : ...
4866 | '&' varname
4869 PRE_P points to the list where side effects that must happen before
4870 *EXPR_P should be stored.
4872 POST_P points to the list where side effects that must happen after
4873 *EXPR_P should be stored. */
4875 static enum gimplify_status
4876 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4878 tree expr = *expr_p;
4879 tree op0 = TREE_OPERAND (expr, 0);
4880 enum gimplify_status ret;
4881 location_t loc = EXPR_LOCATION (*expr_p);
4883 switch (TREE_CODE (op0))
4885 case INDIRECT_REF:
4886 do_indirect_ref:
4887 /* Check if we are dealing with an expression of the form '&*ptr'.
4888 While the front end folds away '&*ptr' into 'ptr', these
4889 expressions may be generated internally by the compiler (e.g.,
4890 builtins like __builtin_va_end). */
4891 /* Caution: the silent array decomposition semantics we allow for
4892 ADDR_EXPR means we can't always discard the pair. */
4893 /* Gimplification of the ADDR_EXPR operand may drop
4894 cv-qualification conversions, so make sure we add them if
4895 needed. */
4897 tree op00 = TREE_OPERAND (op0, 0);
4898 tree t_expr = TREE_TYPE (expr);
4899 tree t_op00 = TREE_TYPE (op00);
4901 if (!useless_type_conversion_p (t_expr, t_op00))
4902 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4903 *expr_p = op00;
4904 ret = GS_OK;
4906 break;
4908 case VIEW_CONVERT_EXPR:
4909 /* Take the address of our operand and then convert it to the type of
4910 this ADDR_EXPR.
4912 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4913 all clear. The impact of this transformation is even less clear. */
4915 /* If the operand is a useless conversion, look through it. Doing so
4916 guarantees that the ADDR_EXPR and its operand will remain of the
4917 same type. */
4918 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4919 op0 = TREE_OPERAND (op0, 0);
4921 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4922 build_fold_addr_expr_loc (loc,
4923 TREE_OPERAND (op0, 0)));
4924 ret = GS_OK;
4925 break;
4927 default:
4928 /* We use fb_either here because the C frontend sometimes takes
4929 the address of a call that returns a struct; see
4930 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4931 the implied temporary explicit. */
4933 /* Make the operand addressable. */
4934 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4935 is_gimple_addressable, fb_either);
4936 if (ret == GS_ERROR)
4937 break;
4939 /* Then mark it. Beware that it may not be possible to do so directly
4940 if a temporary has been created by the gimplification. */
4941 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4943 op0 = TREE_OPERAND (expr, 0);
4945 /* For various reasons, the gimplification of the expression
4946 may have made a new INDIRECT_REF. */
4947 if (TREE_CODE (op0) == INDIRECT_REF)
4948 goto do_indirect_ref;
4950 mark_addressable (TREE_OPERAND (expr, 0));
4952 /* The FEs may end up building ADDR_EXPRs early on a decl with
4953 an incomplete type. Re-build ADDR_EXPRs in canonical form
4954 here. */
4955 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4956 *expr_p = build_fold_addr_expr (op0);
4958 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4959 recompute_tree_invariant_for_addr_expr (*expr_p);
4961 /* If we re-built the ADDR_EXPR add a conversion to the original type
4962 if required. */
4963 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4964 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4966 break;
4969 return ret;
4972 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4973 value; output operands should be a gimple lvalue. */
4975 static enum gimplify_status
4976 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4978 tree expr;
4979 int noutputs;
4980 const char **oconstraints;
4981 int i;
4982 tree link;
4983 const char *constraint;
4984 bool allows_mem, allows_reg, is_inout;
4985 enum gimplify_status ret, tret;
4986 gimple stmt;
4987 vec<tree, va_gc> *inputs;
4988 vec<tree, va_gc> *outputs;
4989 vec<tree, va_gc> *clobbers;
4990 vec<tree, va_gc> *labels;
4991 tree link_next;
4993 expr = *expr_p;
4994 noutputs = list_length (ASM_OUTPUTS (expr));
4995 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4997 inputs = NULL;
4998 outputs = NULL;
4999 clobbers = NULL;
5000 labels = NULL;
5002 ret = GS_ALL_DONE;
5003 link_next = NULL_TREE;
5004 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5006 bool ok;
5007 size_t constraint_len;
5009 link_next = TREE_CHAIN (link);
5011 oconstraints[i]
5012 = constraint
5013 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5014 constraint_len = strlen (constraint);
5015 if (constraint_len == 0)
5016 continue;
5018 ok = parse_output_constraint (&constraint, i, 0, 0,
5019 &allows_mem, &allows_reg, &is_inout);
5020 if (!ok)
5022 ret = GS_ERROR;
5023 is_inout = false;
5026 if (!allows_reg && allows_mem)
5027 mark_addressable (TREE_VALUE (link));
5029 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5030 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5031 fb_lvalue | fb_mayfail);
5032 if (tret == GS_ERROR)
5034 error ("invalid lvalue in asm output %d", i);
5035 ret = tret;
5038 vec_safe_push (outputs, link);
5039 TREE_CHAIN (link) = NULL_TREE;
5041 if (is_inout)
5043 /* An input/output operand. To give the optimizers more
5044 flexibility, split it into separate input and output
5045 operands. */
5046 tree input;
5047 char buf[10];
5049 /* Turn the in/out constraint into an output constraint. */
5050 char *p = xstrdup (constraint);
5051 p[0] = '=';
5052 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5054 /* And add a matching input constraint. */
5055 if (allows_reg)
5057 sprintf (buf, "%d", i);
5059 /* If there are multiple alternatives in the constraint,
5060 handle each of them individually. Those that allow register
5061 will be replaced with operand number, the others will stay
5062 unchanged. */
5063 if (strchr (p, ',') != NULL)
5065 size_t len = 0, buflen = strlen (buf);
5066 char *beg, *end, *str, *dst;
5068 for (beg = p + 1;;)
5070 end = strchr (beg, ',');
5071 if (end == NULL)
5072 end = strchr (beg, '\0');
5073 if ((size_t) (end - beg) < buflen)
5074 len += buflen + 1;
5075 else
5076 len += end - beg + 1;
5077 if (*end)
5078 beg = end + 1;
5079 else
5080 break;
5083 str = (char *) alloca (len);
5084 for (beg = p + 1, dst = str;;)
5086 const char *tem;
5087 bool mem_p, reg_p, inout_p;
5089 end = strchr (beg, ',');
5090 if (end)
5091 *end = '\0';
5092 beg[-1] = '=';
5093 tem = beg - 1;
5094 parse_output_constraint (&tem, i, 0, 0,
5095 &mem_p, &reg_p, &inout_p);
5096 if (dst != str)
5097 *dst++ = ',';
5098 if (reg_p)
5100 memcpy (dst, buf, buflen);
5101 dst += buflen;
5103 else
5105 if (end)
5106 len = end - beg;
5107 else
5108 len = strlen (beg);
5109 memcpy (dst, beg, len);
5110 dst += len;
5112 if (end)
5113 beg = end + 1;
5114 else
5115 break;
5117 *dst = '\0';
5118 input = build_string (dst - str, str);
5120 else
5121 input = build_string (strlen (buf), buf);
5123 else
5124 input = build_string (constraint_len - 1, constraint + 1);
5126 free (p);
5128 input = build_tree_list (build_tree_list (NULL_TREE, input),
5129 unshare_expr (TREE_VALUE (link)));
5130 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5134 link_next = NULL_TREE;
5135 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5137 link_next = TREE_CHAIN (link);
5138 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5139 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5140 oconstraints, &allows_mem, &allows_reg);
5142 /* If we can't make copies, we can only accept memory. */
5143 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5145 if (allows_mem)
5146 allows_reg = 0;
5147 else
5149 error ("impossible constraint in %<asm%>");
5150 error ("non-memory input %d must stay in memory", i);
5151 return GS_ERROR;
5155 /* If the operand is a memory input, it should be an lvalue. */
5156 if (!allows_reg && allows_mem)
5158 tree inputv = TREE_VALUE (link);
5159 STRIP_NOPS (inputv);
5160 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5161 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5162 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5163 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5164 TREE_VALUE (link) = error_mark_node;
5165 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5166 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5167 mark_addressable (TREE_VALUE (link));
5168 if (tret == GS_ERROR)
5170 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5171 input_location = EXPR_LOCATION (TREE_VALUE (link));
5172 error ("memory input %d is not directly addressable", i);
5173 ret = tret;
5176 else
5178 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5179 is_gimple_asm_val, fb_rvalue);
5180 if (tret == GS_ERROR)
5181 ret = tret;
5184 TREE_CHAIN (link) = NULL_TREE;
5185 vec_safe_push (inputs, link);
5188 link_next = NULL_TREE;
5189 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5191 link_next = TREE_CHAIN (link);
5192 TREE_CHAIN (link) = NULL_TREE;
5193 vec_safe_push (clobbers, link);
5196 link_next = NULL_TREE;
5197 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5199 link_next = TREE_CHAIN (link);
5200 TREE_CHAIN (link) = NULL_TREE;
5201 vec_safe_push (labels, link);
5204 /* Do not add ASMs with errors to the gimple IL stream. */
5205 if (ret != GS_ERROR)
5207 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5208 inputs, outputs, clobbers, labels);
5210 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5211 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5213 gimplify_seq_add_stmt (pre_p, stmt);
5216 return ret;
5219 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5220 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5221 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5222 return to this function.
5224 FIXME should we complexify the prequeue handling instead? Or use flags
5225 for all the cleanups and let the optimizer tighten them up? The current
5226 code seems pretty fragile; it will break on a cleanup within any
5227 non-conditional nesting. But any such nesting would be broken, anyway;
5228 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5229 and continues out of it. We can do that at the RTL level, though, so
5230 having an optimizer to tighten up try/finally regions would be a Good
5231 Thing. */
5233 static enum gimplify_status
5234 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5236 gimple_stmt_iterator iter;
5237 gimple_seq body_sequence = NULL;
5239 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5241 /* We only care about the number of conditions between the innermost
5242 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5243 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5244 int old_conds = gimplify_ctxp->conditions;
5245 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5246 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5247 gimplify_ctxp->conditions = 0;
5248 gimplify_ctxp->conditional_cleanups = NULL;
5249 gimplify_ctxp->in_cleanup_point_expr = true;
5251 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5253 gimplify_ctxp->conditions = old_conds;
5254 gimplify_ctxp->conditional_cleanups = old_cleanups;
5255 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5257 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5259 gimple wce = gsi_stmt (iter);
5261 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5263 if (gsi_one_before_end_p (iter))
5265 /* Note that gsi_insert_seq_before and gsi_remove do not
5266 scan operands, unlike some other sequence mutators. */
5267 if (!gimple_wce_cleanup_eh_only (wce))
5268 gsi_insert_seq_before_without_update (&iter,
5269 gimple_wce_cleanup (wce),
5270 GSI_SAME_STMT);
5271 gsi_remove (&iter, true);
5272 break;
5274 else
5276 gimple_statement_try *gtry;
5277 gimple_seq seq;
5278 enum gimple_try_flags kind;
5280 if (gimple_wce_cleanup_eh_only (wce))
5281 kind = GIMPLE_TRY_CATCH;
5282 else
5283 kind = GIMPLE_TRY_FINALLY;
5284 seq = gsi_split_seq_after (iter);
5286 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5287 /* Do not use gsi_replace here, as it may scan operands.
5288 We want to do a simple structural modification only. */
5289 gsi_set_stmt (&iter, gtry);
5290 iter = gsi_start (gtry->eval);
5293 else
5294 gsi_next (&iter);
5297 gimplify_seq_add_seq (pre_p, body_sequence);
5298 if (temp)
5300 *expr_p = temp;
5301 return GS_OK;
5303 else
5305 *expr_p = NULL;
5306 return GS_ALL_DONE;
5310 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5311 is the cleanup action required. EH_ONLY is true if the cleanup should
5312 only be executed if an exception is thrown, not on normal exit. */
5314 static void
5315 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5317 gimple wce;
5318 gimple_seq cleanup_stmts = NULL;
5320 /* Errors can result in improperly nested cleanups. Which results in
5321 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5322 if (seen_error ())
5323 return;
5325 if (gimple_conditional_context ())
5327 /* If we're in a conditional context, this is more complex. We only
5328 want to run the cleanup if we actually ran the initialization that
5329 necessitates it, but we want to run it after the end of the
5330 conditional context. So we wrap the try/finally around the
5331 condition and use a flag to determine whether or not to actually
5332 run the destructor. Thus
5334 test ? f(A()) : 0
5336 becomes (approximately)
5338 flag = 0;
5339 try {
5340 if (test) { A::A(temp); flag = 1; val = f(temp); }
5341 else { val = 0; }
5342 } finally {
5343 if (flag) A::~A(temp);
5347 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5348 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5349 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5351 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5352 gimplify_stmt (&cleanup, &cleanup_stmts);
5353 wce = gimple_build_wce (cleanup_stmts);
5355 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5356 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5357 gimplify_seq_add_stmt (pre_p, ftrue);
5359 /* Because of this manipulation, and the EH edges that jump
5360 threading cannot redirect, the temporary (VAR) will appear
5361 to be used uninitialized. Don't warn. */
5362 TREE_NO_WARNING (var) = 1;
5364 else
5366 gimplify_stmt (&cleanup, &cleanup_stmts);
5367 wce = gimple_build_wce (cleanup_stmts);
5368 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5369 gimplify_seq_add_stmt (pre_p, wce);
5373 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5375 static enum gimplify_status
5376 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5378 tree targ = *expr_p;
5379 tree temp = TARGET_EXPR_SLOT (targ);
5380 tree init = TARGET_EXPR_INITIAL (targ);
5381 enum gimplify_status ret;
5383 if (init)
5385 tree cleanup = NULL_TREE;
5387 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5388 to the temps list. Handle also variable length TARGET_EXPRs. */
5389 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5391 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5392 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5393 gimplify_vla_decl (temp, pre_p);
5395 else
5396 gimple_add_tmp_var (temp);
5398 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5399 expression is supposed to initialize the slot. */
5400 if (VOID_TYPE_P (TREE_TYPE (init)))
5401 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5402 else
5404 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5405 init = init_expr;
5406 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5407 init = NULL;
5408 ggc_free (init_expr);
5410 if (ret == GS_ERROR)
5412 /* PR c++/28266 Make sure this is expanded only once. */
5413 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5414 return GS_ERROR;
5416 if (init)
5417 gimplify_and_add (init, pre_p);
5419 /* If needed, push the cleanup for the temp. */
5420 if (TARGET_EXPR_CLEANUP (targ))
5422 if (CLEANUP_EH_ONLY (targ))
5423 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5424 CLEANUP_EH_ONLY (targ), pre_p);
5425 else
5426 cleanup = TARGET_EXPR_CLEANUP (targ);
5429 /* Add a clobber for the temporary going out of scope, like
5430 gimplify_bind_expr. */
5431 if (gimplify_ctxp->in_cleanup_point_expr
5432 && needs_to_live_in_memory (temp)
5433 && flag_stack_reuse == SR_ALL)
5435 tree clobber = build_constructor (TREE_TYPE (temp),
5436 NULL);
5437 TREE_THIS_VOLATILE (clobber) = true;
5438 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5439 if (cleanup)
5440 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5441 clobber);
5442 else
5443 cleanup = clobber;
5446 if (cleanup)
5447 gimple_push_cleanup (temp, cleanup, false, pre_p);
5449 /* Only expand this once. */
5450 TREE_OPERAND (targ, 3) = init;
5451 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5453 else
5454 /* We should have expanded this before. */
5455 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5457 *expr_p = temp;
5458 return GS_OK;
5461 /* Gimplification of expression trees. */
5463 /* Gimplify an expression which appears at statement context. The
5464 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5465 NULL, a new sequence is allocated.
5467 Return true if we actually added a statement to the queue. */
5469 bool
5470 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5472 gimple_seq_node last;
5474 last = gimple_seq_last (*seq_p);
5475 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5476 return last != gimple_seq_last (*seq_p);
5479 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5480 to CTX. If entries already exist, force them to be some flavor of private.
5481 If there is no enclosing parallel, do nothing. */
5483 void
5484 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5486 splay_tree_node n;
5488 if (decl == NULL || !DECL_P (decl))
5489 return;
5493 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5494 if (n != NULL)
5496 if (n->value & GOVD_SHARED)
5497 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5498 else if (n->value & GOVD_MAP)
5499 n->value |= GOVD_MAP_TO_ONLY;
5500 else
5501 return;
5503 else if (ctx->region_type & ORT_TARGET)
5505 if (ctx->region_type & ORT_TARGET_OFFLOAD)
5506 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5508 else if (ctx->region_type != ORT_WORKSHARE
5509 && ctx->region_type != ORT_SIMD)
5510 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5512 ctx = ctx->outer_context;
5514 while (ctx);
5517 /* Similarly for each of the type sizes of TYPE. */
5519 static void
5520 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5522 if (type == NULL || type == error_mark_node)
5523 return;
5524 type = TYPE_MAIN_VARIANT (type);
5526 if (ctx->privatized_types->add (type))
5527 return;
5529 switch (TREE_CODE (type))
5531 case INTEGER_TYPE:
5532 case ENUMERAL_TYPE:
5533 case BOOLEAN_TYPE:
5534 case REAL_TYPE:
5535 case FIXED_POINT_TYPE:
5536 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5537 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5538 break;
5540 case ARRAY_TYPE:
5541 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5542 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5543 break;
5545 case RECORD_TYPE:
5546 case UNION_TYPE:
5547 case QUAL_UNION_TYPE:
5549 tree field;
5550 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5551 if (TREE_CODE (field) == FIELD_DECL)
5553 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5554 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5557 break;
5559 case POINTER_TYPE:
5560 case REFERENCE_TYPE:
5561 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5562 break;
5564 default:
5565 break;
5568 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5569 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5570 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5573 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5575 static void
5576 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5578 splay_tree_node n;
5579 unsigned int nflags;
5580 tree t;
5582 if (error_operand_p (decl))
5583 return;
5585 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5586 there are constructors involved somewhere. */
5587 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5588 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5589 flags |= GOVD_SEEN;
5591 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5592 if (n != NULL && n->value != GOVD_ALIGNED)
5594 /* We shouldn't be re-adding the decl with the same data
5595 sharing class. */
5596 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5597 /* The only combination of data sharing classes we should see is
5598 FIRSTPRIVATE and LASTPRIVATE. */
5599 nflags = n->value | flags;
5600 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5601 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5602 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5603 n->value = nflags;
5604 return;
5607 /* When adding a variable-sized variable, we have to handle all sorts
5608 of additional bits of data: the pointer replacement variable, and
5609 the parameters of the type. */
5610 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5612 /* Add the pointer replacement variable as PRIVATE if the variable
5613 replacement is private, else FIRSTPRIVATE since we'll need the
5614 address of the original variable either for SHARED, or for the
5615 copy into or out of the context. */
5616 if (!(flags & GOVD_LOCAL))
5618 if (flags & GOVD_MAP)
5619 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5620 else if (flags & GOVD_PRIVATE)
5621 nflags = GOVD_PRIVATE;
5622 else
5623 nflags = GOVD_FIRSTPRIVATE;
5624 nflags |= flags & GOVD_SEEN;
5625 t = DECL_VALUE_EXPR (decl);
5626 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5627 t = TREE_OPERAND (t, 0);
5628 gcc_assert (DECL_P (t));
5629 omp_add_variable (ctx, t, nflags);
5632 /* Add all of the variable and type parameters (which should have
5633 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5634 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5635 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5636 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5638 /* The variable-sized variable itself is never SHARED, only some form
5639 of PRIVATE. The sharing would take place via the pointer variable
5640 which we remapped above. */
5641 if (flags & GOVD_SHARED)
5642 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5643 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5645 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5646 alloca statement we generate for the variable, so make sure it
5647 is available. This isn't automatically needed for the SHARED
5648 case, since we won't be allocating local storage then.
5649 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5650 in this case omp_notice_variable will be called later
5651 on when it is gimplified. */
5652 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5653 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5654 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5656 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5657 && lang_hooks.decls.omp_privatize_by_reference (decl))
5659 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5661 /* Similar to the direct variable sized case above, we'll need the
5662 size of references being privatized. */
5663 if ((flags & GOVD_SHARED) == 0)
5665 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5666 if (TREE_CODE (t) != INTEGER_CST)
5667 omp_notice_variable (ctx, t, true);
5671 if (n != NULL)
5672 n->value |= flags;
5673 else
5674 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5677 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5678 This just prints out diagnostics about threadprivate variable uses
5679 in untied tasks. If DECL2 is non-NULL, prevent this warning
5680 on that variable. */
5682 static bool
5683 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5684 tree decl2)
5686 splay_tree_node n;
5687 struct gimplify_omp_ctx *octx;
5689 for (octx = ctx; octx; octx = octx->outer_context)
5690 if ((octx->region_type & ORT_TARGET)
5691 && (octx->region_type & ORT_TARGET_OFFLOAD))
5693 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5694 if (n == NULL)
5696 error ("threadprivate variable %qE used in target region",
5697 DECL_NAME (decl));
5698 error_at (octx->location, "enclosing target region");
5699 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5701 if (decl2)
5702 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5705 if (ctx->region_type != ORT_UNTIED_TASK)
5706 return false;
5707 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5708 if (n == NULL)
5710 error ("threadprivate variable %qE used in untied task",
5711 DECL_NAME (decl));
5712 error_at (ctx->location, "enclosing task");
5713 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5715 if (decl2)
5716 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5717 return false;
5720 /* Record the fact that DECL was used within the OpenMP context CTX.
5721 IN_CODE is true when real code uses DECL, and false when we should
5722 merely emit default(none) errors. Return true if DECL is going to
5723 be remapped and thus DECL shouldn't be gimplified into its
5724 DECL_VALUE_EXPR (if any). */
5726 static bool
5727 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5729 splay_tree_node n;
5730 unsigned flags = in_code ? GOVD_SEEN : 0;
5731 bool ret = false, shared;
5733 if (error_operand_p (decl))
5734 return false;
5736 /* Threadprivate variables are predetermined. */
5737 if (is_global_var (decl))
5739 if (DECL_THREAD_LOCAL_P (decl))
5740 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5742 if (DECL_HAS_VALUE_EXPR_P (decl))
5744 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5746 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5747 return omp_notice_threadprivate_variable (ctx, decl, value);
5751 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5752 if ((ctx->region_type & ORT_TARGET)
5753 && (ctx->region_type & ORT_TARGET_OFFLOAD))
5755 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5756 if (n == NULL)
5758 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5760 error ("%qD referenced in target region does not have "
5761 "a mappable type", decl);
5762 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5764 else
5765 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5767 else
5769 /* If nothing changed, there's nothing left to do. */
5770 if ((n->value & flags) == flags)
5771 return ret;
5772 n->value |= flags;
5774 goto do_outer;
5777 if (n == NULL)
5779 enum omp_clause_default_kind default_kind, kind;
5780 struct gimplify_omp_ctx *octx;
5782 if (ctx->region_type == ORT_WORKSHARE
5783 || ctx->region_type == ORT_SIMD
5784 || ((ctx->region_type & ORT_TARGET)
5785 && !(ctx->region_type & ORT_TARGET_OFFLOAD)))
5786 goto do_outer;
5788 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5789 remapped firstprivate instead of shared. To some extent this is
5790 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5791 default_kind = ctx->default_kind;
5792 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5793 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5794 default_kind = kind;
5796 switch (default_kind)
5798 case OMP_CLAUSE_DEFAULT_NONE:
5799 if ((ctx->region_type & ORT_PARALLEL) != 0)
5801 error ("%qE not specified in enclosing parallel",
5802 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5803 error_at (ctx->location, "enclosing parallel");
5805 else if ((ctx->region_type & ORT_TASK) != 0)
5807 error ("%qE not specified in enclosing task",
5808 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5809 error_at (ctx->location, "enclosing task");
5811 else if (ctx->region_type == ORT_TEAMS)
5813 error ("%qE not specified in enclosing teams construct",
5814 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5815 error_at (ctx->location, "enclosing teams construct");
5817 else
5818 gcc_unreachable ();
5819 /* FALLTHRU */
5820 case OMP_CLAUSE_DEFAULT_SHARED:
5821 flags |= GOVD_SHARED;
5822 break;
5823 case OMP_CLAUSE_DEFAULT_PRIVATE:
5824 flags |= GOVD_PRIVATE;
5825 break;
5826 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5827 flags |= GOVD_FIRSTPRIVATE;
5828 break;
5829 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5830 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5831 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5832 if (ctx->outer_context)
5833 omp_notice_variable (ctx->outer_context, decl, in_code);
5834 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5836 splay_tree_node n2;
5838 if (octx->region_type & ORT_TARGET)
5839 continue;
5840 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5841 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5843 flags |= GOVD_FIRSTPRIVATE;
5844 break;
5846 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5847 break;
5849 if (flags & GOVD_FIRSTPRIVATE)
5850 break;
5851 if (octx == NULL
5852 && (TREE_CODE (decl) == PARM_DECL
5853 || (!is_global_var (decl)
5854 && DECL_CONTEXT (decl) == current_function_decl)))
5856 flags |= GOVD_FIRSTPRIVATE;
5857 break;
5859 flags |= GOVD_SHARED;
5860 break;
5861 default:
5862 gcc_unreachable ();
5865 if ((flags & GOVD_PRIVATE)
5866 && lang_hooks.decls.omp_private_outer_ref (decl))
5867 flags |= GOVD_PRIVATE_OUTER_REF;
5869 omp_add_variable (ctx, decl, flags);
5871 shared = (flags & GOVD_SHARED) != 0;
5872 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5873 goto do_outer;
5876 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5877 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5878 && DECL_SIZE (decl)
5879 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5881 splay_tree_node n2;
5882 tree t = DECL_VALUE_EXPR (decl);
5883 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5884 t = TREE_OPERAND (t, 0);
5885 gcc_assert (DECL_P (t));
5886 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5887 n2->value |= GOVD_SEEN;
5890 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5891 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5893 /* If nothing changed, there's nothing left to do. */
5894 if ((n->value & flags) == flags)
5895 return ret;
5896 flags |= n->value;
5897 n->value = flags;
5899 do_outer:
5900 /* If the variable is private in the current context, then we don't
5901 need to propagate anything to an outer context. */
5902 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5903 return ret;
5904 if (ctx->outer_context
5905 && omp_notice_variable (ctx->outer_context, decl, in_code))
5906 return true;
5907 return ret;
5910 /* Verify that DECL is private within CTX. If there's specific information
5911 to the contrary in the innermost scope, generate an error. */
5913 static bool
5914 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5916 splay_tree_node n;
5918 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5919 if (n != NULL)
5921 if (n->value & GOVD_SHARED)
5923 if (ctx == gimplify_omp_ctxp)
5925 if (simd)
5926 error ("iteration variable %qE is predetermined linear",
5927 DECL_NAME (decl));
5928 else
5929 error ("iteration variable %qE should be private",
5930 DECL_NAME (decl));
5931 n->value = GOVD_PRIVATE;
5932 return true;
5934 else
5935 return false;
5937 else if ((n->value & GOVD_EXPLICIT) != 0
5938 && (ctx == gimplify_omp_ctxp
5939 || (ctx->region_type == ORT_COMBINED_PARALLEL
5940 && gimplify_omp_ctxp->outer_context == ctx)))
5942 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5943 error ("iteration variable %qE should not be firstprivate",
5944 DECL_NAME (decl));
5945 else if ((n->value & GOVD_REDUCTION) != 0)
5946 error ("iteration variable %qE should not be reduction",
5947 DECL_NAME (decl));
5948 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5949 error ("iteration variable %qE should not be lastprivate",
5950 DECL_NAME (decl));
5951 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5952 error ("iteration variable %qE should not be private",
5953 DECL_NAME (decl));
5954 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5955 error ("iteration variable %qE is predetermined linear",
5956 DECL_NAME (decl));
5958 return (ctx == gimplify_omp_ctxp
5959 || (ctx->region_type == ORT_COMBINED_PARALLEL
5960 && gimplify_omp_ctxp->outer_context == ctx));
5963 if (ctx->region_type != ORT_WORKSHARE
5964 && ctx->region_type != ORT_SIMD)
5965 return false;
5966 else if (ctx->outer_context)
5967 return omp_is_private (ctx->outer_context, decl, simd);
5968 return false;
5971 /* Return true if DECL is private within a parallel region
5972 that binds to the current construct's context or in parallel
5973 region's REDUCTION clause. */
5975 static bool
5976 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5978 splay_tree_node n;
5982 ctx = ctx->outer_context;
5983 if (ctx == NULL)
5984 return !(is_global_var (decl)
5985 /* References might be private, but might be shared too,
5986 when checking for copyprivate, assume they might be
5987 private, otherwise assume they might be shared. */
5988 || (!copyprivate
5989 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5991 if (ctx->region_type & ORT_TARGET)
5992 continue;
5994 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5995 if (n != NULL)
5996 return (n->value & GOVD_SHARED) == 0;
5998 while (ctx->region_type == ORT_WORKSHARE
5999 || ctx->region_type == ORT_SIMD);
6000 return false;
6003 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6004 and previous omp contexts. */
6006 static void
6007 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6008 enum omp_region_type region_type)
6010 struct gimplify_omp_ctx *ctx, *outer_ctx;
6011 tree c;
6013 ctx = new_omp_context (region_type);
6014 outer_ctx = ctx->outer_context;
6016 while ((c = *list_p) != NULL)
6018 bool remove = false;
6019 bool notice_outer = true;
6020 const char *check_non_private = NULL;
6021 unsigned int flags;
6022 tree decl;
6024 switch (OMP_CLAUSE_CODE (c))
6026 case OMP_CLAUSE_PRIVATE:
6027 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6028 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6030 flags |= GOVD_PRIVATE_OUTER_REF;
6031 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6033 else
6034 notice_outer = false;
6035 goto do_add;
6036 case OMP_CLAUSE_SHARED:
6037 flags = GOVD_SHARED | GOVD_EXPLICIT;
6038 goto do_add;
6039 case OMP_CLAUSE_FIRSTPRIVATE:
6040 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6041 check_non_private = "firstprivate";
6042 goto do_add;
6043 case OMP_CLAUSE_LASTPRIVATE:
6044 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6045 check_non_private = "lastprivate";
6046 goto do_add;
6047 case OMP_CLAUSE_REDUCTION:
6048 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6049 check_non_private = "reduction";
6050 goto do_add;
6051 case OMP_CLAUSE_LINEAR:
6052 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6053 is_gimple_val, fb_rvalue) == GS_ERROR)
6055 remove = true;
6056 break;
6058 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6059 goto do_add;
6061 case OMP_CLAUSE_MAP:
6062 decl = OMP_CLAUSE_DECL (c);
6063 if (error_operand_p (decl))
6065 remove = true;
6066 break;
6068 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6069 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6070 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6071 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6072 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6074 remove = true;
6075 break;
6077 if (!DECL_P (decl))
6079 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6080 NULL, is_gimple_lvalue, fb_lvalue)
6081 == GS_ERROR)
6083 remove = true;
6084 break;
6086 break;
6088 flags = GOVD_MAP | GOVD_EXPLICIT;
6089 goto do_add;
6091 case OMP_CLAUSE_DEPEND:
6092 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6094 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6095 NULL, is_gimple_val, fb_rvalue);
6096 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6098 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6100 remove = true;
6101 break;
6103 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6104 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6105 is_gimple_val, fb_rvalue) == GS_ERROR)
6107 remove = true;
6108 break;
6110 break;
6112 case OMP_CLAUSE_TO:
6113 case OMP_CLAUSE_FROM:
6114 case OMP_CLAUSE__CACHE_:
6115 decl = OMP_CLAUSE_DECL (c);
6116 if (error_operand_p (decl))
6118 remove = true;
6119 break;
6121 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6122 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6123 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6124 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6125 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6127 remove = true;
6128 break;
6130 if (!DECL_P (decl))
6132 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6133 NULL, is_gimple_lvalue, fb_lvalue)
6134 == GS_ERROR)
6136 remove = true;
6137 break;
6139 break;
6141 goto do_notice;
6143 do_add:
6144 decl = OMP_CLAUSE_DECL (c);
6145 if (error_operand_p (decl))
6147 remove = true;
6148 break;
6150 omp_add_variable (ctx, decl, flags);
6151 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6152 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6154 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6155 GOVD_LOCAL | GOVD_SEEN);
6156 gimplify_omp_ctxp = ctx;
6157 push_gimplify_context ();
6159 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6160 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6162 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6163 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6164 pop_gimplify_context
6165 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6166 push_gimplify_context ();
6167 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6168 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6169 pop_gimplify_context
6170 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6171 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6172 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6174 gimplify_omp_ctxp = outer_ctx;
6176 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6177 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6179 gimplify_omp_ctxp = ctx;
6180 push_gimplify_context ();
6181 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6183 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6184 NULL, NULL);
6185 TREE_SIDE_EFFECTS (bind) = 1;
6186 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6187 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6189 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6190 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6191 pop_gimplify_context
6192 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6193 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6195 gimplify_omp_ctxp = outer_ctx;
6197 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6198 && OMP_CLAUSE_LINEAR_STMT (c))
6200 gimplify_omp_ctxp = ctx;
6201 push_gimplify_context ();
6202 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6204 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6205 NULL, NULL);
6206 TREE_SIDE_EFFECTS (bind) = 1;
6207 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6208 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6210 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6211 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6212 pop_gimplify_context
6213 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6214 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6216 gimplify_omp_ctxp = outer_ctx;
6218 if (notice_outer)
6219 goto do_notice;
6220 break;
6222 case OMP_CLAUSE_COPYIN:
6223 case OMP_CLAUSE_COPYPRIVATE:
6224 decl = OMP_CLAUSE_DECL (c);
6225 if (error_operand_p (decl))
6227 remove = true;
6228 break;
6230 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6231 && !remove
6232 && !omp_check_private (ctx, decl, true))
6234 remove = true;
6235 if (is_global_var (decl))
6237 if (DECL_THREAD_LOCAL_P (decl))
6238 remove = false;
6239 else if (DECL_HAS_VALUE_EXPR_P (decl))
6241 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6243 if (value
6244 && DECL_P (value)
6245 && DECL_THREAD_LOCAL_P (value))
6246 remove = false;
6249 if (remove)
6250 error_at (OMP_CLAUSE_LOCATION (c),
6251 "copyprivate variable %qE is not threadprivate"
6252 " or private in outer context", DECL_NAME (decl));
6254 do_notice:
6255 if (outer_ctx)
6256 omp_notice_variable (outer_ctx, decl, true);
6257 if (check_non_private
6258 && region_type == ORT_WORKSHARE
6259 && omp_check_private (ctx, decl, false))
6261 error ("%s variable %qE is private in outer context",
6262 check_non_private, DECL_NAME (decl));
6263 remove = true;
6265 break;
6267 case OMP_CLAUSE_FINAL:
6268 case OMP_CLAUSE_IF:
6269 OMP_CLAUSE_OPERAND (c, 0)
6270 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6271 /* Fall through. */
6273 case OMP_CLAUSE_SCHEDULE:
6274 case OMP_CLAUSE_NUM_THREADS:
6275 case OMP_CLAUSE_NUM_TEAMS:
6276 case OMP_CLAUSE_THREAD_LIMIT:
6277 case OMP_CLAUSE_DIST_SCHEDULE:
6278 case OMP_CLAUSE_DEVICE:
6279 case OMP_CLAUSE__CILK_FOR_COUNT_:
6280 case OMP_CLAUSE_NUM_GANGS:
6281 case OMP_CLAUSE_NUM_WORKERS:
6282 case OMP_CLAUSE_VECTOR_LENGTH:
6283 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6284 is_gimple_val, fb_rvalue) == GS_ERROR)
6285 remove = true;
6286 break;
6288 case OMP_CLAUSE_DEVICE_RESIDENT:
6289 case OMP_CLAUSE_USE_DEVICE:
6290 case OMP_CLAUSE_GANG:
6291 case OMP_CLAUSE_ASYNC:
6292 case OMP_CLAUSE_WAIT:
6293 case OMP_CLAUSE_INDEPENDENT:
6294 case OMP_CLAUSE_WORKER:
6295 case OMP_CLAUSE_VECTOR:
6296 remove = true;
6297 break;
6299 case OMP_CLAUSE_NOWAIT:
6300 case OMP_CLAUSE_ORDERED:
6301 case OMP_CLAUSE_UNTIED:
6302 case OMP_CLAUSE_COLLAPSE:
6303 case OMP_CLAUSE_MERGEABLE:
6304 case OMP_CLAUSE_PROC_BIND:
6305 case OMP_CLAUSE_SAFELEN:
6306 break;
6308 case OMP_CLAUSE_ALIGNED:
6309 decl = OMP_CLAUSE_DECL (c);
6310 if (error_operand_p (decl))
6312 remove = true;
6313 break;
6315 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6316 is_gimple_val, fb_rvalue) == GS_ERROR)
6318 remove = true;
6319 break;
6321 if (!is_global_var (decl)
6322 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6323 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6324 break;
6326 case OMP_CLAUSE_DEFAULT:
6327 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6328 break;
6330 default:
6331 gcc_unreachable ();
6334 if (remove)
6335 *list_p = OMP_CLAUSE_CHAIN (c);
6336 else
6337 list_p = &OMP_CLAUSE_CHAIN (c);
6340 gimplify_omp_ctxp = ctx;
6343 struct gimplify_adjust_omp_clauses_data
6345 tree *list_p;
6346 gimple_seq *pre_p;
6349 /* For all variables that were not actually used within the context,
6350 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6352 static int
6353 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6355 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6356 gimple_seq *pre_p
6357 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6358 tree decl = (tree) n->key;
6359 unsigned flags = n->value;
6360 enum omp_clause_code code;
6361 tree clause;
6362 bool private_debug;
6364 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6365 return 0;
6366 if ((flags & GOVD_SEEN) == 0)
6367 return 0;
6368 if (flags & GOVD_DEBUG_PRIVATE)
6370 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6371 private_debug = true;
6373 else if (flags & GOVD_MAP)
6374 private_debug = false;
6375 else
6376 private_debug
6377 = lang_hooks.decls.omp_private_debug_clause (decl,
6378 !!(flags & GOVD_SHARED));
6379 if (private_debug)
6380 code = OMP_CLAUSE_PRIVATE;
6381 else if (flags & GOVD_MAP)
6382 code = OMP_CLAUSE_MAP;
6383 else if (flags & GOVD_SHARED)
6385 if (is_global_var (decl))
6387 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6388 while (ctx != NULL)
6390 splay_tree_node on
6391 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6392 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6393 | GOVD_PRIVATE | GOVD_REDUCTION
6394 | GOVD_LINEAR | GOVD_MAP)) != 0)
6395 break;
6396 ctx = ctx->outer_context;
6398 if (ctx == NULL)
6399 return 0;
6401 code = OMP_CLAUSE_SHARED;
6403 else if (flags & GOVD_PRIVATE)
6404 code = OMP_CLAUSE_PRIVATE;
6405 else if (flags & GOVD_FIRSTPRIVATE)
6406 code = OMP_CLAUSE_FIRSTPRIVATE;
6407 else if (flags & GOVD_LASTPRIVATE)
6408 code = OMP_CLAUSE_LASTPRIVATE;
6409 else if (flags & GOVD_ALIGNED)
6410 return 0;
6411 else
6412 gcc_unreachable ();
6414 clause = build_omp_clause (input_location, code);
6415 OMP_CLAUSE_DECL (clause) = decl;
6416 OMP_CLAUSE_CHAIN (clause) = *list_p;
6417 if (private_debug)
6418 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6419 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6420 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6421 else if (code == OMP_CLAUSE_MAP)
6423 enum omp_clause_map_kind map_kind;
6424 map_kind = (flags & GOVD_MAP_TO_ONLY
6425 ? OMP_CLAUSE_MAP_TO
6426 : OMP_CLAUSE_MAP_TOFROM);
6427 OMP_CLAUSE_MAP_KIND (clause) = map_kind;
6429 if (DECL_SIZE (decl)
6430 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6432 tree decl2 = DECL_VALUE_EXPR (decl);
6433 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6434 decl2 = TREE_OPERAND (decl2, 0);
6435 gcc_assert (DECL_P (decl2));
6436 tree mem = build_simple_mem_ref (decl2);
6437 OMP_CLAUSE_DECL (clause) = mem;
6438 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6439 if (gimplify_omp_ctxp->outer_context)
6441 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6442 omp_notice_variable (ctx, decl2, true);
6443 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6445 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6446 OMP_CLAUSE_MAP);
6447 OMP_CLAUSE_DECL (nc) = decl;
6448 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6449 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6450 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6451 OMP_CLAUSE_CHAIN (clause) = nc;
6453 else
6454 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6456 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6458 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6459 OMP_CLAUSE_DECL (nc) = decl;
6460 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6461 OMP_CLAUSE_CHAIN (nc) = *list_p;
6462 OMP_CLAUSE_CHAIN (clause) = nc;
6463 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6464 gimplify_omp_ctxp = ctx->outer_context;
6465 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6466 gimplify_omp_ctxp = ctx;
6468 *list_p = clause;
6469 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6470 gimplify_omp_ctxp = ctx->outer_context;
6471 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6472 gimplify_omp_ctxp = ctx;
6473 return 0;
6476 static void
6477 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6479 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6480 tree c, decl;
6482 while ((c = *list_p) != NULL)
6484 splay_tree_node n;
6485 bool remove = false;
6487 switch (OMP_CLAUSE_CODE (c))
6489 case OMP_CLAUSE_PRIVATE:
6490 case OMP_CLAUSE_SHARED:
6491 case OMP_CLAUSE_FIRSTPRIVATE:
6492 case OMP_CLAUSE_LINEAR:
6493 decl = OMP_CLAUSE_DECL (c);
6494 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6495 remove = !(n->value & GOVD_SEEN);
6496 if (! remove)
6498 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6499 if ((n->value & GOVD_DEBUG_PRIVATE)
6500 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6502 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6503 || ((n->value & GOVD_DATA_SHARE_CLASS)
6504 == GOVD_PRIVATE));
6505 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6506 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6508 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6509 && ctx->outer_context
6510 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6511 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6513 if (ctx->outer_context->combined_loop
6514 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6516 n = splay_tree_lookup (ctx->outer_context->variables,
6517 (splay_tree_key) decl);
6518 if (n == NULL
6519 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6521 int flags = GOVD_FIRSTPRIVATE;
6522 /* #pragma omp distribute does not allow
6523 lastprivate clause. */
6524 if (!ctx->outer_context->distribute)
6525 flags |= GOVD_LASTPRIVATE;
6526 if (n == NULL)
6527 omp_add_variable (ctx->outer_context, decl,
6528 flags | GOVD_SEEN);
6529 else
6530 n->value |= flags | GOVD_SEEN;
6533 else if (!is_global_var (decl))
6534 omp_notice_variable (ctx->outer_context, decl, true);
6537 break;
6539 case OMP_CLAUSE_LASTPRIVATE:
6540 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6541 accurately reflect the presence of a FIRSTPRIVATE clause. */
6542 decl = OMP_CLAUSE_DECL (c);
6543 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6544 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6545 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6546 break;
6548 case OMP_CLAUSE_ALIGNED:
6549 decl = OMP_CLAUSE_DECL (c);
6550 if (!is_global_var (decl))
6552 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6553 remove = n == NULL || !(n->value & GOVD_SEEN);
6554 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6556 struct gimplify_omp_ctx *octx;
6557 if (n != NULL
6558 && (n->value & (GOVD_DATA_SHARE_CLASS
6559 & ~GOVD_FIRSTPRIVATE)))
6560 remove = true;
6561 else
6562 for (octx = ctx->outer_context; octx;
6563 octx = octx->outer_context)
6565 n = splay_tree_lookup (octx->variables,
6566 (splay_tree_key) decl);
6567 if (n == NULL)
6568 continue;
6569 if (n->value & GOVD_LOCAL)
6570 break;
6571 /* We have to avoid assigning a shared variable
6572 to itself when trying to add
6573 __builtin_assume_aligned. */
6574 if (n->value & GOVD_SHARED)
6576 remove = true;
6577 break;
6582 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6584 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6585 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6586 remove = true;
6588 break;
6590 case OMP_CLAUSE_MAP:
6591 decl = OMP_CLAUSE_DECL (c);
6592 if (!DECL_P (decl))
6593 break;
6594 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6595 if ((ctx->region_type & ORT_TARGET)
6596 && (ctx->region_type & ORT_TARGET_OFFLOAD)
6597 && !(n->value & GOVD_SEEN))
6598 remove = true;
6599 else if (DECL_SIZE (decl)
6600 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6601 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6603 /* For OMP_CLAUSE_MAP_FORCE_DEVICEPTR, we'll never enter here,
6604 because for these, TREE_CODE (DECL_SIZE (decl)) will always be
6605 INTEGER_CST. */
6606 gcc_assert (OMP_CLAUSE_MAP_KIND (c)
6607 != OMP_CLAUSE_MAP_FORCE_DEVICEPTR);
6609 tree decl2 = DECL_VALUE_EXPR (decl);
6610 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6611 decl2 = TREE_OPERAND (decl2, 0);
6612 gcc_assert (DECL_P (decl2));
6613 tree mem = build_simple_mem_ref (decl2);
6614 OMP_CLAUSE_DECL (c) = mem;
6615 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6616 if (ctx->outer_context)
6618 omp_notice_variable (ctx->outer_context, decl2, true);
6619 omp_notice_variable (ctx->outer_context,
6620 OMP_CLAUSE_SIZE (c), true);
6622 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6623 OMP_CLAUSE_MAP);
6624 OMP_CLAUSE_DECL (nc) = decl;
6625 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6626 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6627 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6628 OMP_CLAUSE_CHAIN (c) = nc;
6629 c = nc;
6631 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6632 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6633 break;
6635 case OMP_CLAUSE_TO:
6636 case OMP_CLAUSE_FROM:
6637 case OMP_CLAUSE__CACHE_:
6638 decl = OMP_CLAUSE_DECL (c);
6639 if (!DECL_P (decl))
6640 break;
6641 if (DECL_SIZE (decl)
6642 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6644 tree decl2 = DECL_VALUE_EXPR (decl);
6645 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6646 decl2 = TREE_OPERAND (decl2, 0);
6647 gcc_assert (DECL_P (decl2));
6648 tree mem = build_simple_mem_ref (decl2);
6649 OMP_CLAUSE_DECL (c) = mem;
6650 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6651 if (ctx->outer_context)
6653 omp_notice_variable (ctx->outer_context, decl2, true);
6654 omp_notice_variable (ctx->outer_context,
6655 OMP_CLAUSE_SIZE (c), true);
6658 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6659 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6660 break;
6662 case OMP_CLAUSE_REDUCTION:
6663 case OMP_CLAUSE_COPYIN:
6664 case OMP_CLAUSE_COPYPRIVATE:
6665 case OMP_CLAUSE_IF:
6666 case OMP_CLAUSE_NUM_THREADS:
6667 case OMP_CLAUSE_NUM_TEAMS:
6668 case OMP_CLAUSE_THREAD_LIMIT:
6669 case OMP_CLAUSE_DIST_SCHEDULE:
6670 case OMP_CLAUSE_DEVICE:
6671 case OMP_CLAUSE_SCHEDULE:
6672 case OMP_CLAUSE_NOWAIT:
6673 case OMP_CLAUSE_ORDERED:
6674 case OMP_CLAUSE_DEFAULT:
6675 case OMP_CLAUSE_UNTIED:
6676 case OMP_CLAUSE_COLLAPSE:
6677 case OMP_CLAUSE_FINAL:
6678 case OMP_CLAUSE_MERGEABLE:
6679 case OMP_CLAUSE_PROC_BIND:
6680 case OMP_CLAUSE_SAFELEN:
6681 case OMP_CLAUSE_DEPEND:
6682 case OMP_CLAUSE__CILK_FOR_COUNT_:
6683 case OMP_CLAUSE_NUM_GANGS:
6684 case OMP_CLAUSE_NUM_WORKERS:
6685 case OMP_CLAUSE_VECTOR_LENGTH:
6686 break;
6688 case OMP_CLAUSE_DEVICE_RESIDENT:
6689 case OMP_CLAUSE_USE_DEVICE:
6690 case OMP_CLAUSE_GANG:
6691 case OMP_CLAUSE_ASYNC:
6692 case OMP_CLAUSE_WAIT:
6693 case OMP_CLAUSE_INDEPENDENT:
6694 case OMP_CLAUSE_WORKER:
6695 case OMP_CLAUSE_VECTOR:
6696 default:
6697 gcc_unreachable ();
6700 if (remove)
6701 *list_p = OMP_CLAUSE_CHAIN (c);
6702 else
6703 list_p = &OMP_CLAUSE_CHAIN (c);
6706 /* Add in any implicit data sharing. */
6707 struct gimplify_adjust_omp_clauses_data data;
6708 data.list_p = list_p;
6709 data.pre_p = pre_p;
6710 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6712 gimplify_omp_ctxp = ctx->outer_context;
6713 delete_omp_context (ctx);
6716 /* Gimplify OACC_CACHE. */
6718 static void
6719 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
6721 tree expr = *expr_p;
6723 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6724 gimplify_adjust_omp_clauses (pre_p, &OACC_CACHE_CLAUSES (expr));
6726 /* TODO: Do something sensible with this information. */
6728 *expr_p = NULL_TREE;
6731 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6732 gimplification of the body, as well as scanning the body for used
6733 variables. We need to do this scan now, because variable-sized
6734 decls will be decomposed during gimplification. */
6736 static void
6737 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6739 tree expr = *expr_p;
6740 gimple g;
6741 gimple_seq body = NULL;
6743 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6744 OMP_PARALLEL_COMBINED (expr)
6745 ? ORT_COMBINED_PARALLEL
6746 : ORT_PARALLEL);
6748 push_gimplify_context ();
6750 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6751 if (gimple_code (g) == GIMPLE_BIND)
6752 pop_gimplify_context (g);
6753 else
6754 pop_gimplify_context (NULL);
6756 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6758 g = gimple_build_omp_parallel (body,
6759 OMP_PARALLEL_CLAUSES (expr),
6760 NULL_TREE, NULL_TREE);
6761 if (OMP_PARALLEL_COMBINED (expr))
6762 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6763 gimplify_seq_add_stmt (pre_p, g);
6764 *expr_p = NULL_TREE;
6767 /* Gimplify the contents of an OMP_TASK statement. This involves
6768 gimplification of the body, as well as scanning the body for used
6769 variables. We need to do this scan now, because variable-sized
6770 decls will be decomposed during gimplification. */
6772 static void
6773 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6775 tree expr = *expr_p;
6776 gimple g;
6777 gimple_seq body = NULL;
6779 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6780 find_omp_clause (OMP_TASK_CLAUSES (expr),
6781 OMP_CLAUSE_UNTIED)
6782 ? ORT_UNTIED_TASK : ORT_TASK);
6784 push_gimplify_context ();
6786 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6787 if (gimple_code (g) == GIMPLE_BIND)
6788 pop_gimplify_context (g);
6789 else
6790 pop_gimplify_context (NULL);
6792 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6794 g = gimple_build_omp_task (body,
6795 OMP_TASK_CLAUSES (expr),
6796 NULL_TREE, NULL_TREE,
6797 NULL_TREE, NULL_TREE, NULL_TREE);
6798 gimplify_seq_add_stmt (pre_p, g);
6799 *expr_p = NULL_TREE;
6802 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6803 with non-NULL OMP_FOR_INIT. */
6805 static tree
6806 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6808 *walk_subtrees = 0;
6809 switch (TREE_CODE (*tp))
6811 case OMP_FOR:
6812 *walk_subtrees = 1;
6813 /* FALLTHRU */
6814 case OMP_SIMD:
6815 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6816 return *tp;
6817 break;
6818 case BIND_EXPR:
6819 case STATEMENT_LIST:
6820 case OMP_PARALLEL:
6821 *walk_subtrees = 1;
6822 break;
6823 default:
6824 break;
6826 return NULL_TREE;
6829 /* Gimplify the gross structure of an OMP_FOR statement. */
6831 static enum gimplify_status
6832 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6834 tree for_stmt, orig_for_stmt, decl, var, t;
6835 enum gimplify_status ret = GS_ALL_DONE;
6836 enum gimplify_status tret;
6837 gimple gfor;
6838 gimple_seq for_body, for_pre_body;
6839 int i;
6840 bool simd;
6841 enum omp_region_type ort;
6842 bitmap has_decl_expr = NULL;
6844 orig_for_stmt = for_stmt = *expr_p;
6846 switch (TREE_CODE (for_stmt))
6848 case OMP_FOR:
6849 case CILK_FOR:
6850 case OMP_DISTRIBUTE:
6851 simd = false;
6852 ort = ORT_WORKSHARE;
6853 break;
6854 case OMP_SIMD:
6855 case CILK_SIMD:
6856 simd = true;
6857 ort = ORT_SIMD;
6858 break;
6859 case OACC_LOOP:
6860 simd = false;
6861 ort = /* TODO */ ORT_WORKSHARE;
6862 break;
6863 default:
6864 gcc_unreachable ();
6867 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort);
6868 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6869 gimplify_omp_ctxp->distribute = true;
6871 /* Handle OMP_FOR_INIT. */
6872 for_pre_body = NULL;
6873 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6875 has_decl_expr = BITMAP_ALLOC (NULL);
6876 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6877 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6878 == VAR_DECL)
6880 t = OMP_FOR_PRE_BODY (for_stmt);
6881 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6883 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6885 tree_stmt_iterator si;
6886 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6887 tsi_next (&si))
6889 t = tsi_stmt (si);
6890 if (TREE_CODE (t) == DECL_EXPR
6891 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6892 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6896 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6897 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6899 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6901 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
6902 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6903 NULL, NULL);
6904 gcc_assert (for_stmt != NULL_TREE);
6905 gimplify_omp_ctxp->combined_loop = true;
6908 for_body = NULL;
6909 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6910 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6911 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6912 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6913 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6915 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6916 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6917 decl = TREE_OPERAND (t, 0);
6918 gcc_assert (DECL_P (decl));
6919 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6920 || POINTER_TYPE_P (TREE_TYPE (decl)));
6922 /* Make sure the iteration variable is private. */
6923 tree c = NULL_TREE;
6924 tree c2 = NULL_TREE;
6925 if (orig_for_stmt != for_stmt)
6926 /* Do this only on innermost construct for combined ones. */;
6927 else if (simd)
6929 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6930 (splay_tree_key)decl);
6931 omp_is_private (gimplify_omp_ctxp, decl,
6932 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6933 != 1));
6934 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6935 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6936 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6938 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6939 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6940 if (has_decl_expr
6941 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6942 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6943 OMP_CLAUSE_DECL (c) = decl;
6944 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6945 OMP_FOR_CLAUSES (for_stmt) = c;
6946 omp_add_variable (gimplify_omp_ctxp, decl,
6947 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6949 else
6951 bool lastprivate
6952 = (!has_decl_expr
6953 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6954 if (lastprivate
6955 && gimplify_omp_ctxp->outer_context
6956 && gimplify_omp_ctxp->outer_context->region_type
6957 == ORT_WORKSHARE
6958 && gimplify_omp_ctxp->outer_context->combined_loop
6959 && !gimplify_omp_ctxp->outer_context->distribute)
6961 struct gimplify_omp_ctx *outer
6962 = gimplify_omp_ctxp->outer_context;
6963 n = splay_tree_lookup (outer->variables,
6964 (splay_tree_key) decl);
6965 if (n != NULL
6966 && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6967 lastprivate = false;
6968 else if (omp_check_private (outer, decl, false))
6969 error ("lastprivate variable %qE is private in outer "
6970 "context", DECL_NAME (decl));
6971 else
6973 omp_add_variable (outer, decl,
6974 GOVD_LASTPRIVATE | GOVD_SEEN);
6975 if (outer->outer_context)
6976 omp_notice_variable (outer->outer_context, decl, true);
6979 c = build_omp_clause (input_location,
6980 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6981 : OMP_CLAUSE_PRIVATE);
6982 OMP_CLAUSE_DECL (c) = decl;
6983 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6984 OMP_FOR_CLAUSES (for_stmt) = c;
6985 omp_add_variable (gimplify_omp_ctxp, decl,
6986 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6987 | GOVD_EXPLICIT | GOVD_SEEN);
6988 c = NULL_TREE;
6991 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6992 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6993 else
6994 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6996 /* If DECL is not a gimple register, create a temporary variable to act
6997 as an iteration counter. This is valid, since DECL cannot be
6998 modified in the body of the loop. Similarly for any iteration vars
6999 in simd with collapse > 1 where the iterator vars must be
7000 lastprivate. */
7001 if (orig_for_stmt != for_stmt)
7002 var = decl;
7003 else if (!is_gimple_reg (decl)
7004 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
7006 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7007 TREE_OPERAND (t, 0) = var;
7009 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
7011 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7013 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7014 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
7015 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
7016 OMP_CLAUSE_DECL (c2) = var;
7017 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
7018 OMP_FOR_CLAUSES (for_stmt) = c2;
7019 omp_add_variable (gimplify_omp_ctxp, var,
7020 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
7021 if (c == NULL_TREE)
7023 c = c2;
7024 c2 = NULL_TREE;
7027 else
7028 omp_add_variable (gimplify_omp_ctxp, var,
7029 GOVD_PRIVATE | GOVD_SEEN);
7031 else
7032 var = decl;
7034 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7035 is_gimple_val, fb_rvalue);
7036 ret = MIN (ret, tret);
7037 if (ret == GS_ERROR)
7038 return ret;
7040 /* Handle OMP_FOR_COND. */
7041 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7042 gcc_assert (COMPARISON_CLASS_P (t));
7043 gcc_assert (TREE_OPERAND (t, 0) == decl);
7045 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7046 is_gimple_val, fb_rvalue);
7047 ret = MIN (ret, tret);
7049 /* Handle OMP_FOR_INCR. */
7050 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7051 switch (TREE_CODE (t))
7053 case PREINCREMENT_EXPR:
7054 case POSTINCREMENT_EXPR:
7056 tree decl = TREE_OPERAND (t, 0);
7057 /* c_omp_for_incr_canonicalize_ptr() should have been
7058 called to massage things appropriately. */
7059 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7061 if (orig_for_stmt != for_stmt)
7062 break;
7063 t = build_int_cst (TREE_TYPE (decl), 1);
7064 if (c)
7065 OMP_CLAUSE_LINEAR_STEP (c) = t;
7066 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7067 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7068 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7069 break;
7072 case PREDECREMENT_EXPR:
7073 case POSTDECREMENT_EXPR:
7074 /* c_omp_for_incr_canonicalize_ptr() should have been
7075 called to massage things appropriately. */
7076 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7077 if (orig_for_stmt != for_stmt)
7078 break;
7079 t = build_int_cst (TREE_TYPE (decl), -1);
7080 if (c)
7081 OMP_CLAUSE_LINEAR_STEP (c) = t;
7082 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7083 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7084 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7085 break;
7087 case MODIFY_EXPR:
7088 gcc_assert (TREE_OPERAND (t, 0) == decl);
7089 TREE_OPERAND (t, 0) = var;
7091 t = TREE_OPERAND (t, 1);
7092 switch (TREE_CODE (t))
7094 case PLUS_EXPR:
7095 if (TREE_OPERAND (t, 1) == decl)
7097 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
7098 TREE_OPERAND (t, 0) = var;
7099 break;
7102 /* Fallthru. */
7103 case MINUS_EXPR:
7104 case POINTER_PLUS_EXPR:
7105 gcc_assert (TREE_OPERAND (t, 0) == decl);
7106 TREE_OPERAND (t, 0) = var;
7107 break;
7108 default:
7109 gcc_unreachable ();
7112 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7113 is_gimple_val, fb_rvalue);
7114 ret = MIN (ret, tret);
7115 if (c)
7117 tree step = TREE_OPERAND (t, 1);
7118 tree stept = TREE_TYPE (decl);
7119 if (POINTER_TYPE_P (stept))
7120 stept = sizetype;
7121 step = fold_convert (stept, step);
7122 if (TREE_CODE (t) == MINUS_EXPR)
7123 step = fold_build1 (NEGATE_EXPR, stept, step);
7124 OMP_CLAUSE_LINEAR_STEP (c) = step;
7125 if (step != TREE_OPERAND (t, 1))
7127 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7128 &for_pre_body, NULL,
7129 is_gimple_val, fb_rvalue);
7130 ret = MIN (ret, tret);
7133 break;
7135 default:
7136 gcc_unreachable ();
7139 if (c2)
7141 gcc_assert (c);
7142 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7145 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7146 && orig_for_stmt == for_stmt)
7148 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7149 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7150 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7151 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7152 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7153 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7154 && OMP_CLAUSE_DECL (c) == decl)
7156 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7157 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7158 gcc_assert (TREE_OPERAND (t, 0) == var);
7159 t = TREE_OPERAND (t, 1);
7160 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7161 || TREE_CODE (t) == MINUS_EXPR
7162 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7163 gcc_assert (TREE_OPERAND (t, 0) == var);
7164 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7165 TREE_OPERAND (t, 1));
7166 gimple_seq *seq;
7167 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7168 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7169 else
7170 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7171 gimplify_assign (decl, t, seq);
7176 BITMAP_FREE (has_decl_expr);
7178 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7180 if (orig_for_stmt != for_stmt)
7181 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7183 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7184 decl = TREE_OPERAND (t, 0);
7185 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7186 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7187 TREE_OPERAND (t, 0) = var;
7188 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7189 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7190 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7193 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7195 int kind;
7196 switch (TREE_CODE (orig_for_stmt))
7198 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7199 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7200 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7201 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7202 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7203 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
7204 default:
7205 gcc_unreachable ();
7207 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7208 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7209 for_pre_body);
7210 if (orig_for_stmt != for_stmt)
7211 gimple_omp_for_set_combined_p (gfor, true);
7212 if (gimplify_omp_ctxp
7213 && (gimplify_omp_ctxp->combined_loop
7214 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7215 && gimplify_omp_ctxp->outer_context
7216 && gimplify_omp_ctxp->outer_context->combined_loop)))
7218 gimple_omp_for_set_combined_into_p (gfor, true);
7219 if (gimplify_omp_ctxp->combined_loop)
7220 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7221 else
7222 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7225 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7227 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7228 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7229 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7230 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7231 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7232 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7233 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7234 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7237 gimplify_seq_add_stmt (pre_p, gfor);
7238 if (ret != GS_ALL_DONE)
7239 return GS_ERROR;
7240 *expr_p = NULL_TREE;
7241 return GS_ALL_DONE;
7244 /* Gimplify the gross structure of several OpenACC or OpenMP constructs. */
7246 static void
7247 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7249 tree expr = *expr_p;
7250 gimple stmt;
7251 gimple_seq body = NULL;
7252 enum omp_region_type ort;
7254 switch (TREE_CODE (expr))
7256 case OMP_SECTIONS:
7257 case OMP_SINGLE:
7258 ort = ORT_WORKSHARE;
7259 break;
7260 case OACC_KERNELS:
7261 case OACC_PARALLEL:
7262 case OMP_TARGET:
7263 ort = (enum omp_region_type) (ORT_TARGET | ORT_TARGET_OFFLOAD);
7264 break;
7265 case OACC_DATA:
7266 case OMP_TARGET_DATA:
7267 ort = ORT_TARGET;
7268 break;
7269 case OMP_TEAMS:
7270 ort = ORT_TEAMS;
7271 break;
7272 default:
7273 gcc_unreachable ();
7275 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7276 if (ort & ORT_TARGET)
7278 push_gimplify_context ();
7279 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7280 if (gimple_code (g) == GIMPLE_BIND)
7281 pop_gimplify_context (g);
7282 else
7283 pop_gimplify_context (NULL);
7284 if (!(ort & ORT_TARGET_OFFLOAD))
7286 enum built_in_function end_ix;
7287 switch (TREE_CODE (expr))
7289 case OACC_DATA:
7290 end_ix = BUILT_IN_GOACC_DATA_END;
7291 break;
7292 case OMP_TARGET_DATA:
7293 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
7294 break;
7295 default:
7296 gcc_unreachable ();
7298 tree fn = builtin_decl_explicit (end_ix);
7299 g = gimple_build_call (fn, 0);
7300 gimple_seq cleanup = NULL;
7301 gimple_seq_add_stmt (&cleanup, g);
7302 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7303 body = NULL;
7304 gimple_seq_add_stmt (&body, g);
7307 else
7308 gimplify_and_add (OMP_BODY (expr), &body);
7309 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7311 switch (TREE_CODE (expr))
7313 case OACC_DATA:
7314 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
7315 OACC_DATA_CLAUSES (expr));
7316 break;
7317 case OACC_KERNELS:
7318 stmt = gimple_build_oacc_kernels (body, OACC_KERNELS_CLAUSES (expr));
7319 break;
7320 case OACC_PARALLEL:
7321 stmt = gimple_build_oacc_parallel (body, OACC_PARALLEL_CLAUSES (expr));
7322 break;
7323 case OMP_SECTIONS:
7324 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7325 break;
7326 case OMP_SINGLE:
7327 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7328 break;
7329 case OMP_TARGET:
7330 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7331 OMP_CLAUSES (expr));
7332 break;
7333 case OMP_TARGET_DATA:
7334 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7335 OMP_CLAUSES (expr));
7336 break;
7337 case OMP_TEAMS:
7338 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7339 break;
7340 default:
7341 gcc_unreachable ();
7344 gimplify_seq_add_stmt (pre_p, stmt);
7345 *expr_p = NULL_TREE;
7348 /* Gimplify the gross structure of OpenACC update and OpenMP target update
7349 constructs. */
7351 static void
7352 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7354 tree expr = *expr_p, clauses;
7355 int kind;
7356 gimple stmt;
7358 switch (TREE_CODE (expr))
7360 case OACC_ENTER_DATA:
7361 clauses = OACC_ENTER_DATA_CLAUSES (expr);
7362 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7363 break;
7364 case OACC_EXIT_DATA:
7365 clauses = OACC_EXIT_DATA_CLAUSES (expr);
7366 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7367 break;
7368 case OACC_UPDATE:
7369 clauses = OACC_UPDATE_CLAUSES (expr);
7370 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
7371 break;
7372 case OMP_TARGET_UPDATE:
7373 clauses = OMP_TARGET_UPDATE_CLAUSES (expr);
7374 kind = GF_OMP_TARGET_KIND_UPDATE;
7375 break;
7376 default:
7377 gcc_unreachable ();
7379 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_WORKSHARE);
7380 gimplify_adjust_omp_clauses (pre_p, &clauses);
7381 stmt = gimple_build_omp_target (NULL, kind, clauses);
7383 gimplify_seq_add_stmt (pre_p, stmt);
7384 *expr_p = NULL_TREE;
7387 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7388 stabilized the lhs of the atomic operation as *ADDR. Return true if
7389 EXPR is this stabilized form. */
7391 static bool
7392 goa_lhs_expr_p (tree expr, tree addr)
7394 /* Also include casts to other type variants. The C front end is fond
7395 of adding these for e.g. volatile variables. This is like
7396 STRIP_TYPE_NOPS but includes the main variant lookup. */
7397 STRIP_USELESS_TYPE_CONVERSION (expr);
7399 if (TREE_CODE (expr) == INDIRECT_REF)
7401 expr = TREE_OPERAND (expr, 0);
7402 while (expr != addr
7403 && (CONVERT_EXPR_P (expr)
7404 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7405 && TREE_CODE (expr) == TREE_CODE (addr)
7406 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7408 expr = TREE_OPERAND (expr, 0);
7409 addr = TREE_OPERAND (addr, 0);
7411 if (expr == addr)
7412 return true;
7413 return (TREE_CODE (addr) == ADDR_EXPR
7414 && TREE_CODE (expr) == ADDR_EXPR
7415 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7417 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7418 return true;
7419 return false;
7422 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7423 expression does not involve the lhs, evaluate it into a temporary.
7424 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7425 or -1 if an error was encountered. */
7427 static int
7428 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7429 tree lhs_var)
7431 tree expr = *expr_p;
7432 int saw_lhs;
7434 if (goa_lhs_expr_p (expr, lhs_addr))
7436 *expr_p = lhs_var;
7437 return 1;
7439 if (is_gimple_val (expr))
7440 return 0;
7442 saw_lhs = 0;
7443 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7445 case tcc_binary:
7446 case tcc_comparison:
7447 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7448 lhs_var);
7449 case tcc_unary:
7450 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7451 lhs_var);
7452 break;
7453 case tcc_expression:
7454 switch (TREE_CODE (expr))
7456 case TRUTH_ANDIF_EXPR:
7457 case TRUTH_ORIF_EXPR:
7458 case TRUTH_AND_EXPR:
7459 case TRUTH_OR_EXPR:
7460 case TRUTH_XOR_EXPR:
7461 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7462 lhs_addr, lhs_var);
7463 case TRUTH_NOT_EXPR:
7464 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7465 lhs_addr, lhs_var);
7466 break;
7467 case COMPOUND_EXPR:
7468 /* Break out any preevaluations from cp_build_modify_expr. */
7469 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7470 expr = TREE_OPERAND (expr, 1))
7471 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7472 *expr_p = expr;
7473 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7474 default:
7475 break;
7477 break;
7478 default:
7479 break;
7482 if (saw_lhs == 0)
7484 enum gimplify_status gs;
7485 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7486 if (gs != GS_ALL_DONE)
7487 saw_lhs = -1;
7490 return saw_lhs;
7493 /* Gimplify an OMP_ATOMIC statement. */
7495 static enum gimplify_status
7496 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7498 tree addr = TREE_OPERAND (*expr_p, 0);
7499 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7500 ? NULL : TREE_OPERAND (*expr_p, 1);
7501 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7502 tree tmp_load;
7503 gimple loadstmt, storestmt;
7505 tmp_load = create_tmp_reg (type, NULL);
7506 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7507 return GS_ERROR;
7509 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7510 != GS_ALL_DONE)
7511 return GS_ERROR;
7513 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7514 gimplify_seq_add_stmt (pre_p, loadstmt);
7515 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7516 != GS_ALL_DONE)
7517 return GS_ERROR;
7519 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7520 rhs = tmp_load;
7521 storestmt = gimple_build_omp_atomic_store (rhs);
7522 gimplify_seq_add_stmt (pre_p, storestmt);
7523 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7525 gimple_omp_atomic_set_seq_cst (loadstmt);
7526 gimple_omp_atomic_set_seq_cst (storestmt);
7528 switch (TREE_CODE (*expr_p))
7530 case OMP_ATOMIC_READ:
7531 case OMP_ATOMIC_CAPTURE_OLD:
7532 *expr_p = tmp_load;
7533 gimple_omp_atomic_set_need_value (loadstmt);
7534 break;
7535 case OMP_ATOMIC_CAPTURE_NEW:
7536 *expr_p = rhs;
7537 gimple_omp_atomic_set_need_value (storestmt);
7538 break;
7539 default:
7540 *expr_p = NULL;
7541 break;
7544 return GS_ALL_DONE;
7547 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7548 body, and adding some EH bits. */
7550 static enum gimplify_status
7551 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7553 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7554 gimple g;
7555 gimple_seq body = NULL;
7556 int subcode = 0;
7558 /* Wrap the transaction body in a BIND_EXPR so we have a context
7559 where to put decls for OpenMP. */
7560 if (TREE_CODE (tbody) != BIND_EXPR)
7562 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7563 TREE_SIDE_EFFECTS (bind) = 1;
7564 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7565 TRANSACTION_EXPR_BODY (expr) = bind;
7568 push_gimplify_context ();
7569 temp = voidify_wrapper_expr (*expr_p, NULL);
7571 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7572 pop_gimplify_context (g);
7574 g = gimple_build_transaction (body, NULL);
7575 if (TRANSACTION_EXPR_OUTER (expr))
7576 subcode = GTMA_IS_OUTER;
7577 else if (TRANSACTION_EXPR_RELAXED (expr))
7578 subcode = GTMA_IS_RELAXED;
7579 gimple_transaction_set_subcode (g, subcode);
7581 gimplify_seq_add_stmt (pre_p, g);
7583 if (temp)
7585 *expr_p = temp;
7586 return GS_OK;
7589 *expr_p = NULL_TREE;
7590 return GS_ALL_DONE;
7593 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7594 expression produces a value to be used as an operand inside a GIMPLE
7595 statement, the value will be stored back in *EXPR_P. This value will
7596 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7597 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7598 emitted in PRE_P and POST_P.
7600 Additionally, this process may overwrite parts of the input
7601 expression during gimplification. Ideally, it should be
7602 possible to do non-destructive gimplification.
7604 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7605 the expression needs to evaluate to a value to be used as
7606 an operand in a GIMPLE statement, this value will be stored in
7607 *EXPR_P on exit. This happens when the caller specifies one
7608 of fb_lvalue or fb_rvalue fallback flags.
7610 PRE_P will contain the sequence of GIMPLE statements corresponding
7611 to the evaluation of EXPR and all the side-effects that must
7612 be executed before the main expression. On exit, the last
7613 statement of PRE_P is the core statement being gimplified. For
7614 instance, when gimplifying 'if (++a)' the last statement in
7615 PRE_P will be 'if (t.1)' where t.1 is the result of
7616 pre-incrementing 'a'.
7618 POST_P will contain the sequence of GIMPLE statements corresponding
7619 to the evaluation of all the side-effects that must be executed
7620 after the main expression. If this is NULL, the post
7621 side-effects are stored at the end of PRE_P.
7623 The reason why the output is split in two is to handle post
7624 side-effects explicitly. In some cases, an expression may have
7625 inner and outer post side-effects which need to be emitted in
7626 an order different from the one given by the recursive
7627 traversal. For instance, for the expression (*p--)++ the post
7628 side-effects of '--' must actually occur *after* the post
7629 side-effects of '++'. However, gimplification will first visit
7630 the inner expression, so if a separate POST sequence was not
7631 used, the resulting sequence would be:
7633 1 t.1 = *p
7634 2 p = p - 1
7635 3 t.2 = t.1 + 1
7636 4 *p = t.2
7638 However, the post-decrement operation in line #2 must not be
7639 evaluated until after the store to *p at line #4, so the
7640 correct sequence should be:
7642 1 t.1 = *p
7643 2 t.2 = t.1 + 1
7644 3 *p = t.2
7645 4 p = p - 1
7647 So, by specifying a separate post queue, it is possible
7648 to emit the post side-effects in the correct order.
7649 If POST_P is NULL, an internal queue will be used. Before
7650 returning to the caller, the sequence POST_P is appended to
7651 the main output sequence PRE_P.
7653 GIMPLE_TEST_F points to a function that takes a tree T and
7654 returns nonzero if T is in the GIMPLE form requested by the
7655 caller. The GIMPLE predicates are in gimple.c.
7657 FALLBACK tells the function what sort of a temporary we want if
7658 gimplification cannot produce an expression that complies with
7659 GIMPLE_TEST_F.
7661 fb_none means that no temporary should be generated
7662 fb_rvalue means that an rvalue is OK to generate
7663 fb_lvalue means that an lvalue is OK to generate
7664 fb_either means that either is OK, but an lvalue is preferable.
7665 fb_mayfail means that gimplification may fail (in which case
7666 GS_ERROR will be returned)
7668 The return value is either GS_ERROR or GS_ALL_DONE, since this
7669 function iterates until EXPR is completely gimplified or an error
7670 occurs. */
7672 enum gimplify_status
7673 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7674 bool (*gimple_test_f) (tree), fallback_t fallback)
7676 tree tmp;
7677 gimple_seq internal_pre = NULL;
7678 gimple_seq internal_post = NULL;
7679 tree save_expr;
7680 bool is_statement;
7681 location_t saved_location;
7682 enum gimplify_status ret;
7683 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7685 save_expr = *expr_p;
7686 if (save_expr == NULL_TREE)
7687 return GS_ALL_DONE;
7689 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7690 is_statement = gimple_test_f == is_gimple_stmt;
7691 if (is_statement)
7692 gcc_assert (pre_p);
7694 /* Consistency checks. */
7695 if (gimple_test_f == is_gimple_reg)
7696 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7697 else if (gimple_test_f == is_gimple_val
7698 || gimple_test_f == is_gimple_call_addr
7699 || gimple_test_f == is_gimple_condexpr
7700 || gimple_test_f == is_gimple_mem_rhs
7701 || gimple_test_f == is_gimple_mem_rhs_or_call
7702 || gimple_test_f == is_gimple_reg_rhs
7703 || gimple_test_f == is_gimple_reg_rhs_or_call
7704 || gimple_test_f == is_gimple_asm_val
7705 || gimple_test_f == is_gimple_mem_ref_addr)
7706 gcc_assert (fallback & fb_rvalue);
7707 else if (gimple_test_f == is_gimple_min_lval
7708 || gimple_test_f == is_gimple_lvalue)
7709 gcc_assert (fallback & fb_lvalue);
7710 else if (gimple_test_f == is_gimple_addressable)
7711 gcc_assert (fallback & fb_either);
7712 else if (gimple_test_f == is_gimple_stmt)
7713 gcc_assert (fallback == fb_none);
7714 else
7716 /* We should have recognized the GIMPLE_TEST_F predicate to
7717 know what kind of fallback to use in case a temporary is
7718 needed to hold the value or address of *EXPR_P. */
7719 gcc_unreachable ();
7722 /* We used to check the predicate here and return immediately if it
7723 succeeds. This is wrong; the design is for gimplification to be
7724 idempotent, and for the predicates to only test for valid forms, not
7725 whether they are fully simplified. */
7726 if (pre_p == NULL)
7727 pre_p = &internal_pre;
7729 if (post_p == NULL)
7730 post_p = &internal_post;
7732 /* Remember the last statements added to PRE_P and POST_P. Every
7733 new statement added by the gimplification helpers needs to be
7734 annotated with location information. To centralize the
7735 responsibility, we remember the last statement that had been
7736 added to both queues before gimplifying *EXPR_P. If
7737 gimplification produces new statements in PRE_P and POST_P, those
7738 statements will be annotated with the same location information
7739 as *EXPR_P. */
7740 pre_last_gsi = gsi_last (*pre_p);
7741 post_last_gsi = gsi_last (*post_p);
7743 saved_location = input_location;
7744 if (save_expr != error_mark_node
7745 && EXPR_HAS_LOCATION (*expr_p))
7746 input_location = EXPR_LOCATION (*expr_p);
7748 /* Loop over the specific gimplifiers until the toplevel node
7749 remains the same. */
7752 /* Strip away as many useless type conversions as possible
7753 at the toplevel. */
7754 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7756 /* Remember the expr. */
7757 save_expr = *expr_p;
7759 /* Die, die, die, my darling. */
7760 if (save_expr == error_mark_node
7761 || (TREE_TYPE (save_expr)
7762 && TREE_TYPE (save_expr) == error_mark_node))
7764 ret = GS_ERROR;
7765 break;
7768 /* Do any language-specific gimplification. */
7769 ret = ((enum gimplify_status)
7770 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7771 if (ret == GS_OK)
7773 if (*expr_p == NULL_TREE)
7774 break;
7775 if (*expr_p != save_expr)
7776 continue;
7778 else if (ret != GS_UNHANDLED)
7779 break;
7781 /* Make sure that all the cases set 'ret' appropriately. */
7782 ret = GS_UNHANDLED;
7783 switch (TREE_CODE (*expr_p))
7785 /* First deal with the special cases. */
7787 case POSTINCREMENT_EXPR:
7788 case POSTDECREMENT_EXPR:
7789 case PREINCREMENT_EXPR:
7790 case PREDECREMENT_EXPR:
7791 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7792 fallback != fb_none,
7793 TREE_TYPE (*expr_p));
7794 break;
7796 case VIEW_CONVERT_EXPR:
7797 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7798 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7800 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7801 post_p, is_gimple_val, fb_rvalue);
7802 recalculate_side_effects (*expr_p);
7803 break;
7805 /* Fallthru. */
7807 case ARRAY_REF:
7808 case ARRAY_RANGE_REF:
7809 case REALPART_EXPR:
7810 case IMAGPART_EXPR:
7811 case COMPONENT_REF:
7812 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7813 fallback ? fallback : fb_rvalue);
7814 break;
7816 case COND_EXPR:
7817 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7819 /* C99 code may assign to an array in a structure value of a
7820 conditional expression, and this has undefined behavior
7821 only on execution, so create a temporary if an lvalue is
7822 required. */
7823 if (fallback == fb_lvalue)
7825 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7826 mark_addressable (*expr_p);
7827 ret = GS_OK;
7829 break;
7831 case CALL_EXPR:
7832 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7834 /* C99 code may assign to an array in a structure returned
7835 from a function, and this has undefined behavior only on
7836 execution, so create a temporary if an lvalue is
7837 required. */
7838 if (fallback == fb_lvalue)
7840 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7841 mark_addressable (*expr_p);
7842 ret = GS_OK;
7844 break;
7846 case TREE_LIST:
7847 gcc_unreachable ();
7849 case COMPOUND_EXPR:
7850 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7851 break;
7853 case COMPOUND_LITERAL_EXPR:
7854 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7855 gimple_test_f, fallback);
7856 break;
7858 case MODIFY_EXPR:
7859 case INIT_EXPR:
7860 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7861 fallback != fb_none);
7862 break;
7864 case TRUTH_ANDIF_EXPR:
7865 case TRUTH_ORIF_EXPR:
7867 /* Preserve the original type of the expression and the
7868 source location of the outer expression. */
7869 tree org_type = TREE_TYPE (*expr_p);
7870 *expr_p = gimple_boolify (*expr_p);
7871 *expr_p = build3_loc (input_location, COND_EXPR,
7872 org_type, *expr_p,
7873 fold_convert_loc
7874 (input_location,
7875 org_type, boolean_true_node),
7876 fold_convert_loc
7877 (input_location,
7878 org_type, boolean_false_node));
7879 ret = GS_OK;
7880 break;
7883 case TRUTH_NOT_EXPR:
7885 tree type = TREE_TYPE (*expr_p);
7886 /* The parsers are careful to generate TRUTH_NOT_EXPR
7887 only with operands that are always zero or one.
7888 We do not fold here but handle the only interesting case
7889 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7890 *expr_p = gimple_boolify (*expr_p);
7891 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7892 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7893 TREE_TYPE (*expr_p),
7894 TREE_OPERAND (*expr_p, 0));
7895 else
7896 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7897 TREE_TYPE (*expr_p),
7898 TREE_OPERAND (*expr_p, 0),
7899 build_int_cst (TREE_TYPE (*expr_p), 1));
7900 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7901 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7902 ret = GS_OK;
7903 break;
7906 case ADDR_EXPR:
7907 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7908 break;
7910 case ANNOTATE_EXPR:
7912 tree cond = TREE_OPERAND (*expr_p, 0);
7913 tree kind = TREE_OPERAND (*expr_p, 1);
7914 tree type = TREE_TYPE (cond);
7915 if (!INTEGRAL_TYPE_P (type))
7917 *expr_p = cond;
7918 ret = GS_OK;
7919 break;
7921 tree tmp = create_tmp_var (type, NULL);
7922 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7923 gimple call
7924 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7925 gimple_call_set_lhs (call, tmp);
7926 gimplify_seq_add_stmt (pre_p, call);
7927 *expr_p = tmp;
7928 ret = GS_ALL_DONE;
7929 break;
7932 case VA_ARG_EXPR:
7933 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7934 break;
7936 CASE_CONVERT:
7937 if (IS_EMPTY_STMT (*expr_p))
7939 ret = GS_ALL_DONE;
7940 break;
7943 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7944 || fallback == fb_none)
7946 /* Just strip a conversion to void (or in void context) and
7947 try again. */
7948 *expr_p = TREE_OPERAND (*expr_p, 0);
7949 ret = GS_OK;
7950 break;
7953 ret = gimplify_conversion (expr_p);
7954 if (ret == GS_ERROR)
7955 break;
7956 if (*expr_p != save_expr)
7957 break;
7958 /* FALLTHRU */
7960 case FIX_TRUNC_EXPR:
7961 /* unary_expr: ... | '(' cast ')' val | ... */
7962 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7963 is_gimple_val, fb_rvalue);
7964 recalculate_side_effects (*expr_p);
7965 break;
7967 case INDIRECT_REF:
7969 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7970 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7971 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7973 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7974 if (*expr_p != save_expr)
7976 ret = GS_OK;
7977 break;
7980 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7981 is_gimple_reg, fb_rvalue);
7982 if (ret == GS_ERROR)
7983 break;
7985 recalculate_side_effects (*expr_p);
7986 *expr_p = fold_build2_loc (input_location, MEM_REF,
7987 TREE_TYPE (*expr_p),
7988 TREE_OPERAND (*expr_p, 0),
7989 build_int_cst (saved_ptr_type, 0));
7990 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7991 TREE_THIS_NOTRAP (*expr_p) = notrap;
7992 ret = GS_OK;
7993 break;
7996 /* We arrive here through the various re-gimplifcation paths. */
7997 case MEM_REF:
7998 /* First try re-folding the whole thing. */
7999 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
8000 TREE_OPERAND (*expr_p, 0),
8001 TREE_OPERAND (*expr_p, 1));
8002 if (tmp)
8004 *expr_p = tmp;
8005 recalculate_side_effects (*expr_p);
8006 ret = GS_OK;
8007 break;
8009 /* Avoid re-gimplifying the address operand if it is already
8010 in suitable form. Re-gimplifying would mark the address
8011 operand addressable. Always gimplify when not in SSA form
8012 as we still may have to gimplify decls with value-exprs. */
8013 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
8014 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
8016 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8017 is_gimple_mem_ref_addr, fb_rvalue);
8018 if (ret == GS_ERROR)
8019 break;
8021 recalculate_side_effects (*expr_p);
8022 ret = GS_ALL_DONE;
8023 break;
8025 /* Constants need not be gimplified. */
8026 case INTEGER_CST:
8027 case REAL_CST:
8028 case FIXED_CST:
8029 case STRING_CST:
8030 case COMPLEX_CST:
8031 case VECTOR_CST:
8032 /* Drop the overflow flag on constants, we do not want
8033 that in the GIMPLE IL. */
8034 if (TREE_OVERFLOW_P (*expr_p))
8035 *expr_p = drop_tree_overflow (*expr_p);
8036 ret = GS_ALL_DONE;
8037 break;
8039 case CONST_DECL:
8040 /* If we require an lvalue, such as for ADDR_EXPR, retain the
8041 CONST_DECL node. Otherwise the decl is replaceable by its
8042 value. */
8043 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
8044 if (fallback & fb_lvalue)
8045 ret = GS_ALL_DONE;
8046 else
8048 *expr_p = DECL_INITIAL (*expr_p);
8049 ret = GS_OK;
8051 break;
8053 case DECL_EXPR:
8054 ret = gimplify_decl_expr (expr_p, pre_p);
8055 break;
8057 case BIND_EXPR:
8058 ret = gimplify_bind_expr (expr_p, pre_p);
8059 break;
8061 case LOOP_EXPR:
8062 ret = gimplify_loop_expr (expr_p, pre_p);
8063 break;
8065 case SWITCH_EXPR:
8066 ret = gimplify_switch_expr (expr_p, pre_p);
8067 break;
8069 case EXIT_EXPR:
8070 ret = gimplify_exit_expr (expr_p);
8071 break;
8073 case GOTO_EXPR:
8074 /* If the target is not LABEL, then it is a computed jump
8075 and the target needs to be gimplified. */
8076 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
8078 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
8079 NULL, is_gimple_val, fb_rvalue);
8080 if (ret == GS_ERROR)
8081 break;
8083 gimplify_seq_add_stmt (pre_p,
8084 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
8085 ret = GS_ALL_DONE;
8086 break;
8088 case PREDICT_EXPR:
8089 gimplify_seq_add_stmt (pre_p,
8090 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
8091 PREDICT_EXPR_OUTCOME (*expr_p)));
8092 ret = GS_ALL_DONE;
8093 break;
8095 case LABEL_EXPR:
8096 ret = GS_ALL_DONE;
8097 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
8098 == current_function_decl);
8099 gimplify_seq_add_stmt (pre_p,
8100 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
8101 break;
8103 case CASE_LABEL_EXPR:
8104 ret = gimplify_case_label_expr (expr_p, pre_p);
8105 break;
8107 case RETURN_EXPR:
8108 ret = gimplify_return_expr (*expr_p, pre_p);
8109 break;
8111 case CONSTRUCTOR:
8112 /* Don't reduce this in place; let gimplify_init_constructor work its
8113 magic. Buf if we're just elaborating this for side effects, just
8114 gimplify any element that has side-effects. */
8115 if (fallback == fb_none)
8117 unsigned HOST_WIDE_INT ix;
8118 tree val;
8119 tree temp = NULL_TREE;
8120 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
8121 if (TREE_SIDE_EFFECTS (val))
8122 append_to_statement_list (val, &temp);
8124 *expr_p = temp;
8125 ret = temp ? GS_OK : GS_ALL_DONE;
8127 /* C99 code may assign to an array in a constructed
8128 structure or union, and this has undefined behavior only
8129 on execution, so create a temporary if an lvalue is
8130 required. */
8131 else if (fallback == fb_lvalue)
8133 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8134 mark_addressable (*expr_p);
8135 ret = GS_OK;
8137 else
8138 ret = GS_ALL_DONE;
8139 break;
8141 /* The following are special cases that are not handled by the
8142 original GIMPLE grammar. */
8144 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
8145 eliminated. */
8146 case SAVE_EXPR:
8147 ret = gimplify_save_expr (expr_p, pre_p, post_p);
8148 break;
8150 case BIT_FIELD_REF:
8151 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8152 post_p, is_gimple_lvalue, fb_either);
8153 recalculate_side_effects (*expr_p);
8154 break;
8156 case TARGET_MEM_REF:
8158 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
8160 if (TMR_BASE (*expr_p))
8161 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
8162 post_p, is_gimple_mem_ref_addr, fb_either);
8163 if (TMR_INDEX (*expr_p))
8164 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
8165 post_p, is_gimple_val, fb_rvalue);
8166 if (TMR_INDEX2 (*expr_p))
8167 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
8168 post_p, is_gimple_val, fb_rvalue);
8169 /* TMR_STEP and TMR_OFFSET are always integer constants. */
8170 ret = MIN (r0, r1);
8172 break;
8174 case NON_LVALUE_EXPR:
8175 /* This should have been stripped above. */
8176 gcc_unreachable ();
8178 case ASM_EXPR:
8179 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
8180 break;
8182 case TRY_FINALLY_EXPR:
8183 case TRY_CATCH_EXPR:
8185 gimple_seq eval, cleanup;
8186 gimple try_;
8188 /* Calls to destructors are generated automatically in FINALLY/CATCH
8189 block. They should have location as UNKNOWN_LOCATION. However,
8190 gimplify_call_expr will reset these call stmts to input_location
8191 if it finds stmt's location is unknown. To prevent resetting for
8192 destructors, we set the input_location to unknown.
8193 Note that this only affects the destructor calls in FINALLY/CATCH
8194 block, and will automatically reset to its original value by the
8195 end of gimplify_expr. */
8196 input_location = UNKNOWN_LOCATION;
8197 eval = cleanup = NULL;
8198 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8199 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8200 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8201 if (gimple_seq_empty_p (cleanup))
8203 gimple_seq_add_seq (pre_p, eval);
8204 ret = GS_ALL_DONE;
8205 break;
8207 try_ = gimple_build_try (eval, cleanup,
8208 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8209 ? GIMPLE_TRY_FINALLY
8210 : GIMPLE_TRY_CATCH);
8211 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8212 gimple_set_location (try_, saved_location);
8213 else
8214 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8215 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8216 gimple_try_set_catch_is_cleanup (try_,
8217 TRY_CATCH_IS_CLEANUP (*expr_p));
8218 gimplify_seq_add_stmt (pre_p, try_);
8219 ret = GS_ALL_DONE;
8220 break;
8223 case CLEANUP_POINT_EXPR:
8224 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8225 break;
8227 case TARGET_EXPR:
8228 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8229 break;
8231 case CATCH_EXPR:
8233 gimple c;
8234 gimple_seq handler = NULL;
8235 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8236 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8237 gimplify_seq_add_stmt (pre_p, c);
8238 ret = GS_ALL_DONE;
8239 break;
8242 case EH_FILTER_EXPR:
8244 gimple ehf;
8245 gimple_seq failure = NULL;
8247 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8248 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8249 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8250 gimplify_seq_add_stmt (pre_p, ehf);
8251 ret = GS_ALL_DONE;
8252 break;
8255 case OBJ_TYPE_REF:
8257 enum gimplify_status r0, r1;
8258 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8259 post_p, is_gimple_val, fb_rvalue);
8260 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8261 post_p, is_gimple_val, fb_rvalue);
8262 TREE_SIDE_EFFECTS (*expr_p) = 0;
8263 ret = MIN (r0, r1);
8265 break;
8267 case LABEL_DECL:
8268 /* We get here when taking the address of a label. We mark
8269 the label as "forced"; meaning it can never be removed and
8270 it is a potential target for any computed goto. */
8271 FORCED_LABEL (*expr_p) = 1;
8272 ret = GS_ALL_DONE;
8273 break;
8275 case STATEMENT_LIST:
8276 ret = gimplify_statement_list (expr_p, pre_p);
8277 break;
8279 case WITH_SIZE_EXPR:
8281 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8282 post_p == &internal_post ? NULL : post_p,
8283 gimple_test_f, fallback);
8284 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8285 is_gimple_val, fb_rvalue);
8286 ret = GS_ALL_DONE;
8288 break;
8290 case VAR_DECL:
8291 case PARM_DECL:
8292 ret = gimplify_var_or_parm_decl (expr_p);
8293 break;
8295 case RESULT_DECL:
8296 /* When within an OpenMP context, notice uses of variables. */
8297 if (gimplify_omp_ctxp)
8298 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8299 ret = GS_ALL_DONE;
8300 break;
8302 case SSA_NAME:
8303 /* Allow callbacks into the gimplifier during optimization. */
8304 ret = GS_ALL_DONE;
8305 break;
8307 case OACC_HOST_DATA:
8308 case OACC_DECLARE:
8309 sorry ("directive not yet implemented");
8310 ret = GS_ALL_DONE;
8311 break;
8313 case OMP_PARALLEL:
8314 gimplify_omp_parallel (expr_p, pre_p);
8315 ret = GS_ALL_DONE;
8316 break;
8318 case OMP_TASK:
8319 gimplify_omp_task (expr_p, pre_p);
8320 ret = GS_ALL_DONE;
8321 break;
8323 case OMP_FOR:
8324 case OMP_SIMD:
8325 case CILK_SIMD:
8326 case CILK_FOR:
8327 case OMP_DISTRIBUTE:
8328 case OACC_LOOP:
8329 ret = gimplify_omp_for (expr_p, pre_p);
8330 break;
8332 case OACC_KERNELS:
8333 if (OACC_KERNELS_COMBINED (*expr_p))
8334 sorry ("directive not yet implemented");
8335 else
8336 gimplify_omp_workshare (expr_p, pre_p);
8337 ret = GS_ALL_DONE;
8338 break;
8340 case OACC_PARALLEL:
8341 if (OACC_PARALLEL_COMBINED (*expr_p))
8342 sorry ("directive not yet implemented");
8343 else
8344 gimplify_omp_workshare (expr_p, pre_p);
8345 ret = GS_ALL_DONE;
8346 break;
8348 case OACC_CACHE:
8349 gimplify_oacc_cache (expr_p, pre_p);
8350 ret = GS_ALL_DONE;
8351 break;
8353 case OACC_DATA:
8354 case OMP_SECTIONS:
8355 case OMP_SINGLE:
8356 case OMP_TARGET:
8357 case OMP_TARGET_DATA:
8358 case OMP_TEAMS:
8359 gimplify_omp_workshare (expr_p, pre_p);
8360 ret = GS_ALL_DONE;
8361 break;
8363 case OACC_UPDATE:
8364 case OMP_TARGET_UPDATE:
8365 case OACC_ENTER_DATA:
8366 case OACC_EXIT_DATA:
8367 gimplify_omp_target_update (expr_p, pre_p);
8368 ret = GS_ALL_DONE;
8369 break;
8371 case OMP_SECTION:
8372 case OMP_MASTER:
8373 case OMP_TASKGROUP:
8374 case OMP_ORDERED:
8375 case OMP_CRITICAL:
8377 gimple_seq body = NULL;
8378 gimple g;
8380 gimplify_and_add (OMP_BODY (*expr_p), &body);
8381 switch (TREE_CODE (*expr_p))
8383 case OMP_SECTION:
8384 g = gimple_build_omp_section (body);
8385 break;
8386 case OMP_MASTER:
8387 g = gimple_build_omp_master (body);
8388 break;
8389 case OMP_TASKGROUP:
8391 gimple_seq cleanup = NULL;
8392 tree fn
8393 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8394 g = gimple_build_call (fn, 0);
8395 gimple_seq_add_stmt (&cleanup, g);
8396 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8397 body = NULL;
8398 gimple_seq_add_stmt (&body, g);
8399 g = gimple_build_omp_taskgroup (body);
8401 break;
8402 case OMP_ORDERED:
8403 g = gimple_build_omp_ordered (body);
8404 break;
8405 case OMP_CRITICAL:
8406 g = gimple_build_omp_critical (body,
8407 OMP_CRITICAL_NAME (*expr_p));
8408 break;
8409 default:
8410 gcc_unreachable ();
8412 gimplify_seq_add_stmt (pre_p, g);
8413 ret = GS_ALL_DONE;
8414 break;
8417 case OMP_ATOMIC:
8418 case OMP_ATOMIC_READ:
8419 case OMP_ATOMIC_CAPTURE_OLD:
8420 case OMP_ATOMIC_CAPTURE_NEW:
8421 ret = gimplify_omp_atomic (expr_p, pre_p);
8422 break;
8424 case TRANSACTION_EXPR:
8425 ret = gimplify_transaction (expr_p, pre_p);
8426 break;
8428 case TRUTH_AND_EXPR:
8429 case TRUTH_OR_EXPR:
8430 case TRUTH_XOR_EXPR:
8432 tree orig_type = TREE_TYPE (*expr_p);
8433 tree new_type, xop0, xop1;
8434 *expr_p = gimple_boolify (*expr_p);
8435 new_type = TREE_TYPE (*expr_p);
8436 if (!useless_type_conversion_p (orig_type, new_type))
8438 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8439 ret = GS_OK;
8440 break;
8443 /* Boolified binary truth expressions are semantically equivalent
8444 to bitwise binary expressions. Canonicalize them to the
8445 bitwise variant. */
8446 switch (TREE_CODE (*expr_p))
8448 case TRUTH_AND_EXPR:
8449 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8450 break;
8451 case TRUTH_OR_EXPR:
8452 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8453 break;
8454 case TRUTH_XOR_EXPR:
8455 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8456 break;
8457 default:
8458 break;
8460 /* Now make sure that operands have compatible type to
8461 expression's new_type. */
8462 xop0 = TREE_OPERAND (*expr_p, 0);
8463 xop1 = TREE_OPERAND (*expr_p, 1);
8464 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8465 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8466 new_type,
8467 xop0);
8468 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8469 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8470 new_type,
8471 xop1);
8472 /* Continue classified as tcc_binary. */
8473 goto expr_2;
8476 case FMA_EXPR:
8477 case VEC_COND_EXPR:
8478 case VEC_PERM_EXPR:
8479 /* Classified as tcc_expression. */
8480 goto expr_3;
8482 case POINTER_PLUS_EXPR:
8484 enum gimplify_status r0, r1;
8485 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8486 post_p, is_gimple_val, fb_rvalue);
8487 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8488 post_p, is_gimple_val, fb_rvalue);
8489 recalculate_side_effects (*expr_p);
8490 ret = MIN (r0, r1);
8491 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8492 after gimplifying operands - this is similar to how
8493 it would be folding all gimplified stmts on creation
8494 to have them canonicalized, which is what we eventually
8495 should do anyway. */
8496 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8497 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8499 *expr_p = build_fold_addr_expr_with_type_loc
8500 (input_location,
8501 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8502 TREE_OPERAND (*expr_p, 0),
8503 fold_convert (ptr_type_node,
8504 TREE_OPERAND (*expr_p, 1))),
8505 TREE_TYPE (*expr_p));
8506 ret = MIN (ret, GS_OK);
8508 break;
8511 case CILK_SYNC_STMT:
8513 if (!fn_contains_cilk_spawn_p (cfun))
8515 error_at (EXPR_LOCATION (*expr_p),
8516 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8517 ret = GS_ERROR;
8519 else
8521 gimplify_cilk_sync (expr_p, pre_p);
8522 ret = GS_ALL_DONE;
8524 break;
8527 default:
8528 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8530 case tcc_comparison:
8531 /* Handle comparison of objects of non scalar mode aggregates
8532 with a call to memcmp. It would be nice to only have to do
8533 this for variable-sized objects, but then we'd have to allow
8534 the same nest of reference nodes we allow for MODIFY_EXPR and
8535 that's too complex.
8537 Compare scalar mode aggregates as scalar mode values. Using
8538 memcmp for them would be very inefficient at best, and is
8539 plain wrong if bitfields are involved. */
8541 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8543 /* Vector comparisons need no boolification. */
8544 if (TREE_CODE (type) == VECTOR_TYPE)
8545 goto expr_2;
8546 else if (!AGGREGATE_TYPE_P (type))
8548 tree org_type = TREE_TYPE (*expr_p);
8549 *expr_p = gimple_boolify (*expr_p);
8550 if (!useless_type_conversion_p (org_type,
8551 TREE_TYPE (*expr_p)))
8553 *expr_p = fold_convert_loc (input_location,
8554 org_type, *expr_p);
8555 ret = GS_OK;
8557 else
8558 goto expr_2;
8560 else if (TYPE_MODE (type) != BLKmode)
8561 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8562 else
8563 ret = gimplify_variable_sized_compare (expr_p);
8565 break;
8568 /* If *EXPR_P does not need to be special-cased, handle it
8569 according to its class. */
8570 case tcc_unary:
8571 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8572 post_p, is_gimple_val, fb_rvalue);
8573 break;
8575 case tcc_binary:
8576 expr_2:
8578 enum gimplify_status r0, r1;
8580 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8581 post_p, is_gimple_val, fb_rvalue);
8582 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8583 post_p, is_gimple_val, fb_rvalue);
8585 ret = MIN (r0, r1);
8586 break;
8589 expr_3:
8591 enum gimplify_status r0, r1, r2;
8593 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8594 post_p, is_gimple_val, fb_rvalue);
8595 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8596 post_p, is_gimple_val, fb_rvalue);
8597 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8598 post_p, is_gimple_val, fb_rvalue);
8600 ret = MIN (MIN (r0, r1), r2);
8601 break;
8604 case tcc_declaration:
8605 case tcc_constant:
8606 ret = GS_ALL_DONE;
8607 goto dont_recalculate;
8609 default:
8610 gcc_unreachable ();
8613 recalculate_side_effects (*expr_p);
8615 dont_recalculate:
8616 break;
8619 gcc_assert (*expr_p || ret != GS_OK);
8621 while (ret == GS_OK);
8623 /* If we encountered an error_mark somewhere nested inside, either
8624 stub out the statement or propagate the error back out. */
8625 if (ret == GS_ERROR)
8627 if (is_statement)
8628 *expr_p = NULL;
8629 goto out;
8632 /* This was only valid as a return value from the langhook, which
8633 we handled. Make sure it doesn't escape from any other context. */
8634 gcc_assert (ret != GS_UNHANDLED);
8636 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8638 /* We aren't looking for a value, and we don't have a valid
8639 statement. If it doesn't have side-effects, throw it away. */
8640 if (!TREE_SIDE_EFFECTS (*expr_p))
8641 *expr_p = NULL;
8642 else if (!TREE_THIS_VOLATILE (*expr_p))
8644 /* This is probably a _REF that contains something nested that
8645 has side effects. Recurse through the operands to find it. */
8646 enum tree_code code = TREE_CODE (*expr_p);
8648 switch (code)
8650 case COMPONENT_REF:
8651 case REALPART_EXPR:
8652 case IMAGPART_EXPR:
8653 case VIEW_CONVERT_EXPR:
8654 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8655 gimple_test_f, fallback);
8656 break;
8658 case ARRAY_REF:
8659 case ARRAY_RANGE_REF:
8660 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8661 gimple_test_f, fallback);
8662 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8663 gimple_test_f, fallback);
8664 break;
8666 default:
8667 /* Anything else with side-effects must be converted to
8668 a valid statement before we get here. */
8669 gcc_unreachable ();
8672 *expr_p = NULL;
8674 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8675 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8677 /* Historically, the compiler has treated a bare reference
8678 to a non-BLKmode volatile lvalue as forcing a load. */
8679 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8681 /* Normally, we do not want to create a temporary for a
8682 TREE_ADDRESSABLE type because such a type should not be
8683 copied by bitwise-assignment. However, we make an
8684 exception here, as all we are doing here is ensuring that
8685 we read the bytes that make up the type. We use
8686 create_tmp_var_raw because create_tmp_var will abort when
8687 given a TREE_ADDRESSABLE type. */
8688 tree tmp = create_tmp_var_raw (type, "vol");
8689 gimple_add_tmp_var (tmp);
8690 gimplify_assign (tmp, *expr_p, pre_p);
8691 *expr_p = NULL;
8693 else
8694 /* We can't do anything useful with a volatile reference to
8695 an incomplete type, so just throw it away. Likewise for
8696 a BLKmode type, since any implicit inner load should
8697 already have been turned into an explicit one by the
8698 gimplification process. */
8699 *expr_p = NULL;
8702 /* If we are gimplifying at the statement level, we're done. Tack
8703 everything together and return. */
8704 if (fallback == fb_none || is_statement)
8706 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8707 it out for GC to reclaim it. */
8708 *expr_p = NULL_TREE;
8710 if (!gimple_seq_empty_p (internal_pre)
8711 || !gimple_seq_empty_p (internal_post))
8713 gimplify_seq_add_seq (&internal_pre, internal_post);
8714 gimplify_seq_add_seq (pre_p, internal_pre);
8717 /* The result of gimplifying *EXPR_P is going to be the last few
8718 statements in *PRE_P and *POST_P. Add location information
8719 to all the statements that were added by the gimplification
8720 helpers. */
8721 if (!gimple_seq_empty_p (*pre_p))
8722 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8724 if (!gimple_seq_empty_p (*post_p))
8725 annotate_all_with_location_after (*post_p, post_last_gsi,
8726 input_location);
8728 goto out;
8731 #ifdef ENABLE_GIMPLE_CHECKING
8732 if (*expr_p)
8734 enum tree_code code = TREE_CODE (*expr_p);
8735 /* These expressions should already be in gimple IR form. */
8736 gcc_assert (code != MODIFY_EXPR
8737 && code != ASM_EXPR
8738 && code != BIND_EXPR
8739 && code != CATCH_EXPR
8740 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8741 && code != EH_FILTER_EXPR
8742 && code != GOTO_EXPR
8743 && code != LABEL_EXPR
8744 && code != LOOP_EXPR
8745 && code != SWITCH_EXPR
8746 && code != TRY_FINALLY_EXPR
8747 && code != OACC_PARALLEL
8748 && code != OACC_KERNELS
8749 && code != OACC_DATA
8750 && code != OACC_HOST_DATA
8751 && code != OACC_DECLARE
8752 && code != OACC_UPDATE
8753 && code != OACC_ENTER_DATA
8754 && code != OACC_EXIT_DATA
8755 && code != OACC_CACHE
8756 && code != OMP_CRITICAL
8757 && code != OMP_FOR
8758 && code != OACC_LOOP
8759 && code != OMP_MASTER
8760 && code != OMP_TASKGROUP
8761 && code != OMP_ORDERED
8762 && code != OMP_PARALLEL
8763 && code != OMP_SECTIONS
8764 && code != OMP_SECTION
8765 && code != OMP_SINGLE);
8767 #endif
8769 /* Otherwise we're gimplifying a subexpression, so the resulting
8770 value is interesting. If it's a valid operand that matches
8771 GIMPLE_TEST_F, we're done. Unless we are handling some
8772 post-effects internally; if that's the case, we need to copy into
8773 a temporary before adding the post-effects to POST_P. */
8774 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8775 goto out;
8777 /* Otherwise, we need to create a new temporary for the gimplified
8778 expression. */
8780 /* We can't return an lvalue if we have an internal postqueue. The
8781 object the lvalue refers to would (probably) be modified by the
8782 postqueue; we need to copy the value out first, which means an
8783 rvalue. */
8784 if ((fallback & fb_lvalue)
8785 && gimple_seq_empty_p (internal_post)
8786 && is_gimple_addressable (*expr_p))
8788 /* An lvalue will do. Take the address of the expression, store it
8789 in a temporary, and replace the expression with an INDIRECT_REF of
8790 that temporary. */
8791 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8792 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8793 *expr_p = build_simple_mem_ref (tmp);
8795 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8797 /* An rvalue will do. Assign the gimplified expression into a
8798 new temporary TMP and replace the original expression with
8799 TMP. First, make sure that the expression has a type so that
8800 it can be assigned into a temporary. */
8801 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8802 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8804 else
8806 #ifdef ENABLE_GIMPLE_CHECKING
8807 if (!(fallback & fb_mayfail))
8809 fprintf (stderr, "gimplification failed:\n");
8810 print_generic_expr (stderr, *expr_p, 0);
8811 debug_tree (*expr_p);
8812 internal_error ("gimplification failed");
8814 #endif
8815 gcc_assert (fallback & fb_mayfail);
8817 /* If this is an asm statement, and the user asked for the
8818 impossible, don't die. Fail and let gimplify_asm_expr
8819 issue an error. */
8820 ret = GS_ERROR;
8821 goto out;
8824 /* Make sure the temporary matches our predicate. */
8825 gcc_assert ((*gimple_test_f) (*expr_p));
8827 if (!gimple_seq_empty_p (internal_post))
8829 annotate_all_with_location (internal_post, input_location);
8830 gimplify_seq_add_seq (pre_p, internal_post);
8833 out:
8834 input_location = saved_location;
8835 return ret;
8838 /* Look through TYPE for variable-sized objects and gimplify each such
8839 size that we find. Add to LIST_P any statements generated. */
8841 void
8842 gimplify_type_sizes (tree type, gimple_seq *list_p)
8844 tree field, t;
8846 if (type == NULL || type == error_mark_node)
8847 return;
8849 /* We first do the main variant, then copy into any other variants. */
8850 type = TYPE_MAIN_VARIANT (type);
8852 /* Avoid infinite recursion. */
8853 if (TYPE_SIZES_GIMPLIFIED (type))
8854 return;
8856 TYPE_SIZES_GIMPLIFIED (type) = 1;
8858 switch (TREE_CODE (type))
8860 case INTEGER_TYPE:
8861 case ENUMERAL_TYPE:
8862 case BOOLEAN_TYPE:
8863 case REAL_TYPE:
8864 case FIXED_POINT_TYPE:
8865 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8866 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8868 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8870 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8871 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8873 break;
8875 case ARRAY_TYPE:
8876 /* These types may not have declarations, so handle them here. */
8877 gimplify_type_sizes (TREE_TYPE (type), list_p);
8878 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8879 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8880 with assigned stack slots, for -O1+ -g they should be tracked
8881 by VTA. */
8882 if (!(TYPE_NAME (type)
8883 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8884 && DECL_IGNORED_P (TYPE_NAME (type)))
8885 && TYPE_DOMAIN (type)
8886 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8888 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8889 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8890 DECL_IGNORED_P (t) = 0;
8891 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8892 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8893 DECL_IGNORED_P (t) = 0;
8895 break;
8897 case RECORD_TYPE:
8898 case UNION_TYPE:
8899 case QUAL_UNION_TYPE:
8900 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8901 if (TREE_CODE (field) == FIELD_DECL)
8903 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8904 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8905 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8906 gimplify_type_sizes (TREE_TYPE (field), list_p);
8908 break;
8910 case POINTER_TYPE:
8911 case REFERENCE_TYPE:
8912 /* We used to recurse on the pointed-to type here, which turned out to
8913 be incorrect because its definition might refer to variables not
8914 yet initialized at this point if a forward declaration is involved.
8916 It was actually useful for anonymous pointed-to types to ensure
8917 that the sizes evaluation dominates every possible later use of the
8918 values. Restricting to such types here would be safe since there
8919 is no possible forward declaration around, but would introduce an
8920 undesirable middle-end semantic to anonymity. We then defer to
8921 front-ends the responsibility of ensuring that the sizes are
8922 evaluated both early and late enough, e.g. by attaching artificial
8923 type declarations to the tree. */
8924 break;
8926 default:
8927 break;
8930 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8931 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8933 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8935 TYPE_SIZE (t) = TYPE_SIZE (type);
8936 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8937 TYPE_SIZES_GIMPLIFIED (t) = 1;
8941 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8942 a size or position, has had all of its SAVE_EXPRs evaluated.
8943 We add any required statements to *STMT_P. */
8945 void
8946 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8948 tree expr = *expr_p;
8950 /* We don't do anything if the value isn't there, is constant, or contains
8951 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8952 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8953 will want to replace it with a new variable, but that will cause problems
8954 if this type is from outside the function. It's OK to have that here. */
8955 if (is_gimple_sizepos (expr))
8956 return;
8958 *expr_p = unshare_expr (expr);
8960 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8963 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8964 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8965 is true, also gimplify the parameters. */
8967 gimple
8968 gimplify_body (tree fndecl, bool do_parms)
8970 location_t saved_location = input_location;
8971 gimple_seq parm_stmts, seq;
8972 gimple outer_bind;
8973 struct cgraph_node *cgn;
8975 timevar_push (TV_TREE_GIMPLIFY);
8977 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8978 gimplification. */
8979 default_rtl_profile ();
8981 gcc_assert (gimplify_ctxp == NULL);
8982 push_gimplify_context ();
8984 if (flag_openacc || flag_openmp)
8986 gcc_assert (gimplify_omp_ctxp == NULL);
8987 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8988 gimplify_omp_ctxp
8989 = new_omp_context ((enum omp_region_type) (ORT_TARGET
8990 | ORT_TARGET_OFFLOAD));
8993 /* Unshare most shared trees in the body and in that of any nested functions.
8994 It would seem we don't have to do this for nested functions because
8995 they are supposed to be output and then the outer function gimplified
8996 first, but the g++ front end doesn't always do it that way. */
8997 unshare_body (fndecl);
8998 unvisit_body (fndecl);
9000 cgn = cgraph_node::get (fndecl);
9001 if (cgn && cgn->origin)
9002 nonlocal_vlas = new hash_set<tree>;
9004 /* Make sure input_location isn't set to something weird. */
9005 input_location = DECL_SOURCE_LOCATION (fndecl);
9007 /* Resolve callee-copies. This has to be done before processing
9008 the body so that DECL_VALUE_EXPR gets processed correctly. */
9009 parm_stmts = do_parms ? gimplify_parameters () : NULL;
9011 /* Gimplify the function's body. */
9012 seq = NULL;
9013 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
9014 outer_bind = gimple_seq_first_stmt (seq);
9015 if (!outer_bind)
9017 outer_bind = gimple_build_nop ();
9018 gimplify_seq_add_stmt (&seq, outer_bind);
9021 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
9022 not the case, wrap everything in a GIMPLE_BIND to make it so. */
9023 if (gimple_code (outer_bind) == GIMPLE_BIND
9024 && gimple_seq_first (seq) == gimple_seq_last (seq))
9026 else
9027 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
9029 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9031 /* If we had callee-copies statements, insert them at the beginning
9032 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
9033 if (!gimple_seq_empty_p (parm_stmts))
9035 tree parm;
9037 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
9038 gimple_bind_set_body (outer_bind, parm_stmts);
9040 for (parm = DECL_ARGUMENTS (current_function_decl);
9041 parm; parm = DECL_CHAIN (parm))
9042 if (DECL_HAS_VALUE_EXPR_P (parm))
9044 DECL_HAS_VALUE_EXPR_P (parm) = 0;
9045 DECL_IGNORED_P (parm) = 0;
9049 if (nonlocal_vlas)
9051 if (nonlocal_vla_vars)
9053 /* tree-nested.c may later on call declare_vars (..., true);
9054 which relies on BLOCK_VARS chain to be the tail of the
9055 gimple_bind_vars chain. Ensure we don't violate that
9056 assumption. */
9057 if (gimple_bind_block (outer_bind)
9058 == DECL_INITIAL (current_function_decl))
9059 declare_vars (nonlocal_vla_vars, outer_bind, true);
9060 else
9061 BLOCK_VARS (DECL_INITIAL (current_function_decl))
9062 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
9063 nonlocal_vla_vars);
9064 nonlocal_vla_vars = NULL_TREE;
9066 delete nonlocal_vlas;
9067 nonlocal_vlas = NULL;
9070 if ((flag_openacc || flag_openmp || flag_openmp_simd)
9071 && gimplify_omp_ctxp)
9073 delete_omp_context (gimplify_omp_ctxp);
9074 gimplify_omp_ctxp = NULL;
9077 pop_gimplify_context (outer_bind);
9078 gcc_assert (gimplify_ctxp == NULL);
9080 #ifdef ENABLE_CHECKING
9081 if (!seen_error ())
9082 verify_gimple_in_seq (gimple_bind_body (outer_bind));
9083 #endif
9085 timevar_pop (TV_TREE_GIMPLIFY);
9086 input_location = saved_location;
9088 return outer_bind;
9091 typedef char *char_p; /* For DEF_VEC_P. */
9093 /* Return whether we should exclude FNDECL from instrumentation. */
9095 static bool
9096 flag_instrument_functions_exclude_p (tree fndecl)
9098 vec<char_p> *v;
9100 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
9101 if (v && v->length () > 0)
9103 const char *name;
9104 int i;
9105 char *s;
9107 name = lang_hooks.decl_printable_name (fndecl, 0);
9108 FOR_EACH_VEC_ELT (*v, i, s)
9109 if (strstr (name, s) != NULL)
9110 return true;
9113 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
9114 if (v && v->length () > 0)
9116 const char *name;
9117 int i;
9118 char *s;
9120 name = DECL_SOURCE_FILE (fndecl);
9121 FOR_EACH_VEC_ELT (*v, i, s)
9122 if (strstr (name, s) != NULL)
9123 return true;
9126 return false;
9129 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
9130 node for the function we want to gimplify.
9132 Return the sequence of GIMPLE statements corresponding to the body
9133 of FNDECL. */
9135 void
9136 gimplify_function_tree (tree fndecl)
9138 tree parm, ret;
9139 gimple_seq seq;
9140 gimple bind;
9142 gcc_assert (!gimple_body (fndecl));
9144 if (DECL_STRUCT_FUNCTION (fndecl))
9145 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
9146 else
9147 push_struct_function (fndecl);
9149 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
9151 /* Preliminarily mark non-addressed complex variables as eligible
9152 for promotion to gimple registers. We'll transform their uses
9153 as we find them. */
9154 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
9155 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
9156 && !TREE_THIS_VOLATILE (parm)
9157 && !needs_to_live_in_memory (parm))
9158 DECL_GIMPLE_REG_P (parm) = 1;
9161 ret = DECL_RESULT (fndecl);
9162 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
9163 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
9164 && !needs_to_live_in_memory (ret))
9165 DECL_GIMPLE_REG_P (ret) = 1;
9167 bind = gimplify_body (fndecl, true);
9169 /* The tree body of the function is no longer needed, replace it
9170 with the new GIMPLE body. */
9171 seq = NULL;
9172 gimple_seq_add_stmt (&seq, bind);
9173 gimple_set_body (fndecl, seq);
9175 /* If we're instrumenting function entry/exit, then prepend the call to
9176 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
9177 catch the exit hook. */
9178 /* ??? Add some way to ignore exceptions for this TFE. */
9179 if (flag_instrument_function_entry_exit
9180 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
9181 && !flag_instrument_functions_exclude_p (fndecl))
9183 tree x;
9184 gimple new_bind;
9185 gimple tf;
9186 gimple_seq cleanup = NULL, body = NULL;
9187 tree tmp_var;
9188 gimple call;
9190 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9191 call = gimple_build_call (x, 1, integer_zero_node);
9192 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9193 gimple_call_set_lhs (call, tmp_var);
9194 gimplify_seq_add_stmt (&cleanup, call);
9195 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
9196 call = gimple_build_call (x, 2,
9197 build_fold_addr_expr (current_function_decl),
9198 tmp_var);
9199 gimplify_seq_add_stmt (&cleanup, call);
9200 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
9202 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9203 call = gimple_build_call (x, 1, integer_zero_node);
9204 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9205 gimple_call_set_lhs (call, tmp_var);
9206 gimplify_seq_add_stmt (&body, call);
9207 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
9208 call = gimple_build_call (x, 2,
9209 build_fold_addr_expr (current_function_decl),
9210 tmp_var);
9211 gimplify_seq_add_stmt (&body, call);
9212 gimplify_seq_add_stmt (&body, tf);
9213 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
9214 /* Clear the block for BIND, since it is no longer directly inside
9215 the function, but within a try block. */
9216 gimple_bind_set_block (bind, NULL);
9218 /* Replace the current function body with the body
9219 wrapped in the try/finally TF. */
9220 seq = NULL;
9221 gimple_seq_add_stmt (&seq, new_bind);
9222 gimple_set_body (fndecl, seq);
9225 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9226 cfun->curr_properties = PROP_gimple_any;
9228 pop_cfun ();
9231 /* Return a dummy expression of type TYPE in order to keep going after an
9232 error. */
9234 static tree
9235 dummy_object (tree type)
9237 tree t = build_int_cst (build_pointer_type (type), 0);
9238 return build2 (MEM_REF, type, t, t);
9241 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9242 builtin function, but a very special sort of operator. */
9244 enum gimplify_status
9245 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9247 tree promoted_type, have_va_type;
9248 tree valist = TREE_OPERAND (*expr_p, 0);
9249 tree type = TREE_TYPE (*expr_p);
9250 tree t;
9251 location_t loc = EXPR_LOCATION (*expr_p);
9253 /* Verify that valist is of the proper type. */
9254 have_va_type = TREE_TYPE (valist);
9255 if (have_va_type == error_mark_node)
9256 return GS_ERROR;
9257 have_va_type = targetm.canonical_va_list_type (have_va_type);
9259 if (have_va_type == NULL_TREE)
9261 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9262 return GS_ERROR;
9265 /* Generate a diagnostic for requesting data of a type that cannot
9266 be passed through `...' due to type promotion at the call site. */
9267 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9268 != type)
9270 static bool gave_help;
9271 bool warned;
9273 /* Unfortunately, this is merely undefined, rather than a constraint
9274 violation, so we cannot make this an error. If this call is never
9275 executed, the program is still strictly conforming. */
9276 warned = warning_at (loc, 0,
9277 "%qT is promoted to %qT when passed through %<...%>",
9278 type, promoted_type);
9279 if (!gave_help && warned)
9281 gave_help = true;
9282 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9283 promoted_type, type);
9286 /* We can, however, treat "undefined" any way we please.
9287 Call abort to encourage the user to fix the program. */
9288 if (warned)
9289 inform (loc, "if this code is reached, the program will abort");
9290 /* Before the abort, allow the evaluation of the va_list
9291 expression to exit or longjmp. */
9292 gimplify_and_add (valist, pre_p);
9293 t = build_call_expr_loc (loc,
9294 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9295 gimplify_and_add (t, pre_p);
9297 /* This is dead code, but go ahead and finish so that the
9298 mode of the result comes out right. */
9299 *expr_p = dummy_object (type);
9300 return GS_ALL_DONE;
9302 else
9304 /* Make it easier for the backends by protecting the valist argument
9305 from multiple evaluations. */
9306 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9308 /* For this case, the backends will be expecting a pointer to
9309 TREE_TYPE (abi), but it's possible we've
9310 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9311 So fix it. */
9312 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9314 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9315 valist = fold_convert_loc (loc, p1,
9316 build_fold_addr_expr_loc (loc, valist));
9319 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9321 else
9322 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9324 if (!targetm.gimplify_va_arg_expr)
9325 /* FIXME: Once most targets are converted we should merely
9326 assert this is non-null. */
9327 return GS_ALL_DONE;
9329 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9330 return GS_OK;
9334 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9336 DST/SRC are the destination and source respectively. You can pass
9337 ungimplified trees in DST or SRC, in which case they will be
9338 converted to a gimple operand if necessary.
9340 This function returns the newly created GIMPLE_ASSIGN tuple. */
9342 gimple
9343 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9345 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9346 gimplify_and_add (t, seq_p);
9347 ggc_free (t);
9348 return gimple_seq_last_stmt (*seq_p);
9351 inline hashval_t
9352 gimplify_hasher::hash (const value_type *p)
9354 tree t = p->val;
9355 return iterative_hash_expr (t, 0);
9358 inline bool
9359 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9361 tree t1 = p1->val;
9362 tree t2 = p2->val;
9363 enum tree_code code = TREE_CODE (t1);
9365 if (TREE_CODE (t2) != code
9366 || TREE_TYPE (t1) != TREE_TYPE (t2))
9367 return false;
9369 if (!operand_equal_p (t1, t2, 0))
9370 return false;
9372 #ifdef ENABLE_CHECKING
9373 /* Only allow them to compare equal if they also hash equal; otherwise
9374 results are nondeterminate, and we fail bootstrap comparison. */
9375 gcc_assert (hash (p1) == hash (p2));
9376 #endif
9378 return true;