svn merge -r 217500:218679 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / gimplify.c
blob6ca4a6e6fef4747d9314c8aba263c35f85ea4766
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<gbind *> 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 (gbind *bind_stmt)
287 gimplify_ctxp->bind_expr_stack.reserve (8);
288 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
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 gbind *
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<gbind *>
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)));
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 gs, bool debug_info)
588 tree last = vars;
589 if (last)
591 tree temps, block;
593 gbind *scope = as_a <gbind *> (gs);
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 (gcall **save, gcall **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 gbind *bind_stmt;
1065 gimple_seq body, cleanup;
1066 gcall *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 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1110 BIND_EXPR_BLOCK (bind_expr));
1111 gimple_push_bind_expr (bind_stmt);
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 (bind_stmt, 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 gcall *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 gtry *gs;
1175 gimple_seq new_body;
1177 new_body = NULL;
1178 gs = gimple_build_try (gimple_bind_body (bind_stmt), 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 (bind_stmt, new_body);
1187 gimplify_ctxp->save_stack = old_save_stack;
1188 gimple_pop_bind_expr ();
1190 gimplify_seq_add_stmt (pre_p, bind_stmt);
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 greturn *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 greturn *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));
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 gswitch *switch_stmt;
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 glabel *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 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
1535 default_case, labels);
1536 gimplify_seq_add_stmt (pre_p, switch_stmt);
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 glabel *label_stmt;
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 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
1563 ctxp->case_labels.safe_push (*expr_p);
1564 gimplify_seq_add_stmt (pre_p, label_stmt);
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 gcall *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 /* 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 /* Gimplify the static chain. */
2467 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
2469 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
2470 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
2471 else
2473 enum gimplify_status t;
2474 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
2475 EXPR_LOCATION (*expr_p));
2476 if (t == GS_ERROR)
2477 ret = GS_ERROR;
2481 /* Verify the function result. */
2482 if (want_value && fndecl
2483 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2485 error_at (loc, "using result of function returning %<void%>");
2486 ret = GS_ERROR;
2489 /* Try this again in case gimplification exposed something. */
2490 if (ret != GS_ERROR)
2492 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2494 if (new_tree && new_tree != *expr_p)
2496 /* There was a transformation of this call which computes the
2497 same value, but in a more efficient way. Return and try
2498 again. */
2499 *expr_p = new_tree;
2500 return GS_OK;
2503 else
2505 *expr_p = error_mark_node;
2506 return GS_ERROR;
2509 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2510 decl. This allows us to eliminate redundant or useless
2511 calls to "const" functions. */
2512 if (TREE_CODE (*expr_p) == CALL_EXPR)
2514 int flags = call_expr_flags (*expr_p);
2515 if (flags & (ECF_CONST | ECF_PURE)
2516 /* An infinite loop is considered a side effect. */
2517 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2518 TREE_SIDE_EFFECTS (*expr_p) = 0;
2521 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2522 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2523 form and delegate the creation of a GIMPLE_CALL to
2524 gimplify_modify_expr. This is always possible because when
2525 WANT_VALUE is true, the caller wants the result of this call into
2526 a temporary, which means that we will emit an INIT_EXPR in
2527 internal_get_tmp_var which will then be handled by
2528 gimplify_modify_expr. */
2529 if (!want_value)
2531 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2532 have to do is replicate it as a GIMPLE_CALL tuple. */
2533 gimple_stmt_iterator gsi;
2534 call = gimple_build_call_from_tree (*expr_p);
2535 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2536 notice_special_calls (call);
2537 gimplify_seq_add_stmt (pre_p, call);
2538 gsi = gsi_last (*pre_p);
2539 maybe_fold_stmt (&gsi);
2540 *expr_p = NULL_TREE;
2542 else
2543 /* Remember the original function type. */
2544 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2545 CALL_EXPR_FN (*expr_p));
2547 return ret;
2550 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2551 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2553 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2554 condition is true or false, respectively. If null, we should generate
2555 our own to skip over the evaluation of this specific expression.
2557 LOCUS is the source location of the COND_EXPR.
2559 This function is the tree equivalent of do_jump.
2561 shortcut_cond_r should only be called by shortcut_cond_expr. */
2563 static tree
2564 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2565 location_t locus)
2567 tree local_label = NULL_TREE;
2568 tree t, expr = NULL;
2570 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2571 retain the shortcut semantics. Just insert the gotos here;
2572 shortcut_cond_expr will append the real blocks later. */
2573 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2575 location_t new_locus;
2577 /* Turn if (a && b) into
2579 if (a); else goto no;
2580 if (b) goto yes; else goto no;
2581 (no:) */
2583 if (false_label_p == NULL)
2584 false_label_p = &local_label;
2586 /* Keep the original source location on the first 'if'. */
2587 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2588 append_to_statement_list (t, &expr);
2590 /* Set the source location of the && on the second 'if'. */
2591 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2592 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2593 new_locus);
2594 append_to_statement_list (t, &expr);
2596 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2598 location_t new_locus;
2600 /* Turn if (a || b) into
2602 if (a) goto yes;
2603 if (b) goto yes; else goto no;
2604 (yes:) */
2606 if (true_label_p == NULL)
2607 true_label_p = &local_label;
2609 /* Keep the original source location on the first 'if'. */
2610 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2611 append_to_statement_list (t, &expr);
2613 /* Set the source location of the || on the second 'if'. */
2614 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2615 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2616 new_locus);
2617 append_to_statement_list (t, &expr);
2619 else if (TREE_CODE (pred) == COND_EXPR
2620 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2621 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2623 location_t new_locus;
2625 /* As long as we're messing with gotos, turn if (a ? b : c) into
2626 if (a)
2627 if (b) goto yes; else goto no;
2628 else
2629 if (c) goto yes; else goto no;
2631 Don't do this if one of the arms has void type, which can happen
2632 in C++ when the arm is throw. */
2634 /* Keep the original source location on the first 'if'. Set the source
2635 location of the ? on the second 'if'. */
2636 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2637 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2638 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2639 false_label_p, locus),
2640 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2641 false_label_p, new_locus));
2643 else
2645 expr = build3 (COND_EXPR, void_type_node, pred,
2646 build_and_jump (true_label_p),
2647 build_and_jump (false_label_p));
2648 SET_EXPR_LOCATION (expr, locus);
2651 if (local_label)
2653 t = build1 (LABEL_EXPR, void_type_node, local_label);
2654 append_to_statement_list (t, &expr);
2657 return expr;
2660 /* Given a conditional expression EXPR with short-circuit boolean
2661 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2662 predicate apart into the equivalent sequence of conditionals. */
2664 static tree
2665 shortcut_cond_expr (tree expr)
2667 tree pred = TREE_OPERAND (expr, 0);
2668 tree then_ = TREE_OPERAND (expr, 1);
2669 tree else_ = TREE_OPERAND (expr, 2);
2670 tree true_label, false_label, end_label, t;
2671 tree *true_label_p;
2672 tree *false_label_p;
2673 bool emit_end, emit_false, jump_over_else;
2674 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2675 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2677 /* First do simple transformations. */
2678 if (!else_se)
2680 /* If there is no 'else', turn
2681 if (a && b) then c
2682 into
2683 if (a) if (b) then c. */
2684 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2686 /* Keep the original source location on the first 'if'. */
2687 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2688 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2689 /* Set the source location of the && on the second 'if'. */
2690 if (EXPR_HAS_LOCATION (pred))
2691 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2692 then_ = shortcut_cond_expr (expr);
2693 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2694 pred = TREE_OPERAND (pred, 0);
2695 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2696 SET_EXPR_LOCATION (expr, locus);
2700 if (!then_se)
2702 /* If there is no 'then', turn
2703 if (a || b); else d
2704 into
2705 if (a); else if (b); else d. */
2706 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2708 /* Keep the original source location on the first 'if'. */
2709 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2710 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2711 /* Set the source location of the || on the second 'if'. */
2712 if (EXPR_HAS_LOCATION (pred))
2713 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2714 else_ = shortcut_cond_expr (expr);
2715 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2716 pred = TREE_OPERAND (pred, 0);
2717 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2718 SET_EXPR_LOCATION (expr, locus);
2722 /* If we're done, great. */
2723 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2724 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2725 return expr;
2727 /* Otherwise we need to mess with gotos. Change
2728 if (a) c; else d;
2730 if (a); else goto no;
2731 c; goto end;
2732 no: d; end:
2733 and recursively gimplify the condition. */
2735 true_label = false_label = end_label = NULL_TREE;
2737 /* If our arms just jump somewhere, hijack those labels so we don't
2738 generate jumps to jumps. */
2740 if (then_
2741 && TREE_CODE (then_) == GOTO_EXPR
2742 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2744 true_label = GOTO_DESTINATION (then_);
2745 then_ = NULL;
2746 then_se = false;
2749 if (else_
2750 && TREE_CODE (else_) == GOTO_EXPR
2751 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2753 false_label = GOTO_DESTINATION (else_);
2754 else_ = NULL;
2755 else_se = false;
2758 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2759 if (true_label)
2760 true_label_p = &true_label;
2761 else
2762 true_label_p = NULL;
2764 /* The 'else' branch also needs a label if it contains interesting code. */
2765 if (false_label || else_se)
2766 false_label_p = &false_label;
2767 else
2768 false_label_p = NULL;
2770 /* If there was nothing else in our arms, just forward the label(s). */
2771 if (!then_se && !else_se)
2772 return shortcut_cond_r (pred, true_label_p, false_label_p,
2773 EXPR_LOC_OR_LOC (expr, input_location));
2775 /* If our last subexpression already has a terminal label, reuse it. */
2776 if (else_se)
2777 t = expr_last (else_);
2778 else if (then_se)
2779 t = expr_last (then_);
2780 else
2781 t = NULL;
2782 if (t && TREE_CODE (t) == LABEL_EXPR)
2783 end_label = LABEL_EXPR_LABEL (t);
2785 /* If we don't care about jumping to the 'else' branch, jump to the end
2786 if the condition is false. */
2787 if (!false_label_p)
2788 false_label_p = &end_label;
2790 /* We only want to emit these labels if we aren't hijacking them. */
2791 emit_end = (end_label == NULL_TREE);
2792 emit_false = (false_label == NULL_TREE);
2794 /* We only emit the jump over the else clause if we have to--if the
2795 then clause may fall through. Otherwise we can wind up with a
2796 useless jump and a useless label at the end of gimplified code,
2797 which will cause us to think that this conditional as a whole
2798 falls through even if it doesn't. If we then inline a function
2799 which ends with such a condition, that can cause us to issue an
2800 inappropriate warning about control reaching the end of a
2801 non-void function. */
2802 jump_over_else = block_may_fallthru (then_);
2804 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2805 EXPR_LOC_OR_LOC (expr, input_location));
2807 expr = NULL;
2808 append_to_statement_list (pred, &expr);
2810 append_to_statement_list (then_, &expr);
2811 if (else_se)
2813 if (jump_over_else)
2815 tree last = expr_last (expr);
2816 t = build_and_jump (&end_label);
2817 if (EXPR_HAS_LOCATION (last))
2818 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2819 append_to_statement_list (t, &expr);
2821 if (emit_false)
2823 t = build1 (LABEL_EXPR, void_type_node, false_label);
2824 append_to_statement_list (t, &expr);
2826 append_to_statement_list (else_, &expr);
2828 if (emit_end && end_label)
2830 t = build1 (LABEL_EXPR, void_type_node, end_label);
2831 append_to_statement_list (t, &expr);
2834 return expr;
2837 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2839 tree
2840 gimple_boolify (tree expr)
2842 tree type = TREE_TYPE (expr);
2843 location_t loc = EXPR_LOCATION (expr);
2845 if (TREE_CODE (expr) == NE_EXPR
2846 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2847 && integer_zerop (TREE_OPERAND (expr, 1)))
2849 tree call = TREE_OPERAND (expr, 0);
2850 tree fn = get_callee_fndecl (call);
2852 /* For __builtin_expect ((long) (x), y) recurse into x as well
2853 if x is truth_value_p. */
2854 if (fn
2855 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2856 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2857 && call_expr_nargs (call) == 2)
2859 tree arg = CALL_EXPR_ARG (call, 0);
2860 if (arg)
2862 if (TREE_CODE (arg) == NOP_EXPR
2863 && TREE_TYPE (arg) == TREE_TYPE (call))
2864 arg = TREE_OPERAND (arg, 0);
2865 if (truth_value_p (TREE_CODE (arg)))
2867 arg = gimple_boolify (arg);
2868 CALL_EXPR_ARG (call, 0)
2869 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2875 switch (TREE_CODE (expr))
2877 case TRUTH_AND_EXPR:
2878 case TRUTH_OR_EXPR:
2879 case TRUTH_XOR_EXPR:
2880 case TRUTH_ANDIF_EXPR:
2881 case TRUTH_ORIF_EXPR:
2882 /* Also boolify the arguments of truth exprs. */
2883 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2884 /* FALLTHRU */
2886 case TRUTH_NOT_EXPR:
2887 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2889 /* These expressions always produce boolean results. */
2890 if (TREE_CODE (type) != BOOLEAN_TYPE)
2891 TREE_TYPE (expr) = boolean_type_node;
2892 return expr;
2894 case ANNOTATE_EXPR:
2895 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2897 case annot_expr_ivdep_kind:
2898 case annot_expr_no_vector_kind:
2899 case annot_expr_vector_kind:
2900 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2901 if (TREE_CODE (type) != BOOLEAN_TYPE)
2902 TREE_TYPE (expr) = boolean_type_node;
2903 return expr;
2904 default:
2905 gcc_unreachable ();
2908 default:
2909 if (COMPARISON_CLASS_P (expr))
2911 /* There expressions always prduce boolean results. */
2912 if (TREE_CODE (type) != BOOLEAN_TYPE)
2913 TREE_TYPE (expr) = boolean_type_node;
2914 return expr;
2916 /* Other expressions that get here must have boolean values, but
2917 might need to be converted to the appropriate mode. */
2918 if (TREE_CODE (type) == BOOLEAN_TYPE)
2919 return expr;
2920 return fold_convert_loc (loc, boolean_type_node, expr);
2924 /* Given a conditional expression *EXPR_P without side effects, gimplify
2925 its operands. New statements are inserted to PRE_P. */
2927 static enum gimplify_status
2928 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2930 tree expr = *expr_p, cond;
2931 enum gimplify_status ret, tret;
2932 enum tree_code code;
2934 cond = gimple_boolify (COND_EXPR_COND (expr));
2936 /* We need to handle && and || specially, as their gimplification
2937 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2938 code = TREE_CODE (cond);
2939 if (code == TRUTH_ANDIF_EXPR)
2940 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2941 else if (code == TRUTH_ORIF_EXPR)
2942 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2943 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2944 COND_EXPR_COND (*expr_p) = cond;
2946 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2947 is_gimple_val, fb_rvalue);
2948 ret = MIN (ret, tret);
2949 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2950 is_gimple_val, fb_rvalue);
2952 return MIN (ret, tret);
2955 /* Return true if evaluating EXPR could trap.
2956 EXPR is GENERIC, while tree_could_trap_p can be called
2957 only on GIMPLE. */
2959 static bool
2960 generic_expr_could_trap_p (tree expr)
2962 unsigned i, n;
2964 if (!expr || is_gimple_val (expr))
2965 return false;
2967 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2968 return true;
2970 n = TREE_OPERAND_LENGTH (expr);
2971 for (i = 0; i < n; i++)
2972 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2973 return true;
2975 return false;
2978 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2979 into
2981 if (p) if (p)
2982 t1 = a; a;
2983 else or else
2984 t1 = b; b;
2987 The second form is used when *EXPR_P is of type void.
2989 PRE_P points to the list where side effects that must happen before
2990 *EXPR_P should be stored. */
2992 static enum gimplify_status
2993 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2995 tree expr = *expr_p;
2996 tree type = TREE_TYPE (expr);
2997 location_t loc = EXPR_LOCATION (expr);
2998 tree tmp, arm1, arm2;
2999 enum gimplify_status ret;
3000 tree label_true, label_false, label_cont;
3001 bool have_then_clause_p, have_else_clause_p;
3002 gcond *cond_stmt;
3003 enum tree_code pred_code;
3004 gimple_seq seq = NULL;
3006 /* If this COND_EXPR has a value, copy the values into a temporary within
3007 the arms. */
3008 if (!VOID_TYPE_P (type))
3010 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3011 tree result;
3013 /* If either an rvalue is ok or we do not require an lvalue, create the
3014 temporary. But we cannot do that if the type is addressable. */
3015 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3016 && !TREE_ADDRESSABLE (type))
3018 if (gimplify_ctxp->allow_rhs_cond_expr
3019 /* If either branch has side effects or could trap, it can't be
3020 evaluated unconditionally. */
3021 && !TREE_SIDE_EFFECTS (then_)
3022 && !generic_expr_could_trap_p (then_)
3023 && !TREE_SIDE_EFFECTS (else_)
3024 && !generic_expr_could_trap_p (else_))
3025 return gimplify_pure_cond_expr (expr_p, pre_p);
3027 tmp = create_tmp_var (type, "iftmp");
3028 result = tmp;
3031 /* Otherwise, only create and copy references to the values. */
3032 else
3034 type = build_pointer_type (type);
3036 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3037 then_ = build_fold_addr_expr_loc (loc, then_);
3039 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3040 else_ = build_fold_addr_expr_loc (loc, else_);
3042 expr
3043 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3045 tmp = create_tmp_var (type, "iftmp");
3046 result = build_simple_mem_ref_loc (loc, tmp);
3049 /* Build the new then clause, `tmp = then_;'. But don't build the
3050 assignment if the value is void; in C++ it can be if it's a throw. */
3051 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3052 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3054 /* Similarly, build the new else clause, `tmp = else_;'. */
3055 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3056 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3058 TREE_TYPE (expr) = void_type_node;
3059 recalculate_side_effects (expr);
3061 /* Move the COND_EXPR to the prequeue. */
3062 gimplify_stmt (&expr, pre_p);
3064 *expr_p = result;
3065 return GS_ALL_DONE;
3068 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3069 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3070 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3071 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3073 /* Make sure the condition has BOOLEAN_TYPE. */
3074 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3076 /* Break apart && and || conditions. */
3077 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3078 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3080 expr = shortcut_cond_expr (expr);
3082 if (expr != *expr_p)
3084 *expr_p = expr;
3086 /* We can't rely on gimplify_expr to re-gimplify the expanded
3087 form properly, as cleanups might cause the target labels to be
3088 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3089 set up a conditional context. */
3090 gimple_push_condition ();
3091 gimplify_stmt (expr_p, &seq);
3092 gimple_pop_condition (pre_p);
3093 gimple_seq_add_seq (pre_p, seq);
3095 return GS_ALL_DONE;
3099 /* Now do the normal gimplification. */
3101 /* Gimplify condition. */
3102 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3103 fb_rvalue);
3104 if (ret == GS_ERROR)
3105 return GS_ERROR;
3106 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3108 gimple_push_condition ();
3110 have_then_clause_p = have_else_clause_p = false;
3111 if (TREE_OPERAND (expr, 1) != NULL
3112 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3113 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3114 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3115 == current_function_decl)
3116 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3117 have different locations, otherwise we end up with incorrect
3118 location information on the branches. */
3119 && (optimize
3120 || !EXPR_HAS_LOCATION (expr)
3121 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3122 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3124 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3125 have_then_clause_p = true;
3127 else
3128 label_true = create_artificial_label (UNKNOWN_LOCATION);
3129 if (TREE_OPERAND (expr, 2) != NULL
3130 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3131 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3132 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3133 == current_function_decl)
3134 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3135 have different locations, otherwise we end up with incorrect
3136 location information on the branches. */
3137 && (optimize
3138 || !EXPR_HAS_LOCATION (expr)
3139 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3140 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3142 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3143 have_else_clause_p = true;
3145 else
3146 label_false = create_artificial_label (UNKNOWN_LOCATION);
3148 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3149 &arm2);
3151 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3152 label_false);
3154 gimplify_seq_add_stmt (&seq, cond_stmt);
3155 label_cont = NULL_TREE;
3156 if (!have_then_clause_p)
3158 /* For if (...) {} else { code; } put label_true after
3159 the else block. */
3160 if (TREE_OPERAND (expr, 1) == NULL_TREE
3161 && !have_else_clause_p
3162 && TREE_OPERAND (expr, 2) != NULL_TREE)
3163 label_cont = label_true;
3164 else
3166 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3167 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3168 /* For if (...) { code; } else {} or
3169 if (...) { code; } else goto label; or
3170 if (...) { code; return; } else { ... }
3171 label_cont isn't needed. */
3172 if (!have_else_clause_p
3173 && TREE_OPERAND (expr, 2) != NULL_TREE
3174 && gimple_seq_may_fallthru (seq))
3176 gimple g;
3177 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3179 g = gimple_build_goto (label_cont);
3181 /* GIMPLE_COND's are very low level; they have embedded
3182 gotos. This particular embedded goto should not be marked
3183 with the location of the original COND_EXPR, as it would
3184 correspond to the COND_EXPR's condition, not the ELSE or the
3185 THEN arms. To avoid marking it with the wrong location, flag
3186 it as "no location". */
3187 gimple_set_do_not_emit_location (g);
3189 gimplify_seq_add_stmt (&seq, g);
3193 if (!have_else_clause_p)
3195 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3196 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3198 if (label_cont)
3199 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3201 gimple_pop_condition (pre_p);
3202 gimple_seq_add_seq (pre_p, seq);
3204 if (ret == GS_ERROR)
3205 ; /* Do nothing. */
3206 else if (have_then_clause_p || have_else_clause_p)
3207 ret = GS_ALL_DONE;
3208 else
3210 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3211 expr = TREE_OPERAND (expr, 0);
3212 gimplify_stmt (&expr, pre_p);
3215 *expr_p = NULL;
3216 return ret;
3219 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3220 to be marked addressable.
3222 We cannot rely on such an expression being directly markable if a temporary
3223 has been created by the gimplification. In this case, we create another
3224 temporary and initialize it with a copy, which will become a store after we
3225 mark it addressable. This can happen if the front-end passed us something
3226 that it could not mark addressable yet, like a Fortran pass-by-reference
3227 parameter (int) floatvar. */
3229 static void
3230 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3232 while (handled_component_p (*expr_p))
3233 expr_p = &TREE_OPERAND (*expr_p, 0);
3234 if (is_gimple_reg (*expr_p))
3236 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3237 DECL_GIMPLE_REG_P (var) = 0;
3238 *expr_p = var;
3242 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3243 a call to __builtin_memcpy. */
3245 static enum gimplify_status
3246 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3247 gimple_seq *seq_p)
3249 tree t, to, to_ptr, from, from_ptr;
3250 gcall *gs;
3251 location_t loc = EXPR_LOCATION (*expr_p);
3253 to = TREE_OPERAND (*expr_p, 0);
3254 from = TREE_OPERAND (*expr_p, 1);
3256 /* Mark the RHS addressable. Beware that it may not be possible to do so
3257 directly if a temporary has been created by the gimplification. */
3258 prepare_gimple_addressable (&from, seq_p);
3260 mark_addressable (from);
3261 from_ptr = build_fold_addr_expr_loc (loc, from);
3262 gimplify_arg (&from_ptr, seq_p, loc);
3264 mark_addressable (to);
3265 to_ptr = build_fold_addr_expr_loc (loc, to);
3266 gimplify_arg (&to_ptr, seq_p, loc);
3268 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3270 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3272 if (want_value)
3274 /* tmp = memcpy() */
3275 t = create_tmp_var (TREE_TYPE (to_ptr));
3276 gimple_call_set_lhs (gs, t);
3277 gimplify_seq_add_stmt (seq_p, gs);
3279 *expr_p = build_simple_mem_ref (t);
3280 return GS_ALL_DONE;
3283 gimplify_seq_add_stmt (seq_p, gs);
3284 *expr_p = NULL;
3285 return GS_ALL_DONE;
3288 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3289 a call to __builtin_memset. In this case we know that the RHS is
3290 a CONSTRUCTOR with an empty element list. */
3292 static enum gimplify_status
3293 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3294 gimple_seq *seq_p)
3296 tree t, from, to, to_ptr;
3297 gcall *gs;
3298 location_t loc = EXPR_LOCATION (*expr_p);
3300 /* Assert our assumptions, to abort instead of producing wrong code
3301 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3302 not be immediately exposed. */
3303 from = TREE_OPERAND (*expr_p, 1);
3304 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3305 from = TREE_OPERAND (from, 0);
3307 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3308 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3310 /* Now proceed. */
3311 to = TREE_OPERAND (*expr_p, 0);
3313 to_ptr = build_fold_addr_expr_loc (loc, to);
3314 gimplify_arg (&to_ptr, seq_p, loc);
3315 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3317 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3319 if (want_value)
3321 /* tmp = memset() */
3322 t = create_tmp_var (TREE_TYPE (to_ptr));
3323 gimple_call_set_lhs (gs, t);
3324 gimplify_seq_add_stmt (seq_p, gs);
3326 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3327 return GS_ALL_DONE;
3330 gimplify_seq_add_stmt (seq_p, gs);
3331 *expr_p = NULL;
3332 return GS_ALL_DONE;
3335 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3336 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3337 assignment. Return non-null if we detect a potential overlap. */
3339 struct gimplify_init_ctor_preeval_data
3341 /* The base decl of the lhs object. May be NULL, in which case we
3342 have to assume the lhs is indirect. */
3343 tree lhs_base_decl;
3345 /* The alias set of the lhs object. */
3346 alias_set_type lhs_alias_set;
3349 static tree
3350 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3352 struct gimplify_init_ctor_preeval_data *data
3353 = (struct gimplify_init_ctor_preeval_data *) xdata;
3354 tree t = *tp;
3356 /* If we find the base object, obviously we have overlap. */
3357 if (data->lhs_base_decl == t)
3358 return t;
3360 /* If the constructor component is indirect, determine if we have a
3361 potential overlap with the lhs. The only bits of information we
3362 have to go on at this point are addressability and alias sets. */
3363 if ((INDIRECT_REF_P (t)
3364 || TREE_CODE (t) == MEM_REF)
3365 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3366 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3367 return t;
3369 /* If the constructor component is a call, determine if it can hide a
3370 potential overlap with the lhs through an INDIRECT_REF like above.
3371 ??? Ugh - this is completely broken. In fact this whole analysis
3372 doesn't look conservative. */
3373 if (TREE_CODE (t) == CALL_EXPR)
3375 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3377 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3378 if (POINTER_TYPE_P (TREE_VALUE (type))
3379 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3380 && alias_sets_conflict_p (data->lhs_alias_set,
3381 get_alias_set
3382 (TREE_TYPE (TREE_VALUE (type)))))
3383 return t;
3386 if (IS_TYPE_OR_DECL_P (t))
3387 *walk_subtrees = 0;
3388 return NULL;
3391 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3392 force values that overlap with the lhs (as described by *DATA)
3393 into temporaries. */
3395 static void
3396 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3397 struct gimplify_init_ctor_preeval_data *data)
3399 enum gimplify_status one;
3401 /* If the value is constant, then there's nothing to pre-evaluate. */
3402 if (TREE_CONSTANT (*expr_p))
3404 /* Ensure it does not have side effects, it might contain a reference to
3405 the object we're initializing. */
3406 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3407 return;
3410 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3411 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3412 return;
3414 /* Recurse for nested constructors. */
3415 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3417 unsigned HOST_WIDE_INT ix;
3418 constructor_elt *ce;
3419 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3421 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3422 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3424 return;
3427 /* If this is a variable sized type, we must remember the size. */
3428 maybe_with_size_expr (expr_p);
3430 /* Gimplify the constructor element to something appropriate for the rhs
3431 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3432 the gimplifier will consider this a store to memory. Doing this
3433 gimplification now means that we won't have to deal with complicated
3434 language-specific trees, nor trees like SAVE_EXPR that can induce
3435 exponential search behavior. */
3436 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3437 if (one == GS_ERROR)
3439 *expr_p = NULL;
3440 return;
3443 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3444 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3445 always be true for all scalars, since is_gimple_mem_rhs insists on a
3446 temporary variable for them. */
3447 if (DECL_P (*expr_p))
3448 return;
3450 /* If this is of variable size, we have no choice but to assume it doesn't
3451 overlap since we can't make a temporary for it. */
3452 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3453 return;
3455 /* Otherwise, we must search for overlap ... */
3456 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3457 return;
3459 /* ... and if found, force the value into a temporary. */
3460 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3463 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3464 a RANGE_EXPR in a CONSTRUCTOR for an array.
3466 var = lower;
3467 loop_entry:
3468 object[var] = value;
3469 if (var == upper)
3470 goto loop_exit;
3471 var = var + 1;
3472 goto loop_entry;
3473 loop_exit:
3475 We increment var _after_ the loop exit check because we might otherwise
3476 fail if upper == TYPE_MAX_VALUE (type for upper).
3478 Note that we never have to deal with SAVE_EXPRs here, because this has
3479 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3481 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3482 gimple_seq *, bool);
3484 static void
3485 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3486 tree value, tree array_elt_type,
3487 gimple_seq *pre_p, bool cleared)
3489 tree loop_entry_label, loop_exit_label, fall_thru_label;
3490 tree var, var_type, cref, tmp;
3492 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3493 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3494 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3496 /* Create and initialize the index variable. */
3497 var_type = TREE_TYPE (upper);
3498 var = create_tmp_var (var_type);
3499 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3501 /* Add the loop entry label. */
3502 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3504 /* Build the reference. */
3505 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3506 var, NULL_TREE, NULL_TREE);
3508 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3509 the store. Otherwise just assign value to the reference. */
3511 if (TREE_CODE (value) == CONSTRUCTOR)
3512 /* NB we might have to call ourself recursively through
3513 gimplify_init_ctor_eval if the value is a constructor. */
3514 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3515 pre_p, cleared);
3516 else
3517 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3519 /* We exit the loop when the index var is equal to the upper bound. */
3520 gimplify_seq_add_stmt (pre_p,
3521 gimple_build_cond (EQ_EXPR, var, upper,
3522 loop_exit_label, fall_thru_label));
3524 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3526 /* Otherwise, increment the index var... */
3527 tmp = build2 (PLUS_EXPR, var_type, var,
3528 fold_convert (var_type, integer_one_node));
3529 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3531 /* ...and jump back to the loop entry. */
3532 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3534 /* Add the loop exit label. */
3535 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3538 /* Return true if FDECL is accessing a field that is zero sized. */
3540 static bool
3541 zero_sized_field_decl (const_tree fdecl)
3543 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3544 && integer_zerop (DECL_SIZE (fdecl)))
3545 return true;
3546 return false;
3549 /* Return true if TYPE is zero sized. */
3551 static bool
3552 zero_sized_type (const_tree type)
3554 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3555 && integer_zerop (TYPE_SIZE (type)))
3556 return true;
3557 return false;
3560 /* A subroutine of gimplify_init_constructor. Generate individual
3561 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3562 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3563 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3564 zeroed first. */
3566 static void
3567 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3568 gimple_seq *pre_p, bool cleared)
3570 tree array_elt_type = NULL;
3571 unsigned HOST_WIDE_INT ix;
3572 tree purpose, value;
3574 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3575 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3577 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3579 tree cref;
3581 /* NULL values are created above for gimplification errors. */
3582 if (value == NULL)
3583 continue;
3585 if (cleared && initializer_zerop (value))
3586 continue;
3588 /* ??? Here's to hoping the front end fills in all of the indices,
3589 so we don't have to figure out what's missing ourselves. */
3590 gcc_assert (purpose);
3592 /* Skip zero-sized fields, unless value has side-effects. This can
3593 happen with calls to functions returning a zero-sized type, which
3594 we shouldn't discard. As a number of downstream passes don't
3595 expect sets of zero-sized fields, we rely on the gimplification of
3596 the MODIFY_EXPR we make below to drop the assignment statement. */
3597 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3598 continue;
3600 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3601 whole range. */
3602 if (TREE_CODE (purpose) == RANGE_EXPR)
3604 tree lower = TREE_OPERAND (purpose, 0);
3605 tree upper = TREE_OPERAND (purpose, 1);
3607 /* If the lower bound is equal to upper, just treat it as if
3608 upper was the index. */
3609 if (simple_cst_equal (lower, upper))
3610 purpose = upper;
3611 else
3613 gimplify_init_ctor_eval_range (object, lower, upper, value,
3614 array_elt_type, pre_p, cleared);
3615 continue;
3619 if (array_elt_type)
3621 /* Do not use bitsizetype for ARRAY_REF indices. */
3622 if (TYPE_DOMAIN (TREE_TYPE (object)))
3623 purpose
3624 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3625 purpose);
3626 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3627 purpose, NULL_TREE, NULL_TREE);
3629 else
3631 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3632 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3633 unshare_expr (object), purpose, NULL_TREE);
3636 if (TREE_CODE (value) == CONSTRUCTOR
3637 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3638 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3639 pre_p, cleared);
3640 else
3642 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3643 gimplify_and_add (init, pre_p);
3644 ggc_free (init);
3649 /* Return the appropriate RHS predicate for this LHS. */
3651 gimple_predicate
3652 rhs_predicate_for (tree lhs)
3654 if (is_gimple_reg (lhs))
3655 return is_gimple_reg_rhs_or_call;
3656 else
3657 return is_gimple_mem_rhs_or_call;
3660 /* Gimplify a C99 compound literal expression. This just means adding
3661 the DECL_EXPR before the current statement and using its anonymous
3662 decl instead. */
3664 static enum gimplify_status
3665 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3666 bool (*gimple_test_f) (tree),
3667 fallback_t fallback)
3669 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3670 tree decl = DECL_EXPR_DECL (decl_s);
3671 tree init = DECL_INITIAL (decl);
3672 /* Mark the decl as addressable if the compound literal
3673 expression is addressable now, otherwise it is marked too late
3674 after we gimplify the initialization expression. */
3675 if (TREE_ADDRESSABLE (*expr_p))
3676 TREE_ADDRESSABLE (decl) = 1;
3677 /* Otherwise, if we don't need an lvalue and have a literal directly
3678 substitute it. Check if it matches the gimple predicate, as
3679 otherwise we'd generate a new temporary, and we can as well just
3680 use the decl we already have. */
3681 else if (!TREE_ADDRESSABLE (decl)
3682 && init
3683 && (fallback & fb_lvalue) == 0
3684 && gimple_test_f (init))
3686 *expr_p = init;
3687 return GS_OK;
3690 /* Preliminarily mark non-addressed complex variables as eligible
3691 for promotion to gimple registers. We'll transform their uses
3692 as we find them. */
3693 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3694 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3695 && !TREE_THIS_VOLATILE (decl)
3696 && !needs_to_live_in_memory (decl))
3697 DECL_GIMPLE_REG_P (decl) = 1;
3699 /* If the decl is not addressable, then it is being used in some
3700 expression or on the right hand side of a statement, and it can
3701 be put into a readonly data section. */
3702 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3703 TREE_READONLY (decl) = 1;
3705 /* This decl isn't mentioned in the enclosing block, so add it to the
3706 list of temps. FIXME it seems a bit of a kludge to say that
3707 anonymous artificial vars aren't pushed, but everything else is. */
3708 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3709 gimple_add_tmp_var (decl);
3711 gimplify_and_add (decl_s, pre_p);
3712 *expr_p = decl;
3713 return GS_OK;
3716 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3717 return a new CONSTRUCTOR if something changed. */
3719 static tree
3720 optimize_compound_literals_in_ctor (tree orig_ctor)
3722 tree ctor = orig_ctor;
3723 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3724 unsigned int idx, num = vec_safe_length (elts);
3726 for (idx = 0; idx < num; idx++)
3728 tree value = (*elts)[idx].value;
3729 tree newval = value;
3730 if (TREE_CODE (value) == CONSTRUCTOR)
3731 newval = optimize_compound_literals_in_ctor (value);
3732 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3734 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3735 tree decl = DECL_EXPR_DECL (decl_s);
3736 tree init = DECL_INITIAL (decl);
3738 if (!TREE_ADDRESSABLE (value)
3739 && !TREE_ADDRESSABLE (decl)
3740 && init
3741 && TREE_CODE (init) == CONSTRUCTOR)
3742 newval = optimize_compound_literals_in_ctor (init);
3744 if (newval == value)
3745 continue;
3747 if (ctor == orig_ctor)
3749 ctor = copy_node (orig_ctor);
3750 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3751 elts = CONSTRUCTOR_ELTS (ctor);
3753 (*elts)[idx].value = newval;
3755 return ctor;
3758 /* A subroutine of gimplify_modify_expr. Break out elements of a
3759 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3761 Note that we still need to clear any elements that don't have explicit
3762 initializers, so if not all elements are initialized we keep the
3763 original MODIFY_EXPR, we just remove all of the constructor elements.
3765 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3766 GS_ERROR if we would have to create a temporary when gimplifying
3767 this constructor. Otherwise, return GS_OK.
3769 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3771 static enum gimplify_status
3772 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3773 bool want_value, bool notify_temp_creation)
3775 tree object, ctor, type;
3776 enum gimplify_status ret;
3777 vec<constructor_elt, va_gc> *elts;
3779 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3781 if (!notify_temp_creation)
3783 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3784 is_gimple_lvalue, fb_lvalue);
3785 if (ret == GS_ERROR)
3786 return ret;
3789 object = TREE_OPERAND (*expr_p, 0);
3790 ctor = TREE_OPERAND (*expr_p, 1) =
3791 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3792 type = TREE_TYPE (ctor);
3793 elts = CONSTRUCTOR_ELTS (ctor);
3794 ret = GS_ALL_DONE;
3796 switch (TREE_CODE (type))
3798 case RECORD_TYPE:
3799 case UNION_TYPE:
3800 case QUAL_UNION_TYPE:
3801 case ARRAY_TYPE:
3803 struct gimplify_init_ctor_preeval_data preeval_data;
3804 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3805 bool cleared, complete_p, valid_const_initializer;
3807 /* Aggregate types must lower constructors to initialization of
3808 individual elements. The exception is that a CONSTRUCTOR node
3809 with no elements indicates zero-initialization of the whole. */
3810 if (vec_safe_is_empty (elts))
3812 if (notify_temp_creation)
3813 return GS_OK;
3814 break;
3817 /* Fetch information about the constructor to direct later processing.
3818 We might want to make static versions of it in various cases, and
3819 can only do so if it known to be a valid constant initializer. */
3820 valid_const_initializer
3821 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3822 &num_ctor_elements, &complete_p);
3824 /* If a const aggregate variable is being initialized, then it
3825 should never be a lose to promote the variable to be static. */
3826 if (valid_const_initializer
3827 && num_nonzero_elements > 1
3828 && TREE_READONLY (object)
3829 && TREE_CODE (object) == VAR_DECL
3830 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3832 if (notify_temp_creation)
3833 return GS_ERROR;
3834 DECL_INITIAL (object) = ctor;
3835 TREE_STATIC (object) = 1;
3836 if (!DECL_NAME (object))
3837 DECL_NAME (object) = create_tmp_var_name ("C");
3838 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3840 /* ??? C++ doesn't automatically append a .<number> to the
3841 assembler name, and even when it does, it looks at FE private
3842 data structures to figure out what that number should be,
3843 which are not set for this variable. I suppose this is
3844 important for local statics for inline functions, which aren't
3845 "local" in the object file sense. So in order to get a unique
3846 TU-local symbol, we must invoke the lhd version now. */
3847 lhd_set_decl_assembler_name (object);
3849 *expr_p = NULL_TREE;
3850 break;
3853 /* If there are "lots" of initialized elements, even discounting
3854 those that are not address constants (and thus *must* be
3855 computed at runtime), then partition the constructor into
3856 constant and non-constant parts. Block copy the constant
3857 parts in, then generate code for the non-constant parts. */
3858 /* TODO. There's code in cp/typeck.c to do this. */
3860 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3861 /* store_constructor will ignore the clearing of variable-sized
3862 objects. Initializers for such objects must explicitly set
3863 every field that needs to be set. */
3864 cleared = false;
3865 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3866 /* If the constructor isn't complete, clear the whole object
3867 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3869 ??? This ought not to be needed. For any element not present
3870 in the initializer, we should simply set them to zero. Except
3871 we'd need to *find* the elements that are not present, and that
3872 requires trickery to avoid quadratic compile-time behavior in
3873 large cases or excessive memory use in small cases. */
3874 cleared = true;
3875 else if (num_ctor_elements - num_nonzero_elements
3876 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3877 && num_nonzero_elements < num_ctor_elements / 4)
3878 /* If there are "lots" of zeros, it's more efficient to clear
3879 the memory and then set the nonzero elements. */
3880 cleared = true;
3881 else
3882 cleared = false;
3884 /* If there are "lots" of initialized elements, and all of them
3885 are valid address constants, then the entire initializer can
3886 be dropped to memory, and then memcpy'd out. Don't do this
3887 for sparse arrays, though, as it's more efficient to follow
3888 the standard CONSTRUCTOR behavior of memset followed by
3889 individual element initialization. Also don't do this for small
3890 all-zero initializers (which aren't big enough to merit
3891 clearing), and don't try to make bitwise copies of
3892 TREE_ADDRESSABLE types.
3894 We cannot apply such transformation when compiling chkp static
3895 initializer because creation of initializer image in the memory
3896 will require static initialization of bounds for it. It should
3897 result in another gimplification of similar initializer and we
3898 may fall into infinite loop. */
3899 if (valid_const_initializer
3900 && !(cleared || num_nonzero_elements == 0)
3901 && !TREE_ADDRESSABLE (type)
3902 && (!current_function_decl
3903 || !lookup_attribute ("chkp ctor",
3904 DECL_ATTRIBUTES (current_function_decl))))
3906 HOST_WIDE_INT size = int_size_in_bytes (type);
3907 unsigned int align;
3909 /* ??? We can still get unbounded array types, at least
3910 from the C++ front end. This seems wrong, but attempt
3911 to work around it for now. */
3912 if (size < 0)
3914 size = int_size_in_bytes (TREE_TYPE (object));
3915 if (size >= 0)
3916 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3919 /* Find the maximum alignment we can assume for the object. */
3920 /* ??? Make use of DECL_OFFSET_ALIGN. */
3921 if (DECL_P (object))
3922 align = DECL_ALIGN (object);
3923 else
3924 align = TYPE_ALIGN (type);
3926 /* Do a block move either if the size is so small as to make
3927 each individual move a sub-unit move on average, or if it
3928 is so large as to make individual moves inefficient. */
3929 if (size > 0
3930 && num_nonzero_elements > 1
3931 && (size < num_nonzero_elements
3932 || !can_move_by_pieces (size, align)))
3934 if (notify_temp_creation)
3935 return GS_ERROR;
3937 walk_tree (&ctor, force_labels_r, NULL, NULL);
3938 ctor = tree_output_constant_def (ctor);
3939 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3940 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3941 TREE_OPERAND (*expr_p, 1) = ctor;
3943 /* This is no longer an assignment of a CONSTRUCTOR, but
3944 we still may have processing to do on the LHS. So
3945 pretend we didn't do anything here to let that happen. */
3946 return GS_UNHANDLED;
3950 /* If the target is volatile, we have non-zero elements and more than
3951 one field to assign, initialize the target from a temporary. */
3952 if (TREE_THIS_VOLATILE (object)
3953 && !TREE_ADDRESSABLE (type)
3954 && num_nonzero_elements > 0
3955 && vec_safe_length (elts) > 1)
3957 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
3958 TREE_OPERAND (*expr_p, 0) = temp;
3959 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3960 *expr_p,
3961 build2 (MODIFY_EXPR, void_type_node,
3962 object, temp));
3963 return GS_OK;
3966 if (notify_temp_creation)
3967 return GS_OK;
3969 /* If there are nonzero elements and if needed, pre-evaluate to capture
3970 elements overlapping with the lhs into temporaries. We must do this
3971 before clearing to fetch the values before they are zeroed-out. */
3972 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3974 preeval_data.lhs_base_decl = get_base_address (object);
3975 if (!DECL_P (preeval_data.lhs_base_decl))
3976 preeval_data.lhs_base_decl = NULL;
3977 preeval_data.lhs_alias_set = get_alias_set (object);
3979 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3980 pre_p, post_p, &preeval_data);
3983 if (cleared)
3985 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3986 Note that we still have to gimplify, in order to handle the
3987 case of variable sized types. Avoid shared tree structures. */
3988 CONSTRUCTOR_ELTS (ctor) = NULL;
3989 TREE_SIDE_EFFECTS (ctor) = 0;
3990 object = unshare_expr (object);
3991 gimplify_stmt (expr_p, pre_p);
3994 /* If we have not block cleared the object, or if there are nonzero
3995 elements in the constructor, add assignments to the individual
3996 scalar fields of the object. */
3997 if (!cleared || num_nonzero_elements > 0)
3998 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4000 *expr_p = NULL_TREE;
4002 break;
4004 case COMPLEX_TYPE:
4006 tree r, i;
4008 if (notify_temp_creation)
4009 return GS_OK;
4011 /* Extract the real and imaginary parts out of the ctor. */
4012 gcc_assert (elts->length () == 2);
4013 r = (*elts)[0].value;
4014 i = (*elts)[1].value;
4015 if (r == NULL || i == NULL)
4017 tree zero = build_zero_cst (TREE_TYPE (type));
4018 if (r == NULL)
4019 r = zero;
4020 if (i == NULL)
4021 i = zero;
4024 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4025 represent creation of a complex value. */
4026 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4028 ctor = build_complex (type, r, i);
4029 TREE_OPERAND (*expr_p, 1) = ctor;
4031 else
4033 ctor = build2 (COMPLEX_EXPR, type, r, i);
4034 TREE_OPERAND (*expr_p, 1) = ctor;
4035 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4036 pre_p,
4037 post_p,
4038 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4039 fb_rvalue);
4042 break;
4044 case VECTOR_TYPE:
4046 unsigned HOST_WIDE_INT ix;
4047 constructor_elt *ce;
4049 if (notify_temp_creation)
4050 return GS_OK;
4052 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4053 if (TREE_CONSTANT (ctor))
4055 bool constant_p = true;
4056 tree value;
4058 /* Even when ctor is constant, it might contain non-*_CST
4059 elements, such as addresses or trapping values like
4060 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4061 in VECTOR_CST nodes. */
4062 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4063 if (!CONSTANT_CLASS_P (value))
4065 constant_p = false;
4066 break;
4069 if (constant_p)
4071 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4072 break;
4075 TREE_CONSTANT (ctor) = 0;
4078 /* Vector types use CONSTRUCTOR all the way through gimple
4079 compilation as a general initializer. */
4080 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4082 enum gimplify_status tret;
4083 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4084 fb_rvalue);
4085 if (tret == GS_ERROR)
4086 ret = GS_ERROR;
4088 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4089 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4091 break;
4093 default:
4094 /* So how did we get a CONSTRUCTOR for a scalar type? */
4095 gcc_unreachable ();
4098 if (ret == GS_ERROR)
4099 return GS_ERROR;
4100 else if (want_value)
4102 *expr_p = object;
4103 return GS_OK;
4105 else
4107 /* If we have gimplified both sides of the initializer but have
4108 not emitted an assignment, do so now. */
4109 if (*expr_p)
4111 tree lhs = TREE_OPERAND (*expr_p, 0);
4112 tree rhs = TREE_OPERAND (*expr_p, 1);
4113 gassign *init = gimple_build_assign (lhs, rhs);
4114 gimplify_seq_add_stmt (pre_p, init);
4115 *expr_p = NULL;
4118 return GS_ALL_DONE;
4122 /* Given a pointer value OP0, return a simplified version of an
4123 indirection through OP0, or NULL_TREE if no simplification is
4124 possible. This may only be applied to a rhs of an expression.
4125 Note that the resulting type may be different from the type pointed
4126 to in the sense that it is still compatible from the langhooks
4127 point of view. */
4129 static tree
4130 gimple_fold_indirect_ref_rhs (tree t)
4132 return gimple_fold_indirect_ref (t);
4135 /* Subroutine of gimplify_modify_expr to do simplifications of
4136 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4137 something changes. */
4139 static enum gimplify_status
4140 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4141 gimple_seq *pre_p, gimple_seq *post_p,
4142 bool want_value)
4144 enum gimplify_status ret = GS_UNHANDLED;
4145 bool changed;
4149 changed = false;
4150 switch (TREE_CODE (*from_p))
4152 case VAR_DECL:
4153 /* If we're assigning from a read-only variable initialized with
4154 a constructor, do the direct assignment from the constructor,
4155 but only if neither source nor target are volatile since this
4156 latter assignment might end up being done on a per-field basis. */
4157 if (DECL_INITIAL (*from_p)
4158 && TREE_READONLY (*from_p)
4159 && !TREE_THIS_VOLATILE (*from_p)
4160 && !TREE_THIS_VOLATILE (*to_p)
4161 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4163 tree old_from = *from_p;
4164 enum gimplify_status subret;
4166 /* Move the constructor into the RHS. */
4167 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4169 /* Let's see if gimplify_init_constructor will need to put
4170 it in memory. */
4171 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4172 false, true);
4173 if (subret == GS_ERROR)
4175 /* If so, revert the change. */
4176 *from_p = old_from;
4178 else
4180 ret = GS_OK;
4181 changed = true;
4184 break;
4185 case INDIRECT_REF:
4187 /* If we have code like
4189 *(const A*)(A*)&x
4191 where the type of "x" is a (possibly cv-qualified variant
4192 of "A"), treat the entire expression as identical to "x".
4193 This kind of code arises in C++ when an object is bound
4194 to a const reference, and if "x" is a TARGET_EXPR we want
4195 to take advantage of the optimization below. */
4196 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4197 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4198 if (t)
4200 if (TREE_THIS_VOLATILE (t) != volatile_p)
4202 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4203 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4204 build_fold_addr_expr (t));
4205 if (REFERENCE_CLASS_P (t))
4206 TREE_THIS_VOLATILE (t) = volatile_p;
4208 *from_p = t;
4209 ret = GS_OK;
4210 changed = true;
4212 break;
4215 case TARGET_EXPR:
4217 /* If we are initializing something from a TARGET_EXPR, strip the
4218 TARGET_EXPR and initialize it directly, if possible. This can't
4219 be done if the initializer is void, since that implies that the
4220 temporary is set in some non-trivial way.
4222 ??? What about code that pulls out the temp and uses it
4223 elsewhere? I think that such code never uses the TARGET_EXPR as
4224 an initializer. If I'm wrong, we'll die because the temp won't
4225 have any RTL. In that case, I guess we'll need to replace
4226 references somehow. */
4227 tree init = TARGET_EXPR_INITIAL (*from_p);
4229 if (init
4230 && !VOID_TYPE_P (TREE_TYPE (init)))
4232 *from_p = init;
4233 ret = GS_OK;
4234 changed = true;
4237 break;
4239 case COMPOUND_EXPR:
4240 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4241 caught. */
4242 gimplify_compound_expr (from_p, pre_p, true);
4243 ret = GS_OK;
4244 changed = true;
4245 break;
4247 case CONSTRUCTOR:
4248 /* If we already made some changes, let the front end have a
4249 crack at this before we break it down. */
4250 if (ret != GS_UNHANDLED)
4251 break;
4252 /* If we're initializing from a CONSTRUCTOR, break this into
4253 individual MODIFY_EXPRs. */
4254 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4255 false);
4257 case COND_EXPR:
4258 /* If we're assigning to a non-register type, push the assignment
4259 down into the branches. This is mandatory for ADDRESSABLE types,
4260 since we cannot generate temporaries for such, but it saves a
4261 copy in other cases as well. */
4262 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4264 /* This code should mirror the code in gimplify_cond_expr. */
4265 enum tree_code code = TREE_CODE (*expr_p);
4266 tree cond = *from_p;
4267 tree result = *to_p;
4269 ret = gimplify_expr (&result, pre_p, post_p,
4270 is_gimple_lvalue, fb_lvalue);
4271 if (ret != GS_ERROR)
4272 ret = GS_OK;
4274 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4275 TREE_OPERAND (cond, 1)
4276 = build2 (code, void_type_node, result,
4277 TREE_OPERAND (cond, 1));
4278 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4279 TREE_OPERAND (cond, 2)
4280 = build2 (code, void_type_node, unshare_expr (result),
4281 TREE_OPERAND (cond, 2));
4283 TREE_TYPE (cond) = void_type_node;
4284 recalculate_side_effects (cond);
4286 if (want_value)
4288 gimplify_and_add (cond, pre_p);
4289 *expr_p = unshare_expr (result);
4291 else
4292 *expr_p = cond;
4293 return ret;
4295 break;
4297 case CALL_EXPR:
4298 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4299 return slot so that we don't generate a temporary. */
4300 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4301 && aggregate_value_p (*from_p, *from_p))
4303 bool use_target;
4305 if (!(rhs_predicate_for (*to_p))(*from_p))
4306 /* If we need a temporary, *to_p isn't accurate. */
4307 use_target = false;
4308 /* It's OK to use the return slot directly unless it's an NRV. */
4309 else if (TREE_CODE (*to_p) == RESULT_DECL
4310 && DECL_NAME (*to_p) == NULL_TREE
4311 && needs_to_live_in_memory (*to_p))
4312 use_target = true;
4313 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4314 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4315 /* Don't force regs into memory. */
4316 use_target = false;
4317 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4318 /* It's OK to use the target directly if it's being
4319 initialized. */
4320 use_target = true;
4321 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4322 /* Always use the target and thus RSO for variable-sized types.
4323 GIMPLE cannot deal with a variable-sized assignment
4324 embedded in a call statement. */
4325 use_target = true;
4326 else if (TREE_CODE (*to_p) != SSA_NAME
4327 && (!is_gimple_variable (*to_p)
4328 || needs_to_live_in_memory (*to_p)))
4329 /* Don't use the original target if it's already addressable;
4330 if its address escapes, and the called function uses the
4331 NRV optimization, a conforming program could see *to_p
4332 change before the called function returns; see c++/19317.
4333 When optimizing, the return_slot pass marks more functions
4334 as safe after we have escape info. */
4335 use_target = false;
4336 else
4337 use_target = true;
4339 if (use_target)
4341 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4342 mark_addressable (*to_p);
4345 break;
4347 case WITH_SIZE_EXPR:
4348 /* Likewise for calls that return an aggregate of non-constant size,
4349 since we would not be able to generate a temporary at all. */
4350 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4352 *from_p = TREE_OPERAND (*from_p, 0);
4353 /* We don't change ret in this case because the
4354 WITH_SIZE_EXPR might have been added in
4355 gimplify_modify_expr, so returning GS_OK would lead to an
4356 infinite loop. */
4357 changed = true;
4359 break;
4361 /* If we're initializing from a container, push the initialization
4362 inside it. */
4363 case CLEANUP_POINT_EXPR:
4364 case BIND_EXPR:
4365 case STATEMENT_LIST:
4367 tree wrap = *from_p;
4368 tree t;
4370 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4371 fb_lvalue);
4372 if (ret != GS_ERROR)
4373 ret = GS_OK;
4375 t = voidify_wrapper_expr (wrap, *expr_p);
4376 gcc_assert (t == *expr_p);
4378 if (want_value)
4380 gimplify_and_add (wrap, pre_p);
4381 *expr_p = unshare_expr (*to_p);
4383 else
4384 *expr_p = wrap;
4385 return GS_OK;
4388 case COMPOUND_LITERAL_EXPR:
4390 tree complit = TREE_OPERAND (*expr_p, 1);
4391 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4392 tree decl = DECL_EXPR_DECL (decl_s);
4393 tree init = DECL_INITIAL (decl);
4395 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4396 into struct T x = { 0, 1, 2 } if the address of the
4397 compound literal has never been taken. */
4398 if (!TREE_ADDRESSABLE (complit)
4399 && !TREE_ADDRESSABLE (decl)
4400 && init)
4402 *expr_p = copy_node (*expr_p);
4403 TREE_OPERAND (*expr_p, 1) = init;
4404 return GS_OK;
4408 default:
4409 break;
4412 while (changed);
4414 return ret;
4418 /* Return true if T looks like a valid GIMPLE statement. */
4420 static bool
4421 is_gimple_stmt (tree t)
4423 const enum tree_code code = TREE_CODE (t);
4425 switch (code)
4427 case NOP_EXPR:
4428 /* The only valid NOP_EXPR is the empty statement. */
4429 return IS_EMPTY_STMT (t);
4431 case BIND_EXPR:
4432 case COND_EXPR:
4433 /* These are only valid if they're void. */
4434 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4436 case SWITCH_EXPR:
4437 case GOTO_EXPR:
4438 case RETURN_EXPR:
4439 case LABEL_EXPR:
4440 case CASE_LABEL_EXPR:
4441 case TRY_CATCH_EXPR:
4442 case TRY_FINALLY_EXPR:
4443 case EH_FILTER_EXPR:
4444 case CATCH_EXPR:
4445 case ASM_EXPR:
4446 case STATEMENT_LIST:
4447 case OACC_PARALLEL:
4448 case OACC_KERNELS:
4449 case OACC_DATA:
4450 case OACC_HOST_DATA:
4451 case OACC_DECLARE:
4452 case OACC_UPDATE:
4453 case OACC_ENTER_DATA:
4454 case OACC_EXIT_DATA:
4455 case OACC_CACHE:
4456 case OMP_PARALLEL:
4457 case OMP_FOR:
4458 case OMP_SIMD:
4459 case CILK_SIMD:
4460 case OMP_DISTRIBUTE:
4461 case OACC_LOOP:
4462 case OMP_SECTIONS:
4463 case OMP_SECTION:
4464 case OMP_SINGLE:
4465 case OMP_MASTER:
4466 case OMP_TASKGROUP:
4467 case OMP_ORDERED:
4468 case OMP_CRITICAL:
4469 case OMP_TASK:
4470 /* These are always void. */
4471 return true;
4473 case CALL_EXPR:
4474 case MODIFY_EXPR:
4475 case PREDICT_EXPR:
4476 /* These are valid regardless of their type. */
4477 return true;
4479 default:
4480 return false;
4485 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4486 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4487 DECL_GIMPLE_REG_P set.
4489 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4490 other, unmodified part of the complex object just before the total store.
4491 As a consequence, if the object is still uninitialized, an undefined value
4492 will be loaded into a register, which may result in a spurious exception
4493 if the register is floating-point and the value happens to be a signaling
4494 NaN for example. Then the fully-fledged complex operations lowering pass
4495 followed by a DCE pass are necessary in order to fix things up. */
4497 static enum gimplify_status
4498 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4499 bool want_value)
4501 enum tree_code code, ocode;
4502 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4504 lhs = TREE_OPERAND (*expr_p, 0);
4505 rhs = TREE_OPERAND (*expr_p, 1);
4506 code = TREE_CODE (lhs);
4507 lhs = TREE_OPERAND (lhs, 0);
4509 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4510 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4511 TREE_NO_WARNING (other) = 1;
4512 other = get_formal_tmp_var (other, pre_p);
4514 realpart = code == REALPART_EXPR ? rhs : other;
4515 imagpart = code == REALPART_EXPR ? other : rhs;
4517 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4518 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4519 else
4520 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4522 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4523 *expr_p = (want_value) ? rhs : NULL_TREE;
4525 return GS_ALL_DONE;
4528 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4530 modify_expr
4531 : varname '=' rhs
4532 | '*' ID '=' rhs
4534 PRE_P points to the list where side effects that must happen before
4535 *EXPR_P should be stored.
4537 POST_P points to the list where side effects that must happen after
4538 *EXPR_P should be stored.
4540 WANT_VALUE is nonzero iff we want to use the value of this expression
4541 in another expression. */
4543 static enum gimplify_status
4544 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4545 bool want_value)
4547 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4548 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4549 enum gimplify_status ret = GS_UNHANDLED;
4550 gimple assign;
4551 location_t loc = EXPR_LOCATION (*expr_p);
4552 gimple_stmt_iterator gsi;
4554 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4555 || TREE_CODE (*expr_p) == INIT_EXPR);
4557 /* Trying to simplify a clobber using normal logic doesn't work,
4558 so handle it here. */
4559 if (TREE_CLOBBER_P (*from_p))
4561 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4562 if (ret == GS_ERROR)
4563 return ret;
4564 gcc_assert (!want_value
4565 && (TREE_CODE (*to_p) == VAR_DECL
4566 || TREE_CODE (*to_p) == MEM_REF));
4567 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4568 *expr_p = NULL;
4569 return GS_ALL_DONE;
4572 /* Insert pointer conversions required by the middle-end that are not
4573 required by the frontend. This fixes middle-end type checking for
4574 for example gcc.dg/redecl-6.c. */
4575 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4577 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4578 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4579 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4582 /* See if any simplifications can be done based on what the RHS is. */
4583 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4584 want_value);
4585 if (ret != GS_UNHANDLED)
4586 return ret;
4588 /* For zero sized types only gimplify the left hand side and right hand
4589 side as statements and throw away the assignment. Do this after
4590 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4591 types properly. */
4592 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4594 gimplify_stmt (from_p, pre_p);
4595 gimplify_stmt (to_p, pre_p);
4596 *expr_p = NULL_TREE;
4597 return GS_ALL_DONE;
4600 /* If the value being copied is of variable width, compute the length
4601 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4602 before gimplifying any of the operands so that we can resolve any
4603 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4604 the size of the expression to be copied, not of the destination, so
4605 that is what we must do here. */
4606 maybe_with_size_expr (from_p);
4608 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4609 if (ret == GS_ERROR)
4610 return ret;
4612 /* As a special case, we have to temporarily allow for assignments
4613 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4614 a toplevel statement, when gimplifying the GENERIC expression
4615 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4616 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4618 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4619 prevent gimplify_expr from trying to create a new temporary for
4620 foo's LHS, we tell it that it should only gimplify until it
4621 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4622 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4623 and all we need to do here is set 'a' to be its LHS. */
4624 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4625 fb_rvalue);
4626 if (ret == GS_ERROR)
4627 return ret;
4629 /* Now see if the above changed *from_p to something we handle specially. */
4630 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4631 want_value);
4632 if (ret != GS_UNHANDLED)
4633 return ret;
4635 /* If we've got a variable sized assignment between two lvalues (i.e. does
4636 not involve a call), then we can make things a bit more straightforward
4637 by converting the assignment to memcpy or memset. */
4638 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4640 tree from = TREE_OPERAND (*from_p, 0);
4641 tree size = TREE_OPERAND (*from_p, 1);
4643 if (TREE_CODE (from) == CONSTRUCTOR)
4644 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4646 if (is_gimple_addressable (from))
4648 *from_p = from;
4649 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4650 pre_p);
4654 /* Transform partial stores to non-addressable complex variables into
4655 total stores. This allows us to use real instead of virtual operands
4656 for these variables, which improves optimization. */
4657 if ((TREE_CODE (*to_p) == REALPART_EXPR
4658 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4659 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4660 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4662 /* Try to alleviate the effects of the gimplification creating artificial
4663 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4664 if (!gimplify_ctxp->into_ssa
4665 && TREE_CODE (*from_p) == VAR_DECL
4666 && DECL_IGNORED_P (*from_p)
4667 && DECL_P (*to_p)
4668 && !DECL_IGNORED_P (*to_p))
4670 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4671 DECL_NAME (*from_p)
4672 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4673 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4674 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4677 if (want_value && TREE_THIS_VOLATILE (*to_p))
4678 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4680 if (TREE_CODE (*from_p) == CALL_EXPR)
4682 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4683 instead of a GIMPLE_ASSIGN. */
4684 gcall *call_stmt;
4685 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
4687 /* Gimplify internal functions created in the FEs. */
4688 int nargs = call_expr_nargs (*from_p), i;
4689 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
4690 auto_vec<tree> vargs (nargs);
4692 for (i = 0; i < nargs; i++)
4694 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
4695 EXPR_LOCATION (*from_p));
4696 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
4698 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
4699 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
4701 else
4703 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4704 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4705 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4706 tree fndecl = get_callee_fndecl (*from_p);
4707 if (fndecl
4708 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4709 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4710 && call_expr_nargs (*from_p) == 3)
4711 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4712 CALL_EXPR_ARG (*from_p, 0),
4713 CALL_EXPR_ARG (*from_p, 1),
4714 CALL_EXPR_ARG (*from_p, 2));
4715 else
4717 call_stmt = gimple_build_call_from_tree (*from_p);
4718 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
4721 notice_special_calls (call_stmt);
4722 if (!gimple_call_noreturn_p (call_stmt))
4723 gimple_call_set_lhs (call_stmt, *to_p);
4724 assign = call_stmt;
4726 else
4728 assign = gimple_build_assign (*to_p, *from_p);
4729 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4732 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4734 /* We should have got an SSA name from the start. */
4735 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4738 gimplify_seq_add_stmt (pre_p, assign);
4739 gsi = gsi_last (*pre_p);
4740 maybe_fold_stmt (&gsi);
4742 if (want_value)
4744 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4745 return GS_OK;
4747 else
4748 *expr_p = NULL;
4750 return GS_ALL_DONE;
4753 /* Gimplify a comparison between two variable-sized objects. Do this
4754 with a call to BUILT_IN_MEMCMP. */
4756 static enum gimplify_status
4757 gimplify_variable_sized_compare (tree *expr_p)
4759 location_t loc = EXPR_LOCATION (*expr_p);
4760 tree op0 = TREE_OPERAND (*expr_p, 0);
4761 tree op1 = TREE_OPERAND (*expr_p, 1);
4762 tree t, arg, dest, src, expr;
4764 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4765 arg = unshare_expr (arg);
4766 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4767 src = build_fold_addr_expr_loc (loc, op1);
4768 dest = build_fold_addr_expr_loc (loc, op0);
4769 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4770 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4772 expr
4773 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4774 SET_EXPR_LOCATION (expr, loc);
4775 *expr_p = expr;
4777 return GS_OK;
4780 /* Gimplify a comparison between two aggregate objects of integral scalar
4781 mode as a comparison between the bitwise equivalent scalar values. */
4783 static enum gimplify_status
4784 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4786 location_t loc = EXPR_LOCATION (*expr_p);
4787 tree op0 = TREE_OPERAND (*expr_p, 0);
4788 tree op1 = TREE_OPERAND (*expr_p, 1);
4790 tree type = TREE_TYPE (op0);
4791 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4793 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4794 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4796 *expr_p
4797 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4799 return GS_OK;
4802 /* Gimplify an expression sequence. This function gimplifies each
4803 expression and rewrites the original expression with the last
4804 expression of the sequence in GIMPLE form.
4806 PRE_P points to the list where the side effects for all the
4807 expressions in the sequence will be emitted.
4809 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4811 static enum gimplify_status
4812 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4814 tree t = *expr_p;
4818 tree *sub_p = &TREE_OPERAND (t, 0);
4820 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4821 gimplify_compound_expr (sub_p, pre_p, false);
4822 else
4823 gimplify_stmt (sub_p, pre_p);
4825 t = TREE_OPERAND (t, 1);
4827 while (TREE_CODE (t) == COMPOUND_EXPR);
4829 *expr_p = t;
4830 if (want_value)
4831 return GS_OK;
4832 else
4834 gimplify_stmt (expr_p, pre_p);
4835 return GS_ALL_DONE;
4839 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4840 gimplify. After gimplification, EXPR_P will point to a new temporary
4841 that holds the original value of the SAVE_EXPR node.
4843 PRE_P points to the list where side effects that must happen before
4844 *EXPR_P should be stored. */
4846 static enum gimplify_status
4847 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4849 enum gimplify_status ret = GS_ALL_DONE;
4850 tree val;
4852 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4853 val = TREE_OPERAND (*expr_p, 0);
4855 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4856 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4858 /* The operand may be a void-valued expression such as SAVE_EXPRs
4859 generated by the Java frontend for class initialization. It is
4860 being executed only for its side-effects. */
4861 if (TREE_TYPE (val) == void_type_node)
4863 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4864 is_gimple_stmt, fb_none);
4865 val = NULL;
4867 else
4868 val = get_initialized_tmp_var (val, pre_p, post_p);
4870 TREE_OPERAND (*expr_p, 0) = val;
4871 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4874 *expr_p = val;
4876 return ret;
4879 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4881 unary_expr
4882 : ...
4883 | '&' varname
4886 PRE_P points to the list where side effects that must happen before
4887 *EXPR_P should be stored.
4889 POST_P points to the list where side effects that must happen after
4890 *EXPR_P should be stored. */
4892 static enum gimplify_status
4893 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4895 tree expr = *expr_p;
4896 tree op0 = TREE_OPERAND (expr, 0);
4897 enum gimplify_status ret;
4898 location_t loc = EXPR_LOCATION (*expr_p);
4900 switch (TREE_CODE (op0))
4902 case INDIRECT_REF:
4903 do_indirect_ref:
4904 /* Check if we are dealing with an expression of the form '&*ptr'.
4905 While the front end folds away '&*ptr' into 'ptr', these
4906 expressions may be generated internally by the compiler (e.g.,
4907 builtins like __builtin_va_end). */
4908 /* Caution: the silent array decomposition semantics we allow for
4909 ADDR_EXPR means we can't always discard the pair. */
4910 /* Gimplification of the ADDR_EXPR operand may drop
4911 cv-qualification conversions, so make sure we add them if
4912 needed. */
4914 tree op00 = TREE_OPERAND (op0, 0);
4915 tree t_expr = TREE_TYPE (expr);
4916 tree t_op00 = TREE_TYPE (op00);
4918 if (!useless_type_conversion_p (t_expr, t_op00))
4919 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4920 *expr_p = op00;
4921 ret = GS_OK;
4923 break;
4925 case VIEW_CONVERT_EXPR:
4926 /* Take the address of our operand and then convert it to the type of
4927 this ADDR_EXPR.
4929 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4930 all clear. The impact of this transformation is even less clear. */
4932 /* If the operand is a useless conversion, look through it. Doing so
4933 guarantees that the ADDR_EXPR and its operand will remain of the
4934 same type. */
4935 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4936 op0 = TREE_OPERAND (op0, 0);
4938 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4939 build_fold_addr_expr_loc (loc,
4940 TREE_OPERAND (op0, 0)));
4941 ret = GS_OK;
4942 break;
4944 default:
4945 /* We use fb_either here because the C frontend sometimes takes
4946 the address of a call that returns a struct; see
4947 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4948 the implied temporary explicit. */
4950 /* Make the operand addressable. */
4951 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4952 is_gimple_addressable, fb_either);
4953 if (ret == GS_ERROR)
4954 break;
4956 /* Then mark it. Beware that it may not be possible to do so directly
4957 if a temporary has been created by the gimplification. */
4958 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4960 op0 = TREE_OPERAND (expr, 0);
4962 /* For various reasons, the gimplification of the expression
4963 may have made a new INDIRECT_REF. */
4964 if (TREE_CODE (op0) == INDIRECT_REF)
4965 goto do_indirect_ref;
4967 mark_addressable (TREE_OPERAND (expr, 0));
4969 /* The FEs may end up building ADDR_EXPRs early on a decl with
4970 an incomplete type. Re-build ADDR_EXPRs in canonical form
4971 here. */
4972 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4973 *expr_p = build_fold_addr_expr (op0);
4975 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4976 recompute_tree_invariant_for_addr_expr (*expr_p);
4978 /* If we re-built the ADDR_EXPR add a conversion to the original type
4979 if required. */
4980 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4981 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4983 break;
4986 return ret;
4989 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4990 value; output operands should be a gimple lvalue. */
4992 static enum gimplify_status
4993 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4995 tree expr;
4996 int noutputs;
4997 const char **oconstraints;
4998 int i;
4999 tree link;
5000 const char *constraint;
5001 bool allows_mem, allows_reg, is_inout;
5002 enum gimplify_status ret, tret;
5003 gasm *stmt;
5004 vec<tree, va_gc> *inputs;
5005 vec<tree, va_gc> *outputs;
5006 vec<tree, va_gc> *clobbers;
5007 vec<tree, va_gc> *labels;
5008 tree link_next;
5010 expr = *expr_p;
5011 noutputs = list_length (ASM_OUTPUTS (expr));
5012 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5014 inputs = NULL;
5015 outputs = NULL;
5016 clobbers = NULL;
5017 labels = NULL;
5019 ret = GS_ALL_DONE;
5020 link_next = NULL_TREE;
5021 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5023 bool ok;
5024 size_t constraint_len;
5026 link_next = TREE_CHAIN (link);
5028 oconstraints[i]
5029 = constraint
5030 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5031 constraint_len = strlen (constraint);
5032 if (constraint_len == 0)
5033 continue;
5035 ok = parse_output_constraint (&constraint, i, 0, 0,
5036 &allows_mem, &allows_reg, &is_inout);
5037 if (!ok)
5039 ret = GS_ERROR;
5040 is_inout = false;
5043 if (!allows_reg && allows_mem)
5044 mark_addressable (TREE_VALUE (link));
5046 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5047 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5048 fb_lvalue | fb_mayfail);
5049 if (tret == GS_ERROR)
5051 error ("invalid lvalue in asm output %d", i);
5052 ret = tret;
5055 vec_safe_push (outputs, link);
5056 TREE_CHAIN (link) = NULL_TREE;
5058 if (is_inout)
5060 /* An input/output operand. To give the optimizers more
5061 flexibility, split it into separate input and output
5062 operands. */
5063 tree input;
5064 char buf[10];
5066 /* Turn the in/out constraint into an output constraint. */
5067 char *p = xstrdup (constraint);
5068 p[0] = '=';
5069 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5071 /* And add a matching input constraint. */
5072 if (allows_reg)
5074 sprintf (buf, "%d", i);
5076 /* If there are multiple alternatives in the constraint,
5077 handle each of them individually. Those that allow register
5078 will be replaced with operand number, the others will stay
5079 unchanged. */
5080 if (strchr (p, ',') != NULL)
5082 size_t len = 0, buflen = strlen (buf);
5083 char *beg, *end, *str, *dst;
5085 for (beg = p + 1;;)
5087 end = strchr (beg, ',');
5088 if (end == NULL)
5089 end = strchr (beg, '\0');
5090 if ((size_t) (end - beg) < buflen)
5091 len += buflen + 1;
5092 else
5093 len += end - beg + 1;
5094 if (*end)
5095 beg = end + 1;
5096 else
5097 break;
5100 str = (char *) alloca (len);
5101 for (beg = p + 1, dst = str;;)
5103 const char *tem;
5104 bool mem_p, reg_p, inout_p;
5106 end = strchr (beg, ',');
5107 if (end)
5108 *end = '\0';
5109 beg[-1] = '=';
5110 tem = beg - 1;
5111 parse_output_constraint (&tem, i, 0, 0,
5112 &mem_p, &reg_p, &inout_p);
5113 if (dst != str)
5114 *dst++ = ',';
5115 if (reg_p)
5117 memcpy (dst, buf, buflen);
5118 dst += buflen;
5120 else
5122 if (end)
5123 len = end - beg;
5124 else
5125 len = strlen (beg);
5126 memcpy (dst, beg, len);
5127 dst += len;
5129 if (end)
5130 beg = end + 1;
5131 else
5132 break;
5134 *dst = '\0';
5135 input = build_string (dst - str, str);
5137 else
5138 input = build_string (strlen (buf), buf);
5140 else
5141 input = build_string (constraint_len - 1, constraint + 1);
5143 free (p);
5145 input = build_tree_list (build_tree_list (NULL_TREE, input),
5146 unshare_expr (TREE_VALUE (link)));
5147 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5151 link_next = NULL_TREE;
5152 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5154 link_next = TREE_CHAIN (link);
5155 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5156 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5157 oconstraints, &allows_mem, &allows_reg);
5159 /* If we can't make copies, we can only accept memory. */
5160 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5162 if (allows_mem)
5163 allows_reg = 0;
5164 else
5166 error ("impossible constraint in %<asm%>");
5167 error ("non-memory input %d must stay in memory", i);
5168 return GS_ERROR;
5172 /* If the operand is a memory input, it should be an lvalue. */
5173 if (!allows_reg && allows_mem)
5175 tree inputv = TREE_VALUE (link);
5176 STRIP_NOPS (inputv);
5177 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5178 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5179 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5180 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5181 TREE_VALUE (link) = error_mark_node;
5182 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5183 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5184 mark_addressable (TREE_VALUE (link));
5185 if (tret == GS_ERROR)
5187 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5188 input_location = EXPR_LOCATION (TREE_VALUE (link));
5189 error ("memory input %d is not directly addressable", i);
5190 ret = tret;
5193 else
5195 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5196 is_gimple_asm_val, fb_rvalue);
5197 if (tret == GS_ERROR)
5198 ret = tret;
5201 TREE_CHAIN (link) = NULL_TREE;
5202 vec_safe_push (inputs, link);
5205 link_next = NULL_TREE;
5206 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5208 link_next = TREE_CHAIN (link);
5209 TREE_CHAIN (link) = NULL_TREE;
5210 vec_safe_push (clobbers, link);
5213 link_next = NULL_TREE;
5214 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5216 link_next = TREE_CHAIN (link);
5217 TREE_CHAIN (link) = NULL_TREE;
5218 vec_safe_push (labels, link);
5221 /* Do not add ASMs with errors to the gimple IL stream. */
5222 if (ret != GS_ERROR)
5224 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5225 inputs, outputs, clobbers, labels);
5227 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5228 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5230 gimplify_seq_add_stmt (pre_p, stmt);
5233 return ret;
5236 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5237 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5238 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5239 return to this function.
5241 FIXME should we complexify the prequeue handling instead? Or use flags
5242 for all the cleanups and let the optimizer tighten them up? The current
5243 code seems pretty fragile; it will break on a cleanup within any
5244 non-conditional nesting. But any such nesting would be broken, anyway;
5245 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5246 and continues out of it. We can do that at the RTL level, though, so
5247 having an optimizer to tighten up try/finally regions would be a Good
5248 Thing. */
5250 static enum gimplify_status
5251 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5253 gimple_stmt_iterator iter;
5254 gimple_seq body_sequence = NULL;
5256 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5258 /* We only care about the number of conditions between the innermost
5259 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5260 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5261 int old_conds = gimplify_ctxp->conditions;
5262 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5263 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5264 gimplify_ctxp->conditions = 0;
5265 gimplify_ctxp->conditional_cleanups = NULL;
5266 gimplify_ctxp->in_cleanup_point_expr = true;
5268 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5270 gimplify_ctxp->conditions = old_conds;
5271 gimplify_ctxp->conditional_cleanups = old_cleanups;
5272 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5274 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5276 gimple wce = gsi_stmt (iter);
5278 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5280 if (gsi_one_before_end_p (iter))
5282 /* Note that gsi_insert_seq_before and gsi_remove do not
5283 scan operands, unlike some other sequence mutators. */
5284 if (!gimple_wce_cleanup_eh_only (wce))
5285 gsi_insert_seq_before_without_update (&iter,
5286 gimple_wce_cleanup (wce),
5287 GSI_SAME_STMT);
5288 gsi_remove (&iter, true);
5289 break;
5291 else
5293 gtry *gtry;
5294 gimple_seq seq;
5295 enum gimple_try_flags kind;
5297 if (gimple_wce_cleanup_eh_only (wce))
5298 kind = GIMPLE_TRY_CATCH;
5299 else
5300 kind = GIMPLE_TRY_FINALLY;
5301 seq = gsi_split_seq_after (iter);
5303 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5304 /* Do not use gsi_replace here, as it may scan operands.
5305 We want to do a simple structural modification only. */
5306 gsi_set_stmt (&iter, gtry);
5307 iter = gsi_start (gtry->eval);
5310 else
5311 gsi_next (&iter);
5314 gimplify_seq_add_seq (pre_p, body_sequence);
5315 if (temp)
5317 *expr_p = temp;
5318 return GS_OK;
5320 else
5322 *expr_p = NULL;
5323 return GS_ALL_DONE;
5327 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5328 is the cleanup action required. EH_ONLY is true if the cleanup should
5329 only be executed if an exception is thrown, not on normal exit. */
5331 static void
5332 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5334 gimple wce;
5335 gimple_seq cleanup_stmts = NULL;
5337 /* Errors can result in improperly nested cleanups. Which results in
5338 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5339 if (seen_error ())
5340 return;
5342 if (gimple_conditional_context ())
5344 /* If we're in a conditional context, this is more complex. We only
5345 want to run the cleanup if we actually ran the initialization that
5346 necessitates it, but we want to run it after the end of the
5347 conditional context. So we wrap the try/finally around the
5348 condition and use a flag to determine whether or not to actually
5349 run the destructor. Thus
5351 test ? f(A()) : 0
5353 becomes (approximately)
5355 flag = 0;
5356 try {
5357 if (test) { A::A(temp); flag = 1; val = f(temp); }
5358 else { val = 0; }
5359 } finally {
5360 if (flag) A::~A(temp);
5364 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5365 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
5366 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
5368 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5369 gimplify_stmt (&cleanup, &cleanup_stmts);
5370 wce = gimple_build_wce (cleanup_stmts);
5372 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5373 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5374 gimplify_seq_add_stmt (pre_p, ftrue);
5376 /* Because of this manipulation, and the EH edges that jump
5377 threading cannot redirect, the temporary (VAR) will appear
5378 to be used uninitialized. Don't warn. */
5379 TREE_NO_WARNING (var) = 1;
5381 else
5383 gimplify_stmt (&cleanup, &cleanup_stmts);
5384 wce = gimple_build_wce (cleanup_stmts);
5385 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5386 gimplify_seq_add_stmt (pre_p, wce);
5390 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5392 static enum gimplify_status
5393 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5395 tree targ = *expr_p;
5396 tree temp = TARGET_EXPR_SLOT (targ);
5397 tree init = TARGET_EXPR_INITIAL (targ);
5398 enum gimplify_status ret;
5400 if (init)
5402 tree cleanup = NULL_TREE;
5404 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5405 to the temps list. Handle also variable length TARGET_EXPRs. */
5406 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5408 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5409 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5410 gimplify_vla_decl (temp, pre_p);
5412 else
5413 gimple_add_tmp_var (temp);
5415 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5416 expression is supposed to initialize the slot. */
5417 if (VOID_TYPE_P (TREE_TYPE (init)))
5418 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5419 else
5421 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5422 init = init_expr;
5423 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5424 init = NULL;
5425 ggc_free (init_expr);
5427 if (ret == GS_ERROR)
5429 /* PR c++/28266 Make sure this is expanded only once. */
5430 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5431 return GS_ERROR;
5433 if (init)
5434 gimplify_and_add (init, pre_p);
5436 /* If needed, push the cleanup for the temp. */
5437 if (TARGET_EXPR_CLEANUP (targ))
5439 if (CLEANUP_EH_ONLY (targ))
5440 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5441 CLEANUP_EH_ONLY (targ), pre_p);
5442 else
5443 cleanup = TARGET_EXPR_CLEANUP (targ);
5446 /* Add a clobber for the temporary going out of scope, like
5447 gimplify_bind_expr. */
5448 if (gimplify_ctxp->in_cleanup_point_expr
5449 && needs_to_live_in_memory (temp)
5450 && flag_stack_reuse == SR_ALL)
5452 tree clobber = build_constructor (TREE_TYPE (temp),
5453 NULL);
5454 TREE_THIS_VOLATILE (clobber) = true;
5455 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5456 if (cleanup)
5457 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5458 clobber);
5459 else
5460 cleanup = clobber;
5463 if (cleanup)
5464 gimple_push_cleanup (temp, cleanup, false, pre_p);
5466 /* Only expand this once. */
5467 TREE_OPERAND (targ, 3) = init;
5468 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5470 else
5471 /* We should have expanded this before. */
5472 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5474 *expr_p = temp;
5475 return GS_OK;
5478 /* Gimplification of expression trees. */
5480 /* Gimplify an expression which appears at statement context. The
5481 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5482 NULL, a new sequence is allocated.
5484 Return true if we actually added a statement to the queue. */
5486 bool
5487 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5489 gimple_seq_node last;
5491 last = gimple_seq_last (*seq_p);
5492 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5493 return last != gimple_seq_last (*seq_p);
5496 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5497 to CTX. If entries already exist, force them to be some flavor of private.
5498 If there is no enclosing parallel, do nothing. */
5500 void
5501 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5503 splay_tree_node n;
5505 if (decl == NULL || !DECL_P (decl))
5506 return;
5510 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5511 if (n != NULL)
5513 if (n->value & GOVD_SHARED)
5514 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5515 else if (n->value & GOVD_MAP)
5516 n->value |= GOVD_MAP_TO_ONLY;
5517 else
5518 return;
5520 else if (ctx->region_type & ORT_TARGET)
5522 if (ctx->region_type & ORT_TARGET_OFFLOAD)
5523 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5525 else if (ctx->region_type != ORT_WORKSHARE
5526 && ctx->region_type != ORT_SIMD)
5527 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5529 ctx = ctx->outer_context;
5531 while (ctx);
5534 /* Similarly for each of the type sizes of TYPE. */
5536 static void
5537 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5539 if (type == NULL || type == error_mark_node)
5540 return;
5541 type = TYPE_MAIN_VARIANT (type);
5543 if (ctx->privatized_types->add (type))
5544 return;
5546 switch (TREE_CODE (type))
5548 case INTEGER_TYPE:
5549 case ENUMERAL_TYPE:
5550 case BOOLEAN_TYPE:
5551 case REAL_TYPE:
5552 case FIXED_POINT_TYPE:
5553 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5554 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5555 break;
5557 case ARRAY_TYPE:
5558 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5559 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5560 break;
5562 case RECORD_TYPE:
5563 case UNION_TYPE:
5564 case QUAL_UNION_TYPE:
5566 tree field;
5567 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5568 if (TREE_CODE (field) == FIELD_DECL)
5570 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5571 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5574 break;
5576 case POINTER_TYPE:
5577 case REFERENCE_TYPE:
5578 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5579 break;
5581 default:
5582 break;
5585 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5586 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5587 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5590 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5592 static void
5593 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5595 splay_tree_node n;
5596 unsigned int nflags;
5597 tree t;
5599 if (error_operand_p (decl))
5600 return;
5602 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5603 there are constructors involved somewhere. */
5604 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5605 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5606 flags |= GOVD_SEEN;
5608 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5609 if (n != NULL && n->value != GOVD_ALIGNED)
5611 /* We shouldn't be re-adding the decl with the same data
5612 sharing class. */
5613 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5614 /* The only combination of data sharing classes we should see is
5615 FIRSTPRIVATE and LASTPRIVATE. */
5616 nflags = n->value | flags;
5617 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5618 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5619 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5620 n->value = nflags;
5621 return;
5624 /* When adding a variable-sized variable, we have to handle all sorts
5625 of additional bits of data: the pointer replacement variable, and
5626 the parameters of the type. */
5627 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5629 /* Add the pointer replacement variable as PRIVATE if the variable
5630 replacement is private, else FIRSTPRIVATE since we'll need the
5631 address of the original variable either for SHARED, or for the
5632 copy into or out of the context. */
5633 if (!(flags & GOVD_LOCAL))
5635 if (flags & GOVD_MAP)
5636 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5637 else if (flags & GOVD_PRIVATE)
5638 nflags = GOVD_PRIVATE;
5639 else
5640 nflags = GOVD_FIRSTPRIVATE;
5641 nflags |= flags & GOVD_SEEN;
5642 t = DECL_VALUE_EXPR (decl);
5643 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5644 t = TREE_OPERAND (t, 0);
5645 gcc_assert (DECL_P (t));
5646 omp_add_variable (ctx, t, nflags);
5649 /* Add all of the variable and type parameters (which should have
5650 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5651 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5652 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5653 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5655 /* The variable-sized variable itself is never SHARED, only some form
5656 of PRIVATE. The sharing would take place via the pointer variable
5657 which we remapped above. */
5658 if (flags & GOVD_SHARED)
5659 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5660 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5662 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5663 alloca statement we generate for the variable, so make sure it
5664 is available. This isn't automatically needed for the SHARED
5665 case, since we won't be allocating local storage then.
5666 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5667 in this case omp_notice_variable will be called later
5668 on when it is gimplified. */
5669 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5670 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5671 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5673 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5674 && lang_hooks.decls.omp_privatize_by_reference (decl))
5676 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5678 /* Similar to the direct variable sized case above, we'll need the
5679 size of references being privatized. */
5680 if ((flags & GOVD_SHARED) == 0)
5682 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5683 if (TREE_CODE (t) != INTEGER_CST)
5684 omp_notice_variable (ctx, t, true);
5688 if (n != NULL)
5689 n->value |= flags;
5690 else
5691 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5694 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5695 This just prints out diagnostics about threadprivate variable uses
5696 in untied tasks. If DECL2 is non-NULL, prevent this warning
5697 on that variable. */
5699 static bool
5700 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5701 tree decl2)
5703 splay_tree_node n;
5704 struct gimplify_omp_ctx *octx;
5706 for (octx = ctx; octx; octx = octx->outer_context)
5707 if ((octx->region_type & ORT_TARGET)
5708 && (octx->region_type & ORT_TARGET_OFFLOAD))
5710 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5711 if (n == NULL)
5713 error ("threadprivate variable %qE used in target region",
5714 DECL_NAME (decl));
5715 error_at (octx->location, "enclosing target region");
5716 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5718 if (decl2)
5719 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5722 if (ctx->region_type != ORT_UNTIED_TASK)
5723 return false;
5724 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5725 if (n == NULL)
5727 error ("threadprivate variable %qE used in untied task",
5728 DECL_NAME (decl));
5729 error_at (ctx->location, "enclosing task");
5730 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5732 if (decl2)
5733 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5734 return false;
5737 /* Record the fact that DECL was used within the OpenMP context CTX.
5738 IN_CODE is true when real code uses DECL, and false when we should
5739 merely emit default(none) errors. Return true if DECL is going to
5740 be remapped and thus DECL shouldn't be gimplified into its
5741 DECL_VALUE_EXPR (if any). */
5743 static bool
5744 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5746 splay_tree_node n;
5747 unsigned flags = in_code ? GOVD_SEEN : 0;
5748 bool ret = false, shared;
5750 if (error_operand_p (decl))
5751 return false;
5753 /* Threadprivate variables are predetermined. */
5754 if (is_global_var (decl))
5756 if (DECL_THREAD_LOCAL_P (decl))
5757 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5759 if (DECL_HAS_VALUE_EXPR_P (decl))
5761 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5763 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5764 return omp_notice_threadprivate_variable (ctx, decl, value);
5768 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5769 if ((ctx->region_type & ORT_TARGET)
5770 && (ctx->region_type & ORT_TARGET_OFFLOAD))
5772 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5773 if (n == NULL)
5775 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5777 error ("%qD referenced in target region does not have "
5778 "a mappable type", decl);
5779 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5781 else
5782 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5784 else
5786 /* If nothing changed, there's nothing left to do. */
5787 if ((n->value & flags) == flags)
5788 return ret;
5789 n->value |= flags;
5791 goto do_outer;
5794 if (n == NULL)
5796 enum omp_clause_default_kind default_kind, kind;
5797 struct gimplify_omp_ctx *octx;
5799 if (ctx->region_type == ORT_WORKSHARE
5800 || ctx->region_type == ORT_SIMD
5801 || ((ctx->region_type & ORT_TARGET)
5802 && !(ctx->region_type & ORT_TARGET_OFFLOAD)))
5803 goto do_outer;
5805 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5806 remapped firstprivate instead of shared. To some extent this is
5807 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5808 default_kind = ctx->default_kind;
5809 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5810 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5811 default_kind = kind;
5813 switch (default_kind)
5815 case OMP_CLAUSE_DEFAULT_NONE:
5816 if ((ctx->region_type & ORT_PARALLEL) != 0)
5818 error ("%qE not specified in enclosing parallel",
5819 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5820 error_at (ctx->location, "enclosing parallel");
5822 else if ((ctx->region_type & ORT_TASK) != 0)
5824 error ("%qE not specified in enclosing task",
5825 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5826 error_at (ctx->location, "enclosing task");
5828 else if (ctx->region_type == ORT_TEAMS)
5830 error ("%qE not specified in enclosing teams construct",
5831 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5832 error_at (ctx->location, "enclosing teams construct");
5834 else
5835 gcc_unreachable ();
5836 /* FALLTHRU */
5837 case OMP_CLAUSE_DEFAULT_SHARED:
5838 flags |= GOVD_SHARED;
5839 break;
5840 case OMP_CLAUSE_DEFAULT_PRIVATE:
5841 flags |= GOVD_PRIVATE;
5842 break;
5843 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5844 flags |= GOVD_FIRSTPRIVATE;
5845 break;
5846 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5847 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5848 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5849 if (ctx->outer_context)
5850 omp_notice_variable (ctx->outer_context, decl, in_code);
5851 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5853 splay_tree_node n2;
5855 if (octx->region_type & ORT_TARGET)
5856 continue;
5857 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5858 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5860 flags |= GOVD_FIRSTPRIVATE;
5861 break;
5863 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5864 break;
5866 if (flags & GOVD_FIRSTPRIVATE)
5867 break;
5868 if (octx == NULL
5869 && (TREE_CODE (decl) == PARM_DECL
5870 || (!is_global_var (decl)
5871 && DECL_CONTEXT (decl) == current_function_decl)))
5873 flags |= GOVD_FIRSTPRIVATE;
5874 break;
5876 flags |= GOVD_SHARED;
5877 break;
5878 default:
5879 gcc_unreachable ();
5882 if ((flags & GOVD_PRIVATE)
5883 && lang_hooks.decls.omp_private_outer_ref (decl))
5884 flags |= GOVD_PRIVATE_OUTER_REF;
5886 omp_add_variable (ctx, decl, flags);
5888 shared = (flags & GOVD_SHARED) != 0;
5889 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5890 goto do_outer;
5893 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5894 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5895 && DECL_SIZE (decl)
5896 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5898 splay_tree_node n2;
5899 tree t = DECL_VALUE_EXPR (decl);
5900 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5901 t = TREE_OPERAND (t, 0);
5902 gcc_assert (DECL_P (t));
5903 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5904 n2->value |= GOVD_SEEN;
5907 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5908 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5910 /* If nothing changed, there's nothing left to do. */
5911 if ((n->value & flags) == flags)
5912 return ret;
5913 flags |= n->value;
5914 n->value = flags;
5916 do_outer:
5917 /* If the variable is private in the current context, then we don't
5918 need to propagate anything to an outer context. */
5919 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5920 return ret;
5921 if (ctx->outer_context
5922 && omp_notice_variable (ctx->outer_context, decl, in_code))
5923 return true;
5924 return ret;
5927 /* Verify that DECL is private within CTX. If there's specific information
5928 to the contrary in the innermost scope, generate an error. */
5930 static bool
5931 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5933 splay_tree_node n;
5935 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5936 if (n != NULL)
5938 if (n->value & GOVD_SHARED)
5940 if (ctx == gimplify_omp_ctxp)
5942 if (simd)
5943 error ("iteration variable %qE is predetermined linear",
5944 DECL_NAME (decl));
5945 else
5946 error ("iteration variable %qE should be private",
5947 DECL_NAME (decl));
5948 n->value = GOVD_PRIVATE;
5949 return true;
5951 else
5952 return false;
5954 else if ((n->value & GOVD_EXPLICIT) != 0
5955 && (ctx == gimplify_omp_ctxp
5956 || (ctx->region_type == ORT_COMBINED_PARALLEL
5957 && gimplify_omp_ctxp->outer_context == ctx)))
5959 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5960 error ("iteration variable %qE should not be firstprivate",
5961 DECL_NAME (decl));
5962 else if ((n->value & GOVD_REDUCTION) != 0)
5963 error ("iteration variable %qE should not be reduction",
5964 DECL_NAME (decl));
5965 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5966 error ("iteration variable %qE should not be lastprivate",
5967 DECL_NAME (decl));
5968 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5969 error ("iteration variable %qE should not be private",
5970 DECL_NAME (decl));
5971 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5972 error ("iteration variable %qE is predetermined linear",
5973 DECL_NAME (decl));
5975 return (ctx == gimplify_omp_ctxp
5976 || (ctx->region_type == ORT_COMBINED_PARALLEL
5977 && gimplify_omp_ctxp->outer_context == ctx));
5980 if (ctx->region_type != ORT_WORKSHARE
5981 && ctx->region_type != ORT_SIMD)
5982 return false;
5983 else if (ctx->outer_context)
5984 return omp_is_private (ctx->outer_context, decl, simd);
5985 return false;
5988 /* Return true if DECL is private within a parallel region
5989 that binds to the current construct's context or in parallel
5990 region's REDUCTION clause. */
5992 static bool
5993 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5995 splay_tree_node n;
5999 ctx = ctx->outer_context;
6000 if (ctx == NULL)
6001 return !(is_global_var (decl)
6002 /* References might be private, but might be shared too,
6003 when checking for copyprivate, assume they might be
6004 private, otherwise assume they might be shared. */
6005 || (!copyprivate
6006 && lang_hooks.decls.omp_privatize_by_reference (decl)));
6008 if (ctx->region_type & ORT_TARGET)
6009 continue;
6011 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6012 if (n != NULL)
6013 return (n->value & GOVD_SHARED) == 0;
6015 while (ctx->region_type == ORT_WORKSHARE
6016 || ctx->region_type == ORT_SIMD);
6017 return false;
6020 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6021 and previous omp contexts. */
6023 static void
6024 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6025 enum omp_region_type region_type)
6027 struct gimplify_omp_ctx *ctx, *outer_ctx;
6028 tree c;
6030 ctx = new_omp_context (region_type);
6031 outer_ctx = ctx->outer_context;
6033 while ((c = *list_p) != NULL)
6035 bool remove = false;
6036 bool notice_outer = true;
6037 const char *check_non_private = NULL;
6038 unsigned int flags;
6039 tree decl;
6041 switch (OMP_CLAUSE_CODE (c))
6043 case OMP_CLAUSE_PRIVATE:
6044 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6045 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6047 flags |= GOVD_PRIVATE_OUTER_REF;
6048 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6050 else
6051 notice_outer = false;
6052 goto do_add;
6053 case OMP_CLAUSE_SHARED:
6054 flags = GOVD_SHARED | GOVD_EXPLICIT;
6055 goto do_add;
6056 case OMP_CLAUSE_FIRSTPRIVATE:
6057 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6058 check_non_private = "firstprivate";
6059 goto do_add;
6060 case OMP_CLAUSE_LASTPRIVATE:
6061 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6062 check_non_private = "lastprivate";
6063 goto do_add;
6064 case OMP_CLAUSE_REDUCTION:
6065 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6066 check_non_private = "reduction";
6067 goto do_add;
6068 case OMP_CLAUSE_LINEAR:
6069 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6070 is_gimple_val, fb_rvalue) == GS_ERROR)
6072 remove = true;
6073 break;
6075 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6076 goto do_add;
6078 case OMP_CLAUSE_MAP:
6079 decl = OMP_CLAUSE_DECL (c);
6080 if (error_operand_p (decl))
6082 remove = true;
6083 break;
6085 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6086 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6087 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6088 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6089 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6091 remove = true;
6092 break;
6094 if (!DECL_P (decl))
6096 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6097 NULL, is_gimple_lvalue, fb_lvalue)
6098 == GS_ERROR)
6100 remove = true;
6101 break;
6103 break;
6105 flags = GOVD_MAP | GOVD_EXPLICIT;
6106 goto do_add;
6108 case OMP_CLAUSE_DEPEND:
6109 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6111 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6112 NULL, is_gimple_val, fb_rvalue);
6113 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6115 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6117 remove = true;
6118 break;
6120 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6121 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6122 is_gimple_val, fb_rvalue) == GS_ERROR)
6124 remove = true;
6125 break;
6127 break;
6129 case OMP_CLAUSE_TO:
6130 case OMP_CLAUSE_FROM:
6131 case OMP_CLAUSE__CACHE_:
6132 decl = OMP_CLAUSE_DECL (c);
6133 if (error_operand_p (decl))
6135 remove = true;
6136 break;
6138 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6139 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6140 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6141 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6142 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6144 remove = true;
6145 break;
6147 if (!DECL_P (decl))
6149 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6150 NULL, is_gimple_lvalue, fb_lvalue)
6151 == GS_ERROR)
6153 remove = true;
6154 break;
6156 break;
6158 goto do_notice;
6160 do_add:
6161 decl = OMP_CLAUSE_DECL (c);
6162 if (error_operand_p (decl))
6164 remove = true;
6165 break;
6167 omp_add_variable (ctx, decl, flags);
6168 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6169 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6171 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6172 GOVD_LOCAL | GOVD_SEEN);
6173 gimplify_omp_ctxp = ctx;
6174 push_gimplify_context ();
6176 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6177 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6179 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6180 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6181 pop_gimplify_context
6182 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6183 push_gimplify_context ();
6184 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6185 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6186 pop_gimplify_context
6187 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6188 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6189 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6191 gimplify_omp_ctxp = outer_ctx;
6193 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6194 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6196 gimplify_omp_ctxp = ctx;
6197 push_gimplify_context ();
6198 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6200 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6201 NULL, NULL);
6202 TREE_SIDE_EFFECTS (bind) = 1;
6203 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6204 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6206 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6207 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6208 pop_gimplify_context
6209 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6210 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6212 gimplify_omp_ctxp = outer_ctx;
6214 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6215 && OMP_CLAUSE_LINEAR_STMT (c))
6217 gimplify_omp_ctxp = ctx;
6218 push_gimplify_context ();
6219 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6221 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6222 NULL, NULL);
6223 TREE_SIDE_EFFECTS (bind) = 1;
6224 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6225 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6227 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6228 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6229 pop_gimplify_context
6230 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6231 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6233 gimplify_omp_ctxp = outer_ctx;
6235 if (notice_outer)
6236 goto do_notice;
6237 break;
6239 case OMP_CLAUSE_COPYIN:
6240 case OMP_CLAUSE_COPYPRIVATE:
6241 decl = OMP_CLAUSE_DECL (c);
6242 if (error_operand_p (decl))
6244 remove = true;
6245 break;
6247 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6248 && !remove
6249 && !omp_check_private (ctx, decl, true))
6251 remove = true;
6252 if (is_global_var (decl))
6254 if (DECL_THREAD_LOCAL_P (decl))
6255 remove = false;
6256 else if (DECL_HAS_VALUE_EXPR_P (decl))
6258 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6260 if (value
6261 && DECL_P (value)
6262 && DECL_THREAD_LOCAL_P (value))
6263 remove = false;
6266 if (remove)
6267 error_at (OMP_CLAUSE_LOCATION (c),
6268 "copyprivate variable %qE is not threadprivate"
6269 " or private in outer context", DECL_NAME (decl));
6271 do_notice:
6272 if (outer_ctx)
6273 omp_notice_variable (outer_ctx, decl, true);
6274 if (check_non_private
6275 && region_type == ORT_WORKSHARE
6276 && omp_check_private (ctx, decl, false))
6278 error ("%s variable %qE is private in outer context",
6279 check_non_private, DECL_NAME (decl));
6280 remove = true;
6282 break;
6284 case OMP_CLAUSE_FINAL:
6285 case OMP_CLAUSE_IF:
6286 OMP_CLAUSE_OPERAND (c, 0)
6287 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6288 /* Fall through. */
6290 case OMP_CLAUSE_SCHEDULE:
6291 case OMP_CLAUSE_NUM_THREADS:
6292 case OMP_CLAUSE_NUM_TEAMS:
6293 case OMP_CLAUSE_THREAD_LIMIT:
6294 case OMP_CLAUSE_DIST_SCHEDULE:
6295 case OMP_CLAUSE_DEVICE:
6296 case OMP_CLAUSE__CILK_FOR_COUNT_:
6297 case OMP_CLAUSE_NUM_GANGS:
6298 case OMP_CLAUSE_NUM_WORKERS:
6299 case OMP_CLAUSE_VECTOR_LENGTH:
6300 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6301 is_gimple_val, fb_rvalue) == GS_ERROR)
6302 remove = true;
6303 break;
6305 case OMP_CLAUSE_DEVICE_RESIDENT:
6306 case OMP_CLAUSE_USE_DEVICE:
6307 case OMP_CLAUSE_GANG:
6308 case OMP_CLAUSE_ASYNC:
6309 case OMP_CLAUSE_WAIT:
6310 case OMP_CLAUSE_INDEPENDENT:
6311 case OMP_CLAUSE_WORKER:
6312 case OMP_CLAUSE_VECTOR:
6313 remove = true;
6314 break;
6316 case OMP_CLAUSE_NOWAIT:
6317 case OMP_CLAUSE_ORDERED:
6318 case OMP_CLAUSE_UNTIED:
6319 case OMP_CLAUSE_COLLAPSE:
6320 case OMP_CLAUSE_MERGEABLE:
6321 case OMP_CLAUSE_PROC_BIND:
6322 case OMP_CLAUSE_SAFELEN:
6323 break;
6325 case OMP_CLAUSE_ALIGNED:
6326 decl = OMP_CLAUSE_DECL (c);
6327 if (error_operand_p (decl))
6329 remove = true;
6330 break;
6332 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6333 is_gimple_val, fb_rvalue) == GS_ERROR)
6335 remove = true;
6336 break;
6338 if (!is_global_var (decl)
6339 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6340 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6341 break;
6343 case OMP_CLAUSE_DEFAULT:
6344 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6345 break;
6347 default:
6348 gcc_unreachable ();
6351 if (remove)
6352 *list_p = OMP_CLAUSE_CHAIN (c);
6353 else
6354 list_p = &OMP_CLAUSE_CHAIN (c);
6357 gimplify_omp_ctxp = ctx;
6360 struct gimplify_adjust_omp_clauses_data
6362 tree *list_p;
6363 gimple_seq *pre_p;
6366 /* For all variables that were not actually used within the context,
6367 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6369 static int
6370 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6372 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6373 gimple_seq *pre_p
6374 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6375 tree decl = (tree) n->key;
6376 unsigned flags = n->value;
6377 enum omp_clause_code code;
6378 tree clause;
6379 bool private_debug;
6381 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6382 return 0;
6383 if ((flags & GOVD_SEEN) == 0)
6384 return 0;
6385 if (flags & GOVD_DEBUG_PRIVATE)
6387 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6388 private_debug = true;
6390 else if (flags & GOVD_MAP)
6391 private_debug = false;
6392 else
6393 private_debug
6394 = lang_hooks.decls.omp_private_debug_clause (decl,
6395 !!(flags & GOVD_SHARED));
6396 if (private_debug)
6397 code = OMP_CLAUSE_PRIVATE;
6398 else if (flags & GOVD_MAP)
6399 code = OMP_CLAUSE_MAP;
6400 else if (flags & GOVD_SHARED)
6402 if (is_global_var (decl))
6404 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6405 while (ctx != NULL)
6407 splay_tree_node on
6408 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6409 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6410 | GOVD_PRIVATE | GOVD_REDUCTION
6411 | GOVD_LINEAR | GOVD_MAP)) != 0)
6412 break;
6413 ctx = ctx->outer_context;
6415 if (ctx == NULL)
6416 return 0;
6418 code = OMP_CLAUSE_SHARED;
6420 else if (flags & GOVD_PRIVATE)
6421 code = OMP_CLAUSE_PRIVATE;
6422 else if (flags & GOVD_FIRSTPRIVATE)
6423 code = OMP_CLAUSE_FIRSTPRIVATE;
6424 else if (flags & GOVD_LASTPRIVATE)
6425 code = OMP_CLAUSE_LASTPRIVATE;
6426 else if (flags & GOVD_ALIGNED)
6427 return 0;
6428 else
6429 gcc_unreachable ();
6431 clause = build_omp_clause (input_location, code);
6432 OMP_CLAUSE_DECL (clause) = decl;
6433 OMP_CLAUSE_CHAIN (clause) = *list_p;
6434 if (private_debug)
6435 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6436 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6437 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6438 else if (code == OMP_CLAUSE_MAP)
6440 enum omp_clause_map_kind map_kind;
6441 map_kind = (flags & GOVD_MAP_TO_ONLY
6442 ? OMP_CLAUSE_MAP_TO
6443 : OMP_CLAUSE_MAP_TOFROM);
6444 OMP_CLAUSE_MAP_KIND (clause) = map_kind;
6446 if (DECL_SIZE (decl)
6447 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6449 tree decl2 = DECL_VALUE_EXPR (decl);
6450 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6451 decl2 = TREE_OPERAND (decl2, 0);
6452 gcc_assert (DECL_P (decl2));
6453 tree mem = build_simple_mem_ref (decl2);
6454 OMP_CLAUSE_DECL (clause) = mem;
6455 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6456 if (gimplify_omp_ctxp->outer_context)
6458 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6459 omp_notice_variable (ctx, decl2, true);
6460 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6462 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6463 OMP_CLAUSE_MAP);
6464 OMP_CLAUSE_DECL (nc) = decl;
6465 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6466 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6467 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6468 OMP_CLAUSE_CHAIN (clause) = nc;
6470 else
6471 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6473 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6475 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6476 OMP_CLAUSE_DECL (nc) = decl;
6477 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6478 OMP_CLAUSE_CHAIN (nc) = *list_p;
6479 OMP_CLAUSE_CHAIN (clause) = nc;
6480 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6481 gimplify_omp_ctxp = ctx->outer_context;
6482 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6483 gimplify_omp_ctxp = ctx;
6485 *list_p = clause;
6486 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6487 gimplify_omp_ctxp = ctx->outer_context;
6488 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6489 gimplify_omp_ctxp = ctx;
6490 return 0;
6493 static void
6494 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6496 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6497 tree c, decl;
6499 while ((c = *list_p) != NULL)
6501 splay_tree_node n;
6502 bool remove = false;
6504 switch (OMP_CLAUSE_CODE (c))
6506 case OMP_CLAUSE_PRIVATE:
6507 case OMP_CLAUSE_SHARED:
6508 case OMP_CLAUSE_FIRSTPRIVATE:
6509 case OMP_CLAUSE_LINEAR:
6510 decl = OMP_CLAUSE_DECL (c);
6511 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6512 remove = !(n->value & GOVD_SEEN);
6513 if (! remove)
6515 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6516 if ((n->value & GOVD_DEBUG_PRIVATE)
6517 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6519 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6520 || ((n->value & GOVD_DATA_SHARE_CLASS)
6521 == GOVD_PRIVATE));
6522 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6523 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6525 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6526 && ctx->outer_context
6527 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6528 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6530 if (ctx->outer_context->combined_loop
6531 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6533 n = splay_tree_lookup (ctx->outer_context->variables,
6534 (splay_tree_key) decl);
6535 if (n == NULL
6536 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6538 int flags = GOVD_FIRSTPRIVATE;
6539 /* #pragma omp distribute does not allow
6540 lastprivate clause. */
6541 if (!ctx->outer_context->distribute)
6542 flags |= GOVD_LASTPRIVATE;
6543 if (n == NULL)
6544 omp_add_variable (ctx->outer_context, decl,
6545 flags | GOVD_SEEN);
6546 else
6547 n->value |= flags | GOVD_SEEN;
6550 else if (!is_global_var (decl))
6551 omp_notice_variable (ctx->outer_context, decl, true);
6554 break;
6556 case OMP_CLAUSE_LASTPRIVATE:
6557 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6558 accurately reflect the presence of a FIRSTPRIVATE clause. */
6559 decl = OMP_CLAUSE_DECL (c);
6560 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6561 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6562 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6563 break;
6565 case OMP_CLAUSE_ALIGNED:
6566 decl = OMP_CLAUSE_DECL (c);
6567 if (!is_global_var (decl))
6569 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6570 remove = n == NULL || !(n->value & GOVD_SEEN);
6571 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6573 struct gimplify_omp_ctx *octx;
6574 if (n != NULL
6575 && (n->value & (GOVD_DATA_SHARE_CLASS
6576 & ~GOVD_FIRSTPRIVATE)))
6577 remove = true;
6578 else
6579 for (octx = ctx->outer_context; octx;
6580 octx = octx->outer_context)
6582 n = splay_tree_lookup (octx->variables,
6583 (splay_tree_key) decl);
6584 if (n == NULL)
6585 continue;
6586 if (n->value & GOVD_LOCAL)
6587 break;
6588 /* We have to avoid assigning a shared variable
6589 to itself when trying to add
6590 __builtin_assume_aligned. */
6591 if (n->value & GOVD_SHARED)
6593 remove = true;
6594 break;
6599 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6601 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6602 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6603 remove = true;
6605 break;
6607 case OMP_CLAUSE_MAP:
6608 decl = OMP_CLAUSE_DECL (c);
6609 if (!DECL_P (decl))
6610 break;
6611 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6612 if ((ctx->region_type & ORT_TARGET)
6613 && (ctx->region_type & ORT_TARGET_OFFLOAD)
6614 && !(n->value & GOVD_SEEN))
6615 remove = true;
6616 else if (DECL_SIZE (decl)
6617 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6618 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6620 /* For OMP_CLAUSE_MAP_FORCE_DEVICEPTR, we'll never enter here,
6621 because for these, TREE_CODE (DECL_SIZE (decl)) will always be
6622 INTEGER_CST. */
6623 gcc_assert (OMP_CLAUSE_MAP_KIND (c)
6624 != OMP_CLAUSE_MAP_FORCE_DEVICEPTR);
6626 tree decl2 = DECL_VALUE_EXPR (decl);
6627 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6628 decl2 = TREE_OPERAND (decl2, 0);
6629 gcc_assert (DECL_P (decl2));
6630 tree mem = build_simple_mem_ref (decl2);
6631 OMP_CLAUSE_DECL (c) = mem;
6632 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6633 if (ctx->outer_context)
6635 omp_notice_variable (ctx->outer_context, decl2, true);
6636 omp_notice_variable (ctx->outer_context,
6637 OMP_CLAUSE_SIZE (c), true);
6639 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6640 OMP_CLAUSE_MAP);
6641 OMP_CLAUSE_DECL (nc) = decl;
6642 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6643 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6644 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6645 OMP_CLAUSE_CHAIN (c) = nc;
6646 c = nc;
6648 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6649 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6650 break;
6652 case OMP_CLAUSE_TO:
6653 case OMP_CLAUSE_FROM:
6654 case OMP_CLAUSE__CACHE_:
6655 decl = OMP_CLAUSE_DECL (c);
6656 if (!DECL_P (decl))
6657 break;
6658 if (DECL_SIZE (decl)
6659 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6661 tree decl2 = DECL_VALUE_EXPR (decl);
6662 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6663 decl2 = TREE_OPERAND (decl2, 0);
6664 gcc_assert (DECL_P (decl2));
6665 tree mem = build_simple_mem_ref (decl2);
6666 OMP_CLAUSE_DECL (c) = mem;
6667 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6668 if (ctx->outer_context)
6670 omp_notice_variable (ctx->outer_context, decl2, true);
6671 omp_notice_variable (ctx->outer_context,
6672 OMP_CLAUSE_SIZE (c), true);
6675 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6676 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6677 break;
6679 case OMP_CLAUSE_REDUCTION:
6680 case OMP_CLAUSE_COPYIN:
6681 case OMP_CLAUSE_COPYPRIVATE:
6682 case OMP_CLAUSE_IF:
6683 case OMP_CLAUSE_NUM_THREADS:
6684 case OMP_CLAUSE_NUM_TEAMS:
6685 case OMP_CLAUSE_THREAD_LIMIT:
6686 case OMP_CLAUSE_DIST_SCHEDULE:
6687 case OMP_CLAUSE_DEVICE:
6688 case OMP_CLAUSE_SCHEDULE:
6689 case OMP_CLAUSE_NOWAIT:
6690 case OMP_CLAUSE_ORDERED:
6691 case OMP_CLAUSE_DEFAULT:
6692 case OMP_CLAUSE_UNTIED:
6693 case OMP_CLAUSE_COLLAPSE:
6694 case OMP_CLAUSE_FINAL:
6695 case OMP_CLAUSE_MERGEABLE:
6696 case OMP_CLAUSE_PROC_BIND:
6697 case OMP_CLAUSE_SAFELEN:
6698 case OMP_CLAUSE_DEPEND:
6699 case OMP_CLAUSE__CILK_FOR_COUNT_:
6700 case OMP_CLAUSE_NUM_GANGS:
6701 case OMP_CLAUSE_NUM_WORKERS:
6702 case OMP_CLAUSE_VECTOR_LENGTH:
6703 break;
6705 case OMP_CLAUSE_DEVICE_RESIDENT:
6706 case OMP_CLAUSE_USE_DEVICE:
6707 case OMP_CLAUSE_GANG:
6708 case OMP_CLAUSE_ASYNC:
6709 case OMP_CLAUSE_WAIT:
6710 case OMP_CLAUSE_INDEPENDENT:
6711 case OMP_CLAUSE_WORKER:
6712 case OMP_CLAUSE_VECTOR:
6713 default:
6714 gcc_unreachable ();
6717 if (remove)
6718 *list_p = OMP_CLAUSE_CHAIN (c);
6719 else
6720 list_p = &OMP_CLAUSE_CHAIN (c);
6723 /* Add in any implicit data sharing. */
6724 struct gimplify_adjust_omp_clauses_data data;
6725 data.list_p = list_p;
6726 data.pre_p = pre_p;
6727 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6729 gimplify_omp_ctxp = ctx->outer_context;
6730 delete_omp_context (ctx);
6733 /* Gimplify OACC_CACHE. */
6735 static void
6736 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
6738 tree expr = *expr_p;
6740 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6741 gimplify_adjust_omp_clauses (pre_p, &OACC_CACHE_CLAUSES (expr));
6743 /* TODO: Do something sensible with this information. */
6745 *expr_p = NULL_TREE;
6748 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6749 gimplification of the body, as well as scanning the body for used
6750 variables. We need to do this scan now, because variable-sized
6751 decls will be decomposed during gimplification. */
6753 static void
6754 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6756 tree expr = *expr_p;
6757 gimple g;
6758 gimple_seq body = NULL;
6760 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6761 OMP_PARALLEL_COMBINED (expr)
6762 ? ORT_COMBINED_PARALLEL
6763 : ORT_PARALLEL);
6765 push_gimplify_context ();
6767 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6768 if (gimple_code (g) == GIMPLE_BIND)
6769 pop_gimplify_context (g);
6770 else
6771 pop_gimplify_context (NULL);
6773 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6775 g = gimple_build_omp_parallel (body,
6776 OMP_PARALLEL_CLAUSES (expr),
6777 NULL_TREE, NULL_TREE);
6778 if (OMP_PARALLEL_COMBINED (expr))
6779 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6780 gimplify_seq_add_stmt (pre_p, g);
6781 *expr_p = NULL_TREE;
6784 /* Gimplify the contents of an OMP_TASK statement. This involves
6785 gimplification of the body, as well as scanning the body for used
6786 variables. We need to do this scan now, because variable-sized
6787 decls will be decomposed during gimplification. */
6789 static void
6790 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6792 tree expr = *expr_p;
6793 gimple g;
6794 gimple_seq body = NULL;
6796 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6797 find_omp_clause (OMP_TASK_CLAUSES (expr),
6798 OMP_CLAUSE_UNTIED)
6799 ? ORT_UNTIED_TASK : ORT_TASK);
6801 push_gimplify_context ();
6803 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6804 if (gimple_code (g) == GIMPLE_BIND)
6805 pop_gimplify_context (g);
6806 else
6807 pop_gimplify_context (NULL);
6809 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6811 g = gimple_build_omp_task (body,
6812 OMP_TASK_CLAUSES (expr),
6813 NULL_TREE, NULL_TREE,
6814 NULL_TREE, NULL_TREE, NULL_TREE);
6815 gimplify_seq_add_stmt (pre_p, g);
6816 *expr_p = NULL_TREE;
6819 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6820 with non-NULL OMP_FOR_INIT. */
6822 static tree
6823 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6825 *walk_subtrees = 0;
6826 switch (TREE_CODE (*tp))
6828 case OMP_FOR:
6829 *walk_subtrees = 1;
6830 /* FALLTHRU */
6831 case OMP_SIMD:
6832 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6833 return *tp;
6834 break;
6835 case BIND_EXPR:
6836 case STATEMENT_LIST:
6837 case OMP_PARALLEL:
6838 *walk_subtrees = 1;
6839 break;
6840 default:
6841 break;
6843 return NULL_TREE;
6846 /* Gimplify the gross structure of an OMP_FOR statement. */
6848 static enum gimplify_status
6849 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6851 tree for_stmt, orig_for_stmt, decl, var, t;
6852 enum gimplify_status ret = GS_ALL_DONE;
6853 enum gimplify_status tret;
6854 gomp_for *gfor;
6855 gimple_seq for_body, for_pre_body;
6856 int i;
6857 bool simd;
6858 enum omp_region_type ort;
6859 bitmap has_decl_expr = NULL;
6861 orig_for_stmt = for_stmt = *expr_p;
6863 switch (TREE_CODE (for_stmt))
6865 case OMP_FOR:
6866 case CILK_FOR:
6867 case OMP_DISTRIBUTE:
6868 simd = false;
6869 ort = ORT_WORKSHARE;
6870 break;
6871 case OMP_SIMD:
6872 case CILK_SIMD:
6873 simd = true;
6874 ort = ORT_SIMD;
6875 break;
6876 case OACC_LOOP:
6877 simd = false;
6878 ort = /* TODO */ ORT_WORKSHARE;
6879 break;
6880 default:
6881 gcc_unreachable ();
6884 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort);
6885 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6886 gimplify_omp_ctxp->distribute = true;
6888 /* Handle OMP_FOR_INIT. */
6889 for_pre_body = NULL;
6890 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6892 has_decl_expr = BITMAP_ALLOC (NULL);
6893 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6894 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6895 == VAR_DECL)
6897 t = OMP_FOR_PRE_BODY (for_stmt);
6898 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6900 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6902 tree_stmt_iterator si;
6903 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6904 tsi_next (&si))
6906 t = tsi_stmt (si);
6907 if (TREE_CODE (t) == DECL_EXPR
6908 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6909 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6913 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6914 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6916 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6918 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
6919 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6920 NULL, NULL);
6921 gcc_assert (for_stmt != NULL_TREE);
6922 gimplify_omp_ctxp->combined_loop = true;
6925 for_body = NULL;
6926 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6927 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6928 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6929 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6930 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6932 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6933 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6934 decl = TREE_OPERAND (t, 0);
6935 gcc_assert (DECL_P (decl));
6936 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6937 || POINTER_TYPE_P (TREE_TYPE (decl)));
6939 /* Make sure the iteration variable is private. */
6940 tree c = NULL_TREE;
6941 tree c2 = NULL_TREE;
6942 if (orig_for_stmt != for_stmt)
6943 /* Do this only on innermost construct for combined ones. */;
6944 else if (simd)
6946 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6947 (splay_tree_key)decl);
6948 omp_is_private (gimplify_omp_ctxp, decl,
6949 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6950 != 1));
6951 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6952 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6953 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6955 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6956 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6957 if (has_decl_expr
6958 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6959 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6960 OMP_CLAUSE_DECL (c) = decl;
6961 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6962 OMP_FOR_CLAUSES (for_stmt) = c;
6963 omp_add_variable (gimplify_omp_ctxp, decl,
6964 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6966 else
6968 bool lastprivate
6969 = (!has_decl_expr
6970 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6971 if (lastprivate
6972 && gimplify_omp_ctxp->outer_context
6973 && gimplify_omp_ctxp->outer_context->region_type
6974 == ORT_WORKSHARE
6975 && gimplify_omp_ctxp->outer_context->combined_loop
6976 && !gimplify_omp_ctxp->outer_context->distribute)
6978 struct gimplify_omp_ctx *outer
6979 = gimplify_omp_ctxp->outer_context;
6980 n = splay_tree_lookup (outer->variables,
6981 (splay_tree_key) decl);
6982 if (n != NULL
6983 && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6984 lastprivate = false;
6985 else if (omp_check_private (outer, decl, false))
6986 error ("lastprivate variable %qE is private in outer "
6987 "context", DECL_NAME (decl));
6988 else
6990 omp_add_variable (outer, decl,
6991 GOVD_LASTPRIVATE | GOVD_SEEN);
6992 if (outer->outer_context)
6993 omp_notice_variable (outer->outer_context, decl, true);
6996 c = build_omp_clause (input_location,
6997 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6998 : OMP_CLAUSE_PRIVATE);
6999 OMP_CLAUSE_DECL (c) = decl;
7000 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
7001 OMP_FOR_CLAUSES (for_stmt) = c;
7002 omp_add_variable (gimplify_omp_ctxp, decl,
7003 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
7004 | GOVD_EXPLICIT | GOVD_SEEN);
7005 c = NULL_TREE;
7008 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
7009 omp_notice_variable (gimplify_omp_ctxp, decl, true);
7010 else
7011 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
7013 /* If DECL is not a gimple register, create a temporary variable to act
7014 as an iteration counter. This is valid, since DECL cannot be
7015 modified in the body of the loop. Similarly for any iteration vars
7016 in simd with collapse > 1 where the iterator vars must be
7017 lastprivate. */
7018 if (orig_for_stmt != for_stmt)
7019 var = decl;
7020 else if (!is_gimple_reg (decl)
7021 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
7023 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7024 TREE_OPERAND (t, 0) = var;
7026 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
7028 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7030 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7031 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
7032 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
7033 OMP_CLAUSE_DECL (c2) = var;
7034 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
7035 OMP_FOR_CLAUSES (for_stmt) = c2;
7036 omp_add_variable (gimplify_omp_ctxp, var,
7037 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
7038 if (c == NULL_TREE)
7040 c = c2;
7041 c2 = NULL_TREE;
7044 else
7045 omp_add_variable (gimplify_omp_ctxp, var,
7046 GOVD_PRIVATE | GOVD_SEEN);
7048 else
7049 var = decl;
7051 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7052 is_gimple_val, fb_rvalue);
7053 ret = MIN (ret, tret);
7054 if (ret == GS_ERROR)
7055 return ret;
7057 /* Handle OMP_FOR_COND. */
7058 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7059 gcc_assert (COMPARISON_CLASS_P (t));
7060 gcc_assert (TREE_OPERAND (t, 0) == decl);
7062 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7063 is_gimple_val, fb_rvalue);
7064 ret = MIN (ret, tret);
7066 /* Handle OMP_FOR_INCR. */
7067 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7068 switch (TREE_CODE (t))
7070 case PREINCREMENT_EXPR:
7071 case POSTINCREMENT_EXPR:
7073 tree decl = TREE_OPERAND (t, 0);
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)));
7078 if (orig_for_stmt != for_stmt)
7079 break;
7080 t = build_int_cst (TREE_TYPE (decl), 1);
7081 if (c)
7082 OMP_CLAUSE_LINEAR_STEP (c) = t;
7083 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7084 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7085 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7086 break;
7089 case PREDECREMENT_EXPR:
7090 case POSTDECREMENT_EXPR:
7091 /* c_omp_for_incr_canonicalize_ptr() should have been
7092 called to massage things appropriately. */
7093 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7094 if (orig_for_stmt != for_stmt)
7095 break;
7096 t = build_int_cst (TREE_TYPE (decl), -1);
7097 if (c)
7098 OMP_CLAUSE_LINEAR_STEP (c) = t;
7099 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7100 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7101 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7102 break;
7104 case MODIFY_EXPR:
7105 gcc_assert (TREE_OPERAND (t, 0) == decl);
7106 TREE_OPERAND (t, 0) = var;
7108 t = TREE_OPERAND (t, 1);
7109 switch (TREE_CODE (t))
7111 case PLUS_EXPR:
7112 if (TREE_OPERAND (t, 1) == decl)
7114 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
7115 TREE_OPERAND (t, 0) = var;
7116 break;
7119 /* Fallthru. */
7120 case MINUS_EXPR:
7121 case POINTER_PLUS_EXPR:
7122 gcc_assert (TREE_OPERAND (t, 0) == decl);
7123 TREE_OPERAND (t, 0) = var;
7124 break;
7125 default:
7126 gcc_unreachable ();
7129 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7130 is_gimple_val, fb_rvalue);
7131 ret = MIN (ret, tret);
7132 if (c)
7134 tree step = TREE_OPERAND (t, 1);
7135 tree stept = TREE_TYPE (decl);
7136 if (POINTER_TYPE_P (stept))
7137 stept = sizetype;
7138 step = fold_convert (stept, step);
7139 if (TREE_CODE (t) == MINUS_EXPR)
7140 step = fold_build1 (NEGATE_EXPR, stept, step);
7141 OMP_CLAUSE_LINEAR_STEP (c) = step;
7142 if (step != TREE_OPERAND (t, 1))
7144 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7145 &for_pre_body, NULL,
7146 is_gimple_val, fb_rvalue);
7147 ret = MIN (ret, tret);
7150 break;
7152 default:
7153 gcc_unreachable ();
7156 if (c2)
7158 gcc_assert (c);
7159 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7162 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7163 && orig_for_stmt == for_stmt)
7165 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7166 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7167 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7168 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7169 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7170 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7171 && OMP_CLAUSE_DECL (c) == decl)
7173 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7174 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7175 gcc_assert (TREE_OPERAND (t, 0) == var);
7176 t = TREE_OPERAND (t, 1);
7177 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7178 || TREE_CODE (t) == MINUS_EXPR
7179 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7180 gcc_assert (TREE_OPERAND (t, 0) == var);
7181 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7182 TREE_OPERAND (t, 1));
7183 gimple_seq *seq;
7184 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7185 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7186 else
7187 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7188 gimplify_assign (decl, t, seq);
7193 BITMAP_FREE (has_decl_expr);
7195 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7197 if (orig_for_stmt != for_stmt)
7198 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7200 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7201 decl = TREE_OPERAND (t, 0);
7202 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7203 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7204 TREE_OPERAND (t, 0) = var;
7205 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7206 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7207 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7210 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7212 int kind;
7213 switch (TREE_CODE (orig_for_stmt))
7215 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7216 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7217 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7218 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7219 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7220 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
7221 default:
7222 gcc_unreachable ();
7224 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7225 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7226 for_pre_body);
7227 if (orig_for_stmt != for_stmt)
7228 gimple_omp_for_set_combined_p (gfor, true);
7229 if (gimplify_omp_ctxp
7230 && (gimplify_omp_ctxp->combined_loop
7231 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7232 && gimplify_omp_ctxp->outer_context
7233 && gimplify_omp_ctxp->outer_context->combined_loop)))
7235 gimple_omp_for_set_combined_into_p (gfor, true);
7236 if (gimplify_omp_ctxp->combined_loop)
7237 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7238 else
7239 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7242 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7244 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7245 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7246 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7247 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7248 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7249 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7250 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7251 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7254 gimplify_seq_add_stmt (pre_p, gfor);
7255 if (ret != GS_ALL_DONE)
7256 return GS_ERROR;
7257 *expr_p = NULL_TREE;
7258 return GS_ALL_DONE;
7261 /* Gimplify the gross structure of several OpenACC or OpenMP constructs. */
7263 static void
7264 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7266 tree expr = *expr_p;
7267 gimple stmt;
7268 gimple_seq body = NULL;
7269 enum omp_region_type ort;
7271 switch (TREE_CODE (expr))
7273 case OMP_SECTIONS:
7274 case OMP_SINGLE:
7275 ort = ORT_WORKSHARE;
7276 break;
7277 case OACC_KERNELS:
7278 case OACC_PARALLEL:
7279 case OMP_TARGET:
7280 ort = (enum omp_region_type) (ORT_TARGET | ORT_TARGET_OFFLOAD);
7281 break;
7282 case OACC_DATA:
7283 case OMP_TARGET_DATA:
7284 ort = ORT_TARGET;
7285 break;
7286 case OMP_TEAMS:
7287 ort = ORT_TEAMS;
7288 break;
7289 default:
7290 gcc_unreachable ();
7292 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7293 if (ort & ORT_TARGET)
7295 push_gimplify_context ();
7296 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7297 if (gimple_code (g) == GIMPLE_BIND)
7298 pop_gimplify_context (g);
7299 else
7300 pop_gimplify_context (NULL);
7301 if (!(ort & ORT_TARGET_OFFLOAD))
7303 enum built_in_function end_ix;
7304 switch (TREE_CODE (expr))
7306 case OACC_DATA:
7307 end_ix = BUILT_IN_GOACC_DATA_END;
7308 break;
7309 case OMP_TARGET_DATA:
7310 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
7311 break;
7312 default:
7313 gcc_unreachable ();
7315 tree fn = builtin_decl_explicit (end_ix);
7316 g = gimple_build_call (fn, 0);
7317 gimple_seq cleanup = NULL;
7318 gimple_seq_add_stmt (&cleanup, g);
7319 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7320 body = NULL;
7321 gimple_seq_add_stmt (&body, g);
7324 else
7325 gimplify_and_add (OMP_BODY (expr), &body);
7326 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7328 switch (TREE_CODE (expr))
7330 case OACC_DATA:
7331 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
7332 OACC_DATA_CLAUSES (expr));
7333 break;
7334 case OACC_KERNELS:
7335 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
7336 OMP_CLAUSES (expr));
7337 break;
7338 case OACC_PARALLEL:
7339 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
7340 OMP_CLAUSES (expr));
7341 break;
7342 case OMP_SECTIONS:
7343 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7344 break;
7345 case OMP_SINGLE:
7346 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7347 break;
7348 case OMP_TARGET:
7349 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7350 OMP_CLAUSES (expr));
7351 break;
7352 case OMP_TARGET_DATA:
7353 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7354 OMP_CLAUSES (expr));
7355 break;
7356 case OMP_TEAMS:
7357 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7358 break;
7359 default:
7360 gcc_unreachable ();
7363 gimplify_seq_add_stmt (pre_p, stmt);
7364 *expr_p = NULL_TREE;
7367 /* Gimplify the gross structure of OpenACC update and OpenMP target update
7368 constructs. */
7370 static void
7371 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7373 tree expr = *expr_p, clauses;
7374 int kind;
7375 gomp_target *stmt;
7377 switch (TREE_CODE (expr))
7379 case OACC_ENTER_DATA:
7380 clauses = OACC_ENTER_DATA_CLAUSES (expr);
7381 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7382 break;
7383 case OACC_EXIT_DATA:
7384 clauses = OACC_EXIT_DATA_CLAUSES (expr);
7385 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7386 break;
7387 case OACC_UPDATE:
7388 clauses = OACC_UPDATE_CLAUSES (expr);
7389 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
7390 break;
7391 case OMP_TARGET_UPDATE:
7392 clauses = OMP_TARGET_UPDATE_CLAUSES (expr);
7393 kind = GF_OMP_TARGET_KIND_UPDATE;
7394 break;
7395 default:
7396 gcc_unreachable ();
7398 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_WORKSHARE);
7399 gimplify_adjust_omp_clauses (pre_p, &clauses);
7400 stmt = gimple_build_omp_target (NULL, kind, clauses);
7402 gimplify_seq_add_stmt (pre_p, stmt);
7403 *expr_p = NULL_TREE;
7406 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7407 stabilized the lhs of the atomic operation as *ADDR. Return true if
7408 EXPR is this stabilized form. */
7410 static bool
7411 goa_lhs_expr_p (tree expr, tree addr)
7413 /* Also include casts to other type variants. The C front end is fond
7414 of adding these for e.g. volatile variables. This is like
7415 STRIP_TYPE_NOPS but includes the main variant lookup. */
7416 STRIP_USELESS_TYPE_CONVERSION (expr);
7418 if (TREE_CODE (expr) == INDIRECT_REF)
7420 expr = TREE_OPERAND (expr, 0);
7421 while (expr != addr
7422 && (CONVERT_EXPR_P (expr)
7423 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7424 && TREE_CODE (expr) == TREE_CODE (addr)
7425 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7427 expr = TREE_OPERAND (expr, 0);
7428 addr = TREE_OPERAND (addr, 0);
7430 if (expr == addr)
7431 return true;
7432 return (TREE_CODE (addr) == ADDR_EXPR
7433 && TREE_CODE (expr) == ADDR_EXPR
7434 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7436 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7437 return true;
7438 return false;
7441 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7442 expression does not involve the lhs, evaluate it into a temporary.
7443 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7444 or -1 if an error was encountered. */
7446 static int
7447 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7448 tree lhs_var)
7450 tree expr = *expr_p;
7451 int saw_lhs;
7453 if (goa_lhs_expr_p (expr, lhs_addr))
7455 *expr_p = lhs_var;
7456 return 1;
7458 if (is_gimple_val (expr))
7459 return 0;
7461 saw_lhs = 0;
7462 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7464 case tcc_binary:
7465 case tcc_comparison:
7466 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7467 lhs_var);
7468 case tcc_unary:
7469 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7470 lhs_var);
7471 break;
7472 case tcc_expression:
7473 switch (TREE_CODE (expr))
7475 case TRUTH_ANDIF_EXPR:
7476 case TRUTH_ORIF_EXPR:
7477 case TRUTH_AND_EXPR:
7478 case TRUTH_OR_EXPR:
7479 case TRUTH_XOR_EXPR:
7480 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7481 lhs_addr, lhs_var);
7482 case TRUTH_NOT_EXPR:
7483 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7484 lhs_addr, lhs_var);
7485 break;
7486 case COMPOUND_EXPR:
7487 /* Break out any preevaluations from cp_build_modify_expr. */
7488 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7489 expr = TREE_OPERAND (expr, 1))
7490 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7491 *expr_p = expr;
7492 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7493 default:
7494 break;
7496 break;
7497 default:
7498 break;
7501 if (saw_lhs == 0)
7503 enum gimplify_status gs;
7504 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7505 if (gs != GS_ALL_DONE)
7506 saw_lhs = -1;
7509 return saw_lhs;
7512 /* Gimplify an OMP_ATOMIC statement. */
7514 static enum gimplify_status
7515 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7517 tree addr = TREE_OPERAND (*expr_p, 0);
7518 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7519 ? NULL : TREE_OPERAND (*expr_p, 1);
7520 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7521 tree tmp_load;
7522 gomp_atomic_load *loadstmt;
7523 gomp_atomic_store *storestmt;
7525 tmp_load = create_tmp_reg (type);
7526 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7527 return GS_ERROR;
7529 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7530 != GS_ALL_DONE)
7531 return GS_ERROR;
7533 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7534 gimplify_seq_add_stmt (pre_p, loadstmt);
7535 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7536 != GS_ALL_DONE)
7537 return GS_ERROR;
7539 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7540 rhs = tmp_load;
7541 storestmt = gimple_build_omp_atomic_store (rhs);
7542 gimplify_seq_add_stmt (pre_p, storestmt);
7543 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7545 gimple_omp_atomic_set_seq_cst (loadstmt);
7546 gimple_omp_atomic_set_seq_cst (storestmt);
7548 switch (TREE_CODE (*expr_p))
7550 case OMP_ATOMIC_READ:
7551 case OMP_ATOMIC_CAPTURE_OLD:
7552 *expr_p = tmp_load;
7553 gimple_omp_atomic_set_need_value (loadstmt);
7554 break;
7555 case OMP_ATOMIC_CAPTURE_NEW:
7556 *expr_p = rhs;
7557 gimple_omp_atomic_set_need_value (storestmt);
7558 break;
7559 default:
7560 *expr_p = NULL;
7561 break;
7564 return GS_ALL_DONE;
7567 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7568 body, and adding some EH bits. */
7570 static enum gimplify_status
7571 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7573 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7574 gimple body_stmt;
7575 gtransaction *trans_stmt;
7576 gimple_seq body = NULL;
7577 int subcode = 0;
7579 /* Wrap the transaction body in a BIND_EXPR so we have a context
7580 where to put decls for OpenMP. */
7581 if (TREE_CODE (tbody) != BIND_EXPR)
7583 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7584 TREE_SIDE_EFFECTS (bind) = 1;
7585 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7586 TRANSACTION_EXPR_BODY (expr) = bind;
7589 push_gimplify_context ();
7590 temp = voidify_wrapper_expr (*expr_p, NULL);
7592 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7593 pop_gimplify_context (body_stmt);
7595 trans_stmt = gimple_build_transaction (body, NULL);
7596 if (TRANSACTION_EXPR_OUTER (expr))
7597 subcode = GTMA_IS_OUTER;
7598 else if (TRANSACTION_EXPR_RELAXED (expr))
7599 subcode = GTMA_IS_RELAXED;
7600 gimple_transaction_set_subcode (trans_stmt, subcode);
7602 gimplify_seq_add_stmt (pre_p, trans_stmt);
7604 if (temp)
7606 *expr_p = temp;
7607 return GS_OK;
7610 *expr_p = NULL_TREE;
7611 return GS_ALL_DONE;
7614 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7615 expression produces a value to be used as an operand inside a GIMPLE
7616 statement, the value will be stored back in *EXPR_P. This value will
7617 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7618 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7619 emitted in PRE_P and POST_P.
7621 Additionally, this process may overwrite parts of the input
7622 expression during gimplification. Ideally, it should be
7623 possible to do non-destructive gimplification.
7625 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7626 the expression needs to evaluate to a value to be used as
7627 an operand in a GIMPLE statement, this value will be stored in
7628 *EXPR_P on exit. This happens when the caller specifies one
7629 of fb_lvalue or fb_rvalue fallback flags.
7631 PRE_P will contain the sequence of GIMPLE statements corresponding
7632 to the evaluation of EXPR and all the side-effects that must
7633 be executed before the main expression. On exit, the last
7634 statement of PRE_P is the core statement being gimplified. For
7635 instance, when gimplifying 'if (++a)' the last statement in
7636 PRE_P will be 'if (t.1)' where t.1 is the result of
7637 pre-incrementing 'a'.
7639 POST_P will contain the sequence of GIMPLE statements corresponding
7640 to the evaluation of all the side-effects that must be executed
7641 after the main expression. If this is NULL, the post
7642 side-effects are stored at the end of PRE_P.
7644 The reason why the output is split in two is to handle post
7645 side-effects explicitly. In some cases, an expression may have
7646 inner and outer post side-effects which need to be emitted in
7647 an order different from the one given by the recursive
7648 traversal. For instance, for the expression (*p--)++ the post
7649 side-effects of '--' must actually occur *after* the post
7650 side-effects of '++'. However, gimplification will first visit
7651 the inner expression, so if a separate POST sequence was not
7652 used, the resulting sequence would be:
7654 1 t.1 = *p
7655 2 p = p - 1
7656 3 t.2 = t.1 + 1
7657 4 *p = t.2
7659 However, the post-decrement operation in line #2 must not be
7660 evaluated until after the store to *p at line #4, so the
7661 correct sequence should be:
7663 1 t.1 = *p
7664 2 t.2 = t.1 + 1
7665 3 *p = t.2
7666 4 p = p - 1
7668 So, by specifying a separate post queue, it is possible
7669 to emit the post side-effects in the correct order.
7670 If POST_P is NULL, an internal queue will be used. Before
7671 returning to the caller, the sequence POST_P is appended to
7672 the main output sequence PRE_P.
7674 GIMPLE_TEST_F points to a function that takes a tree T and
7675 returns nonzero if T is in the GIMPLE form requested by the
7676 caller. The GIMPLE predicates are in gimple.c.
7678 FALLBACK tells the function what sort of a temporary we want if
7679 gimplification cannot produce an expression that complies with
7680 GIMPLE_TEST_F.
7682 fb_none means that no temporary should be generated
7683 fb_rvalue means that an rvalue is OK to generate
7684 fb_lvalue means that an lvalue is OK to generate
7685 fb_either means that either is OK, but an lvalue is preferable.
7686 fb_mayfail means that gimplification may fail (in which case
7687 GS_ERROR will be returned)
7689 The return value is either GS_ERROR or GS_ALL_DONE, since this
7690 function iterates until EXPR is completely gimplified or an error
7691 occurs. */
7693 enum gimplify_status
7694 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7695 bool (*gimple_test_f) (tree), fallback_t fallback)
7697 tree tmp;
7698 gimple_seq internal_pre = NULL;
7699 gimple_seq internal_post = NULL;
7700 tree save_expr;
7701 bool is_statement;
7702 location_t saved_location;
7703 enum gimplify_status ret;
7704 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7706 save_expr = *expr_p;
7707 if (save_expr == NULL_TREE)
7708 return GS_ALL_DONE;
7710 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7711 is_statement = gimple_test_f == is_gimple_stmt;
7712 if (is_statement)
7713 gcc_assert (pre_p);
7715 /* Consistency checks. */
7716 if (gimple_test_f == is_gimple_reg)
7717 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7718 else if (gimple_test_f == is_gimple_val
7719 || gimple_test_f == is_gimple_call_addr
7720 || gimple_test_f == is_gimple_condexpr
7721 || gimple_test_f == is_gimple_mem_rhs
7722 || gimple_test_f == is_gimple_mem_rhs_or_call
7723 || gimple_test_f == is_gimple_reg_rhs
7724 || gimple_test_f == is_gimple_reg_rhs_or_call
7725 || gimple_test_f == is_gimple_asm_val
7726 || gimple_test_f == is_gimple_mem_ref_addr)
7727 gcc_assert (fallback & fb_rvalue);
7728 else if (gimple_test_f == is_gimple_min_lval
7729 || gimple_test_f == is_gimple_lvalue)
7730 gcc_assert (fallback & fb_lvalue);
7731 else if (gimple_test_f == is_gimple_addressable)
7732 gcc_assert (fallback & fb_either);
7733 else if (gimple_test_f == is_gimple_stmt)
7734 gcc_assert (fallback == fb_none);
7735 else
7737 /* We should have recognized the GIMPLE_TEST_F predicate to
7738 know what kind of fallback to use in case a temporary is
7739 needed to hold the value or address of *EXPR_P. */
7740 gcc_unreachable ();
7743 /* We used to check the predicate here and return immediately if it
7744 succeeds. This is wrong; the design is for gimplification to be
7745 idempotent, and for the predicates to only test for valid forms, not
7746 whether they are fully simplified. */
7747 if (pre_p == NULL)
7748 pre_p = &internal_pre;
7750 if (post_p == NULL)
7751 post_p = &internal_post;
7753 /* Remember the last statements added to PRE_P and POST_P. Every
7754 new statement added by the gimplification helpers needs to be
7755 annotated with location information. To centralize the
7756 responsibility, we remember the last statement that had been
7757 added to both queues before gimplifying *EXPR_P. If
7758 gimplification produces new statements in PRE_P and POST_P, those
7759 statements will be annotated with the same location information
7760 as *EXPR_P. */
7761 pre_last_gsi = gsi_last (*pre_p);
7762 post_last_gsi = gsi_last (*post_p);
7764 saved_location = input_location;
7765 if (save_expr != error_mark_node
7766 && EXPR_HAS_LOCATION (*expr_p))
7767 input_location = EXPR_LOCATION (*expr_p);
7769 /* Loop over the specific gimplifiers until the toplevel node
7770 remains the same. */
7773 /* Strip away as many useless type conversions as possible
7774 at the toplevel. */
7775 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7777 /* Remember the expr. */
7778 save_expr = *expr_p;
7780 /* Die, die, die, my darling. */
7781 if (save_expr == error_mark_node
7782 || (TREE_TYPE (save_expr)
7783 && TREE_TYPE (save_expr) == error_mark_node))
7785 ret = GS_ERROR;
7786 break;
7789 /* Do any language-specific gimplification. */
7790 ret = ((enum gimplify_status)
7791 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7792 if (ret == GS_OK)
7794 if (*expr_p == NULL_TREE)
7795 break;
7796 if (*expr_p != save_expr)
7797 continue;
7799 else if (ret != GS_UNHANDLED)
7800 break;
7802 /* Make sure that all the cases set 'ret' appropriately. */
7803 ret = GS_UNHANDLED;
7804 switch (TREE_CODE (*expr_p))
7806 /* First deal with the special cases. */
7808 case POSTINCREMENT_EXPR:
7809 case POSTDECREMENT_EXPR:
7810 case PREINCREMENT_EXPR:
7811 case PREDECREMENT_EXPR:
7812 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7813 fallback != fb_none,
7814 TREE_TYPE (*expr_p));
7815 break;
7817 case VIEW_CONVERT_EXPR:
7818 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7819 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7821 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7822 post_p, is_gimple_val, fb_rvalue);
7823 recalculate_side_effects (*expr_p);
7824 break;
7826 /* Fallthru. */
7828 case ARRAY_REF:
7829 case ARRAY_RANGE_REF:
7830 case REALPART_EXPR:
7831 case IMAGPART_EXPR:
7832 case COMPONENT_REF:
7833 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7834 fallback ? fallback : fb_rvalue);
7835 break;
7837 case COND_EXPR:
7838 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7840 /* C99 code may assign to an array in a structure value of a
7841 conditional expression, and this has undefined behavior
7842 only on execution, so create a temporary if an lvalue is
7843 required. */
7844 if (fallback == fb_lvalue)
7846 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7847 mark_addressable (*expr_p);
7848 ret = GS_OK;
7850 break;
7852 case CALL_EXPR:
7853 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7855 /* C99 code may assign to an array in a structure returned
7856 from a function, and this has undefined behavior only on
7857 execution, so create a temporary if an lvalue is
7858 required. */
7859 if (fallback == fb_lvalue)
7861 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7862 mark_addressable (*expr_p);
7863 ret = GS_OK;
7865 break;
7867 case TREE_LIST:
7868 gcc_unreachable ();
7870 case COMPOUND_EXPR:
7871 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7872 break;
7874 case COMPOUND_LITERAL_EXPR:
7875 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7876 gimple_test_f, fallback);
7877 break;
7879 case MODIFY_EXPR:
7880 case INIT_EXPR:
7881 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7882 fallback != fb_none);
7883 break;
7885 case TRUTH_ANDIF_EXPR:
7886 case TRUTH_ORIF_EXPR:
7888 /* Preserve the original type of the expression and the
7889 source location of the outer expression. */
7890 tree org_type = TREE_TYPE (*expr_p);
7891 *expr_p = gimple_boolify (*expr_p);
7892 *expr_p = build3_loc (input_location, COND_EXPR,
7893 org_type, *expr_p,
7894 fold_convert_loc
7895 (input_location,
7896 org_type, boolean_true_node),
7897 fold_convert_loc
7898 (input_location,
7899 org_type, boolean_false_node));
7900 ret = GS_OK;
7901 break;
7904 case TRUTH_NOT_EXPR:
7906 tree type = TREE_TYPE (*expr_p);
7907 /* The parsers are careful to generate TRUTH_NOT_EXPR
7908 only with operands that are always zero or one.
7909 We do not fold here but handle the only interesting case
7910 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7911 *expr_p = gimple_boolify (*expr_p);
7912 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7913 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7914 TREE_TYPE (*expr_p),
7915 TREE_OPERAND (*expr_p, 0));
7916 else
7917 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7918 TREE_TYPE (*expr_p),
7919 TREE_OPERAND (*expr_p, 0),
7920 build_int_cst (TREE_TYPE (*expr_p), 1));
7921 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7922 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7923 ret = GS_OK;
7924 break;
7927 case ADDR_EXPR:
7928 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7929 break;
7931 case ANNOTATE_EXPR:
7933 tree cond = TREE_OPERAND (*expr_p, 0);
7934 tree kind = TREE_OPERAND (*expr_p, 1);
7935 tree type = TREE_TYPE (cond);
7936 if (!INTEGRAL_TYPE_P (type))
7938 *expr_p = cond;
7939 ret = GS_OK;
7940 break;
7942 tree tmp = create_tmp_var (type);
7943 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7944 gcall *call
7945 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7946 gimple_call_set_lhs (call, tmp);
7947 gimplify_seq_add_stmt (pre_p, call);
7948 *expr_p = tmp;
7949 ret = GS_ALL_DONE;
7950 break;
7953 case VA_ARG_EXPR:
7954 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7955 break;
7957 CASE_CONVERT:
7958 if (IS_EMPTY_STMT (*expr_p))
7960 ret = GS_ALL_DONE;
7961 break;
7964 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7965 || fallback == fb_none)
7967 /* Just strip a conversion to void (or in void context) and
7968 try again. */
7969 *expr_p = TREE_OPERAND (*expr_p, 0);
7970 ret = GS_OK;
7971 break;
7974 ret = gimplify_conversion (expr_p);
7975 if (ret == GS_ERROR)
7976 break;
7977 if (*expr_p != save_expr)
7978 break;
7979 /* FALLTHRU */
7981 case FIX_TRUNC_EXPR:
7982 /* unary_expr: ... | '(' cast ')' val | ... */
7983 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7984 is_gimple_val, fb_rvalue);
7985 recalculate_side_effects (*expr_p);
7986 break;
7988 case INDIRECT_REF:
7990 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7991 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7992 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7994 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7995 if (*expr_p != save_expr)
7997 ret = GS_OK;
7998 break;
8001 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8002 is_gimple_reg, fb_rvalue);
8003 if (ret == GS_ERROR)
8004 break;
8006 recalculate_side_effects (*expr_p);
8007 *expr_p = fold_build2_loc (input_location, MEM_REF,
8008 TREE_TYPE (*expr_p),
8009 TREE_OPERAND (*expr_p, 0),
8010 build_int_cst (saved_ptr_type, 0));
8011 TREE_THIS_VOLATILE (*expr_p) = volatilep;
8012 TREE_THIS_NOTRAP (*expr_p) = notrap;
8013 ret = GS_OK;
8014 break;
8017 /* We arrive here through the various re-gimplifcation paths. */
8018 case MEM_REF:
8019 /* First try re-folding the whole thing. */
8020 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
8021 TREE_OPERAND (*expr_p, 0),
8022 TREE_OPERAND (*expr_p, 1));
8023 if (tmp)
8025 *expr_p = tmp;
8026 recalculate_side_effects (*expr_p);
8027 ret = GS_OK;
8028 break;
8030 /* Avoid re-gimplifying the address operand if it is already
8031 in suitable form. Re-gimplifying would mark the address
8032 operand addressable. Always gimplify when not in SSA form
8033 as we still may have to gimplify decls with value-exprs. */
8034 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
8035 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
8037 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8038 is_gimple_mem_ref_addr, fb_rvalue);
8039 if (ret == GS_ERROR)
8040 break;
8042 recalculate_side_effects (*expr_p);
8043 ret = GS_ALL_DONE;
8044 break;
8046 /* Constants need not be gimplified. */
8047 case INTEGER_CST:
8048 case REAL_CST:
8049 case FIXED_CST:
8050 case STRING_CST:
8051 case COMPLEX_CST:
8052 case VECTOR_CST:
8053 /* Drop the overflow flag on constants, we do not want
8054 that in the GIMPLE IL. */
8055 if (TREE_OVERFLOW_P (*expr_p))
8056 *expr_p = drop_tree_overflow (*expr_p);
8057 ret = GS_ALL_DONE;
8058 break;
8060 case CONST_DECL:
8061 /* If we require an lvalue, such as for ADDR_EXPR, retain the
8062 CONST_DECL node. Otherwise the decl is replaceable by its
8063 value. */
8064 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
8065 if (fallback & fb_lvalue)
8066 ret = GS_ALL_DONE;
8067 else
8069 *expr_p = DECL_INITIAL (*expr_p);
8070 ret = GS_OK;
8072 break;
8074 case DECL_EXPR:
8075 ret = gimplify_decl_expr (expr_p, pre_p);
8076 break;
8078 case BIND_EXPR:
8079 ret = gimplify_bind_expr (expr_p, pre_p);
8080 break;
8082 case LOOP_EXPR:
8083 ret = gimplify_loop_expr (expr_p, pre_p);
8084 break;
8086 case SWITCH_EXPR:
8087 ret = gimplify_switch_expr (expr_p, pre_p);
8088 break;
8090 case EXIT_EXPR:
8091 ret = gimplify_exit_expr (expr_p);
8092 break;
8094 case GOTO_EXPR:
8095 /* If the target is not LABEL, then it is a computed jump
8096 and the target needs to be gimplified. */
8097 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
8099 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
8100 NULL, is_gimple_val, fb_rvalue);
8101 if (ret == GS_ERROR)
8102 break;
8104 gimplify_seq_add_stmt (pre_p,
8105 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
8106 ret = GS_ALL_DONE;
8107 break;
8109 case PREDICT_EXPR:
8110 gimplify_seq_add_stmt (pre_p,
8111 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
8112 PREDICT_EXPR_OUTCOME (*expr_p)));
8113 ret = GS_ALL_DONE;
8114 break;
8116 case LABEL_EXPR:
8117 ret = GS_ALL_DONE;
8118 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
8119 == current_function_decl);
8120 gimplify_seq_add_stmt (pre_p,
8121 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
8122 break;
8124 case CASE_LABEL_EXPR:
8125 ret = gimplify_case_label_expr (expr_p, pre_p);
8126 break;
8128 case RETURN_EXPR:
8129 ret = gimplify_return_expr (*expr_p, pre_p);
8130 break;
8132 case CONSTRUCTOR:
8133 /* Don't reduce this in place; let gimplify_init_constructor work its
8134 magic. Buf if we're just elaborating this for side effects, just
8135 gimplify any element that has side-effects. */
8136 if (fallback == fb_none)
8138 unsigned HOST_WIDE_INT ix;
8139 tree val;
8140 tree temp = NULL_TREE;
8141 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
8142 if (TREE_SIDE_EFFECTS (val))
8143 append_to_statement_list (val, &temp);
8145 *expr_p = temp;
8146 ret = temp ? GS_OK : GS_ALL_DONE;
8148 /* C99 code may assign to an array in a constructed
8149 structure or union, and this has undefined behavior only
8150 on execution, so create a temporary if an lvalue is
8151 required. */
8152 else if (fallback == fb_lvalue)
8154 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8155 mark_addressable (*expr_p);
8156 ret = GS_OK;
8158 else
8159 ret = GS_ALL_DONE;
8160 break;
8162 /* The following are special cases that are not handled by the
8163 original GIMPLE grammar. */
8165 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
8166 eliminated. */
8167 case SAVE_EXPR:
8168 ret = gimplify_save_expr (expr_p, pre_p, post_p);
8169 break;
8171 case BIT_FIELD_REF:
8172 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8173 post_p, is_gimple_lvalue, fb_either);
8174 recalculate_side_effects (*expr_p);
8175 break;
8177 case TARGET_MEM_REF:
8179 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
8181 if (TMR_BASE (*expr_p))
8182 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
8183 post_p, is_gimple_mem_ref_addr, fb_either);
8184 if (TMR_INDEX (*expr_p))
8185 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
8186 post_p, is_gimple_val, fb_rvalue);
8187 if (TMR_INDEX2 (*expr_p))
8188 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
8189 post_p, is_gimple_val, fb_rvalue);
8190 /* TMR_STEP and TMR_OFFSET are always integer constants. */
8191 ret = MIN (r0, r1);
8193 break;
8195 case NON_LVALUE_EXPR:
8196 /* This should have been stripped above. */
8197 gcc_unreachable ();
8199 case ASM_EXPR:
8200 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
8201 break;
8203 case TRY_FINALLY_EXPR:
8204 case TRY_CATCH_EXPR:
8206 gimple_seq eval, cleanup;
8207 gtry *try_;
8209 /* Calls to destructors are generated automatically in FINALLY/CATCH
8210 block. They should have location as UNKNOWN_LOCATION. However,
8211 gimplify_call_expr will reset these call stmts to input_location
8212 if it finds stmt's location is unknown. To prevent resetting for
8213 destructors, we set the input_location to unknown.
8214 Note that this only affects the destructor calls in FINALLY/CATCH
8215 block, and will automatically reset to its original value by the
8216 end of gimplify_expr. */
8217 input_location = UNKNOWN_LOCATION;
8218 eval = cleanup = NULL;
8219 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8220 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8221 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8222 if (gimple_seq_empty_p (cleanup))
8224 gimple_seq_add_seq (pre_p, eval);
8225 ret = GS_ALL_DONE;
8226 break;
8228 try_ = gimple_build_try (eval, cleanup,
8229 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8230 ? GIMPLE_TRY_FINALLY
8231 : GIMPLE_TRY_CATCH);
8232 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8233 gimple_set_location (try_, saved_location);
8234 else
8235 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8236 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8237 gimple_try_set_catch_is_cleanup (try_,
8238 TRY_CATCH_IS_CLEANUP (*expr_p));
8239 gimplify_seq_add_stmt (pre_p, try_);
8240 ret = GS_ALL_DONE;
8241 break;
8244 case CLEANUP_POINT_EXPR:
8245 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8246 break;
8248 case TARGET_EXPR:
8249 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8250 break;
8252 case CATCH_EXPR:
8254 gimple c;
8255 gimple_seq handler = NULL;
8256 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8257 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8258 gimplify_seq_add_stmt (pre_p, c);
8259 ret = GS_ALL_DONE;
8260 break;
8263 case EH_FILTER_EXPR:
8265 gimple ehf;
8266 gimple_seq failure = NULL;
8268 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8269 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8270 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8271 gimplify_seq_add_stmt (pre_p, ehf);
8272 ret = GS_ALL_DONE;
8273 break;
8276 case OBJ_TYPE_REF:
8278 enum gimplify_status r0, r1;
8279 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8280 post_p, is_gimple_val, fb_rvalue);
8281 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8282 post_p, is_gimple_val, fb_rvalue);
8283 TREE_SIDE_EFFECTS (*expr_p) = 0;
8284 ret = MIN (r0, r1);
8286 break;
8288 case LABEL_DECL:
8289 /* We get here when taking the address of a label. We mark
8290 the label as "forced"; meaning it can never be removed and
8291 it is a potential target for any computed goto. */
8292 FORCED_LABEL (*expr_p) = 1;
8293 ret = GS_ALL_DONE;
8294 break;
8296 case STATEMENT_LIST:
8297 ret = gimplify_statement_list (expr_p, pre_p);
8298 break;
8300 case WITH_SIZE_EXPR:
8302 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8303 post_p == &internal_post ? NULL : post_p,
8304 gimple_test_f, fallback);
8305 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8306 is_gimple_val, fb_rvalue);
8307 ret = GS_ALL_DONE;
8309 break;
8311 case VAR_DECL:
8312 case PARM_DECL:
8313 ret = gimplify_var_or_parm_decl (expr_p);
8314 break;
8316 case RESULT_DECL:
8317 /* When within an OpenMP context, notice uses of variables. */
8318 if (gimplify_omp_ctxp)
8319 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8320 ret = GS_ALL_DONE;
8321 break;
8323 case SSA_NAME:
8324 /* Allow callbacks into the gimplifier during optimization. */
8325 ret = GS_ALL_DONE;
8326 break;
8328 case OACC_HOST_DATA:
8329 case OACC_DECLARE:
8330 sorry ("directive not yet implemented");
8331 ret = GS_ALL_DONE;
8332 break;
8334 case OMP_PARALLEL:
8335 gimplify_omp_parallel (expr_p, pre_p);
8336 ret = GS_ALL_DONE;
8337 break;
8339 case OMP_TASK:
8340 gimplify_omp_task (expr_p, pre_p);
8341 ret = GS_ALL_DONE;
8342 break;
8344 case OMP_FOR:
8345 case OMP_SIMD:
8346 case CILK_SIMD:
8347 case CILK_FOR:
8348 case OMP_DISTRIBUTE:
8349 case OACC_LOOP:
8350 ret = gimplify_omp_for (expr_p, pre_p);
8351 break;
8353 case OACC_KERNELS:
8354 if (OACC_KERNELS_COMBINED (*expr_p))
8355 sorry ("directive not yet implemented");
8356 else
8357 gimplify_omp_workshare (expr_p, pre_p);
8358 ret = GS_ALL_DONE;
8359 break;
8361 case OACC_PARALLEL:
8362 if (OACC_PARALLEL_COMBINED (*expr_p))
8363 sorry ("directive not yet implemented");
8364 else
8365 gimplify_omp_workshare (expr_p, pre_p);
8366 ret = GS_ALL_DONE;
8367 break;
8369 case OACC_CACHE:
8370 gimplify_oacc_cache (expr_p, pre_p);
8371 ret = GS_ALL_DONE;
8372 break;
8374 case OACC_DATA:
8375 case OMP_SECTIONS:
8376 case OMP_SINGLE:
8377 case OMP_TARGET:
8378 case OMP_TARGET_DATA:
8379 case OMP_TEAMS:
8380 gimplify_omp_workshare (expr_p, pre_p);
8381 ret = GS_ALL_DONE;
8382 break;
8384 case OACC_UPDATE:
8385 case OMP_TARGET_UPDATE:
8386 case OACC_ENTER_DATA:
8387 case OACC_EXIT_DATA:
8388 gimplify_omp_target_update (expr_p, pre_p);
8389 ret = GS_ALL_DONE;
8390 break;
8392 case OMP_SECTION:
8393 case OMP_MASTER:
8394 case OMP_TASKGROUP:
8395 case OMP_ORDERED:
8396 case OMP_CRITICAL:
8398 gimple_seq body = NULL;
8399 gimple g;
8401 gimplify_and_add (OMP_BODY (*expr_p), &body);
8402 switch (TREE_CODE (*expr_p))
8404 case OMP_SECTION:
8405 g = gimple_build_omp_section (body);
8406 break;
8407 case OMP_MASTER:
8408 g = gimple_build_omp_master (body);
8409 break;
8410 case OMP_TASKGROUP:
8412 gimple_seq cleanup = NULL;
8413 tree fn
8414 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8415 g = gimple_build_call (fn, 0);
8416 gimple_seq_add_stmt (&cleanup, g);
8417 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8418 body = NULL;
8419 gimple_seq_add_stmt (&body, g);
8420 g = gimple_build_omp_taskgroup (body);
8422 break;
8423 case OMP_ORDERED:
8424 g = gimple_build_omp_ordered (body);
8425 break;
8426 case OMP_CRITICAL:
8427 g = gimple_build_omp_critical (body,
8428 OMP_CRITICAL_NAME (*expr_p));
8429 break;
8430 default:
8431 gcc_unreachable ();
8433 gimplify_seq_add_stmt (pre_p, g);
8434 ret = GS_ALL_DONE;
8435 break;
8438 case OMP_ATOMIC:
8439 case OMP_ATOMIC_READ:
8440 case OMP_ATOMIC_CAPTURE_OLD:
8441 case OMP_ATOMIC_CAPTURE_NEW:
8442 ret = gimplify_omp_atomic (expr_p, pre_p);
8443 break;
8445 case TRANSACTION_EXPR:
8446 ret = gimplify_transaction (expr_p, pre_p);
8447 break;
8449 case TRUTH_AND_EXPR:
8450 case TRUTH_OR_EXPR:
8451 case TRUTH_XOR_EXPR:
8453 tree orig_type = TREE_TYPE (*expr_p);
8454 tree new_type, xop0, xop1;
8455 *expr_p = gimple_boolify (*expr_p);
8456 new_type = TREE_TYPE (*expr_p);
8457 if (!useless_type_conversion_p (orig_type, new_type))
8459 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8460 ret = GS_OK;
8461 break;
8464 /* Boolified binary truth expressions are semantically equivalent
8465 to bitwise binary expressions. Canonicalize them to the
8466 bitwise variant. */
8467 switch (TREE_CODE (*expr_p))
8469 case TRUTH_AND_EXPR:
8470 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8471 break;
8472 case TRUTH_OR_EXPR:
8473 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8474 break;
8475 case TRUTH_XOR_EXPR:
8476 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8477 break;
8478 default:
8479 break;
8481 /* Now make sure that operands have compatible type to
8482 expression's new_type. */
8483 xop0 = TREE_OPERAND (*expr_p, 0);
8484 xop1 = TREE_OPERAND (*expr_p, 1);
8485 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8486 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8487 new_type,
8488 xop0);
8489 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8490 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8491 new_type,
8492 xop1);
8493 /* Continue classified as tcc_binary. */
8494 goto expr_2;
8497 case FMA_EXPR:
8498 case VEC_COND_EXPR:
8499 case VEC_PERM_EXPR:
8500 /* Classified as tcc_expression. */
8501 goto expr_3;
8503 case POINTER_PLUS_EXPR:
8505 enum gimplify_status r0, r1;
8506 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8507 post_p, is_gimple_val, fb_rvalue);
8508 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8509 post_p, is_gimple_val, fb_rvalue);
8510 recalculate_side_effects (*expr_p);
8511 ret = MIN (r0, r1);
8512 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8513 after gimplifying operands - this is similar to how
8514 it would be folding all gimplified stmts on creation
8515 to have them canonicalized, which is what we eventually
8516 should do anyway. */
8517 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8518 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8520 *expr_p = build_fold_addr_expr_with_type_loc
8521 (input_location,
8522 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8523 TREE_OPERAND (*expr_p, 0),
8524 fold_convert (ptr_type_node,
8525 TREE_OPERAND (*expr_p, 1))),
8526 TREE_TYPE (*expr_p));
8527 ret = MIN (ret, GS_OK);
8529 break;
8532 case CILK_SYNC_STMT:
8534 if (!fn_contains_cilk_spawn_p (cfun))
8536 error_at (EXPR_LOCATION (*expr_p),
8537 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8538 ret = GS_ERROR;
8540 else
8542 gimplify_cilk_sync (expr_p, pre_p);
8543 ret = GS_ALL_DONE;
8545 break;
8548 default:
8549 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8551 case tcc_comparison:
8552 /* Handle comparison of objects of non scalar mode aggregates
8553 with a call to memcmp. It would be nice to only have to do
8554 this for variable-sized objects, but then we'd have to allow
8555 the same nest of reference nodes we allow for MODIFY_EXPR and
8556 that's too complex.
8558 Compare scalar mode aggregates as scalar mode values. Using
8559 memcmp for them would be very inefficient at best, and is
8560 plain wrong if bitfields are involved. */
8562 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8564 /* Vector comparisons need no boolification. */
8565 if (TREE_CODE (type) == VECTOR_TYPE)
8566 goto expr_2;
8567 else if (!AGGREGATE_TYPE_P (type))
8569 tree org_type = TREE_TYPE (*expr_p);
8570 *expr_p = gimple_boolify (*expr_p);
8571 if (!useless_type_conversion_p (org_type,
8572 TREE_TYPE (*expr_p)))
8574 *expr_p = fold_convert_loc (input_location,
8575 org_type, *expr_p);
8576 ret = GS_OK;
8578 else
8579 goto expr_2;
8581 else if (TYPE_MODE (type) != BLKmode)
8582 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8583 else
8584 ret = gimplify_variable_sized_compare (expr_p);
8586 break;
8589 /* If *EXPR_P does not need to be special-cased, handle it
8590 according to its class. */
8591 case tcc_unary:
8592 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8593 post_p, is_gimple_val, fb_rvalue);
8594 break;
8596 case tcc_binary:
8597 expr_2:
8599 enum gimplify_status r0, r1;
8601 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8602 post_p, is_gimple_val, fb_rvalue);
8603 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8604 post_p, is_gimple_val, fb_rvalue);
8606 ret = MIN (r0, r1);
8607 break;
8610 expr_3:
8612 enum gimplify_status r0, r1, r2;
8614 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8615 post_p, is_gimple_val, fb_rvalue);
8616 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8617 post_p, is_gimple_val, fb_rvalue);
8618 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8619 post_p, is_gimple_val, fb_rvalue);
8621 ret = MIN (MIN (r0, r1), r2);
8622 break;
8625 case tcc_declaration:
8626 case tcc_constant:
8627 ret = GS_ALL_DONE;
8628 goto dont_recalculate;
8630 default:
8631 gcc_unreachable ();
8634 recalculate_side_effects (*expr_p);
8636 dont_recalculate:
8637 break;
8640 gcc_assert (*expr_p || ret != GS_OK);
8642 while (ret == GS_OK);
8644 /* If we encountered an error_mark somewhere nested inside, either
8645 stub out the statement or propagate the error back out. */
8646 if (ret == GS_ERROR)
8648 if (is_statement)
8649 *expr_p = NULL;
8650 goto out;
8653 /* This was only valid as a return value from the langhook, which
8654 we handled. Make sure it doesn't escape from any other context. */
8655 gcc_assert (ret != GS_UNHANDLED);
8657 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8659 /* We aren't looking for a value, and we don't have a valid
8660 statement. If it doesn't have side-effects, throw it away. */
8661 if (!TREE_SIDE_EFFECTS (*expr_p))
8662 *expr_p = NULL;
8663 else if (!TREE_THIS_VOLATILE (*expr_p))
8665 /* This is probably a _REF that contains something nested that
8666 has side effects. Recurse through the operands to find it. */
8667 enum tree_code code = TREE_CODE (*expr_p);
8669 switch (code)
8671 case COMPONENT_REF:
8672 case REALPART_EXPR:
8673 case IMAGPART_EXPR:
8674 case VIEW_CONVERT_EXPR:
8675 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8676 gimple_test_f, fallback);
8677 break;
8679 case ARRAY_REF:
8680 case ARRAY_RANGE_REF:
8681 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8682 gimple_test_f, fallback);
8683 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8684 gimple_test_f, fallback);
8685 break;
8687 default:
8688 /* Anything else with side-effects must be converted to
8689 a valid statement before we get here. */
8690 gcc_unreachable ();
8693 *expr_p = NULL;
8695 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8696 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8698 /* Historically, the compiler has treated a bare reference
8699 to a non-BLKmode volatile lvalue as forcing a load. */
8700 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8702 /* Normally, we do not want to create a temporary for a
8703 TREE_ADDRESSABLE type because such a type should not be
8704 copied by bitwise-assignment. However, we make an
8705 exception here, as all we are doing here is ensuring that
8706 we read the bytes that make up the type. We use
8707 create_tmp_var_raw because create_tmp_var will abort when
8708 given a TREE_ADDRESSABLE type. */
8709 tree tmp = create_tmp_var_raw (type, "vol");
8710 gimple_add_tmp_var (tmp);
8711 gimplify_assign (tmp, *expr_p, pre_p);
8712 *expr_p = NULL;
8714 else
8715 /* We can't do anything useful with a volatile reference to
8716 an incomplete type, so just throw it away. Likewise for
8717 a BLKmode type, since any implicit inner load should
8718 already have been turned into an explicit one by the
8719 gimplification process. */
8720 *expr_p = NULL;
8723 /* If we are gimplifying at the statement level, we're done. Tack
8724 everything together and return. */
8725 if (fallback == fb_none || is_statement)
8727 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8728 it out for GC to reclaim it. */
8729 *expr_p = NULL_TREE;
8731 if (!gimple_seq_empty_p (internal_pre)
8732 || !gimple_seq_empty_p (internal_post))
8734 gimplify_seq_add_seq (&internal_pre, internal_post);
8735 gimplify_seq_add_seq (pre_p, internal_pre);
8738 /* The result of gimplifying *EXPR_P is going to be the last few
8739 statements in *PRE_P and *POST_P. Add location information
8740 to all the statements that were added by the gimplification
8741 helpers. */
8742 if (!gimple_seq_empty_p (*pre_p))
8743 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8745 if (!gimple_seq_empty_p (*post_p))
8746 annotate_all_with_location_after (*post_p, post_last_gsi,
8747 input_location);
8749 goto out;
8752 #ifdef ENABLE_GIMPLE_CHECKING
8753 if (*expr_p)
8755 enum tree_code code = TREE_CODE (*expr_p);
8756 /* These expressions should already be in gimple IR form. */
8757 gcc_assert (code != MODIFY_EXPR
8758 && code != ASM_EXPR
8759 && code != BIND_EXPR
8760 && code != CATCH_EXPR
8761 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8762 && code != EH_FILTER_EXPR
8763 && code != GOTO_EXPR
8764 && code != LABEL_EXPR
8765 && code != LOOP_EXPR
8766 && code != SWITCH_EXPR
8767 && code != TRY_FINALLY_EXPR
8768 && code != OACC_PARALLEL
8769 && code != OACC_KERNELS
8770 && code != OACC_DATA
8771 && code != OACC_HOST_DATA
8772 && code != OACC_DECLARE
8773 && code != OACC_UPDATE
8774 && code != OACC_ENTER_DATA
8775 && code != OACC_EXIT_DATA
8776 && code != OACC_CACHE
8777 && code != OMP_CRITICAL
8778 && code != OMP_FOR
8779 && code != OACC_LOOP
8780 && code != OMP_MASTER
8781 && code != OMP_TASKGROUP
8782 && code != OMP_ORDERED
8783 && code != OMP_PARALLEL
8784 && code != OMP_SECTIONS
8785 && code != OMP_SECTION
8786 && code != OMP_SINGLE);
8788 #endif
8790 /* Otherwise we're gimplifying a subexpression, so the resulting
8791 value is interesting. If it's a valid operand that matches
8792 GIMPLE_TEST_F, we're done. Unless we are handling some
8793 post-effects internally; if that's the case, we need to copy into
8794 a temporary before adding the post-effects to POST_P. */
8795 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8796 goto out;
8798 /* Otherwise, we need to create a new temporary for the gimplified
8799 expression. */
8801 /* We can't return an lvalue if we have an internal postqueue. The
8802 object the lvalue refers to would (probably) be modified by the
8803 postqueue; we need to copy the value out first, which means an
8804 rvalue. */
8805 if ((fallback & fb_lvalue)
8806 && gimple_seq_empty_p (internal_post)
8807 && is_gimple_addressable (*expr_p))
8809 /* An lvalue will do. Take the address of the expression, store it
8810 in a temporary, and replace the expression with an INDIRECT_REF of
8811 that temporary. */
8812 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8813 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8814 *expr_p = build_simple_mem_ref (tmp);
8816 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8818 /* An rvalue will do. Assign the gimplified expression into a
8819 new temporary TMP and replace the original expression with
8820 TMP. First, make sure that the expression has a type so that
8821 it can be assigned into a temporary. */
8822 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8823 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8825 else
8827 #ifdef ENABLE_GIMPLE_CHECKING
8828 if (!(fallback & fb_mayfail))
8830 fprintf (stderr, "gimplification failed:\n");
8831 print_generic_expr (stderr, *expr_p, 0);
8832 debug_tree (*expr_p);
8833 internal_error ("gimplification failed");
8835 #endif
8836 gcc_assert (fallback & fb_mayfail);
8838 /* If this is an asm statement, and the user asked for the
8839 impossible, don't die. Fail and let gimplify_asm_expr
8840 issue an error. */
8841 ret = GS_ERROR;
8842 goto out;
8845 /* Make sure the temporary matches our predicate. */
8846 gcc_assert ((*gimple_test_f) (*expr_p));
8848 if (!gimple_seq_empty_p (internal_post))
8850 annotate_all_with_location (internal_post, input_location);
8851 gimplify_seq_add_seq (pre_p, internal_post);
8854 out:
8855 input_location = saved_location;
8856 return ret;
8859 /* Look through TYPE for variable-sized objects and gimplify each such
8860 size that we find. Add to LIST_P any statements generated. */
8862 void
8863 gimplify_type_sizes (tree type, gimple_seq *list_p)
8865 tree field, t;
8867 if (type == NULL || type == error_mark_node)
8868 return;
8870 /* We first do the main variant, then copy into any other variants. */
8871 type = TYPE_MAIN_VARIANT (type);
8873 /* Avoid infinite recursion. */
8874 if (TYPE_SIZES_GIMPLIFIED (type))
8875 return;
8877 TYPE_SIZES_GIMPLIFIED (type) = 1;
8879 switch (TREE_CODE (type))
8881 case INTEGER_TYPE:
8882 case ENUMERAL_TYPE:
8883 case BOOLEAN_TYPE:
8884 case REAL_TYPE:
8885 case FIXED_POINT_TYPE:
8886 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8887 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8889 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8891 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8892 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8894 break;
8896 case ARRAY_TYPE:
8897 /* These types may not have declarations, so handle them here. */
8898 gimplify_type_sizes (TREE_TYPE (type), list_p);
8899 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8900 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8901 with assigned stack slots, for -O1+ -g they should be tracked
8902 by VTA. */
8903 if (!(TYPE_NAME (type)
8904 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8905 && DECL_IGNORED_P (TYPE_NAME (type)))
8906 && TYPE_DOMAIN (type)
8907 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8909 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8910 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8911 DECL_IGNORED_P (t) = 0;
8912 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8913 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8914 DECL_IGNORED_P (t) = 0;
8916 break;
8918 case RECORD_TYPE:
8919 case UNION_TYPE:
8920 case QUAL_UNION_TYPE:
8921 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8922 if (TREE_CODE (field) == FIELD_DECL)
8924 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8925 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8926 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8927 gimplify_type_sizes (TREE_TYPE (field), list_p);
8929 break;
8931 case POINTER_TYPE:
8932 case REFERENCE_TYPE:
8933 /* We used to recurse on the pointed-to type here, which turned out to
8934 be incorrect because its definition might refer to variables not
8935 yet initialized at this point if a forward declaration is involved.
8937 It was actually useful for anonymous pointed-to types to ensure
8938 that the sizes evaluation dominates every possible later use of the
8939 values. Restricting to such types here would be safe since there
8940 is no possible forward declaration around, but would introduce an
8941 undesirable middle-end semantic to anonymity. We then defer to
8942 front-ends the responsibility of ensuring that the sizes are
8943 evaluated both early and late enough, e.g. by attaching artificial
8944 type declarations to the tree. */
8945 break;
8947 default:
8948 break;
8951 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8952 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8954 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8956 TYPE_SIZE (t) = TYPE_SIZE (type);
8957 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8958 TYPE_SIZES_GIMPLIFIED (t) = 1;
8962 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8963 a size or position, has had all of its SAVE_EXPRs evaluated.
8964 We add any required statements to *STMT_P. */
8966 void
8967 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8969 tree expr = *expr_p;
8971 /* We don't do anything if the value isn't there, is constant, or contains
8972 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8973 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8974 will want to replace it with a new variable, but that will cause problems
8975 if this type is from outside the function. It's OK to have that here. */
8976 if (is_gimple_sizepos (expr))
8977 return;
8979 *expr_p = unshare_expr (expr);
8981 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8984 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8985 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8986 is true, also gimplify the parameters. */
8988 gbind *
8989 gimplify_body (tree fndecl, bool do_parms)
8991 location_t saved_location = input_location;
8992 gimple_seq parm_stmts, seq;
8993 gimple outer_stmt;
8994 gbind *outer_bind;
8995 struct cgraph_node *cgn;
8997 timevar_push (TV_TREE_GIMPLIFY);
8999 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
9000 gimplification. */
9001 default_rtl_profile ();
9003 gcc_assert (gimplify_ctxp == NULL);
9004 push_gimplify_context ();
9006 if (flag_openacc || flag_openmp)
9008 gcc_assert (gimplify_omp_ctxp == NULL);
9009 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
9010 gimplify_omp_ctxp
9011 = new_omp_context ((enum omp_region_type) (ORT_TARGET
9012 | ORT_TARGET_OFFLOAD));
9015 /* Unshare most shared trees in the body and in that of any nested functions.
9016 It would seem we don't have to do this for nested functions because
9017 they are supposed to be output and then the outer function gimplified
9018 first, but the g++ front end doesn't always do it that way. */
9019 unshare_body (fndecl);
9020 unvisit_body (fndecl);
9022 cgn = cgraph_node::get (fndecl);
9023 if (cgn && cgn->origin)
9024 nonlocal_vlas = new hash_set<tree>;
9026 /* Make sure input_location isn't set to something weird. */
9027 input_location = DECL_SOURCE_LOCATION (fndecl);
9029 /* Resolve callee-copies. This has to be done before processing
9030 the body so that DECL_VALUE_EXPR gets processed correctly. */
9031 parm_stmts = do_parms ? gimplify_parameters () : NULL;
9033 /* Gimplify the function's body. */
9034 seq = NULL;
9035 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
9036 outer_stmt = gimple_seq_first_stmt (seq);
9037 if (!outer_stmt)
9039 outer_stmt = gimple_build_nop ();
9040 gimplify_seq_add_stmt (&seq, outer_stmt);
9043 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
9044 not the case, wrap everything in a GIMPLE_BIND to make it so. */
9045 if (gimple_code (outer_stmt) == GIMPLE_BIND
9046 && gimple_seq_first (seq) == gimple_seq_last (seq))
9047 outer_bind = as_a <gbind *> (outer_stmt);
9048 else
9049 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
9051 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9053 /* If we had callee-copies statements, insert them at the beginning
9054 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
9055 if (!gimple_seq_empty_p (parm_stmts))
9057 tree parm;
9059 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
9060 gimple_bind_set_body (outer_bind, parm_stmts);
9062 for (parm = DECL_ARGUMENTS (current_function_decl);
9063 parm; parm = DECL_CHAIN (parm))
9064 if (DECL_HAS_VALUE_EXPR_P (parm))
9066 DECL_HAS_VALUE_EXPR_P (parm) = 0;
9067 DECL_IGNORED_P (parm) = 0;
9071 if (nonlocal_vlas)
9073 if (nonlocal_vla_vars)
9075 /* tree-nested.c may later on call declare_vars (..., true);
9076 which relies on BLOCK_VARS chain to be the tail of the
9077 gimple_bind_vars chain. Ensure we don't violate that
9078 assumption. */
9079 if (gimple_bind_block (outer_bind)
9080 == DECL_INITIAL (current_function_decl))
9081 declare_vars (nonlocal_vla_vars, outer_bind, true);
9082 else
9083 BLOCK_VARS (DECL_INITIAL (current_function_decl))
9084 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
9085 nonlocal_vla_vars);
9086 nonlocal_vla_vars = NULL_TREE;
9088 delete nonlocal_vlas;
9089 nonlocal_vlas = NULL;
9092 if ((flag_openacc || flag_openmp || flag_openmp_simd)
9093 && gimplify_omp_ctxp)
9095 delete_omp_context (gimplify_omp_ctxp);
9096 gimplify_omp_ctxp = NULL;
9099 pop_gimplify_context (outer_bind);
9100 gcc_assert (gimplify_ctxp == NULL);
9102 #ifdef ENABLE_CHECKING
9103 if (!seen_error ())
9104 verify_gimple_in_seq (gimple_bind_body (outer_bind));
9105 #endif
9107 timevar_pop (TV_TREE_GIMPLIFY);
9108 input_location = saved_location;
9110 return outer_bind;
9113 typedef char *char_p; /* For DEF_VEC_P. */
9115 /* Return whether we should exclude FNDECL from instrumentation. */
9117 static bool
9118 flag_instrument_functions_exclude_p (tree fndecl)
9120 vec<char_p> *v;
9122 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
9123 if (v && v->length () > 0)
9125 const char *name;
9126 int i;
9127 char *s;
9129 name = lang_hooks.decl_printable_name (fndecl, 0);
9130 FOR_EACH_VEC_ELT (*v, i, s)
9131 if (strstr (name, s) != NULL)
9132 return true;
9135 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
9136 if (v && v->length () > 0)
9138 const char *name;
9139 int i;
9140 char *s;
9142 name = DECL_SOURCE_FILE (fndecl);
9143 FOR_EACH_VEC_ELT (*v, i, s)
9144 if (strstr (name, s) != NULL)
9145 return true;
9148 return false;
9151 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
9152 node for the function we want to gimplify.
9154 Return the sequence of GIMPLE statements corresponding to the body
9155 of FNDECL. */
9157 void
9158 gimplify_function_tree (tree fndecl)
9160 tree parm, ret;
9161 gimple_seq seq;
9162 gbind *bind;
9164 gcc_assert (!gimple_body (fndecl));
9166 if (DECL_STRUCT_FUNCTION (fndecl))
9167 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
9168 else
9169 push_struct_function (fndecl);
9171 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
9173 /* Preliminarily mark non-addressed complex variables as eligible
9174 for promotion to gimple registers. We'll transform their uses
9175 as we find them. */
9176 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
9177 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
9178 && !TREE_THIS_VOLATILE (parm)
9179 && !needs_to_live_in_memory (parm))
9180 DECL_GIMPLE_REG_P (parm) = 1;
9183 ret = DECL_RESULT (fndecl);
9184 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
9185 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
9186 && !needs_to_live_in_memory (ret))
9187 DECL_GIMPLE_REG_P (ret) = 1;
9189 bind = gimplify_body (fndecl, true);
9191 /* The tree body of the function is no longer needed, replace it
9192 with the new GIMPLE body. */
9193 seq = NULL;
9194 gimple_seq_add_stmt (&seq, bind);
9195 gimple_set_body (fndecl, seq);
9197 /* If we're instrumenting function entry/exit, then prepend the call to
9198 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
9199 catch the exit hook. */
9200 /* ??? Add some way to ignore exceptions for this TFE. */
9201 if (flag_instrument_function_entry_exit
9202 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
9203 && !flag_instrument_functions_exclude_p (fndecl))
9205 tree x;
9206 gbind *new_bind;
9207 gimple tf;
9208 gimple_seq cleanup = NULL, body = NULL;
9209 tree tmp_var;
9210 gcall *call;
9212 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9213 call = gimple_build_call (x, 1, integer_zero_node);
9214 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9215 gimple_call_set_lhs (call, tmp_var);
9216 gimplify_seq_add_stmt (&cleanup, call);
9217 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
9218 call = gimple_build_call (x, 2,
9219 build_fold_addr_expr (current_function_decl),
9220 tmp_var);
9221 gimplify_seq_add_stmt (&cleanup, call);
9222 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
9224 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9225 call = gimple_build_call (x, 1, integer_zero_node);
9226 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9227 gimple_call_set_lhs (call, tmp_var);
9228 gimplify_seq_add_stmt (&body, call);
9229 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
9230 call = gimple_build_call (x, 2,
9231 build_fold_addr_expr (current_function_decl),
9232 tmp_var);
9233 gimplify_seq_add_stmt (&body, call);
9234 gimplify_seq_add_stmt (&body, tf);
9235 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
9236 /* Clear the block for BIND, since it is no longer directly inside
9237 the function, but within a try block. */
9238 gimple_bind_set_block (bind, NULL);
9240 /* Replace the current function body with the body
9241 wrapped in the try/finally TF. */
9242 seq = NULL;
9243 gimple_seq_add_stmt (&seq, new_bind);
9244 gimple_set_body (fndecl, seq);
9247 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9248 cfun->curr_properties = PROP_gimple_any;
9250 pop_cfun ();
9253 /* Return a dummy expression of type TYPE in order to keep going after an
9254 error. */
9256 static tree
9257 dummy_object (tree type)
9259 tree t = build_int_cst (build_pointer_type (type), 0);
9260 return build2 (MEM_REF, type, t, t);
9263 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9264 builtin function, but a very special sort of operator. */
9266 enum gimplify_status
9267 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9269 tree promoted_type, have_va_type;
9270 tree valist = TREE_OPERAND (*expr_p, 0);
9271 tree type = TREE_TYPE (*expr_p);
9272 tree t;
9273 location_t loc = EXPR_LOCATION (*expr_p);
9275 /* Verify that valist is of the proper type. */
9276 have_va_type = TREE_TYPE (valist);
9277 if (have_va_type == error_mark_node)
9278 return GS_ERROR;
9279 have_va_type = targetm.canonical_va_list_type (have_va_type);
9281 if (have_va_type == NULL_TREE)
9283 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9284 return GS_ERROR;
9287 /* Generate a diagnostic for requesting data of a type that cannot
9288 be passed through `...' due to type promotion at the call site. */
9289 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9290 != type)
9292 static bool gave_help;
9293 bool warned;
9295 /* Unfortunately, this is merely undefined, rather than a constraint
9296 violation, so we cannot make this an error. If this call is never
9297 executed, the program is still strictly conforming. */
9298 warned = warning_at (loc, 0,
9299 "%qT is promoted to %qT when passed through %<...%>",
9300 type, promoted_type);
9301 if (!gave_help && warned)
9303 gave_help = true;
9304 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9305 promoted_type, type);
9308 /* We can, however, treat "undefined" any way we please.
9309 Call abort to encourage the user to fix the program. */
9310 if (warned)
9311 inform (loc, "if this code is reached, the program will abort");
9312 /* Before the abort, allow the evaluation of the va_list
9313 expression to exit or longjmp. */
9314 gimplify_and_add (valist, pre_p);
9315 t = build_call_expr_loc (loc,
9316 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9317 gimplify_and_add (t, pre_p);
9319 /* This is dead code, but go ahead and finish so that the
9320 mode of the result comes out right. */
9321 *expr_p = dummy_object (type);
9322 return GS_ALL_DONE;
9324 else
9326 /* Make it easier for the backends by protecting the valist argument
9327 from multiple evaluations. */
9328 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9330 /* For this case, the backends will be expecting a pointer to
9331 TREE_TYPE (abi), but it's possible we've
9332 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9333 So fix it. */
9334 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9336 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9337 valist = fold_convert_loc (loc, p1,
9338 build_fold_addr_expr_loc (loc, valist));
9341 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9343 else
9344 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9346 if (!targetm.gimplify_va_arg_expr)
9347 /* FIXME: Once most targets are converted we should merely
9348 assert this is non-null. */
9349 return GS_ALL_DONE;
9351 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9352 return GS_OK;
9356 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9358 DST/SRC are the destination and source respectively. You can pass
9359 ungimplified trees in DST or SRC, in which case they will be
9360 converted to a gimple operand if necessary.
9362 This function returns the newly created GIMPLE_ASSIGN tuple. */
9364 gimple
9365 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9367 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9368 gimplify_and_add (t, seq_p);
9369 ggc_free (t);
9370 return gimple_seq_last_stmt (*seq_p);
9373 inline hashval_t
9374 gimplify_hasher::hash (const value_type *p)
9376 tree t = p->val;
9377 return iterative_hash_expr (t, 0);
9380 inline bool
9381 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9383 tree t1 = p1->val;
9384 tree t2 = p2->val;
9385 enum tree_code code = TREE_CODE (t1);
9387 if (TREE_CODE (t2) != code
9388 || TREE_TYPE (t1) != TREE_TYPE (t2))
9389 return false;
9391 if (!operand_equal_p (t1, t2, 0))
9392 return false;
9394 #ifdef ENABLE_CHECKING
9395 /* Only allow them to compare equal if they also hash equal; otherwise
9396 results are nondeterminate, and we fail bootstrap comparison. */
9397 gcc_assert (hash (p1) == hash (p2));
9398 #endif
9400 return true;